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)
38 auto upstartsession = qgetenv("UPSTART_SESSION");
39 if (!upstartsession.isNull()) {
40 DBusConnection * conn = NULL;
41 conn = dbus_connection_open(upstartsession.constData(), NULL);
43 m_upstart = nih_dbus_proxy_new(NULL, conn,
47 dbus_connection_unref(conn);
51 if (m_upstart != NULL) {
52 m_upstart->auto_start = FALSE;
60 Indicator::Ptr m_indicator;
62 NihDBusProxy * m_upstart;
65IndicatorsManager::IndicatorsManager(QObject* parent)
71IndicatorsManager::~IndicatorsManager()
76void IndicatorsManager::load()
79 QStringList xdgLocations = shellDataDirs();//QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
81 m_fsWatcher.reset(new QFileSystemWatcher(this));
83 Q_FOREACH(const QString& xdgLocation, xdgLocations)
85 QString indicator_path = QDir::cleanPath(xdgLocation + "/unity/indicators");
86 QDir indicator_dir(indicator_path);
87 if (indicator_dir.exists())
89 // watch folder for changes.
90 m_fsWatcher->addPath(indicator_path);
92 loadDir(indicator_dir);
96 QObject::connect(m_fsWatcher.data(), SIGNAL(directoryChanged(const QString&)), this, SLOT(onDirectoryChanged(const QString&)));
97 QObject::connect(m_fsWatcher.data(), SIGNAL(fileChanged(const QString&)), this, SLOT(onFileChanged(const QString&)));
101void IndicatorsManager::onDirectoryChanged(const QString& directory)
103 loadDir(QDir(directory));
106void IndicatorsManager::onFileChanged(const QString& file)
108 QFileInfo file_info(file);
109 if (!file_info.exists())
111 unloadFile(file_info);
116 loadFile(QFileInfo(file));
120void IndicatorsManager::loadDir(const QDir& dir)
122 startVerify(dir.canonicalPath());
124 QFileInfoList indicator_files = dir.entryInfoList(QStringList(), QDir::Files|QDir::NoDotAndDotDot);
125 Q_FOREACH(const QFileInfo& indicator_file, indicator_files)
127 loadFile(indicator_file);
130 endVerify(dir.canonicalPath());
133void IndicatorsManager::loadFile(const QFileInfo& file_info)
135 QSettings indicator_settings(file_info.absoluteFilePath(), QSettings::IniFormat, this);
136 QString name = indicator_settings.value("Indicator Service/Name").toString();
138 auto iter = m_indicatorsData.find(name);
139 if (iter != m_indicatorsData.end())
141 QString newFileInfoDir = QDir::cleanPath(file_info.canonicalPath());
142 IndicatorData* currentData = (*iter);
143 currentData->m_verified = true;
145 int file_info_location = -1;
146 int current_data_location = -1;
148 QString currentDataDir = QDir::cleanPath(currentData->m_fileInfo.canonicalPath());
150 // if we've already got this indicator, we need to make sure we're not overwriting data which is
151 // from a lower priority standard path
152 QStringList xdgLocations = shellDataDirs();
153 for (int i = 0; i < xdgLocations.size(); i++)
155 QString indicatorDir = QDir::cleanPath(xdgLocations[i] + "/unity/indicators");
157 if (newFileInfoDir == indicatorDir)
159 file_info_location = i;
161 if (currentDataDir == indicatorDir)
163 current_data_location = i;
166 if (file_info_location != -1 && current_data_location != -1)
172 // file location is higher (or of equal) priority. overwrite.
173 if (file_info_location <= current_data_location &&
174 file_info != currentData->m_fileInfo)
176 currentData->m_fileInfo = file_info;
177 Q_EMIT indicatorLoaded(name);
182 IndicatorData* data = new IndicatorData(name, file_info);
183 data->m_verified = true;
184 m_indicatorsData[name]= data;
185 Q_EMIT indicatorLoaded(name);
189void IndicatorsManager::unload()
191 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
192 while(iter.hasNext())
195 Q_EMIT indicatorAboutToBeUnloaded(iter.key());
198 qDeleteAll(m_indicatorsData);
199 m_indicatorsData.clear();
204void IndicatorsManager::unloadFile(const QFileInfo& file)
206 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
207 while(iter.hasNext())
210 IndicatorData* data = iter.value();
211 if (data->m_fileInfo.absoluteFilePath() == file.absoluteFilePath())
213 if (!data->m_verified)
215 QString name = data->m_name;
216 Q_EMIT indicatorAboutToBeUnloaded(name);
224 setLoaded(m_indicatorsData.size() > 0);
227void IndicatorsManager::setLoaded(bool loaded)
229 if (loaded != m_loaded)
232 Q_EMIT loadedChanged(m_loaded);
236void IndicatorsManager::startVerify(const QString& path)
238 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
239 while(iter.hasNext())
242 IndicatorData* data = iter.value();
243 if (data->m_fileInfo.canonicalPath() == path)
245 data->m_verified = false;
250void IndicatorsManager::endVerify(const QString& path)
252 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
253 while(iter.hasNext())
256 IndicatorData* data = iter.value();
257 if (data->m_fileInfo.canonicalPath() == path)
259 if (!data->m_verified)
261 QString name = data->m_name;
262 Q_EMIT indicatorAboutToBeUnloaded(name);
271Indicator::Ptr IndicatorsManager::indicator(const QString& indicator_name)
273 if (!m_indicatorsData.contains(indicator_name))
275 qWarning() << Q_FUNC_INFO << "Invalid indicator name: " << indicator_name;
276 return Indicator::Ptr();
279 IndicatorData *data = m_indicatorsData[indicator_name];
280 if (data->m_indicator)
282 return data->m_indicator;
285 Indicator::Ptr new_indicator(new Indicator(this));
286 data->m_indicator = new_indicator;
287 QSettings settings(data->m_fileInfo.absoluteFilePath(), QSettings::IniFormat, this);
288 new_indicator->init(data->m_fileInfo.fileName(), settings);
289 return new_indicator;
292QList<Indicator::Ptr> IndicatorsManager::indicators()
294 QList<Indicator::Ptr> list;
295 Q_FOREACH(IndicatorData* data, m_indicatorsData)
297 Indicator::Ptr ret = indicator(data->m_name);
304bool 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