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"
30class IndicatorsManager::IndicatorData
33 IndicatorData(const QString& name, const QFileInfo& fileInfo)
35 , m_fileInfo(fileInfo)
44 Indicator::Ptr m_indicator;
47IndicatorsManager::IndicatorsManager(QObject* parent)
53IndicatorsManager::~IndicatorsManager()
59void IndicatorsManager::load()
62 QStringList xdgLocations = shellDataDirs();//QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
64 m_fsWatcher.reset(new QFileSystemWatcher(this));
66 Q_FOREACH(const QString& xdgLocation, xdgLocations)
68 QString indicator_path = QDir::cleanPath(xdgLocation + "/unity/indicators");
69 QDir indicator_dir(indicator_path);
70 if (indicator_dir.exists())
72 // watch folder for changes.
73 m_fsWatcher->addPath(indicator_path);
75 loadDir(indicator_dir);
79 QObject::connect(m_fsWatcher.data(), SIGNAL(directoryChanged(const QString&)), this, SLOT(onDirectoryChanged(const QString&)));
80 QObject::connect(m_fsWatcher.data(), SIGNAL(fileChanged(const QString&)), this, SLOT(onFileChanged(const QString&)));
84void IndicatorsManager::onDirectoryChanged(const QString& directory)
86 loadDir(QDir(directory));
89void IndicatorsManager::onFileChanged(const QString& file)
91 QFileInfo file_info(file);
92 if (!file_info.exists())
94 unloadFile(file_info);
99 loadFile(QFileInfo(file));
103void IndicatorsManager::loadDir(const QDir& dir)
105 startVerify(dir.canonicalPath());
107 QFileInfoList indicator_files = dir.entryInfoList(QStringList(), QDir::Files|QDir::NoDotAndDotDot);
108 Q_FOREACH(const QFileInfo& indicator_file, indicator_files)
110 loadFile(indicator_file);
113 endVerify(dir.canonicalPath());
116void IndicatorsManager::loadFile(const QFileInfo& file_info)
118 QSettings indicator_settings(file_info.absoluteFilePath(), QSettings::IniFormat, this);
119 QString name = indicator_settings.value("Indicator Service/Name").toString();
121 auto iter = m_indicatorsData.find(name);
122 if (iter != m_indicatorsData.end())
124 QString newFileInfoDir = QDir::cleanPath(file_info.canonicalPath());
125 IndicatorData* currentData = (*iter);
126 currentData->m_verified = true;
128 int file_info_location = -1;
129 int current_data_location = -1;
131 QString currentDataDir = QDir::cleanPath(currentData->m_fileInfo.canonicalPath());
133 // if we've already got this indicator, we need to make sure we're not overwriting data which is
134 // from a lower priority standard path
135 QStringList xdgLocations = shellDataDirs();
136 for (int i = 0; i < xdgLocations.size(); i++)
138 QString indicatorDir = QDir::cleanPath(xdgLocations[i] + "/unity/indicators");
140 if (newFileInfoDir == indicatorDir)
142 file_info_location = i;
144 if (currentDataDir == indicatorDir)
146 current_data_location = i;
149 if (file_info_location != -1 && current_data_location != -1)
155 // file location is higher (or of equal) priority. overwrite.
156 if (file_info_location <= current_data_location &&
157 file_info != currentData->m_fileInfo)
159 currentData->m_fileInfo = file_info;
160 Q_EMIT indicatorLoaded(name);
165 IndicatorData* data = new IndicatorData(name, file_info);
166 data->m_verified = true;
167 m_indicatorsData[name]= data;
168 Q_EMIT indicatorLoaded(name);
172void IndicatorsManager::unload()
174 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
175 while(iter.hasNext())
178 Q_EMIT indicatorAboutToBeUnloaded(iter.key());
181 qDeleteAll(m_indicatorsData);
182 m_indicatorsData.clear();
187void IndicatorsManager::unloadFile(const QFileInfo& file)
189 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
190 while(iter.hasNext())
193 IndicatorData* data = iter.value();
194 if (data->m_fileInfo.absoluteFilePath() == file.absoluteFilePath())
196 if (!data->m_verified)
198 QString name = data->m_name;
199 Q_EMIT indicatorAboutToBeUnloaded(name);
207 setLoaded(m_indicatorsData.size() > 0);
210void IndicatorsManager::setLoaded(bool loaded)
212 if (loaded != m_loaded)
215 Q_EMIT loadedChanged(m_loaded);
217 auto upstartsession = qgetenv("UPSTART_SESSION");
218 NihDBusProxy * upstart = NULL;
220 if (!upstartsession.isNull()) {
221 DBusConnection * conn = NULL;
222 conn = dbus_connection_open(upstartsession.constData(), NULL);
224 upstart = nih_dbus_proxy_new(NULL, conn,
228 dbus_connection_unref(conn);
232 if (upstart != NULL) {
235 event_sent = upstart_emit_event_sync(NULL, upstart, "indicator-services-start", NULL, 0);
237 event_sent = upstart_emit_event_sync(NULL, upstart, "indicator-services-end", NULL, 0);
240 if (event_sent != 0) {
241 qWarning() << "Unable to send indicator event to Upstart";
243 nih_unref(upstart, NULL);
249void IndicatorsManager::startVerify(const QString& path)
251 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
252 while(iter.hasNext())
255 IndicatorData* data = iter.value();
256 if (data->m_fileInfo.canonicalPath() == path)
258 data->m_verified = false;
263void IndicatorsManager::endVerify(const QString& path)
265 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
266 while(iter.hasNext())
269 IndicatorData* data = iter.value();
270 if (data->m_fileInfo.canonicalPath() == path)
272 if (!data->m_verified)
274 QString name = data->m_name;
275 Q_EMIT indicatorAboutToBeUnloaded(name);
284Indicator::Ptr IndicatorsManager::indicator(const QString& indicator_name)
286 if (!m_indicatorsData.contains(indicator_name))
288 qWarning() << Q_FUNC_INFO << "Invalid indicator name: " << indicator_name;
289 return Indicator::Ptr();
292 IndicatorData *data = m_indicatorsData[indicator_name];
293 if (data->m_indicator)
295 return data->m_indicator;
298 Indicator::Ptr new_indicator(new Indicator(this));
299 data->m_indicator = new_indicator;
300 QSettings settings(data->m_fileInfo.absoluteFilePath(), QSettings::IniFormat, this);
301 new_indicator->init(data->m_fileInfo.fileName(), settings);
302 return new_indicator;
305QList<Indicator::Ptr> IndicatorsManager::indicators()
307 QList<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