File indexing completed on 2024-09-08 04:55:18
0001 /** 0002 * SPDX-FileCopyrightText: 2022 Suhaas Joshi <joshiesuhaas0@gmail.com> 0003 * SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk> 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "kcm.h" 0008 0009 #include <KLocalizedString> 0010 #include <KPluginFactory> 0011 #include <QFile> 0012 #include <algorithm> 0013 0014 K_PLUGIN_CLASS_WITH_JSON(KCMFlatpak, "kcm_flatpak.json") 0015 0016 KCMFlatpak::KCMFlatpak(QObject *parent, const KPluginMetaData &data, const QVariantList &args) 0017 : KQuickManagedConfigModule(parent, data) 0018 , m_refsModel(new FlatpakReferencesModel(this)) 0019 { 0020 QString requestedReference; 0021 if (args.count() > 0) { 0022 const QVariant &arg0 = args.at(0); 0023 if (arg0.canConvert<QString>()) { 0024 const QString arg0str = arg0.toString(); 0025 requestedReference = arg0str; 0026 } 0027 } 0028 0029 constexpr const char *uri = "org.kde.plasma.kcm.flatpakpermissions"; 0030 0031 qmlRegisterUncreatableType<KCMFlatpak>(uri, 1, 0, "KCMFlatpak", QString()); 0032 qmlRegisterUncreatableType<FlatpakReference>(uri, 1, 0, "FlatpakReference", QStringLiteral("Should be obtained from a FlatpakReferencesModel")); 0033 qmlRegisterType<FlatpakPermissionModel>(uri, 1, 0, "FlatpakPermissionModel"); 0034 qmlRegisterUncreatableType<FlatpakReferencesModel>(uri, 1, 0, "FlatpakReferencesModel", QStringLiteral("For enum access only")); 0035 qmlRegisterUncreatableType<FlatpakPermissionsSectionType>(uri, 1, 0, "FlatpakPermissionsSectionType", QStringLiteral("For enum access only")); 0036 0037 connect(m_refsModel, &FlatpakReferencesModel::needsLoad, this, &KCMFlatpak::load); 0038 connect(m_refsModel, &FlatpakReferencesModel::settingsChanged, this, &KCMFlatpak::settingsChanged); 0039 0040 if (!requestedReference.isEmpty()) { 0041 const auto &refs = m_refsModel->references(); 0042 const auto it = std::find_if(refs.constBegin(), refs.constEnd(), [&](FlatpakReference *ref) { 0043 return ref->ref() == requestedReference; 0044 }); 0045 if (it != refs.constEnd()) { 0046 const auto index = std::distance(refs.constBegin(), it); 0047 m_index = index; 0048 } 0049 } 0050 0051 settingsChanged(); // Initialize Reset & Defaults buttons 0052 } 0053 0054 void KCMFlatpak::load() 0055 { 0056 m_refsModel->load(m_index); 0057 setNeedsSave(false); 0058 } 0059 0060 void KCMFlatpak::save() 0061 { 0062 m_refsModel->save(m_index); 0063 } 0064 0065 void KCMFlatpak::defaults() 0066 { 0067 m_refsModel->defaults(m_index); 0068 } 0069 0070 bool KCMFlatpak::isSaveNeeded() const 0071 { 0072 return m_refsModel->isSaveNeeded(m_index); 0073 } 0074 0075 bool KCMFlatpak::isDefaults() const 0076 { 0077 return m_refsModel->isDefaults(m_index); 0078 } 0079 0080 void KCMFlatpak::setIndex(int index) 0081 { 0082 m_index = index; 0083 settingsChanged(); // Because Apply, Reset & Defaults buttons depend on m_index. 0084 } 0085 0086 int KCMFlatpak::currentIndex() const 0087 { 0088 return m_index; 0089 } 0090 0091 #include "kcm.moc" 0092 0093 #include "moc_kcm.cpp"