~ubuntu-branches/ubuntu/trusty/gwenview/trusty

« back to all changes in this revision

Viewing changes to lib/recursivedirmodel.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2012-11-19 15:42:29 UTC
  • mto: This revision was merged to the branch mainline in revision 27.
  • Revision ID: package-import@ubuntu.com-20121119154229-selaegu8d5rzxtln
Tags: upstream-4.9.80
Import upstream version 4.9.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
 
2
/*
 
3
Gwenview: an image viewer
 
4
Copyright 2012 Aurélien Gâteau <agateau@kde.org>
 
5
 
 
6
This program is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU General Public License
 
8
as published by the Free Software Foundation; either version 2
 
9
of the License, or (at your option) any later version.
 
10
 
 
11
This program is distributed in the hope that it will be useful,
 
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
GNU General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License
 
17
along with this program; if not, write to the Free Software
 
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
 
19
 
 
20
*/
 
21
// Self
 
22
#include "recursivedirmodel.moc"
 
23
 
 
24
// Local
 
25
 
 
26
// KDE
 
27
#include <KDebug>
 
28
#include <KDirLister>
 
29
#include <KDirModel>
 
30
 
 
31
// Qt
 
32
 
 
33
namespace Gwenview
 
34
{
 
35
 
 
36
struct RecursiveDirModelPrivate {
 
37
    KDirLister* mDirLister;
 
38
    KFileItemList mList;
 
39
 
 
40
    int indexForUrl(const KUrl &url) const
 
41
    {
 
42
        int row = 0;
 
43
        KFileItemList::ConstIterator it = mList.begin(), end = mList.end();
 
44
        for (; it != end; ++it , ++row) {
 
45
            if (it->url() == url) {
 
46
                return row;
 
47
            }
 
48
        }
 
49
        return -1;
 
50
    }
 
51
 
 
52
    void removeAt(int row)
 
53
    {
 
54
        mList.removeAt(row);
 
55
    }
 
56
 
 
57
    void addItem(const KFileItem& item)
 
58
    {
 
59
        mList.append(item);
 
60
    }
 
61
};
 
62
 
 
63
RecursiveDirModel::RecursiveDirModel(QObject* parent)
 
64
: QAbstractListModel(parent)
 
65
, d(new RecursiveDirModelPrivate)
 
66
{
 
67
    d->mDirLister = new KDirLister(this);
 
68
    connect(d->mDirLister, SIGNAL(itemsAdded(KUrl, KFileItemList)),
 
69
        SLOT(slotItemsAdded(KUrl, KFileItemList)));
 
70
    connect(d->mDirLister, SIGNAL(itemsDeleted(KFileItemList)),
 
71
        SLOT(slotItemsDeleted(KFileItemList)));
 
72
    connect(d->mDirLister, SIGNAL(completed()),
 
73
        SIGNAL(completed()));
 
74
    connect(d->mDirLister, SIGNAL(clear()),
 
75
        SLOT(slotCleared()));
 
76
    connect(d->mDirLister, SIGNAL(clear(KUrl)),
 
77
        SLOT(slotDirCleared(KUrl)));
 
78
}
 
79
 
 
80
RecursiveDirModel::~RecursiveDirModel()
 
81
{
 
82
    delete d;
 
83
}
 
84
 
 
85
KUrl RecursiveDirModel::url() const
 
86
{
 
87
    return d->mDirLister->url();
 
88
}
 
89
 
 
90
void RecursiveDirModel::setUrl(const KUrl& url)
 
91
{
 
92
    beginResetModel();
 
93
    d->mList.clear();
 
94
    endResetModel();
 
95
    d->mDirLister->openUrl(url);
 
96
}
 
97
 
 
98
int RecursiveDirModel::rowCount(const QModelIndex& parent) const
 
99
{
 
100
    if (parent.isValid()) {
 
101
        return 0;
 
102
    } else {
 
103
        return d->mList.count();
 
104
    }
 
105
}
 
106
 
 
107
QVariant RecursiveDirModel::data(const QModelIndex& index, int role) const
 
108
{
 
109
    if (index.parent().isValid()) {
 
110
        return QVariant();
 
111
    }
 
112
    KFileItem item = d->mList.value(index.row());
 
113
    if (item.isNull()) {
 
114
        kWarning() << "Invalid row" << index.row();
 
115
        return QVariant();
 
116
    }
 
117
    switch (role) {
 
118
    case Qt::DisplayRole:
 
119
        return item.text();
 
120
    case Qt::DecorationRole:
 
121
        return item.iconName();
 
122
    case KDirModel::FileItemRole:
 
123
        return QVariant(item);
 
124
    default:
 
125
        kWarning() << "Unhandled role" << role;
 
126
        break;
 
127
    }
 
128
    return QVariant();
 
129
}
 
130
 
 
131
void RecursiveDirModel::slotItemsAdded(const KUrl&, const KFileItemList& newList)
 
132
{
 
133
    QList<KUrl> dirUrls;
 
134
    Q_FOREACH(const KFileItem& item, newList) {
 
135
        if (item.isFile()) {
 
136
            if (d->indexForUrl(item.url()) == -1) {
 
137
                beginInsertRows(QModelIndex(), d->mList.count(), d->mList.count());
 
138
                d->addItem(item);
 
139
                endInsertRows();
 
140
            }
 
141
        } else {
 
142
            dirUrls << item.url();
 
143
        }
 
144
    }
 
145
    Q_FOREACH(const KUrl& url, dirUrls) {
 
146
        d->mDirLister->openUrl(url, KDirLister::Keep);
 
147
    }
 
148
}
 
149
 
 
150
void RecursiveDirModel::slotItemsDeleted(const KFileItemList& list)
 
151
{
 
152
    Q_FOREACH(const KFileItem& item, list) {
 
153
        if (item.isDir()) {
 
154
            continue;
 
155
        }
 
156
        int row = d->indexForUrl(item.url());
 
157
        if (row == -1) {
 
158
            kWarning() << "Received itemsDeleted for an unknown item: this should not happen!";
 
159
            continue;
 
160
        }
 
161
        beginRemoveRows(QModelIndex(), row, row);
 
162
        d->removeAt(row);
 
163
        endRemoveRows();
 
164
    }
 
165
}
 
166
 
 
167
void RecursiveDirModel::slotCleared()
 
168
{
 
169
    if (d->mList.isEmpty()) {
 
170
        return;
 
171
    }
 
172
    beginRemoveRows(QModelIndex(), 0, d->mList.count() - 1);
 
173
    d->mList.clear();
 
174
    endRemoveRows();
 
175
}
 
176
 
 
177
void RecursiveDirModel::slotDirCleared(const KUrl& dirUrl)
 
178
{
 
179
    int row;
 
180
    for (row = d->mList.count() - 1; row >= 0; --row) {
 
181
        const KUrl url = d->mList.at(row).url();
 
182
        if (dirUrl.isParentOf(url)) {
 
183
            beginRemoveRows(QModelIndex(), row, row);
 
184
            d->removeAt(row);
 
185
            endRemoveRows();
 
186
        }
 
187
    }
 
188
}
 
189
 
 
190
} // namespace