1
0
This commit is contained in:
MatMoul 2024-03-08 23:29:39 +01:00
parent afb927d5fe
commit 7bfdaa70b5
4 changed files with 211 additions and 258 deletions

77
src/CMakeLists-old.txt Normal file
View File

@ -0,0 +1,77 @@
cmake_minimum_required(VERSION 3.16)
project(plasma-containmentactions-customdesktopmenu)
set(PROJECT_VERSION "6.0.80")
string(REPLACE "." ";" VERSION_LIST ${PROJECT_VERSION})
list(GET VERSION_LIST 0 PROJECT_VERSION_MAJOR)
list(GET VERSION_LIST 1 PROJECT_VERSION_MINOR)
list(GET VERSION_LIST 2 PROJECT_VERSION_PATCH)
set(PROJECT_DEP_VERSION "6.0.0")
set(QT_MIN_VERSION "6.6.0")
set(KF6_MIN_VERSION "6.0.0")
set(KDE_COMPILERSETTINGS_LEVEL "5.82")
set(INSTALL_SDDM_THEME TRUE)
option(PLASMA_X11_DEFAULT_SESSION "Use X11 session by default for Plasma" OFF)
option(INSTALL_SDDM_WAYLAND_SESSION OFF)
option(WITH_X11 "Build with X11 support. Building without is experimental" ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(ECM ${KF6_MIN_VERSION} REQUIRED NO_MODULE)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(KDEInstallDirs)
include(KDECMakeSettings)
include(KDECompilerSettings NO_POLICY_SCOPE)
#include(ECMMarkNonGuiExecutable)
#include(CMakePackageConfigHelpers)
#include(WriteBasicConfigVersionFile)
#include(CheckIncludeFiles)
#include(FeatureSummary)
#include(ECMOptionalAddSubdirectory)
#include(ECMQtDeclareLoggingCategory)
#include(ECMQueryQt)
#include(ECMInstallIcons)
#include(KDEClangFormat)
#include(KDEGitCommitHooks)
#include(ECMConfiguredInstall)
#include(ECMGenerateDBusServiceFile)
#include(ECMFindQmlModule)
#include(ECMGenerateExportHeader)
#include(ECMGenerateQmlTypes)
#include(ECMDeprecationSettings)
#include(ECMQmlModule)
find_package(Qt6 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS Svg Widgets Quick QuickWidgets Concurrent Network Core5Compat DBus ShaderTools)
find_package(KF6 ${KF6_MIN_VERSION} REQUIRED COMPONENTS
Auth Parts Runner Notifications NotifyConfig NewStuff Wallet IdleTime
Svg Declarative I18n KCMUtils TextWidgets Crash GlobalAccel DBusAddons
CoreAddons KIO Prison Package
GuiAddons Archive ItemModels IconThemes UnitConversion TextEditor StatusNotifierItem
OPTIONAL_COMPONENTS DocTools)
#find_package(Plasma5Support ${PROJECT_DEP_VERSION} REQUIRED)
find_package(Plasma ${PROJECT_DEP_VERSION} REQUIRED)
#find_package(PlasmaQuick ${PROJECT_DEP_VERSION} REQUIRED)
#find_package(KWayland ${PROJECT_DEP_VERSION} REQUIRED)
#find_package(PlasmaActivities ${PROJECT_DEP_VERSION} REQUIRED)
#find_package(PlasmaActivitiesStats ${PROJECT_DEP_VERSION} REQUIRED)
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
#add_subdirectory(containmentactions)
add_definitions(-DTRANSLATION_DOMAIN=\"plasma_containmentactions_customdesktopmenu\")
set(customdesktopmenu_SRCS
customdesktopmenu.cpp
)
ki18n_wrap_ui(customdesktopmenu_SRCS config.ui)
kcoreaddons_add_plugin(plasma_containmentactions_customdesktopmenu SOURCES ${customdesktopmenu_SRCS} INSTALL_NAMESPACE "plasma/containmentactions")
set_target_properties(plasma_containmentactions_customdesktopmenu PROPERTIES
OUTPUT_NAME customdesktopmenu
)
target_link_libraries(plasma_containmentactions_customdesktopmenu Plasma::Plasma KF6::KIOCore KF6::KIOWidgets KF6::I18n)

View File

@ -1,17 +1,8 @@
/*
SPDX-FileCopyrightText: 2009 Chani Armitage <chani@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "customdesktopmenu.h" #include "customdesktopmenu.h"
#include <QDebug>
#include <KConfigGroup> #include <KConfigGroup>
#include <KIO/ApplicationLauncherJob> #include <KIO/ApplicationLauncherJob>
#include <KPluginFactory> #include <KPluginFactory>
#include <Plasma/PluginLoader>
#include <KProcess> #include <KProcess>
#include <KDesktopFile> #include <KDesktopFile>
@ -20,7 +11,6 @@ CustomDesktopMenu::CustomDesktopMenu(QObject *parent, const QVariantList &args)
, m_group(new KServiceGroup(QStringLiteral("/"))) , m_group(new KServiceGroup(QStringLiteral("/")))
{ {
} }
CustomDesktopMenu::~CustomDesktopMenu() CustomDesktopMenu::~CustomDesktopMenu()
{ {
} }
@ -34,136 +24,40 @@ QList<QAction *> CustomDesktopMenu::contextualActions()
qDeleteAll(m_actions); qDeleteAll(m_actions);
m_actions.clear(); m_actions.clear();
if (!menuconfig.isEmpty()) { parseConfig();
QList<QMenu*> menuList;
menuList.append(0);
QMenu* curMenu = 0;
QStringList configLines = menuconfig.split( "\n", Qt::SkipEmptyParts );
foreach( QString cfgLine, configLines ) {
if (!cfgLine.startsWith("#")) {
if (cfgLine.startsWith("-")) {
addSep(curMenu);
} else if (cfgLine.endsWith(".desktop")) {
addApp(curMenu, cfgLine);
} else if (cfgLine.startsWith("[menu]")) {
QStringList cfgParts = cfgLine.split( "\t", Qt::SkipEmptyParts );
if (cfgParts.size() == 3) {
curMenu = addMnu(curMenu, cfgParts[2], cfgParts[1]);
menuList.append(curMenu);
} else if (cfgParts.size() == 2) {
curMenu = addMnu(curMenu, "", cfgParts[1]);
menuList.append(curMenu);
}
} else if (cfgLine.startsWith("[end]")) {
menuList.removeLast();
curMenu = menuList.last();
} else if (cfgLine == "{favorites}") {
fillFavorites(curMenu);
} else if (cfgLine.startsWith("{programs}")) {
QStringList cfgParts = cfgLine.split( "\t", Qt::SkipEmptyParts );
if (cfgParts.size() == 2) {
fillPrograms(curMenu, cfgParts[1]);
} else {
fillPrograms(curMenu, "/");
}
} else {
QStringList cfgParts = cfgLine.split( "\t", Qt::SkipEmptyParts );
if (cfgParts.size() == 3) {
addCmd(curMenu, cfgParts[1], cfgParts[0], cfgParts[2]);
} else if (cfgParts.size() == 2) {
addCmd(curMenu, "", cfgParts[0], cfgParts[1]);
} else {
addItm(curMenu, "", cfgParts[0]);
}
}
}
}
}
return m_actions; return m_actions;
} }
/*
void CustomDesktopMenu::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 *CustomDesktopMenu::createConfigurationInterface(QWidget *parent) QWidget *CustomDesktopMenu::createConfigurationInterface(QWidget *parent)
{ {
QWidget *widget = new QWidget(parent); QWidget *widget = new QWidget(parent);
m_ui.setupUi(widget); m_ui.setupUi(widget);
widget->setWindowTitle(i18nc("plasma_containmentactions_customdesktopmenu", "Configure Application Launcher Plugin")); widget->setWindowTitle(i18nc("plasma_containmentactions_customdesktopmenu", "Configure Application Launcher Plugin"));
m_ui.configData->setPlainText(m_menuConfig);
m_ui.showAppsByName->setChecked(m_showAppsByName); m_ui.showAppsByName->setChecked(m_showAppsByName);
m_ui.configData->setPlainText(menuconfig);
return widget; return widget;
} }
void CustomDesktopMenu::configurationAccepted() void CustomDesktopMenu::configurationAccepted()
{ {
m_menuConfig = m_ui.configData->document()->toPlainText();
m_showAppsByName = m_ui.showAppsByName->isChecked(); m_showAppsByName = m_ui.showAppsByName->isChecked();
menuconfig = m_ui.configData->document()->toPlainText();
} }
void CustomDesktopMenu::restore(const KConfigGroup &config) void CustomDesktopMenu::restore(const KConfigGroup &config)
{ {
m_menuConfig = config.readEntry(QStringLiteral("menuConfig"), getDefaultConfig());
m_showAppsByName = config.readEntry(QStringLiteral("showAppsByName"), true); m_showAppsByName = config.readEntry(QStringLiteral("showAppsByName"), true);
menuconfig = config.readEntry(QStringLiteral("menuConfig"), getDefaultMenu());
} }
void CustomDesktopMenu::save(KConfigGroup &config) void CustomDesktopMenu::save(KConfigGroup &config)
{ {
config.writeEntry(QStringLiteral("menuConfig"), m_menuConfig);
config.writeEntry(QStringLiteral("showAppsByName"), m_showAppsByName); config.writeEntry(QStringLiteral("showAppsByName"), m_showAppsByName);
config.writeEntry(QStringLiteral("menuConfig"), menuconfig);
} }
QString CustomDesktopMenu::getDefaultConfig()
QString CustomDesktopMenu::getDefaultMenu()
{ {
QString defMenuConfig = "{favorites}\n"; QString defMenuConfig = "{favorites}\n";
defMenuConfig += "-\n"; defMenuConfig += "-\n";
@ -194,123 +88,126 @@ QString CustomDesktopMenu::getDefaultMenu()
return defMenuConfig; return defMenuConfig;
} }
QIcon CustomDesktopMenu::getIcon(const QString &txt) void CustomDesktopMenu::parseConfig()
{ {
QIcon icon = QIcon::fromTheme(txt); if (m_menuConfig.isEmpty()) return;
return icon; QStringList configLines = m_menuConfig.split("\n", Qt::SkipEmptyParts);
} foreach(QString cfgLine, configLines) {
if (cfgLine.startsWith("#")) continue;
void CustomDesktopMenu::addSep(QMenu *menu) QAction *action = nullptr;
{ if (cfgLine.startsWith("-")) {
QAction *action = new QAction(this); // Separator
action = new QAction(this);
action->setSeparator(true); action->setSeparator(true);
if (menu) { } else if (cfgLine.startsWith("[menu]")) {
menu->addAction(action); // Begin menu
} else { QStringList cfgParts = cfgLine.split("\t", Qt::SkipEmptyParts);
m_actions << action; QString text = "";
} QIcon icon;
} if (cfgParts.size() > 1) text = cfgParts[1].replace("&", "&&");
if (cfgParts.size() > 2) icon = QIcon::fromTheme(cfgParts[2]);
void CustomDesktopMenu::addItm(QMenu *menu, const QString &icon, const QString &txt) QMenu *subMenu = new QMenu();
{ action = new QAction(icon, text, this);
QString text = txt; action->setMenu(subMenu);
text.replace("&", "&&"); //escaping addAction(action);
QAction *action; m_menuList.append(subMenu);
action = new QAction(getIcon(icon), text, this); action = nullptr;
if (menu) { } else if (cfgLine.startsWith("[end]")) {
menu->addAction(action); // End menu
} else { m_menuList.removeLast();
m_actions << action; } else if (cfgLine.endsWith(".desktop")) {
} // .desktop file
} if(KDesktopFile::isDesktopFile(cfgLine) == true) {
KDesktopFile desktopFile(cfgLine);
void CustomDesktopMenu::addCmd(QMenu *menu, const QString &icon, const QString &txt, const QString &cmd) action = new QAction(QIcon::fromTheme(desktopFile.readIcon()), desktopFile.readName(), this);
{ action->setData(cfgLine);
QString text = txt;
text.replace("&", "&&"); //escaping
QAction *action;
action = new QAction(getIcon(icon), text, this);
action->setData(cmd);
connect(action, &QAction::triggered, [action](){ connect(action, &QAction::triggered, [action](){
QString source = action->data().toString();
if (!source.isEmpty()) {
if (source.endsWith(".desktop")) {
KService::Ptr service = KService::serviceByDesktopPath(action->data().toString()); KService::Ptr service = KService::serviceByDesktopPath(action->data().toString());
auto job = new KIO::ApplicationLauncherJob(service); auto job = new KIO::ApplicationLauncherJob(service);
job->start(); job->start();
} else {
QStringList cmd = source.split(" ");
KProcess *process = new KProcess(0);
process->startDetached(cmd);
}
}
}); });
if (menu) {
menu->addAction(action);
} else { } else {
m_actions << action; 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(" "));
});
}
}
addAction(action);
} }
} }
QMenu* CustomDesktopMenu::addMnu(QMenu *menu, const QString &icon, const QString &txt) void CustomDesktopMenu::addAction(QAction* action)
{ {
QString text = txt; if (action) {
text.replace("&", "&&"); //escaping if (m_menuList.size() == 0) m_actions << action;
QAction *action; else m_menuList.last()->addAction(action);
action = new QAction(getIcon(icon), text, this);
QMenu *subMenu = new QMenu();
action->setMenu(subMenu);
if (menu) {
menu->addAction(action);
} else {
m_actions << action;
}
return subMenu;
}
void CustomDesktopMenu::addApp(QMenu *menu, const QString &path)
{
if(KDesktopFile::isDesktopFile(path) == true) {
KDesktopFile dskFile(path);
addCmd(menu, dskFile.readIcon(), dskFile.readName(), path);
} else {
addCmd(menu, "", path, path);
} }
} }
void CustomDesktopMenu::fillPrograms(QMenu *menu, const QString &path) void CustomDesktopMenu::fillPrograms(const QString& path)
{ {
KServiceGroup::Ptr root = KServiceGroup::group(path); KServiceGroup::Ptr root = KServiceGroup::group(path);
KServiceGroup::List list = root->entries(true, true, true); KServiceGroup::List list = root->entries(true, true, true);
for (const KSycocaEntry::Ptr &p : list){ for (const KSycocaEntry::Ptr &p : list){
QAction *action = nullptr;
if(p->isType(KST_KService)) { if(p->isType(KST_KService)) {
addApp(menu, p->entryPath()); if(KDesktopFile::isDesktopFile(p->entryPath()) == true) {
KDesktopFile desktopFile(p->entryPath());
action = new QAction(QIcon::fromTheme(desktopFile.readIcon()), desktopFile.readName(), this);
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);
}
} else if(p->isType(KST_KServiceGroup)) { } else if(p->isType(KST_KServiceGroup)) {
const KServiceGroup::Ptr service(static_cast<KServiceGroup *>(p.data())); const KServiceGroup::Ptr service(static_cast<KServiceGroup *>(p.data()));
if(service->childCount() == 0) { if(service->childCount() == 0) continue;
continue; QString text = service->caption();
} QIcon icon = QIcon::fromTheme(service->icon());
QMenu* menu2 = addMnu(menu, service->icon(), service->caption()); QMenu *subMenu = new QMenu();
fillPrograms(menu2, p->name()); action = new QAction(icon, text, this);
action->setMenu(subMenu);
addAction(action);
m_menuList.append(subMenu);
fillPrograms(p->name());
m_menuList.removeLast();
} else if(p->isType(KST_KServiceSeparator)) { } else if(p->isType(KST_KServiceSeparator)) {
menu->addSeparator(); action = new QAction(this);
action->setSeparator(true);
addAction(action);
} }
} }
} }
void CustomDesktopMenu::fillFavorites(QMenu *menu)
{
/*
KConfig config("kickoffrc");
KConfigGroup favoritesGroup = config.group("Favorites");
QList<QString> favoriteList = favoritesGroup.readEntry("FavoriteURLs", QList<QString>());
foreach (const QString &source, favoriteList) {
addApp(menu, source);
}
*/
}
K_PLUGIN_CLASS_WITH_JSON(CustomDesktopMenu, "plasma-containmentactions-customdesktopmenu.json") K_PLUGIN_CLASS_WITH_JSON(CustomDesktopMenu, "plasma-containmentactions-customdesktopmenu.json")

View File

@ -1,17 +1,8 @@
/*
SPDX-FileCopyrightText: 2009 Chani Armitage <chani@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#pragma once #pragma once
#include <QMenu> #include <QMenu>
#include <KServiceGroup> #include <KServiceGroup>
#include <plasma/containmentactions.h> #include <plasma/containmentactions.h>
#include "ui_config.h" #include "ui_config.h"
class QAction; class QAction;
@ -22,36 +13,24 @@ class CustomDesktopMenu : public Plasma::ContainmentActions
public: public:
CustomDesktopMenu(QObject *parent, const QVariantList &args); CustomDesktopMenu(QObject *parent, const QVariantList &args);
~CustomDesktopMenu() override; ~CustomDesktopMenu() override;
void init(const KConfigGroup &config); void init(const KConfigGroup &config);
QList<QAction *> contextualActions() override; QList<QAction *> contextualActions() override;
QWidget *createConfigurationInterface(QWidget *parent) override; QWidget *createConfigurationInterface(QWidget *parent) override;
void configurationAccepted() override; void configurationAccepted() override;
void restore(const KConfigGroup &config) override; void restore(const KConfigGroup &config) override;
void save(KConfigGroup &config) override; void save(KConfigGroup &config) override;
protected:
// void makeMenu(QMenu *menu, const KServiceGroup::Ptr &group);
private: private:
KServiceGroup::Ptr m_group; KServiceGroup::Ptr m_group;
QList<QAction *> m_actions; QList<QAction *> m_actions;
QList<QMenu *> m_menuList;
Ui::Config m_ui; Ui::Config m_ui;
QString m_menuConfig;
bool m_showAppsByName = true; bool m_showAppsByName = true;
QString menuconfig; QString getDefaultConfig();
QString getDefaultMenu(); void parseConfig();
// QTextEdit *configtextbox; void addAction(QAction *action);
QIcon getIcon(const QString &txt); void fillPrograms(const QString &path);
void addSep(QMenu *menu);
void addItm(QMenu *menu, const QString &icon, const QString &txt);
void addCmd(QMenu *menu, const QString &icon, const QString &txt, const QString &cmd);
QMenu* addMnu(QMenu *menu, const QString &icon, const QString &txt);
void addApp(QMenu *menu, const QString &path);
void fillPrograms(QMenu *menu, const QString &path);
void fillFavorites(QMenu *menu);
}; };