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