File indexing completed on 2024-05-12 05:37:10
0001 /* 0002 SPDX-FileCopyrightText: 2020 Konrad Materka <materka@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "systemtraysettings.h" 0008 0009 #include "debug.h" 0010 0011 #include <KConfigLoader> 0012 0013 static const QString KNOWN_ITEMS_KEY = QStringLiteral("knownItems"); 0014 static const QString EXTRA_ITEMS_KEY = QStringLiteral("extraItems"); 0015 static const QString SHOW_ALL_ITEMS_KEY = QStringLiteral("showAllItems"); 0016 static const QString SHOWN_ITEMS_KEY = QStringLiteral("shownItems"); 0017 static const QString HIDDEN_ITEMS_KEY = QStringLiteral("hiddenItems"); 0018 0019 SystemTraySettings::SystemTraySettings(KConfigLoader *config, QObject *parent) 0020 : QObject(parent) 0021 , config(config) 0022 { 0023 connect(config, &KConfigLoader::configChanged, this, [this]() { 0024 if (!updatingConfigValue) { 0025 loadConfig(); 0026 } 0027 }); 0028 0029 loadConfig(); 0030 } 0031 0032 bool SystemTraySettings::isKnownPlugin(const QString &pluginId) 0033 { 0034 return m_knownItems.contains(pluginId); 0035 } 0036 0037 const QStringList SystemTraySettings::knownPlugins() const 0038 { 0039 return m_knownItems; 0040 } 0041 0042 void SystemTraySettings::addKnownPlugin(const QString &pluginId) 0043 { 0044 m_knownItems << pluginId; 0045 writeConfigValue(KNOWN_ITEMS_KEY, m_knownItems); 0046 } 0047 0048 void SystemTraySettings::removeKnownPlugin(const QString &pluginId) 0049 { 0050 m_knownItems.removeAll(pluginId); 0051 writeConfigValue(KNOWN_ITEMS_KEY, m_knownItems); 0052 } 0053 0054 bool SystemTraySettings::isEnabledPlugin(const QString &pluginId) const 0055 { 0056 return m_extraItems.contains(pluginId); 0057 } 0058 0059 const QStringList SystemTraySettings::enabledPlugins() const 0060 { 0061 return m_extraItems; 0062 } 0063 0064 void SystemTraySettings::addEnabledPlugin(const QString &pluginId) 0065 { 0066 m_extraItems << pluginId; 0067 writeConfigValue(EXTRA_ITEMS_KEY, m_extraItems); 0068 Q_EMIT enabledPluginsChanged({pluginId}, {}); 0069 } 0070 0071 void SystemTraySettings::removeEnabledPlugin(const QString &pluginId) 0072 { 0073 m_extraItems.removeAll(pluginId); 0074 writeConfigValue(EXTRA_ITEMS_KEY, m_extraItems); 0075 Q_EMIT enabledPluginsChanged({}, {pluginId}); 0076 } 0077 0078 bool SystemTraySettings::isShowAllItems() const 0079 { 0080 return config->property(SHOW_ALL_ITEMS_KEY).toBool(); 0081 } 0082 0083 const QStringList SystemTraySettings::shownItems() const 0084 { 0085 return config->property(SHOWN_ITEMS_KEY).toStringList(); 0086 } 0087 0088 const QStringList SystemTraySettings::hiddenItems() const 0089 { 0090 return config->property(HIDDEN_ITEMS_KEY).toStringList(); 0091 } 0092 0093 void SystemTraySettings::cleanupPlugin(const QString &pluginId) 0094 { 0095 removeKnownPlugin(pluginId); 0096 removeEnabledPlugin(pluginId); 0097 0098 QStringList shown = shownItems(); 0099 shown.removeAll(pluginId); 0100 writeConfigValue(SHOWN_ITEMS_KEY, shown); 0101 0102 QStringList hidden = hiddenItems(); 0103 hidden.removeAll(pluginId); 0104 writeConfigValue(HIDDEN_ITEMS_KEY, hidden); 0105 } 0106 0107 void SystemTraySettings::loadConfig() 0108 { 0109 if (!config) { 0110 return; 0111 } 0112 config->load(); 0113 0114 m_knownItems = config->property(KNOWN_ITEMS_KEY).toStringList(); 0115 0116 QStringList extraItems = config->property(EXTRA_ITEMS_KEY).toStringList(); 0117 if (extraItems != m_extraItems) { 0118 QStringList extraItemsOld = m_extraItems; 0119 m_extraItems = extraItems; 0120 notifyAboutChangedEnabledPlugins(extraItemsOld, m_extraItems); 0121 } 0122 0123 Q_EMIT configurationChanged(); 0124 } 0125 0126 void SystemTraySettings::writeConfigValue(const QString &key, const QVariant &value) 0127 { 0128 if (!config) { 0129 return; 0130 } 0131 0132 KConfigSkeletonItem *item = config->findItemByName(key); 0133 if (item) { 0134 updatingConfigValue = true; 0135 item->setWriteFlags(KConfigBase::Notify); 0136 item->setProperty(value); 0137 config->save(); 0138 // refresh state of config scheme, if not, above writes are ignored 0139 config->read(); 0140 updatingConfigValue = false; 0141 } 0142 0143 Q_EMIT configurationChanged(); 0144 } 0145 0146 void SystemTraySettings::notifyAboutChangedEnabledPlugins(const QStringList &enabledPluginsOld, const QStringList &enabledPluginsNew) 0147 { 0148 QStringList newlyEnabled; 0149 QStringList newlyDisabled; 0150 0151 for (const QString &pluginId : enabledPluginsOld) { 0152 if (!enabledPluginsNew.contains(pluginId)) { 0153 newlyDisabled << pluginId; 0154 } 0155 } 0156 0157 for (const QString &pluginId : enabledPluginsNew) { 0158 if (!enabledPluginsOld.contains(pluginId)) { 0159 newlyEnabled << pluginId; 0160 } 0161 } 0162 0163 Q_EMIT enabledPluginsChanged(newlyEnabled, newlyDisabled); 0164 }