Add (and install) svg for the new krunner interface.
[kdebase/uwolfer.git] / workspace / khotkeys / kcontrol / kcmkhotkeys.cpp
blob916846b92c972688d50f83808a5f3ade65284253
1 /****************************************************************************
3 KHotKeys
5 Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org>
7 Distributed under the terms of the GNU General Public License version 2.
9 ****************************************************************************/
11 #define _KCMKHOTKEYS_CPP_
13 #include <config-khotkeys.h> // HAVE_ARTS
15 #include "kcmkhotkeys.h"
16 #include "khotkeysiface.h"
18 #include <unistd.h>
19 #include <stdlib.h>
21 #include <QLayout>
22 #include <QSplitter>
23 //Added by qt3to4:
24 #include <QVBoxLayout>
26 #include <kcmodule.h>
27 #include <kaboutdata.h>
28 #include <klocale.h>
29 #include <kapplication.h>
30 #include <kconfig.h>
31 #include <kdebug.h>
32 #include <kmessagebox.h>
33 #include <kglobal.h>
34 #include <kfiledialog.h>
35 #include <ktoolinvocation.h>
36 #include <klibloader.h>
38 #include <input.h>
39 #include <triggers.h>
40 #include <action_data.h>
42 #include <QtDBus/QtDBus>
44 #include "tab_widget.h"
45 #include "actions_listview_widget.h"
46 #include "main_buttons_widget.h"
47 #include "voicerecorder.h"
49 K_PLUGIN_FACTORY(KHotKeysFactory,
50 registerPlugin<KHotKeys::Module>();
52 K_EXPORT_PLUGIN(KHotKeysFactory("khotkeys"))
54 extern "C"
56 KDE_EXPORT void kcminit_khotkeys()
58 KConfig _cfg( "khotkeysrc" );
59 KConfigGroup cfg(&_cfg, "Main" );
60 if( !cfg.readEntry( "Autostart", false))
61 return;
62 // Non-xinerama multhead support in KDE is just a hack
63 // involving forking apps per-screen. Don't bother with
64 // kded modules in such case.
65 QByteArray multiHead = getenv("KDE_MULTIHEAD");
66 if (multiHead.toLower() == "true")
67 KToolInvocation::kdeinitExec( "khotkeys" );
68 else
70 QDBusInterface kded("org.kde.kded", "/kded", "org.kde.kded");
71 QDBusReply<bool> reply = kded.call("loadModule",QString( "khotkeys" ) );
72 if( !reply.isValid())
74 kWarning( 1217 ) << "Loading of khotkeys module failed." ;
75 KToolInvocation::kdeinitExec( "khotkeys" );
81 namespace KHotKeys
84 Module::Module( QWidget* parent_P, const QVariantList & )
85 : KCModule( KHotKeysFactory::componentData(), parent_P ), _actions_root( NULL ), _current_action_data( NULL ),
86 listview_is_changed( false ), deleting_action( false )
88 setButtons( Apply );
89 module = this;
90 init_global_data( false, this ); // don't grab keys
91 init_arts();
92 QVBoxLayout* vbox = new QVBoxLayout( this );
93 vbox->setSpacing( KDialog::spacingHint() );
94 vbox->setMargin( 0 );
95 QSplitter* splt = new QSplitter( this );
96 actions_listview_widget = new Actions_listview_widget( splt );
97 actions_listview_widget->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Expanding );
98 tab_widget = new Tab_widget( splt );
99 tab_widget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
100 vbox->addWidget( splt );
101 splt->setStretchFactor( 0, 0 );
102 splt->setStretchFactor( 1, 1 );
103 buttons_widget = new Main_buttons_widget( this );
104 vbox->addWidget( buttons_widget );
105 connect( actions_listview_widget, SIGNAL( current_action_changed()),
106 SLOT( listview_current_action_changed()));
107 connect( buttons_widget, SIGNAL( new_action_pressed()), SLOT( new_action()));
108 connect( buttons_widget, SIGNAL( new_action_group_pressed()), SLOT( new_action_group()));
109 connect( buttons_widget, SIGNAL( delete_action_pressed()), SLOT( delete_action()));
110 connect( buttons_widget, SIGNAL( global_settings_pressed()), SLOT( global_settings()));
111 // listview_current_action_changed(); // init
113 KAboutData* about = new KAboutData("kcmkhotkeys", 0, ki18n("KHotKeys"), KHOTKEYS_VERSION,
114 KLocalizedString(), KAboutData::License_GPL, ki18n("(c) 1999-2005 Lubos Lunak"));
115 about->addAuthor(ki18n("Lubos Lunak"), ki18n("Maintainer"), "l.lunak@kde.org");
116 setAboutData( about );
117 load();
120 Module::~Module()
122 _current_action_data = NULL;
123 tab_widget->load_current_action(); // clear tab_widget
124 delete _actions_root;
125 module = NULL;
128 void Module::load()
130 actions_listview_widget->clear();
131 delete _actions_root;
132 settings.actions = NULL;
133 _current_action_data = NULL;
134 settings.read_settings( true );
135 _actions_root = settings.actions;
136 kDebug( 1217 ) << "actions_root:" << _actions_root;
137 actions_listview_widget->build_up();
138 tab_widget->load_current_action();
139 emit KCModule::changed( false ); // HACK otherwise the module would be changed from the very beginning
142 void Module::save()
144 tab_widget->save_current_action_changes();
145 settings.actions = _actions_root;
146 kDebug(1217) << "Storing actions" << _actions_root;
147 settings.write_settings();
148 QDBusConnection bus = QDBusConnection::sessionBus();
149 if( daemon_disabled())
151 if( bus.interface()->isServiceRegistered( "org.kde.khotkeys" ))
153 // wait for it to finish
154 org::kde::khotkeys* iface = new org::kde::khotkeys("org.kde.khotkeys", "/KHotKeys", bus, this);
155 iface->quit();
156 sleep( 1 );
158 kDebug( 1217 ) << "disabling khotkeys daemon";
160 else
162 if( !bus.interface()->isServiceRegistered( "org.kde.khotkeys" ))
164 kDebug( 1217 ) << "launching new khotkeys daemon";
165 KToolInvocation::kdeinitExec( "khotkeys" );
167 else
169 org::kde::khotkeys iface("org.kde.khotkeys", "/KHotKeys", bus);
170 iface.reread_configuration();
171 kDebug( 1217 ) << "telling khotkeys daemon to reread configuration";
174 emit KCModule::changed( false );
178 QString Module::quickHelp() const
180 // return i18n( "" ); // TODO CHECKME
181 return QString(); // TODO CHECKME
184 void Module::action_name_changed( const QString& name_P )
186 current_action_data()->set_name( name_P );
187 actions_listview_widget->action_name_changed( name_P );
190 void Module::listview_current_action_changed()
192 // CHECKME tohle je trosku hack, aby se pri save zmenenych hodnot ve stare vybrane polozce
193 // zmenila data v te stare polozce a ne nove aktivni
194 listview_is_changed = true;
195 set_new_current_action( !deleting_action );
196 listview_is_changed = false;
199 void Module::set_new_current_action( bool save_old_P )
201 if( save_old_P )
202 tab_widget->save_current_action_changes();
203 _current_action_data = actions_listview_widget->current_action_data();
204 kDebug( 1217 ) << "set_new_current_action : " << _current_action_data;
205 tab_widget->load_current_action();
206 buttons_widget->enable_delete( current_action_data() != NULL );
209 // CHECKME volano jen z Tab_widget pro nastaveni zmenenych dat ( novy Action_data_base )
210 void Module::set_current_action_data( Action_data_base* data_P )
212 delete _current_action_data;
213 _current_action_data = data_P;
214 actions_listview_widget->set_action_data( data_P, listview_is_changed );
215 // tab_widget->load_current_action(); CHECKME asi neni treba
218 #if 0
221 #include <iostream>
222 #include <iomanip>
223 #include <KPluginFactory>
224 #include <KPluginLoader>
225 namespace KHotKeys {
227 void check_tree( Action_data_group* b, int lev_P = 0 )
229 using namespace std;
230 cerr << setw( lev_P ) << "" << b << ":Group:" << b->name().toLatin1() << ":" << b->parent() << endl;
231 for( Action_data_group::Iterator it = b->first_child();
233 ++it )
234 if( Action_data_group* g = dynamic_cast< Action_data_group* >( *it ))
235 check_tree( g, lev_P + 1 );
236 else
237 cerr << setw( lev_P + 1 ) << "" << (*it) << ":Action:" << (*it)->name().toLatin1() << ":" << (*it)->parent() << endl;
240 #endif
242 void Module::new_action()
244 tab_widget->save_current_action_changes();
245 // check_tree( actions_root());
246 Action_data_group* parent = current_action_data() != NULL
247 ? dynamic_cast< Action_data_group* >( current_action_data()) : NULL;
248 if( parent == NULL )
250 if( current_action_data() != NULL )
251 parent = current_action_data()->parent();
252 else
253 parent = module->actions_root();
255 Action_data_base* item = new Generic_action_data( parent, i18n( "New Action" ), "",
256 new Trigger_list( "" ), new Condition_list( "", NULL ), new Action_list( "" ), true );
257 actions_listview_widget->new_action( item );
258 // check_tree( actions_root());
259 set_new_current_action( false );
262 // CHECKME spojit tyhle dve do jedne
263 void Module::new_action_group()
265 tab_widget->save_current_action_changes();
266 // check_tree( actions_root());
267 Action_data_group* parent = current_action_data() != NULL
268 ? dynamic_cast< Action_data_group* >( current_action_data()) : NULL;
269 if( parent == NULL )
271 if( current_action_data() != NULL )
272 parent = current_action_data()->parent();
273 else
274 parent = module->actions_root();
276 Action_data_base* item = new Action_data_group( parent, i18n( "New Action Group" ), "",
277 new Condition_list( "", NULL ), Action_data_group::SYSTEM_NONE, true );
278 actions_listview_widget->new_action( item );
279 // check_tree( actions_root());
280 set_new_current_action( false );
283 void Module::delete_action()
285 delete _current_action_data;
286 _current_action_data = NULL;
287 deleting_action = true; // CHECKME zase tak trosku hack, jinak by se snazilo provest save
288 actions_listview_widget->delete_action(); // prave mazane polozky
289 deleting_action = false;
290 set_new_current_action( false );
293 void Module::global_settings()
295 actions_listview_widget->set_current_action( NULL );
296 set_new_current_action( true );
299 void Module::set_gestures_exclude( Windowdef_list* windows )
301 delete settings.gestures_exclude;
302 settings.gestures_exclude = windows;
305 void Module::import()
307 QString file = KFileDialog::getOpenFileName( QString(), "*.khotkeys", window(),
308 i18n( "Select File with Actions to Be Imported" ));
309 if( file.isEmpty())
310 return;
311 KConfig cfg( file, KConfig::SimpleConfig);
312 if( !settings.import( cfg, true ))
314 KMessageBox::error( window(),
315 i18n( "Import of the specified file failed. Most probably the file is not a valid "
316 "file with actions." ));
317 return;
319 actions_listview_widget->clear();
320 actions_listview_widget->build_up();
321 tab_widget->load_current_action();
322 emit KCModule::changed( true );
325 void Module::changed()
327 emit KCModule::changed( true );
330 void Module::init_arts()
332 #warning Port to Phonon
333 #ifdef HAVE_ARTS
334 if( haveArts())
336 KLibrary* arts = KLibLoader::self()->library( QLatin1String("khotkeys_arts") );
337 if( arts == NULL )
338 kDebug( 1217 ) << "Couldn't load khotkeys_arts:" << KLibLoader::self()->lastErrorMessage();
339 if( arts != NULL && VoiceRecorder::init( arts ))
340 ; // ok
341 else
342 disableArts();
344 #endif
347 Module* module; // CHECKME
349 } // namespace KHotKeys
351 #include "kcmkhotkeys.moc"