Phosphor
Qt6 / Wayland library suite for window-management tools
Loading...
Searching...
No Matches
RuleSet.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include <QJsonObject>
7#include <QList>
8#include <QString>
9#include <QUuid>
10
11#include <optional>
12
13#include "Rule.h"
14#include "phosphorrules_export.h"
15
16namespace PhosphorRules {
17
32class PHOSPHORRULES_EXPORT RuleSet
33{
34public:
36 static constexpr int SchemaVersion = 4;
37
38 RuleSet() = default;
39
40 // ── Accessors ──
41 const QList<Rule>& rules() const
42 {
43 return m_rules;
44 }
45 int count() const
46 {
47 return m_rules.size();
48 }
49 bool isEmpty() const
50 {
51 return m_rules.isEmpty();
52 }
53
56 quint64 revision() const
57 {
58 return m_revision;
59 }
60
62 std::optional<Rule> ruleById(const QUuid& id) const;
63
64 // ── Mutators — each bumps the revision ──
65
68 bool addRule(const Rule& rule);
69
72 bool updateRule(const Rule& rule);
73
75 bool removeRule(const QUuid& id);
76
84 int setRules(const QList<Rule>& rules);
85
88 void clear();
89
90 // ── Serialization ──
91
92 QJsonObject toJson() const;
93
97 static std::optional<RuleSet> fromJson(const QJsonObject& obj);
98
102 static std::optional<RuleSet> loadFromFile(const QString& path);
103
106 bool saveToFile(const QString& path) const;
107
115 bool operator==(const RuleSet& other) const
116 {
117 return m_rules == other.m_rules;
118 }
119 bool operator!=(const RuleSet& other) const
120 {
121 return !(*this == other);
122 }
123
124private:
125 QList<Rule> m_rules;
126 quint64 m_revision = 0;
127};
128
129} // namespace PhosphorRules
quint64 revision() const
The monotonic revision — bumped on every mutating call.
Definition RuleSet.h:56
bool updateRule(const Rule &rule)
Replace the rule with the same id.
QJsonObject toJson() const
static std::optional< RuleSet > loadFromFile(const QString &path)
Load from path (an explicit file path — the library never derives config locations).
bool addRule(const Rule &rule)
Append a rule.
bool operator!=(const RuleSet &other) const
Definition RuleSet.h:119
std::optional< Rule > ruleById(const QUuid &id) const
The rule with id, or nullopt if not present.
static constexpr int SchemaVersion
The schema version this library reads and writes.
Definition RuleSet.h:36
bool operator==(const RuleSet &other) const
Equality compares the rule LIST only; revision is intentionally ignored.
Definition RuleSet.h:115
bool removeRule(const QUuid &id)
Remove the rule with id. Returns false if no such rule exists.
void clear()
Drop every rule.
bool saveToFile(const QString &path) const
Canonicalize-on-save: serialize and atomically write to path (temp-file + rename).
static std::optional< RuleSet > fromJson(const QJsonObject &obj)
Strict loader.
int setRules(const QList< Rule > &rules)
Replace the entire ordered rule list.
const QList< Rule > & rules() const
Definition RuleSet.h:41
bool isEmpty() const
Definition RuleSet.h:49
int count() const
Definition RuleSet.h:45
Definition ActionParams.h:25
One rule — { id, name, enabled, priority, match, actions, managed }.
Definition Rule.h:106