Changeset View
Changeset View
Standalone View
Standalone View
common/control.cpp
Show All 20 Lines | |||||
21 | #include <QJsonDocument> | 21 | #include <QJsonDocument> | ||
22 | #include <QDir> | 22 | #include <QDir> | ||
23 | 23 | | |||
24 | #include <kscreen/config.h> | 24 | #include <kscreen/config.h> | ||
25 | #include <kscreen/output.h> | 25 | #include <kscreen/output.h> | ||
26 | 26 | | |||
27 | QString Control::s_dirName = QStringLiteral("control/"); | 27 | QString Control::s_dirName = QStringLiteral("control/"); | ||
28 | 28 | | |||
29 | QString Control::dirPath() | 29 | QString Control::dirPath() const | ||
30 | { | 30 | { | ||
31 | return Globals::dirPath() % s_dirName; | 31 | return Globals::dirPath() % s_dirName; | ||
32 | } | 32 | } | ||
33 | 33 | | |||
34 | QString Control::filePathFromHash(const QString &hash) const | ||||
35 | { | ||||
36 | return dirPath() % hash; | ||||
37 | } | ||||
38 | | ||||
34 | Control::OutputRetention Control::convertVariantToOutputRetention(QVariant variant) | 39 | Control::OutputRetention Control::convertVariantToOutputRetention(QVariant variant) | ||
35 | { | 40 | { | ||
36 | if (variant.canConvert<int>()) { | 41 | if (variant.canConvert<int>()) { | ||
37 | const auto retention = variant.toInt(); | 42 | const auto retention = variant.toInt(); | ||
38 | if (retention == (int)OutputRetention::Global) { | 43 | if (retention == (int)OutputRetention::Global) { | ||
39 | return OutputRetention::Global; | 44 | return OutputRetention::Global; | ||
40 | } | 45 | } | ||
41 | if (retention == (int)OutputRetention::Individual) { | 46 | if (retention == (int)OutputRetention::Individual) { | ||
42 | return OutputRetention::Individual; | 47 | return OutputRetention::Individual; | ||
43 | } | 48 | } | ||
44 | } | 49 | } | ||
45 | return OutputRetention::Undefined; | 50 | return OutputRetention::Undefined; | ||
46 | } | 51 | } | ||
47 | 52 | | |||
48 | ControlConfig::ControlConfig(KScreen::ConfigPtr config) | 53 | ControlConfig::ControlConfig(KScreen::ConfigPtr config) | ||
49 | : m_config(config) | 54 | : m_config(config) | ||
50 | { | 55 | { | ||
51 | // qDebug() << "Looking for control file:" << config->connectedOutputsHash(); | 56 | // qDebug() << "Looking for control file:" << config->connectedOutputsHash(); | ||
52 | QFile file(filePath(config->connectedOutputsHash())); | 57 | QFile file(filePathFromHash(config->connectedOutputsHash())); | ||
53 | if (file.open(QIODevice::ReadOnly)) { | 58 | if (file.open(QIODevice::ReadOnly)) { | ||
54 | // This might not be reached, bus this is ok. The control file will | 59 | // This might not be reached, bus this is ok. The control file will | ||
55 | // eventually be created on first write later on. | 60 | // eventually be created on first write later on. | ||
56 | QJsonDocument parser; | 61 | QJsonDocument parser; | ||
57 | m_info = parser.fromJson(file.readAll()).toVariant().toMap(); | 62 | m_info = parser.fromJson(file.readAll()).toVariant().toMap(); | ||
58 | } | 63 | } | ||
59 | 64 | | |||
60 | // TODO: use a file watcher in case of changes to the control file while | 65 | // TODO: use a file watcher in case of changes to the control file while | ||
Show All 15 Lines | |||||
76 | } | 81 | } | ||
77 | 82 | | |||
78 | // TODO: this is same in Output::readInOutputs of the daemon. Combine? | 83 | // TODO: this is same in Output::readInOutputs of the daemon. Combine? | ||
79 | 84 | | |||
80 | // TODO: connect to outputs added/removed signals and reevaluate duplicate ids | 85 | // TODO: connect to outputs added/removed signals and reevaluate duplicate ids | ||
81 | // in case of such a change while object exists? | 86 | // in case of such a change while object exists? | ||
82 | } | 87 | } | ||
83 | 88 | | |||
84 | QString ControlConfig::filePath(const QString &hash) | 89 | QString ControlConfig::dirPath() const | ||
85 | { | 90 | { | ||
86 | const QString dir = dirPath() % QStringLiteral("configs/"); | 91 | return Control::dirPath() % QStringLiteral("configs/"); | ||
87 | if (!QDir().mkpath(dir)) { | | |||
88 | return QString(); | | |||
89 | } | | |||
90 | return dir % hash; | | |||
91 | } | 92 | } | ||
92 | 93 | | |||
93 | QString ControlConfig::filePath() | 94 | QString ControlConfig::filePath() const | ||
94 | { | 95 | { | ||
95 | if (!m_config) { | 96 | if (!m_config) { | ||
96 | return QString(); | 97 | return QString(); | ||
97 | } | 98 | } | ||
98 | return ControlConfig::filePath(m_config->connectedOutputsHash()); | 99 | return filePathFromHash(m_config->connectedOutputsHash()); | ||
99 | } | 100 | } | ||
100 | 101 | | |||
101 | bool ControlConfig::infoIsOutput(const QVariantMap &info, const QString &outputId, const QString &outputName) const | 102 | bool ControlConfig::infoIsOutput(const QVariantMap &info, const QString &outputId, const QString &outputName) const | ||
102 | { | 103 | { | ||
103 | const QString outputIdInfo = info[QStringLiteral("id")].toString(); | 104 | const QString outputIdInfo = info[QStringLiteral("id")].toString(); | ||
104 | if (outputIdInfo.isEmpty()) { | 105 | if (outputIdInfo.isEmpty()) { | ||
105 | return false; | 106 | return false; | ||
106 | } | 107 | } | ||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Line(s) | 157 | { | |||
174 | outputInfo[QStringLiteral("retention")] = (int)value; | 175 | outputInfo[QStringLiteral("retention")] = (int)value; | ||
175 | 176 | | |||
176 | outputsInfo << outputInfo; | 177 | outputsInfo << outputInfo; | ||
177 | setOutputs(outputsInfo); | 178 | setOutputs(outputsInfo); | ||
178 | } | 179 | } | ||
179 | 180 | | |||
180 | bool ControlConfig::writeFile() | 181 | bool ControlConfig::writeFile() | ||
181 | { | 182 | { | ||
183 | const QString path = filePath(); | ||||
184 | | ||||
185 | if (m_info.isEmpty()) { | ||||
186 | // Nothing to write. Default control. Remove file if it exists. | ||||
187 | QFile::remove(path); | ||||
188 | return true; | ||||
189 | } | ||||
190 | if (!QDir().mkpath(dirPath())) { | ||||
191 | // TODO: error message | ||||
192 | return false; | ||||
193 | } | ||||
194 | | ||||
182 | // write updated data to file | 195 | // write updated data to file | ||
183 | QFile file(filePath()); | 196 | QFile file(path); | ||
184 | if (!file.open(QIODevice::WriteOnly)) { | 197 | if (!file.open(QIODevice::WriteOnly)) { | ||
185 | // TODO: logging category? | 198 | // TODO: logging category? | ||
186 | // qCWarning(KSCREEN_COMMON) << "Failed to open config control file for writing! " << file.errorString(); | 199 | // qCWarning(KSCREEN_COMMON) << "Failed to open config control file for writing! " << file.errorString(); | ||
187 | return false; | 200 | return false; | ||
188 | } | 201 | } | ||
189 | file.write(QJsonDocument::fromVariant(m_info).toJson()); | 202 | file.write(QJsonDocument::fromVariant(m_info).toJson()); | ||
190 | // qCDebug(KSCREEN_COMMON) << "Config control saved on: " << file.fileName(); | 203 | // qCDebug(KSCREEN_COMMON) << "Config control saved on: " << file.fileName(); | ||
191 | return true; | 204 | return true; | ||
Show All 9 Lines | 213 | { | |||
201 | m_info[QStringLiteral("outputs")] = outputsInfo; | 214 | m_info[QStringLiteral("outputs")] = outputsInfo; | ||
202 | } | 215 | } | ||
203 | 216 | | |||
204 | ControlOutput::ControlOutput(KScreen::OutputPtr output) | 217 | ControlOutput::ControlOutput(KScreen::OutputPtr output) | ||
205 | : m_output(output) | 218 | : m_output(output) | ||
206 | { | 219 | { | ||
207 | } | 220 | } | ||
208 | 221 | | |||
209 | QString ControlOutput::filePath(const QString &hash) | 222 | QString ControlOutput::dirPath() const | ||
210 | { | 223 | { | ||
211 | const QString dir = dirPath() % QStringLiteral("outputs/"); | 224 | return Control::dirPath() % QStringLiteral("outputs/"); | ||
davidedmundson: who would now makes this folder?
ControlConfig has a line to do so, but ControlOutput does not. | |||||
ControlOutput is currently not used, just a stub. At some point I want to add the "auto" value for refresh rate and so on to that. romangg: ControlOutput is currently not used, just a stub. At some point I want to add the "auto" value… | |||||
212 | if (!QDir().mkpath(dir)) { | | |||
213 | return QString(); | | |||
214 | } | | |||
215 | return dir % hash; | | |||
216 | } | 225 | } | ||
217 | 226 | | |||
218 | QString ControlOutput::filePath() | 227 | QString ControlOutput::filePath() const | ||
219 | { | 228 | { | ||
220 | if (!m_output) { | 229 | if (!m_output) { | ||
221 | return QString(); | 230 | return QString(); | ||
222 | } | 231 | } | ||
223 | return ControlOutput::filePath(m_output->hash()); | 232 | return filePathFromHash(m_output->hash()); | ||
224 | } | 233 | } |
who would now makes this folder?
ControlConfig has a line to do so, but ControlOutput does not.
I can't see a line in config.cpp (though I'm not 100% sure where ControlOutput is currently used)