~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to khotkeys/libkhotkeysprivate/triggers/window_trigger.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org>
 
3
   Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>
 
4
 
 
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.
 
8
 
 
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.
 
13
 
 
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.
 
18
*/
 
19
 
 
20
#include "triggers/triggers.h"
 
21
#include "action_data/action_data.h"
 
22
#include "windows_handler.h"
 
23
#include "windows_helper/window_selection_list.h"
 
24
 
 
25
#include <KDE/KConfigGroup>
 
26
#include <KDE/KDebug>
 
27
 
 
28
#include <X11/X.h>
 
29
 
 
30
namespace KHotKeys {
 
31
 
 
32
 
 
33
WindowTriggerVisitor::~WindowTriggerVisitor()
 
34
    {}
 
35
 
 
36
 
 
37
WindowTrigger::WindowTrigger(
 
38
        ActionData* data_P,
 
39
        Windowdef_list* windows_P,
 
40
        WindowEvents window_actions_P)
 
41
    :   Trigger( data_P ),
 
42
        _windows( windows_P ),
 
43
        window_actions( window_actions_P ),
 
44
        existing_windows(),
 
45
        last_active_window( None ),
 
46
        active( true )
 
47
    {
 
48
    if (!_windows)
 
49
        {
 
50
        _windows = new Windowdef_list( "Windowdef_list comment");
 
51
        }
 
52
 
 
53
    Q_ASSERT(_windows->isEmpty());
 
54
    init();
 
55
    }
 
56
 
 
57
 
 
58
WindowTrigger::~WindowTrigger()
 
59
    {
 
60
//    kDebug() << "~WindowTrigger :" << this;
 
61
    disconnect( windows_handler, NULL, this, NULL );
 
62
    delete _windows;
 
63
    }
 
64
 
 
65
 
 
66
void WindowTrigger::accept(TriggerVisitor& visitor)
 
67
    {
 
68
    if (WindowTriggerVisitor *v = dynamic_cast<WindowTriggerVisitor*>(&visitor))
 
69
        {
 
70
        v->visit(*this);
 
71
        }
 
72
    else
 
73
        {
 
74
        kDebug() << "Visitor error";
 
75
        }
 
76
    }
 
77
 
 
78
 
 
79
void WindowTrigger::init()
 
80
    {
 
81
    kDebug() << "WindowTrigger::init()";
 
82
    connect( windows_handler, SIGNAL( window_added( WId )), this, SLOT( window_added( WId )));
 
83
    connect( windows_handler, SIGNAL( window_removed( WId )), this, SLOT( window_removed( WId )));
 
84
    if( window_actions & ( WINDOW_ACTIVATES | WINDOW_DEACTIVATES /*| WINDOW_DISAPPEARS*/ ))
 
85
        connect( windows_handler, SIGNAL( active_window_changed( WId )),
 
86
            this, SLOT( active_window_changed( WId )));
 
87
    connect( windows_handler, SIGNAL( window_changed( WId, unsigned int )),
 
88
        this, SLOT( window_changed( WId, unsigned int )));
 
89
    }
 
90
 
 
91
 
 
92
void WindowTrigger::activate( bool activate_P )
 
93
    {
 
94
    active = activate_P;
 
95
    }
 
96
 
 
97
 
 
98
void WindowTrigger::active_window_changed( WId window_P )
 
99
    {
 
100
    if (!existing_windows.contains(window_P))
 
101
        {
 
102
        existing_windows[window_P] = windows()->match( Window_data( window_P ));
 
103
        }
 
104
 
 
105
    if (!active || !khotkeys_active())
 
106
        {
 
107
        // We still keep track of the last active window so we have valid data
 
108
        // if khotkeys is switched on again.
 
109
        last_active_window = window_P;
 
110
        return;
 
111
        }
 
112
 
 
113
    // Check if the last active window was a match for us
 
114
    bool was_match = existing_windows.contains(last_active_window)
 
115
        ? existing_windows[last_active_window]
 
116
        : false;
 
117
 
 
118
    if (was_match && (window_actions & WINDOW_DEACTIVATES))
 
119
        {
 
120
        windows_handler->set_action_window( window_P );
 
121
        data->execute();
 
122
        }
 
123
 
 
124
    if (existing_windows[window_P] && ( window_actions & WINDOW_ACTIVATES))
 
125
        {
 
126
        kDebug() << "Executing data";
 
127
        windows_handler->set_action_window( window_P );
 
128
        data->execute();
 
129
        }
 
130
 
 
131
    last_active_window = window_P;
 
132
    }
 
133
 
 
134
 
 
135
void WindowTrigger::cfg_write( KConfigGroup& cfg_P ) const
 
136
    {
 
137
    base::cfg_write( cfg_P );
 
138
    KConfigGroup windowsConfig( cfg_P.config(), cfg_P.name() + "Windows" );
 
139
    if (windows())
 
140
        {
 
141
        windows()->cfg_write( windowsConfig );
 
142
        }
 
143
    cfg_P.writeEntry( "WindowActions", static_cast<int>(window_actions));
 
144
    cfg_P.writeEntry( "Type", "WINDOW" ); // overwrites value set in base::cfg_write()
 
145
    }
 
146
 
 
147
 
 
148
const QString WindowTrigger::description() const
 
149
    {
 
150
    return i18n( "Window trigger: " ) + windows()->comment();
 
151
    }
 
152
 
 
153
 
 
154
void WindowTrigger::setOnWindowEvents(WindowEvents events)
 
155
    {
 
156
    window_actions = events;
 
157
    }
 
158
 
 
159
 
 
160
void WindowTrigger::set_window_rules(Windowdef_list *list)
 
161
    {
 
162
     delete _windows;
 
163
    _windows = list;
 
164
    }
 
165
 
 
166
 
 
167
bool WindowTrigger::triggers_on( window_action_t w_action_P ) const
 
168
    {
 
169
    return window_actions & w_action_P;
 
170
    }
 
171
 
 
172
 
 
173
void WindowTrigger::window_added( WId window_P )
 
174
    {
 
175
    // Always keep track of windows,
 
176
    existing_windows[window_P] = windows()->match( Window_data( window_P ));
 
177
 
 
178
    if (!active || !khotkeys_active())
 
179
        {
 
180
        return;
 
181
        }
 
182
 
 
183
    if (existing_windows[window_P] && (window_actions & WINDOW_APPEARS))
 
184
        {
 
185
        windows_handler->set_action_window(window_P);
 
186
        data->execute();
 
187
        }
 
188
    }
 
189
 
 
190
 
 
191
void WindowTrigger::window_removed( WId window_P )
 
192
    {
 
193
    // Always keep track of windows,
 
194
    bool matches = false;
 
195
    if (existing_windows.contains(window_P))
 
196
        {
 
197
        matches = existing_windows[window_P];
 
198
        existing_windows.remove( window_P );
 
199
        }
 
200
 
 
201
    if (!active || !khotkeys_active())
 
202
        {
 
203
        return;
 
204
        }
 
205
 
 
206
    if (matches && (window_actions & WINDOW_DISAPPEARS))
 
207
        {
 
208
        windows_handler->set_action_window( window_P );
 
209
        data->execute();
 
210
        }
 
211
    }
 
212
 
 
213
 
 
214
void WindowTrigger::window_changed( WId window_P, unsigned int dirty_P )
 
215
    {
 
216
    if (! (dirty_P & (NET::WMName | NET::WMWindowType)))
 
217
        return;
 
218
 
 
219
    // Check if the old state was a match
 
220
    bool was_match = false;
 
221
    if (existing_windows.contains(window_P))
 
222
        was_match = existing_windows[window_P];
 
223
 
 
224
    // Check if the new state is a match
 
225
    bool matches = windows()->match( Window_data( window_P ));
 
226
    existing_windows[window_P] = matches;
 
227
 
 
228
    if (!active || !khotkeys_active())
 
229
        {
 
230
        return;
 
231
        }
 
232
 
 
233
    if (matches && !was_match)
 
234
        {
 
235
        if (window_actions & WINDOW_APPEARS)
 
236
            {
 
237
            windows_handler->set_action_window( window_P );
 
238
            data->execute();
 
239
            }
 
240
        else if (window_actions & WINDOW_ACTIVATES && window_P == windows_handler->active_window())
 
241
            {
 
242
            windows_handler->set_action_window( window_P );
 
243
            data->execute();
 
244
            }
 
245
        }
 
246
    }
 
247
 
 
248
 
 
249
const Windowdef_list* WindowTrigger::windows() const
 
250
    {
 
251
    return _windows;
 
252
    }
 
253
 
 
254
 
 
255
Windowdef_list* WindowTrigger::windows()
 
256
    {
 
257
    return _windows;
 
258
    }
 
259
 
 
260
 
 
261
WindowTrigger* WindowTrigger::copy( ActionData* data_P ) const
 
262
    {
 
263
    WindowTrigger* ret = new WindowTrigger( data_P ? data_P : data, windows()->copy(),
 
264
        window_actions );
 
265
    ret->existing_windows = existing_windows; // CHECKME je tohle vazne treba ?
 
266
    return ret;
 
267
    }
 
268
 
 
269
} // namespace KHotKeys
 
270