not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / khotkeys / libkhotkeysprivate / actions / keyboard_input_action.cpp
blobba1ffd590714e6f4986346497a46a06f32c65f8c
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 "input.h"
23 #include <KDE/KConfigGroup>
25 #include "windows.h"
27 #include "shortcuts_handler.h"
29 #include <X11/X.h>
30 #include <X11/Xlib.h>
31 #include <QX11Info>
34 namespace KHotKeys {
36 KeyboardInputAction::KeyboardInputAction( ActionData* data_P, const QString& input_P,
37 const Windowdef_list* dest_window_P, bool active_window_P )
38 : Action( data_P ), _input( input_P ), _dest_window( dest_window_P ), _active_window( active_window_P )
43 KeyboardInputAction::KeyboardInputAction( KConfigGroup& cfg_P, ActionData* data_P )
44 : Action( cfg_P, data_P )
46 _input = cfg_P.readEntry( "Input" );
47 if( cfg_P.readEntry( "IsDestinationWindow" , false))
49 KConfigGroup windowGroup( cfg_P.config(), cfg_P.name() + "DestinationWindow" );
50 _dest_window = new Windowdef_list( windowGroup );
51 _active_window = false; // ignored with _dest_window set anyway
53 else
55 _dest_window = NULL;
56 _active_window = cfg_P.readEntry( "ActiveWindow" , false);
61 KeyboardInputAction::~KeyboardInputAction()
63 delete _dest_window;
67 const QString& KeyboardInputAction::input() const
69 return _input;
73 const Windowdef_list* KeyboardInputAction::dest_window() const
75 return _dest_window;
79 bool KeyboardInputAction::activeWindow() const
81 return _active_window;
85 void KeyboardInputAction::cfg_write( KConfigGroup& cfg_P ) const
87 base::cfg_write( cfg_P );
88 cfg_P.writeEntry( "Type", "KEYBOARD_INPUT" ); // overwrites value set in base::cfg_write()
89 cfg_P.writeEntry( "Input", input());
90 if( dest_window() != NULL )
92 cfg_P.writeEntry( "IsDestinationWindow", true );
93 KConfigGroup windowGroup( cfg_P.config(), cfg_P.name() + "DestinationWindow" );
94 dest_window()->cfg_write( windowGroup );
96 else
97 cfg_P.writeEntry( "IsDestinationWindow", false );
98 cfg_P.writeEntry( "ActiveWindow", _active_window );
102 void KeyboardInputAction::execute()
104 if( input().isEmpty())
105 return;
106 Window w = InputFocus;
107 if( dest_window() != NULL )
109 w = windows_handler->find_window( dest_window());
110 if( w == None )
111 w = InputFocus;
113 else
115 if( !_active_window )
116 w = windows_handler->action_window();
117 if( w == None )
118 w = InputFocus;
120 int last_index = -1, start = 0;
121 while(( last_index = input().indexOf( ':', last_index + 1 )) != -1 ) // find next ';'
123 QString key = input().mid( start, last_index - start ).trimmed();
124 keyboard_handler->send_macro_key( key, w );
125 start = last_index + 1;
127 // and the last one
128 QString key = input().mid( start, input().length()).trimmed();
129 keyboard_handler->send_macro_key( key, w ); // the rest
130 XFlush( QX11Info::display());
134 const QString KeyboardInputAction::description() const
136 QString tmp = input();
137 tmp.replace( '\n', ' ' );
138 tmp.truncate( 30 );
139 return i18n( "Keyboard input: " ) + tmp;
143 Action* KeyboardInputAction::copy( ActionData* data_P ) const
145 return new KeyboardInputAction( data_P, input(),
146 dest_window() ? dest_window()->copy() : NULL, _active_window );
149 } // namespace KHotKeys