]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphinitemcategorizer.cpp
Allow to sort by rating, which can be quite useful in combination with the "Show...
[dolphin.git] / src / dolphinitemcategorizer.cpp
index 0f55881c43183e6e0725e69fca1919622b97b9f9..7dc19ab92fded0020ea3ee789012bf93c4729479 100644 (file)
 
 #include "dolphinview.h"
 
-#include <klocale.h>
+#ifdef HAVE_NEPOMUK
+#include <config-nepomuk.h>
+#include <nepomuk/global.h>
+#include <nepomuk/resource.h>
+#endif
+
+#include <kdatetime.h>
 #include <kdirmodel.h>
+#include <kfileitem.h>
+#include <klocale.h>
+#include <kurl.h>
 
 #include <QtGui/QSortFilterProxyModel>
 
@@ -46,25 +55,24 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
         return retString;
     }
 
-    // KDirModel checks columns to know to which role are
-    // we talking about
-    QModelIndex theIndex = index.model()->index(index.row(),
-                                                sortRole,
-                                                index.parent());
-
-    if (!theIndex.isValid()) {
-        return retString;
-    }
-
-    QVariant data = theIndex.model()->data(theIndex, Qt::DisplayRole);
-
     const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
     KFileItem *item = dirModel->itemForIndex(index);
-    int fileSize;
 
     switch (sortRole)
     {
-        case KDirModel::Name:
+        case DolphinView::SortByName:
+        {
+            // KDirModel checks columns to know to which role are
+            // we talking about
+            QModelIndex theIndex = index.model()->index(index.row(),
+                                                        KDirModel::Name,
+                                                        index.parent());
+
+            if (!theIndex.isValid()) {
+                return retString;
+            }
+
+            QVariant data = theIndex.model()->data(theIndex, Qt::DisplayRole);
             if (data.toString().size())
             {
                 if (!item->isHidden() && data.toString().at(0).isLetter())
@@ -80,12 +88,64 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
                 else if (item->isHidden())
                     retString = data.toString().toUpper().at(0);
                 else
-                    retString = i18n("Others");
+                {
+                    bool validCategory = false;
+
+                    const QString str(data.toString().toUpper());
+                    const QChar* currA = str.unicode();
+                    while (!currA->isNull() && !validCategory) {
+                        if (currA->isLetter())
+                            validCategory = true;
+                        else if (currA->isDigit())
+                            return i18n("Others");
+                        else
+                            ++currA;
+                    }
+
+                    if (!validCategory)
+                        retString = i18n("Others");
+                    else
+                        retString = *currA;
+                }
             }
             break;
+        }
+
+        case DolphinView::SortByDate:
+        {
+            KDateTime modifiedTime;
+            modifiedTime.setTime_t(item->time(KIO::UDS_MODIFICATION_TIME));
+            modifiedTime = modifiedTime.toLocalZone();
+
+            if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 0)
+                retString = i18n("Today");
+            else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 1)
+                retString = i18n("Yesterday");
+            else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 7)
+                retString = i18n("Less than a week");
+            else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 31)
+                retString = i18n("Less than a month");
+            else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 365)
+                retString = i18n("Less than a year");
+            else
+                retString = i18n("More than a year");
+            break;
+        }
+
+        case DolphinView::SortByPermissions:
+            retString = item->permissionsString();
+            break;
 
-        case KDirModel::Size:
-            fileSize = (item) ? item->size() : -1;
+        case DolphinView::SortByOwner:
+            retString = item->user();
+            break;
+
+        case DolphinView::SortByGroup:
+            retString = item->group();
+            break;
+
+        case DolphinView::SortBySize: {
+            const int fileSize = item ? item->size() : -1;
             if (item && item->isDir()) {
                 retString = i18n("Folders");
             } else if (fileSize < 5242880) {
@@ -96,10 +156,25 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
                 retString = i18nc("Size", "Big");
             }
             break;
+        }
 
-        case KDirModel::Type:
+        case DolphinView::SortByType:
             retString = item->mimeComment();
             break;
+
+#ifdef HAVE_NEPOMUK
+        case DolphinView::SortByRating: {
+            KFileItem* item = dirModel->itemForIndex(index);
+            if (item != 0) {
+                const Nepomuk::Resource resource(item->url().url(), Nepomuk::NFO::File());
+                const quint32 rating = resource.rating();
+                retString = i18np("1 star", "%1 stars", rating);
+            }
+            break;
+        }
+        case DolphinView::SortByTags:
+            break;
+#endif
     }
 
     return retString;