]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmodel.cpp
* some internal cleanups
[dolphin.git] / src / dolphinmodel.cpp
1 /**
2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@gmail.com>
4 *
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.
9 *
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.
14 *
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.
19 */
20
21 #include "dolphinmodel.h"
22
23 extern "C" {
24 #include <sys/stat.h>
25 }
26
27 #include "dolphinsortfilterproxymodel.h"
28
29 #include "kcategorizedview.h"
30
31 #include <config-nepomuk.h>
32 #ifdef HAVE_NEPOMUK
33 #include <nepomuk/global.h>
34 #include <nepomuk/resource.h>
35 #include <nepomuk/tag.h>
36 #endif
37
38 #include <kdatetime.h>
39 #include <kdirmodel.h>
40 #include <kfileitem.h>
41 #include <kiconloader.h>
42 #include <klocale.h>
43 #include <kurl.h>
44 #include <kuser.h>
45 #include <kmimetype.h>
46 #include <kstandarddirs.h>
47 #include <kpixmapeffect.h>
48
49 #include <QList>
50 #include <QSortFilterProxyModel>
51 #include <QPainter>
52 #include <QDir>
53
54 DolphinModel::DolphinModel(QObject *parent)
55 : KDirModel(parent)
56 {
57 }
58
59 DolphinModel::~DolphinModel()
60 {
61 }
62
63 QVariant DolphinModel::data(const QModelIndex &index, int role) const
64 {
65 if (role == KCategorizedSortFilterProxyModel::CategoryRole)
66 {
67 QString retString;
68
69 if (!index.isValid())
70 {
71 return retString;
72 }
73
74 const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
75 KFileItem item = dirModel->itemForIndex(index);
76
77 switch (index.column())
78 {
79 case KDirModel::Name:
80 {
81 // KDirModel checks columns to know to which role are
82 // we talking about
83 QModelIndex theIndex = index.model()->index(index.row(),
84 KDirModel::Name,
85 index.parent());
86
87 if (!theIndex.isValid()) {
88 return retString;
89 }
90
91 QVariant data = theIndex.model()->data(theIndex, Qt::DisplayRole);
92 if (data.toString().size())
93 {
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);
106 else
107 {
108 bool validCategory = false;
109
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");
117 else
118 ++currA;
119 }
120
121 if (!validCategory)
122 retString = i18nc("@title:group Name", "Others");
123 else
124 retString = *currA;
125 }
126 }
127 break;
128 }
129
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");
138 } else {
139 retString = i18nc("@title:group Size", "Big");
140 }
141 break;
142 }
143
144 case KDirModel::ModifiedTime:
145 {
146 KDateTime modifiedTime;
147 modifiedTime.setTime_t(item.time(KIO::UDSEntry::UDS_MODIFICATION_TIME));
148 modifiedTime = modifiedTime.toLocalZone();
149
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");
160 else
161 retString = i18nc("@title:group Date", "More than a year");
162 break;
163 }
164
165 case KDirModel::Permissions:
166 {
167 QString user;
168 QString group;
169 QString others;
170
171 mode_t permissions = item.permissions();
172
173 if (permissions & S_IRUSR)
174 user = i18n("Read, ");
175
176 if (permissions & S_IWUSR)
177 user += i18n("Write, ");
178
179 if (permissions & S_IXUSR)
180 user += i18n("Execute, ");
181
182 if (user.isEmpty())
183 user = i18n("Forbidden");
184 else
185 user = user.mid(0, user.count() - 2);
186
187 if (permissions & S_IRGRP)
188 group = i18n("Read, ");
189
190 if (permissions & S_IWGRP)
191 group += i18n("Write, ");
192
193 if (permissions & S_IXGRP)
194 group += i18n("Execute, ");
195
196 if (group.isEmpty())
197 group = i18n("Forbidden");
198 else
199 group = group.mid(0, group.count() - 2);
200
201 if (permissions & S_IROTH)
202 others = i18n("Read, ");
203
204 if (permissions & S_IWOTH)
205 others += i18n("Write, ");
206
207 if (permissions & S_IXOTH)
208 others += i18n("Execute, ");
209
210 if (others.isEmpty())
211 others = i18n("Forbidden");
212 else
213 others = others.mid(0, others.count() - 2);
214
215 retString = i18nc("This shows files and folders permissions: user, group and others", "(User: %1) (Group: %2) (Others: %3)", user, group, others);
216 break;
217 }
218
219 case KDirModel::Owner:
220 retString = item.user();
221 break;
222
223 case KDirModel::Group:
224 retString = item.group();
225 break;
226
227 case KDirModel::Type:
228 retString = item.mimeComment();
229 break;
230
231 #ifdef HAVE_NEPOMUK
232 case DolphinModel::Rating: {
233 const quint32 rating = ratingForIndex(index);
234
235 retString = QString::number(rating);
236 break;
237 }
238
239 case DolphinModel::Tags: {
240 retString = tagsForIndex(index);
241
242 if (retString.isEmpty())
243 retString = i18nc("@title:group Tags", "Not yet tagged");
244
245 break;
246 }
247 #endif
248 }
249
250 return retString;
251 }
252
253 return KDirModel::data(index, role);
254 }
255
256 int DolphinModel::columnCount(const QModelIndex &parent) const
257 {
258 return KDirModel::columnCount(parent) + (ExtraColumnCount - ColumnCount);
259 }
260
261 quint32 DolphinModel::ratingForIndex(const QModelIndex& index)
262 {
263 #ifdef HAVE_NEPOMUK
264 quint32 rating = 0;
265
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();
271 }
272 return rating;
273 #else
274 Q_UNUSED(index);
275 return 0;
276 #endif
277 }
278
279 QString DolphinModel::tagsForIndex(const QModelIndex& index)
280 {
281 #ifdef HAVE_NEPOMUK
282 QString tagsString;
283
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());
292 }
293 stringList.sort();
294
295 foreach (const QString& str, stringList) {
296 tagsString += str;
297 tagsString += ", ";
298 }
299
300 if (!tagsString.isEmpty()) {
301 tagsString.resize(tagsString.size() - 2);
302 }
303 }
304
305 return tagsString;
306 #else
307 Q_UNUSED(index);
308 return QString();
309 #endif
310 }