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 RevisionState state
= static_cast<RevisionState
>(value
.toInt());
72 if (m_revisionHash
.value(key
, LocalRevision
) != state
) {
73 m_hasRevisionData
= true;
74 m_revisionHash
.insert(key
, state
);
75 emit
dataChanged(index
, index
);
80 return KDirModel::setData(index
, value
, role
);
83 QVariant
DolphinModel::data(const QModelIndex
& index
, int role
) const
86 case KCategorizedSortFilterProxyModel::CategoryDisplayRole
:
87 return displayRoleData(index
);
89 case KCategorizedSortFilterProxyModel::CategorySortRole
:
90 return sortRoleData(index
);
92 case Qt::DecorationRole
:
93 if (index
.column() == DolphinModel::Revision
) {
94 return m_revisionHash
.value(index
, LocalRevision
);
99 if (index
.column() == DolphinModel::Revision
) {
100 switch (m_revisionHash
.value(index
, LocalRevision
)) {
102 return i18nc("@item::intable", "Latest");
106 return i18nc("@item::intable", "Local");
115 return KDirModel::data(index
, role
);
118 QVariant
DolphinModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
120 if ((orientation
== Qt::Horizontal
) && (role
== Qt::DisplayRole
)) {
121 if (section
< KDirModel::ColumnCount
) {
122 return KDirModel::headerData(section
, orientation
, role
);
125 Q_ASSERT(section
== DolphinModel::Revision
);
126 return i18nc("@title::column", "Revision");
131 int DolphinModel::columnCount(const QModelIndex
& parent
) const
133 return KDirModel::columnCount(parent
) + (ExtraColumnCount
- ColumnCount
);
136 bool DolphinModel::hasRevisionData() const
138 return m_hasRevisionData
;
141 QVariant
DolphinModel::displayRoleData(const QModelIndex
& index
) const
145 if (!index
.isValid()) {
149 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
150 KFileItem item
= dirModel
->itemForIndex(index
);
152 switch (index
.column()) {
153 case KDirModel::Name
: {
154 // KDirModel checks columns to know to which role are
156 const QModelIndex nameIndex
= index
.model()->index(index
.row(), KDirModel::Name
, index
.parent());
157 if (!nameIndex
.isValid()) {
160 const QVariant data
= nameIndex
.model()->data(nameIndex
, Qt::DisplayRole
);
161 const QString name
= data
.toString();
162 if (!name
.isEmpty()) {
163 if (!item
.isHidden() && name
.at(0).isLetter())
164 retString
= name
.at(0).toUpper();
165 else if (item
.isHidden()) {
166 if (name
.at(0) == '.') {
167 if (name
.size() > 1 && name
.at(1).isLetter()) {
168 retString
= name
.at(1).toUpper();
170 retString
= i18nc("@title:group Name", m_others
);
173 retString
= name
.at(0).toUpper();
176 bool validCategory
= false;
178 const QString
str(name
.toUpper());
179 const QChar
* currA
= str
.unicode();
180 while (!currA
->isNull() && !validCategory
) {
181 if (currA
->isLetter()) {
182 validCategory
= true;
183 } else if (currA
->isDigit()) {
184 return i18nc("@title:group Name", m_others
);
190 if (!validCategory
) {
191 retString
= validCategory
? *currA
: i18nc("@title:group Name", m_others
);
200 case KDirModel::Size
: {
201 const KIO::filesize_t fileSize
= !item
.isNull() ? item
.size() : ~0U;
202 if (!item
.isNull() && item
.isDir()) {
203 retString
= i18nc("@title:group Size", "Folders");
204 } else if (fileSize
< 5242880) {
205 retString
= i18nc("@title:group Size", "Small");
206 } else if (fileSize
< 10485760) {
207 retString
= i18nc("@title:group Size", "Medium");
209 retString
= i18nc("@title:group Size", "Big");
214 case KDirModel::ModifiedTime
: {
215 KDateTime modifiedTime
= item
.time(KFileItem::ModificationTime
);
216 modifiedTime
= modifiedTime
.toLocalZone();
218 const QDate currentDate
= KDateTime::currentLocalDateTime().date();
219 const QDate modifiedDate
= modifiedTime
.date();
221 const int daysDistance
= modifiedDate
.daysTo(currentDate
);
223 int yearForCurrentWeek
= 0;
224 int currentWeek
= currentDate
.weekNumber(&yearForCurrentWeek
);
225 if (yearForCurrentWeek
== currentDate
.year() + 1) {
229 int yearForModifiedWeek
= 0;
230 int modifiedWeek
= modifiedDate
.weekNumber(&yearForModifiedWeek
);
231 if (yearForModifiedWeek
== modifiedDate
.year() + 1) {
235 if (currentDate
.year() == modifiedDate
.year() && currentDate
.month() == modifiedDate
.month()) {
236 switch (currentWeek
- modifiedWeek
) {
238 switch (daysDistance
) {
239 case 0: retString
= i18nc("@title:group Date", "Today"); break;
240 case 1: retString
= i18nc("@title:group Date", "Yesterday"); break;
241 default: retString
= modifiedTime
.toString(i18nc("@title:group The week day name: %A", "%A"));
245 retString
= i18nc("@title:group Date", "Last Week");
248 retString
= i18nc("@title:group Date", "Two Weeks Ago");
251 retString
= i18nc("@title:group Date", "Three Weeks Ago");
255 retString
= i18nc("@title:group Date", "Earlier this Month");
261 const QDate lastMonthDate
= currentDate
.addMonths(-1);
262 if (lastMonthDate
.year() == modifiedDate
.year() && lastMonthDate
.month() == modifiedDate
.month()) {
263 if (daysDistance
== 1) {
264 retString
= modifiedTime
.toString(i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Yesterday (%B, %Y)"));
265 } else if (daysDistance
<= 7) {
266 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)"));
267 } else if (daysDistance
<= 7 * 2) {
268 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)"));
269 } else if (daysDistance
<= 7 * 3) {
270 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)"));
271 } else if (daysDistance
<= 7 * 4) {
272 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)"));
274 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"));
277 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"));
283 case KDirModel::Permissions
: {
288 QFileInfo
info(item
.url().pathOrUrl());
291 if (info
.permission(QFile::ReadUser
)) {
292 user
= i18nc("@item:intext Access permission, concatenated", "Read, ");
294 if (info
.permission(QFile::WriteUser
)) {
295 user
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
297 if (info
.permission(QFile::ExeUser
)) {
298 user
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
300 user
= user
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : user
.mid(0, user
.count() - 2);
303 if (info
.permission(QFile::ReadGroup
)) {
304 group
= i18nc("@item:intext Access permission, concatenated", "Read, ");
306 if (info
.permission(QFile::WriteGroup
)) {
307 group
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
309 if (info
.permission(QFile::ExeGroup
)) {
310 group
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
312 group
= group
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : group
.mid(0, group
.count() - 2);
314 // set permission string
315 if (info
.permission(QFile::ReadOther
)) {
316 others
= i18nc("@item:intext Access permission, concatenated", "Read, ");
318 if (info
.permission(QFile::WriteOther
)) {
319 others
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
321 if (info
.permission(QFile::ExeOther
)) {
322 others
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
324 others
= others
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : others
.mid(0, others
.count() - 2);
326 retString
= i18nc("@title:group Files and folders by permissions", "(User: %1) (Group: %2) (Others: %3)", user
, group
, others
);
330 case KDirModel::Owner
:
331 retString
= item
.user();
334 case KDirModel::Group
:
335 retString
= item
.group();
338 case KDirModel::Type
:
339 retString
= item
.mimeComment();
342 case DolphinModel::Revision
:
350 QVariant
DolphinModel::sortRoleData(const QModelIndex
& index
) const
354 if (!index
.isValid()) {
358 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
359 KFileItem item
= dirModel
->itemForIndex(index
);
361 switch (index
.column()) {
362 case KDirModel::Name
: {
363 retVariant
= data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
);
364 if (retVariant
== i18nc("@title:group Name", m_others
)) {
365 // assure that the "Others" group is always the last categorization
366 retVariant
= QString(QChar(QChar::ReplacementCharacter
));
371 case KDirModel::Size
: {
372 const KIO::filesize_t fileSize
= !item
.isNull() ? item
.size() : ~0U;
375 } else if (fileSize
< 5242880) {
377 } else if (fileSize
< 10485760) {
385 case KDirModel::ModifiedTime
: {
386 KDateTime modifiedTime
= item
.time(KFileItem::ModificationTime
);
387 modifiedTime
= modifiedTime
.toLocalZone();
389 const QDate currentDate
= KDateTime::currentLocalDateTime().date();
390 const QDate modifiedDate
= modifiedTime
.date();
392 retVariant
= -modifiedDate
.daysTo(currentDate
);
396 case KDirModel::Permissions
: {
397 QFileInfo
info(item
.url().pathOrUrl());
399 retVariant
= -KDirSortFilterProxyModel::pointsForPermissions(info
);
403 case KDirModel::Owner
:
404 retVariant
= item
.user();
407 case KDirModel::Group
:
408 retVariant
= item
.group();
411 case KDirModel::Type
:
413 // when sorting we want folders to be placed first
414 retVariant
= QString(); // krazy:exclude=nullstrassign
416 retVariant
= item
.mimeComment();