]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinitemcategorizer.cpp
it's not needed anymore to remember the URL per column
[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 QChar* currA = data.toString().toUpper().unicode(); // iterator over a
118 while (!currA->isNull() && !validCategory) {
119 if (currA->isLetter())
120 validCategory = true;
121 else if (currA->isDigit())
122 return i18n("Others");
123 else
124 ++currA;
125 }
126
127 if (!validCategory)
128 retString = i18n("Others");
129 else
130 retString = *currA;
131 }
132 }
133 break;
134
135 case DolphinView::SortByDate:
136 modifiedTime.setTime_t(item->time(KIO::UDS_MODIFICATION_TIME));
137 modifiedTime = modifiedTime.toLocalZone();
138
139 if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 0)
140 retString = i18n("Today");
141 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 1)
142 retString = i18n("Yesterday");
143 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 7)
144 retString = i18n("Less than a week");
145 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 31)
146 retString = i18n("Less than a month");
147 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 365)
148 retString = i18n("Less than a year");
149 else
150 retString = i18n("More than a year");
151 break;
152
153 case DolphinView::SortByPermissions:
154 retString = item->permissionsString();
155 break;
156
157 case DolphinView::SortByOwner:
158 retString = item->user();
159 break;
160
161 case DolphinView::SortByGroup:
162 retString = item->group();
163 break;
164
165 case DolphinView::SortBySize:
166 fileSize = (item) ? item->size() : -1;
167 if (item && item->isDir()) {
168 retString = i18n("Folders");
169 } else if (fileSize < 5242880) {
170 retString = i18nc("Size", "Small");
171 } else if (fileSize < 10485760) {
172 retString = i18nc("Size", "Medium");
173 } else {
174 retString = i18nc("Size", "Big");
175 }
176 break;
177
178 case DolphinView::SortByType:
179 retString = item->mimeComment();
180 break;
181 }
182
183 return retString;
184 }