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 <kdatetime.h>
28 #include <kdirmodel.h>
29 #include <kfileitem.h>
30 #include <kiconloader.h>
34 #include <kmimetype.h>
35 #include <kstandarddirs.h>
38 #include <QSortFilterProxyModel>
40 #include <QPersistentModelIndex>
44 const char* const DolphinModel::m_others
= I18N_NOOP2("@title:group Name", "Others");
46 DolphinModel::DolphinModel(QObject
* parent
) :
48 m_hasVersionData(false),
51 setJobTransfersVisible(true);
54 DolphinModel::~DolphinModel()
58 bool DolphinModel::setData(const QModelIndex
& index
, const QVariant
& value
, int role
)
60 if ((index
.column() == DolphinModel::Version
) && (role
== Qt::DecorationRole
)) {
61 // TODO: remove data again when items are deleted...
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;
72 m_revisionHash
.insert(key
, state
);
73 emit
dataChanged(index
, index
);
78 return KDirModel::setData(index
, value
, role
);
81 QVariant
DolphinModel::data(const QModelIndex
& index
, int role
) const
84 case KCategorizedSortFilterProxyModel::CategoryDisplayRole
:
85 return displayRoleData(index
);
87 case KCategorizedSortFilterProxyModel::CategorySortRole
:
88 return sortRoleData(index
);
90 case Qt::DecorationRole
:
91 if (index
.column() == DolphinModel::Version
) {
92 return m_revisionHash
.value(index
, KVersionControlPlugin::UnversionedVersion
);
97 if (index
.column() == DolphinModel::Version
) {
98 switch (m_revisionHash
.value(index
, KVersionControlPlugin::UnversionedVersion
)) {
99 case KVersionControlPlugin::NormalVersion
:
100 return i18nc("@item::intable", "Normal");
101 case KVersionControlPlugin::UpdateRequiredVersion
:
102 return i18nc("@item::intable", "Update required");
103 case KVersionControlPlugin::LocallyModifiedVersion
:
104 return i18nc("@item::intable", "Locally modified");
105 case KVersionControlPlugin::AddedVersion
:
106 return i18nc("@item::intable", "Added");
107 case KVersionControlPlugin::RemovedVersion
:
108 return i18nc("@item::intable", "Removed");
109 case KVersionControlPlugin::ConflictingVersion
:
110 return i18nc("@item::intable", "Conflicting");
111 case KVersionControlPlugin::UnversionedVersion
:
113 return i18nc("@item::intable", "Unversioned");
116 else if (index
.column() == DolphinModel::LinkDestination
) {
117 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
118 KFileItem item
= dirModel
->itemForIndex(index
);
119 return item
.linkDest();
128 return KDirModel::data(index
, role
);
131 QVariant
DolphinModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
133 if ((orientation
== Qt::Horizontal
) && (role
== Qt::DisplayRole
)) {
135 case DolphinModel::Version
:
136 return i18nc("@title::column", "Version");
137 case DolphinModel::LinkDestination
:
138 return i18nc("@title::column", "Link Destination");
140 return KDirModel::headerData(section
, orientation
, role
);
146 int DolphinModel::columnCount(const QModelIndex
& parent
) const
148 return KDirModel::columnCount(parent
) + (ExtraColumnCount
- ColumnCount
);
151 void DolphinModel::clearVersionData()
153 m_revisionHash
.clear();
154 m_hasVersionData
= false;
157 bool DolphinModel::hasVersionData() const
159 return m_hasVersionData
;
162 void DolphinModel::slotRowsRemoved(const QModelIndex
& parent
, int start
, int end
)
164 if (m_hasVersionData
) {
165 const int column
= parent
.column();
166 for (int row
= start
; row
<= end
; ++row
) {
167 m_revisionHash
.remove(parent
.child(row
, column
));
172 QVariant
DolphinModel::displayRoleData(const QModelIndex
& index
) const
176 if (!index
.isValid()) {
180 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
181 KFileItem item
= dirModel
->itemForIndex(index
);
183 switch (index
.column()) {
184 case KDirModel::Name
: {
185 // KDirModel checks columns to know to which role are
187 const QModelIndex nameIndex
= index
.model()->index(index
.row(), KDirModel::Name
, index
.parent());
188 if (!nameIndex
.isValid()) {
191 const QVariant data
= nameIndex
.model()->data(nameIndex
, Qt::DisplayRole
);
192 const QString name
= data
.toString();
193 if (!name
.isEmpty()) {
194 if (!item
.isHidden() && name
.at(0).isLetter())
195 retString
= name
.at(0).toUpper();
196 else if (item
.isHidden()) {
197 if (name
.at(0) == '.') {
198 if (name
.size() > 1 && name
.at(1).isLetter()) {
199 retString
= name
.at(1).toUpper();
201 retString
= i18nc("@title:group Name", m_others
);
204 retString
= name
.at(0).toUpper();
207 bool validCategory
= false;
209 const QString
str(name
.toUpper());
210 const QChar
* currA
= str
.unicode();
211 while (!currA
->isNull() && !validCategory
) {
212 if (currA
->isLetter()) {
213 validCategory
= true;
214 } else if (currA
->isDigit()) {
215 return i18nc("@title:group Name", m_others
);
221 retString
= validCategory
? *currA
: i18nc("@title:group Name", m_others
);
227 case KDirModel::Size
: {
228 const KIO::filesize_t fileSize
= !item
.isNull() ? item
.size() : ~0U;
229 if (!item
.isNull() && item
.isDir()) {
230 retString
= i18nc("@title:group Size", "Folders");
231 } else if (fileSize
< 5242880) {
232 retString
= i18nc("@title:group Size", "Small");
233 } else if (fileSize
< 10485760) {
234 retString
= i18nc("@title:group Size", "Medium");
236 retString
= i18nc("@title:group Size", "Big");
241 case KDirModel::ModifiedTime
: {
242 KDateTime modifiedTime
= item
.time(KFileItem::ModificationTime
);
243 modifiedTime
= modifiedTime
.toLocalZone();
245 const QDate currentDate
= KDateTime::currentLocalDateTime().date();
246 const QDate modifiedDate
= modifiedTime
.date();
248 const int daysDistance
= modifiedDate
.daysTo(currentDate
);
250 int yearForCurrentWeek
= 0;
251 int currentWeek
= currentDate
.weekNumber(&yearForCurrentWeek
);
252 if (yearForCurrentWeek
== currentDate
.year() + 1) {
256 int yearForModifiedWeek
= 0;
257 int modifiedWeek
= modifiedDate
.weekNumber(&yearForModifiedWeek
);
258 if (yearForModifiedWeek
== modifiedDate
.year() + 1) {
262 if (currentDate
.year() == modifiedDate
.year() && currentDate
.month() == modifiedDate
.month()) {
263 if (modifiedWeek
> currentWeek
) {
264 // use case: modified date = 2010-01-01, current date = 2010-01-22
265 // modified week = 53, current week = 3
268 switch (currentWeek
- modifiedWeek
) {
270 switch (daysDistance
) {
271 case 0: retString
= i18nc("@title:group Date", "Today"); break;
272 case 1: retString
= i18nc("@title:group Date", "Yesterday"); break;
273 default: retString
= modifiedTime
.toString(i18nc("@title:group The week day name: %A", "%A"));
277 retString
= i18nc("@title:group Date", "Last Week");
280 retString
= i18nc("@title:group Date", "Two Weeks Ago");
283 retString
= i18nc("@title:group Date", "Three Weeks Ago");
287 retString
= i18nc("@title:group Date", "Earlier this Month");
293 const QDate lastMonthDate
= currentDate
.addMonths(-1);
294 if (lastMonthDate
.year() == modifiedDate
.year() && lastMonthDate
.month() == modifiedDate
.month()) {
295 if (daysDistance
== 1) {
296 retString
= modifiedTime
.toString(i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Yesterday (%B, %Y)"));
297 } else if (daysDistance
<= 7) {
298 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)"));
299 } else if (daysDistance
<= 7 * 2) {
300 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)"));
301 } else if (daysDistance
<= 7 * 3) {
302 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)"));
303 } else if (daysDistance
<= 7 * 4) {
304 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)"));
306 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"));
309 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"));
315 case KDirModel::Permissions
: {
320 QFileInfo
info(item
.url().pathOrUrl());
323 if (info
.permission(QFile::ReadUser
)) {
324 user
= i18nc("@item:intext Access permission, concatenated", "Read, ");
326 if (info
.permission(QFile::WriteUser
)) {
327 user
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
329 if (info
.permission(QFile::ExeUser
)) {
330 user
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
332 user
= user
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : user
.mid(0, user
.count() - 2);
335 if (info
.permission(QFile::ReadGroup
)) {
336 group
= i18nc("@item:intext Access permission, concatenated", "Read, ");
338 if (info
.permission(QFile::WriteGroup
)) {
339 group
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
341 if (info
.permission(QFile::ExeGroup
)) {
342 group
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
344 group
= group
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : group
.mid(0, group
.count() - 2);
346 // set permission string
347 if (info
.permission(QFile::ReadOther
)) {
348 others
= i18nc("@item:intext Access permission, concatenated", "Read, ");
350 if (info
.permission(QFile::WriteOther
)) {
351 others
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
353 if (info
.permission(QFile::ExeOther
)) {
354 others
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
356 others
= others
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : others
.mid(0, others
.count() - 2);
358 retString
= i18nc("@title:group Files and folders by permissions", "(User: %1) (Group: %2) (Others: %3)", user
, group
, others
);
362 case KDirModel::Owner
:
363 retString
= item
.user();
366 case KDirModel::Group
:
367 retString
= item
.group();
370 case KDirModel::Type
:
371 retString
= item
.mimeComment();
374 case DolphinModel::Version
:
382 QVariant
DolphinModel::sortRoleData(const QModelIndex
& index
) const
386 if (!index
.isValid()) {
390 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
391 KFileItem item
= dirModel
->itemForIndex(index
);
393 switch (index
.column()) {
394 case KDirModel::Name
: {
395 retVariant
= data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
);
396 if (retVariant
== i18nc("@title:group Name", m_others
)) {
397 // assure that the "Others" group is always the last categorization
398 retVariant
= QString('Z').append(QChar::ReplacementCharacter
);
403 case KDirModel::Size
: {
404 const KIO::filesize_t fileSize
= !item
.isNull() ? item
.size() : ~0U;
407 } else if (fileSize
< 5242880) {
409 } else if (fileSize
< 10485760) {
417 case KDirModel::ModifiedTime
: {
418 KDateTime modifiedTime
= item
.time(KFileItem::ModificationTime
);
419 modifiedTime
= modifiedTime
.toLocalZone();
421 const QDate currentDate
= KDateTime::currentLocalDateTime().date();
422 const QDate modifiedDate
= modifiedTime
.date();
424 retVariant
= -modifiedDate
.daysTo(currentDate
);
428 case KDirModel::Permissions
: {
429 QFileInfo
info(item
.url().pathOrUrl());
431 retVariant
= -KDirSortFilterProxyModel::pointsForPermissions(info
);
435 case KDirModel::Owner
:
436 retVariant
= item
.user();
439 case KDirModel::Group
:
440 retVariant
= item
.group();
443 case KDirModel::Type
:
445 // when sorting we want folders to be placed first
446 retVariant
= QString(); // krazy:exclude=nullstrassign
448 retVariant
= item
.mimeComment();