add more spacing
[personal-kdebase.git] / workspace / plasma / dataengines / weather / weatherengine.cpp
blobc6a91c9bef9a4b9667feae0f7a01402b29469673
1 /***************************************************************************
2 * Copyright (C) 2007 by Shawn Starr <shawn.starr@rogers.com> *
3 * Copyright (C) 2009 by Aaron Seigo <aseigo@kde.org> *
4 * *
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. *
9 * *
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. *
14 * *
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"
22 #include <KServiceTypeTrader>
23 #include <KDateTime>
24 #include <KLocale>
26 #include <Plasma/DataEngineManager>
28 #include "ions/ion.h"
30 class WeatherEngine::Private
32 public:
33 /**
34 * Get instance of a loaded ion.
35 * @returns a IonInterface instance of a loaded plugin.
37 IonInterface* ionForSource(const QString& name) {
38 int offset = name.indexOf('|');
40 if (offset < 1) {
41 return NULL;
44 QString ionName = name.left(offset);
45 return qobject_cast<IonInterface *>(Plasma::DataEngineManager::self()->engine(ionName));
48 /**
49 * Get plugin name from datasource.
50 * @returns The plugin name given a datasource.
52 QString ionNameForSource(const QString& source) {
53 int offset = source.indexOf('|');
54 if (offset < 1) {
55 return QString();
58 return QString(source.left(offset));
61 KDateTime m_localTime;
62 QStringList m_ions;
65 /**
66 * Loads an ion plugin given a plugin name found via KService.
68 Plasma::DataEngine *WeatherEngine::loadIon(const QString& plugName)
70 KPluginInfo foundPlugin;
72 foreach (const KPluginInfo &info, Plasma::DataEngineManager::listEngineInfo("weatherengine")) {
73 if (info.pluginName() == plugName) {
74 foundPlugin = info;
75 break;
79 if (!foundPlugin.isValid()) {
80 return NULL;
83 // Load the Ion plugin, store it into a QMap to handle multiple ions.
84 Plasma::DataEngine *ion = Plasma::DataEngineManager::self()->loadEngine(foundPlugin.pluginName());
85 ion->setObjectName(plugName);
86 connect(ion, SIGNAL(sourceAdded(QString)), this, SLOT(newIonSource(QString)));
88 /* Set system related properties for the ion
89 * timezone is displaying the time/date in UTC or user's local time
90 * unit is setting the weather units used, Celsius/Fahrenheit, Kilopascals/Inches of Mercury, etc
93 ion->setProperty("timezone", d->m_localTime.isUtc());
94 ion->setProperty("unit", KGlobal::locale()->measureSystem());
95 d->m_ions << plugName;
97 return ion;
101 * Unload an Ion plugin given a Ion plugin name.
103 void WeatherEngine::unloadIon(const QString &name)
105 Plasma::DataEngineManager::self()->unloadEngine(name);
106 d->m_ions.removeOne(name);
109 void WeatherEngine::init()
111 // Get the list of available plugins but don't load them
112 foreach(const KPluginInfo &info, Plasma::DataEngineManager::listEngineInfo("weatherengine")) {
113 setData("ions", info.pluginName(),
114 QString("%1|%2").arg(info.property("Name").toString()).arg(info.pluginName()));
119 * SLOT: Get data from a new source
121 void WeatherEngine::newIonSource(const QString& source)
123 IonInterface *ion = qobject_cast<IonInterface*>(sender());
125 kDebug() << "New Ion Source" << source;
126 if (!ion) {
127 return;
130 ion->connectSource(source, this);
134 * SLOT: Remove the datasource from the ion and unload plugin if needed
136 void WeatherEngine::removeIonSource(const QString& source)
138 IonInterface *ion = d->ionForSource(source);
139 if (ion) {
140 ion->removeSource(source);
141 // If plugin has no more sources let's unload the plugin
142 if (ion->isEmpty()) {
143 kDebug() << "No more Sources found for this plugin let's unload it!";
144 unloadIon(d->ionNameForSource(source));
150 * SLOT: Push out new data to applet
152 void WeatherEngine::dataUpdated(const QString& source, Plasma::DataEngine::Data data)
154 kDebug() << "data updated" << source;
155 setData(source, data);
158 // Constructor
159 WeatherEngine::WeatherEngine(QObject *parent, const QVariantList& args)
160 : Plasma::DataEngine(parent, args), d(new Private())
162 Q_UNUSED(args)
164 // Set any local properties for Ion to use
165 d->m_localTime = KDateTime::currentDateTime(KDateTime::LocalZone);
167 // Globally notify all plugins to remove their sources (and unload plugin)
168 connect(this, SIGNAL(sourceRemoved(QString)), this, SLOT(removeIonSource(QString)));
171 // Destructor
172 WeatherEngine::~WeatherEngine()
174 // Cleanup all private data.
175 foreach (const QString &ion, d->m_ions) {
176 Plasma::DataEngineManager::self()->unloadEngine(ion);
179 delete d;
183 * SLOT: Set up each Ion for the first time and get any data
185 bool WeatherEngine::sourceRequestEvent(const QString &source)
187 kDebug() << "sourceRequestEvent()" << source;
188 Plasma::DataEngine *ion = d->ionForSource(source);
190 if (!ion) {
191 kDebug() << "sourceRequestEvent(): No Ion Found, load it up!";
192 ion = loadIon(d->ionNameForSource(source));
193 if (!ion) {
194 return false;
198 QByteArray str = source.toLocal8Bit();
200 ion->connectSource(source, this);
201 if (!containerForSource(source)) {
202 // it is an async reply, we need to set up the data anyways
203 kDebug() << "no item?";
204 setData(source, Data());
206 return true;
210 * SLOT: update the Applet with new data from all ions loaded.
212 bool WeatherEngine::updateSourceEvent(const QString& source)
214 IonInterface *ion = d->ionForSource(source);
216 QByteArray str = source.toLocal8Bit();
218 kDebug() << "updateSourceEvent()";
219 if (!ion) {
220 return false;
223 ion->setProperty("timezone", d->m_localTime.isUtc());
224 ion->setProperty("unit", KGlobal::locale()->measureSystem());
226 if (ion->updateSourceEvent(source)) {
227 return true;
228 } else {
229 return false;
233 #include "weatherengine.moc"