2 * Copyright (C) 2013 Canonical, Ltd.
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.
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.
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/>.
16 * Author: Nick Dedekind <nick.dedekind@canonical.com>
19#include "indicatorsmanager.h"
27class IndicatorsManager::IndicatorData
30 IndicatorData(const QString& name, const QFileInfo& fileInfo)
32 , m_fileInfo(fileInfo)
41 Indicator::Ptr m_indicator;
44IndicatorsManager::IndicatorsManager(QObject* parent)
47 , m_profile(QStringLiteral("phone"))
51IndicatorsManager::~IndicatorsManager()
56void IndicatorsManager::load()
60 const QStringList xdgLocations = shellDataDirs();
62 m_fsWatcher.reset(new QFileSystemWatcher(this));
64 Q_FOREACH(const QString& xdgLocation, xdgLocations)
66 const QString indicator_path = QDir::cleanPath(xdgLocation + "/unity/indicators");
67 QDir indicator_dir(indicator_path);
68 if (indicator_dir.exists())
70 // watch folder for changes.
71 m_fsWatcher->addPath(indicator_path);
73 loadDir(indicator_dir);
77 QObject::connect(m_fsWatcher.data(), &QFileSystemWatcher::directoryChanged, this, &IndicatorsManager::onDirectoryChanged);
78 QObject::connect(m_fsWatcher.data(), &QFileSystemWatcher::fileChanged, this, &IndicatorsManager::onFileChanged);
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())
92 unloadFile(file_info);
97 loadFile(QFileInfo(file));
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)
108 loadFile(indicator_file);
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 if (m_platform.isPC() && name == "indicator-keyboard") {
120 return; // convergence: skip this indicator until it works in Mir
123 auto iter = m_indicatorsData.constFind(name);
124 if (iter != m_indicatorsData.constEnd())
126 const QString newFileInfoDir = QDir::cleanPath(file_info.canonicalPath());
127 IndicatorData* currentData = (*iter);
128 currentData->m_verified = true;
130 int file_info_location = -1;
131 int current_data_location = -1;
133 const QString currentDataDir = QDir::cleanPath(currentData->m_fileInfo.canonicalPath());
135 // if we've already got this indicator, we need to make sure we're not overwriting data which is
136 // from a lower priority standard path
137 QStringList xdgLocations = shellDataDirs();
138 for (int i = 0; i < xdgLocations.size(); i++)
140 const QString indicatorDir = QDir::cleanPath(xdgLocations[i] + "/unity/indicators");
142 if (newFileInfoDir == indicatorDir)
144 file_info_location = i;
146 if (currentDataDir == indicatorDir)
148 current_data_location = i;
151 if (file_info_location != -1 && current_data_location != -1)
157 // file location is higher (or of equal) priority. overwrite.
158 if (file_info_location <= current_data_location &&
159 file_info != currentData->m_fileInfo)
161 currentData->m_fileInfo = file_info;
162 Q_EMIT indicatorLoaded(name);
167 IndicatorData* data = new IndicatorData(name, file_info);
168 data->m_verified = true;
169 m_indicatorsData[name]= data;
170 Q_EMIT indicatorLoaded(name);
174void IndicatorsManager::unload()
176 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
177 while(iter.hasNext())
180 Q_EMIT indicatorAboutToBeUnloaded(iter.key());
183 qDeleteAll(m_indicatorsData);
184 m_indicatorsData.clear();
189void IndicatorsManager::unloadFile(const QFileInfo& file)
191 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
192 while(iter.hasNext())
195 IndicatorData* data = iter.value();
196 if (data->m_fileInfo.absoluteFilePath() == file.absoluteFilePath())
198 if (!data->m_verified)
200 const QString name = data->m_name;
201 Q_EMIT indicatorAboutToBeUnloaded(name);
209 setLoaded(m_indicatorsData.size() > 0);
212void IndicatorsManager::setLoaded(bool loaded)
214 if (loaded != m_loaded)
217 Q_EMIT loadedChanged(m_loaded);
221QString IndicatorsManager::profile() const
226void IndicatorsManager::setProfile(const QString& profile)
228 if (m_profile != profile) {
230 Q_EMIT profileChanged(m_profile);
234void IndicatorsManager::startVerify(const QString& path)
236 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
237 while(iter.hasNext())
240 IndicatorData* data = iter.value();
241 if (data->m_fileInfo.canonicalPath() == path)
243 data->m_verified = false;
248void IndicatorsManager::endVerify(const QString& path)
250 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
251 while(iter.hasNext())
254 IndicatorData* data = iter.value();
255 if (data->m_fileInfo.canonicalPath() == path)
257 if (!data->m_verified)
259 const QString name = data->m_name;
260 Q_EMIT indicatorAboutToBeUnloaded(name);
269Indicator::Ptr IndicatorsManager::indicator(const QString& indicator_name)
271 if (!m_indicatorsData.contains(indicator_name))
273 qWarning() << Q_FUNC_INFO << "Invalid indicator name: " << indicator_name;
274 return Indicator::Ptr();
277 IndicatorData *data = m_indicatorsData.value(indicator_name);
278 if (data->m_indicator)
280 return data->m_indicator;
283 Indicator::Ptr new_indicator(new Indicator(this));
284 data->m_indicator = new_indicator;
285 QSettings settings(data->m_fileInfo.absoluteFilePath(), QSettings::IniFormat, this);
286 new_indicator->init(data->m_fileInfo.fileName(), settings);
289 // 1) enable session indicator conditionally, typically when running in a multisession/multiuser environment
290 // 2) on a PC, switch the battery/power indicator to desktop mode,
291 // can't control brightness for now and phone-on-desktop broken (FIXME)
293 // The rest of the indicators respect their default profile (which is "phone", even on desktop PCs)
294 if ((new_indicator->identifier() == QStringLiteral("indicator-session") && m_platform.isMultiSession())
295 || (new_indicator->identifier() == QStringLiteral("indicator-power") && m_platform.isPC())) {
296 new_indicator->setProfile("desktop");
298 new_indicator->setProfile(m_profile);
301 QObject::connect(this, &IndicatorsManager::profileChanged, new_indicator.data(), &Indicator::setProfile);
302 return new_indicator;
305QVector<Indicator::Ptr> IndicatorsManager::indicators()
307 QVector<Indicator::Ptr> list;
308 Q_FOREACH(IndicatorData* data, m_indicatorsData)
310 Indicator::Ptr ret = indicator(data->m_name);
317bool IndicatorsManager::isLoaded() const
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list