1
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
3
Gwenview: an image viewer
4
Copyright 2009 AurĆ©lien GĆ¢teau <agateau@kde.org>
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.
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.
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.
22
#include "historymodel.moc"
31
#include <kconfiggroup.h>
33
#include <kdirmodel.h>
34
#include <kfileitem.h>
35
#include <kfileplacesmodel.h>
38
#include <kmimetype.h>
39
#include <kstandarddirs.h>
40
#include <ktemporaryfile.h>
47
struct HistoryItem : public QStandardItem {
49
KConfig config(mConfigPath, KConfig::SimpleConfig);
50
KConfigGroup group(&config, "general");
51
group.writeEntry("url", mUrl);
52
group.writeEntry("dateTime", mDateTime.toString(Qt::ISODate));
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;
62
file.setAutoRemove(false);
63
file.setPrefix(storageDir);
66
kError() << "Could not create history file";
70
HistoryItem* item = new HistoryItem(url, dateTime, file.fileName());
75
static HistoryItem* load(const QString& fileName) {
76
KConfig config(fileName, KConfig::SimpleConfig);
77
KConfigGroup group(&config, "general");
79
KUrl url(group.readEntry("url"));
81
kError() << "Invalid url" << url;
84
QDateTime dateTime = QDateTime::fromString(group.readEntry("dateTime"), Qt::ISODate);
85
if (!dateTime.isValid()) {
86
kError() << "Invalid dateTime" << dateTime;
90
return new HistoryItem(url, dateTime, fileName);
97
QDateTime dateTime() const {
101
void setDateTime(const QDateTime& dateTime) {
102
if (mDateTime != dateTime) {
103
mDateTime = dateTime;
109
QFile::remove(mConfigPath);
117
HistoryItem(const KUrl& url, const QDateTime& dateTime, const QString& configPath)
119
, mDateTime(dateTime)
120
, mConfigPath(configPath)
123
mUrl.adjustPath(KUrl::RemoveTrailingSlash);
124
setText(mUrl.pathOrUrl());
126
QString iconName = KMimeType::iconNameForUrl(mUrl);
127
setIcon(KIcon(iconName));
129
setData(QVariant(mUrl), KFilePlacesModel::UrlRole);
131
KFileItem fileItem(KFileItem::Unknown, KFileItem::Unknown, mUrl);
132
setData(QVariant(fileItem), KDirModel::FileItemRole);
134
QString date = KGlobal::locale()->formatDateTime(mDateTime, KLocale::FancyLongDate);
135
setData(QVariant(i18n("Last visited: %1", date)), Qt::ToolTipRole);
138
bool operator<(const QStandardItem& other) const {
139
return mDateTime > static_cast<const HistoryItem*>(&other)->mDateTime;
144
struct HistoryModelPrivate {
149
QMap<KUrl, HistoryItem*> mHistoryItemForUrl;
152
QDir dir(mStorageDir);
156
Q_FOREACH(const QString& name, dir.entryList(QStringList() << "*rc")) {
157
HistoryItem* item = HistoryItem::load(dir.filePath(name));
161
HistoryItem* existingItem = mHistoryItemForUrl.value(item->url());
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());
171
mHistoryItemForUrl.insert(item->url(), item);
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());
189
HistoryModel::HistoryModel(QObject* parent, const QString& storageDir, int maxCount)
190
: QStandardItemModel(parent)
191
, d(new HistoryModelPrivate) {
193
d->mStorageDir = storageDir;
194
d->mMaxCount = maxCount;
199
HistoryModel::~HistoryModel() {
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);
208
historyItem->setDateTime(dateTime);
211
historyItem = HistoryItem::create(url, dateTime, d->mStorageDir);
213
kError() << "Could not save history for url" << url;
216
appendRow(historyItem);
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();
231
return QStandardItemModel::removeRows(start, count, parent);