]>
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
64 if ( role
== KCategorizedSortFilterProxyModel :: CategoryDisplayRole
) {
67 if (! index
. isValid ()) {
71 const KDirModel
* dirModel
= qobject_cast
< const KDirModel
*>( index
. model ());
72 KFileItem item
= dirModel
-> itemForIndex ( index
);
74 switch ( index
. column ()) {
75 case KDirModel :: Name
: {
76 // KDirModel checks columns to know to which role are
78 QModelIndex theIndex
= index
. model ()-> index ( index
. row (),
82 if (! theIndex
. isValid ()) {
85 QVariant data
= theIndex
. model ()-> data ( theIndex
, Qt :: DisplayRole
);
86 QString name
= data
. toString ();
87 if (! name
. isEmpty ()) {
88 if (! item
. isHidden () && name
. at ( 0 ). isLetter ())
89 retString
= name
. at ( 0 ). toUpper ();
90 else if ( item
. isHidden ()) {
91 if ( name
. at ( 0 ) == '.' ) {
92 if ( name
. size () > 1 && name
. at ( 1 ). isLetter ()) {
93 retString
= name
. at ( 1 ). toUpper ();
95 retString
= i18nc ( "@title:group Name" , others
);
98 retString
= name
. at ( 0 ). toUpper ();
101 bool validCategory
= false ;
103 const QString
str ( name
. toUpper ());
104 const QChar
* currA
= str
. unicode ();
105 while (! currA
-> isNull () && ! validCategory
) {
106 if ( currA
-> isLetter ()) {
107 validCategory
= true ;
108 } else if ( currA
-> isDigit ()) {
109 return i18nc ( "@title:group Name" , others
);
115 if (! validCategory
) {
116 retString
= validCategory
? * currA
: i18nc ( "@title:group Name" , others
);
125 case KDirModel :: Size
: {
126 const int fileSize
= ! item
. isNull () ? item
. size () : - 1 ;
127 if (! item
. isNull () && item
. isDir ()) {
128 retString
= i18nc ( "@title:group Size" , "Folders" );
129 } else if ( fileSize
< 5242880 ) {
130 retString
= i18nc ( "@title:group Size" , "Small" );
131 } else if ( fileSize
< 10485760 ) {
132 retString
= i18nc ( "@title:group Size" , "Medium" );
134 retString
= i18nc ( "@title:group Size" , "Big" );
139 case KDirModel :: ModifiedTime
: {
140 KDateTime modifiedTime
= item
. time ( KFileItem :: ModificationTime
);
141 modifiedTime
= modifiedTime
. toLocalZone ();
143 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 " ));
147 case KDirModel :: Permissions
: {
152 QFileInfo
info ( item
. url (). pathOrUrl ());
154 if ( info
. permission ( QFile :: ReadUser
))
155 user
= i18n ( "Read, " );
157 if ( info
. permission ( QFile :: WriteUser
))
158 user
+= i18n ( "Write, " );
160 if ( info
. permission ( QFile :: ExeUser
))
161 user
+= i18n ( "Execute, " );
164 user
= i18n ( "Forbidden" );
166 user
= user
. mid ( 0 , user
. count () - 2 );
168 if ( info
. permission ( QFile :: ReadGroup
))
169 group
= i18n ( "Read, " );
171 if ( info
. permission ( QFile :: WriteGroup
))
172 group
+= i18n ( "Write, " );
174 if ( info
. permission ( QFile :: ExeGroup
))
175 group
+= i18n ( "Execute, " );
178 group
= i18n ( "Forbidden" );
180 group
= group
. mid ( 0 , group
. count () - 2 );
182 if ( info
. permission ( QFile :: ReadOther
))
183 others
= i18n ( "Read, " );
185 if ( info
. permission ( QFile :: WriteOther
))
186 others
+= i18n ( "Write, " );
188 if ( info
. permission ( QFile :: ExeOther
))
189 others
+= i18n ( "Execute, " );
191 if ( others
. isEmpty ())
192 others
= i18n ( "Forbidden" );
194 others
= others
. mid ( 0 , others
. count () - 2 );
196 retString
= i18nc ( "This shows files and folders permissions: user, group and others" , "(User: %1 ) (Group: %2 ) (Others: %3 )" , user
, group
, others
);
200 case KDirModel :: Owner
:
201 retString
= item
. user ();
204 case KDirModel :: Group
:
205 retString
= item
. group ();
208 case KDirModel :: Type
:
209 retString
= item
. mimeComment ();
213 case DolphinModel :: Rating
: {
214 const quint32 rating
= ratingForIndex ( index
);
216 retString
= QString :: number ( rating
);
220 case DolphinModel :: Tags
: {
221 retString
= tagsForIndex ( index
);
223 if ( retString
. isEmpty ()) {
224 retString
= i18nc ( "@title:group Tags" , "Not yet tagged" );
233 else if ( role
== KCategorizedSortFilterProxyModel :: CategorySortRole
) {
236 if (! index
. isValid ()) {
240 const KDirModel
* dirModel
= qobject_cast
< const KDirModel
*>( index
. model ());
241 KFileItem item
= dirModel
-> itemForIndex ( index
);
243 switch ( index
. column ()) {
244 case KDirModel :: Name
: {
245 retVariant
= data ( index
, KCategorizedSortFilterProxyModel :: CategoryDisplayRole
);
247 if ( retVariant
== i18nc ( "@title:group Name" , others
))
248 retVariant
= QString ( QChar ( QChar :: ReplacementCharacter
));
253 case KDirModel :: Size
: {
254 const int fileSize
= ! item
. isNull () ? item
. size () : - 1 ;
257 } else if ( fileSize
< 5242880 ) {
259 } else if ( fileSize
< 10485760 ) {
267 case KDirModel :: ModifiedTime
: {
268 KDateTime modifiedTime
= item
. time ( KFileItem :: ModificationTime
);
269 modifiedTime
= modifiedTime
. toLocalZone ();
271 retVariant
= -( modifiedTime
. date (). year () * 100 + modifiedTime
. date (). month ());
275 case KDirModel :: Permissions
: {
276 QFileInfo
info ( item
. url (). pathOrUrl ());
278 retVariant
= - KDirSortFilterProxyModel :: pointsForPermissions ( info
);
282 case KDirModel :: Owner
:
283 retVariant
= item
. user ();
286 case KDirModel :: Group
:
287 retVariant
= item
. group ();
290 case KDirModel :: Type
:
292 retVariant
= QString (); // when sorting we want folders to be placed first
294 retVariant
= item
. mimeComment ();
298 case DolphinModel :: Rating
: {
299 retVariant
= ratingForIndex ( index
);
303 case DolphinModel :: Tags
: {
304 retVariant
= tagsForIndex ( index
). count ();
317 return KDirModel :: data ( index
, role
);
320 int DolphinModel :: columnCount ( const QModelIndex
& parent
) const
322 return KDirModel :: columnCount ( parent
) + ( ExtraColumnCount
- ColumnCount
);
325 quint32
DolphinModel :: ratingForIndex ( const QModelIndex
& index
)
330 const DolphinModel
* dolphinModel
= static_cast < const DolphinModel
*>( index
. model ());
331 KFileItem item
= dolphinModel
-> itemForIndex ( index
);
332 if (! item
. isNull ()) {
333 const Nepomuk :: Resource
resource ( item
. url (). url (), Soprano :: Vocabulary :: Xesam :: File ());
334 rating
= resource
. rating ();
343 QString
DolphinModel :: tagsForIndex ( const QModelIndex
& index
)
348 const DolphinModel
* dolphinModel
= static_cast < const DolphinModel
*>( index
. model ());
349 KFileItem item
= dolphinModel
-> itemForIndex ( index
);
350 if (! item
. isNull ()) {
351 const Nepomuk :: Resource
resource ( item
. url (). url (), Soprano :: Vocabulary :: Xesam :: File ());
352 const QList
< Nepomuk :: Tag
> tags
= resource
. tags ();
353 QStringList stringList
;
354 foreach ( const Nepomuk :: Tag
& tag
, tags
) {
355 stringList
. append ( tag
. label ());
359 foreach ( const QString
& str
, stringList
) {
364 if (! tagsString
. isEmpty ()) {
365 tagsString
. resize ( tagsString
. size () - 2 );