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()
57void IndicatorsManager::load()
61 QStringList xdgLocations = shellDataDirs();//QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
63 m_fsWatcher.reset(new QFileSystemWatcher(this));
65 Q_FOREACH(const QString& xdgLocation, xdgLocations)
67 QString indicator_path = QDir::cleanPath(xdgLocation + "/unity/indicators");
68 QDir indicator_dir(indicator_path);
69 if (indicator_dir.exists())
71 // watch folder for changes.
72 m_fsWatcher->addPath(indicator_path);
74 loadDir(indicator_dir);
78 QObject::connect(m_fsWatcher.data(), &QFileSystemWatcher::directoryChanged, this, &IndicatorsManager::onDirectoryChanged);
79 QObject::connect(m_fsWatcher.data(), &QFileSystemWatcher::fileChanged, this, &IndicatorsManager::onFileChanged);
83void IndicatorsManager::onDirectoryChanged(const QString& directory)
85 loadDir(QDir(directory));
88void IndicatorsManager::onFileChanged(const QString& file)
90 QFileInfo file_info(file);
91 if (!file_info.exists())
93 unloadFile(file_info);
98 loadFile(QFileInfo(file));
102void IndicatorsManager::loadDir(const QDir& dir)
104 startVerify(dir.canonicalPath());
106 QFileInfoList indicator_files = dir.entryInfoList(QStringList(), QDir::Files|QDir::NoDotAndDotDot);
107 Q_FOREACH(const QFileInfo& indicator_file, indicator_files)
109 loadFile(indicator_file);
112 endVerify(dir.canonicalPath());
115void IndicatorsManager::loadFile(const QFileInfo& file_info)
117 QSettings indicator_settings(file_info.absoluteFilePath(), QSettings::IniFormat, this);
118 QString name = indicator_settings.value(QStringLiteral("Indicator Service/Name")).toString();
120 auto iter = m_indicatorsData.find(name);
121 if (iter != m_indicatorsData.end())
123 QString newFileInfoDir = QDir::cleanPath(file_info.canonicalPath());
124 IndicatorData* currentData = (*iter);
125 currentData->m_verified = true;
127 int file_info_location = -1;
128 int current_data_location = -1;
130 QString currentDataDir = QDir::cleanPath(currentData->m_fileInfo.canonicalPath());
132 // if we've already got this indicator, we need to make sure we're not overwriting data which is
133 // from a lower priority standard path
134 QStringList xdgLocations = shellDataDirs();
135 for (int i = 0; i < xdgLocations.size(); i++)
137 QString indicatorDir = QDir::cleanPath(xdgLocations[i] + "/unity/indicators");
139 if (newFileInfoDir == indicatorDir)
141 file_info_location = i;
143 if (currentDataDir == indicatorDir)
145 current_data_location = i;
148 if (file_info_location != -1 && current_data_location != -1)
154 // file location is higher (or of equal) priority. overwrite.
155 if (file_info_location <= current_data_location &&
156 file_info != currentData->m_fileInfo)
158 currentData->m_fileInfo = file_info;
159 Q_EMIT indicatorLoaded(name);
164 IndicatorData* data = new IndicatorData(name, file_info);
165 data->m_verified = true;
166 m_indicatorsData[name]= data;
167 Q_EMIT indicatorLoaded(name);
171void IndicatorsManager::unload()
173 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
174 while(iter.hasNext())
177 Q_EMIT indicatorAboutToBeUnloaded(iter.key());
180 qDeleteAll(m_indicatorsData);
181 m_indicatorsData.clear();
186void IndicatorsManager::unloadFile(const QFileInfo& file)
188 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
189 while(iter.hasNext())
192 IndicatorData* data = iter.value();
193 if (data->m_fileInfo.absoluteFilePath() == file.absoluteFilePath())
195 if (!data->m_verified)
197 QString name = data->m_name;
198 Q_EMIT indicatorAboutToBeUnloaded(name);
206 setLoaded(m_indicatorsData.size() > 0);
209void IndicatorsManager::setLoaded(bool loaded)
211 if (loaded != m_loaded)
214 Q_EMIT loadedChanged(m_loaded);
218QString IndicatorsManager::profile() const
223void IndicatorsManager::setProfile(const QString& profile)
225 if (m_profile != profile) {
227 Q_EMIT profileChanged(m_profile);
231void IndicatorsManager::startVerify(const QString& path)
233 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
234 while(iter.hasNext())
237 IndicatorData* data = iter.value();
238 if (data->m_fileInfo.canonicalPath() == path)
240 data->m_verified = false;
245void IndicatorsManager::endVerify(const QString& path)
247 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
248 while(iter.hasNext())
251 IndicatorData* data = iter.value();
252 if (data->m_fileInfo.canonicalPath() == path)
254 if (!data->m_verified)
256 QString name = data->m_name;
257 Q_EMIT indicatorAboutToBeUnloaded(name);
266Indicator::Ptr IndicatorsManager::indicator(const QString& indicator_name)
268 if (!m_indicatorsData.contains(indicator_name))
270 qWarning() << Q_FUNC_INFO << "Invalid indicator name: " << indicator_name;
271 return Indicator::Ptr();
274 IndicatorData *data = m_indicatorsData[indicator_name];
275 if (data->m_indicator)
277 return data->m_indicator;
280 Indicator::Ptr new_indicator(new Indicator(this));
281 data->m_indicator = new_indicator;
282 QSettings settings(data->m_fileInfo.absoluteFilePath(), QSettings::IniFormat, this);
283 new_indicator->init(data->m_fileInfo.fileName(), settings);
284 new_indicator->setProfile(m_profile);
285 QObject::connect(this, &IndicatorsManager::profileChanged, new_indicator.data(), &Indicator::setProfile);
286 return new_indicator;
289QVector<Indicator::Ptr> IndicatorsManager::indicators()
291 QVector<Indicator::Ptr> list;
292 Q_FOREACH(IndicatorData* data, m_indicatorsData)
294 Indicator::Ptr ret = indicator(data->m_name);
301bool 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