dolphinitemcategorizer.cpp 6.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
  * This file is part of the KDE project
  * Copyright (C) 2007 Rafael Fernández López <ereslibre@gmail.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public License
  * along with this library; see the file COPYING.LIB.  If not, write to
  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  * Boston, MA 02110-1301, USA.
  */

#include "dolphinitemcategorizer.h"

#include "dolphinview.h"

25
26
27
28
29
30
#ifdef HAVE_NEPOMUK
#include <config-nepomuk.h>
#include <nepomuk/global.h>
#include <nepomuk/resource.h>
#endif

31
#include <kdatetime.h>
32
33
34
35
#include <kdirmodel.h>
#include <kfileitem.h>
#include <klocale.h>
#include <kurl.h>
36

Dirk Mueller's avatar
Dirk Mueller committed
37
#include <QtGui/QSortFilterProxyModel>
38
39

DolphinItemCategorizer::DolphinItemCategorizer() :
40
41
42
    KItemCategorizer()
{
}
43
44

DolphinItemCategorizer::~DolphinItemCategorizer()
45
46
{
}
47
48
49
50
51
52

QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
                                                int sortRole)
{
    QString retString;

53
54
55
56
57
58
    if (!index.isValid())
    {
        return retString;
    }

    const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
59
    KFileItem *item = dirModel->itemForIndex(index);
60

61
62
    switch (sortRole)
    {
63
        case DolphinView::SortByName:
64
65
66
67
68
69
70
71
72
73
74
75
        {
            // KDirModel checks columns to know to which role are
            // we talking about
            QModelIndex theIndex = index.model()->index(index.row(),
                                                        KDirModel::Name,
                                                        index.parent());

            if (!theIndex.isValid()) {
                return retString;
            }

            QVariant data = theIndex.model()->data(theIndex, Qt::DisplayRole);
76
77
78
79
80
81
            if (data.toString().size())
            {
                if (!item->isHidden() && data.toString().at(0).isLetter())
                    retString = data.toString().toUpper().at(0);
                else if (item->isHidden() && data.toString().at(0) == '.' &&
                         data.toString().at(1).isLetter())
82
                    retString = data.toString().toUpper().at(1);
83
84
                else if (item->isHidden() && data.toString().at(0) == '.' &&
                         !data.toString().at(1).isLetter())
85
                    retString = i18n("Others");
86
                else if (item->isHidden() && data.toString().at(0) != '.')
87
                    retString = data.toString().toUpper().at(0);
88
89
90
                else if (item->isHidden())
                    retString = data.toString().toUpper().at(0);
                else
91
92
93
                {
                    bool validCategory = false;

94
95
                    const QString str(data.toString().toUpper());
                    const QChar* currA = str.unicode();
96
97
98
99
100
101
102
103
104
105
106
107
108
109
                    while (!currA->isNull() && !validCategory) {
                        if (currA->isLetter())
                            validCategory = true;
                        else if (currA->isDigit())
                            return i18n("Others");
                        else
                            ++currA;
                    }

                    if (!validCategory)
                        retString = i18n("Others");
                    else
                        retString = *currA;
                }
110
            }
111
            break;
112
        }
113

114
        case DolphinView::SortByDate:
115
116
        {
            KDateTime modifiedTime;
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
            modifiedTime.setTime_t(item->time(KIO::UDS_MODIFICATION_TIME));
            modifiedTime = modifiedTime.toLocalZone();

            if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 0)
                retString = i18n("Today");
            else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 1)
                retString = i18n("Yesterday");
            else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 7)
                retString = i18n("Less than a week");
            else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 31)
                retString = i18n("Less than a month");
            else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 365)
                retString = i18n("Less than a year");
            else
                retString = i18n("More than a year");
            break;
133
        }
134
135

        case DolphinView::SortByPermissions:
136
            retString = item->permissionsString();
137
138
139
            break;

        case DolphinView::SortByOwner:
140
            retString = item->user();
141
142
143
            break;

        case DolphinView::SortByGroup:
144
            retString = item->group();
145
146
            break;

147
148
        case DolphinView::SortBySize: {
            const int fileSize = item ? item->size() : -1;
149
150
151
152
153
154
155
156
157
158
            if (item && item->isDir()) {
                retString = i18n("Folders");
            } else if (fileSize < 5242880) {
                retString = i18nc("Size", "Small");
            } else if (fileSize < 10485760) {
                retString = i18nc("Size", "Medium");
            } else {
                retString = i18nc("Size", "Big");
            }
            break;
159
        }
160

161
        case DolphinView::SortByType:
162
163
            retString = item->mimeComment();
            break;
164
165
166
167
168
169
170

#ifdef HAVE_NEPOMUK
        case DolphinView::SortByRating: {
            KFileItem* item = dirModel->itemForIndex(index);
            if (item != 0) {
                const Nepomuk::Resource resource(item->url().url(), Nepomuk::NFO::File());
                const quint32 rating = resource.rating();
171
172
173
174
                if (!rating)
                    retString = i18n("Not yet rated");
                else
                    retString = i18np("1 star", "%1 stars", rating);
175
176
177
178
179
180
            }
            break;
        }
        case DolphinView::SortByTags:
            break;
#endif
181
182
183
184
    }

    return retString;
}