1
/***************************************************************************
2
* Copyright (C) 2007-2009 by Shawn Starr <shawn.starr@rogers.com> *
3
* Copyright (C) 2009 by Aaron Seigo <aseigo@kde.org> *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
19
***************************************************************************/
21
#include "weatherengine.h"
23
#include <QCoreApplication>
30
#include <Plasma/DataEngineManager>
31
#include <Plasma/DataContainer>
36
WeatherEngine::WeatherEngine(QObject *parent, const QVariantList& args)
37
: Plasma::DataEngine(parent, args),
38
m_networkAvailable(false)
42
m_reconnectTimer.setSingleShot(true);
43
connect(&m_reconnectTimer, SIGNAL(timeout()), this, SLOT(startReconnect()));
45
// Globally notify all plugins to remove their sources (and unload plugin)
46
connect(this, SIGNAL(sourceRemoved(QString)), this, SLOT(removeIonSource(QString)));
47
connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(unloadIons()));
51
WeatherEngine::~WeatherEngine()
53
// Cleanup all private data.
58
* Loads an ion plugin given a plugin name found via KService.
60
Plasma::DataEngine *WeatherEngine::loadIon(const QString& plugName)
62
KPluginInfo foundPlugin;
64
foreach(const KPluginInfo &info, Plasma::DataEngineManager::listEngineInfo("weatherengine")) {
65
if (info.pluginName() == plugName) {
71
if (!foundPlugin.isValid()) {
75
// Load the Ion plugin, store it into a QMap to handle multiple ions.
76
Plasma::DataEngine *ion = Plasma::DataEngineManager::self()->loadEngine(foundPlugin.pluginName());
77
ion->setObjectName(plugName);
78
connect(ion, SIGNAL(sourceAdded(QString)), this, SLOT(newIonSource(QString)));
79
connect(ion, SIGNAL(forceUpdate(IonInterface*,QString)), this, SLOT(forceUpdate(IonInterface*,QString)));
86
/* FIXME: Q_PROPERTY functions to update the list of available plugins */
89
* Unload an Ion plugin given a Ion plugin name.
91
void WeatherEngine::unloadIon(const QString &name)
93
Plasma::DataEngineManager::self()->unloadEngine(name);
94
m_ions.removeOne(name);
97
void WeatherEngine::init()
99
// Get the list of available plugins but don't load them
100
Solid::Networking::Status status = Solid::Networking::status();
101
m_networkAvailable = (status == Solid::Networking::Connected ||
102
status == Solid::Networking::Unknown);
103
connect(Solid::Networking::notifier(), SIGNAL(statusChanged(Solid::Networking::Status)),
104
this, SLOT(networkStatusChanged(Solid::Networking::Status)));
105
connect(KSycoca::self(), SIGNAL(databaseChanged(QStringList)), this, SLOT(updateIonList()));
108
kDebug() << "init()";
111
void WeatherEngine::updateIonList(const QStringList &changedResources)
113
if (changedResources.isEmpty() || changedResources.contains("services")) {
114
removeAllData("ions");
115
foreach (const KPluginInfo &info, Plasma::DataEngineManager::listEngineInfo("weatherengine")) {
116
setData("ions", info.pluginName(),
117
QString("%1|%2").arg(info.property("Name").toString()).arg(info.pluginName()));
123
* SLOT: Get data from a new source
125
void WeatherEngine::newIonSource(const QString& source)
127
IonInterface *ion = qobject_cast<IonInterface*>(sender());
133
kDebug() << "newIonSource()";
134
ion->connectSource(source, this);
138
* SLOT: Remove the datasource from the ion and unload plugin if needed
140
void WeatherEngine::removeIonSource(const QString& source)
142
IonInterface *ion = ionForSource(source);
144
ion->removeSource(source);
145
// If plugin has no more sources let's unload the plugin
146
if (ion->isEmpty()) {
147
unloadIon(ionNameForSource(source));
150
kDebug() << "removeIonSource()";
154
* SLOT: Push out new data to applet
156
void WeatherEngine::dataUpdated(const QString& source, Plasma::DataEngine::Data data)
158
kDebug() << "dataUpdated()";
159
setData(source, data);
162
void WeatherEngine::unloadIons()
164
foreach (const QString &ion, m_ions) {
165
Plasma::DataEngineManager::self()->unloadEngine(ion);
172
* SLOT: Set up each Ion for the first time and get any data
174
bool WeatherEngine::sourceRequestEvent(const QString &source)
176
Plasma::DataEngine *ion = ionForSource(source);
179
ion = loadIon(ionNameForSource(source));
185
// we should connect to the ion anyway, even if the network
186
// is down. when it comes up again, then it will be refreshed
187
ion->connectSource(source, this);
189
kDebug() << "sourceRequestEvent(): Network is: " << m_networkAvailable;
190
if (!m_networkAvailable) {
191
setData(source, Data());
195
if (!containerForSource(source)) {
196
// it is an async reply, we need to set up the data anyways
197
setData(source, Data());
203
* SLOT: update the Applet with new data from all ions loaded.
205
bool WeatherEngine::updateSourceEvent(const QString& source)
207
IonInterface *ion = ionForSource(source);
212
kDebug() << "updateSourceEvent(): Network is: " << m_networkAvailable;
213
if (!m_networkAvailable) {
217
return ion->updateSourceEvent(source);
220
void WeatherEngine::networkStatusChanged(Solid::Networking::Status status)
223
m_networkAvailable = status == Solid::Networking::Connected || status == Solid::Networking::Unknown;
224
if (m_networkAvailable) {
225
// allow the network to settle down and actually come up
226
m_reconnectTimer.start(5000);
230
void WeatherEngine::startReconnect()
232
foreach (const QString &i, m_ions) {
233
IonInterface * ion = qobject_cast<IonInterface *>(Plasma::DataEngineManager::self()->engine(i));
234
kDebug() << "resetting" << ion;
241
void WeatherEngine::forceUpdate(IonInterface *i, const QString &source)
243
const QString actualSource(i->pluginName() + '|' + source);
244
Plasma::DataContainer *container = containerForSource(source);
246
kDebug() << "immediate update of" << source;
247
container->forceImmediateUpdate();
249
kDebug() << "innexplicable failure of" << source;
253
IonInterface* WeatherEngine::ionForSource(const QString& name) const
255
int offset = name.indexOf('|');
261
QString ionName = name.left(offset);
262
return qobject_cast<IonInterface *>(Plasma::DataEngineManager::self()->engine(ionName));
265
QString WeatherEngine::ionNameForSource(const QString& source) const
267
int offset = source.indexOf('|');
272
return QString(source.left(offset));
275
#include "weatherengine.moc"