]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmodel.cpp
revert SVN commit 899245: The patch does not work as expected.
[dolphin.git] / src / dolphinmodel.cpp
1 /**
2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@kde.org>
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 "dolphinmodel.h"
22
23 #include "dolphinsortfilterproxymodel.h"
24
25 #include "kcategorizedview.h"
26
27 #include <config-nepomuk.h>
28 #ifdef HAVE_NEPOMUK
29 #include <nepomuk/global.h>
30 #include <nepomuk/resource.h>
31 #include <nepomuk/tag.h>
32 #include <Soprano/Vocabulary/Xesam>
33 #endif
34
35 #include <kdatetime.h>
36 #include <kdirmodel.h>
37 #include <kfileitem.h>
38 #include <kiconloader.h>
39 #include <klocale.h>
40 #include <kurl.h>
41 #include <kuser.h>
42 #include <kmimetype.h>
43 #include <kstandarddirs.h>
44
45 #include <QList>
46 #include <QSortFilterProxyModel>
47 #include <QPainter>
48 #include <QDir>
49 #include <QFileInfo>
50
51 const char* DolphinModel::m_others = I18N_NOOP2("@title:group Name", "Others");
52
53 DolphinModel::DolphinModel(QObject* parent)
54 : KDirModel(parent)
55 {
56 }
57
58 DolphinModel::~DolphinModel()
59 {
60 }
61
62 QVariant DolphinModel::data(const QModelIndex& index, int role) const
63 {
64 switch (role) {
65 case KCategorizedSortFilterProxyModel::CategoryDisplayRole:
66 return displayRoleData(index);
67 case KCategorizedSortFilterProxyModel::CategorySortRole:
68 return sortRoleData(index);
69 default:
70 return KDirModel::data(index, role);
71 }
72 }
73
74 int DolphinModel::columnCount(const QModelIndex &parent) const
75 {
76 return KDirModel::columnCount(parent) + (ExtraColumnCount - ColumnCount);
77 }
78
79 quint32 DolphinModel::ratingForIndex(const QModelIndex& index)
80 {
81 #ifdef HAVE_NEPOMUK
82 quint32 rating = 0;
83
84 const DolphinModel* dolphinModel = static_cast<const DolphinModel*>(index.model());
85 KFileItem item = dolphinModel->itemForIndex(index);
86 if (!item.isNull()) {
87 const Nepomuk::Resource resource(item.url().url(), Soprano::Vocabulary::Xesam::File());
88 rating = resource.rating();
89 }
90 return rating;
91 #else
92 Q_UNUSED(index);
93 return 0;
94 #endif
95 }
96
97 QString DolphinModel::tagsForIndex(const QModelIndex& index)
98 {
99 #ifdef HAVE_NEPOMUK
100 QString tagsString;
101
102 const DolphinModel* dolphinModel = static_cast<const DolphinModel*>(index.model());
103 KFileItem item = dolphinModel->itemForIndex(index);
104 if (!item.isNull()) {
105 const Nepomuk::Resource resource(item.url().url(), Soprano::Vocabulary::Xesam::File());
106 const QList<Nepomuk::Tag> tags = resource.tags();
107 QStringList stringList;
108 foreach (const Nepomuk::Tag& tag, tags) {
109 stringList.append(tag.label());
110 }
111 stringList.sort();
112
113 foreach (const QString& str, stringList) {
114 tagsString += str;
115 tagsString += ", ";
116 }
117
118 if (!tagsString.isEmpty()) {
119 tagsString.resize(tagsString.size() - 2);
120 }
121 }
122
123 return tagsString;
124 #else
125 Q_UNUSED(index);
126 return QString();
127 #endif
128 }
129
130 QVariant DolphinModel::displayRoleData(const QModelIndex& index) const
131 {
132 QString retString;
133
134 if (!index.isValid()) {
135 return retString;
136 }
137
138 const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
139 KFileItem item = dirModel->itemForIndex(index);
140
141 switch (index.column()) {
142 case KDirModel::Name: {
143 // KDirModel checks columns to know to which role are
144 // we talking about
145 const QModelIndex nameIndex = index.model()->index(index.row(), KDirModel::Name, index.parent());
146 if (!nameIndex.isValid()) {
147 return retString;
148 }
149 const QVariant data = nameIndex.model()->data(nameIndex, Qt::DisplayRole);
150 const QString name = data.toString();
151 if (!name.isEmpty()) {
152 if (!item.isHidden() && name.at(0).isLetter())
153 retString = name.at(0).toUpper();
154 else if (item.isHidden()) {
155 if (name.at(0) == '.') {
156 if (name.size() > 1 && name.at(1).isLetter()) {
157 retString = name.at(1).toUpper();
158 } else {
159 retString = i18nc("@title:group Name", m_others);
160 }
161 } else {
162 retString = name.at(0).toUpper();
163 }
164 } else {
165 bool validCategory = false;
166
167 const QString str(name.toUpper());
168 const QChar* currA = str.unicode();
169 while (!currA->isNull() && !validCategory) {
170 if (currA->isLetter()) {
171 validCategory = true;
172 } else if (currA->isDigit()) {
173 return i18nc("@title:group Name", m_others);
174 } else {
175 ++currA;
176 }
177 }
178
179 if (!validCategory) {
180 retString = validCategory ? *currA : i18nc("@title:group Name", m_others);
181 } else {
182 retString = *currA;
183 }
184 }
185 }
186 break;
187 }
188
189 case KDirModel::Size: {
190 const KIO::filesize_t fileSize = !item.isNull() ? item.size() : ~0U;
191 if (!item.isNull() && item.isDir()) {
192 retString = i18nc("@title:group Size", "Folders");
193 } else if (fileSize < 5242880) {
194 retString = i18nc("@title:group Size", "Small");
195 } else if (fileSize < 10485760) {
196 retString = i18nc("@title:group Size", "Medium");
197 } else {
198 retString = i18nc("@title:group Size", "Big");
199 }
200 break;
201 }
202
203 case KDirModel::ModifiedTime: {
204 KDateTime modifiedTime = item.time(KFileItem::ModificationTime);
205 modifiedTime = modifiedTime.toLocalZone();
206
207 const QDate currentDate = KDateTime::currentLocalDateTime().date();
208 const QDate modifiedDate = modifiedTime.date();
209
210 const int daysDistance = modifiedDate.daysTo(currentDate);
211 const int currentWeek = currentDate.weekNumber();
212 const int modifiedWeek = modifiedDate.weekNumber();
213
214 if (currentDate.year() == modifiedDate.year() &&
215 currentDate.month() == modifiedDate.month()) {
216 switch (currentWeek - modifiedWeek) {
217 case 0:
218 switch (daysDistance) {
219 case 0: retString = i18nc("@title:group Date", "Today"); break;
220 case 1: retString = i18nc("@title:group Date", "Yesterday"); break;
221 default: retString = modifiedTime.toString(i18nc("@title:group The week day name: %A", "%A"));
222 }
223 break;
224 case 1:
225 retString = i18nc("@title:group Date", "Last Week");
226 break;
227 case 2:
228 retString = i18nc("@title:group Date", "Two Weeks Ago");
229 break;
230 case 3:
231 retString = i18nc("@title:group Date", "Three Weeks Ago");
232 break;
233 case 4:
234 retString = i18nc("@title:group Date", "Earlier this Month");
235 break;
236 default:
237 Q_ASSERT(false);
238 }
239 } else {
240 if (daysDistance <= (currentDate.day() + modifiedDate.daysInMonth())) {
241 if (daysDistance == 1) {
242 retString = i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Yesterday (%B, %Y)");
243 } else if (daysDistance <= 7) {
244 retString = modifiedTime.toString(i18nc("@title:group The week day name: %A, %B is full month name in current locale, and %Y is full year number", "%A (%B, %Y)"));
245 } else if (daysDistance <= 7 * 2) {
246 retString = modifiedTime.toString(i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Last Week (%B, %Y)"));
247 } else if (daysDistance <= 7 * 3) {
248 retString = modifiedTime.toString(i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Two Weeks Ago (%B, %Y)"));
249 } else if (daysDistance <= 7 * 4) {
250 retString = modifiedTime.toString(i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Three Weeks Ago (%B, %Y)"));
251 } else {
252 retString = modifiedTime.toString(i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Earlier on %B, %Y"));
253 }
254 } else {
255 retString = modifiedTime.toString(i18nc("@title:group The month and year: %B is full month name in current locale, and %Y is full year number", "%B, %Y"));
256 }
257 }
258 break;
259 }
260
261 case KDirModel::Permissions: {
262 QString user;
263 QString group;
264 QString others;
265
266 QFileInfo info(item.url().pathOrUrl());
267
268 // set user string
269 if (info.permission(QFile::ReadUser)) {
270 user = i18nc("@item:intext Access permission, concatenated", "Read, ");
271 }
272 if (info.permission(QFile::WriteUser)) {
273 user += i18nc("@item:intext Access permission, concatenated", "Write, ");
274 }
275 if (info.permission(QFile::ExeUser)) {
276 user += i18nc("@item:intext Access permission, concatenated", "Execute, ");
277 }
278 user = user.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : user.mid(0, user.count() - 2);
279
280 // set group string
281 if (info.permission(QFile::ReadGroup)) {
282 group = i18nc("@item:intext Access permission, concatenated", "Read, ");
283 }
284 if (info.permission(QFile::WriteGroup)) {
285 group += i18nc("@item:intext Access permission, concatenated", "Write, ");
286 }
287 if (info.permission(QFile::ExeGroup)) {
288 group += i18nc("@item:intext Access permission, concatenated", "Execute, ");
289 }
290 group = group.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : group.mid(0, group.count() - 2);
291
292 // set permission string
293 if (info.permission(QFile::ReadOther)) {
294 others = i18nc("@item:intext Access permission, concatenated", "Read, ");
295 }
296 if (info.permission(QFile::WriteOther)) {
297 others += i18nc("@item:intext Access permission, concatenated", "Write, ");
298 }
299 if (info.permission(QFile::ExeOther)) {
300 others += i18nc("@item:intext Access permission, concatenated", "Execute, ");
301 }
302 others = others.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : others.mid(0, others.count() - 2);
303
304 retString = i18nc("@title:group Files and folders by permissions", "(User: %1) (Group: %2) (Others: %3)", user, group, others);
305 break;
306 }
307
308 case KDirModel::Owner:
309 retString = item.user();
310 break;
311
312 case KDirModel::Group:
313 retString = item.group();
314 break;
315
316 case KDirModel::Type:
317 retString = item.mimeComment();
318 break;
319
320 #ifdef HAVE_NEPOMUK
321 case DolphinModel::Rating: {
322 const quint32 rating = ratingForIndex(index);
323 retString = QString::number(rating);
324 break;
325 }
326
327 case DolphinModel::Tags: {
328 retString = tagsForIndex(index);
329 if (retString.isEmpty()) {
330 retString = i18nc("@title:group Tags", "Not yet tagged");
331 }
332 break;
333 }
334 #endif
335 }
336
337 return retString;
338 }
339
340 QVariant DolphinModel::sortRoleData(const QModelIndex& index) const
341 {
342 QVariant retVariant;
343
344 if (!index.isValid()) {
345 return retVariant;
346 }
347
348 const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
349 KFileItem item = dirModel->itemForIndex(index);
350
351 switch (index.column()) {
352 case KDirModel::Name: {
353 retVariant = data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole);
354 if (retVariant == i18nc("@title:group Name", m_others)) {
355 // assure that the "Others" group is always the last categorization
356 retVariant = QString(QChar(QChar::ReplacementCharacter));
357 }
358 break;
359 }
360
361 case KDirModel::Size: {
362 const KIO::filesize_t fileSize = !item.isNull() ? item.size() : ~0U;
363 if (item.isDir()) {
364 retVariant = 0;
365 } else if (fileSize < 5242880) {
366 retVariant = 1;
367 } else if (fileSize < 10485760) {
368 retVariant = 2;
369 } else {
370 retVariant = 3;
371 }
372 break;
373 }
374
375 case KDirModel::ModifiedTime: {
376 KDateTime modifiedTime = item.time(KFileItem::ModificationTime);
377 modifiedTime = modifiedTime.toLocalZone();
378
379 const QDate currentDate = KDateTime::currentLocalDateTime().date();
380 const QDate modifiedDate = modifiedTime.date();
381
382 retVariant = -modifiedDate.daysTo(currentDate);
383 break;
384 }
385
386 case KDirModel::Permissions: {
387 QFileInfo info(item.url().pathOrUrl());
388
389 retVariant = -KDirSortFilterProxyModel::pointsForPermissions(info);
390 break;
391 }
392
393 case KDirModel::Owner:
394 retVariant = item.user();
395 break;
396
397 case KDirModel::Group:
398 retVariant = item.group();
399 break;
400
401 case KDirModel::Type:
402 if (item.isDir())
403 retVariant = QString(); // when sorting we want folders to be placed first
404 else
405 retVariant = item.mimeComment();
406 break;
407
408 #ifdef HAVE_NEPOMUK
409 case DolphinModel::Rating: {
410 retVariant = ratingForIndex(index);
411 break;
412 }
413
414 case DolphinModel::Tags: {
415 retVariant = tagsForIndex(index).count();
416 break;
417 }
418 #endif
419
420 default:
421 break;
422 }
423
424 return retVariant;
425 }