]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmodel.cpp
Remove bad line
[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(data.size() > 1 && name.at(1).isLetter())
90 retString = name.at(1).toUpper();
91 else
92 retString = i18nc("@title:group Name", "Others");
93 } else
94 retString = name.at(0).toUpper();
95 }
96 else
97 {
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 if (!validCategory)
112 retString = i18nc("@title:group Name", "Others");
113 else
114 retString = *currA;
115 }
116 }
117 break;
118 }
119
120 case KDirModel::Size: {
121 const int fileSize = !item.isNull() ? item.size() : -1;
122 if (!item.isNull() && item.isDir()) {
123 retString = i18nc("@title:group Size", "Folders");
124 } else if (fileSize < 5242880) {
125 retString = i18nc("@title:group Size", "Small");
126 } else if (fileSize < 10485760) {
127 retString = i18nc("@title:group Size", "Medium");
128 } else {
129 retString = i18nc("@title:group Size", "Big");
130 }
131 break;
132 }
133
134 case KDirModel::ModifiedTime: {
135 KDateTime modifiedTime;
136 modifiedTime.setTime_t(item.time(KIO::UDSEntry::UDS_MODIFICATION_TIME));
137 modifiedTime = modifiedTime.toLocalZone();
138
139 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"));
140 break;
141 }
142
143 case KDirModel::Permissions: {
144 QString user;
145 QString group;
146 QString others;
147
148 QFileInfo info(item.url().pathOrUrl());
149
150 if (info.permission(QFile::ReadUser))
151 user = i18n("Read, ");
152
153 if (info.permission(QFile::WriteUser))
154 user += i18n("Write, ");
155
156 if (info.permission(QFile::ExeUser))
157 user += i18n("Execute, ");
158
159 if (user.isEmpty())
160 user = i18n("Forbidden");
161 else
162 user = user.mid(0, user.count() - 2);
163
164 if (info.permission(QFile::ReadGroup))
165 group = i18n("Read, ");
166
167 if (info.permission(QFile::WriteGroup))
168 group += i18n("Write, ");
169
170 if (info.permission(QFile::ExeGroup))
171 group += i18n("Execute, ");
172
173 if (group.isEmpty())
174 group = i18n("Forbidden");
175 else
176 group = group.mid(0, group.count() - 2);
177
178 if (info.permission(QFile::ReadOther))
179 others = i18n("Read, ");
180
181 if (info.permission(QFile::WriteOther))
182 others += i18n("Write, ");
183
184 if (info.permission(QFile::ExeOther))
185 others += i18n("Execute, ");
186
187 if (others.isEmpty())
188 others = i18n("Forbidden");
189 else
190 others = others.mid(0, others.count() - 2);
191
192 retString = i18nc("This shows files and folders permissions: user, group and others", "(User: %1) (Group: %2) (Others: %3)", user, group, others);
193 break;
194 }
195
196 case KDirModel::Owner:
197 retString = item.user();
198 break;
199
200 case KDirModel::Group:
201 retString = item.group();
202 break;
203
204 case KDirModel::Type:
205 retString = item.mimeComment();
206 break;
207
208 #ifdef HAVE_NEPOMUK
209 case DolphinModel::Rating: {
210 const quint32 rating = ratingForIndex(index);
211
212 retString = QString::number(rating);
213 break;
214 }
215
216 case DolphinModel::Tags: {
217 retString = tagsForIndex(index);
218
219 if (retString.isEmpty())
220 retString = i18nc("@title:group Tags", "Not yet tagged");
221
222 break;
223 }
224 #endif
225 }
226
227 return retString;
228 }
229 else if (role == KCategorizedSortFilterProxyModel::CategorySortRole) {
230 QVariant retVariant;
231
232 if (!index.isValid()) {
233 return retVariant;
234 }
235
236 const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
237 KFileItem item = dirModel->itemForIndex(index);
238
239 switch (index.column()) {
240 case KDirModel::Name: {
241 retVariant = data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole);
242 break;
243 }
244
245 case KDirModel::Size: {
246 const int fileSize = !item.isNull() ? item.size() : -1;
247 if (item.isDir()) {
248 retVariant = 0;
249 } else if (fileSize < 5242880) {
250 retVariant = 1;
251 } else if (fileSize < 10485760) {
252 retVariant = 2;
253 } else {
254 retVariant = 3;
255 }
256 break;
257 }
258
259 case KDirModel::ModifiedTime: {
260 KDateTime modifiedTime;
261 modifiedTime.setTime_t(item.time(KIO::UDSEntry::UDS_MODIFICATION_TIME));
262 modifiedTime = modifiedTime.toLocalZone();
263
264 retVariant = modifiedTime.date().year() * 100 + modifiedTime.date().month();
265 break;
266 }
267
268 case KDirModel::Permissions: {
269 QFileInfo info(item.url().pathOrUrl());
270
271 retVariant = -KDirSortFilterProxyModel::pointsForPermissions(info);
272 break;
273 }
274
275 case KDirModel::Owner:
276 retVariant = item.user();
277 break;
278
279 case KDirModel::Group:
280 retVariant = item.group();
281 break;
282
283 case KDirModel::Type:
284 if (item.isDir())
285 retVariant = QString(); // when sorting we want folders to be placed first
286 else
287 retVariant = item.mimeComment();
288 break;
289
290 #ifdef HAVE_NEPOMUK
291 case DolphinModel::Rating: {
292 retVariant = ratingForIndex(index);
293 break;
294 }
295
296 case DolphinModel::Tags: {
297 retVariant = tagsForIndex(index).count();
298 break;
299 }
300 #endif
301
302 default:
303 break;
304
305 }
306
307 return retVariant;
308 }
309
310 return KDirModel::data(index, role);
311 }
312
313 int DolphinModel::columnCount(const QModelIndex &parent) const
314 {
315 return KDirModel::columnCount(parent) + (ExtraColumnCount - ColumnCount);
316 }
317
318 quint32 DolphinModel::ratingForIndex(const QModelIndex& index)
319 {
320 #ifdef HAVE_NEPOMUK
321 quint32 rating = 0;
322
323 const DolphinModel* dolphinModel = static_cast<const DolphinModel*>(index.model());
324 KFileItem item = dolphinModel->itemForIndex(index);
325 if (!item.isNull()) {
326 const Nepomuk::Resource resource(item.url().url(), Nepomuk::NFO::File());
327 rating = resource.rating();
328 }
329 return rating;
330 #else
331 Q_UNUSED(index);
332 return 0;
333 #endif
334 }
335
336 QString DolphinModel::tagsForIndex(const QModelIndex& index)
337 {
338 #ifdef HAVE_NEPOMUK
339 QString tagsString;
340
341 const DolphinModel* dolphinModel = static_cast<const DolphinModel*>(index.model());
342 KFileItem item = dolphinModel->itemForIndex(index);
343 if (!item.isNull()) {
344 const Nepomuk::Resource resource(item.url().url(), Nepomuk::NFO::File());
345 const QList<Nepomuk::Tag> tags = resource.tags();
346 QStringList stringList;
347 foreach (const Nepomuk::Tag& tag, tags) {
348 stringList.append(tag.label());
349 }
350 stringList.sort();
351
352 foreach (const QString& str, stringList) {
353 tagsString += str;
354 tagsString += ", ";
355 }
356
357 if (!tagsString.isEmpty()) {
358 tagsString.resize(tagsString.size() - 2);
359 }
360 }
361
362 return tagsString;
363 #else
364 Q_UNUSED(index);
365 return QString();
366 #endif
367 }