not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / khotkeys / libkhotkeysprivate / actions / command_url_action.cpp
blob56e54d3b96b00adec0afb0bb20ae490aa82529bb
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/actions.h"
21 #include "action_data/action_data.h"
23 #include <KDE/KAuthorized>
24 #include <KDE/KConfigGroup>
25 #include <KDE/KRun>
26 #include <KDE/KService>
27 #include <KDE/KUriFilter>
28 #include <kworkspace/kworkspace.h>
31 namespace KHotKeys {
33 CommandUrlAction::CommandUrlAction( ActionData* data_P, const QString& command_url_P )
34 : Action( data_P ), _command_url( command_url_P )
39 QString CommandUrlAction::command_url() const
41 return _command_url;
45 CommandUrlAction::CommandUrlAction( KConfigGroup& cfg_P, ActionData* data_P )
46 : Action( cfg_P, data_P )
48 _command_url = cfg_P.readEntry( "CommandURL" );
52 void CommandUrlAction::cfg_write( KConfigGroup& cfg_P ) const
54 base::cfg_write( cfg_P );
55 cfg_P.writeEntry( "CommandURL", command_url());
56 cfg_P.writeEntry( "Type", "COMMAND_URL" ); // overwrites value set in base::cfg_write()
60 Action* CommandUrlAction::copy( ActionData* data_P ) const
62 return new CommandUrlAction( data_P, command_url());
66 const QString CommandUrlAction::description() const
68 return i18n( "Command/URL : " ) + command_url();
72 void CommandUrlAction::execute()
74 if( command_url().isEmpty())
75 return;
76 KUriFilterData uri;
77 QString cmd = command_url();
78 static bool sm_ready = false;
79 if( !sm_ready )
81 KWorkSpace::propagateSessionManager();
82 sm_ready = true;
84 // int space_pos = command_url().find( ' ' );
85 // if( command_url()[ 0 ] != '\'' && command_url()[ 0 ] != '"' && space_pos > -1
86 // && command_url()[ space_pos - 1 ] != '\\' )
87 // cmd = command_url().left( space_pos ); // get first 'word'
88 uri.setData( cmd );
89 KUriFilter::self()->filterUri( uri );
90 if( uri.uri().isLocalFile() && !uri.uri().hasRef() )
91 cmd = uri.uri().path();
92 else
93 cmd = uri.uri().url();
94 switch( uri.uriType())
96 case KUriFilterData::LocalFile:
97 case KUriFilterData::LocalDir:
98 case KUriFilterData::NetProtocol:
99 case KUriFilterData::Help:
101 ( void ) new KRun( uri.uri(),0L);
102 break;
104 case KUriFilterData::Executable:
106 if (!KAuthorized::authorizeKAction("shell_access"))
107 return;
108 if( !uri.hasArgsAndOptions())
110 KService::Ptr service = KService::serviceByDesktopName( cmd );
111 if( service )
113 KRun::run( *service, KUrl::List(), NULL );
114 break;
117 // fall though
119 case KUriFilterData::Shell:
121 if (!KAuthorized::authorizeKAction("shell_access"))
122 return;
123 if( !KRun::runCommand(
124 cmd + ( uri.hasArgsAndOptions() ? uri.argsAndOptions() : "" ),
125 cmd, uri.iconName(), NULL )) {
126 // CHECKME ?
128 break;
130 default: // error
131 return;
133 timeout.setSingleShot( true );
134 timeout.start( 1000 ); // 1sec timeout
139 void CommandUrlAction::set_command_url( const QString &command )
141 _command_url = command;
144 } // namespace KHotKeys