diff --git a/kwin/kcmkwin/kwinrules/main.cpp b/kwin/kcmkwin/kwinrules/main.cpp index 1674a271..9324c89b 100644 --- a/kwin/kcmkwin/kwinrules/main.cpp +++ b/kwin/kcmkwin/kwinrules/main.cpp @@ -20,9 +20,9 @@ #include #include #include -#include #include -#include +#include +#include #include #include @@ -35,58 +35,6 @@ namespace KWin { -static void loadRules(QList< Rules* >& rules) -{ - QList ruleids; - KConfig _cfg("kwinrulesrc"); - KConfigGroup cfg(&_cfg, "General"); - int count = cfg.readEntry("count", 0); - for (int i = 1; i <= count; ++i) { - cfg = KConfigGroup(&_cfg, QString::number(i)); - const QByteArray id = cfg.readEntry("id", QByteArray()); - if (ruleids.contains(id)) { - continue; - } - Rules* rule = new Rules(cfg); - rules.append(rule); - ruleids.append(id); - } - const QStringList kwinrules = KGlobal::dirs()->findAllResources("data", "kwin/default_rules/*.kwinrules"); - foreach (const QString &kwinrule, kwinrules) { - KConfig cfg(kwinrule, KConfig::NoGlobals); - count = cfg.group("General").readEntry("count", 0); - for (int i = 1; i <= count; ++i) { - KConfigGroup cg(&cfg, QString::number(i)); - const QByteArray id = cg.readEntry("id", QByteArray()); - if (ruleids.contains(id)) { - continue; - } - Rules* rule = new Rules(cg); - rules.append(rule); - ruleids.append(id); - } - } -} - -static void saveRules(const QList< Rules* >& rules) -{ - KConfig cfg("kwinrulesrc"); - QStringList groups = cfg.groupList(); - for (QStringList::ConstIterator it = groups.constBegin(); - it != groups.constEnd(); - ++it) - cfg.deleteGroup(*it); - cfg.group("General").writeEntry("count", rules.count()); - int i = 1; - for (QList< Rules* >::ConstIterator it = rules.constBegin(); - it != rules.constEnd(); - ++it) { - KConfigGroup cg(&cfg, QString::number(i)); - (*it)->write(cg); - ++i; - } -} - static Rules* findRule(const QList< Rules* >& rules, Window wid, bool whole_app) { KWindowInfo info = KWindowSystem::windowInfo(wid, @@ -229,7 +177,7 @@ static Rules* findRule(const QList< Rules* >& rules, Window wid, bool whole_app) static int edit(Window wid, bool whole_app) { QList< Rules* > rules; - loadRules(rules); + Rules::loadRules(rules); Rules* orig_rule = findRule(rules, wid, whole_app); RulesDialog dlg; if (whole_app) @@ -249,7 +197,7 @@ static int edit(Window wid, bool whole_app) rules.prepend(edited_rule); delete orig_rule; } - saveRules(rules); + Rules::saveRules(rules); // Send signal to all kwin instances QDBusMessage message = QDBusMessage::createSignal("/KWin", "org.kde.KWin", "reloadConfig"); diff --git a/kwin/kcmkwin/kwinrules/ruleslist.cpp b/kwin/kcmkwin/kwinrules/ruleslist.cpp index e461e151..0c70f94b 100644 --- a/kwin/kcmkwin/kwinrules/ruleslist.cpp +++ b/kwin/kcmkwin/kwinrules/ruleslist.cpp @@ -68,7 +68,7 @@ KCMRulesList::KCMRulesList(QWidget* parent) KCMRulesList::~KCMRulesList() { - for (QVector< Rules* >::Iterator it = rules.begin(); + for (QList< Rules* >::Iterator it = rules.begin(); it != rules.end(); ++it) delete *it; @@ -190,7 +190,7 @@ void KCMRulesList::importClicked() if (rules[i]->description == new_rule->description) { delete rules[i]; if (remove) { - rules.remove(i); + rules.removeAt(i); delete rules_listbox->takeItem(i); delete new_rule; pos = qMax(0, rules_listbox->currentRow()); // might have changed! @@ -220,40 +220,12 @@ void KCMRulesList::importClicked() void KCMRulesList::load() { rules_listbox->clear(); - for (QVector< Rules* >::Iterator it = rules.begin(); it != rules.end(); ++it) + for (QList< Rules* >::Iterator it = rules.begin(); it != rules.end(); ++it) delete *it; rules.clear(); - QList ruleids; - KConfig _cfg("kwinrulesrc"); - KConfigGroup cfg(&_cfg, "General"); - int count = cfg.readEntry("count", 0); - rules.reserve(count); - for (int i = 1; i <= count; ++i) { - cfg = KConfigGroup(&_cfg, QString::number(i)); - const QByteArray id = cfg.readEntry("id", QByteArray()); - if (ruleids.contains(id)) { - continue; - } - Rules* rule = new Rules(cfg); - rules.append(rule); + Rules::loadRules(rules); + foreach (const Rules* rule, rules) { rules_listbox->addItem(rule->description); - ruleids.append(id); - } - const QStringList kwinrules = KGlobal::dirs()->findAllResources("data", "kwin/default_rules/*.kwinrules"); - foreach (const QString &kwinrule, kwinrules) { - KConfig cfg(kwinrule, KConfig::NoGlobals); - count = cfg.group("General").readEntry("count", 0); - for (int i = 1; i <= count; ++i) { - KConfigGroup cg(&cfg, QString::number(i)); - const QByteArray id = cg.readEntry("id", QByteArray()); - if (ruleids.contains(id)) { - continue; - } - Rules* rule = new Rules(cg); - rules.append(rule); - rules_listbox->addItem(rule->description); - ruleids.append(id); - } } if (rules.count() > 0) rules_listbox->setCurrentItem(rules_listbox->item(0)); @@ -264,21 +236,7 @@ void KCMRulesList::load() void KCMRulesList::save() { - KConfig cfg(QLatin1String("kwinrulesrc")); - QStringList groups = cfg.groupList(); - for (QStringList::ConstIterator it = groups.constBegin(); - it != groups.constEnd(); - ++it) - cfg.deleteGroup(*it); - cfg.group("General").writeEntry("count", rules.count()); - int i = 1; - for (QVector< Rules* >::ConstIterator it = rules.constBegin(); - it != rules.constEnd(); - ++it) { - KConfigGroup cg(&cfg, QString::number(i)); - (*it)->write(cg); - ++i; - } + Rules::saveRules(rules); } void KCMRulesList::defaults() diff --git a/kwin/kcmkwin/kwinrules/ruleslist.h b/kwin/kcmkwin/kwinrules/ruleslist.h index b7187837..6d00cecb 100644 --- a/kwin/kcmkwin/kwinrules/ruleslist.h +++ b/kwin/kcmkwin/kwinrules/ruleslist.h @@ -49,7 +49,7 @@ private slots: void importClicked(); void activeChanged(); private: - QVector< Rules* > rules; + QList< Rules* > rules; }; } // namespace diff --git a/kwin/rules.cpp b/kwin/rules.cpp index d538a781..33c2bdac 100644 --- a/kwin/rules.cpp +++ b/kwin/rules.cpp @@ -190,6 +190,68 @@ void Rules::readFromCfg(const KConfigGroup& cfg) READ_SET_RULE(demandattention, , false); } +void Rules::loadRules(QList< Rules* >& rules) +{ + Q_ASSERT(rules.size() == 0); + QList ruleids; + KConfig _cfg("kwinrulesrc"); + KConfigGroup cfg(&_cfg, "General"); + int count = cfg.readEntry("count", 0); + rules.reserve(count); + for (int i = 1; i <= count; ++i) { + cfg = KConfigGroup(&_cfg, QString::number(i)); + const QByteArray id = cfg.readEntry("id", QByteArray()); + if (ruleids.contains(id)) { + continue; + } + Rules* rule = new Rules(cfg); + rules.append(rule); + ruleids.append(id); + } + const QStringList kwinrules = KGlobal::dirs()->findAllResources("data", "kwin/default_rules/*.kwinrules"); + foreach (const QString &kwinrule, kwinrules) { + KConfig cfg(kwinrule, KConfig::NoGlobals); + count = cfg.group("General").readEntry("count", 0); + for (int i = 1; i <= count; ++i) { + KConfigGroup cg(&cfg, QString::number(i)); + const QByteArray id = cg.readEntry("id", QByteArray()); + if (ruleids.contains(id)) { + continue; + } + Rules* rule = new Rules(cg); + rules.append(rule); + ruleids.append(id); + } + } +} + +void Rules::saveRules(const QList< Rules* >& rules, const bool temporary) +{ +#ifdef KCMRULES + Q_UNUSED(temporary); +#endif + KConfig cfg("kwinrulesrc"); + QStringList groups = cfg.groupList(); + for (QStringList::ConstIterator it = groups.constBegin(); + it != groups.constEnd(); + ++it) + cfg.deleteGroup(*it); + cfg.group("General").writeEntry("count", rules.count()); + int i = 1; + for (QList< Rules* >::ConstIterator it = rules.constBegin(); + it != rules.constEnd(); + ++it) { +#ifndef KCMRULES + if (!temporary && (*it)->isTemporary()) + continue; +#endif + KConfigGroup cg(&cfg, QString::number(i)); + (*it)->write(cg); + ++i; + } +} + + #undef READ_MATCH_STRING #undef READ_SET_RULE #undef READ_FORCE_RULE @@ -979,56 +1041,13 @@ void RuleBook::edit(Client* c, bool whole_app) void RuleBook::load() { deleteAll(); - QList ruleids; - KConfig cfg("kwinrulesrc", KConfig::NoGlobals); - int count = cfg.group("General").readEntry("count", 0); - for (int i = 1; i <= count; ++i) { - KConfigGroup cg(&cfg, QString::number(i)); - const QByteArray id = cg.readEntry("id", QByteArray()); - if (ruleids.contains(id)) { - continue; - } - Rules* rule = new Rules(cg); - m_rules.append(rule); - ruleids.append(id); - } - const QStringList kwinrules = KGlobal::dirs()->findAllResources("data", "kwin/default_rules/*.kwinrules"); - foreach (const QString &kwinrule, kwinrules) { - KConfig cfg(kwinrule, KConfig::NoGlobals); - count = cfg.group("General").readEntry("count", 0); - for (int i = 1; i <= count; ++i) { - KConfigGroup cg(&cfg, QString::number(i)); - const QByteArray id = cg.readEntry("id", QByteArray()); - if (ruleids.contains(id)) { - continue; - } - Rules* rule = new Rules(cg); - m_rules.append(rule); - ruleids.append(id); - } - } + Rules::loadRules(m_rules); } void RuleBook::save() { m_updateTimer->stop(); - KConfig cfg("kwinrulesrc", KConfig::NoGlobals); - QStringList groups = cfg.groupList(); - for (QStringList::ConstIterator it = groups.constBegin(); - it != groups.constEnd(); - ++it) - cfg.deleteGroup(*it); - cfg.group("General").writeEntry("count", m_rules.count()); - int i = 1; - for (QList< Rules* >::ConstIterator it = m_rules.constBegin(); - it != m_rules.constEnd(); - ++it) { - if ((*it)->isTemporary()) - continue; - KConfigGroup cg(&cfg, QString::number(i)); - (*it)->write(cg); - ++i; - } + Rules::saveRules(m_rules, false); } void RuleBook::temporaryRulesMessage(const QString& message) diff --git a/kwin/rules.h b/kwin/rules.h index bb95a74a..62f859be 100644 --- a/kwin/rules.h +++ b/kwin/rules.h @@ -43,17 +43,18 @@ class Client; class Rules; #ifndef KCMRULES // only for kwin core - class WindowRules : public KDecorationDefines { public: explicit WindowRules(const QVector< Rules* >& rules); WindowRules(); + void update(Client*, int selection); void discardTemporary(); bool contains(const Rules* rule) const; void remove(Rules* rule); + Placement::Policy checkPlacement(Placement::Policy placement) const; QRect checkGeometry(QRect rect, bool init = false) const; // use 'invalidPoint' with checkPosition, unlike QSize() and QRect(), QPoint() is a valid point @@ -88,13 +89,14 @@ public: QString checkShortcut(QString s, bool init = false) const; bool checkDisableGlobalShortcuts(bool disable) const; bool checkDemandAttention(bool demand, bool init = false) const; + private: MaximizeMode checkMaximizeVert(MaximizeMode mode, bool init) const; MaximizeMode checkMaximizeHoriz(MaximizeMode mode, bool init) const; + QVector< Rules* > rules; }; - -#endif +#endif // KCMRULES class Rules : public KDecorationDefines @@ -103,6 +105,7 @@ public: Rules(); explicit Rules(const KConfigGroup&); Rules(const QString&, bool temporary); + enum Type { Position = 1<<0, Size = 1<<1, Desktop = 1<<2, MaximizeVert = 1<<3, MaximizeHoriz = 1<<4, Minimize = 1<<5, @@ -112,6 +115,10 @@ public: Screen = 1<<16, DemandAttention = 1<<17, All = 0xffffffff }; Q_DECLARE_FLAGS(Types, Type) + + static void loadRules(QList< Rules* >& rules); + static void saveRules(const QList< Rules* >& rules, bool temporary = true); + void write(KConfigGroup&) const; bool isEmpty() const; #ifndef KCMRULES @@ -120,6 +127,7 @@ public: bool update(Client*, int selection); bool isTemporary() const; bool discardTemporary(bool force); // removes if temporary and forced or too old + bool applyPlacement(Placement::Policy& placement) const; bool applyGeometry(QRect& rect, bool init) const; // use 'invalidPoint' with applyPosition, unlike QSize() and QRect(), QPoint() is a valid point @@ -155,13 +163,15 @@ public: bool applyShortcut(QString& shortcut, bool init) const; bool applyDisableGlobalShortcuts(bool& disable) const; bool applyDemandAttention(bool& demand, bool init) const; + private: -#endif +#endif // KCMRULES bool matchType(NET::WindowType match_type) const; bool matchWMClass(const QByteArray& match_class, const QByteArray& match_name) const; bool matchRole(const QByteArray& match_role) const; bool matchTitle(const QString& match_title) const; bool matchClientMachine(const QByteArray& match_machine, bool local) const; + // All these values are saved to the cfg file, and are also used in kstart! enum { Unused = 0, @@ -172,14 +182,17 @@ private: ApplyNow, // apply immediatelly, then forget the setting ForceTemporarily // apply and force until the window is withdrawn }; + enum SetRule { UnusedSetRule = Unused, SetRuleDummy = 256 // so that it's at least short int }; + enum ForceRule { UnusedForceRule = Unused, ForceRuleDummy = 256 // so that it's at least short int }; + enum StringMatch { FirstStringMatch, UnimportantMatch = FirstStringMatch, @@ -188,6 +201,7 @@ private: RegExpMatch, LastStringMatch = RegExpMatch }; + void readFromCfg(const KConfigGroup& cfg); static SetRule readSetRule(const KConfigGroup&, const QString& key); static ForceRule readForceRule(const KConfigGroup&, const QString& key); @@ -198,6 +212,7 @@ private: static bool checkSetStop(SetRule rule); static bool checkForceStop(ForceRule rule); #endif + int temporary_state; // e.g. for kstart QByteArray ruleid; QString description; @@ -277,6 +292,7 @@ private: ForceRule disableglobalshortcutsrule; bool demandattention; SetRule demandattentionrule; + friend QDebug& operator<<(QDebug& stream, const Rules*); }; @@ -286,6 +302,7 @@ class RuleBook : public QObject Q_OBJECT public: virtual ~RuleBook(); + WindowRules find(const Client*, bool); void discardUsed(Client* c, bool withdraw); void setUpdatesDisabled(bool disable); @@ -293,6 +310,7 @@ public: void load(); void edit(Client* c, bool whole_app); void requestDiskStorage(); + private Q_SLOTS: void temporaryRulesMessage(const QString&); void cleanupTemporaryRules(); @@ -367,8 +385,7 @@ void WindowRules::remove(Rules* rule) if (pos != rules.end()) rules.erase(pos); } - -#endif +#endif // KCMRULES QDebug& operator<<(QDebug& stream, const Rules*);