File indexing completed on 2024-05-19 05:55:46

0001 /*
0002     SPDX-FileCopyrightText: 2013 Valentin Rusu <kde@rusu.info>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "authorizedappmodel.h"
0008 #include "kwalletmanager_debug.h"
0009 #include <KConfigGroup>
0010 #include <KWallet>
0011 #include <QTimer>
0012 
0013 AuthorizedAppModel::AuthorizedAppModel(KWallet::Wallet *wallet):
0014     QStandardItemModel(),
0015     _cfg(KSharedConfig::openConfig(QStringLiteral("kwalletrc"), KConfig::NoGlobals)),
0016     _wallet(wallet)
0017 {
0018     // TODO: handle "Auto Deny" applications
0019     // KConfigGroup ad(_cfg, "Auto Deny");
0020 
0021     KConfigGroup aa(_cfg, QStringLiteral("Auto Allow"));
0022     QString walletName = _wallet->walletName();
0023     const QStringList keys = aa.entryMap().keys();
0024     for (const QString &cfgWalletName : keys) {
0025         if (cfgWalletName == walletName) {
0026             const QStringList apps = aa.readEntry(cfgWalletName, QStringList());
0027             int row = 0;
0028             for (const QString &appName : apps) {
0029                 setItem(row, 0, new QStandardItem(appName));
0030                 setItem(row, 1, new QStandardItem(QStringLiteral("dummy"))); // this item will be hidden by the disconnect button, see below setIndexWidget call
0031                 _authorizedAppsIndexMap.insert(appName, QPersistentModelIndex(index(row, 0)));
0032                 row++;
0033             }
0034         }
0035     }
0036 }
0037 
0038 void AuthorizedAppModel::removeApp(const QString &appName)
0039 {
0040     if (_authorizedAppsIndexMap.contains(appName)) {
0041         QPersistentModelIndex idx = _authorizedAppsIndexMap[appName];
0042         if (idx.isValid()) {
0043             if (!removeRow(idx.row())) {
0044                 qCDebug(KWALLETMANAGER_LOG) << "Remove row failed for app " << appName;
0045             }
0046         }
0047     } else {
0048         qCDebug(KWALLETMANAGER_LOG) << "Attempting to remove unknown application " << appName;
0049     }
0050     QTimer::singleShot(0, this, &AuthorizedAppModel::saveConfig);
0051 }
0052 
0053 void AuthorizedAppModel::saveConfig()
0054 {
0055     QStringList appList;
0056     appList.reserve(rowCount());
0057     for (int r = 0; r < rowCount(); r++) {
0058         appList << item(r)->text();
0059     }
0060     QString walletName = _wallet->walletName();
0061     KConfigGroup config(_cfg, QStringLiteral("Auto Allow"));
0062     config.deleteEntry(walletName);
0063     config.writeEntry(_wallet->walletName(), appList);
0064     _cfg->sync();
0065 }
0066 
0067 #include "moc_authorizedappmodel.cpp"