-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathPresetManager.cpp
More file actions
308 lines (254 loc) · 7.64 KB
/
PresetManager.cpp
File metadata and controls
308 lines (254 loc) · 7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include "PresetManager.h"
#include "config/AppConfig.h"
#include "config/DspConfig.h"
#include "model/PresetListModel.h"
#include <QFile>
#include <QFileInfo>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
PresetManager::PresetManager() : _presetModel(new PresetListModel(this))
{
loadRules();
loadPresetDevices();
}
bool PresetManager::exists(const QString &name) const
{
return QFile::exists(AppConfig::instance().getPath("presets/") + name + ".conf");
}
bool PresetManager::loadFromPath(const QString &filename)
{
const QString &src = filename;
const QString dest = AppConfig::instance().getDspConfPath();
if (!QFile::exists(src))
{
// Preset does not exist anymore, rescan presets
this->_presetModel->rescan();
return false;
}
if (QFile::exists(dest))
{
QFile::remove(dest);
}
QFile::copy(src, dest);
DspConfig::instance().load();
// If this preset remembers an output device, switch to it. The preset name is
// the file's base name; presets without an association leave the device untouched.
applyPresetDevice(QFileInfo(filename).completeBaseName());
Log::debug("Loaded " + filename);
return true;
}
bool PresetManager::load(const QString &name)
{
return loadFromPath(AppConfig::instance().getPath("presets/") + name + ".conf");
}
void PresetManager::rename(const QString &name, const QString &newName)
{
auto path = AppConfig::instance().getPath("presets/") + name + ".conf";
if (QFile::exists(path))
{
QFile::rename(path, QDir(path).filePath(newName + ".conf"));
}
// Keep the device association in sync with the preset's new name
if (_presetDevices.contains(name))
{
_presetDevices[newName] = _presetDevices.take(name);
savePresetDevices();
}
this->_presetModel->rescan();
}
bool PresetManager::remove(const QString &name)
{
auto path = AppConfig::instance().getPath("presets/") + name + ".conf";
if (QFile::exists(path))
{
QFile::remove(path);
clearPresetDevice(name);
this->_presetModel->rescan();
return true;
}
return false;
}
void PresetManager::save(const QString &name)
{
saveToPath(AppConfig::instance().getPath("presets/") + name + ".conf");
}
void PresetManager::saveToPath(const QString &filename)
{
emit wantsToWriteConfig();
const QString src = AppConfig::instance().getDspConfPath();
const QString &dest = filename;
if (QFile::exists(dest))
{
QFile::remove(dest);
}
QFile::copy(src, dest);
this->_presetModel->rescan();
Log::debug("Saved to " + filename);
}
void PresetManager::onOutputDeviceChanged(const QString &deviceName, const QString &deviceId, const QString& outputRouteId)
{
QString defaultRouteId = QString::fromStdString(RouteListModel::makeDefaultRoute().name);
auto executeRule = [this, deviceName, outputRouteId, defaultRouteId](const PresetRule& rule){
// A rule loads a preset *because* the device changed; do not let the loaded
// preset re-apply a device, which would feed back into this handler.
_suppressDeviceApply = true;
loadFromPath(AppConfig::instance().getPath("presets/" + rule.preset + ".conf"));
_suppressDeviceApply = false;
emit presetAutoloaded(deviceName, rule.routeName, rule.routeId == defaultRouteId);
};
// Look for rule with route
for(const auto& rule : std::as_const(_rules))
{
if(rule.deviceId == deviceId && rule.routeId == outputRouteId)
{
executeRule(rule);
return;
}
}
// Fall back to rule with wildcard route
for(const auto& rule : std::as_const(_rules))
{
if(rule.deviceId == deviceId && rule.routeId == defaultRouteId)
{
executeRule(rule);
return;
}
}
}
QString PresetManager::rulesPath() const
{
return AppConfig::instance().getPath("preset_rules.json");
}
void PresetManager::loadRules()
{
_rules.clear();
QFile indexJson(rulesPath());
if(!indexJson.exists())
{
return;
}
indexJson.open(QFile::ReadOnly);
QJsonDocument d = QJsonDocument::fromJson(indexJson.readAll());
QJsonArray root = d.array();
for(const auto& item : root)
{
_rules.append(PresetRule(item.toObject()));
}
indexJson.close();
}
void PresetManager::saveRules() const
{
QFile json(rulesPath());
if(!json.open(QIODevice::WriteOnly)){
Log::error("PresetRuleTableModel::save: Cannot open json file");
return;
}
QJsonArray root;
for(const auto& item : _rules)
{
root.append(item.toJson());
}
json.write(QJsonDocument(root).toJson(QJsonDocument::Indented));
json.close();
}
PresetListModel *PresetManager::presetModel() const
{
return _presetModel;
}
void PresetManager::setRules(const QVector<PresetRule> &newRules)
{
_rules = newRules;
saveRules();
}
bool PresetManager::addRule(const PresetRule &rule)
{
// Only one rule per device & route id
removeRule(rule.deviceId, rule.routeId);
_rules.append(rule);
saveRules();
return true;
}
void PresetManager::removeRule(const QString &deviceId, const QString &routeId)
{
for(int i = 0; i < _rules.count(); i++) {
if(_rules[i].deviceId == deviceId && _rules[i].routeId == routeId) {
_rules.removeAt(i);
break;
}
}
saveRules();
}
QVector<PresetRule> PresetManager::rules() const
{
return _rules;
}
QString PresetManager::presetDevicesPath() const
{
return AppConfig::instance().getPath("preset_devices.json");
}
void PresetManager::loadPresetDevices()
{
_presetDevices.clear();
QFile json(presetDevicesPath());
if(!json.exists())
{
return;
}
json.open(QFile::ReadOnly);
QJsonObject root = QJsonDocument::fromJson(json.readAll()).object();
json.close();
for(auto it = root.constBegin(); it != root.constEnd(); ++it)
{
_presetDevices.insert(it.key(), it.value().toObject());
}
}
void PresetManager::savePresetDevices()
{
QFile json(presetDevicesPath());
if(!json.open(QIODevice::WriteOnly)){
Log::error("PresetManager::savePresetDevices: Cannot open json file");
return;
}
QJsonObject root;
for(auto it = _presetDevices.constBegin(); it != _presetDevices.constEnd(); ++it)
{
root.insert(it.key(), it.value());
}
json.write(QJsonDocument(root).toJson(QJsonDocument::Indented));
json.close();
}
bool PresetManager::hasPresetDevice(const QString &name) const
{
return _presetDevices.contains(name);
}
void PresetManager::setPresetDevice(const QString &name)
{
QJsonObject entry;
entry["deviceId"] = AppConfig::instance().get<QString>(AppConfig::AudioOutputDevice);
entry["useDefault"] = AppConfig::instance().get<bool>(AppConfig::AudioOutputUseDefault);
_presetDevices[name] = entry;
savePresetDevices();
}
void PresetManager::clearPresetDevice(const QString &name)
{
if(_presetDevices.remove(name) > 0)
{
savePresetDevices();
}
}
void PresetManager::applyPresetDevice(const QString &name)
{
if(_suppressDeviceApply || !_presetDevices.contains(name))
{
return;
}
const QJsonObject entry = _presetDevices.value(name);
// Mirror SettingsFragment: setting these AppConfig keys triggers a live device
// switch via PipewireAudioService::onAppConfigUpdated.
AppConfig::instance().set(AppConfig::AudioOutputUseDefault, entry["useDefault"].toBool());
if(!entry["useDefault"].toBool())
{
AppConfig::instance().set(AppConfig::AudioOutputDevice, entry["deviceId"].toString());
}
}