File indexing completed on 2024-05-05 16:13:56

0001 /*
0002     SPDX-FileCopyrightText: 2000 Yves Arrouye <yves@realnames.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 // Own
0008 #include "main.h"
0009 
0010 // Qt
0011 #include <QTabWidget>
0012 #include <QVBoxLayout>
0013 
0014 // KDE
0015 #include <KAboutData>
0016 #include <KLocalizedString>
0017 #include <KPluginFactory>
0018 #include <KPluginMetaData>
0019 #include <kurifilter.h>
0020 
0021 K_PLUGIN_CLASS_WITH_JSON(KURIFilterModule, "webshortcuts.json")
0022 
0023 KURIFilterModule::KURIFilterModule(QWidget *parent, const QVariantList &args)
0024     : KCModule(parent, args)
0025     , m_widget(nullptr)
0026 {
0027     KAboutData *about = new KAboutData(QStringLiteral("kcm_webshortcuts"),
0028                                        i18n("Web Search Keywords"),
0029                                        QStringLiteral("0.1"),
0030                                        i18n("Configure enhanced browsing features"),
0031                                        KAboutLicense::GPL);
0032     setAboutData(about);
0033 
0034     KCModule::setButtons(KCModule::Buttons(KCModule::Default | KCModule::Apply | KCModule::Help));
0035 
0036     filter = KUriFilter::self();
0037 
0038     setQuickHelp(
0039         i18n("<h1>Enhanced Browsing</h1> In this module you can configure some enhanced browsing"
0040              " features of KDE. "
0041              "<h2>Web Search Keywords</h2>Web Search Keywords are a quick way of using Web search engines. For example, type \"duckduckgo:frobozz\""
0042              " or \"dd:frobozz\" and your web browser will do a search on DuckDuckGo for \"frobozz\"."
0043              " Even easier: just press Alt+F2 (if you have not"
0044              " changed this keyboard shortcut) and enter the shortcut in the Run Command dialog."));
0045 
0046     QVBoxLayout *layout = new QVBoxLayout(this);
0047 
0048     QMap<QString, KCModule *> helper;
0049     // Load the plugins. This saves a public method in KUriFilter just for this.
0050 
0051     const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("kf" QT_STRINGIFY(QT_VERSION_MAJOR) "/urifilters"));
0052     for (const KPluginMetaData &pluginMetaData : plugins) {
0053         if (auto factory = KPluginFactory::loadFactory(pluginMetaData).plugin) {
0054             KCModule *module = nullptr;
0055 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 82)
0056             KUriFilterPlugin *plugin = factory->create<KUriFilterPlugin>(nullptr);
0057             if (plugin) {
0058                 module = plugin->configModule(this, nullptr);
0059             }
0060 #endif
0061             if (!module) {
0062                 module = factory->create<KCModule>(this);
0063             }
0064             if (module) {
0065                 modules.append(module);
0066                 helper.insert(module->windowTitle(), module);
0067                 connect(module, qOverload<bool>(&KCModule::changed), this, qOverload<bool>(&KCModule::changed));
0068             }
0069         }
0070     }
0071 
0072     if (modules.count() > 1) {
0073         QTabWidget *tab = new QTabWidget(this);
0074 
0075         QMap<QString, KCModule *>::iterator it2;
0076         for (it2 = helper.begin(); it2 != helper.end(); ++it2) {
0077             tab->addTab(it2.value(), it2.key());
0078         }
0079 
0080         tab->setCurrentIndex(tab->indexOf(modules.first()));
0081         m_widget = tab;
0082     } else if (modules.count() == 1) {
0083         m_widget = modules.first();
0084         if (m_widget->layout()) {
0085             m_widget->layout()->setContentsMargins(0, 0, 0, 0);
0086         }
0087     }
0088 
0089     if (m_widget) {
0090         layout->addWidget(m_widget);
0091     }
0092     setMinimumWidth(700);
0093 }
0094 
0095 void KURIFilterModule::load()
0096 {
0097     static bool firstLoad = true;
0098 
0099     // Modules automatically call load() when first shown, but subsequent
0100     // calls need to be propagated to make the`Reset` button work
0101     if (firstLoad) {
0102         firstLoad = false;
0103         return;
0104     }
0105 
0106     for (KCModule *module : std::as_const(modules)) {
0107         module->load();
0108     }
0109 }
0110 
0111 void KURIFilterModule::save()
0112 {
0113     for (KCModule *module : std::as_const(modules)) {
0114         module->save();
0115     }
0116 }
0117 
0118 void KURIFilterModule::defaults()
0119 {
0120     for (KCModule *module : std::as_const(modules)) {
0121         module->defaults();
0122     }
0123 }
0124 
0125 KURIFilterModule::~KURIFilterModule()
0126 {
0127     qDeleteAll(modules);
0128 }
0129 
0130 #include "main.moc"
0131 #include "moc_main.cpp"