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; | ||
32 | QString identifier; | ||||
31 | }; | 33 | }; | ||
32 | 34 | | |||
33 | PotdProvider::PotdProvider( QObject *parent, const QVariantList &args ) | 35 | PotdProvider::PotdProvider( QObject *parent, const QVariantList &args ) | ||
34 | : QObject( parent ), | 36 | : QObject( parent ), | ||
35 | d(new PotdProviderPrivate) | 37 | d(new PotdProviderPrivate) | ||
36 | { | 38 | { | ||
37 | if ( args.count() > 0 ) { | 39 | if ( args.count() > 0 ) { | ||
38 | d->name = args[ 0 ].toString(); | 40 | d->name = args[ 0 ].toString(); | ||
39 | 41 | | |||
40 | if ( args.count() > 1 && args[ 1 ].canConvert( QVariant::Date ) ) { | 42 | d->identifier = d->name; | ||
41 | d->date = args[ 1 ].toDate(); | 43 | | ||
44 | QRegularExpression re(QStringLiteral("\\d{4}-\\d{2}-\\d{2}")); | ||||
45 | if ( args.count() > 1 ) { | ||||
46 | for (int i = 1; i < args.count(); i++) { | ||||
47 | d->identifier += QStringLiteral(":") + args[i].toString(); | ||||
48 | if (re.match(args[ i ].toString()).hasMatch()) { | ||||
49 | QDate date = QDate::fromString(args[ i ].toString()); | ||||
50 | if (date.isValid()) { | ||||
51 | d->date = date; | ||||
52 | } | ||||
53 | } | ||||
54 | } | ||||
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… | |||||
42 | } | 55 | } | ||
43 | } else { | 56 | } else { | ||
44 | d->name = QStringLiteral("Unknown"); | 57 | d->name = QStringLiteral("Unknown"); | ||
58 | d->identifier = d->name; | ||||
45 | } | 59 | } | ||
46 | } | 60 | } | ||
47 | 61 | | |||
48 | PotdProvider::~PotdProvider() | 62 | PotdProvider::~PotdProvider() | ||
49 | { | 63 | { | ||
50 | } | 64 | } | ||
51 | 65 | | |||
52 | QString PotdProvider::name() const | 66 | QString PotdProvider::name() const | ||
53 | { | 67 | { | ||
54 | return d->name; | 68 | return d->name; | ||
55 | } | 69 | } | ||
56 | 70 | | |||
57 | QDate PotdProvider::date() const | 71 | QDate PotdProvider::date() const | ||
58 | { | 72 | { | ||
59 | return d->date.isNull() ? QDate::currentDate() : d->date; | 73 | return d->date.isNull() ? QDate::currentDate() : d->date; | ||
60 | } | 74 | } | ||
61 | 75 | | |||
62 | bool PotdProvider::isFixedDate() const | 76 | bool PotdProvider::isFixedDate() const | ||
63 | { | 77 | { | ||
64 | return !d->date.isNull(); | 78 | return !d->date.isNull(); | ||
65 | } | 79 | } | ||
66 | 80 | | |||
67 | QString PotdProvider::identifier() const | 81 | QString PotdProvider::identifier() const | ||
68 | { | 82 | { | ||
69 | if (isFixedDate()) { | 83 | return d->identifier; | ||
70 | return d->name + QLatin1Char(':') + d->date.toString(Qt::ISODate); | | |||
71 | } | | |||
72 | | ||||
73 | return d->name; | | |||
74 | } | 84 | } | ||
75 | 85 | | |||
76 | 86 | |
The lack of error handling and else blocks for all the ifs in this new code makes me feel a bit nervous.