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

« back to all changes in this revision

Viewing changes to lib/historymodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Rohan Garg
  • Date: 2011-07-20 13:46:34 UTC
  • Revision ID: james.westby@ubuntu.com-20110720134634-92930fdjeed4gdc9
Tags: upstream-4.6.90+repack
ImportĀ upstreamĀ versionĀ 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
2
/*
 
3
Gwenview: an image viewer
 
4
Copyright 2009 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 "historymodel.moc"
 
23
 
 
24
// Qt
 
25
#include <QDateTime>
 
26
#include <QDir>
 
27
#include <QFile>
 
28
 
 
29
// KDE
 
30
#include <kconfig.h>
 
31
#include <kconfiggroup.h>
 
32
#include <kdebug.h>
 
33
#include <kdirmodel.h>
 
34
#include <kfileitem.h>
 
35
#include <kfileplacesmodel.h>
 
36
#include <kglobal.h>
 
37
#include <klocale.h>
 
38
#include <kmimetype.h>
 
39
#include <kstandarddirs.h>
 
40
#include <ktemporaryfile.h>
 
41
#include <kurl.h>
 
42
 
 
43
// Local
 
44
 
 
45
namespace Gwenview {
 
46
 
 
47
struct HistoryItem : public QStandardItem {
 
48
        void save() const {
 
49
                KConfig config(mConfigPath, KConfig::SimpleConfig);
 
50
                KConfigGroup group(&config, "general");
 
51
                group.writeEntry("url", mUrl);
 
52
                group.writeEntry("dateTime", mDateTime.toString(Qt::ISODate));
 
53
                config.sync();
 
54
        }
 
55
 
 
56
        static HistoryItem* create(const KUrl& url, const QDateTime& dateTime, const QString& storageDir) {
 
57
                if (!KStandardDirs::makeDir(storageDir, 0600)) {
 
58
                        kError() << "Could not create history dir" << storageDir;
 
59
                        return 0;
 
60
                }
 
61
                KTemporaryFile file;
 
62
                file.setAutoRemove(false);
 
63
                file.setPrefix(storageDir);
 
64
                file.setSuffix("rc");
 
65
                if (!file.open()) {
 
66
                        kError() << "Could not create history file";
 
67
                        return 0;
 
68
                }
 
69
 
 
70
                HistoryItem* item = new HistoryItem(url, dateTime, file.fileName());
 
71
                item->save();
 
72
                return item;
 
73
        }
 
74
 
 
75
        static HistoryItem* load(const QString& fileName) {
 
76
                KConfig config(fileName, KConfig::SimpleConfig);
 
77
                KConfigGroup group(&config, "general");
 
78
 
 
79
                KUrl url(group.readEntry("url"));
 
80
                if (!url.isValid()) {
 
81
                        kError() << "Invalid url" << url;
 
82
                        return 0;
 
83
                }
 
84
                QDateTime dateTime = QDateTime::fromString(group.readEntry("dateTime"), Qt::ISODate);
 
85
                if (!dateTime.isValid()) {
 
86
                        kError() << "Invalid dateTime" << dateTime;
 
87
                        return 0;
 
88
                }
 
89
 
 
90
                return new HistoryItem(url, dateTime, fileName);
 
91
        }
 
92
 
 
93
        KUrl url() const {
 
94
                return mUrl;
 
95
        }
 
96
 
 
97
        QDateTime dateTime() const {
 
98
                return mDateTime;
 
99
        }
 
100
 
 
101
        void setDateTime(const QDateTime& dateTime) {
 
102
                if (mDateTime != dateTime) {
 
103
                        mDateTime = dateTime;
 
104
                        save();
 
105
                }
 
106
        }
 
107
 
 
108
        void unlink() {
 
109
                QFile::remove(mConfigPath);
 
110
        }
 
111
 
 
112
private:
 
113
        KUrl mUrl;
 
114
        QDateTime mDateTime;
 
115
        QString mConfigPath;
 
116
 
 
117
        HistoryItem(const KUrl& url, const QDateTime& dateTime, const QString& configPath)
 
118
        : mUrl(url)
 
119
        , mDateTime(dateTime)
 
120
        , mConfigPath(configPath)
 
121
        {
 
122
                mUrl.cleanPath();
 
123
                mUrl.adjustPath(KUrl::RemoveTrailingSlash);
 
124
                setText(mUrl.pathOrUrl());
 
125
 
 
126
                QString iconName = KMimeType::iconNameForUrl(mUrl);
 
127
                setIcon(KIcon(iconName));
 
128
 
 
129
                setData(QVariant(mUrl), KFilePlacesModel::UrlRole);
 
130
 
 
131
                KFileItem fileItem(KFileItem::Unknown, KFileItem::Unknown, mUrl);
 
132
                setData(QVariant(fileItem), KDirModel::FileItemRole);
 
133
 
 
134
                QString date = KGlobal::locale()->formatDateTime(mDateTime, KLocale::FancyLongDate);
 
135
                setData(QVariant(i18n("Last visited: %1", date)), Qt::ToolTipRole);
 
136
        }
 
137
 
 
138
        bool operator<(const QStandardItem& other) const {
 
139
                return mDateTime > static_cast<const HistoryItem*>(&other)->mDateTime;
 
140
        }
 
141
};
 
142
 
 
143
 
 
144
struct HistoryModelPrivate {
 
145
        HistoryModel* q;
 
146
        QString mStorageDir;
 
147
        int mMaxCount;
 
148
 
 
149
        QMap<KUrl, HistoryItem*> mHistoryItemForUrl;
 
150
 
 
151
        void load() {
 
152
                QDir dir(mStorageDir);
 
153
                if (!dir.exists()) {
 
154
                        return;
 
155
                }
 
156
                Q_FOREACH(const QString& name, dir.entryList(QStringList() << "*rc")) {
 
157
                        HistoryItem* item = HistoryItem::load(dir.filePath(name));
 
158
                        if (!item) {
 
159
                                continue;
 
160
                        }
 
161
                        HistoryItem* existingItem = mHistoryItemForUrl.value(item->url());
 
162
                        if (existingItem) {
 
163
                                // We already know this url(!) update existing item dateTime
 
164
                                // and get rid of duplicate
 
165
                                if (existingItem->dateTime() < item->dateTime()) {
 
166
                                        existingItem->setDateTime(item->dateTime());
 
167
                                }
 
168
                                item->unlink();
 
169
                                delete item;
 
170
                        } else {
 
171
                                mHistoryItemForUrl.insert(item->url(), item);
 
172
                                q->appendRow(item);
 
173
                        }
 
174
                }
 
175
                q->sort(0);
 
176
        }
 
177
 
 
178
        void garbageCollect() {
 
179
                while (q->rowCount() > mMaxCount) {
 
180
                        HistoryItem* item = static_cast<HistoryItem*>(q->takeRow(q->rowCount() - 1).at(0));
 
181
                        mHistoryItemForUrl.remove(item->url());
 
182
                        item->unlink();
 
183
                        delete item;
 
184
                }
 
185
        }
 
186
};
 
187
 
 
188
 
 
189
HistoryModel::HistoryModel(QObject* parent, const QString& storageDir, int maxCount)
 
190
: QStandardItemModel(parent)
 
191
, d(new HistoryModelPrivate) {
 
192
        d->q = this;
 
193
        d->mStorageDir = storageDir;
 
194
        d->mMaxCount = maxCount;
 
195
        d->load();
 
196
}
 
197
 
 
198
 
 
199
HistoryModel::~HistoryModel() {
 
200
        delete d;
 
201
}
 
202
 
 
203
 
 
204
void HistoryModel::addUrl(const KUrl& url, const QDateTime& _dateTime) {
 
205
        QDateTime dateTime = _dateTime.isValid() ? _dateTime : QDateTime::currentDateTime();
 
206
        HistoryItem* historyItem = d->mHistoryItemForUrl.value(url);
 
207
        if (historyItem) {
 
208
                historyItem->setDateTime(dateTime);
 
209
                sort(0);
 
210
        } else {
 
211
                historyItem = HistoryItem::create(url, dateTime, d->mStorageDir);
 
212
                if (!historyItem) {
 
213
                        kError() << "Could not save history for url" << url;
 
214
                        return;
 
215
                }
 
216
                appendRow(historyItem);
 
217
                sort(0);
 
218
                d->garbageCollect();
 
219
        }
 
220
}
 
221
 
 
222
 
 
223
bool HistoryModel::removeRows(int start, int count, const QModelIndex& parent) {
 
224
        Q_ASSERT(!parent.isValid());
 
225
        for (int row=start + count - 1; row >= start ; --row) {
 
226
                HistoryItem* historyItem = static_cast<HistoryItem*>(item(row, 0));
 
227
                Q_ASSERT(historyItem);
 
228
                d->mHistoryItemForUrl.remove(historyItem->url());
 
229
                historyItem->unlink();
 
230
        }
 
231
        return QStandardItemModel::removeRows(start, count, parent);
 
232
}
 
233
 
 
234
 
 
235
} // namespace