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 #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
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
00113 if (!foundPlugin) {
00114 return NULL;
00115 }
00116
00117
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
00126 ion->init();
00127 ion->ref();
00128
00129 connect(ion, SIGNAL(sourceAdded(QString)), this, SLOT(newIonSource(QString)));
00130
00131
00132
00133
00134
00135
00136 ion->setProperty("timezone", d->m_localTime.isUtc());
00137 ion->setProperty("unit", KGlobal::locale()->measureSystem());
00138
00139
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
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
00225 WeatherEngine::WeatherEngine(QObject *parent, const QVariantList& args)
00226 : Plasma::DataEngine(parent, args), d(new Private())
00227 {
00228 Q_UNUSED(args)
00229
00230
00231 d->m_localTime = KDateTime::currentDateTime(KDateTime::LocalZone);
00232
00233
00234 d->m_ionServices = knownIons();
00235
00236
00237 connect(this, SIGNAL(sourceRemoved(QString)), this, SLOT(removeIonSource(QString)));
00238 }
00239
00240
00241 WeatherEngine::~WeatherEngine()
00242 {
00243
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
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"