]>
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>
34 #include <kdatetime.h>
35 #include <kdirmodel.h>
36 #include <kfileitem.h>
37 #include <kiconloader.h>
41 #include <kmimetype.h>
42 #include <kstandarddirs.h>
45 #include <QSortFilterProxyModel>
50 DolphinModel :: DolphinModel ( QObject
* parent
)
55 DolphinModel ::~ DolphinModel ()
59 QVariant
DolphinModel :: data ( const QModelIndex
& index
, int role
) const
61 if ( role
== KCategorizedSortFilterProxyModel :: CategoryDisplayRole
) {
64 if (! index
. isValid ()) {
68 const KDirModel
* dirModel
= qobject_cast
< const KDirModel
*>( index
. model ());
69 KFileItem item
= dirModel
-> itemForIndex ( index
);
71 switch ( index
. column ()) {
72 case KDirModel :: Name
: {
73 // KDirModel checks columns to know to which role are
75 QModelIndex theIndex
= index
. model ()-> index ( index
. row (),
79 if (! theIndex
. isValid ()) {
82 QVariant data
= theIndex
. model ()-> data ( theIndex
, Qt :: DisplayRole
);
83 QString name
= data
. toString ();
84 if (! name
. isEmpty ()) {
85 if (! item
. isHidden () && name
. at ( 0 ). isLetter ())
86 retString
= name
. at ( 0 ). toUpper ();
87 else if ( item
. isHidden ()) {
88 if ( name
. at ( 0 ) == '.' ) {
89 if ( data
. size () > 1 && name
. at ( 1 ). isLetter ())
90 retString
= name
. at ( 1 ). toUpper ();
92 retString
= i18nc ( "@title:group Name" , "Others" );
94 retString
= name
. at ( 0 ). toUpper ();
98 bool validCategory
= false ;
100 const QString
str ( name
. toUpper ());
101 const QChar
* currA
= str
. unicode ();
102 while (! currA
-> isNull () && ! validCategory
) {
103 if ( currA
-> isLetter ())
104 validCategory
= true ;
105 else if ( currA
-> isDigit ())
106 return i18nc ( "@title:group" , "Others" );
112 retString
= i18nc ( "@title:group Name" , "Others" );
120 case KDirModel :: Size
: {
121 const int fileSize
= ! item
. isNull () ? item
. size () : - 1 ;
122 if (! item
. isNull () && item
. isDir ()) {
123 retString
= i18nc ( "@title:group Size" , "Folders" );
124 } else if ( fileSize
< 5242880 ) {
125 retString
= i18nc ( "@title:group Size" , "Small" );
126 } else if ( fileSize
< 10485760 ) {
127 retString
= i18nc ( "@title:group Size" , "Medium" );
129 retString
= i18nc ( "@title:group Size" , "Big" );
134 case KDirModel :: ModifiedTime
: {
135 KDateTime modifiedTime
;
136 modifiedTime
. setTime_t ( item
. time ( KIO :: UDSEntry :: UDS_MODIFICATION_TIME
));
137 modifiedTime
= modifiedTime
. toLocalZone ();
139 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 " ));
143 case KDirModel :: Permissions
: {
148 QFileInfo
info ( item
. url (). pathOrUrl ());
150 if ( info
. permission ( QFile :: ReadUser
))
151 user
= i18n ( "Read, " );
153 if ( info
. permission ( QFile :: WriteUser
))
154 user
+= i18n ( "Write, " );
156 if ( info
. permission ( QFile :: ExeUser
))
157 user
+= i18n ( "Execute, " );
160 user
= i18n ( "Forbidden" );
162 user
= user
. mid ( 0 , user
. count () - 2 );
164 if ( info
. permission ( QFile :: ReadGroup
))
165 group
= i18n ( "Read, " );
167 if ( info
. permission ( QFile :: WriteGroup
))
168 group
+= i18n ( "Write, " );
170 if ( info
. permission ( QFile :: ExeGroup
))
171 group
+= i18n ( "Execute, " );
174 group
= i18n ( "Forbidden" );
176 group
= group
. mid ( 0 , group
. count () - 2 );
178 if ( info
. permission ( QFile :: ReadOther
))
179 others
= i18n ( "Read, " );
181 if ( info
. permission ( QFile :: WriteOther
))
182 others
+= i18n ( "Write, " );
184 if ( info
. permission ( QFile :: ExeOther
))
185 others
+= i18n ( "Execute, " );
187 if ( others
. isEmpty ())
188 others
= i18n ( "Forbidden" );
190 others
= others
. mid ( 0 , others
. count () - 2 );
192 retString
= i18nc ( "This shows files and folders permissions: user, group and others" , "(User: %1 ) (Group: %2 ) (Others: %3 )" , user
, group
, others
);
196 case KDirModel :: Owner
:
197 retString
= item
. user ();
200 case KDirModel :: Group
:
201 retString
= item
. group ();
204 case KDirModel :: Type
:
205 retString
= item
. mimeComment ();
209 case DolphinModel :: Rating
: {
210 const quint32 rating
= ratingForIndex ( index
);
212 retString
= QString :: number ( rating
);
216 case DolphinModel :: Tags
: {
217 retString
= tagsForIndex ( index
);
219 if ( retString
. isEmpty ())
220 retString
= i18nc ( "@title:group Tags" , "Not yet tagged" );
229 else if ( role
== KCategorizedSortFilterProxyModel :: CategorySortRole
) {
232 if (! index
. isValid ()) {
236 const KDirModel
* dirModel
= qobject_cast
< const KDirModel
*>( index
. model ());
237 KFileItem item
= dirModel
-> itemForIndex ( index
);
239 switch ( index
. column ()) {
240 case KDirModel :: Name
: {
241 retVariant
= data ( index
, KCategorizedSortFilterProxyModel :: CategoryDisplayRole
);
245 case KDirModel :: Size
: {
246 const int fileSize
= ! item
. isNull () ? item
. size () : - 1 ;
249 } else if ( fileSize
< 5242880 ) {
251 } else if ( fileSize
< 10485760 ) {
259 case KDirModel :: ModifiedTime
: {
260 KDateTime modifiedTime
;
261 modifiedTime
. setTime_t ( item
. time ( KIO :: UDSEntry :: UDS_MODIFICATION_TIME
));
262 modifiedTime
= modifiedTime
. toLocalZone ();
264 retVariant
= modifiedTime
. date (). year () * 100 + modifiedTime
. date (). month ();
268 case KDirModel :: Permissions
: {
269 QFileInfo
info ( item
. url (). pathOrUrl ());
271 retVariant
= - KDirSortFilterProxyModel :: pointsForPermissions ( info
);
275 case KDirModel :: Owner
:
276 retVariant
= item
. user ();
279 case KDirModel :: Group
:
280 retVariant
= item
. group ();
283 case KDirModel :: Type
:
285 retVariant
= QString (); // when sorting we want folders to be placed first
287 retVariant
= item
. mimeComment ();
291 case DolphinModel :: Rating
: {
292 retVariant
= ratingForIndex ( index
);
296 case DolphinModel :: Tags
: {
297 retVariant
= tagsForIndex ( index
). count ();
310 return KDirModel :: data ( index
, role
);
313 int DolphinModel :: columnCount ( const QModelIndex
& parent
) const
315 return KDirModel :: columnCount ( parent
) + ( ExtraColumnCount
- ColumnCount
);
318 quint32
DolphinModel :: ratingForIndex ( const QModelIndex
& index
)
323 const DolphinModel
* dolphinModel
= static_cast < const DolphinModel
*>( index
. model ());
324 KFileItem item
= dolphinModel
-> itemForIndex ( index
);
325 if (! item
. isNull ()) {
326 const Nepomuk :: Resource
resource ( item
. url (). url (), Nepomuk :: NFO :: File ());
327 rating
= resource
. rating ();
336 QString
DolphinModel :: tagsForIndex ( const QModelIndex
& index
)
341 const DolphinModel
* dolphinModel
= static_cast < const DolphinModel
*>( index
. model ());
342 KFileItem item
= dolphinModel
-> itemForIndex ( index
);
343 if (! item
. isNull ()) {
344 const Nepomuk :: Resource
resource ( item
. url (). url (), Nepomuk :: NFO :: File ());
345 const QList
< Nepomuk :: Tag
> tags
= resource
. tags ();
346 QStringList stringList
;
347 foreach ( const Nepomuk :: Tag
& tag
, tags
) {
348 stringList
. append ( tag
. label ());
352 foreach ( const QString
& str
, stringList
) {
357 if (! tagsString
. isEmpty ()) {
358 tagsString
. resize ( tagsString
. size () - 2 );