not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / khotkeys / libkhotkeysprivate / actions / dbus_action.cpp
blob060d85c82574fabc797c35e393c05a9284589ac3
1 /*
2 Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org>
3 Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "actions.h"
22 #include <KDE/KConfigGroup>
23 #include <KDE/KDebug>
24 #include <KDE/KProcess>
26 namespace KHotKeys {
28 DBusAction::DBusAction( KConfigGroup& cfg_P, ActionData* data_P )
29 : Action( cfg_P, data_P )
31 _application = cfg_P.readEntry( "RemoteApp" );
32 _object = cfg_P.readEntry( "RemoteObj" );
33 _function = cfg_P.readEntry( "Call" );
34 _arguments = cfg_P.readEntry( "Arguments" );
38 DBusAction::DBusAction( ActionData* data_P, const QString& app_P, const QString& obj_P,
39 const QString& call_P, const QString& args_P )
40 : Action( data_P ), _application( app_P ), _object( obj_P ), _function( call_P ), _arguments( args_P )
45 const QString DBusAction::remote_application() const
47 return _application;
51 const QString DBusAction::remote_object() const
53 return _object;
57 const QString DBusAction::called_function() const
59 return _function;
63 const QString DBusAction::arguments() const
65 return _arguments;
69 void DBusAction::cfg_write( KConfigGroup& cfg_P ) const
71 base::cfg_write( cfg_P );
72 cfg_P.writeEntry( "Type", "DBUS" ); // overwrites value set in base::cfg_write()
73 cfg_P.writeEntry( "RemoteApp", _application );
74 cfg_P.writeEntry( "RemoteObj", _object );
75 cfg_P.writeEntry( "Call", _function );
76 cfg_P.writeEntry( "Arguments", _arguments );
80 void DBusAction::execute()
82 if( _application.isEmpty() || _object.isEmpty() || _function.isEmpty())
83 return;
84 QStringList args_list;
85 QString args_str = _arguments;
86 while( !args_str.isEmpty())
88 int pos = 0;
89 while( args_str[ pos ] == ' ' )
90 ++pos;
91 if( args_str[ pos ] == '\"' || args_str[ pos ] == '\'' )
93 QString val = "";
94 QChar sep = args_str[ pos ];
95 bool skip = false;
96 ++pos;
97 for(;
98 pos < args_str.length();
99 ++pos )
101 if( args_str[ pos ] == '\\' )
103 skip = true;
104 continue;
106 if( !skip && args_str[ pos ] == sep )
107 break;
108 skip = false;
109 val += args_str[ pos ];
111 if( pos >= args_str.length())
112 return;
113 ++pos;
114 args_str = args_str.mid( pos );
115 args_list.append( val );
117 else
119 // one word
120 if( pos != 0 )
121 args_str = args_str.mid( pos );
122 int nxt_pos = args_str.indexOf( ' ' );
123 args_list.append( args_str.left( nxt_pos )); // should be ok if nxt_pos is -1
124 args_str = nxt_pos >= 0 ? args_str.mid( nxt_pos ) : "";
127 kDebug() << "D-Bus call:" << _application << ":" << _object << ":" << _function << ":" << args_list;
128 KProcess proc;
129 proc << "qdbus" << _application << _object << _function << args_list;
130 proc.startDetached();
134 const QString DBusAction::description() const
136 return i18n( "D-Bus: " ) + remote_application() + "::" + remote_object() + "::"
137 + called_function();
141 Action* DBusAction::copy( ActionData* data_P ) const
143 return new DBusAction( data_P, remote_application(), remote_object(),
144 called_function(), arguments());
147 void DBusAction::set_arguments( const QString &arguments )
149 _arguments = arguments;
153 void DBusAction::set_called_function( const QString &function )
155 _function = function;
159 void DBusAction::set_remote_application( const QString &application )
161 _application = application;
165 void DBusAction::set_remote_object( const QString &object )
167 _object = object;
171 } // namespace KHotKeys