| 1 |
/* This file is part of the KDE project |
| 2 |
* SPDX-FileCopyrightText: 2021 Dag Andersen <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
// clazy:excludeall=qstring-arg |
| 8 |
#include "TestSchedulerPluginLoader.h" |
| 9 |
|
| 10 |
#include "kptschedulerplugin.h" |
| 11 |
#include "kptdebug.h" |
| 12 |
|
| 13 |
#include <KoPluginLoader.h> |
| 14 |
|
| 15 |
#include <QPluginLoader> |
| 16 |
#include <QLocale> |
| 17 |
|
| 18 |
|
| 19 |
namespace KPlato |
| 20 |
{ |
| 21 |
|
| 22 |
TestSchedulerPluginLoader::TestSchedulerPluginLoader(QObject * parent) |
| 23 |
: QObject(parent) |
| 24 |
{ |
| 25 |
} |
| 26 |
|
| 27 |
TestSchedulerPluginLoader::~TestSchedulerPluginLoader() |
| 28 |
{ |
| 29 |
qInfo()<<Q_FUNC_INFO; |
| 30 |
} |
| 31 |
|
| 32 |
static |
| 33 |
QJsonValue readLocalValue(const QJsonObject &json, const QString &key) |
| 34 |
{ |
| 35 |
// start with language_country |
| 36 |
const QString localeName = QLocale().name(); |
| 37 |
|
| 38 |
QString localKey = key + QLatin1Char('[') + localeName + QLatin1Char(']'); |
| 39 |
QJsonObject::ConstIterator it = json.constFind(localKey); |
| 40 |
if (it != json.constEnd()) { |
| 41 |
return it.value(); |
| 42 |
} |
| 43 |
|
| 44 |
// drop _country |
| 45 |
const int separatorIndex = localeName.indexOf(QLatin1Char('_')); |
| 46 |
if (separatorIndex != -1) { |
| 47 |
const int localKeySeparatorIndex = key.length() + 1 + separatorIndex; |
| 48 |
localKey[localKeySeparatorIndex] = QLatin1Char(']'); |
| 49 |
localKey.truncate(localKeySeparatorIndex + 1); |
| 50 |
it = json.constFind(localKey); |
| 51 |
if (it != json.constEnd()) { |
| 52 |
return it.value(); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// default to unlocalized value |
| 57 |
return json.value(key); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
SchedulerPlugin *TestSchedulerPluginLoader::loadPlugin(const QString &dir, const QString &name_) |
| 62 |
{ |
| 63 |
debugPlan << "Load plugin:"<<dir<<name_; |
| 64 |
SchedulerPlugin *schedulerPlugin = nullptr; |
| 65 |
|
| 66 |
const QList<QPluginLoader *> offers = KoPluginLoader::pluginLoaders(dir); |
| 67 |
|
| 68 |
for(QPluginLoader *pluginLoader : offers) { |
| 69 |
KPluginFactory *factory = qobject_cast<KPluginFactory*>(pluginLoader->instance()); |
| 70 |
|
| 71 |
if (!factory) |
| 72 |
{ |
| 73 |
errorPlan << "KPluginFactory could not load the plugin:" << pluginLoader->fileName(); |
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
QJsonObject json = pluginLoader->metaData().value("MetaData").toObject(); |
| 78 |
json = json.value("KPlugin").toObject(); |
| 79 |
const QString key = json.value(QStringLiteral("Name")).toString(); // use unlocalized name as plugin identifier |
| 80 |
const QString name = readLocalValue(json, QStringLiteral("Name")).toString(); |
| 81 |
const QString comment = readLocalValue(json, QStringLiteral("Description")).toString(); |
| 82 |
|
| 83 |
if (key == name_) { |
| 84 |
debugPlan << "Load plugin:" << key << name << ", " << comment; |
| 85 |
schedulerPlugin = factory->create<SchedulerPlugin>(this); |
| 86 |
Q_ASSERT(schedulerPlugin); |
| 87 |
schedulerPlugin->setName(name); |
| 88 |
schedulerPlugin->setComment(comment); |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
qDeleteAll(offers); |
| 93 |
return schedulerPlugin; |
| 94 |
} |
| 95 |
|
| 96 |
} //namespace KPlato |