1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
/*
SPDX-FileCopyrightText: 2009 Chani Armitage <chani@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "launch.h"
#include <QDebug>
#include <KConfigGroup>
#include <KIO/ApplicationLauncherJob>
#include <KPluginFactory>
#include <Plasma/PluginLoader>
AppLauncher::AppLauncher(QObject *parent, const QVariantList &args)
: Plasma::ContainmentActions(parent, args)
, m_group(new KServiceGroup(QStringLiteral("/")))
{
}
AppLauncher::~AppLauncher()
{
}
void AppLauncher::init(const KConfigGroup &)
{
}
QList<QAction *> AppLauncher::contextualActions()
{
qDeleteAll(m_actions);
m_actions.clear();
makeMenu(nullptr, m_group);
return m_actions;
}
void AppLauncher::makeMenu(QMenu *menu, const KServiceGroup::Ptr &group)
{
const auto entries = group->entries(true, true, true);
for (const KSycocaEntry::Ptr &p : entries) {
if (p->isType(KST_KService)) {
const KService::Ptr service(static_cast<KService *>(p.data()));
QString text = service->name();
if (!m_showAppsByName && !service->genericName().isEmpty()) {
text = service->genericName();
}
QAction *action = new QAction(QIcon::fromTheme(service->icon()), text, this);
connect(action, &QAction::triggered, [action]() {
KService::Ptr service = KService::serviceByStorageId(action->data().toString());
auto job = new KIO::ApplicationLauncherJob(service);
job->start();
});
action->setData(service->storageId());
if (menu) {
menu->addAction(action);
} else {
m_actions << action;
}
} else if (p->isType(KST_KServiceGroup)) {
const KServiceGroup::Ptr service(static_cast<KServiceGroup *>(p.data()));
if (service->childCount() == 0) {
continue;
}
QAction *action = new QAction(QIcon::fromTheme(service->icon()), service->caption(), this);
QMenu *subMenu = new QMenu();
makeMenu(subMenu, service);
action->setMenu(subMenu);
if (menu) {
menu->addAction(action);
} else {
m_actions << action;
}
} else if (p->isType(KST_KServiceSeparator)) {
if (menu) {
menu->addSeparator();
}
}
}
}
QWidget *AppLauncher::createConfigurationInterface(QWidget *parent)
{
QWidget *widget = new QWidget(parent);
m_ui.setupUi(widget);
widget->setWindowTitle(i18nc("plasma_containmentactions_applauncher", "Configure Application Launcher Plugin"));
m_ui.showAppsByName->setChecked(m_showAppsByName);
return widget;
}
void AppLauncher::configurationAccepted()
{
m_showAppsByName = m_ui.showAppsByName->isChecked();
}
void AppLauncher::restore(const KConfigGroup &config)
{
m_showAppsByName = config.readEntry(QStringLiteral("showAppsByName"), false);
}
void AppLauncher::save(KConfigGroup &config)
{
config.writeEntry(QStringLiteral("showAppsByName"), m_showAppsByName);
}
K_PLUGIN_CLASS_WITH_JSON(AppLauncher, "plasma-containmentactions-applauncher.json")
#include "launch.moc"
|