┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/dolphinitemcategorizer.cpp
blob: 96e91b7cb29c9264e0e0524e31fd4f5fde9bf7db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
  * This file is part of the KDE project
  * Copyright (C) 2007 Rafael Fernández López <[email protected]>
  *
  * 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"

#include <klocale.h>
#include <kdirmodel.h>

#include <QtGui/QSortFilterProxyModel>

DolphinItemCategorizer::DolphinItemCategorizer() :
    KItemCategorizer()
{
}

DolphinItemCategorizer::~DolphinItemCategorizer()
{
}

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

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

    int indexColumn;

    switch (sortRole)
    {
        case DolphinView::SortByName:
            indexColumn = KDirModel::Name;
            break;
        case DolphinView::SortBySize:
            indexColumn = KDirModel::Size;
            break;
        default:
        return retString;
    }

    // KDirModel checks columns to know to which role are
    // we talking about
    QModelIndex theIndex = index.model()->index(index.row(),
                                                indexColumn,
                           index.parent());

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

    QVariant data = theIndex.model()->data(theIndex, Qt::DisplayRole);

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

    switch (sortRole)
    {
    case DolphinView::SortByName:
            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())
                    retString = data.toString().toUpper().at(1);
                else if (item->isHidden() && data.toString().at(0) == '.' &&
                         !data.toString().at(1).isLetter())
                    retString = i18n("Others");
                else if (item->isHidden() && data.toString().at(0) != '.')
                    retString = data.toString().toUpper().at(0);
                else if (item->isHidden())
                    retString = data.toString().toUpper().at(0);
                else
                    retString = i18n("Others");
            }
        break;
    case DolphinView::SortBySize:
        int fileSize = (item) ? item->size() : -1;
        if (item && item->isDir()) {
                retString = i18n("Folders");
        } else if (fileSize < 5242880) {
            retString = i18n("Small");
        } else if (fileSize < 10485760) {
            retString = i18n("Medium");
        } else {
            retString = i18n("Big");
        }
        break;
    }

    return retString;
}