File indexing completed on 2024-05-12 05:37:16

0001 /*
0002     SPDX-FileCopyrightText: 2009 Chani Armitage <chani@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "launch.h"
0008 
0009 #include <QDebug>
0010 
0011 #include <KConfigGroup>
0012 #include <KIO/ApplicationLauncherJob>
0013 #include <KPluginFactory>
0014 #include <Plasma/PluginLoader>
0015 
0016 AppLauncher::AppLauncher(QObject *parent, const QVariantList &args)
0017     : Plasma::ContainmentActions(parent, args)
0018     , m_group(new KServiceGroup(QStringLiteral("/")))
0019 {
0020 }
0021 
0022 AppLauncher::~AppLauncher()
0023 {
0024 }
0025 
0026 void AppLauncher::init(const KConfigGroup &)
0027 {
0028 }
0029 
0030 QList<QAction *> AppLauncher::contextualActions()
0031 {
0032     qDeleteAll(m_actions);
0033     m_actions.clear();
0034     makeMenu(nullptr, m_group);
0035 
0036     return m_actions;
0037 }
0038 
0039 void AppLauncher::makeMenu(QMenu *menu, const KServiceGroup::Ptr &group)
0040 {
0041     const auto entries = group->entries(true, true, true);
0042     for (const KSycocaEntry::Ptr &p : entries) {
0043         if (p->isType(KST_KService)) {
0044             const KService::Ptr service(static_cast<KService *>(p.data()));
0045 
0046             QString text = service->name();
0047             if (!m_showAppsByName && !service->genericName().isEmpty()) {
0048                 text = service->genericName();
0049             }
0050 
0051             QAction *action = new QAction(QIcon::fromTheme(service->icon()), text, this);
0052             connect(action, &QAction::triggered, [action]() {
0053                 KService::Ptr service = KService::serviceByStorageId(action->data().toString());
0054                 auto job = new KIO::ApplicationLauncherJob(service);
0055                 job->start();
0056             });
0057             action->setData(service->storageId());
0058             if (menu) {
0059                 menu->addAction(action);
0060             } else {
0061                 m_actions << action;
0062             }
0063         } else if (p->isType(KST_KServiceGroup)) {
0064             const KServiceGroup::Ptr service(static_cast<KServiceGroup *>(p.data()));
0065             if (service->childCount() == 0) {
0066                 continue;
0067             }
0068             QAction *action = new QAction(QIcon::fromTheme(service->icon()), service->caption(), this);
0069             QMenu *subMenu = new QMenu();
0070             makeMenu(subMenu, service);
0071             action->setMenu(subMenu);
0072             if (menu) {
0073                 menu->addAction(action);
0074             } else {
0075                 m_actions << action;
0076             }
0077         } else if (p->isType(KST_KServiceSeparator)) {
0078             if (menu) {
0079                 menu->addSeparator();
0080             }
0081         }
0082     }
0083 }
0084 
0085 QWidget *AppLauncher::createConfigurationInterface(QWidget *parent)
0086 {
0087     QWidget *widget = new QWidget(parent);
0088     m_ui.setupUi(widget);
0089     widget->setWindowTitle(i18nc("plasma_containmentactions_applauncher", "Configure Application Launcher Plugin"));
0090 
0091     m_ui.showAppsByName->setChecked(m_showAppsByName);
0092 
0093     return widget;
0094 }
0095 
0096 void AppLauncher::configurationAccepted()
0097 {
0098     m_showAppsByName = m_ui.showAppsByName->isChecked();
0099 }
0100 
0101 void AppLauncher::restore(const KConfigGroup &config)
0102 {
0103     m_showAppsByName = config.readEntry(QStringLiteral("showAppsByName"), true);
0104 }
0105 
0106 void AppLauncher::save(KConfigGroup &config)
0107 {
0108     config.writeEntry(QStringLiteral("showAppsByName"), m_showAppsByName);
0109 }
0110 
0111 K_PLUGIN_CLASS_WITH_JSON(AppLauncher, "plasma-containmentactions-applauncher.json")
0112 
0113 #include "launch.moc"