File indexing completed on 2024-12-01 13:34:28

0001 /*
0002    SPDX-FileCopyrightText: 1999-2001 Lubos Lunak <l.lunak@kde.org>
0003    SPDX-FileCopyrightText: 2008 Michael Jansen <kde@michael-jansen.biz>
0004 
0005    SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "action_data/action_data.h"
0009 #include "actions.h"
0010 
0011 #include <KConfigGroup>
0012 #include <KMessageBox>
0013 #include <KRun>
0014 #include <QDebug>
0015 #include <QUrl>
0016 
0017 namespace KHotKeys
0018 {
0019 MenuEntryActionVisitor::~MenuEntryActionVisitor()
0020 {
0021 }
0022 
0023 MenuEntryAction::MenuEntryAction(ActionData *data_P, const QString &menuentry_P)
0024     : CommandUrlAction(data_P, menuentry_P)
0025 {
0026 }
0027 
0028 void MenuEntryAction::accept(ActionVisitor &visitor)
0029 {
0030     if (MenuEntryActionVisitor *v = dynamic_cast<MenuEntryActionVisitor *>(&visitor)) {
0031         v->visit(*this);
0032     } else {
0033         qDebug() << "Visitor error";
0034     }
0035 }
0036 
0037 void MenuEntryAction::cfg_write(KConfigGroup &cfg_P) const
0038 {
0039     base::cfg_write(cfg_P);
0040     cfg_P.writeEntry("Type", "MENUENTRY"); // overwrites value set in base::cfg_write()
0041 }
0042 
0043 KService::Ptr MenuEntryAction::service() const
0044 {
0045     if (!_service) {
0046         const_cast<MenuEntryAction *>(this)->_service = KService::serviceByStorageId(command_url());
0047     }
0048     return _service;
0049 }
0050 
0051 void MenuEntryAction::set_service(KService::Ptr service)
0052 {
0053     _service = service;
0054     if (service) {
0055         set_command_url(service->storageId());
0056     } else {
0057         set_command_url(QString());
0058     }
0059 }
0060 
0061 void MenuEntryAction::execute()
0062 {
0063     if (!service()) {
0064         KMessageBox::error(nullptr, i18n("No service configured."), i18n("Input Action: %1", data->comment()));
0065         return;
0066     }
0067 
0068     if (!KRun::run(*service(), {}, nullptr)) {
0069         KMessageBox::error(nullptr, i18n("Failed to start service '%1'.", service()->name()), i18n("Input Action: %1", data->comment()));
0070         return;
0071     }
0072 }
0073 
0074 Action *MenuEntryAction::copy(ActionData *data_P) const
0075 {
0076     return new MenuEntryAction(data_P, command_url());
0077 }
0078 
0079 const QString MenuEntryAction::description() const
0080 {
0081     return i18n("Menu entry: ") + (service() ? service()->comment() : QString());
0082 }
0083 
0084 } // namespace KHotKeys