File indexing completed on 2024-05-12 16:35:47
0001 /* This file is part of the KDE project 0002 Copyright 2008 Stefan Nikolaus <stefan.nikolaus@kdemail.net> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 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 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "ToolRegistry.h" 0021 0022 #include "CellTool.h" 0023 #include "CellToolFactory.h" 0024 0025 #include <KSharedConfig> 0026 #include <KConfigGroup> 0027 #include <KPluginFactory> 0028 #include <QGlobalStatic> 0029 0030 #include <KoPluginLoader.h> 0031 #include <KoToolRegistry.h> 0032 0033 Q_GLOBAL_STATIC(Calligra::Sheets::ToolRegistry, s_instance) 0034 0035 using namespace Calligra::Sheets; 0036 0037 0038 class Q_DECL_HIDDEN ToolRegistry::Private 0039 { 0040 public: 0041 }; 0042 0043 0044 ToolRegistry::ToolRegistry() 0045 : d(new Private) 0046 { 0047 // Add the built-in cell tool. 0048 KoToolRegistry::instance()->add(new CellToolFactory("KSpreadCellToolId")); 0049 // Load the tool plugins. 0050 loadTools(); 0051 } 0052 0053 ToolRegistry::~ToolRegistry() 0054 { 0055 delete d; 0056 } 0057 0058 ToolRegistry* ToolRegistry::instance() 0059 { 0060 return s_instance; 0061 } 0062 0063 void ToolRegistry::loadTools() 0064 { 0065 const QList<QPluginLoader *> offers = KoPluginLoader::pluginLoaders(QStringLiteral("calligrasheets/tools")); 0066 debugSheetsFormula << offers.count() << "tools found."; 0067 0068 const KConfigGroup pluginsConfigGroup = KSharedConfig::openConfig()->group("Plugins"); 0069 foreach (QPluginLoader *loader, offers) { 0070 QJsonObject metaData = loader->metaData().value("MetaData").toObject(); 0071 int version = metaData.value("X-CalligraSheets-InterfaceVersion").toInt(); 0072 if (version != 0) { 0073 debugSheetsFormula << "Skipping" << loader->fileName() << ", because interface version is" << version; 0074 continue; 0075 } 0076 QJsonObject pluginData = metaData.value("KPlugin").toObject(); 0077 QString category = pluginData.value("Category").toString(); 0078 if (category != "Tool") { 0079 debugSheetsFormula << "Skipping" << loader->fileName() << ", because category is " << category; 0080 continue; 0081 } 0082 0083 KPluginFactory* factory = qobject_cast<KPluginFactory *>(loader->instance()); 0084 if (!factory) { 0085 debugSheetsFormula << "Unable to create plugin factory for" << loader->fileName(); 0086 continue; 0087 } 0088 QObject *object = factory->create<QObject>(this, QVariantList()); 0089 CellToolFactory *toolFactory = dynamic_cast<CellToolFactory*>(object); 0090 if (!toolFactory) { 0091 debugSheetsFormula << "Unable to create tool factory for" << loader->fileName(); 0092 continue; 0093 } 0094 const QString pluginConfigEnableKey = pluginData.value("Id").toString() + QLatin1String("Enabled"); 0095 const bool isPluginEnabled = pluginsConfigGroup.hasKey(pluginConfigEnableKey) ? 0096 pluginsConfigGroup.readEntry(pluginConfigEnableKey, true) : 0097 pluginData.value("EnabledByDefault").toBool(true); 0098 0099 if (isPluginEnabled) { 0100 // Tool already registered? 0101 if (KoToolRegistry::instance()->contains(toolFactory->id())) { 0102 continue; 0103 } 0104 0105 toolFactory->setIconName(pluginData.value("Icon").toString()); 0106 toolFactory->setPriority(10); 0107 toolFactory->setToolTip(pluginData.value("Description").toString()); 0108 KoToolRegistry::instance()->add(toolFactory); 0109 } else { 0110 // Tool not registered? 0111 if (!KoToolRegistry::instance()->contains(toolFactory->id())) { 0112 continue; 0113 } 0114 delete KoToolRegistry::instance()->value(toolFactory->id()); 0115 KoToolRegistry::instance()->remove(toolFactory->id()); 0116 } 0117 } 0118 qDeleteAll(offers); 0119 }