Changeset View
Changeset View
Standalone View
Standalone View
src/kioslaves/timeline/timelinetools.cpp
Show All 18 Lines | 1 | /* | |||
---|---|---|---|---|---|
19 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | 19 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #include "timelinetools.h" | 22 | #include "timelinetools.h" | ||
23 | #include "kio_timeline_debug.h" | 23 | #include "kio_timeline_debug.h" | ||
24 | 24 | | |||
25 | #include <QUrl> | 25 | #include <QUrl> | ||
26 | #include <QDate> | 26 | #include <QDate> | ||
27 | #include <QRegExp> | 27 | #include <QRegularExpression> | ||
28 | #include <QUrlQuery> | 28 | #include <QUrlQuery> | ||
29 | 29 | | |||
30 | 30 | | |||
31 | namespace | 31 | namespace | ||
32 | { | 32 | { | ||
33 | QDate applyRelativeDateModificators(const QDate& date, const QMap<QString, QString>& modificators) | 33 | QDate applyRelativeDateModificators(const QDate& date, const QMap<QString, QString>& modificators) | ||
34 | { | 34 | { | ||
35 | QDate newDate(date); | 35 | QDate newDate(date); | ||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Line(s) | 70 | QUrl Baloo::canonicalizeTimelineUrl(const QUrl& url) { | |||
85 | } | 85 | } | ||
86 | return newUrl; | 86 | return newUrl; | ||
87 | } | 87 | } | ||
88 | 88 | | |||
89 | Baloo::TimelineFolderType Baloo::parseTimelineUrl(const QUrl& url, QDate* date, QString* filename) | 89 | Baloo::TimelineFolderType Baloo::parseTimelineUrl(const QUrl& url, QDate* date, QString* filename) | ||
90 | { | 90 | { | ||
91 | qCDebug(KIO_TIMELINE) << url; | 91 | qCDebug(KIO_TIMELINE) << url; | ||
92 | 92 | | |||
93 | static QRegExp s_dateRegexp(QStringLiteral("\\d{4}-\\d{2}(?:-(\\d{2}))?")); | 93 | static const QRegularExpression s_dateRegexp( | ||
94 | QRegularExpression::anchoredPattern(QStringLiteral("\\d{4}-\\d{2}(?:-(\\d{2}))?"))); | ||||
94 | 95 | | |||
95 | // reset | 96 | // reset | ||
96 | *date = QDate(); | 97 | *date = QDate(); | ||
97 | 98 | | |||
98 | QString path = url.path(); | 99 | QString path = url.path(); | ||
99 | if (path.endsWith(QLatin1Char('/'))) | 100 | if (path.endsWith(QLatin1Char('/'))) | ||
100 | path.chop(1); | 101 | path.chop(1); | ||
101 | 102 | | |||
102 | if (path.isEmpty()) { | 103 | if (path.isEmpty()) { | ||
103 | qCDebug(KIO_TIMELINE) << url << "is root folder"; | 104 | qCDebug(KIO_TIMELINE) << url << "is root folder"; | ||
104 | return RootFolder; | 105 | return RootFolder; | ||
105 | } else if (path.startsWith(QLatin1String("/today"))) { | 106 | } else if (path.startsWith(QLatin1String("/today"))) { | ||
106 | *date = QDate::currentDate(); | 107 | *date = QDate::currentDate(); | ||
107 | if (filename) | 108 | if (filename) | ||
108 | *filename = path.mid(7); | 109 | *filename = path.mid(7); | ||
109 | qCDebug(KIO_TIMELINE) << url << "is today folder:" << *date; | 110 | qCDebug(KIO_TIMELINE) << url << "is today folder:" << *date; | ||
110 | return DayFolder; | 111 | return DayFolder; | ||
111 | } else if (path == QLatin1String("/calendar")) { | 112 | } else if (path == QLatin1String("/calendar")) { | ||
112 | qCDebug(KIO_TIMELINE) << url << "is calendar folder"; | 113 | qCDebug(KIO_TIMELINE) << url << "is calendar folder"; | ||
113 | return CalendarFolder; | 114 | return CalendarFolder; | ||
114 | } else { | 115 | } else { | ||
115 | QStringList sections = path.split(QStringLiteral("/"), QString::SkipEmptyParts); | 116 | QStringList sections = path.split(QStringLiteral("/"), QString::SkipEmptyParts); | ||
116 | QString dateString; | 117 | QString dateString; | ||
117 | if (s_dateRegexp.exactMatch(sections.last())) { | 118 | QRegularExpressionMatch match = s_dateRegexp.match(sections.last()); | ||
119 | if (match.hasMatch()) { | ||||
bruns: Pleae avoid assigning in an `if (...)` expression, just move it to the previous line. | |||||
118 | dateString = sections.last(); | 120 | dateString = sections.last(); | ||
119 | } else if (sections.count() > 1 && s_dateRegexp.exactMatch(sections[sections.count() - 2])) { | 121 | } else if (sections.count() > 1 | ||
122 | && (match = s_dateRegexp.match(sections[sections.count() - 2])).hasMatch()) { | ||||
120 | dateString = sections[sections.count() - 2]; | 123 | dateString = sections[sections.count() - 2]; | ||
121 | if (filename) | 124 | if (filename) { | ||
122 | *filename = sections.last(); | 125 | *filename = sections.last(); | ||
126 | } | ||||
By moving this outside the else if above, we no longer go to the "else" branch on line 129 if the matching fails. @bruns I think this is actually a good use case for assigning inside an if () expression, as we already did in a number of other port-to-QRegularExpression commits. The alternative is helper functions or lambdas. dfaure: By moving this outside the `else if` above, we no longer go to the "else" branch on line 129 if… | |||||
123 | } else { | 127 | } else { | ||
124 | qCWarning(KIO_TIMELINE) << url << "COULD NOT PARSE"; | 128 | qCWarning(KIO_TIMELINE) << url << "COULD NOT PARSE"; | ||
125 | return NoFolder; | 129 | return NoFolder; | ||
126 | } | 130 | } | ||
127 | 131 | | |||
128 | if (s_dateRegexp.cap(1).isEmpty()) { | 132 | if (match.captured(1).isEmpty()) { | ||
129 | // no day -> month listing | 133 | // no day -> month listing | ||
130 | qCDebug(KIO_TIMELINE) << "parsing " << dateString; | 134 | qCDebug(KIO_TIMELINE) << "parsing " << dateString; | ||
131 | *date = QDate::fromString(dateString, QStringLiteral("yyyy-MM")); | 135 | *date = QDate::fromString(dateString, QStringLiteral("yyyy-MM")); | ||
132 | qCDebug(KIO_TIMELINE) << url << "is month folder:" << date->month() << date->year(); | 136 | qCDebug(KIO_TIMELINE) << url << "is month folder:" << date->month() << date->year(); | ||
133 | if (date->month() > 0 && date->year() > 0) | 137 | if (date->month() > 0 && date->year() > 0) | ||
134 | return MonthFolder; | 138 | return MonthFolder; | ||
135 | } else { | 139 | } else { | ||
136 | qCDebug(KIO_TIMELINE) << "parsing " << dateString; | 140 | qCDebug(KIO_TIMELINE) << "parsing " << dateString; | ||
Show All 18 Lines |
Pleae avoid assigning in an if (...) expression, just move it to the previous line.