Warning, file /office/calligra/gemini/RecentFileManager.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2012 Boudewijn Rempt <boud@kogmbh.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #include "RecentFileManager.h"
0007 
0008 #include <QFile>
0009 #include <QFileInfo>
0010 #include <QDir>
0011 #include <QUrl>
0012 #include <QDebug>
0013 
0014 #include <KSharedConfig>
0015 #include <kconfiggroup.h>
0016 #include <kconfig.h>
0017 
0018 
0019 // Much of this is a gui-less clone of KRecentFilesAction, so the format of
0020 // storing recent files is compatible.
0021 struct RecentFileEntry {
0022     QString fileName;
0023     QString filePath;
0024 };
0025 
0026 class RecentFileManager::Private {
0027 public:
0028     Private()
0029     {
0030         KConfigGroup grp(KSharedConfig::openConfig(), "RecentFiles");
0031         maxItems = grp.readEntry("maxRecentFileItems", 100);
0032 
0033         loadEntries(grp);
0034     }
0035 
0036     void loadEntries(const KConfigGroup &grp)
0037     {
0038         recents.clear();
0039 
0040         QString value;
0041         QString nameValue;
0042         QUrl url;
0043 
0044         KConfigGroup cg = grp;
0045 
0046         if ( cg.name().isEmpty()) {
0047             cg = KConfigGroup(cg.config(),"RecentFiles");
0048         }
0049 
0050         // read file list
0051         for (int i = 1; i <= maxItems; i++) {
0052 
0053             value = cg.readPathEntry(QString("File%1").arg(i), QString());
0054             if (value.isEmpty()) continue;
0055             url = QUrl(value);
0056 
0057             // gemini only handles local files
0058             // yes, i know the second half here isn't good on windows... but without it we fail on linux, and second part for windows
0059             if (!url.isLocalFile() && !value.startsWith('/') && value.midRef(2, 1) != QLatin1String(":")) {
0060                 qDebug() << "Not a local file:" << url;
0061                 continue;
0062             }
0063 
0064             // Don't restore if file doesn't exist anymore
0065             if (!QFile::exists(url.toLocalFile()) && !QFile::exists(value)) {
0066                 qDebug() << "Recent file apparently no longer exists:" << url.toLocalFile();
0067                 continue;
0068             }
0069 
0070             value = QDir::toNativeSeparators( value );
0071 
0072             // Don't restore where the url is already known (eg. broken config)
0073             for (const RecentFileEntry& entry : recents) {
0074                 if (entry.filePath == value) {
0075                     continue;
0076                 }
0077             }
0078 
0079             nameValue = cg.readPathEntry(QString("Name%1").arg(i), url.fileName());
0080 
0081             if (!value.isNull())  {
0082                 RecentFileEntry entry;
0083                 entry.fileName = nameValue;
0084                 entry.filePath = value;
0085                 recents << entry;
0086            }
0087         }
0088     }
0089 
0090     void saveEntries( const KConfigGroup &grp)
0091     {
0092         KConfigGroup cg = grp;
0093 
0094         if (cg.name().isEmpty()) {
0095             cg = KConfigGroup(cg.config(),"RecentFiles");
0096         }
0097         cg.deleteGroup();
0098 
0099         // write file list
0100         for (int i = 1; i <= recents.size(); ++i) {
0101             // i - 1 because we started from 1
0102             const RecentFileEntry &item = recents[i-1];
0103             cg.writePathEntry(QString("File%1").arg(i), item.filePath);
0104             cg.writePathEntry(QString("Name%1").arg(i), item.fileName);
0105         }
0106     }
0107 
0108     int maxItems;
0109     QList<RecentFileEntry> recents;
0110 };
0111 
0112 
0113 
0114 
0115 RecentFileManager::RecentFileManager(QObject *parent)
0116     : QObject(parent)
0117     , d(new Private())
0118 {
0119 }
0120 
0121 RecentFileManager::~RecentFileManager()
0122 {
0123     KConfigGroup grp(KSharedConfig::openConfig(), "RecentFiles");
0124     grp.writeEntry("maxRecentFileItems", d->maxItems);
0125     delete d;
0126 }
0127 
0128 
0129 QStringList RecentFileManager::recentFileNames() const
0130 {
0131     QStringList files;
0132     for(const RecentFileEntry &item : d->recents) {
0133         files << item.fileName;
0134     }
0135     return files;
0136 }
0137 
0138 QStringList RecentFileManager::recentFiles() const
0139 {
0140     QStringList files;
0141     for(const RecentFileEntry &item : d->recents) {
0142         files << item.filePath;
0143     }
0144     return files;
0145 }
0146 
0147 void RecentFileManager::addRecent(const QString &_url)
0148 {
0149     if (d->recents.size() > d->maxItems) {
0150         d->recents.removeLast();
0151     }
0152 
0153     RecentFileEntry newEntry;
0154     newEntry.filePath = QDir::toNativeSeparators(_url);
0155     newEntry.fileName  = QFileInfo(_url).fileName();
0156 
0157     QMutableListIterator<RecentFileEntry> i(d->recents);
0158     while (i.hasNext()) {
0159         i.next();
0160         if (i.value().filePath == newEntry.filePath) {
0161             i.remove();
0162         }
0163     }
0164     d->recents.insert(0, newEntry);
0165 
0166     d->saveEntries(KConfigGroup(KSharedConfig::openConfig(), "RecentFiles"));
0167     emit recentFilesListChanged();
0168 }
0169 
0170 int RecentFileManager::size()
0171 {
0172     return d->recents.size();
0173 }
0174 
0175 QString RecentFileManager::recentFile(int index) const
0176 {
0177     if (index < d->recents.size()) {
0178         return d->recents.at(index).filePath;
0179     }
0180     return QString();
0181 }
0182 
0183 QString RecentFileManager::recentFileName(int index) const
0184 {
0185     if (index < d->recents.size()) {
0186         return d->recents.at(index).fileName;
0187     }
0188     return QString();
0189 }