]>
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 ( name
. 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 ();
96 bool validCategory
= false ;
98 const QString
str ( name
. toUpper ());
99 const QChar
* currA
= str
. unicode ();
100 while (! currA
-> isNull () && ! validCategory
) {
101 if ( currA
-> isLetter ())
102 validCategory
= true ;
103 else if ( currA
-> isDigit ())
104 return i18nc ( "@title:group" , "Others" );
110 retString
= i18nc ( "@title:group Name" , "Others" );
118 case KDirModel :: Size
: {
119 const int fileSize
= ! item
. isNull () ? item
. size () : - 1 ;
120 if (! item
. isNull () && item
. isDir ()) {
121 retString
= i18nc ( "@title:group Size" , "Folders" );
122 } else if ( fileSize
< 5242880 ) {
123 retString
= i18nc ( "@title:group Size" , "Small" );
124 } else if ( fileSize
< 10485760 ) {
125 retString
= i18nc ( "@title:group Size" , "Medium" );
127 retString
= i18nc ( "@title:group Size" , "Big" );
132 case KDirModel :: ModifiedTime
: {
133 KDateTime modifiedTime
;
134 modifiedTime
. setTime_t ( item
. time ( KIO :: UDSEntry :: UDS_MODIFICATION_TIME
));
135 modifiedTime
= modifiedTime
. toLocalZone ();
137 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 " ));
141 case KDirModel :: Permissions
: {
146 QFileInfo
info ( item
. url (). pathOrUrl ());
148 if ( info
. permission ( QFile :: ReadUser
))
149 user
= i18n ( "Read, " );
151 if ( info
. permission ( QFile :: WriteUser
))
152 user
+= i18n ( "Write, " );
154 if ( info
. permission ( QFile :: ExeUser
))
155 user
+= i18n ( "Execute, " );
158 user
= i18n ( "Forbidden" );
160 user
= user
. mid ( 0 , user
. count () - 2 );
162 if ( info
. permission ( QFile :: ReadGroup
))
163 group
= i18n ( "Read, " );
165 if ( info
. permission ( QFile :: WriteGroup
))
166 group
+= i18n ( "Write, " );
168 if ( info
. permission ( QFile :: ExeGroup
))
169 group
+= i18n ( "Execute, " );
172 group
= i18n ( "Forbidden" );
174 group
= group
. mid ( 0 , group
. count () - 2 );
176 if ( info
. permission ( QFile :: ReadOther
))
177 others
= i18n ( "Read, " );
179 if ( info
. permission ( QFile :: WriteOther
))
180 others
+= i18n ( "Write, " );
182 if ( info
. permission ( QFile :: ExeOther
))
183 others
+= i18n ( "Execute, " );
185 if ( others
. isEmpty ())
186 others
= i18n ( "Forbidden" );
188 others
= others
. mid ( 0 , others
. count () - 2 );
190 retString
= i18nc ( "This shows files and folders permissions: user, group and others" , "(User: %1 ) (Group: %2 ) (Others: %3 )" , user
, group
, others
);
194 case KDirModel :: Owner
:
195 retString
= item
. user ();
198 case KDirModel :: Group
:
199 retString
= item
. group ();
202 case KDirModel :: Type
:
203 retString
= item
. mimeComment ();
207 case DolphinModel :: Rating
: {
208 const quint32 rating
= ratingForIndex ( index
);
210 retString
= QString :: number ( rating
);
214 case DolphinModel :: Tags
: {
215 retString
= tagsForIndex ( index
);
217 if ( retString
. isEmpty ())
218 retString
= i18nc ( "@title:group Tags" , "Not yet tagged" );
227 else if ( role
== KCategorizedSortFilterProxyModel :: CategorySortRole
) {
230 if (! index
. isValid ()) {
234 const KDirModel
* dirModel
= qobject_cast
< const KDirModel
*>( index
. model ());
235 KFileItem item
= dirModel
-> itemForIndex ( index
);
237 switch ( index
. column ()) {
238 case KDirModel :: Name
: {
239 retVariant
= data ( index
, KCategorizedSortFilterProxyModel :: CategoryDisplayRole
);
243 case KDirModel :: Size
: {
244 const int fileSize
= ! item
. isNull () ? item
. size () : - 1 ;
247 } else if ( fileSize
< 5242880 ) {
249 } else if ( fileSize
< 10485760 ) {
257 case KDirModel :: ModifiedTime
: {
258 KDateTime modifiedTime
;
259 modifiedTime
. setTime_t ( item
. time ( KIO :: UDSEntry :: UDS_MODIFICATION_TIME
));
260 modifiedTime
= modifiedTime
. toLocalZone ();
262 retVariant
= -( modifiedTime
. date (). year () * 100 + modifiedTime
. date (). month ());
266 case KDirModel :: Permissions
: {
267 QFileInfo
info ( item
. url (). pathOrUrl ());
269 retVariant
= - KDirSortFilterProxyModel :: pointsForPermissions ( info
);
273 case KDirModel :: Owner
:
274 retVariant
= item
. user ();
277 case KDirModel :: Group
:
278 retVariant
= item
. group ();
281 case KDirModel :: Type
:
283 retVariant
= QString (); // when sorting we want folders to be placed first
285 retVariant
= item
. mimeComment ();
289 case DolphinModel :: Rating
: {
290 retVariant
= ratingForIndex ( index
);
294 case DolphinModel :: Tags
: {
295 retVariant
= tagsForIndex ( index
). count ();
308 return KDirModel :: data ( index
, role
);
311 int DolphinModel :: columnCount ( const QModelIndex
& parent
) const
313 return KDirModel :: columnCount ( parent
) + ( ExtraColumnCount
- ColumnCount
);
316 quint32
DolphinModel :: ratingForIndex ( const QModelIndex
& index
)
321 const DolphinModel
* dolphinModel
= static_cast < const DolphinModel
*>( index
. model ());
322 KFileItem item
= dolphinModel
-> itemForIndex ( index
);
323 if (! item
. isNull ()) {
324 const Nepomuk :: Resource
resource ( item
. url (). url (), Nepomuk :: NFO :: File ());
325 rating
= resource
. rating ();
334 QString
DolphinModel :: tagsForIndex ( const QModelIndex
& index
)
339 const DolphinModel
* dolphinModel
= static_cast < const DolphinModel
*>( index
. model ());
340 KFileItem item
= dolphinModel
-> itemForIndex ( index
);
341 if (! item
. isNull ()) {
342 const Nepomuk :: Resource
resource ( item
. url (). url (), Nepomuk :: NFO :: File ());
343 const QList
< Nepomuk :: Tag
> tags
= resource
. tags ();
344 QStringList stringList
;
345 foreach ( const Nepomuk :: Tag
& tag
, tags
) {
346 stringList
. append ( tag
. label ());
350 foreach ( const QString
& str
, stringList
) {
355 if (! tagsString
. isEmpty ()) {
356 tagsString
. resize ( tagsString
. size () - 2 );