2 * Copyright (C) 2013-2016 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/>.
17#include "indicatorsmanager.h"
25class IndicatorsManager::IndicatorData
28 IndicatorData(const QString& name, const QFileInfo& fileInfo)
30 , m_fileInfo(fileInfo)
39 Indicator::Ptr m_indicator;
42IndicatorsManager::IndicatorsManager(QObject* parent)
45 , m_profile(QStringLiteral("phone"))
49IndicatorsManager::~IndicatorsManager()
54void IndicatorsManager::load()
58 const QStringList xdgLocations = shellDataDirs();
60 m_fsWatcher.reset(new QFileSystemWatcher(this));
62 Q_FOREACH(const QString& xdgLocation, xdgLocations)
64 const QString indicator_path = QDir::cleanPath(xdgLocation + "/unity/indicators");
65 QDir indicator_dir(indicator_path);
66 if (indicator_dir.exists())
68 // watch folder for changes.
69 m_fsWatcher->addPath(indicator_path);
71 loadDir(indicator_dir);
75 QObject::connect(m_fsWatcher.data(), &QFileSystemWatcher::directoryChanged, this, &IndicatorsManager::onDirectoryChanged);
76 QObject::connect(m_fsWatcher.data(), &QFileSystemWatcher::fileChanged, this, &IndicatorsManager::onFileChanged);
80void IndicatorsManager::onDirectoryChanged(const QString& directory)
82 loadDir(QDir(directory));
85void IndicatorsManager::onFileChanged(const QString& file)
87 QFileInfo file_info(file);
88 if (!file_info.exists())
90 unloadFile(file_info);
95 loadFile(QFileInfo(file));
99void IndicatorsManager::loadDir(const QDir& dir)
101 startVerify(dir.canonicalPath());
103 const QFileInfoList indicator_files = dir.entryInfoList(QStringList(), QDir::Files|QDir::NoDotAndDotDot);
104 Q_FOREACH(const QFileInfo& indicator_file, indicator_files)
106 loadFile(indicator_file);
109 endVerify(dir.canonicalPath());
112void IndicatorsManager::loadFile(const QFileInfo& file_info)
114 QSettings indicator_settings(file_info.absoluteFilePath(), QSettings::IniFormat, this);
115 const QString name = indicator_settings.value(QStringLiteral("Indicator Service/Name")).toString();
117 auto iter = m_indicatorsData.constFind(name);
118 if (iter != m_indicatorsData.constEnd())
120 const QString newFileInfoDir = QDir::cleanPath(file_info.canonicalPath());
121 IndicatorData* currentData = (*iter);
122 currentData->m_verified = true;
124 int file_info_location = -1;
125 int current_data_location = -1;
127 const QString currentDataDir = QDir::cleanPath(currentData->m_fileInfo.canonicalPath());
129 // if we've already got this indicator, we need to make sure we're not overwriting data which is
130 // from a lower priority standard path
131 QStringList xdgLocations = shellDataDirs();
132 for (int i = 0; i < xdgLocations.size(); i++)
134 const QString indicatorDir = QDir::cleanPath(xdgLocations[i] + "/unity/indicators");
136 if (newFileInfoDir == indicatorDir)
138 file_info_location = i;
140 if (currentDataDir == indicatorDir)
142 current_data_location = i;
145 if (file_info_location != -1 && current_data_location != -1)
151 // file location is higher (or of equal) priority. overwrite.
152 if (file_info_location <= current_data_location &&
153 file_info != currentData->m_fileInfo)
155 currentData->m_fileInfo = file_info;
156 Q_EMIT indicatorLoaded(name);
161 IndicatorData* data = new IndicatorData(name, file_info);
162 data->m_verified = true;
163 m_indicatorsData[name]= data;
164 Q_EMIT indicatorLoaded(name);
168void IndicatorsManager::unload()
170 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
171 while(iter.hasNext())
174 Q_EMIT indicatorAboutToBeUnloaded(iter.key());
177 qDeleteAll(m_indicatorsData);
178 m_indicatorsData.clear();
183void IndicatorsManager::unloadFile(const QFileInfo& file)
185 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
186 while(iter.hasNext())
189 IndicatorData* data = iter.value();
190 if (data->m_fileInfo.absoluteFilePath() == file.absoluteFilePath())
192 if (!data->m_verified)
194 const QString name = data->m_name;
195 Q_EMIT indicatorAboutToBeUnloaded(name);
203 setLoaded(m_indicatorsData.size() > 0);
206void IndicatorsManager::setLoaded(bool loaded)
208 if (loaded != m_loaded)
211 Q_EMIT loadedChanged(m_loaded);
215QString IndicatorsManager::profile() const
220void IndicatorsManager::setProfile(const QString& profile)
222 if (m_profile != profile) {
224 Q_EMIT profileChanged(m_profile);
228void IndicatorsManager::startVerify(const QString& path)
230 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
231 while(iter.hasNext())
234 IndicatorData* data = iter.value();
235 if (data->m_fileInfo.canonicalPath() == path)
237 data->m_verified = false;
242void IndicatorsManager::endVerify(const QString& path)
244 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
245 while(iter.hasNext())
248 IndicatorData* data = iter.value();
249 if (data->m_fileInfo.canonicalPath() == path)
251 if (!data->m_verified)
253 const QString name = data->m_name;
254 Q_EMIT indicatorAboutToBeUnloaded(name);
263Indicator::Ptr IndicatorsManager::indicator(const QString& indicator_name)
265 if (!m_indicatorsData.contains(indicator_name))
267 qWarning() << Q_FUNC_INFO << "Invalid indicator name: " << indicator_name;
268 return Indicator::Ptr();
271 IndicatorData *data = m_indicatorsData.value(indicator_name);
272 if (data->m_indicator)
274 return data->m_indicator;
277 Indicator::Ptr new_indicator(new Indicator(this));
278 data->m_indicator = new_indicator;
279 QSettings settings(data->m_fileInfo.absoluteFilePath(), QSettings::IniFormat, this);
280 new_indicator->init(data->m_fileInfo.fileName(), settings);
283 // 1) enable session indicator
284 // 2) enable keyboard indicator
286 // The rest of the indicators respect their default profile (which is "phone", even on desktop PCs)
287 if ((new_indicator->identifier() == QStringLiteral("indicator-session"))
288 || new_indicator->identifier() == QStringLiteral("indicator-keyboard")) {
289 new_indicator->setProfile(QString(m_profile).replace(QStringLiteral("phone"), QStringLiteral("desktop")));
291 new_indicator->setProfile(m_profile);
294 QObject::connect(this, &IndicatorsManager::profileChanged, new_indicator.data(), &Indicator::setProfile);
295 return new_indicator;
298QVector<Indicator::Ptr> IndicatorsManager::indicators()
300 QVector<Indicator::Ptr> list;
301 Q_FOREACH(IndicatorData* data, m_indicatorsData)
303 Indicator::Ptr ret = indicator(data->m_name);
310bool 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