]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmodel.cpp
Provide functionality for auto-expanding folders (the whole patch has been provided...
[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 static const char* 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 QModelIndex theIndex = index.model()->index(index.row(),
146 KDirModel::Name,
147 index.parent());
148
149 if (!theIndex.isValid()) {
150 return retString;
151 }
152 QVariant data = theIndex.model()->data(theIndex, Qt::DisplayRole);
153 QString name = data.toString();
154 if (!name.isEmpty()) {
155 if (!item.isHidden() && name.at(0).isLetter())
156 retString = name.at(0).toUpper();
157 else if (item.isHidden()) {
158 if (name.at(0) == '.') {
159 if (name.size() > 1 && name.at(1).isLetter()) {
160 retString = name.at(1).toUpper();
161 } else {
162 retString = i18nc("@title:group Name", others);
163 }
164 } else {
165 retString = name.at(0).toUpper();
166 }
167 } else {
168 bool validCategory = false;
169
170 const QString str(name.toUpper());
171 const QChar* currA = str.unicode();
172 while (!currA->isNull() && !validCategory) {
173 if (currA->isLetter()) {
174 validCategory = true;
175 } else if (currA->isDigit()) {
176 return i18nc("@title:group Name", others);
177 } else {
178 ++currA;
179 }
180 }
181
182 if (!validCategory) {
183 retString = validCategory ? *currA : i18nc("@title:group Name", others);
184 } else {
185 retString = *currA;
186 }
187 }
188 }
189 break;
190 }
191
192 case KDirModel::Size: {
193 const KIO::filesize_t fileSize = !item.isNull() ? item.size() : ~0U;
194 if (!item.isNull() && item.isDir()) {
195 retString = i18nc("@title:group Size", "Folders");
196 } else if (fileSize < 5242880) {
197 retString = i18nc("@title:group Size", "Small");
198 } else if (fileSize < 10485760) {
199 retString = i18nc("@title:group Size", "Medium");
200 } else {
201 retString = i18nc("@title:group Size", "Big");
202 }
203 break;
204 }
205
206 case KDirModel::ModifiedTime: {
207 KDateTime modifiedTime = item.time(KFileItem::ModificationTime);
208 modifiedTime = modifiedTime.toLocalZone();
209
210 const QDate currentDate = KDateTime::currentLocalDateTime().date();
211 const QDate modifiedDate = modifiedTime.date();
212
213 const int daysDistance = modifiedDate.daysTo(currentDate);
214 const int currentWeek = currentDate.weekNumber();
215 const int modifiedWeek = modifiedDate.weekNumber();
216
217 if (currentDate.year() == modifiedDate.year() &&
218 currentDate.month() == modifiedDate.month()) {
219 switch (currentWeek - modifiedWeek) {
220 case 0:
221 switch (daysDistance) {
222 case 0: retString = i18nc("@title:group Date", "Today"); break;
223 case 1: retString = i18nc("@title:group Date", "Yesterday"); break;
224 default: retString = modifiedTime.toString(i18nc("@title:group The week day name: %A", "%A"));
225 }
226 break;
227 case 1:
228 retString = i18nc("@title:group Date", "Last Week");
229 break;
230 case 2:
231 retString = i18nc("@title:group Date", "Two Weeks Ago");
232 break;
233 case 3:
234 retString = i18nc("@title:group Date", "Three Weeks Ago");
235 break;
236 case 4:
237 retString = i18nc("@title:group Date", "Earlier this Month");
238 break;
239 default:
240 Q_ASSERT(false);
241 }
242 } else {
243 if (daysDistance <= (currentDate.day() + modifiedDate.daysInMonth())) {
244 if (daysDistance == 1) {
245 retString = i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Yesterday (%B, %Y)");
246 } else if (daysDistance <= 7) {
247 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)"));
248 } else if (daysDistance <= 7 * 2) {
249 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)"));
250 } else if (daysDistance <= 7 * 3) {
251 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)"));
252 } else if (daysDistance <= 7 * 4) {
253 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)"));
254 } else {
255 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"));
256 }
257 } else {
258 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"));
259 }
260 }
261 break;
262 }
263
264 case KDirModel::Permissions: {
265 QString user;
266 QString group;
267 QString others;
268
269 QFileInfo info(item.url().pathOrUrl());
270
271 // set user string
272 if (info.permission(QFile::ReadUser)) {
273 user = i18nc("@item:intext Access permission, concatenated", "Read, ");
274 }
275 if (info.permission(QFile::WriteUser)) {
276 user += i18nc("@item:intext Access permission, concatenated", "Write, ");
277 }
278 if (info.permission(QFile::ExeUser)) {
279 user += i18nc("@item:intext Access permission, concatenated", "Execute, ");
280 }
281 user = user.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : user.mid(0, user.count() - 2);
282
283 // set group string
284 if (info.permission(QFile::ReadGroup)) {
285 group = i18nc("@item:intext Access permission, concatenated", "Read, ");
286 }
287 if (info.permission(QFile::WriteGroup)) {
288 group += i18nc("@item:intext Access permission, concatenated", "Write, ");
289 }
290 if (info.permission(QFile::ExeGroup)) {
291 group += i18nc("@item:intext Access permission, concatenated", "Execute, ");
292 }
293 group = group.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : group.mid(0, group.count() - 2);
294
295 // set permission string
296 if (info.permission(QFile::ReadOther)) {
297 others = i18nc("@item:intext Access permission, concatenated", "Read, ");
298 }
299 if (info.permission(QFile::WriteOther)) {
300 others += i18nc("@item:intext Access permission, concatenated", "Write, ");
301 }
302 if (info.permission(QFile::ExeOther)) {
303 others += i18nc("@item:intext Access permission, concatenated", "Execute, ");
304 }
305 others = others.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : others.mid(0, others.count() - 2);
306
307 retString = i18nc("@title:group Files and folders by permissions", "(User: %1) (Group: %2) (Others: %3)", user, group, others);
308 break;
309 }
310
311 case KDirModel::Owner:
312 retString = item.user();
313 break;
314
315 case KDirModel::Group:
316 retString = item.group();
317 break;
318
319 case KDirModel::Type:
320 retString = item.mimeComment();
321 break;
322
323 #ifdef HAVE_NEPOMUK
324 case DolphinModel::Rating: {
325 const quint32 rating = ratingForIndex(index);
326 retString = QString::number(rating);
327 break;
328 }
329
330 case DolphinModel::Tags: {
331 retString = tagsForIndex(index);
332 if (retString.isEmpty()) {
333 retString = i18nc("@title:group Tags", "Not yet tagged");
334 }
335 break;
336 }
337 #endif
338 }
339
340 return retString;
341 }
342
343 QVariant DolphinModel::sortRoleData(const QModelIndex& index) const
344 {
345 QVariant retVariant;
346
347 if (!index.isValid()) {
348 return retVariant;
349 }
350
351 const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
352 KFileItem item = dirModel->itemForIndex(index);
353
354 switch (index.column()) {
355 case KDirModel::Name: {
356 retVariant = data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole);
357
358 if (retVariant == i18nc("@title:group Name", others)) {
359 retVariant = QString(QChar(QChar::ReplacementCharacter));
360 }
361 break;
362 }
363
364 case KDirModel::Size: {
365 const KIO::filesize_t fileSize = !item.isNull() ? item.size() : ~0U;
366 if (item.isDir()) {
367 retVariant = 0;
368 } else if (fileSize < 5242880) {
369 retVariant = 1;
370 } else if (fileSize < 10485760) {
371 retVariant = 2;
372 } else {
373 retVariant = 3;
374 }
375 break;
376 }
377
378 case KDirModel::ModifiedTime: {
379 KDateTime modifiedTime = item.time(KFileItem::ModificationTime);
380 modifiedTime = modifiedTime.toLocalZone();
381
382 const QDate currentDate = KDateTime::currentLocalDateTime().date();
383 const QDate modifiedDate = modifiedTime.date();
384
385 retVariant = -modifiedDate.daysTo(currentDate);
386 break;
387 }
388
389 case KDirModel::Permissions: {
390 QFileInfo info(item.url().pathOrUrl());
391
392 retVariant = -KDirSortFilterProxyModel::pointsForPermissions(info);
393 break;
394 }
395
396 case KDirModel::Owner:
397 retVariant = item.user();
398 break;
399
400 case KDirModel::Group:
401 retVariant = item.group();
402 break;
403
404 case KDirModel::Type:
405 if (item.isDir())
406 retVariant = QString(); // when sorting we want folders to be placed first
407 else
408 retVariant = item.mimeComment();
409 break;
410
411 #ifdef HAVE_NEPOMUK
412 case DolphinModel::Rating: {
413 retVariant = ratingForIndex(index);
414 break;
415 }
416
417 case DolphinModel::Tags: {
418 retVariant = tagsForIndex(index).count();
419 break;
420 }
421 #endif
422
423 default:
424 break;
425 }
426
427 return retVariant;
428 }