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