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().toList(); | 57 | m_info = parser.fromJson(file.readAll()).toVariant().toList(); | ||
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 | { | 102 | { | ||
101 | return getOutputRetention(output->hash(), output->name()); | | |||
102 | } | | |||
103 | | ||||
104 | Control::OutputRetention ControlConfig::getOutputRetention(const QString &outputId, const QString &outputName) const | | |||
105 | { | | |||
106 | for (const auto variantInfo : m_info) { | | |||
107 | const QVariantMap info = variantInfo.toMap(); | | |||
108 | | ||||
109 | const QString outputIdInfo = info[QStringLiteral("id")].toString(); | 103 | const QString outputIdInfo = info[QStringLiteral("id")].toString(); | ||
110 | if (outputIdInfo.isEmpty()) { | 104 | if (outputIdInfo.isEmpty()) { | ||
111 | continue; | 105 | return false; | ||
112 | } | 106 | } | ||
113 | if (outputId != outputIdInfo) { | 107 | if (outputId != outputIdInfo) { | ||
114 | continue; | 108 | return false; | ||
115 | } | 109 | } | ||
116 | 110 | | |||
117 | if (!outputName.isEmpty() && m_duplicateOutputIds.contains(outputId)) { | 111 | if (!outputName.isEmpty() && m_duplicateOutputIds.contains(outputId)) { | ||
118 | // 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 | ||
119 | // 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) | ||
120 | const auto metadata = info[QStringLiteral("metadata")].toMap(); | 114 | const auto metadata = info[QStringLiteral("metadata")].toMap(); | ||
121 | const auto outputNameInfo = metadata[QStringLiteral("name")].toString(); | 115 | const auto outputNameInfo = metadata[QStringLiteral("name")].toString(); | ||
122 | if (outputName != outputNameInfo) { | 116 | if (outputName != outputNameInfo) { | ||
123 | // was a duplicate id, but info not for this output | 117 | // was a duplicate id, but info not for this output | ||
124 | continue; | 118 | return false; | ||
119 | } | ||||
125 | } | 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 | for (const auto variantInfo : m_info) { | ||||
132 | const QVariantMap info = variantInfo.toMap(); | ||||
133 | if (!infoIsOutput(info, outputId, outputName)) { | ||||
134 | continue; | ||||
126 | } | 135 | } | ||
127 | return convertVariantToOutputRetention(info[QStringLiteral("retention")]); | 136 | return convertVariantToOutputRetention(info[QStringLiteral("retention")]); | ||
128 | } | 137 | } | ||
129 | // info for output not found | 138 | // info for output not found | ||
130 | return OutputRetention::Undefined; | 139 | return OutputRetention::Undefined; | ||
131 | } | 140 | } | ||
132 | 141 | | |||
142 | static QVariantMap metadata(const QString &outputName) | ||||
143 | { | ||||
144 | QVariantMap metadata; | ||||
145 | metadata[QStringLiteral("name")] = outputName; | ||||
146 | return metadata; | ||||
147 | } | ||||
148 | | ||||
149 | void ControlConfig::setOutputRetention(const QString &outputId, const QString &outputName, OutputRetention value) | ||||
150 | { | ||||
151 | QList<QVariant>::iterator it; | ||||
152 | for (it = m_info.begin(); it != m_info.end(); ++it) { | ||||
153 | QVariantMap outputInfo = (*it).toMap(); | ||||
154 | if (!infoIsOutput(outputInfo, outputId, outputName)) { | ||||
155 | continue; | ||||
156 | } | ||||
157 | outputInfo[QStringLiteral("retention")] = (int)value; | ||||
158 | *it = outputInfo; | ||||
159 | return; | ||||
160 | } | ||||
161 | // no entry yet, create one | ||||
162 | QVariantMap outputInfo; | ||||
163 | outputInfo[QStringLiteral("id")] = outputId; | ||||
164 | outputInfo[QStringLiteral("metadata")] = metadata(outputName); | ||||
165 | outputInfo[QStringLiteral("retention")] = (int)value; | ||||
166 | m_info << outputInfo; | ||||
167 | } | ||||
168 | | ||||
169 | bool ControlConfig::writeFile() | ||||
170 | { | ||||
171 | // write updated data to file | ||||
172 | QFile file(filePath()); | ||||
173 | if (!file.open(QIODevice::WriteOnly)) { | ||||
174 | // TODO: logging category? | ||||
175 | // qCWarning(KSCREEN_COMMON) << "Failed to open config control file for writing! " << file.errorString(); | ||||
176 | return false; | ||||
177 | } | ||||
178 | file.write(QJsonDocument::fromVariant(m_info).toJson()); | ||||
mart: maybe check if it's actually a valid json document?, even just !document.isEmpty() after… | |||||
179 | // qCDebug(KSCREEN_COMMON) << "Config control saved on: " << file.fileName(); | ||||
180 | return true; | ||||
181 | } | ||||
182 | | ||||
133 | ControlOutput::ControlOutput(KScreen::OutputPtr output) | 183 | ControlOutput::ControlOutput(KScreen::OutputPtr output) | ||
134 | : m_output(output) | 184 | : m_output(output) | ||
135 | { | 185 | { | ||
136 | } | 186 | } | ||
137 | 187 | | |||
138 | QString ControlOutput::filePath(const QString &hash) | 188 | QString ControlOutput::filePath(const QString &hash) | ||
139 | { | 189 | { | ||
140 | const QString dir = dirPath() % QStringLiteral("outputs/"); | 190 | 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