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/actions.h"
0010 
0011 #include <KAuthorized>
0012 #include <KConfigGroup>
0013 #include <KDialogJobUiDelegate>
0014 #include <KIO/ApplicationLauncherJob>
0015 #include <KIO/CommandLauncherJob>
0016 #include <KRun>
0017 #include <KService>
0018 #include <KUriFilter>
0019 #include <kworkspace.h>
0020 
0021 #include <QDebug>
0022 #include <QUrl>
0023 
0024 namespace KHotKeys
0025 {
0026 CommandUrlActionVisitor::~CommandUrlActionVisitor()
0027 {
0028 }
0029 
0030 CommandUrlAction::CommandUrlAction(ActionData *data_P, const QString &command_url_P)
0031     : Action(data_P)
0032     , _command_url(command_url_P)
0033 {
0034 }
0035 
0036 QString CommandUrlAction::command_url() const
0037 {
0038     return _command_url;
0039 }
0040 
0041 void CommandUrlAction::accept(ActionVisitor &visitor)
0042 {
0043     if (CommandUrlActionVisitor *v = dynamic_cast<CommandUrlActionVisitor *>(&visitor)) {
0044         v->visit(*this);
0045     } else {
0046         qDebug() << "Visitor error";
0047     }
0048 }
0049 
0050 void CommandUrlAction::cfg_write(KConfigGroup &cfg_P) const
0051 {
0052     base::cfg_write(cfg_P);
0053     cfg_P.writeEntry("CommandURL", command_url());
0054     cfg_P.writeEntry("Type", "COMMAND_URL"); // overwrites value set in base::cfg_write()
0055 }
0056 
0057 Action *CommandUrlAction::copy(ActionData *data_P) const
0058 {
0059     return new CommandUrlAction(data_P, command_url());
0060 }
0061 
0062 const QString CommandUrlAction::description() const
0063 {
0064     return i18n("Command/URL : ") + command_url();
0065 }
0066 
0067 void CommandUrlAction::execute()
0068 {
0069     qDebug();
0070     if (command_url().isEmpty())
0071         return;
0072     KUriFilterData uri;
0073     QString cmd = command_url();
0074     static bool sm_ready = false;
0075     if (!sm_ready) {
0076         KWorkSpace::propagateSessionManager();
0077         sm_ready = true;
0078     }
0079     //    int space_pos = command_url().find( ' ' );
0080     //    if( command_url()[ 0 ] != '\'' && command_url()[ 0 ] != '"' && space_pos > -1
0081     //        && command_url()[ space_pos - 1 ] != '\\' )
0082     //        cmd = command_url().left( space_pos ); // get first 'word'
0083     uri.setData(cmd);
0084     KUriFilter::self()->filterUri(uri);
0085     if (uri.uri().isLocalFile() && !uri.uri().hasFragment())
0086         cmd = uri.uri().toLocalFile();
0087     else
0088         cmd = uri.uri().url();
0089     switch (uri.uriType()) {
0090     case KUriFilterData::LocalFile:
0091     case KUriFilterData::LocalDir:
0092     case KUriFilterData::NetProtocol:
0093     case KUriFilterData::Help: {
0094         (void)new KRun(uri.uri(), 0L);
0095         break;
0096     }
0097     case KUriFilterData::Executable: {
0098         if (!KAuthorized::authorizeAction("shell_access"))
0099             return;
0100         if (!uri.hasArgsAndOptions()) {
0101             KService::Ptr service = KService::serviceByDesktopName(cmd);
0102             if (service) {
0103                 auto *job = new KIO::ApplicationLauncherJob(service);
0104                 job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr));
0105                 job->start();
0106                 break;
0107             }
0108         }
0109         // fall though
0110     }
0111     case KUriFilterData::Shell: {
0112         if (!KAuthorized::authorizeAction("shell_access"))
0113             return;
0114         const QString exec = cmd + (uri.hasArgsAndOptions() ? uri.argsAndOptions() : QString());
0115         auto *job = new KIO::CommandLauncherJob(exec);
0116         job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr));
0117         job->setExecutable(cmd);
0118         job->setIcon(uri.iconName());
0119         job->start();
0120         break;
0121     }
0122     default: // error
0123         return;
0124     }
0125 }
0126 
0127 void CommandUrlAction::set_command_url(const QString &command)
0128 {
0129     _command_url = command;
0130 }
0131 
0132 } // namespace KHotKeys