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