~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to lib/historymodel.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
ImportĀ upstreamĀ versionĀ 4.7.90

Show diffs side-by-side

added added

removed removed

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