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