1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
/*
* Copyright (C) 2013-2016 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "indicatorsmanager.h"
#include <QSettings>
#include <QDebug>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusServiceWatcher>
#include <QRegularExpression>
#include <paths.h>
class IndicatorsManager::IndicatorData
{
public:
IndicatorData(const QString& name, const QFileInfo& fileInfo)
: m_name(name)
, m_fileInfo(fileInfo)
, m_verified (true)
{
}
QString m_name;
QFileInfo m_fileInfo;
bool m_verified;
Indicator::Ptr m_indicator;
};
IndicatorsManager::IndicatorsManager(QObject* parent)
: QObject(parent)
, m_loaded(false)
, m_profile(QStringLiteral("phone"))
{
}
IndicatorsManager::~IndicatorsManager()
{
unload();
}
void IndicatorsManager::load()
{
unload();
m_fsWatcher.reset(new QFileSystemWatcher(this));
for (const auto &xdgPath : shellDataDirs()) {
// For legacy reasons we keep the old unity indicator path
const auto unityPath = QDir::cleanPath(xdgPath + "/unity/indicators");
if (QFile::exists(unityPath)) {
// watch folder for changes.
m_fsWatcher->addPath(unityPath);
loadDir(unityPath);
}
const auto ayatanaPath = QDir::cleanPath(xdgPath + "/ayatana/indicators");
if (QFile::exists(ayatanaPath)) {
// watch folder for changes.
m_fsWatcher->addPath(ayatanaPath);
loadDir(ayatanaPath);
}
}
QObject::connect(m_fsWatcher.data(), &QFileSystemWatcher::directoryChanged, this, &IndicatorsManager::onDirectoryChanged);
QObject::connect(m_fsWatcher.data(), &QFileSystemWatcher::fileChanged, this, &IndicatorsManager::onFileChanged);
setLoaded(true);
}
void IndicatorsManager::onDirectoryChanged(const QString& directory)
{
loadDir(QDir(directory));
}
void IndicatorsManager::onFileChanged(const QString& file)
{
QFileInfo file_info(file);
if (!file_info.exists())
{
unloadFile(file_info);
return;
}
else
{
loadFile(QFileInfo(file));
}
}
void IndicatorsManager::loadDir(const QDir& dir)
{
startVerify(dir.canonicalPath());
const QFileInfoList indicator_files = dir.entryInfoList(QStringList(), QDir::Files|QDir::NoDotAndDotDot);
Q_FOREACH(const QFileInfo& indicator_file, indicator_files)
{
loadFile(indicator_file);
}
endVerify(dir.canonicalPath());
}
void IndicatorsManager::loadFile(const QFileInfo& file_info)
{
// The fake indicators in the unit tests are not registered, so skip them
QString service = file_info.fileName ();
QRegularExpression regex ("^com\\.canonical\\.indicator\\.fake\\d$");
QRegularExpressionMatch match = regex.match (service);
bool test = match.hasMatch ();
if (!test)
{
QDBusConnection connection = QDBusConnection::sessionBus ();
QDBusConnectionInterface *interface = connection.interface ();
bool registered = interface->isServiceRegistered (service);
if (!registered)
{
auto watcher = new QDBusServiceWatcher (service, connection, QDBusServiceWatcher::WatchForRegistration, this);
connect (watcher, &QDBusServiceWatcher::serviceRegistered, this, [this, file_info, watcher] ()
{
watcher->deleteLater ();
this->loadFile (file_info);
});
return;
}
}
QSettings indicator_settings(file_info.absoluteFilePath(), QSettings::IniFormat, this);
const QString name = indicator_settings.value(QStringLiteral("Indicator Service/Name")).toString();
auto iter = m_indicatorsData.constFind(name);
if (iter != m_indicatorsData.constEnd())
{
const QString newFileInfoDir = QDir::cleanPath(file_info.canonicalPath());
IndicatorData* currentData = (*iter);
currentData->m_verified = true;
int file_info_location = -1;
int current_data_location = -1;
const QString currentDataDir = QDir::cleanPath(currentData->m_fileInfo.canonicalPath());
// if we've already got this indicator, we need to make sure we're not overwriting data which is
// from a lower priority standard path
QStringList xdgLocations = shellDataDirs();
for (int i = 0; i < xdgLocations.size(); i++)
{
const QString xdgLocation = QDir::cleanPath(xdgLocations[i]);
if (newFileInfoDir.startsWith(xdgLocation))
{
file_info_location = i;
}
if (currentDataDir.startsWith(xdgLocation))
{
current_data_location = i;
}
if (file_info_location != -1 && current_data_location != -1)
{
break;
}
}
// file location is higher (or of equal) priority. overwrite.
if (file_info_location <= current_data_location &&
file_info != currentData->m_fileInfo)
{
currentData->m_fileInfo = file_info;
Q_EMIT indicatorLoaded(name);
}
}
else
{
IndicatorData* data = new IndicatorData(name, file_info);
data->m_verified = true;
m_indicatorsData[name]= data;
Q_EMIT indicatorLoaded(name);
}
}
void IndicatorsManager::unload()
{
QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
while(iter.hasNext())
{
iter.next();
Q_EMIT indicatorAboutToBeUnloaded(iter.key());
}
qDeleteAll(m_indicatorsData);
m_indicatorsData.clear();
setLoaded(false);
}
void IndicatorsManager::unloadFile(const QFileInfo& file)
{
QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
while(iter.hasNext())
{
iter.next();
IndicatorData* data = iter.value();
if (data->m_fileInfo.absoluteFilePath() == file.absoluteFilePath())
{
if (!data->m_verified)
{
const QString name = data->m_name;
Q_EMIT indicatorAboutToBeUnloaded(name);
delete data;
iter.remove();
}
}
}
setLoaded(m_indicatorsData.size() > 0);
}
void IndicatorsManager::setLoaded(bool loaded)
{
if (loaded != m_loaded)
{
m_loaded = loaded;
Q_EMIT loadedChanged(m_loaded);
}
}
QString IndicatorsManager::profile() const
{
return m_profile;
}
void IndicatorsManager::setProfile(const QString& profile)
{
if (m_profile != profile) {
m_profile = profile;
Q_EMIT profileChanged(m_profile);
}
}
void IndicatorsManager::startVerify(const QString& path)
{
QHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
while(iter.hasNext())
{
iter.next();
IndicatorData* data = iter.value();
if (data->m_fileInfo.canonicalPath() == path)
{
data->m_verified = false;
}
}
}
void IndicatorsManager::endVerify(const QString& path)
{
QMutableHashIterator<QString, IndicatorData*> iter(m_indicatorsData);
while(iter.hasNext())
{
iter.next();
IndicatorData* data = iter.value();
if (data->m_fileInfo.canonicalPath() == path)
{
if (!data->m_verified)
{
const QString name = data->m_name;
Q_EMIT indicatorAboutToBeUnloaded(name);
delete data;
iter.remove();
}
}
}
}
Indicator::Ptr IndicatorsManager::indicator(const QString& indicator_name)
{
if (!m_indicatorsData.contains(indicator_name))
{
qWarning() << Q_FUNC_INFO << "Invalid indicator name: " << indicator_name;
return Indicator::Ptr();
}
IndicatorData *data = m_indicatorsData.value(indicator_name);
if (data->m_indicator)
{
return data->m_indicator;
}
Indicator::Ptr new_indicator(new Indicator(this));
data->m_indicator = new_indicator;
QSettings settings(data->m_fileInfo.absoluteFilePath(), QSettings::IniFormat, this);
new_indicator->init(data->m_fileInfo.fileName(), settings);
// convergence:
// 1) enable session indicator
// 2) enable keyboard indicator
//
// The rest of the indicators respect their default profile (which is "phone", even on desktop PCs)
if ((new_indicator->identifier() == QStringLiteral("ayatana-indicator-session"))
|| new_indicator->identifier() == QStringLiteral("ayatana-indicator-keyboard")) {
new_indicator->setProfile(QString(m_profile).replace(QStringLiteral("phone"), QStringLiteral("desktop")));
} else {
new_indicator->setProfile(m_profile);
}
QObject::connect(this, &IndicatorsManager::profileChanged, new_indicator.data(), &Indicator::setProfile);
return new_indicator;
}
QVector<Indicator::Ptr> IndicatorsManager::indicators()
{
QVector<Indicator::Ptr> list;
Q_FOREACH(IndicatorData* data, m_indicatorsData)
{
Indicator::Ptr ret = indicator(data->m_name);
if (ret)
list.append(ret);
}
return list;
}
bool IndicatorsManager::isLoaded() const
{
return m_loaded;
}
|