File indexing completed on 2023-10-01 08:41:44
0001 /* 0002 * Copyright (C) 2012 Lasath Fernando <kde@lasath.org> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Lesser General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2.1 of the License, or (at your option) any later version. 0008 * 0009 * This library is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 * Lesser General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Lesser General Public 0015 * License along with this library; if not, write to the Free Software 0016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 0017 */ 0018 0019 #include "message-filter-config-manager.h" 0020 #include "message-processor-private.h" 0021 0022 #include <QMutex> 0023 #include <QSet> 0024 0025 #include "ktp-debug.h" 0026 #include <KServiceTypeTrader> 0027 0028 typedef QSet<KPluginInfo> PluginSet; 0029 0030 using namespace KTp; 0031 0032 class MessageFilterConfigManager::Private 0033 { 0034 public: 0035 Private(MessageFilterConfigManager *parent): 0036 q(parent) 0037 { } 0038 0039 PluginSet all; 0040 PluginSet enabled; 0041 0042 KService::List offers() const; 0043 void generateCache(); 0044 0045 private: 0046 MessageFilterConfigManager *q; 0047 }; 0048 0049 KService::List MessageFilterConfigManager::Private::offers() const 0050 { 0051 return KServiceTypeTrader::self()->query(QLatin1String("KTpTextUi/MessageFilter"), 0052 QLatin1String("[X-KTp-PluginInfo-Version] == " KTP_MESSAGE_FILTER_FRAMEWORK_VERSION)); 0053 } 0054 0055 0056 void MessageFilterConfigManager::Private::generateCache() 0057 { 0058 KPluginInfo::List pluginInfos = KPluginInfo::fromServices(offers(), q->configGroup()); 0059 for (KPluginInfo::List::Iterator i = pluginInfos.begin(); i != pluginInfos.end(); i++) { 0060 KPluginInfo &plugin = *i; 0061 0062 all.insert(plugin); 0063 0064 plugin.load(); 0065 if (plugin.isPluginEnabled()) { 0066 enabled.insert(plugin); 0067 } 0068 } 0069 } 0070 0071 MessageFilterConfigManager *MessageFilterConfigManager::self() 0072 { 0073 static MessageFilterConfigManager *mfcm_instance; 0074 static QMutex mutex; 0075 mutex.lock(); 0076 if (!mfcm_instance) { 0077 mfcm_instance = new MessageFilterConfigManager; 0078 } 0079 mutex.unlock(); 0080 0081 return mfcm_instance; 0082 } 0083 0084 MessageFilterConfigManager::MessageFilterConfigManager() : 0085 d(new Private(this)) 0086 { 0087 d->generateCache(); 0088 } 0089 0090 MessageFilterConfigManager::~MessageFilterConfigManager() 0091 { 0092 delete d; 0093 } 0094 0095 KPluginInfo::List MessageFilterConfigManager::allPlugins() const 0096 { 0097 return d->all.values(); 0098 } 0099 0100 KPluginInfo::List MessageFilterConfigManager::enabledPlugins() const 0101 { 0102 return d->enabled.values(); 0103 } 0104 0105 KConfigGroup MessageFilterConfigManager::configGroup() const 0106 { 0107 return sharedConfig()->group("Plugins"); 0108 } 0109 0110 KSharedConfig::Ptr MessageFilterConfigManager::sharedConfig() const 0111 { 0112 return KSharedConfig::openConfig(QLatin1String("ktelepathyrc")); 0113 } 0114 0115 void MessageFilterConfigManager::reloadConfig() 0116 { 0117 PluginSet::ConstIterator iter = d->all.constBegin(); 0118 for ( ; iter != d->all.constEnd(); ++iter) { 0119 KPluginInfo pluginInfo = *iter; 0120 0121 const bool wasEnabled = d->enabled.contains(pluginInfo); 0122 0123 if (!wasEnabled && pluginInfo.isPluginEnabled()) { 0124 d->enabled.insert(pluginInfo); 0125 MessageProcessor::instance()->d->loadFilter(pluginInfo); 0126 } else if (wasEnabled && !pluginInfo.isPluginEnabled()) { 0127 d->enabled.remove(pluginInfo); 0128 MessageProcessor::instance()->d->unloadFilter(pluginInfo); 0129 } 0130 } 0131 }