on fdd4adc help sign in
unity8/plugins/Unity/Indicators/indicatorsmanager.cpp
321 lines 9.0 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Nick Dedekind <nick.dedekind@canonical.com>
17 */
19#include "indicatorsmanager.h"
21#include <QSettings>
22#include <QDebug>
24#include <paths.h>
27class IndicatorsManager::IndicatorData
29public:
30 IndicatorData(const QString& name, const QFileInfo& fileInfo)
31 : m_name(name)
32 , m_fileInfo(fileInfo)
33 , m_verified (true)
34 {
35 }
37 QString m_name;
38 QFileInfo m_fileInfo;
40 bool m_verified;
41 Indicator::Ptr m_indicator;
42};
44IndicatorsManager::IndicatorsManager(QObject* parent)
45 : QObject(parent)
46 , m_loaded(false)
47 , m_profile(QStringLiteral("phone"))
51IndicatorsManager::~IndicatorsManager()
53 unload();
56void IndicatorsManager::load()
58 unload();
60 const QStringList xdgLocations = shellDataDirs();
62 m_fsWatcher.reset(new QFileSystemWatcher(this));
64 Q_FOREACH(const QString& xdgLocation, xdgLocations)
65 {
66 const QString indicator_path = QDir::cleanPath(xdgLocation + "/unity/indicators");
67 QDir indicator_dir(indicator_path);
68 if (indicator_dir.exists())
69 {
70 // watch folder for changes.
71 m_fsWatcher->addPath(indicator_path);
73 loadDir(indicator_dir);
74 }
75 }
77 QObject::connect(m_fsWatcher.data(), &QFileSystemWatcher::directoryChanged, this, &IndicatorsManager::onDirectoryChanged);
78 QObject::connect(m_fsWatcher.data(), &QFileSystemWatcher::fileChanged, this, &IndicatorsManager::onFileChanged);
79 setLoaded(true);
82void IndicatorsManager::onDirectoryChanged(const QString& directory)
84 loadDir(QDir(directory));
87void IndicatorsManager::onFileChanged(const QString& file)
89 QFileInfo file_info(file);
90 if (!file_info.exists())
91 {
92 unloadFile(file_info);
93 return;
94 }
95 else
96 {
97 loadFile(QFileInfo(file));
98 }
101void IndicatorsManager::loadDir(const QDir& dir)
103 startVerify(dir.canonicalPath());
105 const QFileInfoList indicator_files = dir.entryInfoList(QStringList(), QDir::Files|QDir::NoDotAndDotDot);
106 Q_FOREACH(const QFileInfo& indicator_file, indicator_files)
107 {
108 loadFile(indicator_file);
109 }
111 endVerify(dir.canonicalPath());
114void IndicatorsManager::loadFile(const QFileInfo& file_info)
116 QSettings indicator_settings(file_info.absoluteFilePath(), QSettings::IniFormat, this);
117 const QString name = indicator_settings.value(QStringLiteral("Indicator Service/Name")).toString();
119 qDebug() << "!!! Loading indicator" << name << ", profile" << m_profile;
121 if (m_platform.isPC() && name == "indicator-keyboard") {
122 return; // convergence: skip this indicator until it works in Mir
123 }
125 auto iter = m_indicatorsData.constFind(name);
126 if (iter != m_indicatorsData.constEnd())
127 {
128 const QString newFileInfoDir = QDir::cleanPath(file_info.canonicalPath());
129 IndicatorData* currentData = (*iter);
130 currentData->m_verified = true;
132 int file_info_location = -1;
133 int current_data_location = -1;
135 const QString currentDataDir = QDir::cleanPath(currentData->m_fileInfo.canonicalPath());
137 // if we've already got this indicator, we need to make sure we're not overwriting data which is
138 // from a lower priority standard path
139 QStringList xdgLocations = shellDataDirs();
140 for (int i = 0; i < xdgLocations.size(); i++)
141 {
142 const QString indicatorDir = QDir::cleanPath(xdgLocations[i] + "/unity/indicators");
144 if (newFileInfoDir == indicatorDir)
145 {
146 file_info_location = i;
147 }
148 if (currentDataDir == indicatorDir)
149 {
150 current_data_location = i;
151 }
153 if (file_info_location != -1 && current_data_location != -1)
154 {
155 break;
156 }
157 }
159 // file location is higher (or of equal) priority. overwrite.
160 if (file_info_location <= current_data_location &&
161 file_info != currentData->m_fileInfo)
162 {
163 currentData->m_fileInfo = file_info;
164 Q_EMIT indicatorLoaded(name);
165 }
166 }
167 else
168 {
169 IndicatorData* data = new IndicatorData(name, file_info);
170 data->m_verified = true;
171 m_indicatorsData[name]= data;
172 Q_EMIT indicatorLoaded(name);
173 }
176void IndicatorsManager::unload()
178 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
179 while(iter.hasNext())
180 {
181 iter.next();
182 Q_EMIT indicatorAboutToBeUnloaded(iter.key());
183 }
185 qDeleteAll(m_indicatorsData);
186 m_indicatorsData.clear();
188 setLoaded(false);
191void IndicatorsManager::unloadFile(const QFileInfo& file)
193 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
194 while(iter.hasNext())
195 {
196 iter.next();
197 IndicatorData* data = iter.value();
198 if (data->m_fileInfo.absoluteFilePath() == file.absoluteFilePath())
199 {
200 if (!data->m_verified)
201 {
202 const QString name = data->m_name;
203 Q_EMIT indicatorAboutToBeUnloaded(name);
205 delete data;
206 iter.remove();
207 }
208 }
209 }
211 setLoaded(m_indicatorsData.size() > 0);
214void IndicatorsManager::setLoaded(bool loaded)
216 if (loaded != m_loaded)
217 {
218 m_loaded = loaded;
219 Q_EMIT loadedChanged(m_loaded);
220 }
223QString IndicatorsManager::profile() const
225 return m_profile;
228void IndicatorsManager::setProfile(const QString& profile)
230 if (m_profile != profile) {
231 m_profile = profile;
232 Q_EMIT profileChanged(m_profile);
233 }
236void IndicatorsManager::startVerify(const QString& path)
238 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
239 while(iter.hasNext())
240 {
241 iter.next();
242 IndicatorData* data = iter.value();
243 if (data->m_fileInfo.canonicalPath() == path)
244 {
245 data->m_verified = false;
246 }
247 }
250void IndicatorsManager::endVerify(const QString& path)
252 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
253 while(iter.hasNext())
254 {
255 iter.next();
256 IndicatorData* data = iter.value();
257 if (data->m_fileInfo.canonicalPath() == path)
258 {
259 if (!data->m_verified)
260 {
261 const QString name = data->m_name;
262 Q_EMIT indicatorAboutToBeUnloaded(name);
264 delete data;
265 iter.remove();
266 }
267 }
268 }
271Indicator::Ptr IndicatorsManager::indicator(const QString& indicator_name)
273 if (!m_indicatorsData.contains(indicator_name))
274 {
275 qWarning() << Q_FUNC_INFO << "Invalid indicator name: " << indicator_name;
276 return Indicator::Ptr();
277 }
279 IndicatorData *data = m_indicatorsData.value(indicator_name);
280 if (data->m_indicator)
281 {
282 return data->m_indicator;
283 }
285 Indicator::Ptr new_indicator(new Indicator(this));
286 data->m_indicator = new_indicator;
287 QSettings settings(data->m_fileInfo.absoluteFilePath(), QSettings::IniFormat, this);
288 new_indicator->init(data->m_fileInfo.fileName(), settings);
290 // convergence:
291 // 1) enable session indicator conditionally, typically when running on desktop
292 // 2) switch the battery/power indicator to desktop mode, can't control brightness for now and phone-on-desktop broken (FIXME)
293 //
294 // The rest of the indicators respect their default profile (which is "phone", even on desktop PCs)
295 if ((new_indicator->identifier() == QStringLiteral("indicator-session") && m_platform.isMultiSession())
296 || (new_indicator->identifier() == QStringLiteral("indicator-power") && m_platform.isPC())) {
297 new_indicator->setProfile("desktop");
298 } else {
299 new_indicator->setProfile(m_profile);
300 }
302 QObject::connect(this, &IndicatorsManager::profileChanged, new_indicator.data(), &Indicator::setProfile);
303 return new_indicator;
306QVector<Indicator::Ptr> IndicatorsManager::indicators()
308 QVector<Indicator::Ptr> list;
309 Q_FOREACH(IndicatorData* data, m_indicatorsData)
310 {
311 Indicator::Ptr ret = indicator(data->m_name);
312 if (ret)
313 list.append(ret);
314 }
315 return list;
318bool IndicatorsManager::isLoaded() const
320 return m_loaded;