]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmodel.cpp
2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@gmail.com>
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"
27 #include "dolphinsortfilterproxymodel.h"
29 #include "kcategorizedview.h"
31 #include <config-nepomuk.h>
33 #include <nepomuk/global.h>
34 #include <nepomuk/resource.h>
35 #include <nepomuk/tag.h>
38 #include <kdatetime.h>
39 #include <kdirmodel.h>
40 #include <kfileitem.h>
41 #include <kiconloader.h>
45 #include <kmimetype.h>
46 #include <kstandarddirs.h>
47 #include <kpixmapeffect.h>
50 #include <QSortFilterProxyModel>
54 DolphinModel::DolphinModel(QObject
*parent
)
59 DolphinModel::~DolphinModel()
63 QVariant
DolphinModel::data(const QModelIndex
&index
, int role
) const
65 if (role
== KCategorizedSortFilterProxyModel::CategoryRole
)
74 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
75 KFileItem item
= dirModel
->itemForIndex(index
);
77 switch (index
.column())
81 // KDirModel checks columns to know to which role are
83 QModelIndex theIndex
= index
.model()->index(index
.row(),
87 if (!theIndex
.isValid()) {
91 QVariant data
= theIndex
.model()->data(theIndex
, Qt::DisplayRole
);
92 if (data
.toString().size())
94 if (!item
.isHidden() && data
.toString().at(0).isLetter())
95 retString
= data
.toString().toUpper().at(0);
96 else if (item
.isHidden() && data
.toString().at(0) == '.' &&
97 data
.toString().at(1).isLetter())
98 retString
= data
.toString().toUpper().at(1);
99 else if (item
.isHidden() && data
.toString().at(0) == '.' &&
100 !data
.toString().at(1).isLetter())
101 retString
= i18nc("@title:group Name", "Others");
102 else if (item
.isHidden() && data
.toString().at(0) != '.')
103 retString
= data
.toString().toUpper().at(0);
104 else if (item
.isHidden())
105 retString
= data
.toString().toUpper().at(0);
108 bool validCategory
= false;
110 const QString
str(data
.toString().toUpper());
111 const QChar
* currA
= str
.unicode();
112 while (!currA
->isNull() && !validCategory
) {
113 if (currA
->isLetter())
114 validCategory
= true;
115 else if (currA
->isDigit())
116 return i18nc("@title:group", "Others");
122 retString
= i18nc("@title:group Name", "Others");
130 case KDirModel::Size
: {
131 const int fileSize
= !item
.isNull() ? item
.size() : -1;
132 if (!item
.isNull() && item
.isDir()) {
133 retString
= i18nc("@title:group Size", "Folders");
134 } else if (fileSize
< 5242880) {
135 retString
= i18nc("@title:group Size", "Small");
136 } else if (fileSize
< 10485760) {
137 retString
= i18nc("@title:group Size", "Medium");
139 retString
= i18nc("@title:group Size", "Big");
144 case KDirModel::ModifiedTime
:
146 KDateTime modifiedTime
;
147 modifiedTime
.setTime_t(item
.time(KIO::UDSEntry::UDS_MODIFICATION_TIME
));
148 modifiedTime
= modifiedTime
.toLocalZone();
150 if (modifiedTime
.daysTo(KDateTime::currentLocalDateTime()) == 0)
151 retString
= i18nc("@title:group Date", "Today");
152 else if (modifiedTime
.daysTo(KDateTime::currentLocalDateTime()) == 1)
153 retString
= i18nc("@title:group Date", "Yesterday");
154 else if (modifiedTime
.daysTo(KDateTime::currentLocalDateTime()) < 7)
155 retString
= i18nc("@title:group Date", "Less than a week");
156 else if (modifiedTime
.daysTo(KDateTime::currentLocalDateTime()) < 31)
157 retString
= i18nc("@title:group Date", "Less than a month");
158 else if (modifiedTime
.daysTo(KDateTime::currentLocalDateTime()) < 365)
159 retString
= i18nc("@title:group Date", "Less than a year");
161 retString
= i18nc("@title:group Date", "More than a year");
165 case KDirModel::Permissions
:
171 mode_t permissions
= item
.permissions();
173 if (permissions
& S_IRUSR
)
174 user
= i18n("Read, ");
176 if (permissions
& S_IWUSR
)
177 user
+= i18n("Write, ");
179 if (permissions
& S_IXUSR
)
180 user
+= i18n("Execute, ");
183 user
= i18n("Forbidden");
185 user
= user
.mid(0, user
.count() - 2);
187 if (permissions
& S_IRGRP
)
188 group
= i18n("Read, ");
190 if (permissions
& S_IWGRP
)
191 group
+= i18n("Write, ");
193 if (permissions
& S_IXGRP
)
194 group
+= i18n("Execute, ");
197 group
= i18n("Forbidden");
199 group
= group
.mid(0, group
.count() - 2);
201 if (permissions
& S_IROTH
)
202 others
= i18n("Read, ");
204 if (permissions
& S_IWOTH
)
205 others
+= i18n("Write, ");
207 if (permissions
& S_IXOTH
)
208 others
+= i18n("Execute, ");
210 if (others
.isEmpty())
211 others
= i18n("Forbidden");
213 others
= others
.mid(0, others
.count() - 2);
215 retString
= i18nc("This shows files and folders permissions: user, group and others", "(User: %1) (Group: %2) (Others: %3)", user
, group
, others
);
219 case KDirModel::Owner
:
220 retString
= item
.user();
223 case KDirModel::Group
:
224 retString
= item
.group();
227 case KDirModel::Type
:
228 retString
= item
.mimeComment();
232 case DolphinModel::Rating
: {
233 const quint32 rating
= ratingForIndex(index
);
235 retString
= QString::number(rating
);
239 case DolphinModel::Tags
: {
240 retString
= tagsForIndex(index
);
242 if (retString
.isEmpty())
243 retString
= i18nc("@title:group Tags", "Not yet tagged");
253 return KDirModel::data(index
, role
);
256 int DolphinModel::columnCount(const QModelIndex
&parent
) const
258 return KDirModel::columnCount(parent
) + (ExtraColumnCount
- ColumnCount
);
261 quint32
DolphinModel::ratingForIndex(const QModelIndex
& index
)
266 const DolphinModel
* dolphinModel
= static_cast<const DolphinModel
*>(index
.model());
267 KFileItem item
= dolphinModel
->itemForIndex(index
);
268 if (!item
.isNull()) {
269 const Nepomuk::Resource
resource(item
.url().url(), Nepomuk::NFO::File());
270 rating
= resource
.rating();
279 QString
DolphinModel::tagsForIndex(const QModelIndex
& index
)
284 const DolphinModel
* dolphinModel
= static_cast<const DolphinModel
*>(index
.model());
285 KFileItem item
= dolphinModel
->itemForIndex(index
);
286 if (!item
.isNull()) {
287 const Nepomuk::Resource
resource(item
.url().url(), Nepomuk::NFO::File());
288 const QList
<Nepomuk::Tag
> tags
= resource
.tags();
289 QStringList stringList
;
290 foreach (const Nepomuk::Tag
& tag
, tags
) {
291 stringList
.append(tag
.label());
295 foreach (const QString
& str
, stringList
) {
300 if (!tagsString
.isEmpty()) {
301 tagsString
.resize(tagsString
.size() - 2);