]>
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 ()) {
86 if (! item
. isHidden () && name
. at ( 0 ). isLetter ())
87 retString
= name
. at ( 0 ). toUpper ();
88 else if ( item
. isHidden ()) {
89 if ( name
. at ( 0 ) == '.' ) {
90 if ( data
. size () > 1 && name
. at ( 1 ). isLetter ())
91 retString
= name
. at ( 1 ). toUpper ();
93 retString
= i18nc ( "@title:group Name" , "Others" );
95 retString
= name
. at ( 0 ). toUpper ();
99 bool validCategory
= false ;
101 const QString
str ( name
. toUpper ());
102 const QChar
* currA
= str
. unicode ();
103 while (! currA
-> isNull () && ! validCategory
) {
104 if ( currA
-> isLetter ())
105 validCategory
= true ;
106 else if ( currA
-> isDigit ())
107 return i18nc ( "@title:group" , "Others" );
113 retString
= i18nc ( "@title:group Name" , "Others" );
121 case KDirModel :: Size
: {
122 const int fileSize
= ! item
. isNull () ? item
. size () : - 1 ;
123 if (! item
. isNull () && item
. isDir ()) {
124 retString
= i18nc ( "@title:group Size" , "Folders" );
125 } else if ( fileSize
< 5242880 ) {
126 retString
= i18nc ( "@title:group Size" , "Small" );
127 } else if ( fileSize
< 10485760 ) {
128 retString
= i18nc ( "@title:group Size" , "Medium" );
130 retString
= i18nc ( "@title:group Size" , "Big" );
135 case KDirModel :: ModifiedTime
: {
136 KDateTime modifiedTime
;
137 modifiedTime
. setTime_t ( item
. time ( KIO :: UDSEntry :: UDS_MODIFICATION_TIME
));
138 modifiedTime
= modifiedTime
. toLocalZone ();
140 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 " ));
144 case KDirModel :: Permissions
: {
149 QFileInfo
info ( item
. url (). pathOrUrl ());
151 if ( info
. permission ( QFile :: ReadUser
))
152 user
= i18n ( "Read, " );
154 if ( info
. permission ( QFile :: WriteUser
))
155 user
+= i18n ( "Write, " );
157 if ( info
. permission ( QFile :: ExeUser
))
158 user
+= i18n ( "Execute, " );
161 user
= i18n ( "Forbidden" );
163 user
= user
. mid ( 0 , user
. count () - 2 );
165 if ( info
. permission ( QFile :: ReadGroup
))
166 group
= i18n ( "Read, " );
168 if ( info
. permission ( QFile :: WriteGroup
))
169 group
+= i18n ( "Write, " );
171 if ( info
. permission ( QFile :: ExeGroup
))
172 group
+= i18n ( "Execute, " );
175 group
= i18n ( "Forbidden" );
177 group
= group
. mid ( 0 , group
. count () - 2 );
179 if ( info
. permission ( QFile :: ReadOther
))
180 others
= i18n ( "Read, " );
182 if ( info
. permission ( QFile :: WriteOther
))
183 others
+= i18n ( "Write, " );
185 if ( info
. permission ( QFile :: ExeOther
))
186 others
+= i18n ( "Execute, " );
188 if ( others
. isEmpty ())
189 others
= i18n ( "Forbidden" );
191 others
= others
. mid ( 0 , others
. count () - 2 );
193 retString
= i18nc ( "This shows files and folders permissions: user, group and others" , "(User: %1 ) (Group: %2 ) (Others: %3 )" , user
, group
, others
);
197 case KDirModel :: Owner
:
198 retString
= item
. user ();
201 case KDirModel :: Group
:
202 retString
= item
. group ();
205 case KDirModel :: Type
:
206 retString
= item
. mimeComment ();
210 case DolphinModel :: Rating
: {
211 const quint32 rating
= ratingForIndex ( index
);
213 retString
= QString :: number ( rating
);
217 case DolphinModel :: Tags
: {
218 retString
= tagsForIndex ( index
);
220 if ( retString
. isEmpty ())
221 retString
= i18nc ( "@title:group Tags" , "Not yet tagged" );
230 else if ( role
== KCategorizedSortFilterProxyModel :: CategorySortRole
) {
233 if (! index
. isValid ()) {
237 const KDirModel
* dirModel
= qobject_cast
< const KDirModel
*>( index
. model ());
238 KFileItem item
= dirModel
-> itemForIndex ( index
);
240 switch ( index
. column ()) {
241 case KDirModel :: Name
: {
242 retVariant
= data ( index
, KCategorizedSortFilterProxyModel :: CategoryDisplayRole
);
246 case KDirModel :: Size
: {
247 const int fileSize
= ! item
. isNull () ? item
. size () : - 1 ;
250 } else if ( fileSize
< 5242880 ) {
252 } else if ( fileSize
< 10485760 ) {
260 case KDirModel :: ModifiedTime
: {
261 KDateTime modifiedTime
;
262 modifiedTime
. setTime_t ( item
. time ( KIO :: UDSEntry :: UDS_MODIFICATION_TIME
));
263 modifiedTime
= modifiedTime
. toLocalZone ();
265 retVariant
= modifiedTime
. date (). year () * 100 + modifiedTime
. date (). month ();
269 case KDirModel :: Permissions
: {
270 QFileInfo
info ( item
. url (). pathOrUrl ());
272 retVariant
= - KDirSortFilterProxyModel :: pointsForPermissions ( info
);
276 case KDirModel :: Owner
:
277 retVariant
= item
. user ();
280 case KDirModel :: Group
:
281 retVariant
= item
. group ();
284 case KDirModel :: Type
:
286 retVariant
= QString (); // when sorting we want folders to be placed first
288 retVariant
= item
. mimeComment ();
292 case DolphinModel :: Rating
: {
293 retVariant
= ratingForIndex ( index
);
297 case DolphinModel :: Tags
: {
298 retVariant
= tagsForIndex ( index
). count ();
311 return KDirModel :: data ( index
, role
);
314 int DolphinModel :: columnCount ( const QModelIndex
& parent
) const
316 return KDirModel :: columnCount ( parent
) + ( ExtraColumnCount
- ColumnCount
);
319 quint32
DolphinModel :: ratingForIndex ( const QModelIndex
& index
)
324 const DolphinModel
* dolphinModel
= static_cast < const DolphinModel
*>( index
. model ());
325 KFileItem item
= dolphinModel
-> itemForIndex ( index
);
326 if (! item
. isNull ()) {
327 const Nepomuk :: Resource
resource ( item
. url (). url (), Nepomuk :: NFO :: File ());
328 rating
= resource
. rating ();
337 QString
DolphinModel :: tagsForIndex ( const QModelIndex
& index
)
342 const DolphinModel
* dolphinModel
= static_cast < const DolphinModel
*>( index
. model ());
343 KFileItem item
= dolphinModel
-> itemForIndex ( index
);
344 if (! item
. isNull ()) {
345 const Nepomuk :: Resource
resource ( item
. url (). url (), Nepomuk :: NFO :: File ());
346 const QList
< Nepomuk :: Tag
> tags
= resource
. tags ();
347 QStringList stringList
;
348 foreach ( const Nepomuk :: Tag
& tag
, tags
) {
349 stringList
. append ( tag
. label ());
353 foreach ( const QString
& str
, stringList
) {
358 if (! tagsString
. isEmpty ()) {
359 tagsString
. resize ( tagsString
. size () - 2 );