]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmodel.cpp
Pass ref instead of pointer in mouseOverInfo signal
[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 #include <QFileInfo>
50
51 DolphinModel::DolphinModel(QObject *parent)
52 : KDirModel(parent)
53 {
54 }
55
56 DolphinModel::~DolphinModel()
57 {
58 }
59
60 QVariant DolphinModel::data(const QModelIndex &index, int role) const
61 {
62 if (role == KCategorizedSortFilterProxyModel::CategoryRole)
63 {
64 QString retString;
65
66 if (!index.isValid())
67 {
68 return retString;
69 }
70
71 const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
72 KFileItem item = dirModel->itemForIndex(index);
73
74 switch (index.column())
75 {
76 case KDirModel::Name:
77 {
78 // KDirModel checks columns to know to which role are
79 // we talking about
80 QModelIndex theIndex = index.model()->index(index.row(),
81 KDirModel::Name,
82 index.parent());
83
84 if (!theIndex.isValid()) {
85 return retString;
86 }
87
88 QVariant data = theIndex.model()->data(theIndex, Qt::DisplayRole);
89 if (data.toString().size())
90 {
91 if (!item.isHidden() && data.toString().at(0).isLetter())
92 retString = data.toString().toUpper().at(0);
93 else if (item.isHidden() && data.toString().at(0) == '.' &&
94 data.toString().at(1).isLetter())
95 retString = data.toString().toUpper().at(1);
96 else if (item.isHidden() && data.toString().at(0) == '.' &&
97 !data.toString().at(1).isLetter())
98 retString = i18nc("@title:group Name", "Others");
99 else if (item.isHidden() && data.toString().at(0) != '.')
100 retString = data.toString().toUpper().at(0);
101 else if (item.isHidden())
102 retString = data.toString().toUpper().at(0);
103 else
104 {
105 bool validCategory = false;
106
107 const QString str(data.toString().toUpper());
108 const QChar* currA = str.unicode();
109 while (!currA->isNull() && !validCategory) {
110 if (currA->isLetter())
111 validCategory = true;
112 else if (currA->isDigit())
113 return i18nc("@title:group", "Others");
114 else
115 ++currA;
116 }
117
118 if (!validCategory)
119 retString = i18nc("@title:group Name", "Others");
120 else
121 retString = *currA;
122 }
123 }
124 break;
125 }
126
127 case KDirModel::Size: {
128 const int fileSize = !item.isNull() ? item.size() : -1;
129 if (!item.isNull() && item.isDir()) {
130 retString = i18nc("@title:group Size", "Folders");
131 } else if (fileSize < 5242880) {
132 retString = i18nc("@title:group Size", "Small");
133 } else if (fileSize < 10485760) {
134 retString = i18nc("@title:group Size", "Medium");
135 } else {
136 retString = i18nc("@title:group Size", "Big");
137 }
138 break;
139 }
140
141 case KDirModel::ModifiedTime:
142 {
143 KDateTime modifiedTime;
144 modifiedTime.setTime_t(item.time(KIO::UDSEntry::UDS_MODIFICATION_TIME));
145 modifiedTime = modifiedTime.toLocalZone();
146
147 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"));
148 break;
149 }
150
151 case KDirModel::Permissions:
152 {
153 QString user;
154 QString group;
155 QString others;
156
157 QFileInfo info(item.url().pathOrUrl());
158
159 if (info.permission(QFile::ReadUser))
160 user = i18n("Read, ");
161
162 if (info.permission(QFile::WriteUser))
163 user += i18n("Write, ");
164
165 if (info.permission(QFile::ExeUser))
166 user += i18n("Execute, ");
167
168 if (user.isEmpty())
169 user = i18n("Forbidden");
170 else
171 user = user.mid(0, user.count() - 2);
172
173 if (info.permission(QFile::ReadGroup))
174 group = i18n("Read, ");
175
176 if (info.permission(QFile::WriteGroup))
177 group += i18n("Write, ");
178
179 if (info.permission(QFile::ExeGroup))
180 group += i18n("Execute, ");
181
182 if (group.isEmpty())
183 group = i18n("Forbidden");
184 else
185 group = group.mid(0, group.count() - 2);
186
187 if (info.permission(QFile::ReadOther))
188 others = i18n("Read, ");
189
190 if (info.permission(QFile::WriteOther))
191 others += i18n("Write, ");
192
193 if (info.permission(QFile::ExeOther))
194 others += i18n("Execute, ");
195
196 if (others.isEmpty())
197 others = i18n("Forbidden");
198 else
199 others = others.mid(0, others.count() - 2);
200
201 retString = i18nc("This shows files and folders permissions: user, group and others", "(User: %1) (Group: %2) (Others: %3)", user, group, others);
202 break;
203 }
204
205 case KDirModel::Owner:
206 retString = item.user();
207 break;
208
209 case KDirModel::Group:
210 retString = item.group();
211 break;
212
213 case KDirModel::Type:
214 retString = item.mimeComment();
215 break;
216
217 #ifdef HAVE_NEPOMUK
218 case DolphinModel::Rating: {
219 const quint32 rating = ratingForIndex(index);
220
221 retString = QString::number(rating);
222 break;
223 }
224
225 case DolphinModel::Tags: {
226 retString = tagsForIndex(index);
227
228 if (retString.isEmpty())
229 retString = i18nc("@title:group Tags", "Not yet tagged");
230
231 break;
232 }
233 #endif
234 }
235
236 return retString;
237 }
238
239 return KDirModel::data(index, role);
240 }
241
242 int DolphinModel::columnCount(const QModelIndex &parent) const
243 {
244 return KDirModel::columnCount(parent) + (ExtraColumnCount - ColumnCount);
245 }
246
247 quint32 DolphinModel::ratingForIndex(const QModelIndex& index)
248 {
249 #ifdef HAVE_NEPOMUK
250 quint32 rating = 0;
251
252 const DolphinModel* dolphinModel = static_cast<const DolphinModel*>(index.model());
253 KFileItem item = dolphinModel->itemForIndex(index);
254 if (!item.isNull()) {
255 const Nepomuk::Resource resource(item.url().url(), Nepomuk::NFO::File());
256 rating = resource.rating();
257 }
258 return rating;
259 #else
260 Q_UNUSED(index);
261 return 0;
262 #endif
263 }
264
265 QString DolphinModel::tagsForIndex(const QModelIndex& index)
266 {
267 #ifdef HAVE_NEPOMUK
268 QString tagsString;
269
270 const DolphinModel* dolphinModel = static_cast<const DolphinModel*>(index.model());
271 KFileItem item = dolphinModel->itemForIndex(index);
272 if (!item.isNull()) {
273 const Nepomuk::Resource resource(item.url().url(), Nepomuk::NFO::File());
274 const QList<Nepomuk::Tag> tags = resource.tags();
275 QStringList stringList;
276 foreach (const Nepomuk::Tag& tag, tags) {
277 stringList.append(tag.label());
278 }
279 stringList.sort();
280
281 foreach (const QString& str, stringList) {
282 tagsString += str;
283 tagsString += ", ";
284 }
285
286 if (!tagsString.isEmpty()) {
287 tagsString.resize(tagsString.size() - 2);
288 }
289 }
290
291 return tagsString;
292 #else
293 Q_UNUSED(index);
294 return QString();
295 #endif
296 }