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 "actions.h" 0009 0010 #include <KConfigGroup> 0011 #include <KProcess> 0012 #include <QDebug> 0013 0014 namespace KHotKeys 0015 { 0016 DBusActionVisitor::~DBusActionVisitor() 0017 { 0018 } 0019 0020 DBusAction::DBusAction(ActionData *data_P, const QString &app_P, const QString &obj_P, const QString &call_P, const QString &args_P) 0021 : Action(data_P) 0022 , _application(app_P) 0023 , _object(obj_P) 0024 , _function(call_P) 0025 , _arguments(args_P) 0026 { 0027 } 0028 0029 void DBusAction::accept(ActionVisitor &visitor) 0030 { 0031 if (DBusActionVisitor *v = dynamic_cast<DBusActionVisitor *>(&visitor)) { 0032 v->visit(*this); 0033 } else { 0034 qDebug() << "Visitor error"; 0035 } 0036 } 0037 0038 const QString DBusAction::remote_application() const 0039 { 0040 return _application; 0041 } 0042 0043 const QString DBusAction::remote_object() const 0044 { 0045 return _object; 0046 } 0047 0048 const QString DBusAction::called_function() const 0049 { 0050 return _function; 0051 } 0052 0053 const QString DBusAction::arguments() const 0054 { 0055 return _arguments; 0056 } 0057 0058 void DBusAction::cfg_write(KConfigGroup &cfg_P) const 0059 { 0060 base::cfg_write(cfg_P); 0061 cfg_P.writeEntry("Type", "DBUS"); // overwrites value set in base::cfg_write() 0062 cfg_P.writeEntry("RemoteApp", _application); 0063 cfg_P.writeEntry("RemoteObj", _object); 0064 cfg_P.writeEntry("Call", _function); 0065 cfg_P.writeEntry("Arguments", _arguments); 0066 } 0067 0068 void DBusAction::execute() 0069 { 0070 if (_application.isEmpty() || _object.isEmpty() || _function.isEmpty()) 0071 return; 0072 QStringList args_list; 0073 QString args_str = _arguments; 0074 while (!args_str.isEmpty()) { 0075 int pos = 0; 0076 while (args_str[pos] == ' ') 0077 ++pos; 0078 if (args_str[pos] == '\"' || args_str[pos] == '\'') { 0079 QString val = ""; 0080 QChar sep = args_str[pos]; 0081 bool skip = false; 0082 ++pos; 0083 for (; pos < args_str.length(); ++pos) { 0084 if (args_str[pos] == '\\') { 0085 skip = true; 0086 continue; 0087 } 0088 if (!skip && args_str[pos] == sep) 0089 break; 0090 skip = false; 0091 val += args_str[pos]; 0092 } 0093 if (pos >= args_str.length()) 0094 return; 0095 ++pos; 0096 args_str = args_str.mid(pos); 0097 args_list.append(val); 0098 } else { 0099 // one word 0100 if (pos != 0) 0101 args_str = args_str.mid(pos); 0102 int nxt_pos = args_str.indexOf(' '); 0103 args_list.append(args_str.left(nxt_pos)); // should be ok if nxt_pos is -1 0104 args_str = nxt_pos >= 0 ? args_str.mid(nxt_pos) : ""; 0105 } 0106 } 0107 qDebug() << "D-Bus call:" << _application << ":" << _object << ":" << _function << ":" << args_list; 0108 KProcess proc; 0109 proc << "qdbus" << _application << _object << _function << args_list; 0110 proc.startDetached(); 0111 } 0112 0113 const QString DBusAction::description() const 0114 { 0115 return i18n("D-Bus: ") + remote_application() + "::" + remote_object() + "::" + called_function(); 0116 } 0117 0118 Action *DBusAction::copy(ActionData *data_P) const 0119 { 0120 return new DBusAction(data_P, remote_application(), remote_object(), called_function(), arguments()); 0121 } 0122 0123 void DBusAction::set_arguments(const QString &arguments) 0124 { 0125 _arguments = arguments; 0126 } 0127 0128 void DBusAction::set_called_function(const QString &function) 0129 { 0130 _function = function; 0131 } 0132 0133 void DBusAction::set_remote_application(const QString &application) 0134 { 0135 _application = application; 0136 } 0137 0138 void DBusAction::set_remote_object(const QString &object) 0139 { 0140 _object = object; 0141 } 0142 0143 } // namespace KHotKeys