]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmodel.cpp
2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@kde.org>
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.
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.
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.
21 #include "dolphinmodel.h"
23 #include "dolphinsortfilterproxymodel.h"
25 #include "kcategorizedview.h"
27 #include <config-nepomuk.h>
29 #include <nepomuk/global.h>
30 #include <nepomuk/resource.h>
31 #include <nepomuk/tag.h>
32 #include <Soprano/Vocabulary/Xesam>
35 #include <kdatetime.h>
36 #include <kdirmodel.h>
37 #include <kfileitem.h>
38 #include <kiconloader.h>
42 #include <kmimetype.h>
43 #include <kstandarddirs.h>
46 #include <QSortFilterProxyModel>
51 const char * DolphinModel :: m_others
= I18N_NOOP2 ( "@title:group Name" , "Others" );
53 DolphinModel :: DolphinModel ( QObject
* parent
)
58 DolphinModel ::~ DolphinModel ()
62 QVariant
DolphinModel :: data ( const QModelIndex
& index
, int role
) const
65 case KCategorizedSortFilterProxyModel :: CategoryDisplayRole
:
66 return displayRoleData ( index
);
67 case KCategorizedSortFilterProxyModel :: CategorySortRole
:
68 return sortRoleData ( index
);
70 return KDirModel :: data ( index
, role
);
74 int DolphinModel :: columnCount ( const QModelIndex
& parent
) const
76 return KDirModel :: columnCount ( parent
) + ( ExtraColumnCount
- ColumnCount
);
79 quint32
DolphinModel :: ratingForIndex ( const QModelIndex
& index
)
84 const DolphinModel
* dolphinModel
= static_cast < const DolphinModel
*>( index
. model ());
85 KFileItem item
= dolphinModel
-> itemForIndex ( index
);
87 const Nepomuk :: Resource
resource ( item
. url (). url (), Soprano :: Vocabulary :: Xesam :: File ());
88 rating
= resource
. rating ();
97 QString
DolphinModel :: tagsForIndex ( const QModelIndex
& index
)
102 const DolphinModel
* dolphinModel
= static_cast < const DolphinModel
*>( index
. model ());
103 KFileItem item
= dolphinModel
-> itemForIndex ( index
);
104 if (! item
. isNull ()) {
105 const Nepomuk :: Resource
resource ( item
. url (). url (), Soprano :: Vocabulary :: Xesam :: File ());
106 const QList
< Nepomuk :: Tag
> tags
= resource
. tags ();
107 QStringList stringList
;
108 foreach ( const Nepomuk :: Tag
& tag
, tags
) {
109 stringList
. append ( tag
. label ());
113 foreach ( const QString
& str
, stringList
) {
118 if (! tagsString
. isEmpty ()) {
119 tagsString
. resize ( tagsString
. size () - 2 );
130 QVariant
DolphinModel :: displayRoleData ( const QModelIndex
& index
) const
134 if (! index
. isValid ()) {
138 const KDirModel
* dirModel
= qobject_cast
< const KDirModel
*>( index
. model ());
139 KFileItem item
= dirModel
-> itemForIndex ( index
);
141 switch ( index
. column ()) {
142 case KDirModel :: Name
: {
143 // KDirModel checks columns to know to which role are
145 const QModelIndex nameIndex
= index
. model ()-> index ( index
. row (), KDirModel :: Name
, index
. parent ());
146 if (! nameIndex
. isValid ()) {
149 const QVariant data
= nameIndex
. model ()-> data ( nameIndex
, Qt :: DisplayRole
);
150 const QString name
= data
. toString ();
151 if (! name
. isEmpty ()) {
152 if (! item
. isHidden () && name
. at ( 0 ). isLetter ())
153 retString
= name
. at ( 0 ). toUpper ();
154 else if ( item
. isHidden ()) {
155 if ( name
. at ( 0 ) == '.' ) {
156 if ( name
. size () > 1 && name
. at ( 1 ). isLetter ()) {
157 retString
= name
. at ( 1 ). toUpper ();
159 retString
= i18nc ( "@title:group Name" , m_others
);
162 retString
= name
. at ( 0 ). toUpper ();
165 bool validCategory
= false ;
167 const QString
str ( name
. toUpper ());
168 const QChar
* currA
= str
. unicode ();
169 while (! currA
-> isNull () && ! validCategory
) {
170 if ( currA
-> isLetter ()) {
171 validCategory
= true ;
172 } else if ( currA
-> isDigit ()) {
173 return i18nc ( "@title:group Name" , m_others
);
179 if (! validCategory
) {
180 retString
= validCategory
? * currA
: i18nc ( "@title:group Name" , m_others
);
189 case KDirModel :: Size
: {
190 const KIO :: filesize_t fileSize
= ! item
. isNull () ? item
. size () : ~ 0U ;
191 if (! item
. isNull () && item
. isDir ()) {
192 retString
= i18nc ( "@title:group Size" , "Folders" );
193 } else if ( fileSize
< 5242880 ) {
194 retString
= i18nc ( "@title:group Size" , "Small" );
195 } else if ( fileSize
< 10485760 ) {
196 retString
= i18nc ( "@title:group Size" , "Medium" );
198 retString
= i18nc ( "@title:group Size" , "Big" );
203 case KDirModel :: ModifiedTime
: {
204 KDateTime modifiedTime
= item
. time ( KFileItem :: ModificationTime
);
205 modifiedTime
= modifiedTime
. toLocalZone ();
207 const QDate currentDate
= KDateTime :: currentLocalDateTime (). date ();
208 const QDate modifiedDate
= modifiedTime
. date ();
210 const int daysDistance
= modifiedDate
. daysTo ( currentDate
);
211 const int currentWeek
= currentDate
. weekNumber ();
212 const int modifiedWeek
= modifiedDate
. weekNumber ();
214 if ( currentDate
. year () == modifiedDate
. year () &&
215 currentDate
. month () == modifiedDate
. month ()) {
216 switch ( currentWeek
- modifiedWeek
) {
218 switch ( daysDistance
) {
219 case 0 : retString
= i18nc ( "@title:group Date" , "Today" ); break ;
220 case 1 : retString
= i18nc ( "@title:group Date" , "Yesterday" ); break ;
221 default : retString
= modifiedTime
. toString ( i18nc ( "@title:group The week day name: %A " , " %A " ));
225 retString
= i18nc ( "@title:group Date" , "Last Week" );
228 retString
= i18nc ( "@title:group Date" , "Two Weeks Ago" );
231 retString
= i18nc ( "@title:group Date" , "Three Weeks Ago" );
234 retString
= i18nc ( "@title:group Date" , "Earlier this Month" );
240 if ( daysDistance
<= ( currentDate
. day () + modifiedDate
. daysInMonth ())) {
241 if ( daysDistance
== 1 ) {
242 retString
= i18nc ( "@title:group Date: %B is full month name in current locale, and %Y is full year number" , "Yesterday ( %B , %Y )" );
243 } else if ( daysDistance
<= 7 ) {
244 retString
= modifiedTime
. toString ( i18nc ( "@title:group The week day name: %A , %B is full month name in current locale, and %Y is full year number" , " %A ( %B , %Y )" ));
245 } else if ( daysDistance
<= 7 * 2 ) {
246 retString
= modifiedTime
. toString ( i18nc ( "@title:group Date: %B is full month name in current locale, and %Y is full year number" , "Last Week ( %B , %Y )" ));
247 } else if ( daysDistance
<= 7 * 3 ) {
248 retString
= modifiedTime
. toString ( i18nc ( "@title:group Date: %B is full month name in current locale, and %Y is full year number" , "Two Weeks Ago ( %B , %Y )" ));
249 } else if ( daysDistance
<= 7 * 4 ) {
250 retString
= modifiedTime
. toString ( i18nc ( "@title:group Date: %B is full month name in current locale, and %Y is full year number" , "Three Weeks Ago ( %B , %Y )" ));
252 retString
= modifiedTime
. toString ( i18nc ( "@title:group Date: %B is full month name in current locale, and %Y is full year number" , "Earlier on %B , %Y " ));
255 retString
= modifiedTime
. toString ( i18nc ( "@title:group The month and year: %B is full month name in current locale, and %Y is full year number" , " %B , %Y " ));
261 case KDirModel :: Permissions
: {
266 QFileInfo
info ( item
. url (). pathOrUrl ());
269 if ( info
. permission ( QFile :: ReadUser
)) {
270 user
= i18nc ( "@item:intext Access permission, concatenated" , "Read, " );
272 if ( info
. permission ( QFile :: WriteUser
)) {
273 user
+= i18nc ( "@item:intext Access permission, concatenated" , "Write, " );
275 if ( info
. permission ( QFile :: ExeUser
)) {
276 user
+= i18nc ( "@item:intext Access permission, concatenated" , "Execute, " );
278 user
= user
. isEmpty () ? i18nc ( "@item:intext Access permission, concatenated" , "Forbidden" ) : user
. mid ( 0 , user
. count () - 2 );
281 if ( info
. permission ( QFile :: ReadGroup
)) {
282 group
= i18nc ( "@item:intext Access permission, concatenated" , "Read, " );
284 if ( info
. permission ( QFile :: WriteGroup
)) {
285 group
+= i18nc ( "@item:intext Access permission, concatenated" , "Write, " );
287 if ( info
. permission ( QFile :: ExeGroup
)) {
288 group
+= i18nc ( "@item:intext Access permission, concatenated" , "Execute, " );
290 group
= group
. isEmpty () ? i18nc ( "@item:intext Access permission, concatenated" , "Forbidden" ) : group
. mid ( 0 , group
. count () - 2 );
292 // set permission string
293 if ( info
. permission ( QFile :: ReadOther
)) {
294 others
= i18nc ( "@item:intext Access permission, concatenated" , "Read, " );
296 if ( info
. permission ( QFile :: WriteOther
)) {
297 others
+= i18nc ( "@item:intext Access permission, concatenated" , "Write, " );
299 if ( info
. permission ( QFile :: ExeOther
)) {
300 others
+= i18nc ( "@item:intext Access permission, concatenated" , "Execute, " );
302 others
= others
. isEmpty () ? i18nc ( "@item:intext Access permission, concatenated" , "Forbidden" ) : others
. mid ( 0 , others
. count () - 2 );
304 retString
= i18nc ( "@title:group Files and folders by permissions" , "(User: %1 ) (Group: %2 ) (Others: %3 )" , user
, group
, others
);
308 case KDirModel :: Owner
:
309 retString
= item
. user ();
312 case KDirModel :: Group
:
313 retString
= item
. group ();
316 case KDirModel :: Type
:
317 retString
= item
. mimeComment ();
321 case DolphinModel :: Rating
: {
322 const quint32 rating
= ratingForIndex ( index
);
323 retString
= QString :: number ( rating
);
327 case DolphinModel :: Tags
: {
328 retString
= tagsForIndex ( index
);
329 if ( retString
. isEmpty ()) {
330 retString
= i18nc ( "@title:group Tags" , "Not yet tagged" );
340 QVariant
DolphinModel :: sortRoleData ( const QModelIndex
& index
) const
344 if (! index
. isValid ()) {
348 const KDirModel
* dirModel
= qobject_cast
< const KDirModel
*>( index
. model ());
349 KFileItem item
= dirModel
-> itemForIndex ( index
);
351 switch ( index
. column ()) {
352 case KDirModel :: Name
: {
353 retVariant
= data ( index
, KCategorizedSortFilterProxyModel :: CategoryDisplayRole
);
354 if ( retVariant
== i18nc ( "@title:group Name" , m_others
)) {
355 // assure that the "Others" group is always the last categorization
356 retVariant
= QString ( QChar ( QChar :: ReplacementCharacter
));
361 case KDirModel :: Size
: {
362 const KIO :: filesize_t fileSize
= ! item
. isNull () ? item
. size () : ~ 0U ;
365 } else if ( fileSize
< 5242880 ) {
367 } else if ( fileSize
< 10485760 ) {
375 case KDirModel :: ModifiedTime
: {
376 KDateTime modifiedTime
= item
. time ( KFileItem :: ModificationTime
);
377 modifiedTime
= modifiedTime
. toLocalZone ();
379 const QDate currentDate
= KDateTime :: currentLocalDateTime (). date ();
380 const QDate modifiedDate
= modifiedTime
. date ();
382 retVariant
= - modifiedDate
. daysTo ( currentDate
);
386 case KDirModel :: Permissions
: {
387 QFileInfo
info ( item
. url (). pathOrUrl ());
389 retVariant
= - KDirSortFilterProxyModel :: pointsForPermissions ( info
);
393 case KDirModel :: Owner
:
394 retVariant
= item
. user ();
397 case KDirModel :: Group
:
398 retVariant
= item
. group ();
401 case KDirModel :: Type
:
403 retVariant
= QString (); // when sorting we want folders to be placed first
405 retVariant
= item
. mimeComment ();
409 case DolphinModel :: Rating
: {
410 retVariant
= ratingForIndex ( index
);
414 case DolphinModel :: Tags
: {
415 retVariant
= tagsForIndex ( index
). count ();