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