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* DolphinModel::m_others
= I18N_NOOP2("@title:group Name", "Others");
46 DolphinModel::DolphinModel(QObject
* parent
) :
48 m_hasVersionData(false),
53 DolphinModel::~DolphinModel()
57 bool DolphinModel::setData(const QModelIndex
& index
, const QVariant
& value
, int role
)
59 if ((index
.column() == DolphinModel::Version
) && (role
== Qt::DecorationRole
)) {
60 // TODO: remove data again when items are deleted...
62 const QPersistentModelIndex key
= index
;
63 const KVersionControlPlugin::VersionState state
= static_cast<KVersionControlPlugin::VersionState
>(value
.toInt());
64 if (m_revisionHash
.value(key
, KVersionControlPlugin::UnversionedVersion
) != state
) {
65 if (!m_hasVersionData
) {
66 connect(this, SIGNAL(rowsRemoved (const QModelIndex
&, int, int)),
67 this, SLOT(slotRowsRemoved(const QModelIndex
&, int, int)));
68 m_hasVersionData
= true;
71 m_revisionHash
.insert(key
, state
);
72 emit
dataChanged(index
, index
);
77 return KDirModel::setData(index
, value
, role
);
80 QVariant
DolphinModel::data(const QModelIndex
& index
, int role
) const
83 case KCategorizedSortFilterProxyModel::CategoryDisplayRole
:
84 return displayRoleData(index
);
86 case KCategorizedSortFilterProxyModel::CategorySortRole
:
87 return sortRoleData(index
);
89 case Qt::DecorationRole
:
90 if (index
.column() == DolphinModel::Version
) {
91 return m_revisionHash
.value(index
, KVersionControlPlugin::UnversionedVersion
);
96 if (index
.column() == DolphinModel::Version
) {
97 switch (m_revisionHash
.value(index
, KVersionControlPlugin::UnversionedVersion
)) {
98 case KVersionControlPlugin::NormalVersion
:
99 return i18nc("@item::intable", "Normal");
100 case KVersionControlPlugin::UpdateRequiredVersion
:
101 return i18nc("@item::intable", "Update required");
102 case KVersionControlPlugin::LocallyModifiedVersion
:
103 return i18nc("@item::intable", "Locally modified");
104 case KVersionControlPlugin::AddedVersion
:
105 return i18nc("@item::intable", "Added");
106 case KVersionControlPlugin::RemovedVersion
:
107 return i18nc("@item::intable", "Removed");
108 case KVersionControlPlugin::ConflictingVersion
:
109 return i18nc("@item::intable", "Conflicting");
110 case KVersionControlPlugin::UnversionedVersion
:
112 return i18nc("@item::intable", "Unversioned");
121 return KDirModel::data(index
, role
);
124 QVariant
DolphinModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
126 if ((orientation
== Qt::Horizontal
) && (role
== Qt::DisplayRole
)) {
127 if (section
< KDirModel::ColumnCount
) {
128 return KDirModel::headerData(section
, orientation
, role
);
131 Q_ASSERT(section
== DolphinModel::Version
);
132 return i18nc("@title::column", "Version");
137 int DolphinModel::columnCount(const QModelIndex
& parent
) const
139 return KDirModel::columnCount(parent
) + (ExtraColumnCount
- ColumnCount
);
142 void DolphinModel::clearVersionData()
144 m_revisionHash
.clear();
145 m_hasVersionData
= false;
148 bool DolphinModel::hasVersionData() const
150 return m_hasVersionData
;
153 void DolphinModel::slotRowsRemoved(const QModelIndex
& parent
, int start
, int end
)
155 if (m_hasVersionData
) {
156 const int column
= parent
.column();
157 for (int row
= start
; row
<= end
; ++row
) {
158 m_revisionHash
.remove(parent
.child(row
, column
));
163 QVariant
DolphinModel::displayRoleData(const QModelIndex
& index
) const
167 if (!index
.isValid()) {
171 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
172 KFileItem item
= dirModel
->itemForIndex(index
);
174 switch (index
.column()) {
175 case KDirModel::Name
: {
176 // KDirModel checks columns to know to which role are
178 const QModelIndex nameIndex
= index
.model()->index(index
.row(), KDirModel::Name
, index
.parent());
179 if (!nameIndex
.isValid()) {
182 const QVariant data
= nameIndex
.model()->data(nameIndex
, Qt::DisplayRole
);
183 const QString name
= data
.toString();
184 if (!name
.isEmpty()) {
185 if (!item
.isHidden() && name
.at(0).isLetter())
186 retString
= name
.at(0).toUpper();
187 else if (item
.isHidden()) {
188 if (name
.at(0) == '.') {
189 if (name
.size() > 1 && name
.at(1).isLetter()) {
190 retString
= name
.at(1).toUpper();
192 retString
= i18nc("@title:group Name", m_others
);
195 retString
= name
.at(0).toUpper();
198 bool validCategory
= false;
200 const QString
str(name
.toUpper());
201 const QChar
* currA
= str
.unicode();
202 while (!currA
->isNull() && !validCategory
) {
203 if (currA
->isLetter()) {
204 validCategory
= true;
205 } else if (currA
->isDigit()) {
206 return i18nc("@title:group Name", m_others
);
212 retString
= validCategory
? *currA
: i18nc("@title:group Name", m_others
);
218 case KDirModel::Size
: {
219 const KIO::filesize_t fileSize
= !item
.isNull() ? item
.size() : ~0U;
220 if (!item
.isNull() && item
.isDir()) {
221 retString
= i18nc("@title:group Size", "Folders");
222 } else if (fileSize
< 5242880) {
223 retString
= i18nc("@title:group Size", "Small");
224 } else if (fileSize
< 10485760) {
225 retString
= i18nc("@title:group Size", "Medium");
227 retString
= i18nc("@title:group Size", "Big");
232 case KDirModel::ModifiedTime
: {
233 KDateTime modifiedTime
= item
.time(KFileItem::ModificationTime
);
234 modifiedTime
= modifiedTime
.toLocalZone();
236 const QDate currentDate
= KDateTime::currentLocalDateTime().date();
237 const QDate modifiedDate
= modifiedTime
.date();
239 const int daysDistance
= modifiedDate
.daysTo(currentDate
);
241 int yearForCurrentWeek
= 0;
242 int currentWeek
= currentDate
.weekNumber(&yearForCurrentWeek
);
243 if (yearForCurrentWeek
== currentDate
.year() + 1) {
247 int yearForModifiedWeek
= 0;
248 int modifiedWeek
= modifiedDate
.weekNumber(&yearForModifiedWeek
);
249 if (yearForModifiedWeek
== modifiedDate
.year() + 1) {
253 if (currentDate
.year() == modifiedDate
.year() && currentDate
.month() == modifiedDate
.month()) {
254 switch (currentWeek
- modifiedWeek
) {
256 switch (daysDistance
) {
257 case 0: retString
= i18nc("@title:group Date", "Today"); break;
258 case 1: retString
= i18nc("@title:group Date", "Yesterday"); break;
259 default: retString
= modifiedTime
.toString(i18nc("@title:group The week day name: %A", "%A"));
263 retString
= i18nc("@title:group Date", "Last Week");
266 retString
= i18nc("@title:group Date", "Two Weeks Ago");
269 retString
= i18nc("@title:group Date", "Three Weeks Ago");
273 retString
= i18nc("@title:group Date", "Earlier this Month");
279 const QDate lastMonthDate
= currentDate
.addMonths(-1);
280 if (lastMonthDate
.year() == modifiedDate
.year() && lastMonthDate
.month() == modifiedDate
.month()) {
281 if (daysDistance
== 1) {
282 retString
= modifiedTime
.toString(i18nc("@title:group Date: %B is full month name in current locale, and %Y is full year number", "Yesterday (%B, %Y)"));
283 } else if (daysDistance
<= 7) {
284 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)"));
285 } else if (daysDistance
<= 7 * 2) {
286 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)"));
287 } else if (daysDistance
<= 7 * 3) {
288 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)"));
289 } else if (daysDistance
<= 7 * 4) {
290 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)"));
292 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"));
295 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"));
301 case KDirModel::Permissions
: {
306 QFileInfo
info(item
.url().pathOrUrl());
309 if (info
.permission(QFile::ReadUser
)) {
310 user
= i18nc("@item:intext Access permission, concatenated", "Read, ");
312 if (info
.permission(QFile::WriteUser
)) {
313 user
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
315 if (info
.permission(QFile::ExeUser
)) {
316 user
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
318 user
= user
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : user
.mid(0, user
.count() - 2);
321 if (info
.permission(QFile::ReadGroup
)) {
322 group
= i18nc("@item:intext Access permission, concatenated", "Read, ");
324 if (info
.permission(QFile::WriteGroup
)) {
325 group
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
327 if (info
.permission(QFile::ExeGroup
)) {
328 group
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
330 group
= group
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : group
.mid(0, group
.count() - 2);
332 // set permission string
333 if (info
.permission(QFile::ReadOther
)) {
334 others
= i18nc("@item:intext Access permission, concatenated", "Read, ");
336 if (info
.permission(QFile::WriteOther
)) {
337 others
+= i18nc("@item:intext Access permission, concatenated", "Write, ");
339 if (info
.permission(QFile::ExeOther
)) {
340 others
+= i18nc("@item:intext Access permission, concatenated", "Execute, ");
342 others
= others
.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : others
.mid(0, others
.count() - 2);
344 retString
= i18nc("@title:group Files and folders by permissions", "(User: %1) (Group: %2) (Others: %3)", user
, group
, others
);
348 case KDirModel::Owner
:
349 retString
= item
.user();
352 case KDirModel::Group
:
353 retString
= item
.group();
356 case KDirModel::Type
:
357 retString
= item
.mimeComment();
360 case DolphinModel::Version
:
368 QVariant
DolphinModel::sortRoleData(const QModelIndex
& index
) const
372 if (!index
.isValid()) {
376 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
377 KFileItem item
= dirModel
->itemForIndex(index
);
379 switch (index
.column()) {
380 case KDirModel::Name
: {
381 retVariant
= data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
);
382 if (retVariant
== i18nc("@title:group Name", m_others
)) {
383 // assure that the "Others" group is always the last categorization
384 retVariant
= QString('Z').append(QChar::ReplacementCharacter
);
389 case KDirModel::Size
: {
390 const KIO::filesize_t fileSize
= !item
.isNull() ? item
.size() : ~0U;
393 } else if (fileSize
< 5242880) {
395 } else if (fileSize
< 10485760) {
403 case KDirModel::ModifiedTime
: {
404 KDateTime modifiedTime
= item
.time(KFileItem::ModificationTime
);
405 modifiedTime
= modifiedTime
.toLocalZone();
407 const QDate currentDate
= KDateTime::currentLocalDateTime().date();
408 const QDate modifiedDate
= modifiedTime
.date();
410 retVariant
= -modifiedDate
.daysTo(currentDate
);
414 case KDirModel::Permissions
: {
415 QFileInfo
info(item
.url().pathOrUrl());
417 retVariant
= -KDirSortFilterProxyModel::pointsForPermissions(info
);
421 case KDirModel::Owner
:
422 retVariant
= item
.user();
425 case KDirModel::Group
:
426 retVariant
= item
.group();
429 case KDirModel::Type
:
431 // when sorting we want folders to be placed first
432 retVariant
= QString(); // krazy:exclude=nullstrassign
434 retVariant
= item
.mimeComment();