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