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 switch (index
.column()) {
98 case DolphinModel::LinkDest
: {
99 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
100 const KFileItem item
= dirModel
->itemForIndex(index
);
101 return item
.linkDest();
104 case DolphinModel::LocalPathOrUrl
:
105 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
106 const KFileItem item
= dirModel
->itemForIndex(index
);
107 return item
.url().directory();
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
)) {
122 case DolphinModel::LinkDest
:
123 return i18nc("@title::column", "Link Destination");
124 case DolphinModel::LocalPathOrUrl
:
125 return i18nc("@title::column", "Path");
127 return KDirModel::headerData(section
, orientation
, role
);
133 int DolphinModel::columnCount(const QModelIndex
& parent
) const
135 return KDirModel::columnCount(parent
) + (ExtraColumnCount
- ColumnCount
);
138 void DolphinModel::clearVersionData()
140 m_revisionHash
.clear();
141 m_hasVersionData
= false;
144 bool DolphinModel::hasVersionData() const
146 return m_hasVersionData
;
149 void DolphinModel::slotRowsRemoved(const QModelIndex
& parent
, int start
, int end
)
151 if (m_hasVersionData
) {
152 const int column
= parent
.column();
153 for (int row
= start
; row
<= end
; ++row
) {
154 m_revisionHash
.remove(parent
.child(row
, column
));
159 QVariant
DolphinModel::displayRoleData(const QModelIndex
& index
) const
163 if (!index
.isValid()) {
167 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
168 KFileItem item
= dirModel
->itemForIndex(index
);
170 switch (index
.column()) {
171 case KDirModel::Name
: {
172 // KDirModel checks columns to know to which role are
174 const QModelIndex nameIndex
= index
.model()->index(index
.row(), KDirModel::Name
, index
.parent());
175 if (!nameIndex
.isValid()) {
178 const QVariant data
= nameIndex
.model()->data(nameIndex
, Qt::DisplayRole
);
179 const QString name
= data
.toString();
180 if (!name
.isEmpty()) {
181 if (!item
.isHidden() && name
.at(0).isLetter())
182 retString
= name
.at(0).toUpper();
183 else if (item
.isHidden()) {
184 if (name
.at(0) == '.') {
185 if (name
.size() > 1 && name
.at(1).isLetter()) {
186 retString
= name
.at(1).toUpper();
188 retString
= i18nc("@title:group Name", m_others
);
191 retString
= name
.at(0).toUpper();
194 bool validCategory
= false;
196 const QString
str(name
.toUpper());
197 const QChar
* currA
= str
.unicode();
198 while (!currA
->isNull() && !validCategory
) {
199 if (currA
->isLetter()) {
200 validCategory
= true;
201 } else if (currA
->isDigit()) {
202 return i18nc("@title:group Name", m_others
);
208 retString
= validCategory
? *currA
: i18nc("@title:group Name", m_others
);
214 case KDirModel::Size
: {
215 const KIO::filesize_t fileSize
= !item
.isNull() ? item
.size() : ~0U;
216 if (!item
.isNull() && item
.isDir()) {
217 retString
= i18nc("@title:group Size", "Folders");
218 } else if (fileSize
< 5242880) {
219 retString
= i18nc("@title:group Size", "Small");
220 } else if (fileSize
< 10485760) {
221 retString
= i18nc("@title:group Size", "Medium");
223 retString
= i18nc("@title:group Size", "Big");
228 case KDirModel::ModifiedTime
: {
229 KDateTime modifiedTime
= item
.time(KFileItem::ModificationTime
);
230 modifiedTime
= modifiedTime
.toLocalZone();
232 const QDate currentDate
= KDateTime::currentLocalDateTime().date();
233 const QDate modifiedDate
= modifiedTime
.date();
235 const int daysDistance
= modifiedDate
.daysTo(currentDate
);
237 int yearForCurrentWeek
= 0;
238 int currentWeek
= currentDate
.weekNumber(&yearForCurrentWeek
);
239 if (yearForCurrentWeek
== currentDate
.year() + 1) {
243 int yearForModifiedWeek
= 0;
244 int modifiedWeek
= modifiedDate
.weekNumber(&yearForModifiedWeek
);
245 if (yearForModifiedWeek
== modifiedDate
.year() + 1) {
249 if (currentDate
.year() == modifiedDate
.year() && currentDate
.month() == modifiedDate
.month()) {
250 if (modifiedWeek
> currentWeek
) {
251 // use case: modified date = 2010-01-01, current date = 2010-01-22
252 // modified week = 53, current week = 3
255 switch (currentWeek
- modifiedWeek
) {
257 switch (daysDistance
) {
258 case 0: retString
= i18nc("@title:group Date", "Today"); break;
259 case 1: retString
= i18nc("@title:group Date", "Yesterday"); break;
260 default: retString
= modifiedTime
.toString(i18nc("@title:group The week day name: %A", "%A"));
264 retString
= i18nc("@title:group Date", "Last Week");
267 retString
= i18nc("@title:group Date", "Two Weeks Ago");
270 retString
= i18nc("@title:group Date", "Three Weeks Ago");
274 retString
= i18nc("@title:group Date", "Earlier this Month");
280 const QDate lastMonthDate
= currentDate
.addMonths(-1);
281 if (lastMonthDate
.year() == modifiedDate
.year() && lastMonthDate
.month() == modifiedDate
.month()) {
282 if (daysDistance
== 1) {
283 retString
= modifiedTime
.toString(i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Yesterday (%B, %Y)"));
284 } else if (daysDistance
<= 7) {
285 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)"));
286 } else if (daysDistance
<= 7 * 2) {
287 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)"));
288 } else if (daysDistance
<= 7 * 3) {
289 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)"));
290 } else if (daysDistance
<= 7 * 4) {
291 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)"));
293 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"));
296 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"));
302 case KDirModel::Permissions
: {
307 QFileInfo
info(item
.url().pathOrUrl());
310 if (info
.permission(QFile::ReadUser
)) {
311 user
= i18nc("@item:intext Access permission, concatenated", "Read, ");
313 if (info
.permission(QFile::WriteUser
)) {
314 user
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
316 if (info
.permission(QFile::ExeUser
)) {
317 user
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
319 user
= user
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : user
.mid(0, user
.count() - 2);
322 if (info
.permission(QFile::ReadGroup
)) {
323 group
= i18nc("@item:intext Access permission, concatenated", "Read, ");
325 if (info
.permission(QFile::WriteGroup
)) {
326 group
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
328 if (info
.permission(QFile::ExeGroup
)) {
329 group
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
331 group
= group
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : group
.mid(0, group
.count() - 2);
333 // set permission string
334 if (info
.permission(QFile::ReadOther
)) {
335 others
= i18nc("@item:intext Access permission, concatenated", "Read, ");
337 if (info
.permission(QFile::WriteOther
)) {
338 others
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
340 if (info
.permission(QFile::ExeOther
)) {
341 others
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
343 others
= others
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : others
.mid(0, others
.count() - 2);
345 retString
= i18nc("@title:group Files and folders by permissions", "(User: %1) (Group: %2) (Others: %3)", user
, group
, others
);
349 case KDirModel::Owner
:
350 retString
= item
.user();
353 case KDirModel::Group
:
354 retString
= item
.group();
357 case KDirModel::Type
:
358 retString
= item
.mimeComment();
361 case DolphinModel::Version
:
369 QVariant
DolphinModel::sortRoleData(const QModelIndex
& index
) const
373 if (!index
.isValid()) {
377 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
378 KFileItem item
= dirModel
->itemForIndex(index
);
380 switch (index
.column()) {
381 case KDirModel::Name
: {
382 retVariant
= data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
);
383 if (retVariant
== i18nc("@title:group Name", m_others
)) {
384 // assure that the "Others" group is always the last categorization
385 retVariant
= QString('Z').append(QChar::ReplacementCharacter
);
390 case KDirModel::Size
: {
391 const KIO::filesize_t fileSize
= !item
.isNull() ? item
.size() : ~0U;
394 } else if (fileSize
< 5242880) {
396 } else if (fileSize
< 10485760) {
404 case KDirModel::ModifiedTime
: {
405 KDateTime modifiedTime
= item
.time(KFileItem::ModificationTime
);
406 modifiedTime
= modifiedTime
.toLocalZone();
408 const QDate currentDate
= KDateTime::currentLocalDateTime().date();
409 const QDate modifiedDate
= modifiedTime
.date();
411 retVariant
= -modifiedDate
.daysTo(currentDate
);
415 case KDirModel::Permissions
: {
416 QFileInfo
info(item
.url().pathOrUrl());
418 retVariant
= -KDirSortFilterProxyModel::pointsForPermissions(info
);
422 case KDirModel::Owner
:
423 retVariant
= item
.user();
426 case KDirModel::Group
:
427 retVariant
= item
.group();
430 case KDirModel::Type
:
432 // when sorting we want folders to be placed first
433 retVariant
= QString(); // krazy:exclude=nullstrassign
435 retVariant
= item
.mimeComment();