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