Plasma5Support

geolocation.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Petri Damsten <damu@iki.fi>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "geolocation.h"
8
9#include <limits.h>
10
11#include <KPluginMetaData>
12#include <NetworkManagerQt/Manager>
13#include <QDebug>
14
15static const char SOURCE[] = "location";
16
17Geolocation::Geolocation(QObject *parent)
18 : Plasma5Support::DataEngine(parent)
19{
20 setMinimumPollingInterval(500);
21 connect(NetworkManager::notifier(), &NetworkManager::Notifier::networkingEnabledChanged, this, &Geolocation::networkStatusChanged);
22 connect(NetworkManager::notifier(), &NetworkManager::Notifier::wirelessEnabledChanged, this, &Geolocation::networkStatusChanged);
23 m_updateTimer.setInterval(100);
24 m_updateTimer.setSingleShot(true);
25 connect(&m_updateTimer, &QTimer::timeout, this, &Geolocation::actuallySetData);
26 m_networkChangedTimer.setInterval(100);
27 m_networkChangedTimer.setSingleShot(true);
28 connect(&m_networkChangedTimer, &QTimer::timeout, this, [this] {
29 updatePlugins(GeolocationProvider::NetworkConnected);
30 });
31 init();
32}
33
34void Geolocation::init()
35{
36 // TODO: should this be delayed even further, e.g. when the source is requested?
37 const QList<KPluginMetaData> offers = KPluginMetaData::findPlugins("plasma5support/geolocationprovider", {}, KPluginMetaData::AllowEmptyMetaData);
38 for (const auto &metaData : offers) {
39 auto result = KPluginFactory::instantiatePlugin<GeolocationProvider>(metaData, this);
40 if (result) {
41 GeolocationProvider *plugin = result.plugin;
42 m_plugins << plugin;
43 plugin->init(&m_data, &m_accuracy);
44 connect(plugin, &GeolocationProvider::updated, this, &Geolocation::pluginUpdated);
45 connect(plugin, &GeolocationProvider::availabilityChanged, this, &Geolocation::pluginAvailabilityChanged);
46 } else {
47 qDebug() << "Failed to load GeolocationProvider:" << metaData.fileName() << result.errorString;
48 }
49 }
50}
51
52Geolocation::~Geolocation()
53{
54 qDeleteAll(m_plugins);
55}
56
57QStringList Geolocation::sources() const
58{
59 return QStringList() << SOURCE;
60}
61
62bool Geolocation::updateSourceEvent(const QString &name)
63{
64 // qDebug() << name;
65 if (name == SOURCE) {
66 return updatePlugins(GeolocationProvider::SourceEvent);
67 }
68
69 return false;
70}
71
72bool Geolocation::updatePlugins(GeolocationProvider::UpdateTriggers triggers)
73{
74 bool changed = false;
75
76 for (GeolocationProvider *plugin : std::as_const(m_plugins)) {
77 changed = plugin->requestUpdate(triggers) || changed;
78 }
79
80 if (changed) {
81 m_updateTimer.start();
82 }
83
84 return changed;
85}
86
87bool Geolocation::sourceRequestEvent(const QString &name)
88{
89 qDebug() << name;
90 if (name == SOURCE) {
91 updatePlugins(GeolocationProvider::ForcedUpdate);
92 setData(SOURCE, m_data);
93 return true;
94 }
95
96 return false;
97}
98
99void Geolocation::networkStatusChanged(bool isOnline)
100{
101 qDebug() << "network status changed";
102 if (isOnline) {
103 m_networkChangedTimer.start();
104 }
105}
106
107void Geolocation::pluginAvailabilityChanged(GeolocationProvider *provider)
108{
109 m_data.clear();
110 m_accuracy.clear();
111
112 provider->requestUpdate(GeolocationProvider::ForcedUpdate);
113
114 bool changed = false;
115 for (GeolocationProvider *plugin : std::as_const(m_plugins)) {
116 changed = plugin->populateSharedData() || changed;
117 }
118
119 if (changed) {
120 m_updateTimer.start();
121 }
122}
123
124void Geolocation::pluginUpdated()
125{
126 m_updateTimer.start();
127}
128
129void Geolocation::actuallySetData()
130{
131 setData(SOURCE, m_data);
132}
133
134K_PLUGIN_CLASS_WITH_JSON(Geolocation, "plasma-dataengine-geolocation.json")
135
136#include "geolocation.moc"
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
static QList< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter={}, KPluginMetaDataOptions options={})
void setData(const QString &source, const QVariant &value)
Sets a value for a data source.
QString name(StandardAction id)
Namespace for everything in libplasma.
Definition datamodel.cpp:15
QCA_EXPORT void init()
void clear()
void clear()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void start()
void timeout()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.