add more spacing
[personal-kdebase.git] / workspace / khotkeys / libkhotkeysprivate / triggers / window_trigger.cpp
blobb980ce9f88960bbba32f75ace4830a7fe7beb7d5
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 "triggers/triggers.h"
21 #include "action_data/action_data.h"
22 #include "windows.h"
24 #include <KDE/KConfigGroup>
25 #include <KDE/KDebug>
27 #include <X11/X.h>
29 namespace KHotKeys {
31 WindowTrigger::WindowTrigger( ActionData* data_P, Windowdef_list* windows_P,
32 int window_actions_P )
33 : Trigger( data_P ), _windows( windows_P ), window_actions( window_actions_P ),
34 last_active_window( None ), active( false )
36 init();
40 WindowTrigger::WindowTrigger( KConfigGroup& cfg_P, ActionData* data_P )
41 : Trigger( cfg_P, data_P ), active( false )
43 // kDebug() << "WindowTrigger";
44 KConfigGroup windowsConfig( cfg_P.config(), cfg_P.name() + "Windows" );
45 _windows = new Windowdef_list( windowsConfig );
46 window_actions = cfg_P.readEntry( "WindowActions",0 );
47 init();
51 WindowTrigger::~WindowTrigger()
53 // kDebug() << "~WindowTrigger :" << this;
54 disconnect( windows_handler, NULL, this, NULL );
55 delete _windows;
58 void WindowTrigger::init()
60 kDebug() << "WindowTrigger::init()";
61 connect( windows_handler, SIGNAL( window_added( WId )), this, SLOT( window_added( WId )));
62 connect( windows_handler, SIGNAL( window_removed( WId )), this, SLOT( window_removed( WId )));
63 if( window_actions & ( WINDOW_ACTIVATES | WINDOW_DEACTIVATES /*| WINDOW_DISAPPEARS*/ ))
64 connect( windows_handler, SIGNAL( active_window_changed( WId )),
65 this, SLOT( active_window_changed( WId )));
66 connect( windows_handler, SIGNAL( window_changed( WId, unsigned int )),
67 this, SLOT( window_changed( WId, unsigned int )));
71 void WindowTrigger::activate( bool activate_P )
73 active = activate_P && khotkeys_active();
77 void WindowTrigger::active_window_changed( WId window_P )
79 bool was_match = false;
80 if( existing_windows.contains( last_active_window ))
81 was_match = existing_windows[ last_active_window ];
82 if( active && was_match && ( window_actions & WINDOW_DEACTIVATES ))
84 windows_handler->set_action_window( window_P );
85 data->execute();
87 /* bool matches = windows()->match( Window_data( window_P ));
88 existing_windows[ window_P ] = matches;*/
89 bool matches = existing_windows.contains( window_P )
90 ? existing_windows[ window_P ] : false;
91 if( active && matches && ( window_actions & WINDOW_ACTIVATES ))
93 windows_handler->set_action_window( window_P );
94 data->execute();
96 kDebug() << "WindowTrigger::a_w_changed() : " << was_match << "|" << matches;
97 last_active_window = window_P;
101 void WindowTrigger::cfg_write( KConfigGroup& cfg_P ) const
103 base::cfg_write( cfg_P );
104 KConfigGroup windowsConfig( cfg_P.config(), cfg_P.name() + "Windows" );
105 windows()->cfg_write( windowsConfig );
106 cfg_P.writeEntry( "WindowActions", window_actions );
107 cfg_P.writeEntry( "Type", "WINDOW" ); // overwrites value set in base::cfg_write()
111 const QString WindowTrigger::description() const
113 return i18n( "Window trigger: " ) + windows()->comment();
117 bool WindowTrigger::triggers_on( window_action_t w_action_P ) const
119 return window_actions & w_action_P;
123 void WindowTrigger::window_added( WId window_P )
125 bool matches = windows()->match( Window_data( window_P ));
126 existing_windows[ window_P ] = matches;
127 kDebug() << "WindowTrigger::w_added() : " << matches;
128 if( active && matches && ( window_actions & WINDOW_APPEARS ))
130 windows_handler->set_action_window( window_P );
131 data->execute();
136 void WindowTrigger::window_removed( WId window_P )
138 if( existing_windows.contains( window_P ))
140 bool matches = existing_windows[ window_P ];
141 kDebug() << "WindowTrigger::w_removed() : " << matches;
142 if( active && matches && ( window_actions & WINDOW_DISAPPEARS ))
144 windows_handler->set_action_window( window_P );
145 data->execute();
147 existing_windows.remove( window_P );
148 // CHECKME jenze co kdyz se window_removed zavola pred active_window_changed ?
150 else
151 kDebug() << "WindowTrigger::w_removed()";
155 void WindowTrigger::window_changed( WId window_P, unsigned int dirty_P )
156 { // CHECKME snad nebude mit vliv, kdyz budu kaslat na properties_P a zkratka
157 // kontrolovat kazdou zmenu
158 // CHECKME kdyz se zmeni okno z match na non-match, asi to nebrat jako DISAPPEAR
159 if( ! ( dirty_P & ( NET::WMName | NET::WMWindowType )))
160 return;
161 kDebug() << "WindowTrigger::w_changed()";
162 bool was_match = false;
163 if( existing_windows.contains( window_P ))
164 was_match = existing_windows[ window_P ];
165 bool matches = windows()->match( Window_data( window_P ));
166 existing_windows[ window_P ] = matches;
167 if( active && matches && !was_match )
169 if( window_actions & WINDOW_APPEARS )
171 windows_handler->set_action_window( window_P );
172 data->execute();
174 else if( window_actions & WINDOW_ACTIVATES && window_P == windows_handler->active_window())
176 windows_handler->set_action_window( window_P );
177 data->execute();
180 kDebug() << "WindowTrigger::w_changed() : " << was_match << "|" << matches;
184 const Windowdef_list* WindowTrigger::windows() const
186 return _windows;
190 WindowTrigger* WindowTrigger::copy( ActionData* data_P ) const
192 WindowTrigger* ret = new WindowTrigger( data_P ? data_P : data, windows()->copy(),
193 window_actions );
194 ret->existing_windows = existing_windows; // CHECKME je tohle vazne treba ?
195 return ret;
198 } // namespace KHotKeys