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