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"
29class IndicatorsManager::IndicatorData
32 IndicatorData(const QString& name, const QFileInfo& fileInfo)
34 , m_fileInfo(fileInfo)
43 Indicator::Ptr m_indicator;
46IndicatorsManager::IndicatorsManager(QObject* parent)
51 auto upstartsession = qgetenv("UPSTART_SESSION");
52 if (!upstartsession.isNull()) {
53 DBusConnection * conn = NULL;
54 conn = dbus_connection_open(upstartsession.constData(), NULL);
56 m_upstart = nih_dbus_proxy_new(NULL, conn,
60 dbus_connection_unref(conn);
64 if (m_upstart != NULL) {
65 m_upstart->auto_start = FALSE;
69IndicatorsManager::~IndicatorsManager()
73 if (m_upstart != NULL) {
74 nih_unref(m_upstart, NULL);
79void IndicatorsManager::load()
82 QStringList xdgLocations = shellDataDirs();//QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
84 m_fsWatcher.reset(new QFileSystemWatcher(this));
86 Q_FOREACH(const QString& xdgLocation, xdgLocations)
88 QString indicator_path = QDir::cleanPath(xdgLocation + "/unity/indicators");
89 QDir indicator_dir(indicator_path);
90 if (indicator_dir.exists())
92 // watch folder for changes.
93 m_fsWatcher->addPath(indicator_path);
95 loadDir(indicator_dir);
99 QObject::connect(m_fsWatcher.data(), SIGNAL(directoryChanged(const QString&)), this, SLOT(onDirectoryChanged(const QString&)));
100 QObject::connect(m_fsWatcher.data(), SIGNAL(fileChanged(const QString&)), this, SLOT(onFileChanged(const QString&)));
104void IndicatorsManager::onDirectoryChanged(const QString& directory)
106 loadDir(QDir(directory));
109void IndicatorsManager::onFileChanged(const QString& file)
111 QFileInfo file_info(file);
112 if (!file_info.exists())
114 unloadFile(file_info);
119 loadFile(QFileInfo(file));
123void IndicatorsManager::loadDir(const QDir& dir)
125 startVerify(dir.canonicalPath());
127 QFileInfoList indicator_files = dir.entryInfoList(QStringList(), QDir::Files|QDir::NoDotAndDotDot);
128 Q_FOREACH(const QFileInfo& indicator_file, indicator_files)
130 loadFile(indicator_file);
133 endVerify(dir.canonicalPath());
136void IndicatorsManager::loadFile(const QFileInfo& file_info)
138 QSettings indicator_settings(file_info.absoluteFilePath(), QSettings::IniFormat, this);
139 QString name = indicator_settings.value("Indicator Service/Name").toString();
141 auto iter = m_indicatorsData.find(name);
142 if (iter != m_indicatorsData.end())
144 QString newFileInfoDir = QDir::cleanPath(file_info.canonicalPath());
145 IndicatorData* currentData = (*iter);
146 currentData->m_verified = true;
148 int file_info_location = -1;
149 int current_data_location = -1;
151 QString currentDataDir = QDir::cleanPath(currentData->m_fileInfo.canonicalPath());
153 // if we've already got this indicator, we need to make sure we're not overwriting data which is
154 // from a lower priority standard path
155 QStringList xdgLocations = shellDataDirs();
156 for (int i = 0; i < xdgLocations.size(); i++)
158 QString indicatorDir = QDir::cleanPath(xdgLocations[i] + "/unity/indicators");
160 if (newFileInfoDir == indicatorDir)
162 file_info_location = i;
164 if (currentDataDir == indicatorDir)
166 current_data_location = i;
169 if (file_info_location != -1 && current_data_location != -1)
175 // file location is higher (or of equal) priority. overwrite.
176 if (file_info_location <= current_data_location &&
177 file_info != currentData->m_fileInfo)
179 currentData->m_fileInfo = file_info;
180 Q_EMIT indicatorLoaded(name);
185 IndicatorData* data = new IndicatorData(name, file_info);
186 data->m_verified = true;
187 m_indicatorsData[name]= data;
188 Q_EMIT indicatorLoaded(name);
192void IndicatorsManager::unload()
194 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
195 while(iter.hasNext())
198 Q_EMIT indicatorAboutToBeUnloaded(iter.key());
201 qDeleteAll(m_indicatorsData);
202 m_indicatorsData.clear();
207void IndicatorsManager::unloadFile(const QFileInfo& file)
209 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
210 while(iter.hasNext())
213 IndicatorData* data = iter.value();
214 if (data->m_fileInfo.absoluteFilePath() == file.absoluteFilePath())
216 if (!data->m_verified)
218 QString name = data->m_name;
219 Q_EMIT indicatorAboutToBeUnloaded(name);
227 setLoaded(m_indicatorsData.size() > 0);
230void IndicatorsManager::setLoaded(bool loaded)
232 if (loaded != m_loaded)
235 Q_EMIT loadedChanged(m_loaded);
237 DBusPendingCall * pending = NULL;
239 pending = upstart_emit_event(m_upstart, "indicator-services-start", NULL, 0, NULL, NULL, NULL, 100);
241 pending = upstart_emit_event(m_upstart, "indicator-services-end", NULL, 0, NULL, NULL, NULL, 100);
243 dbus_pending_call_unref(pending);
247void IndicatorsManager::startVerify(const QString& path)
249 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
250 while(iter.hasNext())
253 IndicatorData* data = iter.value();
254 if (data->m_fileInfo.canonicalPath() == path)
256 data->m_verified = false;
261void IndicatorsManager::endVerify(const QString& path)
263 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
264 while(iter.hasNext())
267 IndicatorData* data = iter.value();
268 if (data->m_fileInfo.canonicalPath() == path)
270 if (!data->m_verified)
272 QString name = data->m_name;
273 Q_EMIT indicatorAboutToBeUnloaded(name);
282Indicator::Ptr IndicatorsManager::indicator(const QString& indicator_name)
284 if (!m_indicatorsData.contains(indicator_name))
286 qWarning() << Q_FUNC_INFO << "Invalid indicator name: " << indicator_name;
287 return Indicator::Ptr();
290 IndicatorData *data = m_indicatorsData[indicator_name];
291 if (data->m_indicator)
293 return data->m_indicator;
296 Indicator::Ptr new_indicator(new Indicator(this));
297 data->m_indicator = new_indicator;
298 QSettings settings(data->m_fileInfo.absoluteFilePath(), QSettings::IniFormat, this);
299 new_indicator->init(data->m_fileInfo.fileName(), settings);
300 return new_indicator;
303QList<Indicator::Ptr> IndicatorsManager::indicators()
305 QList<Indicator::Ptr> list;
306 Q_FOREACH(IndicatorData* data, m_indicatorsData)
308 Indicator::Ptr ret = indicator(data->m_name);
315bool 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