Engines
weatherengine.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
00089
00090
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
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
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
00159 WeatherEngine::WeatherEngine(QObject *parent, const QVariantList& args)
00160 : Plasma::DataEngine(parent, args), d(new Private())
00161 {
00162 Q_UNUSED(args)
00163
00164
00165 d->m_localTime = KDateTime::currentDateTime(KDateTime::LocalZone);
00166
00167
00168 connect(this, SIGNAL(sourceRemoved(QString)), this, SLOT(removeIonSource(QString)));
00169 }
00170
00171
00172 WeatherEngine::~WeatherEngine()
00173 {
00174
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
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"