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_hasVersionData(false),
61 DolphinModel::~DolphinModel()
65 bool DolphinModel::setData(const QModelIndex
& index
, const QVariant
& value
, int role
)
67 if ((index
.column() == DolphinModel::Version
) && (role
== Qt::DecorationRole
)) {
68 // TODO: remove data again when items are deleted...
70 const QPersistentModelIndex key
= index
;
71 const KVersionControlPlugin::VersionState state
= static_cast<KVersionControlPlugin::VersionState
>(value
.toInt());
72 if (m_revisionHash
.value(key
, KVersionControlPlugin::UnversionedVersion
) != state
) {
73 if (!m_hasVersionData
) {
74 connect(this, SIGNAL(rowsRemoved (const QModelIndex
&, int, int)),
75 this, SLOT(slotRowsRemoved(const QModelIndex
&, int, int)));
76 m_hasVersionData
= 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::Version
) {
99 return m_revisionHash
.value(index
, KVersionControlPlugin::UnversionedVersion
);
103 case Qt::DisplayRole
:
104 if (index
.column() == DolphinModel::Version
) {
105 switch (m_revisionHash
.value(index
, KVersionControlPlugin::UnversionedVersion
)) {
106 case KVersionControlPlugin::NormalVersion
:
107 return i18nc("@item::intable", "Normal");
108 case KVersionControlPlugin::UpdateRequiredVersion
:
109 return i18nc("@item::intable", "Update required");
110 case KVersionControlPlugin::LocallyModifiedVersion
:
111 return i18nc("@item::intable", "Locally modified");
112 case KVersionControlPlugin::AddedVersion
:
113 return i18nc("@item::intable", "Added");
114 case KVersionControlPlugin::RemovedVersion
:
115 return i18nc("@item::intable", "Removed");
116 case KVersionControlPlugin::ConflictingVersion
:
117 return i18nc("@item::intable", "Conflicting");
118 case KVersionControlPlugin::UnversionedVersion
:
120 return i18nc("@item::intable", "Unversioned");
129 return KDirModel::data(index
, role
);
132 QVariant
DolphinModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
134 if ((orientation
== Qt::Horizontal
) && (role
== Qt::DisplayRole
)) {
135 if (section
< KDirModel::ColumnCount
) {
136 return KDirModel::headerData(section
, orientation
, role
);
139 Q_ASSERT(section
== DolphinModel::Version
);
140 return i18nc("@title::column", "Version");
145 int DolphinModel::columnCount(const QModelIndex
& parent
) const
147 return KDirModel::columnCount(parent
) + (ExtraColumnCount
- ColumnCount
);
150 void DolphinModel::clearVersionData()
152 m_revisionHash
.clear();
153 m_hasVersionData
= false;
156 bool DolphinModel::hasVersionData() const
158 return m_hasVersionData
;
161 void DolphinModel::slotRowsRemoved(const QModelIndex
& parent
, int start
, int end
)
163 if (m_hasVersionData
) {
164 const int column
= parent
.column();
165 for (int row
= start
; row
<= end
; ++row
) {
166 m_revisionHash
.remove(parent
.child(row
, column
));
171 QVariant
DolphinModel::displayRoleData(const QModelIndex
& index
) const
175 if (!index
.isValid()) {
179 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
180 KFileItem item
= dirModel
->itemForIndex(index
);
182 switch (index
.column()) {
183 case KDirModel::Name
: {
184 // KDirModel checks columns to know to which role are
186 const QModelIndex nameIndex
= index
.model()->index(index
.row(), KDirModel::Name
, index
.parent());
187 if (!nameIndex
.isValid()) {
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();
200 retString
= i18nc("@title:group Name", m_others
);
203 retString
= name
.at(0).toUpper();
206 bool validCategory
= false;
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
);
220 retString
= validCategory
? *currA
: i18nc("@title:group Name", m_others
);
226 case KDirModel::Size
: {
227 const KIO::filesize_t fileSize
= !item
.isNull() ? item
.size() : ~0U;
228 if (!item
.isNull() && item
.isDir()) {
229 retString
= i18nc("@title:group Size", "Folders");
230 } else if (fileSize
< 5242880) {
231 retString
= i18nc("@title:group Size", "Small");
232 } else if (fileSize
< 10485760) {
233 retString
= i18nc("@title:group Size", "Medium");
235 retString
= i18nc("@title:group Size", "Big");
240 case KDirModel::ModifiedTime
: {
241 KDateTime modifiedTime
= item
.time(KFileItem::ModificationTime
);
242 modifiedTime
= modifiedTime
.toLocalZone();
244 const QDate currentDate
= KDateTime::currentLocalDateTime().date();
245 const QDate modifiedDate
= modifiedTime
.date();
247 const int daysDistance
= modifiedDate
.daysTo(currentDate
);
249 int yearForCurrentWeek
= 0;
250 int currentWeek
= currentDate
.weekNumber(&yearForCurrentWeek
);
251 if (yearForCurrentWeek
== currentDate
.year() + 1) {
255 int yearForModifiedWeek
= 0;
256 int modifiedWeek
= modifiedDate
.weekNumber(&yearForModifiedWeek
);
257 if (yearForModifiedWeek
== modifiedDate
.year() + 1) {
261 if (currentDate
.year() == modifiedDate
.year() && currentDate
.month() == modifiedDate
.month()) {
262 switch (currentWeek
- modifiedWeek
) {
264 switch (daysDistance
) {
265 case 0: retString
= i18nc("@title:group Date", "Today"); break;
266 case 1: retString
= i18nc("@title:group Date", "Yesterday"); break;
267 default: retString
= modifiedTime
.toString(i18nc("@title:group The week day name: %A", "%A"));
271 retString
= i18nc("@title:group Date", "Last Week");
274 retString
= i18nc("@title:group Date", "Two Weeks Ago");
277 retString
= i18nc("@title:group Date", "Three Weeks Ago");
281 retString
= i18nc("@title:group Date", "Earlier this Month");
287 const QDate lastMonthDate
= currentDate
.addMonths(-1);
288 if (lastMonthDate
.year() == modifiedDate
.year() && lastMonthDate
.month() == modifiedDate
.month()) {
289 if (daysDistance
== 1) {
290 retString
= modifiedTime
.toString(i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Yesterday (%B, %Y)"));
291 } else if (daysDistance
<= 7) {
292 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)"));
293 } else if (daysDistance
<= 7 * 2) {
294 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)"));
295 } else if (daysDistance
<= 7 * 3) {
296 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)"));
297 } else if (daysDistance
<= 7 * 4) {
298 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)"));
300 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"));
303 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"));
309 case KDirModel::Permissions
: {
314 QFileInfo
info(item
.url().pathOrUrl());
317 if (info
.permission(QFile::ReadUser
)) {
318 user
= i18nc("@item:intext Access permission, concatenated", "Read, ");
320 if (info
.permission(QFile::WriteUser
)) {
321 user
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
323 if (info
.permission(QFile::ExeUser
)) {
324 user
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
326 user
= user
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : user
.mid(0, user
.count() - 2);
329 if (info
.permission(QFile::ReadGroup
)) {
330 group
= i18nc("@item:intext Access permission, concatenated", "Read, ");
332 if (info
.permission(QFile::WriteGroup
)) {
333 group
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
335 if (info
.permission(QFile::ExeGroup
)) {
336 group
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
338 group
= group
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : group
.mid(0, group
.count() - 2);
340 // set permission string
341 if (info
.permission(QFile::ReadOther
)) {
342 others
= i18nc("@item:intext Access permission, concatenated", "Read, ");
344 if (info
.permission(QFile::WriteOther
)) {
345 others
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
347 if (info
.permission(QFile::ExeOther
)) {
348 others
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
350 others
= others
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : others
.mid(0, others
.count() - 2);
352 retString
= i18nc("@title:group Files and folders by permissions", "(User: %1) (Group: %2) (Others: %3)", user
, group
, others
);
356 case KDirModel::Owner
:
357 retString
= item
.user();
360 case KDirModel::Group
:
361 retString
= item
.group();
364 case KDirModel::Type
:
365 retString
= item
.mimeComment();
368 case DolphinModel::Version
:
376 QVariant
DolphinModel::sortRoleData(const QModelIndex
& index
) const
380 if (!index
.isValid()) {
384 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
385 KFileItem item
= dirModel
->itemForIndex(index
);
387 switch (index
.column()) {
388 case KDirModel::Name
: {
389 retVariant
= data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
);
390 if (retVariant
== i18nc("@title:group Name", m_others
)) {
391 // assure that the "Others" group is always the last categorization
392 retVariant
= QString('Z').append(QChar::ReplacementCharacter
);
397 case KDirModel::Size
: {
398 const KIO::filesize_t fileSize
= !item
.isNull() ? item
.size() : ~0U;
401 } else if (fileSize
< 5242880) {
403 } else if (fileSize
< 10485760) {
411 case KDirModel::ModifiedTime
: {
412 KDateTime modifiedTime
= item
.time(KFileItem::ModificationTime
);
413 modifiedTime
= modifiedTime
.toLocalZone();
415 const QDate currentDate
= KDateTime::currentLocalDateTime().date();
416 const QDate modifiedDate
= modifiedTime
.date();
418 retVariant
= -modifiedDate
.daysTo(currentDate
);
422 case KDirModel::Permissions
: {
423 QFileInfo
info(item
.url().pathOrUrl());
425 retVariant
= -KDirSortFilterProxyModel::pointsForPermissions(info
);
429 case KDirModel::Owner
:
430 retVariant
= item
.user();
433 case KDirModel::Group
:
434 retVariant
= item
.group();
437 case KDirModel::Type
:
439 // when sorting we want folders to be placed first
440 retVariant
= QString(); // krazy:exclude=nullstrassign
442 retVariant
= item
.mimeComment();