• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Engines

weatherengine.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Shawn Starr <shawn.starr@rogers.com>            *
00003  *                                                                         *
00004  *   This program is free software; you can redistribute it and/or modify  *
00005  *   it under the terms of the GNU General Public License as published by  *
00006  *   the Free Software Foundation; either version 2 of the License, or     *
00007  *   (at your option) any later version.                                   *
00008  *                                                                         *
00009  *   This program is distributed in the hope that it will be useful,       *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012  *   GNU General Public License for more details.                          *
00013  *                                                                         *
00014  *   You should have received a copy of the GNU General Public License     *
00015  *   along with this program; if not, write to the                         *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
00018  ***************************************************************************/
00019 
00020 #include "weatherengine.h"
00021 #include <KServiceTypeTrader>
00022 #include <KDateTime>
00023 #include <KLocale>
00024 #include "ions/ion.h"
00025 
00026 class WeatherEngine::Private
00027 {
00028 public:
00029     Private() {}
00030     ~Private() {
00031         qDeleteAll(m_ions);
00032     }
00033 
00038     IonInterface* ionForSource(const QString& name) {
00039         int offset = name.indexOf('|');
00040 
00041         if (offset < 1) {
00042             return NULL;
00043         }
00044 
00045         QString ionName = name.left(offset);
00046 
00047 
00048         if (m_ions.contains(ionName)) {
00049             return m_ions[ionName];
00050         }
00051 
00052         return NULL;
00053     }
00054 
00059     QString ionNameForSource(const QString& source) {
00060         int offset = source.indexOf('|');
00061         if (offset < 1) {
00062             return QString();
00063         }
00064 
00065         return QString(source.left(offset));
00066     }
00067 
00068     KService::List m_ionServices;
00069     IonInterface::IonDict m_ions;
00070     KDateTime m_localTime;
00071 };
00072 
00076 IonInterface* WeatherEngine::Ion(const QString& name) const
00077 {
00078     IonInterface::IonDict::const_iterator it = d->m_ions.find(name);
00079     if (it != d->m_ions.end()) {
00080         return *it;
00081     }
00082 
00083     return NULL;
00084 }
00085 
00089 IonInterface* WeatherEngine::loadIon(const QString& plugName)
00090 {
00091     IonInterface *ion = 0;
00092     KService::Ptr foundPlugin;
00093 
00094     foreach(const KService::Ptr &service, d->m_ionServices) {
00095         if (service->property("X-IonName").toString() == plugName) {
00096             foundPlugin = service;
00097             break;
00098         }
00099     }
00100 
00101     // Check if the plugin is already loaded if so, return the plugin thats already loaded.
00102     IonInterface::IonDict::const_iterator it = d->m_ions.find(plugName);
00103 
00104     if (it != d->m_ions.end()) {
00105         ion = *it;
00106         ion->ref();
00107         return ion;
00108     }
00109 
00110     QString error;
00111 
00112     // Do we have a valid plugin?
00113     if (!foundPlugin) {
00114         return NULL;
00115     }
00116 
00117     // Load the Ion plugin, store it into a QMap to handle multiple ions.
00118     ion = foundPlugin->createInstance<IonInterface>(0, QVariantList(), &error);
00119     ion->setObjectName(plugName);
00120     if (!ion) {
00121         kDebug() << "weatherengine: Couldn't load ion \"" << plugName << "\"!" << error;
00122         return NULL;
00123     }
00124 
00125     // Increment counter of ion.
00126     ion->init();
00127     ion->ref();
00128 
00129     connect(ion, SIGNAL(sourceAdded(QString)), this, SLOT(newIonSource(QString)));
00130 
00131     /* Set system related properties for the ion
00132      * timezone is displaying the time/date in UTC or user's local time
00133      * unit is setting the weather units used, Celsius/Fahrenheit, Kilopascals/Inches of Mercury, etc
00134      */
00135 
00136     ion->setProperty("timezone", d->m_localTime.isUtc());
00137     ion->setProperty("unit", KGlobal::locale()->measureSystem());
00138 
00139     // Assign the instantiated ion the key of the name of the ion.
00140     if (!d->m_ions.contains(plugName)) {
00141         d->m_ions[plugName] = ion;
00142     }
00143 
00144     return ion;
00145 }
00146 
00150 void WeatherEngine::unloadIon(const QString &name)
00151 {
00152     IonInterface *ion = Ion(name);
00153 
00154     if (ion) {
00155         ion->deref();
00156         kDebug() << "Unloading Plugin: " << name;
00157         if (!ion->isUsed()) {
00158             kDebug() << "It's not used anymore, delete it!";
00159             d->m_ions.remove(name);
00160             delete ion;
00161         }
00162     }
00163 }
00164 
00168 KService::List WeatherEngine::knownIons()
00169 {
00170     KService::List offers = KServiceTypeTrader::self()->query("WeatherEngine/Ion");
00171 
00172     if (offers.isEmpty()) {
00173         kDebug() << "weatherengine: No plugins to load!";
00174         return KService::List();
00175     }
00176 
00177     foreach(const KService::Ptr &service, offers) {
00178         setData("ions", service->property("X-IonName").toString(), QString("%1|%2").arg(service->property("Name").toString()).arg(service->property("X-IonName").toString()));
00179     }
00180 
00181     return offers;
00182 }
00183 
00187 void WeatherEngine::newIonSource(const QString& source)
00188 {
00189     IonInterface *ion = qobject_cast<IonInterface*>(sender());
00190 
00191     kDebug() << "New Ion Source" << source;
00192     if (!ion) {
00193         return;
00194     }
00195 
00196     ion->connectSource(source, this);
00197 }
00198 
00202 void WeatherEngine::removeIonSource(const QString& source)
00203 {
00204     IonInterface *ion = d->ionForSource(source);
00205     if (ion) {
00206         ion->removeSource(source);
00207         // If plugin has no more sources let's unload the plugin
00208         if (ion->isEmpty()) {
00209             kDebug() << "No more Sources found for this plugin let's unload it!";
00210             unloadIon(d->ionNameForSource(source));
00211         }
00212     }
00213 }
00214 
00218 void WeatherEngine::dataUpdated(const QString& source, Plasma::DataEngine::Data data)
00219 {
00220     kDebug() << "data updated" << source;
00221     setData(source, data);
00222 }
00223 
00224 // Constructor
00225 WeatherEngine::WeatherEngine(QObject *parent, const QVariantList& args)
00226         :  Plasma::DataEngine(parent, args), d(new Private())
00227 {
00228     Q_UNUSED(args)
00229 
00230     // Set any local properties for Ion to use
00231     d->m_localTime = KDateTime::currentDateTime(KDateTime::LocalZone);
00232 
00233     // Get the list of available plugins but don't load them
00234     d->m_ionServices = knownIons();
00235 
00236     // Globally notify all plugins to remove their sources (and unload plugin)
00237     connect(this, SIGNAL(sourceRemoved(QString)), this, SLOT(removeIonSource(QString)));
00238 }
00239 
00240 // Destructor
00241 WeatherEngine::~WeatherEngine()
00242 {
00243     // Cleanup all private data.
00244     delete d;
00245 }
00246 
00250 bool WeatherEngine::sourceRequestEvent(const QString &source)
00251 {
00252     kDebug() << "sourceRequestEvent()" << source;
00253     IonInterface *ion = d->ionForSource(source);
00254 
00255     if (!ion) {
00256         kDebug() << "sourceRequestEvent(): No Ion Found, load it up!";
00257         ion = loadIon(d->ionNameForSource(source));
00258         if (!ion) {
00259             return false;
00260         }
00261     }
00262 
00263     QByteArray str = source.toLocal8Bit();
00264 
00265     ion->connectSource(source, this);
00266     if (!containerForSource(source)) {
00267         // it is an async reply, we need to set up the data anyways
00268         kDebug() << "no item?";
00269         setData(source, Data());
00270     }
00271     return true;
00272 }
00273 
00277 bool WeatherEngine::updateSourceEvent(const QString& source)
00278 {
00279     IonInterface *ion = d->ionForSource(source);
00280 
00281     QByteArray str = source.toLocal8Bit();
00282 
00283     kDebug() << "updateSourceEvent()";
00284     if (!ion) {
00285         return false;
00286     }
00287 
00288     ion->setProperty("timezone", d->m_localTime.isUtc());
00289     ion->setProperty("unit", KGlobal::locale()->measureSystem());
00290 
00291     if (ion->updateSourceEvent(source)) {
00292         return true;
00293     } else {
00294         return false;
00295     }
00296 }
00297 
00298 #include "weatherengine.moc"

Engines

Skip menu "Engines"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal