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