}
}
- return groups;
+ if (withString) {
+ char const *groupNames[] = {"Folders", "Small", "Medium", "Big"};
+ groupInfo.text = i18nc("@title:group Size", groupNames[groupInfo.comparable]);
+ }
+ return groupInfo;
}
-QList<QPair<int, QVariant>> KFileItemModel::timeRoleGroups(const std::function<QDateTime(const ItemData *)> &fileTimeCb) const
+KFileItemModel::ItemGroupInfo
+KFileItemModel::timeRoleGroup(const std::function<QDateTime(const ItemData *)> &fileTimeCb, const ItemData *itemData, bool withString) const
{
- Q_ASSERT(!m_itemData.isEmpty());
-
- const int maxIndex = count() - 1;
- QList<QPair<int, QVariant>> groups;
+ static bool oldWithString;
+ static ItemGroupInfo oldGroupInfo;
+ static QDate oldFileDate;
+ ItemGroupInfo groupInfo;
const QDate currentDate = QDate::currentDate();
- const QDateTime fileTime = fileTimeCb(itemData);
- const QDate fileDate = fileTime.date();
- const int daysDistance = fileDate.daysTo(currentDate);
+
+ QDate previousFileDate;
+ QString groupValue;
+ for (int i = 0; i <= maxIndex; ++i) {
+ if (isChildItem(i)) {
+ continue;
+ }
+
+ const QLocale locale;
+ const QDateTime fileTime = fileTimeCb(m_itemData.at(i));
+ const QDate fileDate = fileTime.date();
+ if (fileDate == previousFileDate) {
+ // The current item is in the same group as the previous item
+ continue;
+ }
+ previousFileDate = fileDate;
+
+ const int daysDistance = fileDate.daysTo(currentDate);
- QString newGroupValue;
+ if (fileDate == oldFileDate && withString == oldWithString) {
+ return oldGroupInfo;
+ }
+ // Simplified grouping algorithm, preserving dates
+ // but not taking "pretty printing" into account
+ if (currentDate.year() == fileDate.year() && currentDate.month() == fileDate.month()) {
+ if (daysDistance < 7) {
+ groupInfo.comparable = daysDistance; // Today, Yesterday and week days
+ } else if (daysDistance < 14) {
+ groupInfo.comparable = 10; // One Week Ago
+ } else if (daysDistance < 21) {
+ groupInfo.comparable = 20; // Two Weeks Ago
+ } else if (daysDistance < 28) {
+ groupInfo.comparable = 30; // Three Weeks Ago
+ } else {
+ groupInfo.comparable = 40; // Earlier This Month
+ }
+ } else {
+ const QDate lastMonthDate = currentDate.addMonths(-1);
+ if (lastMonthDate.year() == fileDate.year() && lastMonthDate.month() == fileDate.month()) {
+ if (daysDistance < 7) {
+ groupInfo.comparable = daysDistance; // Today, Yesterday and week days (Month, Year)
+ } else if (daysDistance < 14) {
+ groupInfo.comparable = 11; // One Week Ago (Month, Year)
+ } else if (daysDistance < 21) {
+ groupInfo.comparable = 21; // Two Weeks Ago (Month, Year)
+ } else if (daysDistance < 28) {
+ groupInfo.comparable = 31; // Three Weeks Ago (Month, Year)
+ } else {
+ groupInfo.comparable = 41; // Earlier on Month, Year
+ }
+ } else {
+ // The trick will fail for dates past April, 178956967 or before 1 AD.
+ groupInfo.comparable = 2147483647 - (fileDate.year() * 12 + fileDate.month() - 1); // Month, Year; newer < older
+ }
+ }
+ if (withString) {
if (currentDate.year() == fileDate.year() && currentDate.month() == fileDate.month()) {
switch (daysDistance / 7) {
case 0:
switch (daysDistance) {
case 0:
- newGroupValue = i18nc("@title:group Date", "Today");
+ groupInfo.text = i18nc("@title:group Date", "Today");
break;
case 1:
- newGroupValue = i18nc("@title:group Date", "Yesterday");
+ groupInfo.text = i18nc("@title:group Date", "Yesterday");
break;
default:
- groupInfo.text = fileTime.toString(i18nc("@title:group Date: The week day name: dddd", "dddd"));
- groupInfo.text = i18nc(
+ newGroupValue = locale.toString(fileTime, i18nc("@title:group Date: The week day name: dddd", "dddd"));
+ newGroupValue = i18nc(
"Can be used to script translation of \"dddd\""
"with context @title:group Date",
"%1",
qCWarning(DolphinDebug).nospace()
<< "A wrong translation was found: " << translatedFormat << ". Please file a bug report at bugs.kde.org";
const QString untranslatedFormat = format.toString({QLatin1String("en_US")});
- groupInfo.text = fileTime.toString(untranslatedFormat);
+ newGroupValue = locale.toString(fileTime, untranslatedFormat);
}
- } else if (daysDistance <= 7 * 3) {
+ } else if (daysDistance < 7 * 3) {
const KLocalizedString format = ki18nc(
"@title:group Date: "
"MMMM is full month name in current locale, and yyyy is "
qCWarning(DolphinDebug).nospace()
<< "A wrong translation was found: " << translatedFormat << ". Please file a bug report at bugs.kde.org";
const QString untranslatedFormat = format.toString({QLatin1String("en_US")});
- groupInfo.text = fileTime.toString(untranslatedFormat);
+ newGroupValue = locale.toString(fileTime, untranslatedFormat);
}
- } else if (daysDistance <= 7 * 4) {
+ } else if (daysDistance < 7 * 4) {
const KLocalizedString format = ki18nc(
"@title:group Date: "
"MMMM is full month name in current locale, and yyyy is "