on 2ecb97b help sign in
unity8/plugins/IndicatorsClient/indicatorsmanager.cpp
258 lines 7.3 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * Authors:
5 * Michał Sawicz <michal.sawicz@canonical.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
20#include "indicatorsmanager.h"
21#include "indicatorsfactory.h"
23#include <QStandardPaths>
24#include <QSettings>
25#include <QDebug>
28class IndicatorsManager::IndicatorData
30public:
31 IndicatorData(const QString& name, const QFileInfo& fileInfo)
32 : m_name(name)
33 , m_fileInfo(fileInfo)
34 , m_verified (true)
35 {}
37 QString m_name;
38 QFileInfo m_fileInfo;
40 bool m_verified;
41 IndicatorClientInterface::Ptr m_obj;
42};
44IndicatorsManager::IndicatorsManager(QObject* parent)
45: QObject(parent)
46, m_factory(new IndicatorsFactory())
50IndicatorsManager::~IndicatorsManager()
52 unload();
55void IndicatorsManager::load()
57 unload();
58 QStringList xdgLocations = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
60 m_fsWatcher.reset(new QFileSystemWatcher(this));
62 Q_FOREACH(const QString& xdgLocation, xdgLocations)
63 {
64 QString indicator_path = QDir::cleanPath(xdgLocation + "/unity/indicators");
65 QDir indicator_dir(indicator_path);
66 if (indicator_dir.exists())
67 {
68 // watch folder for changes.
69 m_fsWatcher->addPath(indicator_path);
71 load(indicator_dir);
72 }
73 }
75 QObject::connect(m_fsWatcher.data(), SIGNAL(directoryChanged(const QString&)), this, SLOT(onDirectoryChanged(const QString&)));
76 QObject::connect(m_fsWatcher.data(), SIGNAL(fileChanged(const QString&)), this, SLOT(onFileChanged(const QString&)));
79void IndicatorsManager::onDirectoryChanged(const QString& direcory)
81 load(QDir(direcory));
84void IndicatorsManager::onFileChanged(const QString& file)
86 QFileInfo file_info(file);
87 if (!file_info.exists())
88 {
89 unload(file_info);
90 return;
91 }
92 else
93 {
94 load(QFileInfo(file));
95 }
98void IndicatorsManager::load(const QDir& dir)
100 startVerify(dir.canonicalPath());
102 QFileInfoList indicator_files = dir.entryInfoList(QStringList() << "*.indicator", QDir::Files);
103 Q_FOREACH(const QFileInfo& indicator_file, indicator_files)
104 {
105 load(indicator_file);
106 }
108 endVerify(dir.canonicalPath());
111void IndicatorsManager::load(const QFileInfo& file_info)
113 QSettings indicator_settings(file_info.absoluteFilePath(), QSettings::IniFormat, this);
114 indicator_settings.beginGroup("Indicator Service");
115 QString name = indicator_settings.value("Name").toString();
117 auto iter = m_indicatorsData.find(name);
118 if (iter != m_indicatorsData.end())
119 {
120 QString newFileInfoDir = QDir::cleanPath(file_info.canonicalPath());
121 IndicatorData* currentData = (*iter);
122 currentData->m_verified = true;
124 int file_info_location = -1;
125 int current_data_location = -1;
127 QString currentDataDir = QDir::cleanPath(currentData->m_fileInfo.canonicalPath());
129 // if we've already got this indicator, we need to make sure we're not overwriting data which is
130 // from a lower priority standard path
131 QStringList xdgLocations = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
132 for (int i = 0; i < xdgLocations.size(); i++)
133 {
134 QString indicatorDir = QDir::cleanPath(xdgLocations[i] + "/unity/indicators");
136 if (newFileInfoDir == indicatorDir)
137 file_info_location = i;
138 if (currentDataDir == indicatorDir)
139 current_data_location = i;
141 if (file_info_location != -1 && current_data_location != -1)
142 break;
143 }
145 // file location is higher (or of equal) priority. overwrite.
146 if (file_info_location <= current_data_location &&
147 file_info != currentData->m_fileInfo)
148 {
149 currentData->m_fileInfo = file_info;
150 Q_EMIT loaded(name);
151 }
152 }
153 else
154 {
155 IndicatorData* data = new IndicatorData(name, file_info);
156 data->m_verified = true;
157 m_indicatorsData[name]= data;
158 Q_EMIT loaded(name);
159 }
162void IndicatorsManager::unload()
164 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
165 while(iter.hasNext())
166 {
167 iter.next();
168 Q_EMIT aboutToBeUnloaded(iter.key());
169 }
171 qDeleteAll(m_indicatorsData);
172 m_indicatorsData.clear();
175void IndicatorsManager::unload(const QFileInfo& file)
177 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
178 while(iter.hasNext())
179 {
180 iter.next();
181 IndicatorData* data = iter.value();
182 if (data->m_fileInfo.absoluteFilePath() == file.absoluteFilePath())
183 {
184 if (!data->m_verified)
185 {
186 QString name = data->m_name;
187 Q_EMIT aboutToBeUnloaded(name);
189 delete data;
190 iter.remove();
191 }
192 }
193 }
196void IndicatorsManager::startVerify(const QString& path)
198 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
199 while(iter.hasNext())
200 {
201 iter.next();
202 IndicatorData* data = iter.value();
203 if (data->m_fileInfo.canonicalPath() == path)
204 data->m_verified = false;
205 }
208void IndicatorsManager::endVerify(const QString& path)
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.canonicalPath() == path)
216 {
217 if (!data->m_verified)
218 {
219 QString name = data->m_name;
220 Q_EMIT aboutToBeUnloaded(name);
222 delete data;
223 iter.remove();
224 }
225 }
226 }
229IndicatorClientInterface::Ptr IndicatorsManager::indicator(const QString& indicator)
231 if (!m_indicatorsData.contains(indicator)) {
232 qWarning() << Q_FUNC_INFO << "Invalid plugin name: " << indicator;
233 return 0;
234 }
236 IndicatorData *data = m_indicatorsData[indicator];
237 if (data->m_obj)
238 return data->m_obj;
240 IndicatorClientInterface::Ptr plugin = m_factory->create(indicator, this);
241 if (plugin)
242 {
243 data->m_obj = plugin;
244 plugin->init(QSettings(data->m_fileInfo.absoluteFilePath(), QSettings::IniFormat, this));
245 }
246 return plugin;
249QList<IndicatorClientInterface::Ptr> IndicatorsManager::indicators()
251 QList<IndicatorClientInterface::Ptr> list;
252 Q_FOREACH(IndicatorData* data, m_indicatorsData)
253 {
254 IndicatorClientInterface::Ptr plugin = indicator(data->m_name);
255 list.append(plugin);
256 }
257 return list;