Changeset View
Changeset View
Standalone View
Standalone View
kcmkwin/kwinrules/main.cpp
Show All 17 Lines | |||||
18 | 18 | | |||
19 | #include <QCommandLineParser> | 19 | #include <QCommandLineParser> | ||
20 | #include <QApplication> | 20 | #include <QApplication> | ||
21 | #include <kconfig.h> | 21 | #include <kconfig.h> | ||
22 | #include <KLocalizedString> | 22 | #include <KLocalizedString> | ||
23 | #include <kwindowsystem.h> | 23 | #include <kwindowsystem.h> | ||
24 | 24 | | |||
25 | #include "ruleswidget.h" | 25 | #include "ruleswidget.h" | ||
26 | #include "rulesettingsmanager.h" | ||||
26 | #include "../../rules.h" | 27 | #include "../../rules.h" | ||
27 | #include <QByteArray> | 28 | #include <QByteArray> | ||
28 | 29 | | |||
29 | #include <QDBusConnection> | 30 | #include <QDBusConnection> | ||
30 | #include <QDBusMessage> | 31 | #include <QDBusMessage> | ||
31 | #include <QDBusPendingCallWatcher> | 32 | #include <QDBusPendingCallWatcher> | ||
32 | #include <QDBusPendingReply> | 33 | #include <QDBusPendingReply> | ||
33 | #include <QUuid> | 34 | #include <QUuid> | ||
34 | 35 | | |||
35 | Q_DECLARE_METATYPE(NET::WindowType) | 36 | Q_DECLARE_METATYPE(NET::WindowType) | ||
36 | 37 | | |||
37 | namespace KWin | 38 | namespace KWin | ||
38 | { | 39 | { | ||
39 | 40 | | |||
40 | static void loadRules(QList< Rules* >& rules) | 41 | static Rules *findRule(const QVector<Rules *> &rules, const QVariantMap &data, bool whole_app) | ||
41 | { | | |||
42 | KConfig _cfg("kwinrulesrc", KConfig::NoGlobals); | | |||
43 | KConfigGroup cfg(&_cfg, "General"); | | |||
44 | int count = cfg.readEntry("count", 0); | | |||
45 | for (int i = 1; | | |||
46 | i <= count; | | |||
47 | ++i) { | | |||
48 | cfg = KConfigGroup(&_cfg, QString::number(i)); | | |||
49 | Rules* rule = new Rules(cfg); | | |||
50 | rules.append(rule); | | |||
51 | } | | |||
52 | } | | |||
53 | | ||||
54 | static void saveRules(const QList< Rules* >& rules) | | |||
55 | { | | |||
56 | KConfig cfg("kwinrulesrc", KConfig::NoGlobals); | | |||
57 | QStringList groups = cfg.groupList(); | | |||
58 | for (QStringList::ConstIterator it = groups.constBegin(); | | |||
59 | it != groups.constEnd(); | | |||
60 | ++it) | | |||
61 | cfg.deleteGroup(*it); | | |||
62 | cfg.group("General").writeEntry("count", rules.count()); | | |||
63 | int i = 1; | | |||
64 | for (QList< Rules* >::ConstIterator it = rules.constBegin(); | | |||
65 | it != rules.constEnd(); | | |||
66 | ++it) { | | |||
67 | KConfigGroup cg(&cfg, QString::number(i)); | | |||
68 | (*it)->write(cg); | | |||
69 | ++i; | | |||
70 | } | | |||
71 | } | | |||
72 | | ||||
73 | static Rules* findRule(const QList< Rules* >& rules, const QVariantMap &data, bool whole_app) | | |||
74 | { | 42 | { | ||
75 | QByteArray wmclass_class = data.value("resourceClass").toByteArray().toLower(); | 43 | QByteArray wmclass_class = data.value("resourceClass").toByteArray().toLower(); | ||
76 | QByteArray wmclass_name = data.value("resourceName").toByteArray().toLower(); | 44 | QByteArray wmclass_name = data.value("resourceName").toByteArray().toLower(); | ||
77 | QByteArray role = data.value("role").toByteArray().toLower(); | 45 | QByteArray role = data.value("role").toByteArray().toLower(); | ||
78 | NET::WindowType type = data.value("type").value<NET::WindowType>(); | 46 | NET::WindowType type = data.value("type").value<NET::WindowType>(); | ||
79 | QString title = data.value("caption").toString(); | 47 | QString title = data.value("caption").toString(); | ||
80 | QByteArray machine = data.value("clientMachine").toByteArray(); | 48 | QByteArray machine = data.value("clientMachine").toByteArray(); | ||
81 | Rules* best_match = nullptr; | 49 | Rules* best_match = nullptr; | ||
82 | int match_quality = 0; | 50 | int match_quality = 0; | ||
83 | for (QList< Rules* >::ConstIterator it = rules.constBegin(); | 51 | for (const auto rule : rules) { | ||
84 | it != rules.constEnd(); | | |||
85 | ++it) { | | |||
86 | // try to find an exact match, i.e. not a generic rule | 52 | // try to find an exact match, i.e. not a generic rule | ||
87 | Rules* rule = *it; | | |||
88 | int quality = 0; | 53 | int quality = 0; | ||
89 | bool generic = true; | 54 | bool generic = true; | ||
90 | if (rule->wmclassmatch != Rules::ExactMatch) | 55 | if (rule->wmclassmatch != Rules::ExactMatch) | ||
91 | continue; // too generic | 56 | continue; // too generic | ||
92 | if (!rule->matchWMClass(wmclass_class, wmclass_name)) | 57 | if (!rule->matchWMClass(wmclass_class, wmclass_name)) | ||
93 | continue; | 58 | continue; | ||
94 | // from now on, it matches the app - now try to match for a specific window | 59 | // from now on, it matches the app - now try to match for a specific window | ||
95 | if (rule->wmclasscomplete) { | 60 | if (rule->wmclasscomplete) { | ||
▲ Show 20 Lines • Show All 101 Lines • ▼ Show 20 Line(s) | 151 | } else { | |||
197 | ret->wmclassmatch = Rules::ExactMatch; | 162 | ret->wmclassmatch = Rules::ExactMatch; | ||
198 | } | 163 | } | ||
199 | } | 164 | } | ||
200 | return ret; | 165 | return ret; | ||
201 | } | 166 | } | ||
202 | 167 | | |||
203 | static void edit(const QVariantMap &data, bool whole_app) | 168 | static void edit(const QVariantMap &data, bool whole_app) | ||
204 | { | 169 | { | ||
205 | QList< Rules* > rules; | 170 | QVector<Rules *> rules; | ||
206 | loadRules(rules); | 171 | RuleSettingsManager settings(KConfig::NoGlobals); | ||
172 | settings.loadAllInto(rules); | ||||
207 | Rules* orig_rule = findRule(rules, data, whole_app); | 173 | Rules *orig_rule = findRule(rules, data, whole_app); | ||
208 | RulesDialog dlg; | 174 | RulesDialog dlg; | ||
209 | if (whole_app) | 175 | if (whole_app) | ||
210 | dlg.setWindowTitle(i18nc("Window caption for the application wide rules dialog", "Edit Application-Specific Settings")); | 176 | dlg.setWindowTitle(i18nc("Window caption for the application wide rules dialog", "Edit Application-Specific Settings")); | ||
211 | // dlg.edit() creates new Rules instance if edited | 177 | // dlg.edit() creates new Rules instance if edited | ||
212 | Rules* edited_rule = dlg.edit(orig_rule, data, true); | 178 | Rules* edited_rule = dlg.edit(orig_rule, data, true); | ||
213 | if (edited_rule == nullptr || edited_rule->isEmpty()) { | 179 | if (edited_rule == nullptr || edited_rule->isEmpty()) { | ||
214 | rules.removeAll(orig_rule); | 180 | rules.removeAll(orig_rule); | ||
215 | delete orig_rule; | 181 | delete orig_rule; | ||
216 | if (orig_rule != edited_rule) | 182 | if (orig_rule != edited_rule) | ||
217 | delete edited_rule; | 183 | delete edited_rule; | ||
218 | } else if (edited_rule != orig_rule) { | 184 | } else if (edited_rule != orig_rule) { | ||
219 | int pos = rules.indexOf(orig_rule); | 185 | int pos = rules.indexOf(orig_rule); | ||
220 | if (pos != -1) | 186 | if (pos != -1) | ||
221 | rules[ pos ] = edited_rule; | 187 | rules[ pos ] = edited_rule; | ||
222 | else | 188 | else | ||
223 | rules.prepend(edited_rule); | 189 | rules.prepend(edited_rule); | ||
224 | delete orig_rule; | 190 | delete orig_rule; | ||
225 | } | 191 | } | ||
226 | saveRules(rules); | 192 | settings.saveAllFrom(rules); | ||
227 | // Send signal to all kwin instances | 193 | // Send signal to all kwin instances | ||
228 | QDBusMessage message = | 194 | QDBusMessage message = | ||
229 | QDBusMessage::createSignal("/KWin", "org.kde.KWin", "reloadConfig"); | 195 | QDBusMessage::createSignal("/KWin", "org.kde.KWin", "reloadConfig"); | ||
230 | QDBusConnection::sessionBus().send(message); | 196 | QDBusConnection::sessionBus().send(message); | ||
231 | qApp->quit(); | 197 | qApp->quit(); | ||
232 | } | 198 | } | ||
233 | 199 | | |||
234 | } // namespace | 200 | } // namespace | ||
▲ Show 20 Lines • Show All 50 Lines • Show Last 20 Lines |