2015-09-06 14:04:59 +00:00
|
|
|
#include "customdesktopmenu.h"
|
|
|
|
|
2024-03-06 21:05:01 +00:00
|
|
|
#include <KConfigGroup>
|
|
|
|
#include <KIO/ApplicationLauncherJob>
|
|
|
|
#include <KPluginFactory>
|
2015-09-06 14:04:59 +00:00
|
|
|
#include <KProcess>
|
2024-03-06 21:05:01 +00:00
|
|
|
#include <KDesktopFile>
|
2015-09-06 14:04:59 +00:00
|
|
|
|
|
|
|
CustomDesktopMenu::CustomDesktopMenu(QObject *parent, const QVariantList &args)
|
2024-03-06 21:05:01 +00:00
|
|
|
: Plasma::ContainmentActions(parent, args)
|
|
|
|
, m_group(new KServiceGroup(QStringLiteral("/")))
|
2015-09-06 14:04:59 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
CustomDesktopMenu::~CustomDesktopMenu()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-06 21:05:01 +00:00
|
|
|
void CustomDesktopMenu::init(const KConfigGroup &)
|
2015-09-06 14:04:59 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-06 21:05:01 +00:00
|
|
|
QList<QAction *> CustomDesktopMenu::contextualActions()
|
2015-09-06 14:04:59 +00:00
|
|
|
{
|
|
|
|
qDeleteAll(m_actions);
|
|
|
|
m_actions.clear();
|
2024-03-08 22:29:39 +00:00
|
|
|
|
|
|
|
parseConfig();
|
|
|
|
|
2015-09-06 14:04:59 +00:00
|
|
|
return m_actions;
|
|
|
|
}
|
|
|
|
|
2024-03-06 21:05:01 +00:00
|
|
|
QWidget *CustomDesktopMenu::createConfigurationInterface(QWidget *parent)
|
|
|
|
{
|
2024-03-08 22:29:39 +00:00
|
|
|
QWidget *widget = new QWidget(parent);
|
|
|
|
m_ui.setupUi(widget);
|
|
|
|
widget->setWindowTitle(i18nc("plasma_containmentactions_customdesktopmenu", "Configure Application Launcher Plugin"));
|
2024-03-09 14:14:05 +00:00
|
|
|
widget->setFixedWidth(660);
|
|
|
|
widget->setFixedHeight(400);
|
2024-03-08 22:29:39 +00:00
|
|
|
m_ui.configData->setPlainText(m_menuConfig);
|
|
|
|
m_ui.showAppsByName->setChecked(m_showAppsByName);
|
|
|
|
return widget;
|
2024-03-06 21:05:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CustomDesktopMenu::configurationAccepted()
|
|
|
|
{
|
2024-03-08 22:29:39 +00:00
|
|
|
m_menuConfig = m_ui.configData->document()->toPlainText();
|
|
|
|
m_showAppsByName = m_ui.showAppsByName->isChecked();
|
2024-03-06 21:05:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CustomDesktopMenu::restore(const KConfigGroup &config)
|
|
|
|
{
|
2024-03-08 22:29:39 +00:00
|
|
|
m_menuConfig = config.readEntry(QStringLiteral("menuConfig"), getDefaultConfig());
|
|
|
|
m_showAppsByName = config.readEntry(QStringLiteral("showAppsByName"), true);
|
2024-03-06 21:05:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CustomDesktopMenu::save(KConfigGroup &config)
|
|
|
|
{
|
2024-03-08 22:29:39 +00:00
|
|
|
config.writeEntry(QStringLiteral("menuConfig"), m_menuConfig);
|
|
|
|
config.writeEntry(QStringLiteral("showAppsByName"), m_showAppsByName);
|
2024-03-06 21:05:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 22:29:39 +00:00
|
|
|
QString CustomDesktopMenu::getDefaultConfig()
|
2015-09-06 14:04:59 +00:00
|
|
|
{
|
|
|
|
QString defMenuConfig = "{favorites}\n";
|
|
|
|
defMenuConfig += "-\n";
|
|
|
|
defMenuConfig += "[menu]\tApplications\tkde\n";
|
|
|
|
defMenuConfig += "{programs}\n";
|
|
|
|
defMenuConfig += "[end]\n";
|
|
|
|
defMenuConfig += "-\n";
|
2024-03-08 23:16:20 +00:00
|
|
|
defMenuConfig += "/usr/share/applications/org.kde.dolphin.desktop\n";
|
|
|
|
defMenuConfig += "/usr/share/applications/org.kde.kate.desktop\n";
|
|
|
|
defMenuConfig += "#/usr/share/applications/org.kde.kcalc.desktop\n";
|
2015-09-06 14:04:59 +00:00
|
|
|
defMenuConfig += "-\n";
|
|
|
|
defMenuConfig += "[menu]\tSystem\tconfigure-shortcuts\n";
|
|
|
|
defMenuConfig += "{programs}\tSettingsmenu/\n";
|
2024-03-08 23:16:20 +00:00
|
|
|
defMenuConfig += "-\n";
|
2015-09-06 14:04:59 +00:00
|
|
|
defMenuConfig += "{programs}\tSystem/\n";
|
|
|
|
defMenuConfig += "[end]\n";
|
|
|
|
defMenuConfig += "[menu]\tExit\tsystem-shutdown\n";
|
2024-03-06 21:05:01 +00:00
|
|
|
defMenuConfig += "Lock\tsystem-lock-screen\tqdbus6 org.kde.KWin /ScreenSaver Lock\n";
|
|
|
|
defMenuConfig += "Disconnect\tsystem-log-out\tqdbus6 org.kde.LogoutPrompt /LogoutPrompt promptLogout\n";
|
|
|
|
defMenuConfig += "Switch User\tsystem-switch-user\tqdbus6 org.kde.KWin /ScreenSaver org.kde.screensaver.SwitchUser\n";
|
2015-09-06 14:04:59 +00:00
|
|
|
defMenuConfig += "-\n";
|
2024-03-06 21:05:01 +00:00
|
|
|
defMenuConfig += "Sleep\tsystem-suspend\tqdbus6 org.freedesktop.PowerManagement /org/freedesktop/PowerManagement Suspend\n";
|
|
|
|
defMenuConfig += "Hibernate\tsystem-suspend-hibernate\tqdbus6 org.freedesktop.PowerManagement /org/freedesktop/PowerManagement Hibernate\n";
|
2015-09-06 14:04:59 +00:00
|
|
|
defMenuConfig += "-\n";
|
2024-03-06 21:05:01 +00:00
|
|
|
defMenuConfig += "Restart\tsystem-reboot\tqdbus6 org.kde.LogoutPrompt /LogoutPrompt promptReboot\n";
|
|
|
|
defMenuConfig += "Shut down\tsystem-shutdown\tqdbus6 org.kde.LogoutPrompt /LogoutPrompt promptShutDown\n";
|
2015-09-06 14:04:59 +00:00
|
|
|
defMenuConfig += "[end]\n";
|
|
|
|
defMenuConfig += "-\n";
|
2024-03-06 21:05:01 +00:00
|
|
|
defMenuConfig += "/usr/share/applications/org.kde.plasma-systemmonitor.desktop\n";
|
2015-09-06 14:04:59 +00:00
|
|
|
defMenuConfig += "/usr/share/applications/org.kde.konsole.desktop\n";
|
|
|
|
return defMenuConfig;
|
|
|
|
}
|
|
|
|
|
2024-03-08 22:29:39 +00:00
|
|
|
void CustomDesktopMenu::parseConfig()
|
|
|
|
{
|
|
|
|
if (m_menuConfig.isEmpty()) return;
|
|
|
|
QStringList configLines = m_menuConfig.split("\n", Qt::SkipEmptyParts);
|
|
|
|
foreach(QString cfgLine, configLines) {
|
|
|
|
if (cfgLine.startsWith("#")) continue;
|
|
|
|
QAction *action = nullptr;
|
|
|
|
if (cfgLine.startsWith("-")) {
|
|
|
|
// Separator
|
|
|
|
action = new QAction(this);
|
|
|
|
action->setSeparator(true);
|
|
|
|
} else if (cfgLine.startsWith("[menu]")) {
|
|
|
|
// Begin menu
|
|
|
|
QStringList cfgParts = cfgLine.split("\t", Qt::SkipEmptyParts);
|
|
|
|
QString text = "";
|
|
|
|
QIcon icon;
|
|
|
|
if (cfgParts.size() > 1) text = cfgParts[1].replace("&", "&&");
|
|
|
|
if (cfgParts.size() > 2) icon = QIcon::fromTheme(cfgParts[2]);
|
|
|
|
QMenu *subMenu = new QMenu();
|
|
|
|
action = new QAction(icon, text, this);
|
|
|
|
action->setMenu(subMenu);
|
|
|
|
addAction(action);
|
|
|
|
m_menuList.append(subMenu);
|
|
|
|
action = nullptr;
|
|
|
|
} else if (cfgLine.startsWith("[end]")) {
|
|
|
|
// End menu
|
|
|
|
m_menuList.removeLast();
|
|
|
|
} else if (cfgLine.endsWith(".desktop")) {
|
|
|
|
// .desktop file
|
|
|
|
if(KDesktopFile::isDesktopFile(cfgLine) == true) {
|
|
|
|
KDesktopFile desktopFile(cfgLine);
|
2024-03-09 14:25:16 +00:00
|
|
|
QString text = desktopFile.readName();
|
2024-03-09 14:05:29 +00:00
|
|
|
if (!m_showAppsByName && !desktopFile.readGenericName().isEmpty()) {
|
|
|
|
text = desktopFile.readGenericName();
|
|
|
|
}
|
|
|
|
action = new QAction(QIcon::fromTheme(desktopFile.readIcon()), text, this);
|
2024-03-08 22:29:39 +00:00
|
|
|
action->setData(cfgLine);
|
|
|
|
connect(action, &QAction::triggered, [action](){
|
|
|
|
KService::Ptr service = KService::serviceByDesktopPath(action->data().toString());
|
|
|
|
auto job = new KIO::ApplicationLauncherJob(service);
|
|
|
|
job->start();
|
|
|
|
});
|
2015-09-06 14:04:59 +00:00
|
|
|
} else {
|
2024-03-08 22:29:39 +00:00
|
|
|
action = new QAction(cfgLine, this);
|
|
|
|
}
|
|
|
|
} else if (cfgLine.startsWith("{programs}")) {
|
|
|
|
// programs
|
|
|
|
QStringList cfgParts = cfgLine.split("\t", Qt::SkipEmptyParts);
|
|
|
|
QString path = "/";
|
|
|
|
if (cfgParts.size() > 1) path = cfgParts[1];
|
|
|
|
fillPrograms(path);
|
|
|
|
} else if (cfgLine == "{favorites}") {
|
|
|
|
// favorites
|
|
|
|
// Not implemented
|
|
|
|
} else {
|
|
|
|
// cmd
|
|
|
|
QStringList cfgParts = cfgLine.split("\t", Qt::SkipEmptyParts);
|
|
|
|
QString text = "";
|
|
|
|
QIcon icon;
|
|
|
|
QString cmd = "";
|
|
|
|
text = cfgParts[0].replace("&", "&&");
|
|
|
|
if (cfgParts.size() > 1) icon = QIcon::fromTheme(cfgParts[1]);
|
|
|
|
action = new QAction(icon, text, this);
|
|
|
|
if (cfgParts.size() > 2) {
|
|
|
|
action->setData(cfgParts[2]);
|
|
|
|
connect(action, &QAction::triggered, [action](){
|
|
|
|
KProcess *process = new KProcess(0);
|
|
|
|
process->startDetached(action->data().toString().split(" "));
|
|
|
|
});
|
2015-09-06 14:04:59 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-08 22:29:39 +00:00
|
|
|
addAction(action);
|
2015-09-06 14:04:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-08 22:29:39 +00:00
|
|
|
void CustomDesktopMenu::addAction(QAction* action)
|
2015-09-06 14:04:59 +00:00
|
|
|
{
|
2024-03-08 22:29:39 +00:00
|
|
|
if (action) {
|
|
|
|
if (m_menuList.size() == 0) m_actions << action;
|
|
|
|
else m_menuList.last()->addAction(action);
|
2015-09-06 14:04:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-08 22:29:39 +00:00
|
|
|
void CustomDesktopMenu::fillPrograms(const QString& path)
|
2015-09-06 14:04:59 +00:00
|
|
|
{
|
|
|
|
KServiceGroup::Ptr root = KServiceGroup::group(path);
|
|
|
|
KServiceGroup::List list = root->entries(true, true, true);
|
2024-03-06 21:05:01 +00:00
|
|
|
for (const KSycocaEntry::Ptr &p : list){
|
2024-03-08 22:29:39 +00:00
|
|
|
QAction *action = nullptr;
|
2024-03-06 21:05:01 +00:00
|
|
|
if(p->isType(KST_KService)) {
|
2024-03-08 22:29:39 +00:00
|
|
|
if(KDesktopFile::isDesktopFile(p->entryPath()) == true) {
|
|
|
|
KDesktopFile desktopFile(p->entryPath());
|
2024-03-09 14:05:29 +00:00
|
|
|
QString text = p->name();
|
|
|
|
if (!m_showAppsByName && !desktopFile.readGenericName().isEmpty()) {
|
|
|
|
text = desktopFile.readGenericName();
|
|
|
|
}
|
|
|
|
action = new QAction(QIcon::fromTheme(desktopFile.readIcon()), text, this);
|
2024-03-08 22:29:39 +00:00
|
|
|
action->setData(p->entryPath());
|
|
|
|
connect(action, &QAction::triggered, [action](){
|
|
|
|
KService::Ptr service = KService::serviceByDesktopPath(action->data().toString());
|
|
|
|
auto job = new KIO::ApplicationLauncherJob(service);
|
|
|
|
job->start();
|
|
|
|
});
|
|
|
|
addAction(action);
|
|
|
|
} else {
|
|
|
|
action = new QAction(p->entryPath(), this);
|
|
|
|
addAction(action);
|
|
|
|
}
|
2024-03-06 21:05:01 +00:00
|
|
|
} else if(p->isType(KST_KServiceGroup)) {
|
|
|
|
const KServiceGroup::Ptr service(static_cast<KServiceGroup *>(p.data()));
|
2024-03-08 22:29:39 +00:00
|
|
|
if(service->childCount() == 0) continue;
|
|
|
|
QString text = service->caption();
|
|
|
|
QIcon icon = QIcon::fromTheme(service->icon());
|
|
|
|
QMenu *subMenu = new QMenu();
|
|
|
|
action = new QAction(icon, text, this);
|
|
|
|
action->setMenu(subMenu);
|
|
|
|
addAction(action);
|
|
|
|
m_menuList.append(subMenu);
|
|
|
|
fillPrograms(p->name());
|
|
|
|
m_menuList.removeLast();
|
2024-03-06 21:05:01 +00:00
|
|
|
} else if(p->isType(KST_KServiceSeparator)) {
|
2024-03-08 22:29:39 +00:00
|
|
|
action = new QAction(this);
|
|
|
|
action->setSeparator(true);
|
|
|
|
addAction(action);
|
2015-09-06 14:04:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-06 21:05:01 +00:00
|
|
|
K_PLUGIN_CLASS_WITH_JSON(CustomDesktopMenu, "plasma-containmentactions-customdesktopmenu.json")
|
|
|
|
|
2015-09-06 14:04:59 +00:00
|
|
|
#include "customdesktopmenu.moc"
|