2 * Copyright (C) 2009 Petri Damstén <damu@iki.fi>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "weatherlocation.h"
21 #include "weathervalidator.h"
22 #include "weatheri18ncatalog.h"
26 class WeatherLocation::Private
29 Private(WeatherLocation *location)
31 locationEngine(nullptr),
32 weatherEngine(nullptr),
37 void validatorFinished(const QMap<QString, QString> &results)
39 WeatherValidator* validator = qobject_cast<WeatherValidator*>(q->sender());
40 foreach (const QString &source, results.values()) {
41 if (source.isEmpty()) {
44 // qDebug() << Q_FUNC_INFO << source;
45 emit q->valid(source);
48 validators.remove(validator);
49 if (validators.isEmpty()) {
55 Plasma::DataEngine *locationEngine;
56 Plasma::DataEngine *weatherEngine;
58 QMap<WeatherValidator*,QString> validators;
61 WeatherLocation::WeatherLocation(QObject *parent)
63 , d(new Private(this))
65 Weatheri18nCatalog::loadCatalog();
68 WeatherLocation::~WeatherLocation()
70 Q_ASSERT(d->validators.size() == 0);
74 void WeatherLocation::setDataEngines(Plasma::DataEngine* location, Plasma::DataEngine* weather)
76 d->locationEngine = location;
77 d->weatherEngine = weather;
80 void WeatherLocation::getDefault(const QString &ion)
83 if (d->locationEngine && d->locationEngine->isValid()) {
84 d->locationEngine->connectSource(QLatin1String("location"), this);
90 void WeatherLocation::dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
92 if (!d->locationEngine) {
96 Q_ASSERT(d->validators.size() == 0);
97 d->locationEngine->disconnectSource(source, this);
98 // sort the data by accuracy, the lower the accuracy number the higher the accuracy is
99 QMap<int, QVariant> accuratedata;
100 foreach (const QString &datakey, data.keys()) {
101 const QVariant datavariant = data[datakey];
102 const int dataaccuracy = datavariant.toHash()["accuracy"].toInt();
103 accuratedata.insert(dataaccuracy, datavariant);
105 // qDebug() << Q_FUNC_INFO << accuratedata;
106 foreach (const int intdatakey, accuratedata.keys()) {
107 const QVariantHash datahash = accuratedata.value(intdatakey).toHash();
108 QString city = datahash[QLatin1String("city")].toString();
109 if (city.contains(QLatin1Char(','))) {
110 city.truncate(city.indexOf(QLatin1Char(',')) - 1);
112 if (!city.isEmpty()) {
113 WeatherValidator* validator = new WeatherValidator(this);
114 validator->setDataEngine(d->weatherEngine);
115 validator->setIon(d->ion);
117 validator, SIGNAL(finished(QMap<QString,QString>)),
118 this, SLOT(validatorFinished(QMap<QString,QString>))
120 d->validators.insert(validator, city);
121 kDebug() << "validating" << city;
125 if (d->validators.isEmpty()) {
128 foreach (WeatherValidator* validator, d->validators.keys()) {
129 validator->validate(d->validators.value(validator), true);
134 #include "moc_weatherlocation.cpp"