File indexing completed on 2024-05-05 17:34:14

0001 /* SPDX-FileCopyrightText: 1999-2001 Lubos Lunak <l.lunak@kde.org>
0002    SPDX-FileCopyrightText: 2008 Michael Jansen <kde@michael-jansen.biz>
0003 
0004    SPDX-License-Identifier: GPL-2.0-only
0005 */
0006 
0007 #include "windows_helper/window_selection_rules.h"
0008 
0009 #include <KLocalizedString>
0010 #include <QDebug>
0011 
0012 namespace KHotKeys
0013 {
0014 Windowdef_simple::Windowdef_simple(const QString &comment_P,
0015                                    const QString &title_P,
0016                                    substr_type_t title_type_P,
0017                                    const QString &wclass_P,
0018                                    substr_type_t wclass_type_P,
0019                                    const QString &role_P,
0020                                    substr_type_t role_type_P,
0021                                    int window_types_P)
0022     : Windowdef(comment_P)
0023     , _title(title_P)
0024     , _title_match_type(title_type_P)
0025     , _wclass(wclass_P)
0026     , _wclass_match_type(wclass_type_P)
0027     , _role(role_P)
0028     , _role_match_type(role_type_P)
0029     , _window_types(window_types_P)
0030 {
0031 }
0032 
0033 Windowdef_simple::Windowdef_simple(KConfigGroup &cfg_P)
0034     : Windowdef(cfg_P)
0035 {
0036     _title = cfg_P.readEntry("Title");
0037     _title_match_type = static_cast<substr_type_t>(cfg_P.readEntry("TitleType", 0));
0038     _wclass = cfg_P.readEntry("Class");
0039     _wclass_match_type = static_cast<substr_type_t>(cfg_P.readEntry("ClassType", 0));
0040     _role = cfg_P.readEntry("Role");
0041     _role_match_type = static_cast<substr_type_t>(cfg_P.readEntry("RoleType", 0));
0042     _window_types = cfg_P.readEntry("WindowTypes", 0);
0043 }
0044 
0045 void Windowdef_simple::cfg_write(KConfigGroup &cfg_P) const
0046 {
0047     base::cfg_write(cfg_P);
0048     cfg_P.writeEntry("Title", title());
0049     cfg_P.writeEntry("TitleType", int(_title_match_type));
0050     cfg_P.writeEntry("Class", wclass());
0051     cfg_P.writeEntry("ClassType", int(_wclass_match_type));
0052     cfg_P.writeEntry("Role", role());
0053     cfg_P.writeEntry("RoleType", int(_role_match_type));
0054     cfg_P.writeEntry("WindowTypes", window_types());
0055     cfg_P.writeEntry("Type", "SIMPLE"); // overwrites value set in base::cfg_write()
0056 }
0057 
0058 Windowdef_simple *Windowdef_simple::copy() const
0059 {
0060     return new Windowdef_simple(comment(), title(), title_match_type(), wclass(), wclass_match_type(), role(), role_match_type(), window_types());
0061 }
0062 
0063 const QString Windowdef_simple::description() const
0064 {
0065     return i18n("Window simple: ") + comment();
0066 }
0067 
0068 bool Windowdef_simple::is_substr_match(const QString &str1_P, const QString &str2_P, substr_type_t type_P)
0069 {
0070     switch (type_P) {
0071     case NOT_IMPORTANT:
0072         return true;
0073     case CONTAINS:
0074         return str1_P.contains(str2_P) > 0;
0075     case IS:
0076         return str1_P == str2_P;
0077     case REGEXP: {
0078         QRegExp rg(str2_P);
0079         return rg.indexIn(str1_P) >= 0;
0080     }
0081     case CONTAINS_NOT:
0082         return str1_P.contains(str2_P) == 0;
0083     case IS_NOT:
0084         return str1_P != str2_P;
0085     case REGEXP_NOT: {
0086         QRegExp rg(str2_P);
0087         return rg.indexIn(str1_P) < 0;
0088     }
0089     }
0090     return false;
0091 }
0092 
0093 bool Windowdef_simple::match(const Window_data &window_P)
0094 {
0095     if (!type_match(window_P.type))
0096         return false;
0097     if (!is_substr_match(window_P.title, title(), _title_match_type))
0098         return false;
0099     if (!is_substr_match(window_P.wclass, wclass(), _wclass_match_type))
0100         return false;
0101     if (!is_substr_match(window_P.role, role(), _role_match_type))
0102         return false;
0103     qDebug() << "window match:" << window_P.title << ":OK";
0104     return true;
0105 }
0106 
0107 const QString &Windowdef_simple::role() const
0108 {
0109     return _role;
0110 }
0111 
0112 Windowdef_simple::substr_type_t Windowdef_simple::role_match_type() const
0113 {
0114     return _role_match_type;
0115 }
0116 
0117 void Windowdef_simple::set_title(const QString &title)
0118 {
0119     _title = title;
0120 }
0121 
0122 void Windowdef_simple::set_title_match_type(const substr_type_t &type)
0123 {
0124     _title_match_type = type;
0125 }
0126 
0127 void Windowdef_simple::set_role(const QString &role)
0128 {
0129     _role = role;
0130 }
0131 
0132 void Windowdef_simple::set_role_match_type(const substr_type_t &type)
0133 {
0134     _role_match_type = type;
0135 }
0136 
0137 void Windowdef_simple::set_window_types(const int types)
0138 {
0139     _window_types = types;
0140 }
0141 
0142 void Windowdef_simple::set_wclass(const QString &wclass)
0143 {
0144     _wclass = wclass;
0145 }
0146 
0147 void Windowdef_simple::set_wclass_match_type(const substr_type_t &type)
0148 {
0149     _wclass_match_type = type;
0150 }
0151 
0152 const QString &Windowdef_simple::title() const
0153 {
0154     return _title;
0155 }
0156 
0157 Windowdef_simple::substr_type_t Windowdef_simple::title_match_type() const
0158 {
0159     return _title_match_type;
0160 }
0161 
0162 bool Windowdef_simple::type_match(window_type_t type_P) const
0163 {
0164     return window_types() & type_P;
0165 }
0166 
0167 bool Windowdef_simple::type_match(NET::WindowType type_P) const
0168 {
0169     return (window_types() & (1 << type_P)) || (type_P == NET::Unknown && (window_types() & WINDOW_TYPE_NORMAL));
0170     // CHECKME HACK haaaack !
0171 }
0172 
0173 const QString &Windowdef_simple::wclass() const
0174 {
0175     return _wclass;
0176 }
0177 
0178 Windowdef_simple::substr_type_t Windowdef_simple::wclass_match_type() const
0179 {
0180     return _wclass_match_type;
0181 }
0182 
0183 int Windowdef_simple::window_types() const
0184 {
0185     return _window_types;
0186 }
0187 
0188 } // namespace KHotKeys