]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmodel.cpp
Create the new architecture for KCategorizedView. Now DolphinModel is created, inheri...
[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@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 "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 #include <kpixmapeffect.h>
44
45 #include <QList>
46 #include <QSortFilterProxyModel>
47 #include <QPainter>
48 #include <QDir>
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::CategoryRole)
62 {
63 QString retString;
64
65 if (!index.isValid())
66 {
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 {
75 case KDirModel::Name:
76 {
77 // KDirModel checks columns to know to which role are
78 // we talking about
79 QModelIndex theIndex = index.model()->index(index.row(),
80 KDirModel::Name,
81 index.parent());
82
83 if (!theIndex.isValid()) {
84 return retString;
85 }
86
87 QVariant data = theIndex.model()->data(theIndex, Qt::DisplayRole);
88 if (data.toString().size())
89 {
90 if (!item.isHidden() && data.toString().at(0).isLetter())
91 retString = data.toString().toUpper().at(0);
92 else if (item.isHidden() && data.toString().at(0) == '.' &&
93 data.toString().at(1).isLetter())
94 retString = data.toString().toUpper().at(1);
95 else if (item.isHidden() && data.toString().at(0) == '.' &&
96 !data.toString().at(1).isLetter())
97 retString = i18nc("@title:group Name", "Others");
98 else if (item.isHidden() && data.toString().at(0) != '.')
99 retString = data.toString().toUpper().at(0);
100 else if (item.isHidden())
101 retString = data.toString().toUpper().at(0);
102 else
103 {
104 bool validCategory = false;
105
106 const QString str(data.toString().toUpper());
107 const QChar* currA = str.unicode();
108 while (!currA->isNull() && !validCategory) {
109 if (currA->isLetter())
110 validCategory = true;
111 else if (currA->isDigit())
112 return i18nc("@title:group", "Others");
113 else
114 ++currA;
115 }
116
117 if (!validCategory)
118 retString = i18nc("@title:group Name", "Others");
119 else
120 retString = *currA;
121 }
122 }
123 break;
124 }
125
126 case KDirModel::Size: {
127 const int fileSize = !item.isNull() ? item.size() : -1;
128 if (!item.isNull() && item.isDir()) {
129 retString = i18nc("@title:group Size", "Folders");
130 } else if (fileSize < 5242880) {
131 retString = i18nc("@title:group Size", "Small");
132 } else if (fileSize < 10485760) {
133 retString = i18nc("@title:group Size", "Medium");
134 } else {
135 retString = i18nc("@title:group Size", "Big");
136 }
137 break;
138 }
139
140 case KDirModel::ModifiedTime:
141 {
142 KDateTime modifiedTime;
143 modifiedTime.setTime_t(item.time(KIO::UDSEntry::UDS_MODIFICATION_TIME));
144 modifiedTime = modifiedTime.toLocalZone();
145
146 if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 0)
147 retString = i18nc("@title:group Date", "Today");
148 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 1)
149 retString = i18nc("@title:group Date", "Yesterday");
150 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 7)
151 retString = i18nc("@title:group Date", "Less than a week");
152 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 31)
153 retString = i18nc("@title:group Date", "Less than a month");
154 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 365)
155 retString = i18nc("@title:group Date", "Less than a year");
156 else
157 retString = i18nc("@title:group Date", "More than a year");
158 break;
159 }
160
161 case KDirModel::Permissions:
162 retString = item.permissionsString();
163 break;
164
165 case KDirModel::Owner:
166 retString = item.user();
167 break;
168
169 case KDirModel::Group:
170 retString = item.group();
171 break;
172
173 case KDirModel::Type:
174 retString = item.mimeComment();
175 break;
176
177 #ifdef HAVE_NEPOMUK
178 case DolphinModel::Rating: {
179 const quint32 rating = ratingForIndex(index);
180
181 retString = QString::number(rating);
182 break;
183 }
184
185 case DolphinModel::Tags: {
186 retString = tagsForIndex(index);
187
188 if (retString.isEmpty())
189 retString = i18nc("@title:group Tags", "Not yet tagged");
190
191 break;
192 }
193 #endif
194 }
195
196 return retString;
197 }
198
199 return KDirModel::data(index, role);
200 }
201
202 int DolphinModel::columnCount(const QModelIndex &parent) const
203 {
204 return KDirModel::columnCount(parent) + (ExtraColumnCount - ColumnCount);
205 }
206
207 quint32 DolphinModel::ratingForIndex(const QModelIndex& index)
208 {
209 #ifdef HAVE_NEPOMUK
210 quint32 rating = 0;
211
212 const DolphinModel* dolphinModel = static_cast<const DolphinModel*>(index.model());
213 KFileItem item = dolphinModel->itemForIndex(index);
214 if (!item.isNull()) {
215 const Nepomuk::Resource resource(item.url().url(), Nepomuk::NFO::File());
216 rating = resource.rating();
217 }
218 return rating;
219 #else
220 Q_UNUSED(index);
221 return 0;
222 #endif
223 }
224
225 QString DolphinModel::tagsForIndex(const QModelIndex& index)
226 {
227 #ifdef HAVE_NEPOMUK
228 QString tagsString;
229
230 const DolphinModel* dolphinModel = static_cast<const DolphinModel*>(index.model());
231 KFileItem item = dolphinModel->itemForIndex(index);
232 if (!item.isNull()) {
233 const Nepomuk::Resource resource(item.url().url(), Nepomuk::NFO::File());
234 const QList<Nepomuk::Tag> tags = resource.tags();
235 QStringList stringList;
236 foreach (const Nepomuk::Tag& tag, tags) {
237 stringList.append(tag.label());
238 }
239 stringList.sort();
240
241 foreach (const QString& str, stringList) {
242 tagsString += str;
243 tagsString += ", ";
244 }
245
246 if (!tagsString.isEmpty()) {
247 tagsString.resize(tagsString.size() - 2);
248 }
249 }
250
251 return tagsString;
252 #else
253 Q_UNUSED(index);
254 return QString();
255 #endif
256 }