Changeset View
Changeset View
Standalone View
Standalone View
dataengines/potd/potdprovider.cpp
Show All 15 Lines | |||||
16 | * Free Software Foundation, Inc., | 16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "potdprovider.h" | 20 | #include "potdprovider.h" | ||
21 | 21 | | |||
22 | // Qt | 22 | // Qt | ||
23 | #include <QDate> | 23 | #include <QDate> | ||
24 | #include <QRegularExpression> | ||||
24 | 25 | | |||
25 | 26 | | |||
26 | class PotdProviderPrivate | 27 | class PotdProviderPrivate | ||
27 | { | 28 | { | ||
28 | public: | 29 | public: | ||
29 | QString name; | 30 | QString name; | ||
30 | QDate date; | 31 | QDate date; | ||
31 | }; | 32 | }; | ||
32 | 33 | | |||
33 | PotdProvider::PotdProvider( QObject *parent, const QVariantList &args ) | 34 | PotdProvider::PotdProvider( QObject *parent, const QVariantList &args ) | ||
34 | : QObject( parent ), | 35 | : QObject( parent ), | ||
35 | d(new PotdProviderPrivate) | 36 | d(new PotdProviderPrivate) | ||
36 | { | 37 | { | ||
37 | if ( args.count() > 0 ) { | 38 | if ( args.count() > 0 ) { | ||
38 | d->name = args[ 0 ].toString(); | 39 | d->name = args[ 0 ].toString(); | ||
39 | 40 | | |||
40 | if ( args.count() > 1 && args[ 1 ].canConvert( QVariant::Date ) ) { | 41 | QRegularExpression re(QStringLiteral("\\d{4}-\\d{2}-\\d{2}")); | ||
41 | d->date = args[ 1 ].toDate(); | 42 | if ( args.count() > 1 ) { | ||
43 | for (int i = 1; i < args.count(); i++) { | ||||
44 | if (re.match(args[ i ].toString()).hasMatch()) { | ||||
45 | QDate date = QDate::fromString(args[ i ].toString()); | ||||
46 | if (date.isValid()) { | ||||
47 | d->date = date; | ||||
48 | } | ||||
49 | } | ||||
50 | } | ||||
42 | } | 51 | } | ||
ngraham: The lack of error handling and else blocks for all the ifs in this new code makes me feel a bit… | |||||
date is optional, so if it not found, just leave it as undefined. I can add some detection to toString() function, in case they passed some non-string args. mach() should be safe. guoyunhe: `date` is optional, so if it not found, just leave it as undefined. I can add some detection to… | |||||
Read Qt documentation that QVariant::toString() will always return a string even if the type is not supported (empty string). So this code won't produce any error/exception. guoyunhe: Read Qt documentation that `QVariant::toString()` will always return a string even if the type… | |||||
43 | } else { | 52 | } else { | ||
44 | d->name = QStringLiteral("Unknown"); | 53 | d->name = QStringLiteral("Unknown"); | ||
45 | } | 54 | } | ||
46 | } | 55 | } | ||
47 | 56 | | |||
48 | PotdProvider::~PotdProvider() | 57 | PotdProvider::~PotdProvider() | ||
49 | { | 58 | { | ||
50 | } | 59 | } | ||
Show All 26 Lines |
The lack of error handling and else blocks for all the ifs in this new code makes me feel a bit nervous.