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