on e82caeb help sign in
unity8/plugins/Unity/Indicators/indicatorsmanager.cpp
324 lines 8.8 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 * Author: Nick Dedekind <nick.dedekind@canonical.com>
17 */
19#include "indicatorsmanager.h"
21#include <QSettings>
22#include <QDebug>
24#include <upstart.h>
25#include <nih/alloc.h>
27#include "paths.h"
30class IndicatorsManager::IndicatorData
32public:
33 IndicatorData(const QString& name, const QFileInfo& fileInfo)
34 : m_name(name)
35 , m_fileInfo(fileInfo)
36 , m_verified (true)
37 {
38 }
40 QString m_name;
41 QFileInfo m_fileInfo;
43 bool m_verified;
44 Indicator::Ptr m_indicator;
45};
47IndicatorsManager::IndicatorsManager(QObject* parent)
48 : QObject(parent)
49 , m_loaded(false)
50 , m_upstart(NULL)
52 auto upstartsession = qgetenv("UPSTART_SESSION");
53 if (!upstartsession.isNull()) {
54 DBusConnection * conn = NULL;
55 conn = dbus_connection_open(upstartsession.constData(), NULL);
56 if (conn != NULL) {
57 m_upstart = nih_dbus_proxy_new(NULL, conn,
58 NULL,
59 DBUS_PATH_UPSTART,
60 NULL, NULL);
61 dbus_connection_unref(conn);
62 }
63 }
65 if (m_upstart != NULL) {
66 m_upstart->auto_start = FALSE;
67 }
70IndicatorsManager::~IndicatorsManager()
72 unload();
74 if (m_upstart != NULL) {
75 nih_unref(m_upstart, NULL);
76 m_upstart = NULL;
77 }
80void IndicatorsManager::load()
82 unload();
83 QStringList xdgLocations = shellDataDirs();//QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
85 m_fsWatcher.reset(new QFileSystemWatcher(this));
87 Q_FOREACH(const QString& xdgLocation, xdgLocations)
88 {
89 QString indicator_path = QDir::cleanPath(xdgLocation + "/unity/indicators");
90 QDir indicator_dir(indicator_path);
91 if (indicator_dir.exists())
92 {
93 // watch folder for changes.
94 m_fsWatcher->addPath(indicator_path);
96 loadDir(indicator_dir);
97 }
98 }
100 QObject::connect(m_fsWatcher.data(), SIGNAL(directoryChanged(const QString&)), this, SLOT(onDirectoryChanged(const QString&)));
101 QObject::connect(m_fsWatcher.data(), SIGNAL(fileChanged(const QString&)), this, SLOT(onFileChanged(const QString&)));
102 setLoaded(true);
105void IndicatorsManager::onDirectoryChanged(const QString& directory)
107 loadDir(QDir(directory));
110void IndicatorsManager::onFileChanged(const QString& file)
112 QFileInfo file_info(file);
113 if (!file_info.exists())
114 {
115 unloadFile(file_info);
116 return;
117 }
118 else
119 {
120 loadFile(QFileInfo(file));
121 }
124void IndicatorsManager::loadDir(const QDir& dir)
126 startVerify(dir.canonicalPath());
128 QFileInfoList indicator_files = dir.entryInfoList(QStringList(), QDir::Files|QDir::NoDotAndDotDot);
129 Q_FOREACH(const QFileInfo& indicator_file, indicator_files)
130 {
131 loadFile(indicator_file);
132 }
134 endVerify(dir.canonicalPath());
137void IndicatorsManager::loadFile(const QFileInfo& file_info)
139 QSettings indicator_settings(file_info.absoluteFilePath(), QSettings::IniFormat, this);
140 QString name = indicator_settings.value("Indicator Service/Name").toString();
142 auto iter = m_indicatorsData.find(name);
143 if (iter != m_indicatorsData.end())
144 {
145 QString newFileInfoDir = QDir::cleanPath(file_info.canonicalPath());
146 IndicatorData* currentData = (*iter);
147 currentData->m_verified = true;
149 int file_info_location = -1;
150 int current_data_location = -1;
152 QString currentDataDir = QDir::cleanPath(currentData->m_fileInfo.canonicalPath());
154 // if we've already got this indicator, we need to make sure we're not overwriting data which is
155 // from a lower priority standard path
156 QStringList xdgLocations = shellDataDirs();
157 for (int i = 0; i < xdgLocations.size(); i++)
158 {
159 QString indicatorDir = QDir::cleanPath(xdgLocations[i] + "/unity/indicators");
161 if (newFileInfoDir == indicatorDir)
162 {
163 file_info_location = i;
164 }
165 if (currentDataDir == indicatorDir)
166 {
167 current_data_location = i;
168 }
170 if (file_info_location != -1 && current_data_location != -1)
171 {
172 break;
173 }
174 }
176 // file location is higher (or of equal) priority. overwrite.
177 if (file_info_location <= current_data_location &&
178 file_info != currentData->m_fileInfo)
179 {
180 currentData->m_fileInfo = file_info;
181 Q_EMIT indicatorLoaded(name);
182 }
183 }
184 else
185 {
186 IndicatorData* data = new IndicatorData(name, file_info);
187 data->m_verified = true;
188 m_indicatorsData[name]= data;
189 Q_EMIT indicatorLoaded(name);
190 }
193void IndicatorsManager::unload()
195 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
196 while(iter.hasNext())
197 {
198 iter.next();
199 Q_EMIT indicatorAboutToBeUnloaded(iter.key());
200 }
202 qDeleteAll(m_indicatorsData);
203 m_indicatorsData.clear();
205 setLoaded(false);
208void IndicatorsManager::unloadFile(const QFileInfo& file)
210 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
211 while(iter.hasNext())
212 {
213 iter.next();
214 IndicatorData* data = iter.value();
215 if (data->m_fileInfo.absoluteFilePath() == file.absoluteFilePath())
216 {
217 if (!data->m_verified)
218 {
219 QString name = data->m_name;
220 Q_EMIT indicatorAboutToBeUnloaded(name);
222 delete data;
223 iter.remove();
224 }
225 }
226 }
228 setLoaded(m_indicatorsData.size() > 0);
231void IndicatorsManager::setLoaded(bool loaded)
233 if (loaded != m_loaded)
234 {
235 m_loaded = loaded;
236 Q_EMIT loadedChanged(m_loaded);
238 if (m_upstart != NULL) {
239 int event_sent = 0;
240 if (m_loaded) {
241 event_sent = upstart_emit_event_sync(NULL, m_upstart, "indicator-services-start", NULL, 0);
242 } else {
243 event_sent = upstart_emit_event_sync(NULL, m_upstart, "indicator-services-end", NULL, 0);
244 }
246 if (event_sent != 0) {
247 qWarning() << "Unable to send indicator event to Upstart";
248 }
249 }
250 }
253void IndicatorsManager::startVerify(const QString& path)
255 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
256 while(iter.hasNext())
257 {
258 iter.next();
259 IndicatorData* data = iter.value();
260 if (data->m_fileInfo.canonicalPath() == path)
261 {
262 data->m_verified = false;
263 }
264 }
267void IndicatorsManager::endVerify(const QString& path)
269 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
270 while(iter.hasNext())
271 {
272 iter.next();
273 IndicatorData* data = iter.value();
274 if (data->m_fileInfo.canonicalPath() == path)
275 {
276 if (!data->m_verified)
277 {
278 QString name = data->m_name;
279 Q_EMIT indicatorAboutToBeUnloaded(name);
281 delete data;
282 iter.remove();
283 }
284 }
285 }
288Indicator::Ptr IndicatorsManager::indicator(const QString& indicator_name)
290 if (!m_indicatorsData.contains(indicator_name))
291 {
292 qWarning() << Q_FUNC_INFO << "Invalid indicator name: " << indicator_name;
293 return Indicator::Ptr();
294 }
296 IndicatorData *data = m_indicatorsData[indicator_name];
297 if (data->m_indicator)
298 {
299 return data->m_indicator;
300 }
302 Indicator::Ptr new_indicator(new Indicator(this));
303 data->m_indicator = new_indicator;
304 QSettings settings(data->m_fileInfo.absoluteFilePath(), QSettings::IniFormat, this);
305 new_indicator->init(data->m_fileInfo.fileName(), settings);
306 return new_indicator;
309QList<Indicator::Ptr> IndicatorsManager::indicators()
311 QList<Indicator::Ptr> list;
312 Q_FOREACH(IndicatorData* data, m_indicatorsData)
313 {
314 Indicator::Ptr ret = indicator(data->m_name);
315 if (ret)
316 list.append(ret);
317 }
318 return list;
321bool IndicatorsManager::isLoaded() const
323 return m_loaded;