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

Plasma

weatherengine.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Shawn Starr <shawn.starr@rogers.com>            *
00003  *   Copyright (C) 2009 by Aaron Seigo <aseigo@kde.org>                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
00019  ***************************************************************************/
00020 
00021 #include "weatherengine.h"
00022 #include <KServiceTypeTrader>
00023 #include <KDateTime>
00024 #include <KLocale>
00025 
00026 #include <Plasma/DataEngineManager>
00027 
00028 #include "ions/ion.h"
00029 
00030 class WeatherEngine::Private
00031 {
00032 public:
00037     IonInterface* ionForSource(const QString& name) {
00038         int offset = name.indexOf('|');
00039 
00040         if (offset < 1) {
00041             return NULL;
00042         }
00043 
00044         QString ionName = name.left(offset);
00045         return qobject_cast<IonInterface *>(Plasma::DataEngineManager::self()->engine(ionName));
00046     }
00047 
00052     QString ionNameForSource(const QString& source) {
00053         int offset = source.indexOf('|');
00054         if (offset < 1) {
00055             return QString();
00056         }
00057 
00058         return QString(source.left(offset));
00059     }
00060 
00061     KDateTime m_localTime;
00062     QStringList m_ions;
00063 };
00064 
00068 Plasma::DataEngine *WeatherEngine::loadIon(const QString& plugName)
00069 {
00070     KPluginInfo foundPlugin;
00071 
00072     foreach (const KPluginInfo &info, Plasma::DataEngineManager::listEngineInfo("weatherengine")) {
00073         if (info.pluginName() == plugName) {
00074             foundPlugin = info;
00075             break;
00076         }
00077     }
00078 
00079     if (!foundPlugin.isValid()) {
00080         return NULL;
00081     }
00082 
00083     // Load the Ion plugin, store it into a QMap to handle multiple ions.
00084     Plasma::DataEngine *ion = Plasma::DataEngineManager::self()->loadEngine(foundPlugin.pluginName());
00085     ion->setObjectName(plugName);
00086     connect(ion, SIGNAL(sourceAdded(QString)), this, SLOT(newIonSource(QString)));
00087 
00088     /* Set system related properties for the ion
00089      * timezone is displaying the time/date in UTC or user's local time
00090      * unit is setting the weather units used, Celsius/Fahrenheit, Kilopascals/Inches of Mercury, etc
00091      */
00092 
00093     ion->setProperty("timezone", d->m_localTime.isUtc());
00094     ion->setProperty("unit", KGlobal::locale()->measureSystem());
00095     d->m_ions << plugName;
00096 
00097     return ion;
00098 }
00099 
00103 void WeatherEngine::unloadIon(const QString &name)
00104 {
00105     Plasma::DataEngineManager::self()->unloadEngine(name);
00106     d->m_ions.removeOne(name);
00107 }
00108 
00109 void WeatherEngine::init()
00110 {
00111     // Get the list of available plugins but don't load them
00112     foreach(const KPluginInfo &info, Plasma::DataEngineManager::listEngineInfo("weatherengine")) {
00113         setData("ions", info.pluginName(),
00114                 QString("%1|%2").arg(info.property("Name").toString()).arg(info.pluginName()));
00115     }
00116 }
00117 
00121 void WeatherEngine::newIonSource(const QString& source)
00122 {
00123     IonInterface *ion = qobject_cast<IonInterface*>(sender());
00124 
00125     kDebug() << "New Ion Source" << source;
00126     if (!ion) {
00127         return;
00128     }
00129 
00130     ion->connectSource(source, this);
00131 }
00132 
00136 void WeatherEngine::removeIonSource(const QString& source)
00137 {
00138     IonInterface *ion = d->ionForSource(source);
00139     if (ion) {
00140         ion->removeSource(source);
00141         // If plugin has no more sources let's unload the plugin
00142         if (ion->isEmpty()) {
00143             kDebug() << "No more Sources found for this plugin let's unload it!";
00144             unloadIon(d->ionNameForSource(source));
00145         }
00146     }
00147 }
00148 
00152 void WeatherEngine::dataUpdated(const QString& source, Plasma::DataEngine::Data data)
00153 {
00154     kDebug() << "data updated" << source;
00155     setData(source, data);
00156 }
00157 
00158 // Constructor
00159 WeatherEngine::WeatherEngine(QObject *parent, const QVariantList& args)
00160         :  Plasma::DataEngine(parent, args), d(new Private())
00161 {
00162     Q_UNUSED(args)
00163 
00164     // Set any local properties for Ion to use
00165     d->m_localTime = KDateTime::currentDateTime(KDateTime::LocalZone);
00166 
00167     // Globally notify all plugins to remove their sources (and unload plugin)
00168     connect(this, SIGNAL(sourceRemoved(QString)), this, SLOT(removeIonSource(QString)));
00169 }
00170 
00171 // Destructor
00172 WeatherEngine::~WeatherEngine()
00173 {
00174     // Cleanup all private data.
00175     foreach (const QString &ion, d->m_ions) {
00176         Plasma::DataEngineManager::self()->unloadEngine(ion);
00177     }
00178 
00179     delete d;
00180 }
00181 
00185 bool WeatherEngine::sourceRequestEvent(const QString &source)
00186 {
00187     kDebug() << "sourceRequestEvent()" << source;
00188     Plasma::DataEngine *ion = d->ionForSource(source);
00189 
00190     if (!ion) {
00191         kDebug() << "sourceRequestEvent(): No Ion Found, load it up!";
00192         ion = loadIon(d->ionNameForSource(source));
00193         if (!ion) {
00194             return false;
00195         }
00196     }
00197 
00198     QByteArray str = source.toLocal8Bit();
00199 
00200     ion->connectSource(source, this);
00201     if (!containerForSource(source)) {
00202         // it is an async reply, we need to set up the data anyways
00203         kDebug() << "no item?";
00204         setData(source, Data());
00205     }
00206     return true;
00207 }
00208 
00212 bool WeatherEngine::updateSourceEvent(const QString& source)
00213 {
00214     IonInterface *ion = d->ionForSource(source);
00215 
00216     QByteArray str = source.toLocal8Bit();
00217 
00218     kDebug() << "updateSourceEvent()";
00219     if (!ion) {
00220         return false;
00221     }
00222 
00223     ion->setProperty("timezone", d->m_localTime.isUtc());
00224     ion->setProperty("unit", KGlobal::locale()->measureSystem());
00225 
00226     if (ion->updateSourceEvent(source)) {
00227         return true;
00228     } else {
00229         return false;
00230     }
00231 }
00232 
00233 #include "weatherengine.moc"

Plasma

Skip menu "Plasma"
  • 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
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
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