]>
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 static const char * 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 QModelIndex theIndex
= index
. model ()-> index ( index
. row (),
149 if (! theIndex
. isValid ()) {
152 QVariant data
= theIndex
. model ()-> data ( theIndex
, Qt :: DisplayRole
);
153 QString name
= data
. toString ();
154 if (! name
. isEmpty ()) {
155 if (! item
. isHidden () && name
. at ( 0 ). isLetter ())
156 retString
= name
. at ( 0 ). toUpper ();
157 else if ( item
. isHidden ()) {
158 if ( name
. at ( 0 ) == '.' ) {
159 if ( name
. size () > 1 && name
. at ( 1 ). isLetter ()) {
160 retString
= name
. at ( 1 ). toUpper ();
162 retString
= i18nc ( "@title:group Name" , others
);
165 retString
= name
. at ( 0 ). toUpper ();
168 bool validCategory
= false ;
170 const QString
str ( name
. toUpper ());
171 const QChar
* currA
= str
. unicode ();
172 while (! currA
-> isNull () && ! validCategory
) {
173 if ( currA
-> isLetter ()) {
174 validCategory
= true ;
175 } else if ( currA
-> isDigit ()) {
176 return i18nc ( "@title:group Name" , others
);
182 if (! validCategory
) {
183 retString
= validCategory
? * currA
: i18nc ( "@title:group Name" , others
);
192 case KDirModel :: Size
: {
193 const KIO :: filesize_t fileSize
= ! item
. isNull () ? item
. size () : ~ 0U ;
194 if (! item
. isNull () && item
. isDir ()) {
195 retString
= i18nc ( "@title:group Size" , "Folders" );
196 } else if ( fileSize
< 5242880 ) {
197 retString
= i18nc ( "@title:group Size" , "Small" );
198 } else if ( fileSize
< 10485760 ) {
199 retString
= i18nc ( "@title:group Size" , "Medium" );
201 retString
= i18nc ( "@title:group Size" , "Big" );
206 case KDirModel :: ModifiedTime
: {
207 KDateTime modifiedTime
= item
. time ( KFileItem :: ModificationTime
);
208 modifiedTime
= modifiedTime
. toLocalZone ();
210 const QDate currentDate
= KDateTime :: currentLocalDateTime (). date ();
211 const QDate modifiedDate
= modifiedTime
. date ();
213 if (( currentDate
. year () == modifiedDate
. year ()) && ( currentDate
. month () == modifiedDate
. month ())) {
214 const int currentWeek
= currentDate
. weekNumber ();
215 const int modifiedWeek
= modifiedDate
. weekNumber ();
216 switch ( currentWeek
- modifiedWeek
) {
218 switch ( modifiedDate
. daysTo ( currentDate
)) {
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 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 " ));
245 case KDirModel :: Permissions
: {
250 QFileInfo
info ( item
. url (). pathOrUrl ());
253 if ( info
. permission ( QFile :: ReadUser
)) {
254 user
= i18nc ( "@item:intext Access permission, concatenated" , "Read, " );
256 if ( info
. permission ( QFile :: WriteUser
)) {
257 user
+= i18nc ( "@item:intext Access permission, concatenated" , "Write, " );
259 if ( info
. permission ( QFile :: ExeUser
)) {
260 user
+= i18nc ( "@item:intext Access permission, concatenated" , "Execute, " );
262 user
= user
. isEmpty () ? i18nc ( "@item:intext Access permission, concatenated" , "Forbidden" ) : user
. mid ( 0 , user
. count () - 2 );
265 if ( info
. permission ( QFile :: ReadGroup
)) {
266 group
= i18nc ( "@item:intext Access permission, concatenated" , "Read, " );
268 if ( info
. permission ( QFile :: WriteGroup
)) {
269 group
+= i18nc ( "@item:intext Access permission, concatenated" , "Write, " );
271 if ( info
. permission ( QFile :: ExeGroup
)) {
272 group
+= i18nc ( "@item:intext Access permission, concatenated" , "Execute, " );
274 group
= group
. isEmpty () ? i18nc ( "@item:intext Access permission, concatenated" , "Forbidden" ) : group
. mid ( 0 , group
. count () - 2 );
276 // set permission string
277 if ( info
. permission ( QFile :: ReadOther
)) {
278 others
= i18nc ( "@item:intext Access permission, concatenated" , "Read, " );
280 if ( info
. permission ( QFile :: WriteOther
)) {
281 others
+= i18nc ( "@item:intext Access permission, concatenated" , "Write, " );
283 if ( info
. permission ( QFile :: ExeOther
)) {
284 others
+= i18nc ( "@item:intext Access permission, concatenated" , "Execute, " );
286 others
= others
. isEmpty () ? i18nc ( "@item:intext Access permission, concatenated" , "Forbidden" ) : others
. mid ( 0 , others
. count () - 2 );
288 retString
= i18nc ( "@title:group Files and folders by permissions" , "(User: %1 ) (Group: %2 ) (Others: %3 )" , user
, group
, others
);
292 case KDirModel :: Owner
:
293 retString
= item
. user ();
296 case KDirModel :: Group
:
297 retString
= item
. group ();
300 case KDirModel :: Type
:
301 retString
= item
. mimeComment ();
305 case DolphinModel :: Rating
: {
306 const quint32 rating
= ratingForIndex ( index
);
307 retString
= QString :: number ( rating
);
311 case DolphinModel :: Tags
: {
312 retString
= tagsForIndex ( index
);
313 if ( retString
. isEmpty ()) {
314 retString
= i18nc ( "@title:group Tags" , "Not yet tagged" );
324 QVariant
DolphinModel :: sortRoleData ( const QModelIndex
& index
) const
328 if (! index
. isValid ()) {
332 const KDirModel
* dirModel
= qobject_cast
< const KDirModel
*>( index
. model ());
333 KFileItem item
= dirModel
-> itemForIndex ( index
);
335 switch ( index
. column ()) {
336 case KDirModel :: Name
: {
337 retVariant
= data ( index
, KCategorizedSortFilterProxyModel :: CategoryDisplayRole
);
339 if ( retVariant
== i18nc ( "@title:group Name" , others
)) {
340 retVariant
= QString ( QChar ( QChar :: ReplacementCharacter
));
345 case KDirModel :: Size
: {
346 const KIO :: filesize_t fileSize
= ! item
. isNull () ? item
. size () : ~ 0U ;
349 } else if ( fileSize
< 5242880 ) {
351 } else if ( fileSize
< 10485760 ) {
359 case KDirModel :: ModifiedTime
: {
360 KDateTime modifiedTime
= item
. time ( KFileItem :: ModificationTime
);
361 modifiedTime
= modifiedTime
. toLocalZone ();
363 const QDate currentDate
= KDateTime :: currentLocalDateTime (). date ();
364 const QDate modifiedDate
= modifiedTime
. date ();
368 if (( currentDate
. year () == modifiedDate
. year ()) && ( currentDate
. month () == modifiedDate
. month ())) {
369 weekOfMonth
= 4 - currentDate
. weekNumber () + modifiedDate
. weekNumber ();
370 Q_ASSERT ( weekOfMonth
>= 0 );
371 Q_ASSERT ( weekOfMonth
<= 4 );
372 if ( weekOfMonth
== 0 ) {
373 dayOfWeek
= modifiedDate
. dayOfWeek ();
377 retVariant
= modifiedDate
. year () * 10000 + modifiedDate
. month () * 100 +
378 weekOfMonth
* 10 + dayOfWeek
;
382 case KDirModel :: Permissions
: {
383 QFileInfo
info ( item
. url (). pathOrUrl ());
385 retVariant
= - KDirSortFilterProxyModel :: pointsForPermissions ( info
);
389 case KDirModel :: Owner
:
390 retVariant
= item
. user ();
393 case KDirModel :: Group
:
394 retVariant
= item
. group ();
397 case KDirModel :: Type
:
399 retVariant
= QString (); // when sorting we want folders to be placed first
401 retVariant
= item
. mimeComment ();
405 case DolphinModel :: Rating
: {
406 retVariant
= ratingForIndex ( index
);
410 case DolphinModel :: Tags
: {
411 retVariant
= tagsForIndex ( index
). count ();