]>
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 :: CategoryRole
)
70 const KDirModel
* dirModel
= qobject_cast
< const KDirModel
*>( index
. model ());
71 KFileItem item
= dirModel
-> itemForIndex ( index
);
73 switch ( index
. column ())
77 // KDirModel checks columns to know to which role are
79 QModelIndex theIndex
= index
. model ()-> index ( index
. row (),
83 if (! theIndex
. isValid ()) {
87 QVariant data
= theIndex
. model ()-> data ( theIndex
, Qt :: DisplayRole
);
88 if ( data
. toString (). size ())
90 if (! item
. isHidden () && data
. toString (). at ( 0 ). isLetter ())
91 retString
= data
. toString (). toUpper (). at ( 0 );
92 else if ( item
. isHidden () && data
. toString (). at ( 0 ) == '.' &&
93 data
. toString (). at ( 1 ). isLetter ())
94 retString
= data
. toString (). toUpper (). at ( 1 );
95 else if ( item
. isHidden () && data
. toString (). at ( 0 ) == '.' &&
96 ! data
. toString (). at ( 1 ). isLetter ())
97 retString
= i18nc ( "@title:group Name" , "Others" );
98 else if ( item
. isHidden () && data
. toString (). at ( 0 ) != '.' )
99 retString
= data
. toString (). toUpper (). at ( 0 );
100 else if ( item
. isHidden ())
101 retString
= data
. toString (). toUpper (). at ( 0 );
104 bool validCategory
= false ;
106 const QString
str ( data
. toString (). toUpper ());
107 const QChar
* currA
= str
. unicode ();
108 while (! currA
-> isNull () && ! validCategory
) {
109 if ( currA
-> isLetter ())
110 validCategory
= true ;
111 else if ( currA
-> isDigit ())
112 return i18nc ( "@title:group" , "Others" );
118 retString
= i18nc ( "@title:group Name" , "Others" );
126 case KDirModel :: Size
: {
127 const int fileSize
= ! item
. isNull () ? item
. size () : - 1 ;
128 if (! item
. isNull () && item
. isDir ()) {
129 retString
= i18nc ( "@title:group Size" , "Folders" );
130 } else if ( fileSize
< 5242880 ) {
131 retString
= i18nc ( "@title:group Size" , "Small" );
132 } else if ( fileSize
< 10485760 ) {
133 retString
= i18nc ( "@title:group Size" , "Medium" );
135 retString
= i18nc ( "@title:group Size" , "Big" );
140 case KDirModel :: ModifiedTime
:
142 KDateTime modifiedTime
;
143 modifiedTime
. setTime_t ( item
. time ( KIO :: UDSEntry :: UDS_MODIFICATION_TIME
));
144 modifiedTime
= modifiedTime
. toLocalZone ();
146 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 " ));
150 case KDirModel :: Permissions
:
156 QFileInfo
info ( item
. url (). pathOrUrl ());
158 if ( info
. permission ( QFile :: ReadUser
))
159 user
= i18n ( "Read, " );
161 if ( info
. permission ( QFile :: WriteUser
))
162 user
+= i18n ( "Write, " );
164 if ( info
. permission ( QFile :: ExeUser
))
165 user
+= i18n ( "Execute, " );
168 user
= i18n ( "Forbidden" );
170 user
= user
. mid ( 0 , user
. count () - 2 );
172 if ( info
. permission ( QFile :: ReadGroup
))
173 group
= i18n ( "Read, " );
175 if ( info
. permission ( QFile :: WriteGroup
))
176 group
+= i18n ( "Write, " );
178 if ( info
. permission ( QFile :: ExeGroup
))
179 group
+= i18n ( "Execute, " );
182 group
= i18n ( "Forbidden" );
184 group
= group
. mid ( 0 , group
. count () - 2 );
186 if ( info
. permission ( QFile :: ReadOther
))
187 others
= i18n ( "Read, " );
189 if ( info
. permission ( QFile :: WriteOther
))
190 others
+= i18n ( "Write, " );
192 if ( info
. permission ( QFile :: ExeOther
))
193 others
+= i18n ( "Execute, " );
195 if ( others
. isEmpty ())
196 others
= i18n ( "Forbidden" );
198 others
= others
. mid ( 0 , others
. count () - 2 );
200 retString
= i18nc ( "This shows files and folders permissions: user, group and others" , "(User: %1 ) (Group: %2 ) (Others: %3 )" , user
, group
, others
);
204 case KDirModel :: Owner
:
205 retString
= item
. user ();
208 case KDirModel :: Group
:
209 retString
= item
. group ();
212 case KDirModel :: Type
:
213 retString
= item
. mimeComment ();
217 case DolphinModel :: Rating
: {
218 const quint32 rating
= ratingForIndex ( index
);
220 retString
= QString :: number ( rating
);
224 case DolphinModel :: Tags
: {
225 retString
= tagsForIndex ( index
);
227 if ( retString
. isEmpty ())
228 retString
= i18nc ( "@title:group Tags" , "Not yet tagged" );
238 return KDirModel :: data ( index
, role
);
241 int DolphinModel :: columnCount ( const QModelIndex
& parent
) const
243 return KDirModel :: columnCount ( parent
) + ( ExtraColumnCount
- ColumnCount
);
246 quint32
DolphinModel :: ratingForIndex ( const QModelIndex
& index
)
251 const DolphinModel
* dolphinModel
= static_cast < const DolphinModel
*>( index
. model ());
252 KFileItem item
= dolphinModel
-> itemForIndex ( index
);
253 if (! item
. isNull ()) {
254 const Nepomuk :: Resource
resource ( item
. url (). url (), Nepomuk :: NFO :: File ());
255 rating
= resource
. rating ();
264 QString
DolphinModel :: tagsForIndex ( const QModelIndex
& index
)
269 const DolphinModel
* dolphinModel
= static_cast < const DolphinModel
*>( index
. model ());
270 KFileItem item
= dolphinModel
-> itemForIndex ( index
);
271 if (! item
. isNull ()) {
272 const Nepomuk :: Resource
resource ( item
. url (). url (), Nepomuk :: NFO :: File ());
273 const QList
< Nepomuk :: Tag
> tags
= resource
. tags ();
274 QStringList stringList
;
275 foreach ( const Nepomuk :: Tag
& tag
, tags
) {
276 stringList
. append ( tag
. label ());
280 foreach ( const QString
& str
, stringList
) {
285 if (! tagsString
. isEmpty ()) {
286 tagsString
. resize ( tagsString
. size () - 2 );