| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2017~2017 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "fcitxqtconfiguifactory.h" |
| 8 |
#include "fcitxqtconfiguifactory_p.h" |
| 9 |
#include "fcitxqtconfiguiplugin.h" |
| 10 |
#include <QDebug> |
| 11 |
#include <QDir> |
| 12 |
#include <QLatin1String> |
| 13 |
#include <QLibrary> |
| 14 |
#include <QObject> |
| 15 |
#include <QPluginLoader> |
| 16 |
#include <QStandardPaths> |
| 17 |
#include <fcitx-utils/i18n.h> |
| 18 |
#include <fcitx-utils/standardpaths.h> |
| 19 |
#include <filesystem> |
| 20 |
|
| 21 |
namespace fcitx { |
| 22 |
|
| 23 |
namespace { |
| 24 |
|
| 25 |
constexpr char addonConfigPrefix[] = "fcitx://config/addon/"; |
| 26 |
|
| 27 |
QString normalizePath(const QString &file) { |
| 28 |
auto path = file; |
| 29 |
if (path.startsWith(addonConfigPrefix)) { |
| 30 |
path.remove(0, sizeof(addonConfigPrefix) - 1); |
| 31 |
} |
| 32 |
return path; |
| 33 |
} |
| 34 |
|
| 35 |
} // namespace |
| 36 |
|
| 37 |
FcitxQtConfigUIFactoryPrivate::FcitxQtConfigUIFactoryPrivate( |
| 38 |
FcitxQtConfigUIFactory *factory) |
| 39 |
: QObject(factory), q_ptr(factory) {} |
| 40 |
|
| 41 |
FcitxQtConfigUIFactoryPrivate::~FcitxQtConfigUIFactoryPrivate() {} |
| 42 |
|
| 43 |
FcitxQtConfigUIFactory::FcitxQtConfigUIFactory(QObject *parent) |
| 44 |
: QObject(parent), d_ptr(new FcitxQtConfigUIFactoryPrivate(this)) { |
| 45 |
Q_D(FcitxQtConfigUIFactory); |
| 46 |
d->scan(); |
| 47 |
} |
| 48 |
|
| 49 |
FcitxQtConfigUIFactory::~FcitxQtConfigUIFactory() {} |
| 50 |
|
| 51 |
FcitxQtConfigUIWidget *FcitxQtConfigUIFactory::create(const QString &file) { |
| 52 |
Q_D(FcitxQtConfigUIFactory); |
| 53 |
|
| 54 |
auto path = normalizePath(file); |
| 55 |
auto *loader = d->plugins_.value(path); |
| 56 |
if (!loader) { |
| 57 |
return nullptr; |
| 58 |
} |
| 59 |
|
| 60 |
auto *instance = |
| 61 |
qobject_cast<FcitxQtConfigUIFactoryInterface *>(loader->instance()); |
| 62 |
if (!instance) { |
| 63 |
return nullptr; |
| 64 |
} |
| 65 |
return instance->create(path.section('/', 1)); |
| 66 |
} |
| 67 |
|
| 68 |
bool FcitxQtConfigUIFactory::test(const QString &file) { |
| 69 |
Q_D(FcitxQtConfigUIFactory); |
| 70 |
|
| 71 |
auto path = normalizePath(file); |
| 72 |
return d->plugins_.contains(path); |
| 73 |
} |
| 74 |
|
| 75 |
void FcitxQtConfigUIFactoryPrivate::scan() { |
| 76 |
auto addonFiles = fcitx::StandardPaths::global().locate( |
| 77 |
fcitx::StandardPathsType::Addon, "qt6", |
| 78 |
[](const std::filesystem::path &path) { |
| 79 |
return QLibrary::isLibrary(QString::fromStdString(path)); |
| 80 |
}, |
| 81 |
StandardPathsMode::System); |
| 82 |
|
| 83 |
for (const auto &[_, filePath] : addonFiles) { |
| 84 |
auto *loader = |
| 85 |
new QPluginLoader(QString::fromStdString(filePath), this); |
| 86 |
if (loader->metaData().value("IID") != |
| 87 |
QLatin1String(FcitxQtConfigUIFactoryInterface_iid)) { |
| 88 |
delete loader; |
| 89 |
continue; |
| 90 |
} |
| 91 |
auto metadata = loader->metaData().value("MetaData").toObject(); |
| 92 |
auto files = metadata.value("files").toVariant().toStringList(); |
| 93 |
auto addon = metadata.value("addon").toVariant().toString(); |
| 94 |
for (const auto &file : files) { |
| 95 |
plugins_[addon + "/" + file] = loader; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} // namespace fcitx |