]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinitemcategorizer.cpp
Allow to sort by rating, which can be quite useful in combination with the "Show...
[dolphin.git] / src / dolphinitemcategorizer.cpp
1 /**
2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@gmail.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #include "dolphinitemcategorizer.h"
22
23 #include "dolphinview.h"
24
25 #ifdef HAVE_NEPOMUK
26 #include <config-nepomuk.h>
27 #include <nepomuk/global.h>
28 #include <nepomuk/resource.h>
29 #endif
30
31 #include <kdatetime.h>
32 #include <kdirmodel.h>
33 #include <kfileitem.h>
34 #include <klocale.h>
35 #include <kurl.h>
36
37 #include <QtGui/QSortFilterProxyModel>
38
39 DolphinItemCategorizer::DolphinItemCategorizer() :
40 KItemCategorizer()
41 {
42 }
43
44 DolphinItemCategorizer::~DolphinItemCategorizer()
45 {
46 }
47
48 QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
49 int sortRole)
50 {
51 QString retString;
52
53 if (!index.isValid())
54 {
55 return retString;
56 }
57
58 const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
59 KFileItem *item = dirModel->itemForIndex(index);
60
61 switch (sortRole)
62 {
63 case DolphinView::SortByName:
64 {
65 // KDirModel checks columns to know to which role are
66 // we talking about
67 QModelIndex theIndex = index.model()->index(index.row(),
68 KDirModel::Name,
69 index.parent());
70
71 if (!theIndex.isValid()) {
72 return retString;
73 }
74
75 QVariant data = theIndex.model()->data(theIndex, Qt::DisplayRole);
76 if (data.toString().size())
77 {
78 if (!item->isHidden() && data.toString().at(0).isLetter())
79 retString = data.toString().toUpper().at(0);
80 else if (item->isHidden() && data.toString().at(0) == '.' &&
81 data.toString().at(1).isLetter())
82 retString = data.toString().toUpper().at(1);
83 else if (item->isHidden() && data.toString().at(0) == '.' &&
84 !data.toString().at(1).isLetter())
85 retString = i18n("Others");
86 else if (item->isHidden() && data.toString().at(0) != '.')
87 retString = data.toString().toUpper().at(0);
88 else if (item->isHidden())
89 retString = data.toString().toUpper().at(0);
90 else
91 {
92 bool validCategory = false;
93
94 const QString str(data.toString().toUpper());
95 const QChar* currA = str.unicode();
96 while (!currA->isNull() && !validCategory) {
97 if (currA->isLetter())
98 validCategory = true;
99 else if (currA->isDigit())
100 return i18n("Others");
101 else
102 ++currA;
103 }
104
105 if (!validCategory)
106 retString = i18n("Others");
107 else
108 retString = *currA;
109 }
110 }
111 break;
112 }
113
114 case DolphinView::SortByDate:
115 {
116 KDateTime modifiedTime;
117 modifiedTime.setTime_t(item->time(KIO::UDS_MODIFICATION_TIME));
118 modifiedTime = modifiedTime.toLocalZone();
119
120 if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 0)
121 retString = i18n("Today");
122 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 1)
123 retString = i18n("Yesterday");
124 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 7)
125 retString = i18n("Less than a week");
126 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 31)
127 retString = i18n("Less than a month");
128 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 365)
129 retString = i18n("Less than a year");
130 else
131 retString = i18n("More than a year");
132 break;
133 }
134
135 case DolphinView::SortByPermissions:
136 retString = item->permissionsString();
137 break;
138
139 case DolphinView::SortByOwner:
140 retString = item->user();
141 break;
142
143 case DolphinView::SortByGroup:
144 retString = item->group();
145 break;
146
147 case DolphinView::SortBySize: {
148 const int fileSize = item ? item->size() : -1;
149 if (item && item->isDir()) {
150 retString = i18n("Folders");
151 } else if (fileSize < 5242880) {
152 retString = i18nc("Size", "Small");
153 } else if (fileSize < 10485760) {
154 retString = i18nc("Size", "Medium");
155 } else {
156 retString = i18nc("Size", "Big");
157 }
158 break;
159 }
160
161 case DolphinView::SortByType:
162 retString = item->mimeComment();
163 break;
164
165 #ifdef HAVE_NEPOMUK
166 case DolphinView::SortByRating: {
167 KFileItem* item = dirModel->itemForIndex(index);
168 if (item != 0) {
169 const Nepomuk::Resource resource(item->url().url(), Nepomuk::NFO::File());
170 const quint32 rating = resource.rating();
171 retString = i18np("1 star", "%1 stars", rating);
172 }
173 break;
174 }
175 case DolphinView::SortByTags:
176 break;
177 #endif
178 }
179
180 return retString;
181 }