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"
27class IndicatorsManager::IndicatorData
30 IndicatorData(const QString& name, const QFileInfo& fileInfo)
32 , m_fileInfo(fileInfo)
40 Indicator::Ptr m_indicator;
43IndicatorsManager::IndicatorsManager(QObject* parent)
49IndicatorsManager::~IndicatorsManager()
54void IndicatorsManager::load()
57 QStringList xdgLocations = shellDataDirs();//QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
59 m_fsWatcher.reset(new QFileSystemWatcher(this));
61 Q_FOREACH(const QString& xdgLocation, xdgLocations)
63 QString indicator_path = QDir::cleanPath(xdgLocation + "/unity/indicators");
64 QDir indicator_dir(indicator_path);
65 if (indicator_dir.exists())
67 // watch folder for changes.
68 m_fsWatcher->addPath(indicator_path);
70 loadDir(indicator_dir);
74 QObject::connect(m_fsWatcher.data(), SIGNAL(directoryChanged(const QString&)), this, SLOT(onDirectoryChanged(const QString&)));
75 QObject::connect(m_fsWatcher.data(), SIGNAL(fileChanged(const QString&)), this, SLOT(onFileChanged(const QString&)));
79void IndicatorsManager::onDirectoryChanged(const QString& directory)
81 loadDir(QDir(directory));
84void IndicatorsManager::onFileChanged(const QString& file)
86 QFileInfo file_info(file);
87 if (!file_info.exists())
89 unloadFile(file_info);
94 loadFile(QFileInfo(file));
98void IndicatorsManager::loadDir(const QDir& dir)
100 startVerify(dir.canonicalPath());
102 QFileInfoList indicator_files = dir.entryInfoList(QStringList(), QDir::Files|QDir::NoDotAndDotDot);
103 Q_FOREACH(const QFileInfo& indicator_file, indicator_files)
105 loadFile(indicator_file);
108 endVerify(dir.canonicalPath());
111void IndicatorsManager::loadFile(const QFileInfo& file_info)
113 QSettings indicator_settings(file_info.absoluteFilePath(), QSettings::IniFormat, this);
114 QString name = indicator_settings.value("Indicator Service/Name").toString();
116 auto iter = m_indicatorsData.find(name);
117 if (iter != m_indicatorsData.end())
119 QString newFileInfoDir = QDir::cleanPath(file_info.canonicalPath());
120 IndicatorData* currentData = (*iter);
121 currentData->m_verified = true;
123 int file_info_location = -1;
124 int current_data_location = -1;
126 QString currentDataDir = QDir::cleanPath(currentData->m_fileInfo.canonicalPath());
128 // if we've already got this indicator, we need to make sure we're not overwriting data which is
129 // from a lower priority standard path
130 QStringList xdgLocations = shellDataDirs();
131 for (int i = 0; i < xdgLocations.size(); i++)
133 QString indicatorDir = QDir::cleanPath(xdgLocations[i] + "/unity/indicators");
135 if (newFileInfoDir == indicatorDir)
137 file_info_location = i;
139 if (currentDataDir == indicatorDir)
141 current_data_location = i;
144 if (file_info_location != -1 && current_data_location != -1)
150 // file location is higher (or of equal) priority. overwrite.
151 if (file_info_location <= current_data_location &&
152 file_info != currentData->m_fileInfo)
154 currentData->m_fileInfo = file_info;
155 Q_EMIT indicatorLoaded(name);
160 IndicatorData* data = new IndicatorData(name, file_info);
161 data->m_verified = true;
162 m_indicatorsData[name]= data;
163 Q_EMIT indicatorLoaded(name);
167void IndicatorsManager::unload()
169 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
170 while(iter.hasNext())
173 Q_EMIT indicatorAboutToBeUnloaded(iter.key());
176 qDeleteAll(m_indicatorsData);
177 m_indicatorsData.clear();
182void IndicatorsManager::unloadFile(const QFileInfo& file)
184 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
185 while(iter.hasNext())
188 IndicatorData* data = iter.value();
189 if (data->m_fileInfo.absoluteFilePath() == file.absoluteFilePath())
191 if (!data->m_verified)
193 QString name = data->m_name;
194 Q_EMIT indicatorAboutToBeUnloaded(name);
202 setLoaded(m_indicatorsData.size() > 0);
205void IndicatorsManager::setLoaded(bool loaded)
207 if (loaded != m_loaded)
210 Q_EMIT loadedChanged(m_loaded);
214void IndicatorsManager::startVerify(const QString& path)
216 QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
217 while(iter.hasNext())
220 IndicatorData* data = iter.value();
221 if (data->m_fileInfo.canonicalPath() == path)
223 data->m_verified = false;
228void IndicatorsManager::endVerify(const QString& path)
230 QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
231 while(iter.hasNext())
234 IndicatorData* data = iter.value();
235 if (data->m_fileInfo.canonicalPath() == path)
237 if (!data->m_verified)
239 QString name = data->m_name;
240 Q_EMIT indicatorAboutToBeUnloaded(name);
249Indicator::Ptr IndicatorsManager::indicator(const QString& indicator_name)
251 if (!m_indicatorsData.contains(indicator_name))
253 qWarning() << Q_FUNC_INFO << "Invalid indicator name: " << indicator_name;
254 return Indicator::Ptr();
257 IndicatorData *data = m_indicatorsData[indicator_name];
258 if (data->m_indicator)
260 return data->m_indicator;
263 Indicator::Ptr new_indicator(new Indicator(this));
264 data->m_indicator = new_indicator;
265 QSettings settings(data->m_fileInfo.absoluteFilePath(), QSettings::IniFormat, this);
266 new_indicator->init(data->m_fileInfo.fileName(), settings);
267 return new_indicator;
270QList<Indicator::Ptr> IndicatorsManager::indicators()
272 QList<Indicator::Ptr> list;
273 Q_FOREACH(IndicatorData* data, m_indicatorsData)
275 Indicator::Ptr ret = indicator(data->m_name);
282bool 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