From f9073820ab5d75ec787c956550ac2cb35e0536a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Bidar?= Date: Sat, 6 Apr 2024 22:37:29 +0300 Subject: [PATCH] Also support Kf6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Björn Bidar --- CMakeLists.txt | 105 +++++++++++++++++++++++++++++++------------------ pass.cpp | 69 +++++++++++++++++++++++++------- pass.h | 12 ++++++ 3 files changed, 133 insertions(+), 53 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ce0ea8d..3ef7368 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,26 +7,6 @@ set (CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR} ${CMAKE_MODULE_PATH} ) -find_package (Qt5 ${QT_MIN_VERSION} - REQUIRED CONFIG COMPONENTS - Widgets - Core - Quick - DBus -) -find_package (KF5 ${KF5_MIN_VERSION} - REQUIRED COMPONENTS - I18n - Service - Runner - TextWidgets - ConfigWidgets - PlasmaQuick - Notifications - Auth - KCMUtils - GuiAddons -) include(KDEInstallDirs) include(KDECMakeSettings) include(KDECompilerSettings NO_POLICY_SCOPE) @@ -47,6 +27,45 @@ set(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/postinst") INCLUDE(CPack) +if (QT_MAJOR_VERSION STREQUAL "6") + set(QT_MIN_VERSION "6.4.0") + set(KF_MIN_VERSION "5.240.0") + set(KF_MAJOR_VERSION "6") +else() + set(KF_MAJOR_VERSION "5") +endif() + +find_package (Qt${QT_MAJOR_VERSION} ${QT_MIN_VERSION} + REQUIRED CONFIG COMPONENTS + Widgets + Core + Quick + DBus +) +find_package (KF${KF_MAJOR_VERSION} + REQUIRED COMPONENTS + I18n + Service + Runner + TextWidgets + ConfigWidgets + Notifications + Auth + KCMUtils + GuiAddons +) + +if(KF_MAJOR_VERSION STREQUAL 5) + find_package(KF${KF_MAJOR_VERSION} + REQUIRED COMPONENTS + PlasmaQuick + Plasma + ) +else() + find_package(PlasmaQuick REQUIRED) + find_package(Plasma) +endif() + set(krunner_pass_SRCS pass.cpp ) @@ -58,30 +77,38 @@ set(kcm_krunner_pass_SRCS ki18n_wrap_ui(kcm_krunner_pass_SRCS config.ui) add_library(kcm_krunner_pass MODULE ${kcm_krunner_pass_SRCS}) target_link_libraries(kcm_krunner_pass - Qt5::Core - Qt5::Gui - KF5::CoreAddons - KF5::ConfigCore - KF5::I18n - KF5::ConfigWidgets - KF5::Runner - KF5::KCMUtils + Qt${QT_MAJOR_VERSION}::Core + Qt${QT_MAJOR_VERSION}::Gui + KF${KF_MAJOR_VERSION}::CoreAddons + KF${KF_MAJOR_VERSION}::ConfigCore + KF${KF_MAJOR_VERSION}::ConfigWidgets + KF${KF_MAJOR_VERSION}::I18n + KF${KF_MAJOR_VERSION}::Runner + KF${KF_MAJOR_VERSION}::KCMUtils ) add_library(krunner_pass MODULE ${krunner_pass_SRCS}) -target_link_libraries(krunner_pass KF5::Runner Qt5::Widgets - KF5::I18n - KF5::Service - KF5::Plasma - KF5::ConfigWidgets - KF5::Notifications - KF5::GuiAddons) +target_link_libraries(krunner_pass KF${KF_MAJOR_VERSION}::Runner Qt${QT_MAJOR_VERSION}::Widgets + KF${KF_MAJOR_VERSION}::I18n + KF${KF_MAJOR_VERSION}::Service + KF${KF_MAJOR_VERSION}::ConfigWidgets + KF${KF_MAJOR_VERSION}::Notifications + KF${KF_MAJOR_VERSION}::KCMUtils + KF${KF_MAJOR_VERSION}::GuiAddons) +if(KF_MAJOR_VERSION STREQUAL 5) + target_link_libraries(krunner_pass + KF${KF_MAJOR_VERSION}::Plasma + ) +else() + target_link_libraries(krunner_pass + Plasma::Plasma + ) +endif() add_dependencies(krunner_pass kcm_krunner_pass) -install(TARGETS kcm_krunner_pass DESTINATION ${KDE_INSTALL_QTPLUGINDIR}/kf5/krunner/kcms) -install(TARGETS krunner_pass DESTINATION ${KDE_INSTALL_QTPLUGINDIR}/kf5/krunner) -install(FILES plasma-runner-pass_config.desktop DESTINATION ${SERVICES_INSTALL_DIR}) -install(FILES krunner_pass.notifyrc DESTINATION ${KNOTIFYRC_INSTALL_DIR}) +install(TARGETS kcm_krunner_pass DESTINATION ${KDE_INSTALL_QTPLUGINDIR}/kf${KF_MAJOR_VERSION}/krunner/kcms) +install(TARGETS krunner_pass DESTINATION ${KDE_INSTALL_QTPLUGINDIR}/kf${KF_MAJOR_VERSION}/krunner) +install(FILES krunner_pass.notifyrc DESTINATION ${KDE_INSTALL_KNOTIFYRCDIR}) feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/pass.cpp b/pass.cpp index cfab84b..bc8d13a 100644 --- a/pass.cpp +++ b/pass.cpp @@ -36,16 +36,28 @@ #include "pass.h" #include "config.h" +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +#include +#endif + using namespace std; K_PLUGIN_CLASS_WITH_JSON(Pass, "pass.json") Pass::Pass(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args) - : Plasma::AbstractRunner(parent, metaData, args) +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + : KRunner::AbstractRunner(metaData, parent) +#else + : KRunner::AbstractRunner(parent, metaData) +#endif { + Q_UNUSED(args) + // General runner configuration setObjectName(QStringLiteral("Pass")); +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) setPriority(HighestPriority); +#endif } Pass::~Pass() = default; @@ -58,6 +70,9 @@ void Pass::reloadConfiguration() KConfigGroup cfg = config(); cfg.config()->reparseConfiguration(); // Just to be sure this->showActions = cfg.readEntry(Config::showActions, false); +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + uint32_t actionIdCounter = 0; +#endif if (showActions) { const auto configActions = cfg.group(Config::Group::Actions); @@ -67,10 +82,16 @@ void Pass::reloadConfiguration() // FIXME how to fallback? auto passAction = PassAction::fromConfig(group); +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) auto icon = QIcon::fromTheme(passAction.icon, QIcon::fromTheme("object-unlocked")); //KRunner::Action(actionId, QStringLiteral("process-stop"), i18n("Send SIGKILL")), auto *act = new QAction(icon, passAction.name, this); act->setData(passAction.regex); +#else + actionIdCounter++; + auto *act = new KRunner::Action(passAction.regex, QIcon::hasThemeIcon(passAction.icon) ? + passAction.icon : QStringLiteral("object-unlocked"), passAction.name); +#endif this->orderedActions << act; } @@ -79,16 +100,20 @@ void Pass::reloadConfiguration() } if (cfg.readEntry(Config::showFileContentAction, false)) { +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) auto *act = new QAction(QIcon::fromTheme("document-new"), i18n("Show password file contents"), this); act->setData(Config::showFileContentAction); +#else + auto *act = new KRunner::Action(Config::showFileContentAction, "document-new", i18n("Show password file contents")); +#endif this->orderedActions << act; } - addSyntax(Plasma::RunnerSyntax(QString(":q:"), + addSyntax(KRunner::RunnerSyntax(QString(":q:"), i18n("Looks for a password matching :q:. Pressing ENTER copies the password to the clipboard."))); - addSyntax(Plasma::RunnerSyntax(QString("pass :q:"), + addSyntax(KRunner::RunnerSyntax(QString("pass :q:"), i18n("Looks for a password matching :q:. This way you avoid results from other runners"))); } @@ -153,7 +178,7 @@ void Pass::reinitPasswords(const QString &path) lock.unlock(); } -void Pass::match(Plasma::RunnerContext &context) +void Pass::match(KRunner::RunnerContext &context) { if (!context.isValid()) { return; @@ -167,14 +192,14 @@ void Pass::match(Plasma::RunnerContext &context) return; } - QList matches; + QList matches; lock.lockForRead(); for (const auto &password: qAsConst(passwords)) { if (password.contains(input, Qt::CaseInsensitive)) { - Plasma::QueryMatch match(this); - match.setCategoryRelevance(input.length() == password.length() ? Plasma::QueryMatch::CategoryRelevance::Highest : - Plasma::QueryMatch::CategoryRelevance::Moderate); + KRunner::QueryMatch match(this); + match.setCategoryRelevance(input.length() == password.length() ? KRunner::QueryMatch::CategoryRelevance::Highest : + KRunner::QueryMatch::CategoryRelevance::Moderate); match.setIcon(QIcon::fromTheme("object-locked")); match.setText(password); matches.append(match); @@ -198,7 +223,7 @@ void Pass::clip(const QString &msg) }); } -void Pass::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) +void Pass::run(const KRunner::RunnerContext &context, const KRunner::QueryMatch &match) { Q_UNUSED(context); const auto regexp = QRegularExpression("^" + QRegularExpression::escape(this->passOtpIdentifier) + ".*"); @@ -218,8 +243,13 @@ void Pass::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &m if (exitCode == 0) { const auto output = pass->readAllStandardOutput(); - if (match.selectedAction() != nullptr) { - const auto data = match.selectedAction()->data().toString(); + if (match.selectedAction()) { + const auto data = +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + match.selectedAction()->data().toString(); +#else + match.selectedAction().id(); +#endif if (data == Config::showFileContentAction) { QMessageBox::information(nullptr, match.text(), output); } else { @@ -228,7 +258,12 @@ void Pass::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &m if (matchre.hasMatch()) { clip(matchre.captured(1)); - this->showNotification(match.text(), match.selectedAction()->text()); + this->showNotification(match.text(), +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + match.selectedAction()->text()); +#else + match.selectedAction().text()); +#endif } else { // Show some information to understand what went wrong. qInfo() << "Regexp: " << data; @@ -251,7 +286,7 @@ void Pass::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &m pass->deleteLater(); }); } - +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QList Pass::actionsForMatch(const Plasma::QueryMatch &match) { Q_UNUSED(match) @@ -259,12 +294,18 @@ QList Pass::actionsForMatch(const Plasma::QueryMatch &match) return this->orderedActions; } +#endif + void Pass::showNotification(const QString &text, const QString &actionName) { const QString msgPrefix = actionName.isEmpty() ? "" : actionName + i18n(" of "); const QString msg = i18n("Password %1 copied to clipboard for %2 seconds", text, timeout); KNotification::event("password-unlocked", "Pass", msgPrefix + msg, - "object-unlocked", nullptr, KNotification::CloseOnTimeout, + "object-unlocked", +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + nullptr, +#endif + KNotification::CloseOnTimeout, "krunner_pass"); } diff --git a/pass.h b/pass.h index 29184d8..ee853a8 100644 --- a/pass.h +++ b/pass.h @@ -20,6 +20,18 @@ #define PASS_H #include + +namespace KRunner { +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + using AbstractRunner = Plasma::AbstractRunner; + using RunnerContext = Plasma::RunnerContext; + using QueryMatch = Plasma::QueryMatch; + using RunnerSyntax = Plasma::RunnerSyntax; +#else +class Action; +#endif +} + #include #include #include