OSDN Git Service

kdeplasma-addons: use KMessageWidget instead of KMessageBox for the weather configura...
[kde/kde-extraapps.git] / kdeplasma-addons / libs / plasmaweather / weatherlocation.cpp
1 /*
2  *   Copyright (C) 2009 Petri Damstén <damu@iki.fi>
3  *
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.
8  *
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
13  *
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.
18  */
19
20 #include "weatherlocation.h"
21 #include "weathervalidator.h"
22 #include "weatheri18ncatalog.h"
23
24 #include <kdebug.h>
25
26 class WeatherLocation::Private
27 {
28 public:
29     Private(WeatherLocation *location)
30         : q(location),
31         locationEngine(nullptr),
32         weatherEngine(nullptr),
33         ion("wettercom")
34     {
35     }
36
37     void validatorFinished(const QMap<QString, QString> &results)
38     {
39         WeatherValidator* validator = qobject_cast<WeatherValidator*>(q->sender());
40         foreach (const QString &source, results.values()) {
41             if (source.isEmpty()) {
42                 continue;
43             }
44             // qDebug() << Q_FUNC_INFO << source;
45             emit q->valid(source);
46         }
47
48         validators.remove(validator);
49         if (validators.isEmpty()) {
50             emit q->finished();
51         }
52     }
53
54     WeatherLocation *q;
55     Plasma::DataEngine *locationEngine;
56     Plasma::DataEngine *weatherEngine;
57     QString ion;
58     QMap<WeatherValidator*,QString> validators;
59 };
60
61 WeatherLocation::WeatherLocation(QObject *parent)
62     : QObject(parent)
63     , d(new Private(this))
64 {
65     Weatheri18nCatalog::loadCatalog();
66 }
67
68 WeatherLocation::~WeatherLocation()
69 {
70     Q_ASSERT(d->validators.size() == 0);
71     delete d;
72 }
73
74 void WeatherLocation::setDataEngines(Plasma::DataEngine* location, Plasma::DataEngine* weather)
75 {
76     d->locationEngine = location;
77     d->weatherEngine = weather;
78 }
79
80 void WeatherLocation::getDefault(const QString &ion)
81 {
82     d->ion = ion;
83     if (d->locationEngine && d->locationEngine->isValid()) {
84         d->locationEngine->connectSource(QLatin1String("location"), this);
85     } else {
86         emit finished();
87     }
88 }
89
90 void WeatherLocation::dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
91 {
92     if (!d->locationEngine) {
93         return;
94     }
95
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);
104     }
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);
111         }
112         if (!city.isEmpty()) {
113             WeatherValidator* validator = new WeatherValidator(this);
114             validator->setDataEngine(d->weatherEngine);
115             validator->setIon(d->ion);
116             connect(
117                 validator, SIGNAL(finished(QMap<QString,QString>)),
118                 this, SLOT(validatorFinished(QMap<QString,QString>))
119             );
120             d->validators.insert(validator, city);
121             kDebug() <<  "validating" << city;
122         }
123     }
124
125     if (d->validators.isEmpty()) {
126         emit finished();
127     } else {
128         foreach (WeatherValidator* validator, d->validators.keys()) {
129             validator->validate(d->validators.value(validator), true);
130         }
131     }
132 }
133
134 #include "moc_weatherlocation.cpp"