]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmodel.cpp
As requested by Peter: upgrade version to 1.0
[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@kde.org>
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 #include "dolphinsortfilterproxymodel.h"
24
25 #include "kcategorizedview.h"
26
27 #include <config-nepomuk.h>
28 #ifdef HAVE_NEPOMUK
29 #include <nepomuk/global.h>
30 #include <nepomuk/resource.h>
31 #include <nepomuk/tag.h>
32 #include <Soprano/Vocabulary/Xesam>
33 #endif
34
35 #include <kdatetime.h>
36 #include <kdirmodel.h>
37 #include <kfileitem.h>
38 #include <kiconloader.h>
39 #include <klocale.h>
40 #include <kurl.h>
41 #include <kuser.h>
42 #include <kmimetype.h>
43 #include <kstandarddirs.h>
44
45 #include <QList>
46 #include <QSortFilterProxyModel>
47 #include <QPainter>
48 #include <QDir>
49 #include <QFileInfo>
50
51 static const char *others = I18N_NOOP2("@title:group Name", "Others");
52
53 DolphinModel::DolphinModel(QObject *parent)
54 : KDirModel(parent)
55 {
56 }
57
58 DolphinModel::~DolphinModel()
59 {
60 }
61
62 QVariant DolphinModel::data(const QModelIndex &index, int role) const
63 {
64 if (role == KCategorizedSortFilterProxyModel::CategoryDisplayRole) {
65 QString retString;
66
67 if (!index.isValid()) {
68 return retString;
69 }
70
71 const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
72 KFileItem item = dirModel->itemForIndex(index);
73
74 switch (index.column()) {
75 case KDirModel::Name: {
76 // KDirModel checks columns to know to which role are
77 // we talking about
78 QModelIndex theIndex = index.model()->index(index.row(),
79 KDirModel::Name,
80 index.parent());
81
82 if (!theIndex.isValid()) {
83 return retString;
84 }
85 QVariant data = theIndex.model()->data(theIndex, Qt::DisplayRole);
86 QString name = data.toString();
87 if (!name.isEmpty()) {
88 if (!item.isHidden() && name.at(0).isLetter())
89 retString = name.at(0).toUpper();
90 else if (item.isHidden()) {
91 if (name.at(0) == '.') {
92 if (name.size() > 1 && name.at(1).isLetter()) {
93 retString = name.at(1).toUpper();
94 } else {
95 retString = i18nc("@title:group Name", others);
96 }
97 } else {
98 retString = name.at(0).toUpper();
99 }
100 } else {
101 bool validCategory = false;
102
103 const QString str(name.toUpper());
104 const QChar* currA = str.unicode();
105 while (!currA->isNull() && !validCategory) {
106 if (currA->isLetter()) {
107 validCategory = true;
108 } else if (currA->isDigit()) {
109 return i18nc("@title:group Name", others);
110 } else {
111 ++currA;
112 }
113 }
114
115 if (!validCategory) {
116 retString = validCategory ? *currA : i18nc("@title:group Name", others);
117 } else {
118 retString = *currA;
119 }
120 }
121 }
122 break;
123 }
124
125 case KDirModel::Size: {
126 const int fileSize = !item.isNull() ? item.size() : -1;
127 if (!item.isNull() && item.isDir()) {
128 retString = i18nc("@title:group Size", "Folders");
129 } else if (fileSize < 5242880) {
130 retString = i18nc("@title:group Size", "Small");
131 } else if (fileSize < 10485760) {
132 retString = i18nc("@title:group Size", "Medium");
133 } else {
134 retString = i18nc("@title:group Size", "Big");
135 }
136 break;
137 }
138
139 case KDirModel::ModifiedTime: {
140 KDateTime modifiedTime;
141 modifiedTime.setTime_t(item.time(KIO::UDSEntry::UDS_MODIFICATION_TIME));
142 modifiedTime = modifiedTime.toLocalZone();
143
144 retString = modifiedTime.toString(i18nc("Prints out the month and year: %B is full month name in current locale, and %Y is full year number", "%B, %Y"));
145 break;
146 }
147
148 case KDirModel::Permissions: {
149 QString user;
150 QString group;
151 QString others;
152
153 QFileInfo info(item.url().pathOrUrl());
154
155 if (info.permission(QFile::ReadUser))
156 user = i18n("Read, ");
157
158 if (info.permission(QFile::WriteUser))
159 user += i18n("Write, ");
160
161 if (info.permission(QFile::ExeUser))
162 user += i18n("Execute, ");
163
164 if (user.isEmpty())
165 user = i18n("Forbidden");
166 else
167 user = user.mid(0, user.count() - 2);
168
169 if (info.permission(QFile::ReadGroup))
170 group = i18n("Read, ");
171
172 if (info.permission(QFile::WriteGroup))
173 group += i18n("Write, ");
174
175 if (info.permission(QFile::ExeGroup))
176 group += i18n("Execute, ");
177
178 if (group.isEmpty())
179 group = i18n("Forbidden");
180 else
181 group = group.mid(0, group.count() - 2);
182
183 if (info.permission(QFile::ReadOther))
184 others = i18n("Read, ");
185
186 if (info.permission(QFile::WriteOther))
187 others += i18n("Write, ");
188
189 if (info.permission(QFile::ExeOther))
190 others += i18n("Execute, ");
191
192 if (others.isEmpty())
193 others = i18n("Forbidden");
194 else
195 others = others.mid(0, others.count() - 2);
196
197 retString = i18nc("This shows files and folders permissions: user, group and others", "(User: %1) (Group: %2) (Others: %3)", user, group, others);
198 break;
199 }
200
201 case KDirModel::Owner:
202 retString = item.user();
203 break;
204
205 case KDirModel::Group:
206 retString = item.group();
207 break;
208
209 case KDirModel::Type:
210 retString = item.mimeComment();
211 break;
212
213 #ifdef HAVE_NEPOMUK
214 case DolphinModel::Rating: {
215 const quint32 rating = ratingForIndex(index);
216
217 retString = QString::number(rating);
218 break;
219 }
220
221 case DolphinModel::Tags: {
222 retString = tagsForIndex(index);
223
224 if (retString.isEmpty()) {
225 retString = i18nc("@title:group Tags", "Not yet tagged");
226 }
227 break;
228 }
229 #endif
230 }
231
232 return retString;
233 }
234 else if (role == KCategorizedSortFilterProxyModel::CategorySortRole) {
235 QVariant retVariant;
236
237 if (!index.isValid()) {
238 return retVariant;
239 }
240
241 const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
242 KFileItem item = dirModel->itemForIndex(index);
243
244 switch (index.column()) {
245 case KDirModel::Name: {
246 retVariant = data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole);
247
248 if (retVariant == i18nc("@title:group Name", others))
249 retVariant = QString(QChar(QChar::ReplacementCharacter));
250
251 break;
252 }
253
254 case KDirModel::Size: {
255 const int fileSize = !item.isNull() ? item.size() : -1;
256 if (item.isDir()) {
257 retVariant = 0;
258 } else if (fileSize < 5242880) {
259 retVariant = 1;
260 } else if (fileSize < 10485760) {
261 retVariant = 2;
262 } else {
263 retVariant = 3;
264 }
265 break;
266 }
267
268 case KDirModel::ModifiedTime: {
269 KDateTime modifiedTime;
270 modifiedTime.setTime_t(item.time(KIO::UDSEntry::UDS_MODIFICATION_TIME));
271 modifiedTime = modifiedTime.toLocalZone();
272
273 retVariant = -(modifiedTime.date().year() * 100 + modifiedTime.date().month());
274 break;
275 }
276
277 case KDirModel::Permissions: {
278 QFileInfo info(item.url().pathOrUrl());
279
280 retVariant = -KDirSortFilterProxyModel::pointsForPermissions(info);
281 break;
282 }
283
284 case KDirModel::Owner:
285 retVariant = item.user();
286 break;
287
288 case KDirModel::Group:
289 retVariant = item.group();
290 break;
291
292 case KDirModel::Type:
293 if (item.isDir())
294 retVariant = QString(); // when sorting we want folders to be placed first
295 else
296 retVariant = item.mimeComment();
297 break;
298
299 #ifdef HAVE_NEPOMUK
300 case DolphinModel::Rating: {
301 retVariant = ratingForIndex(index);
302 break;
303 }
304
305 case DolphinModel::Tags: {
306 retVariant = tagsForIndex(index).count();
307 break;
308 }
309 #endif
310
311 default:
312 break;
313
314 }
315
316 return retVariant;
317 }
318
319 return KDirModel::data(index, role);
320 }
321
322 int DolphinModel::columnCount(const QModelIndex &parent) const
323 {
324 return KDirModel::columnCount(parent) + (ExtraColumnCount - ColumnCount);
325 }
326
327 quint32 DolphinModel::ratingForIndex(const QModelIndex& index)
328 {
329 #ifdef HAVE_NEPOMUK
330 quint32 rating = 0;
331
332 const DolphinModel* dolphinModel = static_cast<const DolphinModel*>(index.model());
333 KFileItem item = dolphinModel->itemForIndex(index);
334 if (!item.isNull()) {
335 const Nepomuk::Resource resource(item.url().url(), Soprano::Vocabulary::Xesam::File());
336 rating = resource.rating();
337 }
338 return rating;
339 #else
340 Q_UNUSED(index);
341 return 0;
342 #endif
343 }
344
345 QString DolphinModel::tagsForIndex(const QModelIndex& index)
346 {
347 #ifdef HAVE_NEPOMUK
348 QString tagsString;
349
350 const DolphinModel* dolphinModel = static_cast<const DolphinModel*>(index.model());
351 KFileItem item = dolphinModel->itemForIndex(index);
352 if (!item.isNull()) {
353 const Nepomuk::Resource resource(item.url().url(), Soprano::Vocabulary::Xesam::File());
354 const QList<Nepomuk::Tag> tags = resource.tags();
355 QStringList stringList;
356 foreach (const Nepomuk::Tag& tag, tags) {
357 stringList.append(tag.label());
358 }
359 stringList.sort();
360
361 foreach (const QString& str, stringList) {
362 tagsString += str;
363 tagsString += ", ";
364 }
365
366 if (!tagsString.isEmpty()) {
367 tagsString.resize(tagsString.size() - 2);
368 }
369 }
370
371 return tagsString;
372 #else
373 Q_UNUSED(index);
374 return QString();
375 #endif
376 }