File indexing completed on 2024-04-28 16:24:37

0001 /* This file is part of the KDE project
0002   Copyright (C) 2009, 2012 Dag Andersen <danders@get2net.dk>
0003   Copyright (C) 2016 Dag Andersen <danders@get2net.dk>
0004   
0005   This library is free software; you can redistribute it and/or
0006   modify it under the terms of the GNU Library General Public
0007   License as published by the Free Software Foundation; either
0008   version 2 of the License, or (at your option) any later version.
0009 
0010   This library is distributed in the hope that it will be useful,
0011   but WITHOUT ANY WARRANTY; without even the implied warranty of
0012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013   Library General Public License for more details.
0014 
0015   You should have received a copy of the GNU Library General Public License
0016   along with this library; see the file COPYING.LIB.  If not, write to
0017   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018   Boston, MA 02110-1301, USA.
0019 */
0020 
0021 
0022 // clazy:excludeall=qstring-arg
0023 #include "kptschedulerpluginloader.h"
0024  
0025 #include "kptschedulerplugin.h"
0026 #include "kptdebug.h"
0027 
0028 #include <KoPluginLoader.h>
0029 
0030 #include <QPluginLoader>
0031 #include <QLocale>
0032 
0033 
0034 namespace KPlato
0035 {
0036 
0037 SchedulerPluginLoader::SchedulerPluginLoader(QObject * parent)
0038   : QObject(parent)
0039 {
0040 }
0041  
0042 SchedulerPluginLoader::~SchedulerPluginLoader()
0043 {
0044 }
0045 
0046 static
0047 QJsonValue readLocalValue(const QJsonObject &json, const QString &key)
0048 {
0049     // start with language_country
0050     const QString localeName = QLocale().name();
0051 
0052     QString localKey = key + QLatin1Char('[') + localeName + QLatin1Char(']');
0053     QJsonObject::ConstIterator it = json.constFind(localKey);
0054     if (it != json.constEnd()) {
0055         return it.value();
0056     }
0057 
0058     // drop _country
0059     const int separatorIndex = localeName.indexOf(QLatin1Char('_'));
0060     if (separatorIndex != -1) {
0061         const int localKeySeparatorIndex = key.length() + 1 + separatorIndex;
0062         localKey[localKeySeparatorIndex] = QLatin1Char(']');
0063         localKey.truncate(localKeySeparatorIndex + 1);
0064        it = json.constFind(localKey);
0065         if (it != json.constEnd()) {
0066             return it.value();
0067         }
0068     }
0069 
0070     // default to unlocalized value
0071     return json.value(key);
0072 }
0073 
0074 
0075 void SchedulerPluginLoader::loadAllPlugins()
0076 {
0077     debugPlan << "Load all plugins";
0078     const QList<QPluginLoader *> offers = KoPluginLoader::pluginLoaders(QStringLiteral("calligraplan/schedulers"));
0079 
0080     foreach(QPluginLoader *pluginLoader, offers) {
0081         KPluginFactory *factory = qobject_cast<KPluginFactory*>(pluginLoader->instance());
0082  
0083         if (!factory)
0084         {
0085             errorPlan << "KPluginFactory could not load the plugin:" << pluginLoader->fileName();
0086             continue;
0087         }
0088  
0089         SchedulerPlugin *plugin = factory->create<SchedulerPlugin>(this);
0090  
0091         if (plugin) {
0092             QJsonObject json = pluginLoader->metaData().value("MetaData").toObject();
0093             json = json.value("KPlugin").toObject();
0094             const QString key = json.value(QLatin1String("Name")).toString(); // use unlocalized name as plugin identifier
0095             const QString name = readLocalValue(json, QLatin1String("Name")).toString();
0096             const QString comment = readLocalValue(json, QLatin1String("Description")).toString();
0097 
0098             debugPlan << "Load plugin:" << key << name << ", " << comment;
0099             plugin->setName(name);
0100             plugin->setComment(comment);
0101             emit pluginLoaded(key, plugin);
0102         } else {
0103            debugPlan << "KPluginFactory could not create SchedulerPlugin:" << pluginLoader->fileName();
0104         }
0105     }
0106     qDeleteAll(offers);
0107 }
0108 
0109 } //namespace KPlato