2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@kde.org>
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.
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.
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.
21 #include "dolphinmodel.h"
23 #include "dolphinsortfilterproxymodel.h"
25 #include "kcategorizedview.h"
27 #include <config-nepomuk.h>
29 #include <nepomuk/global.h>
30 #include <nepomuk/resource.h>
31 #include <nepomuk/tag.h>
32 #include <Soprano/Vocabulary/Xesam>
35 #include <kdatetime.h>
36 #include <kdirmodel.h>
37 #include <kfileitem.h>
38 #include <kiconloader.h>
42 #include <kmimetype.h>
43 #include <kstandarddirs.h>
46 #include <QSortFilterProxyModel>
48 #include <QPersistentModelIndex>
52 const char* DolphinModel::m_others
= I18N_NOOP2("@title:group Name", "Others");
54 DolphinModel::DolphinModel(QObject
* parent
) :
56 m_hasRevisionData(false),
61 DolphinModel::~DolphinModel()
65 bool DolphinModel::setData(const QModelIndex
& index
, const QVariant
& value
, int role
)
67 if ((index
.column() == DolphinModel::Revision
) && (role
== Qt::DecorationRole
)) {
68 // TODO: remove data again when items are deleted...
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;
79 m_revisionHash
.insert(key
, state
);
80 emit
dataChanged(index
, index
);
85 return KDirModel::setData(index
, value
, role
);
88 QVariant
DolphinModel::data(const QModelIndex
& index
, int role
) const
91 case KCategorizedSortFilterProxyModel::CategoryDisplayRole
:
92 return displayRoleData(index
);
94 case KCategorizedSortFilterProxyModel::CategorySortRole
:
95 return sortRoleData(index
);
97 case Qt::DecorationRole
:
98 if (index
.column() == DolphinModel::Revision
) {
99 return m_revisionHash
.value(index
, RevisionControlPlugin::UnversionedRevision
);
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::LocallyModifiedRevision
:
109 return i18nc("@item::intable", "Locally modified");
110 case RevisionControlPlugin::UpdateRequiredRevision
:
111 return i18nc("@item::intable", "Update required");
112 case RevisionControlPlugin::UnversionedRevision
:
114 return i18nc("@item::intable", "Unversioned");
123 return KDirModel::data(index
, role
);
126 QVariant
DolphinModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
128 if ((orientation
== Qt::Horizontal
) && (role
== Qt::DisplayRole
)) {
129 if (section
< KDirModel::ColumnCount
) {
130 return KDirModel::headerData(section
, orientation
, role
);
133 Q_ASSERT(section
== DolphinModel::Revision
);
134 return i18nc("@title::column", "Revision");
139 int DolphinModel::columnCount(const QModelIndex
& parent
) const
141 return KDirModel::columnCount(parent
) + (ExtraColumnCount
- ColumnCount
);
144 void DolphinModel::clearRevisionData()
146 m_revisionHash
.clear();
147 m_hasRevisionData
= false;
150 bool DolphinModel::hasRevisionData() const
152 return m_hasRevisionData
;
155 void DolphinModel::slotRowsRemoved(const QModelIndex
& parent
, int start
, int end
)
157 if (m_hasRevisionData
) {
158 const int column
= parent
.column();
159 for (int row
= start
; row
<= end
; ++row
) {
160 m_revisionHash
.remove(parent
.child(row
, column
));
165 QVariant
DolphinModel::displayRoleData(const QModelIndex
& index
) const
169 if (!index
.isValid()) {
173 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
174 KFileItem item
= dirModel
->itemForIndex(index
);
176 switch (index
.column()) {
177 case KDirModel::Name
: {
178 // KDirModel checks columns to know to which role are
180 const QModelIndex nameIndex
= index
.model()->index(index
.row(), KDirModel::Name
, index
.parent());
181 if (!nameIndex
.isValid()) {
184 const QVariant data
= nameIndex
.model()->data(nameIndex
, Qt::DisplayRole
);
185 const QString name
= data
.toString();
186 if (!name
.isEmpty()) {
187 if (!item
.isHidden() && name
.at(0).isLetter())
188 retString
= name
.at(0).toUpper();
189 else if (item
.isHidden()) {
190 if (name
.at(0) == '.') {
191 if (name
.size() > 1 && name
.at(1).isLetter()) {
192 retString
= name
.at(1).toUpper();
194 retString
= i18nc("@title:group Name", m_others
);
197 retString
= name
.at(0).toUpper();
200 bool validCategory
= false;
202 const QString
str(name
.toUpper());
203 const QChar
* currA
= str
.unicode();
204 while (!currA
->isNull() && !validCategory
) {
205 if (currA
->isLetter()) {
206 validCategory
= true;
207 } else if (currA
->isDigit()) {
208 return i18nc("@title:group Name", m_others
);
214 if (!validCategory
) {
215 retString
= validCategory
? *currA
: i18nc("@title:group Name", m_others
);
224 case KDirModel::Size
: {
225 const KIO::filesize_t fileSize
= !item
.isNull() ? item
.size() : ~0U;
226 if (!item
.isNull() && item
.isDir()) {
227 retString
= i18nc("@title:group Size", "Folders");
228 } else if (fileSize
< 5242880) {
229 retString
= i18nc("@title:group Size", "Small");
230 } else if (fileSize
< 10485760) {
231 retString
= i18nc("@title:group Size", "Medium");
233 retString
= i18nc("@title:group Size", "Big");
238 case KDirModel::ModifiedTime
: {
239 KDateTime modifiedTime
= item
.time(KFileItem::ModificationTime
);
240 modifiedTime
= modifiedTime
.toLocalZone();
242 const QDate currentDate
= KDateTime::currentLocalDateTime().date();
243 const QDate modifiedDate
= modifiedTime
.date();
245 const int daysDistance
= modifiedDate
.daysTo(currentDate
);
247 int yearForCurrentWeek
= 0;
248 int currentWeek
= currentDate
.weekNumber(&yearForCurrentWeek
);
249 if (yearForCurrentWeek
== currentDate
.year() + 1) {
253 int yearForModifiedWeek
= 0;
254 int modifiedWeek
= modifiedDate
.weekNumber(&yearForModifiedWeek
);
255 if (yearForModifiedWeek
== modifiedDate
.year() + 1) {
259 if (currentDate
.year() == modifiedDate
.year() && currentDate
.month() == modifiedDate
.month()) {
260 switch (currentWeek
- modifiedWeek
) {
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"));
269 retString
= i18nc("@title:group Date", "Last Week");
272 retString
= i18nc("@title:group Date", "Two Weeks Ago");
275 retString
= i18nc("@title:group Date", "Three Weeks Ago");
279 retString
= i18nc("@title:group Date", "Earlier this Month");
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)"));
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"));
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"));
307 case KDirModel::Permissions
: {
312 QFileInfo
info(item
.url().pathOrUrl());
315 if (info
.permission(QFile::ReadUser
)) {
316 user
= i18nc("@item:intext Access permission, concatenated", "Read, ");
318 if (info
.permission(QFile::WriteUser
)) {
319 user
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
321 if (info
.permission(QFile::ExeUser
)) {
322 user
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
324 user
= user
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : user
.mid(0, user
.count() - 2);
327 if (info
.permission(QFile::ReadGroup
)) {
328 group
= i18nc("@item:intext Access permission, concatenated", "Read, ");
330 if (info
.permission(QFile::WriteGroup
)) {
331 group
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
333 if (info
.permission(QFile::ExeGroup
)) {
334 group
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
336 group
= group
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : group
.mid(0, group
.count() - 2);
338 // set permission string
339 if (info
.permission(QFile::ReadOther
)) {
340 others
= i18nc("@item:intext Access permission, concatenated", "Read, ");
342 if (info
.permission(QFile::WriteOther
)) {
343 others
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
345 if (info
.permission(QFile::ExeOther
)) {
346 others
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
348 others
= others
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : others
.mid(0, others
.count() - 2);
350 retString
= i18nc("@title:group Files and folders by permissions", "(User: %1) (Group: %2) (Others: %3)", user
, group
, others
);
354 case KDirModel::Owner
:
355 retString
= item
.user();
358 case KDirModel::Group
:
359 retString
= item
.group();
362 case KDirModel::Type
:
363 retString
= item
.mimeComment();
366 case DolphinModel::Revision
:
374 QVariant
DolphinModel::sortRoleData(const QModelIndex
& index
) const
378 if (!index
.isValid()) {
382 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
383 KFileItem item
= dirModel
->itemForIndex(index
);
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(QChar(QChar::ReplacementCharacter
));
395 case KDirModel::Size
: {
396 const KIO::filesize_t fileSize
= !item
.isNull() ? item
.size() : ~0U;
399 } else if (fileSize
< 5242880) {
401 } else if (fileSize
< 10485760) {
409 case KDirModel::ModifiedTime
: {
410 KDateTime modifiedTime
= item
.time(KFileItem::ModificationTime
);
411 modifiedTime
= modifiedTime
.toLocalZone();
413 const QDate currentDate
= KDateTime::currentLocalDateTime().date();
414 const QDate modifiedDate
= modifiedTime
.date();
416 retVariant
= -modifiedDate
.daysTo(currentDate
);
420 case KDirModel::Permissions
: {
421 QFileInfo
info(item
.url().pathOrUrl());
423 retVariant
= -KDirSortFilterProxyModel::pointsForPermissions(info
);
427 case KDirModel::Owner
:
428 retVariant
= item
.user();
431 case KDirModel::Group
:
432 retVariant
= item
.group();
435 case KDirModel::Type
:
437 // when sorting we want folders to be placed first
438 retVariant
= QString(); // krazy:exclude=nullstrassign
440 retVariant
= item
.mimeComment();