2 * Copyright (C) 2013 Canonical, Ltd.
5 * Michał Sawicz <michal.sawicz@canonical.com>
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.
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.
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/>.
20#include "indicatorsmanager.h"
21#include "indicatorsfactory.h"
23#include <QStandardPaths>
28class IndicatorsManager::IndicatorData
31 IndicatorData(const QString& name, const QFileInfo& fileInfo)
33 , m_fileInfo(fileInfo)
41 IndicatorClientInterface::Ptr m_obj;
44IndicatorsManager::IndicatorsManager(QObject* parent)
50IndicatorsManager::~IndicatorsManager()
55void IndicatorsManager::load()
58 QStringList xdgLocations = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
60 m_fsWatcher.reset(new QFileSystemWatcher(this));
62 Q_FOREACH(const QString& xdgLocation, xdgLocations)
64 QString indicator_path = QDir::cleanPath(xdgLocation + "/unity/indicators");
65 QDir indicator_dir(indicator_path);
66 if (indicator_dir.exists())
68 // watch folder for changes.
69 m_fsWatcher->addPath(indicator_path);
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&)));
80void IndicatorsManager::onDirectoryChanged(const QString& direcory)
85void IndicatorsManager::onFileChanged(const QString& file)
87 QFileInfo file_info(file);
88 if (!file_info.exists())
95 load(QFileInfo(file));
99void IndicatorsManager::load(const QDir& dir)
101 startVerify(dir.canonicalPath());
103 QFileInfoList indicator_files = dir.entryInfoList(QStringList() << "*.indicator", QDir::Files);
104 Q_FOREACH(const QFileInfo& indicator_file, indicator_files)
106 load(indicator_file);
109 endVerify(dir.canonicalPath());
112void IndicatorsManager::load(const QFileInfo& file_info)
114 QSettings indicator_settings(file_info.absoluteFilePath(), QSettings::IniFormat, this);
115 indicator_settings.beginGroup("Indicator Service");
116 QString name = indicator_settings.value("Name").toString();
118 // don't bother loading if it's not registered.
119 if (!IndicatorsFactory::instance().isRegistered(name))
121 qWarning() << Q_FUNC_INFO << "Invalid plugin name: " << name << " - not registered in factory.";
125 auto iter = m_indicatorsData.find(name);
126 if (iter != m_indicatorsData.end())
128 QString newFileInfoDir = QDir::cleanPath(file_info.canonicalPath());
129 IndicatorData* currentData = (*iter);
130 currentData->m_verified = true;
132 int file_info_location = -1;
133 int current_data_location = -1;
135 QString currentDataDir = QDir::cleanPath(currentData->m_fileInfo.canonicalPath());
137 // if we've already got this indicator, we need to make sure we're not overwriting data which is
138 // from a lower priority standard path
139 QStringList xdgLocations = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
140 for (int i = 0; i < xdgLocations.size(); i++)
142 QString indicatorDir = QDir::cleanPath(xdgLocations[i] + "/unity/indicators");
144 if (newFileInfoDir == indicatorDir)
145 file_info_location = i;
146 if (currentDataDir == indicatorDir)
147 current_data_location = i;
149 if (file_info_location != -1 && current_data_location != -1)
153 // file location is higher (or of equal) priority. overwrite.
154 if (file_info_location <= current_data_location &&
155 file_info != currentData->m_fileInfo)
157 currentData->m_fileInfo = file_info;
158 Q_EMIT indicatorLoaded(name);
163 IndicatorData* data = new IndicatorData(name, file_info);
164 data->m_verified = true;
165 m_indicatorsData[name]= data;
166 Q_EMIT indicatorLoaded(name);
170void IndicatorsManager::unload()
172 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
173 while(iter.hasNext())
176 IndicatorData* data = iter.value();
179 data->m_obj->shutdown();
181 Q_EMIT indicatorAboutToBeUnloaded(iter.key());
184 qDeleteAll(m_indicatorsData);
185 m_indicatorsData.clear();
190void IndicatorsManager::unload(const QFileInfo& file)
192 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
193 while(iter.hasNext())
196 IndicatorData* data = iter.value();
197 if (data->m_fileInfo.absoluteFilePath() == file.absoluteFilePath())
199 if (!data->m_verified)
201 QString name = data->m_name;
202 Q_EMIT indicatorAboutToBeUnloaded(name);
210 setLoaded(m_indicatorsData.size() > 0);
213void IndicatorsManager::setLoaded(bool loaded)
215 if (loaded != m_loaded)
218 Q_EMIT loadedChanged(m_loaded);
222void IndicatorsManager::startVerify(const QString& path)
224 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
225 while(iter.hasNext())
228 IndicatorData* data = iter.value();
229 if (data->m_fileInfo.canonicalPath() == path)
230 data->m_verified = false;
234void IndicatorsManager::endVerify(const QString& path)
236 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
237 while(iter.hasNext())
240 IndicatorData* data = iter.value();
241 if (data->m_fileInfo.canonicalPath() == path)
243 if (!data->m_verified)
245 QString name = data->m_name;
246 Q_EMIT indicatorAboutToBeUnloaded(name);
255IndicatorClientInterface::Ptr IndicatorsManager::indicator(const QString& indicator)
257 if (!m_indicatorsData.contains(indicator)) {
258 qWarning() << Q_FUNC_INFO << "Invalid plugin name: " << indicator;
262 IndicatorData *data = m_indicatorsData[indicator];
266 IndicatorClientInterface::Ptr plugin = IndicatorsFactory::instance().create(indicator, this);
269 data->m_obj = plugin;
270 plugin->init(QSettings(data->m_fileInfo.absoluteFilePath(), QSettings::IniFormat, this));
275QList<IndicatorClientInterface::Ptr> IndicatorsManager::indicators()
277 QList<IndicatorClientInterface::Ptr> list;
278 Q_FOREACH(IndicatorData* data, m_indicatorsData)
280 IndicatorClientInterface::Ptr plugin = indicator(data->m_name);
286bool 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