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