File indexing completed on 2024-04-28 04:18:52

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2009 Aurélien Gâteau <agateau@kde.org>
0005 Copyright 2014 Tomas Mecir <mecirt@gmail.com>
0006 
0007 This program is free software; you can redistribute it and/or
0008 modify it under the terms of the GNU General Public License
0009 as published by the Free Software Foundation; either version 2
0010 of the License, or (at your option) any later version.
0011 
0012 This program is distributed in the hope that it will be useful,
0013 but WITHOUT ANY WARRANTY; without even the implied warranty of
0014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015 GNU General Public License for more details.
0016 
0017 You should have received a copy of the GNU General Public License
0018 along with this program; if not, write to the Free Software
0019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
0020 
0021 */
0022 // Self
0023 #include "recentfilesmodel.h"
0024 
0025 // Qt
0026 #include <QDir>
0027 #include <QMimeDatabase>
0028 #include <QRegularExpression>
0029 #include <QUrl>
0030 
0031 // KF
0032 #include <KDirModel>
0033 #include <KFileItem>
0034 #include <KFilePlacesModel>
0035 
0036 // Local
0037 #include "gwenview_lib_debug.h"
0038 #include <lib/urlutils.h>
0039 
0040 namespace Gwenview
0041 {
0042 struct RecentFilesItem : public QStandardItem {
0043     QUrl url() const
0044     {
0045         return mUrl;
0046     }
0047 
0048     RecentFilesItem(const QUrl &url)
0049         : mUrl(url)
0050     {
0051         QString text(mUrl.toDisplayString(QUrl::PreferLocalFile));
0052 #ifdef Q_OS_UNIX
0053         text.replace(QRegularExpression(QStringLiteral("^") + QDir::homePath()), QStringLiteral("~"));
0054 #endif
0055         setText(text);
0056 
0057         QMimeDatabase db;
0058         const QString iconName = db.mimeTypeForUrl(mUrl).iconName();
0059         setIcon(QIcon::fromTheme(iconName));
0060 
0061         setData(mUrl, KFilePlacesModel::UrlRole);
0062 
0063         const KFileItem fileItem(mUrl);
0064         setData(QVariant(fileItem), KDirModel::FileItemRole);
0065     }
0066 
0067 private:
0068     QUrl mUrl;
0069 };
0070 
0071 struct RecentFilesModelPrivate {
0072     RecentFilesModel *q = nullptr;
0073 
0074     QMap<QUrl, RecentFilesItem *> mRecentFilesItemForUrl;
0075 };
0076 
0077 RecentFilesModel::RecentFilesModel(QObject *parent)
0078     : QStandardItemModel(parent)
0079     , d(new RecentFilesModelPrivate)
0080 {
0081     d->q = this;
0082 }
0083 
0084 RecentFilesModel::~RecentFilesModel()
0085 {
0086     delete d;
0087 }
0088 
0089 void RecentFilesModel::addUrl(const QUrl &url)
0090 {
0091     RecentFilesItem *historyItem = d->mRecentFilesItemForUrl.value(url);
0092     if (!historyItem) {
0093         historyItem = new RecentFilesItem(url);
0094         if (!historyItem)
0095             return;
0096         d->mRecentFilesItemForUrl.insert(url, historyItem);
0097         appendRow(historyItem);
0098     }
0099     sort(0);
0100 }
0101 
0102 bool RecentFilesModel::removeRows(int start, int count, const QModelIndex &parent)
0103 {
0104     Q_ASSERT(!parent.isValid());
0105     for (int row = start + count - 1; row >= start; --row) {
0106         auto historyItem = static_cast<RecentFilesItem *>(item(row, 0));
0107         Q_ASSERT(historyItem);
0108         d->mRecentFilesItemForUrl.remove(historyItem->url());
0109     }
0110     return QStandardItemModel::removeRows(start, count, parent);
0111 }
0112 
0113 } // namespace
0114 
0115 #include "moc_recentfilesmodel.cpp"