Changeset View
Changeset View
Standalone View
Standalone View
common/control.cpp
Show First 20 Lines • Show All 45 Lines • ▼ Show 20 Line(s) | |||||
46 | } | 46 | } | ||
47 | 47 | | |||
48 | ControlConfig::ControlConfig(KScreen::ConfigPtr config) | 48 | ControlConfig::ControlConfig(KScreen::ConfigPtr config) | ||
49 | : m_config(config) | 49 | : m_config(config) | ||
50 | { | 50 | { | ||
51 | // qDebug() << "Looking for control file:" << config->connectedOutputsHash(); | 51 | // qDebug() << "Looking for control file:" << config->connectedOutputsHash(); | ||
52 | QFile file(filePath(config->connectedOutputsHash())); | 52 | QFile file(filePath(config->connectedOutputsHash())); | ||
53 | if (file.open(QIODevice::ReadOnly)) { | 53 | if (file.open(QIODevice::ReadOnly)) { | ||
54 | // This might not be reached, bus this is ok. The control file will | ||||
55 | // eventually be created on first write later on. | ||||
54 | QJsonDocument parser; | 56 | QJsonDocument parser; | ||
55 | m_info = parser.fromJson(file.readAll()).toVariant().toMap(); | 57 | m_info = parser.fromJson(file.readAll()).toVariant().toMap(); | ||
56 | } | 58 | } | ||
57 | 59 | | |||
58 | // TODO: use a file watcher in case of changes to the control file while | 60 | // TODO: use a file watcher in case of changes to the control file while | ||
59 | // object exists? | 61 | // object exists? | ||
60 | 62 | | |||
61 | // As global outputs are indexed by a hash of their edid, which is not unique, | 63 | // As global outputs are indexed by a hash of their edid, which is not unique, | ||
Show All 29 Lines | |||||
91 | QString ControlConfig::filePath() | 93 | QString ControlConfig::filePath() | ||
92 | { | 94 | { | ||
93 | if (!m_config) { | 95 | if (!m_config) { | ||
94 | return QString(); | 96 | return QString(); | ||
95 | } | 97 | } | ||
96 | return ControlConfig::filePath(m_config->connectedOutputsHash()); | 98 | return ControlConfig::filePath(m_config->connectedOutputsHash()); | ||
97 | } | 99 | } | ||
98 | 100 | | |||
99 | Control::OutputRetention ControlConfig::getOutputRetention(const KScreen::OutputPtr &output) const | 101 | bool ControlConfig::infoIsOutput(const QVariantMap &info, const QString &outputId, const QString &outputName) const | ||
100 | { | | |||
101 | return getOutputRetention(output->hash(), output->name()); | | |||
102 | } | | |||
103 | | ||||
104 | Control::OutputRetention ControlConfig::getOutputRetention(const QString &outputId, const QString &outputName) const | | |||
105 | { | 102 | { | ||
106 | const QVariantList outputsInfo = m_info[QStringLiteral("outputs")].toList(); | | |||
107 | for (const auto variantInfo : outputsInfo) { | | |||
108 | const QVariantMap info = variantInfo.toMap(); | | |||
109 | | ||||
110 | const QString outputIdInfo = info[QStringLiteral("id")].toString(); | 103 | const QString outputIdInfo = info[QStringLiteral("id")].toString(); | ||
111 | if (outputIdInfo.isEmpty()) { | 104 | if (outputIdInfo.isEmpty()) { | ||
112 | continue; | 105 | return false; | ||
113 | } | 106 | } | ||
114 | if (outputId != outputIdInfo) { | 107 | if (outputId != outputIdInfo) { | ||
115 | continue; | 108 | return false; | ||
116 | } | 109 | } | ||
117 | 110 | | |||
118 | if (!outputName.isEmpty() && m_duplicateOutputIds.contains(outputId)) { | 111 | if (!outputName.isEmpty() && m_duplicateOutputIds.contains(outputId)) { | ||
119 | // We may have identical outputs connected, these will have the same id in the config | 112 | // We may have identical outputs connected, these will have the same id in the config | ||
120 | // in order to find the right one, also check the output's name (usually the connector) | 113 | // in order to find the right one, also check the output's name (usually the connector) | ||
121 | const auto metadata = info[QStringLiteral("metadata")].toMap(); | 114 | const auto metadata = info[QStringLiteral("metadata")].toMap(); | ||
122 | const auto outputNameInfo = metadata[QStringLiteral("name")].toString(); | 115 | const auto outputNameInfo = metadata[QStringLiteral("name")].toString(); | ||
123 | if (outputName != outputNameInfo) { | 116 | if (outputName != outputNameInfo) { | ||
124 | // was a duplicate id, but info not for this output | 117 | // was a duplicate id, but info not for this output | ||
125 | continue; | 118 | return false; | ||
126 | } | 119 | } | ||
127 | } | 120 | } | ||
121 | return true; | ||||
122 | } | ||||
123 | | ||||
124 | Control::OutputRetention ControlConfig::getOutputRetention(const KScreen::OutputPtr &output) const | ||||
125 | { | ||||
126 | return getOutputRetention(output->hash(), output->name()); | ||||
127 | } | ||||
128 | | ||||
129 | Control::OutputRetention ControlConfig::getOutputRetention(const QString &outputId, const QString &outputName) const | ||||
130 | { | ||||
131 | const QVariantList outputsInfo = getOutputs(); | ||||
132 | for (const auto variantInfo : outputsInfo) { | ||||
133 | const QVariantMap info = variantInfo.toMap(); | ||||
134 | if (!infoIsOutput(info, outputId, outputName)) { | ||||
135 | continue; | ||||
136 | } | ||||
128 | return convertVariantToOutputRetention(info[QStringLiteral("retention")]); | 137 | return convertVariantToOutputRetention(info[QStringLiteral("retention")]); | ||
129 | } | 138 | } | ||
130 | // info for output not found | 139 | // info for output not found | ||
131 | return OutputRetention::Undefined; | 140 | return OutputRetention::Undefined; | ||
132 | } | 141 | } | ||
133 | 142 | | |||
143 | static QVariantMap metadata(const QString &outputName) | ||||
144 | { | ||||
145 | QVariantMap metadata; | ||||
146 | metadata[QStringLiteral("name")] = outputName; | ||||
147 | return metadata; | ||||
148 | } | ||||
149 | | ||||
150 | void ControlConfig::setOutputRetention(const QString &outputId, const QString &outputName, OutputRetention value) | ||||
151 | { | ||||
152 | QList<QVariant>::iterator it; | ||||
153 | QVariantList outputsInfo = getOutputs(); | ||||
154 | | ||||
155 | for (it = outputsInfo.begin(); it != outputsInfo.end(); ++it) { | ||||
156 | QVariantMap outputInfo = (*it).toMap(); | ||||
157 | if (!infoIsOutput(outputInfo, outputId, outputName)) { | ||||
158 | continue; | ||||
159 | } | ||||
160 | outputInfo[QStringLiteral("retention")] = (int)value; | ||||
161 | *it = outputInfo; | ||||
162 | setOutputs(outputsInfo); | ||||
163 | return; | ||||
164 | } | ||||
165 | // no entry yet, create one | ||||
166 | QVariantMap outputInfo; | ||||
167 | outputInfo[QStringLiteral("id")] = outputId; | ||||
168 | outputInfo[QStringLiteral("metadata")] = metadata(outputName); | ||||
169 | outputInfo[QStringLiteral("retention")] = (int)value; | ||||
170 | | ||||
171 | outputsInfo << outputInfo; | ||||
172 | setOutputs(outputsInfo); | ||||
173 | } | ||||
174 | | ||||
175 | bool ControlConfig::writeFile() | ||||
176 | { | ||||
177 | // write updated data to file | ||||
178 | QFile file(filePath()); | ||||
179 | if (!file.open(QIODevice::WriteOnly)) { | ||||
mart: maybe check if it's actually a valid json document?, even just !document.isEmpty() after… | |||||
180 | // TODO: logging category? | ||||
181 | // qCWarning(KSCREEN_COMMON) << "Failed to open config control file for writing! " << file.errorString(); | ||||
182 | return false; | ||||
183 | } | ||||
184 | file.write(QJsonDocument::fromVariant(m_info).toJson()); | ||||
185 | // qCDebug(KSCREEN_COMMON) << "Config control saved on: " << file.fileName(); | ||||
186 | return true; | ||||
187 | } | ||||
188 | | ||||
189 | QVariantList ControlConfig::getOutputs() const | ||||
190 | { | ||||
191 | return m_info[QStringLiteral("outputs")].toList(); | ||||
192 | } | ||||
193 | | ||||
194 | void ControlConfig::setOutputs(QVariantList outputsInfo) | ||||
195 | { | ||||
196 | m_info[QStringLiteral("outputs")] = outputsInfo; | ||||
197 | } | ||||
198 | | ||||
134 | ControlOutput::ControlOutput(KScreen::OutputPtr output) | 199 | ControlOutput::ControlOutput(KScreen::OutputPtr output) | ||
135 | : m_output(output) | 200 | : m_output(output) | ||
136 | { | 201 | { | ||
137 | } | 202 | } | ||
138 | 203 | | |||
139 | QString ControlOutput::filePath(const QString &hash) | 204 | QString ControlOutput::filePath(const QString &hash) | ||
140 | { | 205 | { | ||
141 | const QString dir = dirPath() % QStringLiteral("outputs/"); | 206 | const QString dir = dirPath() % QStringLiteral("outputs/"); | ||
Show All 13 Lines |
maybe check if it's actually a valid json document?, even just !document.isEmpty() after creating the document