File indexing completed on 2024-12-22 04:40:00

0001 /*
0002     SPDX-FileCopyrightText: 2010-2022 Mladen Milinkovic <max@smoothware.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "krecentfilesactionext.h"
0008 
0009 #include <KConfigGroup>
0010 #include <QFile>
0011 
0012 static QMap<QUrl, QString> s_subtitleEncodings;
0013 
0014 KRecentFilesActionExt::KRecentFilesActionExt(QObject *parent)
0015     : KRecentFilesAction(parent)
0016 {
0017 }
0018 
0019 
0020 KRecentFilesActionExt::~KRecentFilesActionExt()
0021 {
0022 }
0023 
0024 QString
0025 KRecentFilesActionExt::encodingForUrl(const QUrl &url)
0026 {
0027     return s_subtitleEncodings.value(url);
0028 }
0029 
0030 void
0031 KRecentFilesActionExt::addUrl(const QUrl &url, const QString &encoding, const QString &name)
0032 {
0033     s_subtitleEncodings.insert(url, encoding);
0034     KRecentFilesAction::addUrl(url, name);
0035 }
0036 
0037 void
0038 KRecentFilesActionExt::loadEntries(const KConfigGroup &configGroup)
0039 {
0040     KRecentFilesAction::loadEntries(configGroup);
0041 
0042     for(int i = 1; ; i++) {
0043         const QUrl url = QUrl::fromUserInput(configGroup.readPathEntry(QStringLiteral("File%1").arg(i), QString()));
0044         if(url.isEmpty())
0045             break;
0046         const QString encoding = configGroup.readEntry(QStringLiteral("Encoding%1").arg(i), QString());
0047         if(!encoding.isEmpty())
0048             s_subtitleEncodings.insert(url, encoding);
0049     }
0050 }
0051 
0052 void
0053 KRecentFilesActionExt::saveEntries(const KConfigGroup &configGroup)
0054 {
0055     KConfigGroup config = configGroup;
0056     config.deleteGroup();
0057 
0058     KRecentFilesAction::saveEntries(config);
0059 
0060     for(int i = 1; ; i++) {
0061         const QUrl url = QUrl::fromUserInput(configGroup.readPathEntry(QStringLiteral("File%1").arg(i), QString()));
0062         if(url.isEmpty())
0063             break;
0064         const QString encoding = encodingForUrl(url);
0065         if(!encoding.isEmpty())
0066             config.writeEntry(QStringLiteral("Encoding%1").arg(i), encoding);
0067     }
0068 }