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