]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinitemcategorizer.cpp
Small fixes, but that have an impact of how things are visualized.
[dolphin.git] / src / dolphinitemcategorizer.cpp
1 /**
2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@gmail.com>
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 "dolphinitemcategorizer.h"
22
23 #include "dolphinview.h"
24 #include "dolphinsortfilterproxymodel.h"
25
26 #ifdef HAVE_NEPOMUK
27 #include <config-nepomuk.h>
28 #include <nepomuk/global.h>
29 #include <nepomuk/resource.h>
30 #endif
31
32 #include <kdatetime.h>
33 #include <kdirmodel.h>
34 #include <kfileitem.h>
35 #include <klocale.h>
36 #include <kurl.h>
37
38 #include <QList>
39 #include <QSortFilterProxyModel>
40
41 DolphinItemCategorizer::DolphinItemCategorizer() :
42 KItemCategorizer()
43 {
44 }
45
46 DolphinItemCategorizer::~DolphinItemCategorizer()
47 {
48 }
49
50 QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
51 int sortRole)
52 {
53 QString retString;
54
55 if (!index.isValid())
56 {
57 return retString;
58 }
59
60 const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
61 KFileItem *item = dirModel->itemForIndex(index);
62
63 switch (sortRole)
64 {
65 case DolphinView::SortByName:
66 {
67 // KDirModel checks columns to know to which role are
68 // we talking about
69 QModelIndex theIndex = index.model()->index(index.row(),
70 KDirModel::Name,
71 index.parent());
72
73 if (!theIndex.isValid()) {
74 return retString;
75 }
76
77 QVariant data = theIndex.model()->data(theIndex, Qt::DisplayRole);
78 if (data.toString().size())
79 {
80 if (!item->isHidden() && data.toString().at(0).isLetter())
81 retString = data.toString().toUpper().at(0);
82 else if (item->isHidden() && data.toString().at(0) == '.' &&
83 data.toString().at(1).isLetter())
84 retString = data.toString().toUpper().at(1);
85 else if (item->isHidden() && data.toString().at(0) == '.' &&
86 !data.toString().at(1).isLetter())
87 retString = i18n("Others");
88 else if (item->isHidden() && data.toString().at(0) != '.')
89 retString = data.toString().toUpper().at(0);
90 else if (item->isHidden())
91 retString = data.toString().toUpper().at(0);
92 else
93 {
94 bool validCategory = false;
95
96 const QString str(data.toString().toUpper());
97 const QChar* currA = str.unicode();
98 while (!currA->isNull() && !validCategory) {
99 if (currA->isLetter())
100 validCategory = true;
101 else if (currA->isDigit())
102 return i18n("Others");
103 else
104 ++currA;
105 }
106
107 if (!validCategory)
108 retString = i18n("Others");
109 else
110 retString = *currA;
111 }
112 }
113 break;
114 }
115
116 case DolphinView::SortByDate:
117 {
118 KDateTime modifiedTime;
119 modifiedTime.setTime_t(item->time(KIO::UDS_MODIFICATION_TIME));
120 modifiedTime = modifiedTime.toLocalZone();
121
122 if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 0)
123 retString = i18n("Today");
124 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 1)
125 retString = i18n("Yesterday");
126 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 7)
127 retString = i18n("Less than a week");
128 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 31)
129 retString = i18n("Less than a month");
130 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 365)
131 retString = i18n("Less than a year");
132 else
133 retString = i18n("More than a year");
134 break;
135 }
136
137 case DolphinView::SortByPermissions:
138 retString = item->permissionsString();
139 break;
140
141 case DolphinView::SortByOwner:
142 retString = item->user();
143 break;
144
145 case DolphinView::SortByGroup:
146 retString = item->group();
147 break;
148
149 case DolphinView::SortBySize: {
150 const int fileSize = item ? item->size() : -1;
151 if (item && item->isDir()) {
152 retString = i18n("Folders");
153 } else if (fileSize < 5242880) {
154 retString = i18nc("Size", "Small");
155 } else if (fileSize < 10485760) {
156 retString = i18nc("Size", "Medium");
157 } else {
158 retString = i18nc("Size", "Big");
159 }
160 break;
161 }
162
163 case DolphinView::SortByType:
164 retString = item->mimeComment();
165 break;
166
167 #ifdef HAVE_NEPOMUK
168 case DolphinView::SortByRating: {
169 const quint32 rating = DolphinSortFilterProxyModel::ratingForIndex(index);
170 if (rating) {
171 retString = i18np("1 star", "%1 stars", rating);
172 } else {
173 retString = i18n("Not yet rated");
174 }
175 break;
176 }
177
178 case DolphinView::SortByTags: {
179 retString = DolphinSortFilterProxyModel::tagsForIndex(index);
180
181 if (retString.isEmpty())
182 retString = i18n("Not yet tagged");
183
184 break;
185 }
186 #endif
187 }
188
189 return retString;
190 }