Lomiri
LocationWatcher.cpp
1 /*
2  * Copyright (C) 2021 UBports Foundation.
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  * Authors: Alberto Mardegan <mardy@users.sourceforge.net>
17  */
18 
19 #include "LocationWatcher.h"
20 
21 #include "ProcessControl.h"
22 
23 #include <QDBusConnection>
24 #include <QDebug>
25 #include <QStringList>
26 #include <QTimer>
27 
28 namespace {
29 const QString locationServiceName = QStringLiteral("com.lomiri.location.Service");
30 const QString locationObjectPath = QStringLiteral("/com/lomiri/location/Service");
31 const QString propertiesInterface = QStringLiteral("org.freedesktop.DBus.Properties");
32 const QString methodPropertiesChanged = QStringLiteral("PropertiesChanged");
33 } // namespace
34 
35 class LocationWatcherPrivate: public QObject
36 {
37  Q_OBJECT
38 
39 public:
40  LocationWatcherPrivate(ProcessControl *processControl);
41 
42 private Q_SLOTS:
43  void onPropertiesChanged(const QString &interface,
44  const QVariantMap &changedProps,
45  const QStringList &invalidatedProps);
46 
47 private:
48  friend class LocationWatcher;
49  ProcessControl *m_processControl;
50  QDBusConnection m_connection;
51  QStringList m_clientApplications;
52 };
53 
54 LocationWatcherPrivate::LocationWatcherPrivate(ProcessControl *processControl):
55  QObject(),
56  m_processControl(processControl),
57  m_connection(QDBusConnection::systemBus())
58 {
59  m_connection.connect(locationServiceName,
60  locationObjectPath,
61  propertiesInterface,
62  methodPropertiesChanged,
63  this,
64  SLOT(onPropertiesChanged(QString,QVariantMap,QStringList)));
65 }
66 
67 void LocationWatcherPrivate::onPropertiesChanged(const QString &interface,
68  const QVariantMap &changedProps,
69  const QStringList &invalidatedProps)
70 {
71  Q_UNUSED(interface);
72  Q_UNUSED(invalidatedProps);
73 
74  qDebug() << Q_FUNC_INFO << changedProps;
75  const auto i = changedProps.find(QStringLiteral("ClientApplications"));
76  if (i != changedProps.end()) {
77  const QStringList appIds = i.value().toStringList();
78  qDebug() << "Location clients changed:" << appIds;
79  /* We need to strip off the version (app IDs are in the form
80  * "<publisher>_<app-name>_<version>") */
81  m_clientApplications.clear();
82  for (const QString &appId: appIds) {
83  QStringList parts = appId.split('_');
84  QString versionLessAppId = parts.mid(0, 2).join('_');
85  m_clientApplications.append(versionLessAppId);
86  }
87  m_processControl->setAwakenProcesses(m_clientApplications);
88  }
89 }
90 
91 LocationWatcher::LocationWatcher(ProcessControl *processControl):
92  QObject(processControl),
93  d_ptr(new LocationWatcherPrivate(processControl))
94 {
95 }
96 
97 LocationWatcher::~LocationWatcher() = default;
98 
99 #include "LocationWatcher.moc"