]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmodel.cpp
Enable Dolphin to show the revision states of files that are under revision control...
[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 <QPersistentModelIndex>
49 #include <QDir>
50 #include <QFileInfo>
51
52 const char* DolphinModel::m_others = I18N_NOOP2("@title:group Name", "Others");
53
54 DolphinModel::DolphinModel(QObject* parent) :
55 KDirModel(parent),
56 m_hasRevisionData(false),
57 m_revisionHash()
58 {
59 }
60
61 DolphinModel::~DolphinModel()
62 {
63 }
64
65 bool DolphinModel::setData(const QModelIndex& index, const QVariant& value, int role)
66 {
67 if ((index.column() == DolphinModel::Revision) && (role == Qt::DecorationRole)) {
68 // TODO: remove data again when items are deleted...
69
70 const QPersistentModelIndex key = index;
71 const RevisionState state = static_cast<RevisionState>(value.toInt());
72 if (m_revisionHash.value(key, LocalRevision) != state) {
73 m_hasRevisionData = true;
74 m_revisionHash.insert(key, state);
75 emit dataChanged(index, index);
76 return true;
77 }
78 }
79
80 return KDirModel::setData(index, value, role);
81 }
82
83 QVariant DolphinModel::data(const QModelIndex& index, int role) const
84 {
85 switch (role) {
86 case KCategorizedSortFilterProxyModel::CategoryDisplayRole:
87 return displayRoleData(index);
88
89 case KCategorizedSortFilterProxyModel::CategorySortRole:
90 return sortRoleData(index);
91
92 case Qt::DecorationRole:
93 if (index.column() == DolphinModel::Revision) {
94 return m_revisionHash.value(index, LocalRevision);
95 }
96 break;
97
98 case Qt::DisplayRole:
99 if (index.column() == DolphinModel::Revision) {
100 switch (m_revisionHash.value(index, LocalRevision)) {
101 case LatestRevision:
102 return i18nc("@item::intable", "Latest");
103
104 case LocalRevision:
105 default:
106 return i18nc("@item::intable", "Local");
107 }
108 }
109 break;
110
111 default:
112 break;
113 }
114
115 return KDirModel::data(index, role);
116 }
117
118 QVariant DolphinModel::headerData(int section, Qt::Orientation orientation, int role) const
119 {
120 if ((orientation == Qt::Horizontal) && (role == Qt::DisplayRole)) {
121 if (section < KDirModel::ColumnCount) {
122 return KDirModel::headerData(section, orientation, role);
123 }
124
125 Q_ASSERT(section == DolphinModel::Revision);
126 return i18nc("@title::column", "Revision");
127 }
128 return QVariant();
129 }
130
131 int DolphinModel::columnCount(const QModelIndex& parent) const
132 {
133 return KDirModel::columnCount(parent) + (ExtraColumnCount - ColumnCount);
134 }
135
136 bool DolphinModel::hasRevisionData() const
137 {
138 return m_hasRevisionData;
139 }
140
141 QVariant DolphinModel::displayRoleData(const QModelIndex& index) const
142 {
143 QString retString;
144
145 if (!index.isValid()) {
146 return retString;
147 }
148
149 const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
150 KFileItem item = dirModel->itemForIndex(index);
151
152 switch (index.column()) {
153 case KDirModel::Name: {
154 // KDirModel checks columns to know to which role are
155 // we talking about
156 const QModelIndex nameIndex = index.model()->index(index.row(), KDirModel::Name, index.parent());
157 if (!nameIndex.isValid()) {
158 return retString;
159 }
160 const QVariant data = nameIndex.model()->data(nameIndex, Qt::DisplayRole);
161 const QString name = data.toString();
162 if (!name.isEmpty()) {
163 if (!item.isHidden() && name.at(0).isLetter())
164 retString = name.at(0).toUpper();
165 else if (item.isHidden()) {
166 if (name.at(0) == '.') {
167 if (name.size() > 1 && name.at(1).isLetter()) {
168 retString = name.at(1).toUpper();
169 } else {
170 retString = i18nc("@title:group Name", m_others);
171 }
172 } else {
173 retString = name.at(0).toUpper();
174 }
175 } else {
176 bool validCategory = false;
177
178 const QString str(name.toUpper());
179 const QChar* currA = str.unicode();
180 while (!currA->isNull() && !validCategory) {
181 if (currA->isLetter()) {
182 validCategory = true;
183 } else if (currA->isDigit()) {
184 return i18nc("@title:group Name", m_others);
185 } else {
186 ++currA;
187 }
188 }
189
190 if (!validCategory) {
191 retString = validCategory ? *currA : i18nc("@title:group Name", m_others);
192 } else {
193 retString = *currA;
194 }
195 }
196 }
197 break;
198 }
199
200 case KDirModel::Size: {
201 const KIO::filesize_t fileSize = !item.isNull() ? item.size() : ~0U;
202 if (!item.isNull() && item.isDir()) {
203 retString = i18nc("@title:group Size", "Folders");
204 } else if (fileSize < 5242880) {
205 retString = i18nc("@title:group Size", "Small");
206 } else if (fileSize < 10485760) {
207 retString = i18nc("@title:group Size", "Medium");
208 } else {
209 retString = i18nc("@title:group Size", "Big");
210 }
211 break;
212 }
213
214 case KDirModel::ModifiedTime: {
215 KDateTime modifiedTime = item.time(KFileItem::ModificationTime);
216 modifiedTime = modifiedTime.toLocalZone();
217
218 const QDate currentDate = KDateTime::currentLocalDateTime().date();
219 const QDate modifiedDate = modifiedTime.date();
220
221 const int daysDistance = modifiedDate.daysTo(currentDate);
222
223 int yearForCurrentWeek = 0;
224 int currentWeek = currentDate.weekNumber(&yearForCurrentWeek);
225 if (yearForCurrentWeek == currentDate.year() + 1) {
226 currentWeek = 53;
227 }
228
229 int yearForModifiedWeek = 0;
230 int modifiedWeek = modifiedDate.weekNumber(&yearForModifiedWeek);
231 if (yearForModifiedWeek == modifiedDate.year() + 1) {
232 modifiedWeek = 53;
233 }
234
235 if (currentDate.year() == modifiedDate.year() && currentDate.month() == modifiedDate.month()) {
236 switch (currentWeek - modifiedWeek) {
237 case 0:
238 switch (daysDistance) {
239 case 0: retString = i18nc("@title:group Date", "Today"); break;
240 case 1: retString = i18nc("@title:group Date", "Yesterday"); break;
241 default: retString = modifiedTime.toString(i18nc("@title:group The week day name: %A", "%A"));
242 }
243 break;
244 case 1:
245 retString = i18nc("@title:group Date", "Last Week");
246 break;
247 case 2:
248 retString = i18nc("@title:group Date", "Two Weeks Ago");
249 break;
250 case 3:
251 retString = i18nc("@title:group Date", "Three Weeks Ago");
252 break;
253 case 4:
254 case 5:
255 retString = i18nc("@title:group Date", "Earlier this Month");
256 break;
257 default:
258 Q_ASSERT(false);
259 }
260 } else {
261 const QDate lastMonthDate = currentDate.addMonths(-1);
262 if (lastMonthDate.year() == modifiedDate.year() && lastMonthDate.month() == modifiedDate.month()) {
263 if (daysDistance == 1) {
264 retString = modifiedTime.toString(i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Yesterday (%B, %Y)"));
265 } else if (daysDistance <= 7) {
266 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)"));
267 } else if (daysDistance <= 7 * 2) {
268 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)"));
269 } else if (daysDistance <= 7 * 3) {
270 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)"));
271 } else if (daysDistance <= 7 * 4) {
272 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)"));
273 } else {
274 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"));
275 }
276 } else {
277 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"));
278 }
279 }
280 break;
281 }
282
283 case KDirModel::Permissions: {
284 QString user;
285 QString group;
286 QString others;
287
288 QFileInfo info(item.url().pathOrUrl());
289
290 // set user string
291 if (info.permission(QFile::ReadUser)) {
292 user = i18nc("@item:intext Access permission, concatenated", "Read, ");
293 }
294 if (info.permission(QFile::WriteUser)) {
295 user += i18nc("@item:intext Access permission, concatenated", "Write, ");
296 }
297 if (info.permission(QFile::ExeUser)) {
298 user += i18nc("@item:intext Access permission, concatenated", "Execute, ");
299 }
300 user = user.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : user.mid(0, user.count() - 2);
301
302 // set group string
303 if (info.permission(QFile::ReadGroup)) {
304 group = i18nc("@item:intext Access permission, concatenated", "Read, ");
305 }
306 if (info.permission(QFile::WriteGroup)) {
307 group += i18nc("@item:intext Access permission, concatenated", "Write, ");
308 }
309 if (info.permission(QFile::ExeGroup)) {
310 group += i18nc("@item:intext Access permission, concatenated", "Execute, ");
311 }
312 group = group.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : group.mid(0, group.count() - 2);
313
314 // set permission string
315 if (info.permission(QFile::ReadOther)) {
316 others = i18nc("@item:intext Access permission, concatenated", "Read, ");
317 }
318 if (info.permission(QFile::WriteOther)) {
319 others += i18nc("@item:intext Access permission, concatenated", "Write, ");
320 }
321 if (info.permission(QFile::ExeOther)) {
322 others += i18nc("@item:intext Access permission, concatenated", "Execute, ");
323 }
324 others = others.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : others.mid(0, others.count() - 2);
325
326 retString = i18nc("@title:group Files and folders by permissions", "(User: %1) (Group: %2) (Others: %3)", user, group, others);
327 break;
328 }
329
330 case KDirModel::Owner:
331 retString = item.user();
332 break;
333
334 case KDirModel::Group:
335 retString = item.group();
336 break;
337
338 case KDirModel::Type:
339 retString = item.mimeComment();
340 break;
341
342 case DolphinModel::Revision:
343 retString = "test";
344 break;
345 }
346
347 return retString;
348 }
349
350 QVariant DolphinModel::sortRoleData(const QModelIndex& index) const
351 {
352 QVariant retVariant;
353
354 if (!index.isValid()) {
355 return retVariant;
356 }
357
358 const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
359 KFileItem item = dirModel->itemForIndex(index);
360
361 switch (index.column()) {
362 case KDirModel::Name: {
363 retVariant = data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole);
364 if (retVariant == i18nc("@title:group Name", m_others)) {
365 // assure that the "Others" group is always the last categorization
366 retVariant = QString(QChar(QChar::ReplacementCharacter));
367 }
368 break;
369 }
370
371 case KDirModel::Size: {
372 const KIO::filesize_t fileSize = !item.isNull() ? item.size() : ~0U;
373 if (item.isDir()) {
374 retVariant = 0;
375 } else if (fileSize < 5242880) {
376 retVariant = 1;
377 } else if (fileSize < 10485760) {
378 retVariant = 2;
379 } else {
380 retVariant = 3;
381 }
382 break;
383 }
384
385 case KDirModel::ModifiedTime: {
386 KDateTime modifiedTime = item.time(KFileItem::ModificationTime);
387 modifiedTime = modifiedTime.toLocalZone();
388
389 const QDate currentDate = KDateTime::currentLocalDateTime().date();
390 const QDate modifiedDate = modifiedTime.date();
391
392 retVariant = -modifiedDate.daysTo(currentDate);
393 break;
394 }
395
396 case KDirModel::Permissions: {
397 QFileInfo info(item.url().pathOrUrl());
398
399 retVariant = -KDirSortFilterProxyModel::pointsForPermissions(info);
400 break;
401 }
402
403 case KDirModel::Owner:
404 retVariant = item.user();
405 break;
406
407 case KDirModel::Group:
408 retVariant = item.group();
409 break;
410
411 case KDirModel::Type:
412 if (item.isDir()) {
413 // when sorting we want folders to be placed first
414 retVariant = QString(); // krazy:exclude=nullstrassign
415 } else {
416 retVariant = item.mimeComment();
417 }
418 break;
419
420 default:
421 break;
422 }
423
424 return retVariant;
425 }