File indexing completed on 2024-04-28 16:44:53

0001 /*
0002    SPDX-FileCopyrightText: 1999-2001 Lubos Lunak <l.lunak@kde.org>
0003    SPDX-FileCopyrightText: 2008 Michael Jansen <kde@michael-jansen.biz>
0004 
0005    SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 #include "conditions/existing_window_condition.h"
0008 
0009 #include "windows_helper/window_selection_list.h"
0010 
0011 #include "khotkeysglobal.h"
0012 #include "windows_handler.h"
0013 
0014 namespace KHotKeys
0015 {
0016 Existing_window_condition::Existing_window_condition(KConfigGroup &cfg_P, Condition_list_base *parent_P)
0017     : Condition(cfg_P, parent_P)
0018 {
0019     KConfigGroup windowConfig(cfg_P.config(), cfg_P.name() + "Window");
0020     _window = new Windowdef_list(windowConfig);
0021     init();
0022     set_match();
0023 }
0024 
0025 Existing_window_condition::Existing_window_condition(Windowdef_list *window_P, Condition_list_base *parent_P)
0026     : Condition(parent_P)
0027     , _window(window_P)
0028     , is_match(false)
0029 {
0030     init();
0031     set_match();
0032 }
0033 
0034 Existing_window_condition::~Existing_window_condition()
0035 {
0036     disconnect(windows_handler, nullptr, this, nullptr);
0037     delete _window;
0038 }
0039 
0040 void Existing_window_condition::cfg_write(KConfigGroup &cfg_P) const
0041 {
0042     base::cfg_write(cfg_P);
0043     KConfigGroup windowConfig(cfg_P.config(), cfg_P.name() + "Window");
0044     window()->cfg_write(windowConfig);
0045     cfg_P.writeEntry("Type", "EXISTING_WINDOW"); // overwrites value set in base::cfg_write()
0046 }
0047 
0048 Existing_window_condition *Existing_window_condition::copy() const
0049 {
0050     return new Existing_window_condition(window()->copy());
0051 }
0052 
0053 const QString Existing_window_condition::description() const
0054 {
0055     return i18n("Existing window: ") + window()->comment();
0056 }
0057 
0058 void Existing_window_condition::init()
0059 {
0060     connect(windows_handler, SIGNAL(window_added(WId)), this, SLOT(window_added(WId)));
0061     connect(windows_handler, SIGNAL(window_removed(WId)), this, SLOT(window_removed(WId)));
0062 }
0063 
0064 bool Existing_window_condition::match() const
0065 {
0066     return is_match;
0067 }
0068 
0069 void Existing_window_condition::set_match(WId w_P)
0070 {
0071     if (w_P != None && !is_match)
0072         is_match = window()->match(Window_data(w_P));
0073     else
0074         is_match = windows_handler->find_window(window()) != None;
0075     updated();
0076 }
0077 
0078 const Windowdef_list *Existing_window_condition::window() const
0079 {
0080     return _window;
0081 }
0082 
0083 Windowdef_list *Existing_window_condition::window()
0084 {
0085     return _window;
0086 }
0087 
0088 void Existing_window_condition::window_added(WId w_P)
0089 {
0090     set_match(w_P);
0091 }
0092 
0093 void Existing_window_condition::window_removed(WId)
0094 {
0095     set_match();
0096 }
0097 
0098 } // namespace KHotKeys