]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmodel.cpp
Fix infinite recursion if the default text is empty.
[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_hasRevisionData(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::Revision) && (role == Qt::DecorationRole)) {
68 // TODO: remove data again when items are deleted...
69
70 const QPersistentModelIndex key = index;
71 const RevisionControlPlugin::RevisionState state = static_cast<RevisionControlPlugin::RevisionState>(value.toInt());
72 if (m_revisionHash.value(key, RevisionControlPlugin::UnversionedRevision) != state) {
73 if (!m_hasRevisionData) {
74 connect(this, SIGNAL(rowsRemoved (const QModelIndex&, int, int)),
75 this, SLOT(slotRowsRemoved(const QModelIndex&, int, int)));
76 m_hasRevisionData = 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::Revision) {
99 return m_revisionHash.value(index, RevisionControlPlugin::UnversionedRevision);
100 }
101 break;
102
103 case Qt::DisplayRole:
104 if (index.column() == DolphinModel::Revision) {
105 switch (m_revisionHash.value(index, RevisionControlPlugin::UnversionedRevision)) {
106 case RevisionControlPlugin::NormalRevision:
107 return i18nc("@item::intable", "Normal");
108 case RevisionControlPlugin::UpdateRequiredRevision:
109 return i18nc("@item::intable", "Update required");
110 case RevisionControlPlugin::LocallyModifiedRevision:
111 return i18nc("@item::intable", "Locally modified");
112 case RevisionControlPlugin::AddedRevision:
113 return i18nc("@item::intable", "Added");
114 case RevisionControlPlugin::RemovedRevision:
115 return i18nc("@item::intable", "Removed");
116 case RevisionControlPlugin::ConflictingRevision:
117 return i18nc("@item::intable", "Conflicting");
118 case RevisionControlPlugin::UnversionedRevision:
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::Revision);
140 return i18nc("@title::column", "Revision");
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::clearRevisionData()
151 {
152 m_revisionHash.clear();
153 m_hasRevisionData = false;
154 }
155
156 bool DolphinModel::hasRevisionData() const
157 {
158 return m_hasRevisionData;
159 }
160
161 void DolphinModel::slotRowsRemoved(const QModelIndex& parent, int start, int end)
162 {
163 if (m_hasRevisionData) {
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 if (!validCategory) {
221 retString = validCategory ? *currA : i18nc("@title:group Name", m_others);
222 } else {
223 retString = *currA;
224 }
225 }
226 }
227 break;
228 }
229
230 case KDirModel::Size: {
231 const KIO::filesize_t fileSize = !item.isNull() ? item.size() : ~0U;
232 if (!item.isNull() && item.isDir()) {
233 retString = i18nc("@title:group Size", "Folders");
234 } else if (fileSize < 5242880) {
235 retString = i18nc("@title:group Size", "Small");
236 } else if (fileSize < 10485760) {
237 retString = i18nc("@title:group Size", "Medium");
238 } else {
239 retString = i18nc("@title:group Size", "Big");
240 }
241 break;
242 }
243
244 case KDirModel::ModifiedTime: {
245 KDateTime modifiedTime = item.time(KFileItem::ModificationTime);
246 modifiedTime = modifiedTime.toLocalZone();
247
248 const QDate currentDate = KDateTime::currentLocalDateTime().date();
249 const QDate modifiedDate = modifiedTime.date();
250
251 const int daysDistance = modifiedDate.daysTo(currentDate);
252
253 int yearForCurrentWeek = 0;
254 int currentWeek = currentDate.weekNumber(&yearForCurrentWeek);
255 if (yearForCurrentWeek == currentDate.year() + 1) {
256 currentWeek = 53;
257 }
258
259 int yearForModifiedWeek = 0;
260 int modifiedWeek = modifiedDate.weekNumber(&yearForModifiedWeek);
261 if (yearForModifiedWeek == modifiedDate.year() + 1) {
262 modifiedWeek = 53;
263 }
264
265 if (currentDate.year() == modifiedDate.year() && currentDate.month() == modifiedDate.month()) {
266 switch (currentWeek - modifiedWeek) {
267 case 0:
268 switch (daysDistance) {
269 case 0: retString = i18nc("@title:group Date", "Today"); break;
270 case 1: retString = i18nc("@title:group Date", "Yesterday"); break;
271 default: retString = modifiedTime.toString(i18nc("@title:group The week day name: %A", "%A"));
272 }
273 break;
274 case 1:
275 retString = i18nc("@title:group Date", "Last Week");
276 break;
277 case 2:
278 retString = i18nc("@title:group Date", "Two Weeks Ago");
279 break;
280 case 3:
281 retString = i18nc("@title:group Date", "Three Weeks Ago");
282 break;
283 case 4:
284 case 5:
285 retString = i18nc("@title:group Date", "Earlier this Month");
286 break;
287 default:
288 Q_ASSERT(false);
289 }
290 } else {
291 const QDate lastMonthDate = currentDate.addMonths(-1);
292 if (lastMonthDate.year() == modifiedDate.year() && lastMonthDate.month() == modifiedDate.month()) {
293 if (daysDistance == 1) {
294 retString = modifiedTime.toString(i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Yesterday (%B, %Y)"));
295 } else if (daysDistance <= 7) {
296 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)"));
297 } else if (daysDistance <= 7 * 2) {
298 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)"));
299 } else if (daysDistance <= 7 * 3) {
300 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)"));
301 } else if (daysDistance <= 7 * 4) {
302 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)"));
303 } else {
304 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"));
305 }
306 } else {
307 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"));
308 }
309 }
310 break;
311 }
312
313 case KDirModel::Permissions: {
314 QString user;
315 QString group;
316 QString others;
317
318 QFileInfo info(item.url().pathOrUrl());
319
320 // set user string
321 if (info.permission(QFile::ReadUser)) {
322 user = i18nc("@item:intext Access permission, concatenated", "Read, ");
323 }
324 if (info.permission(QFile::WriteUser)) {
325 user += i18nc("@item:intext Access permission, concatenated", "Write, ");
326 }
327 if (info.permission(QFile::ExeUser)) {
328 user += i18nc("@item:intext Access permission, concatenated", "Execute, ");
329 }
330 user = user.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : user.mid(0, user.count() - 2);
331
332 // set group string
333 if (info.permission(QFile::ReadGroup)) {
334 group = i18nc("@item:intext Access permission, concatenated", "Read, ");
335 }
336 if (info.permission(QFile::WriteGroup)) {
337 group += i18nc("@item:intext Access permission, concatenated", "Write, ");
338 }
339 if (info.permission(QFile::ExeGroup)) {
340 group += i18nc("@item:intext Access permission, concatenated", "Execute, ");
341 }
342 group = group.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : group.mid(0, group.count() - 2);
343
344 // set permission string
345 if (info.permission(QFile::ReadOther)) {
346 others = i18nc("@item:intext Access permission, concatenated", "Read, ");
347 }
348 if (info.permission(QFile::WriteOther)) {
349 others += i18nc("@item:intext Access permission, concatenated", "Write, ");
350 }
351 if (info.permission(QFile::ExeOther)) {
352 others += i18nc("@item:intext Access permission, concatenated", "Execute, ");
353 }
354 others = others.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : others.mid(0, others.count() - 2);
355
356 retString = i18nc("@title:group Files and folders by permissions", "(User: %1) (Group: %2) (Others: %3)", user, group, others);
357 break;
358 }
359
360 case KDirModel::Owner:
361 retString = item.user();
362 break;
363
364 case KDirModel::Group:
365 retString = item.group();
366 break;
367
368 case KDirModel::Type:
369 retString = item.mimeComment();
370 break;
371
372 case DolphinModel::Revision:
373 retString = "test";
374 break;
375 }
376
377 return retString;
378 }
379
380 QVariant DolphinModel::sortRoleData(const QModelIndex& index) const
381 {
382 QVariant retVariant;
383
384 if (!index.isValid()) {
385 return retVariant;
386 }
387
388 const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
389 KFileItem item = dirModel->itemForIndex(index);
390
391 switch (index.column()) {
392 case KDirModel::Name: {
393 retVariant = data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole);
394 if (retVariant == i18nc("@title:group Name", m_others)) {
395 // assure that the "Others" group is always the last categorization
396 retVariant = QString(QChar(QChar::ReplacementCharacter));
397 }
398 break;
399 }
400
401 case KDirModel::Size: {
402 const KIO::filesize_t fileSize = !item.isNull() ? item.size() : ~0U;
403 if (item.isDir()) {
404 retVariant = 0;
405 } else if (fileSize < 5242880) {
406 retVariant = 1;
407 } else if (fileSize < 10485760) {
408 retVariant = 2;
409 } else {
410 retVariant = 3;
411 }
412 break;
413 }
414
415 case KDirModel::ModifiedTime: {
416 KDateTime modifiedTime = item.time(KFileItem::ModificationTime);
417 modifiedTime = modifiedTime.toLocalZone();
418
419 const QDate currentDate = KDateTime::currentLocalDateTime().date();
420 const QDate modifiedDate = modifiedTime.date();
421
422 retVariant = -modifiedDate.daysTo(currentDate);
423 break;
424 }
425
426 case KDirModel::Permissions: {
427 QFileInfo info(item.url().pathOrUrl());
428
429 retVariant = -KDirSortFilterProxyModel::pointsForPermissions(info);
430 break;
431 }
432
433 case KDirModel::Owner:
434 retVariant = item.user();
435 break;
436
437 case KDirModel::Group:
438 retVariant = item.group();
439 break;
440
441 case KDirModel::Type:
442 if (item.isDir()) {
443 // when sorting we want folders to be placed first
444 retVariant = QString(); // krazy:exclude=nullstrassign
445 } else {
446 retVariant = item.mimeComment();
447 }
448 break;
449
450 default:
451 break;
452 }
453
454 return retVariant;
455 }