File indexing completed on 2024-06-16 04:33:42

0001 /*
0002     SPDX-FileCopyrightText: 2009 Michal Malek <michalm@jabster.pl>
0003     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "k3bpluginmanager.h"
0009 #include "k3bplugin.h"
0010 #include "k3bpluginconfigwidget.h"
0011 #include "k3bversion.h"
0012 #include "k3b_i18n.h"
0013 
0014 #include <kcmutils_export.h>
0015 #include <KCModuleLoader>
0016 #include <KPluginFactory>
0017 #include <KPluginMetaData>
0018 #include <KMessageBox>
0019 
0020 #include <QDebug>
0021 #include <QDir>
0022 #include <QList>
0023 #include <QMap>
0024 #include <QSharedPointer>
0025 #include <QAbstractButton>
0026 #include <QDialog>
0027 #include <QDialogButtonBox>
0028 #include <QVBoxLayout>
0029 
0030 
0031 
0032 class K3b::PluginManager::Private
0033 {
0034 public:
0035     Private(){
0036     }
0037 
0038     QList<K3b::Plugin*> plugins;
0039 
0040     KCModule* getModule( Plugin* plugin ) const;
0041 };
0042 
0043 
0044 
0045 
0046 K3b::PluginManager::PluginManager( QObject* parent )
0047     : QObject( parent ),
0048       d( new Private() )
0049 {
0050 }
0051 
0052 
0053 K3b::PluginManager::~PluginManager()
0054 {
0055     delete d;
0056 }
0057 
0058 
0059 
0060 QStringList K3b::PluginManager::categories() const
0061 {
0062     QStringList grps;
0063 
0064     QList<K3b::Plugin*> fl;
0065     Q_FOREACH( K3b::Plugin* plugin, d->plugins ) {
0066         if( !grps.contains( plugin->category() ) )
0067             grps.append( plugin->category() );
0068     }
0069 
0070     return grps;
0071 }
0072 
0073 
0074 QList<K3b::Plugin*> K3b::PluginManager::plugins( const QString& group ) const
0075 {
0076     QList<K3b::Plugin*> fl;
0077     Q_FOREACH( K3b::Plugin* plugin, d->plugins ) {
0078         if( plugin->category() == group || group.isEmpty() )
0079             fl.append( plugin );
0080     }
0081     return fl;
0082 }
0083 
0084 KCModule *K3b::PluginManager::Private::getModule( Plugin* plugin ) const
0085 {
0086     const QString kcm = plugin->pluginMetaData().value("X-KDE-ConfigModule");
0087     qDebug() << "for plugin" << plugin->pluginMetaData().pluginId() << "KCM" << kcm;
0088     if (!kcm.isEmpty()) {
0089         return KCModuleLoader::loadModule(kcm);
0090     }
0091     return 0;
0092 }
0093 
0094 
0095 void K3b::PluginManager::loadAll()
0096 {
0097     const QVector<KPluginMetaData> metadataList = KPluginMetaData::findPlugins("k3b_plugins");
0098     for (const auto &metadata : metadataList) {
0099         KPluginFactory::Result<K3b::Plugin> result = KPluginFactory::instantiatePlugin<K3b::Plugin>(metadata);
0100         if (result) {
0101             K3b::Plugin *plugin = result.plugin;
0102             plugin->d->metadata = metadata;
0103             qDebug() << "Loaded plugin" << metadata.fileName();
0104             d->plugins.append(plugin);
0105         } else {
0106             qDebug() << "failed to load plugin" << metadata.fileName();
0107         }
0108     }
0109 }
0110 
0111 int K3b::PluginManager::pluginSystemVersion() const
0112 {
0113     return K3B_PLUGIN_SYSTEM_VERSION;
0114 }
0115 
0116 
0117 bool K3b::PluginManager::hasPluginDialog( Plugin* plugin ) const
0118 {
0119     QSharedPointer<KCModule> module( d->getModule( plugin ) );
0120     return !module.isNull();
0121 }
0122 
0123 
0124 int K3b::PluginManager::execPluginDialog( Plugin* plugin, QWidget* parent )
0125 {
0126     if( auto module = d->getModule( plugin ) ) {
0127         QDialog dlg( parent );
0128         dlg.setWindowTitle( plugin->pluginMetaData().name() );
0129         QVBoxLayout* layout = new QVBoxLayout( &dlg );
0130         QDialogButtonBox* buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::RestoreDefaults, &dlg );
0131 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0132         layout->addWidget( module );
0133 #else
0134         layout->addWidget( module->widget() );
0135 #endif
0136         layout->addWidget( buttonBox );
0137 
0138         connect( buttonBox, &QDialogButtonBox::clicked, [&](QAbstractButton *button) {
0139             switch( buttonBox->standardButton( button ) )
0140             {
0141             case QDialogButtonBox::Ok: dlg.accept(); break;
0142             case QDialogButtonBox::Cancel: dlg.reject(); break;
0143             case QDialogButtonBox::RestoreDefaults: module->defaults(); break;
0144             default: break;
0145             }
0146         } );
0147         
0148         int ret = dlg.exec();
0149         if( ret == QDialog::Accepted )
0150         {
0151             module->save();
0152         }
0153         return ret;
0154     }
0155     else {
0156         KMessageBox::error( parent, i18n("No settings available for plugin %1.", plugin->pluginMetaData().name() ) );
0157         return 0;
0158     }
0159 }
0160 
0161 #include "moc_k3bpluginmanager.cpp"