]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinitemcategorizer.cpp
Fix memory problem, thanks to Holger Freyther for pointing out
[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
25 #include <klocale.h>
26 #include <kdirmodel.h>
27 #include <kdatetime.h>
28
29 #include <QtGui/QSortFilterProxyModel>
30
31 DolphinItemCategorizer::DolphinItemCategorizer() :
32 KItemCategorizer()
33 {
34 }
35
36 DolphinItemCategorizer::~DolphinItemCategorizer()
37 {
38 }
39
40 QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
41 int sortRole)
42 {
43 QString retString;
44
45 if (!index.isValid())
46 {
47 return retString;
48 }
49
50 int column;
51
52 switch (sortRole)
53 {
54 case DolphinView::SortByName: // KDirModel::Name
55 column = KDirModel::Name;
56 break;
57 case DolphinView::SortBySize: // KDirModel::Size
58 column = KDirModel::Size;
59 break;
60 case DolphinView::SortByDate: // KDirModel::ModifiedTime
61 column = KDirModel::ModifiedTime;
62 break;
63 case DolphinView::SortByPermissions: // KDirModel::Permissions
64 column = KDirModel::Permissions;
65 break;
66 case DolphinView::SortByOwner: // KDirModel::Owner
67 column = KDirModel::Owner;
68 break;
69 case DolphinView::SortByGroup: // KDirModel::Group
70 column = KDirModel::Group;
71 break;
72 case DolphinView::SortByType: // KDirModel::Type
73 column = KDirModel::Type;
74 break;
75 default:
76 column = KDirModel::Name;
77 }
78
79 // KDirModel checks columns to know to which role are
80 // we talking about
81 QModelIndex theIndex = index.model()->index(index.row(),
82 column,
83 index.parent());
84
85 if (!theIndex.isValid()) {
86 return retString;
87 }
88
89 QVariant data = theIndex.model()->data(theIndex, Qt::DisplayRole);
90
91 const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
92 KFileItem *item = dirModel->itemForIndex(index);
93
94 int fileSize;
95 KDateTime modifiedTime;
96 switch (sortRole)
97 {
98 case DolphinView::SortByName:
99 if (data.toString().size())
100 {
101 if (!item->isHidden() && data.toString().at(0).isLetter())
102 retString = data.toString().toUpper().at(0);
103 else if (item->isHidden() && data.toString().at(0) == '.' &&
104 data.toString().at(1).isLetter())
105 retString = data.toString().toUpper().at(1);
106 else if (item->isHidden() && data.toString().at(0) == '.' &&
107 !data.toString().at(1).isLetter())
108 retString = i18n("Others");
109 else if (item->isHidden() && data.toString().at(0) != '.')
110 retString = data.toString().toUpper().at(0);
111 else if (item->isHidden())
112 retString = data.toString().toUpper().at(0);
113 else
114 {
115 bool validCategory = false;
116
117 const QString str(data.toString().toUpper());
118 const QChar* currA = str.unicode();
119 while (!currA->isNull() && !validCategory) {
120 if (currA->isLetter())
121 validCategory = true;
122 else if (currA->isDigit())
123 return i18n("Others");
124 else
125 ++currA;
126 }
127
128 if (!validCategory)
129 retString = i18n("Others");
130 else
131 retString = *currA;
132 }
133 }
134 break;
135
136 case DolphinView::SortByDate:
137 modifiedTime.setTime_t(item->time(KIO::UDS_MODIFICATION_TIME));
138 modifiedTime = modifiedTime.toLocalZone();
139
140 if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 0)
141 retString = i18n("Today");
142 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 1)
143 retString = i18n("Yesterday");
144 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 7)
145 retString = i18n("Less than a week");
146 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 31)
147 retString = i18n("Less than a month");
148 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 365)
149 retString = i18n("Less than a year");
150 else
151 retString = i18n("More than a year");
152 break;
153
154 case DolphinView::SortByPermissions:
155 retString = item->permissionsString();
156 break;
157
158 case DolphinView::SortByOwner:
159 retString = item->user();
160 break;
161
162 case DolphinView::SortByGroup:
163 retString = item->group();
164 break;
165
166 case DolphinView::SortBySize:
167 fileSize = (item) ? item->size() : -1;
168 if (item && item->isDir()) {
169 retString = i18n("Folders");
170 } else if (fileSize < 5242880) {
171 retString = i18nc("Size", "Small");
172 } else if (fileSize < 10485760) {
173 retString = i18nc("Size", "Medium");
174 } else {
175 retString = i18nc("Size", "Big");
176 }
177 break;
178
179 case DolphinView::SortByType:
180 retString = item->mimeComment();
181 break;
182 }
183
184 return retString;
185 }