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 "weathervalidator.h"
21 #include "weatheri18ncatalog.h"
22 #include <KInputDialog>
23 #include <KMessageBox>
25 class WeatherValidator::Private
29 : dataengine(nullptr),
30 ion(QLatin1String("wettercom")),
35 Plasma::DataEngine* dataengine;
41 WeatherValidator::WeatherValidator(QObject *parent)
45 Weatheri18nCatalog::loadCatalog();
48 WeatherValidator::~WeatherValidator()
53 QString WeatherValidator::ion() const
58 void WeatherValidator::setIon(const QString &ion)
63 void WeatherValidator::validate(const QString &location, bool silent)
65 if (d->ion.isEmpty() || !d->dataengine) {
70 QString validation = QString(QLatin1String("%1|validate|%2")).arg(d->ion).arg(location);
71 if (d->validating != validation) {
72 d->dataengine->disconnectSource(d->validating, this);
75 d->validating = validation;
76 d->dataengine->connectSource(validation, this);
79 void WeatherValidator::setDataEngine(Plasma::DataEngine* dataengine)
81 d->dataengine = dataengine;
84 void WeatherValidator::dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
86 QMap<QString, QString> locations;
87 d->dataengine->disconnectSource(source, this);
88 QStringList result = data[QLatin1String("validate")].toString().split(QLatin1Char('|'));
90 if (result.count() < 2) {
91 QString message = i18n("Cannot find '%1' using %2.", source, d->ion);
94 KMessageBox::error(0, message);
96 } else if (result[1] == QLatin1String("valid") && result.count() > 2) {
97 QString weatherSource = result[0] + QLatin1String( "|weather|%1|%2" );
98 QString singleWeatherSource = result[0] + QLatin1String( "|weather|%1" );
100 //kDebug() << d->ion << result.count() << result;
101 while (i < result.count() - 1) {
102 if (result[i] == QLatin1String("place")) {
103 if (i + 1 > result.count()) {
107 QString name = result[i + 1];
108 if (i + 2 < result.count() && result[i + 2] == QLatin1String("extra")) {
109 QString id = result[i + 3];
110 locations.insert(name, weatherSource.arg(name, id));
113 locations.insert(name, singleWeatherSource.arg(name));
121 } else if (result[1] == QLatin1String("timeout")) {
122 QString message = i18n("Connection to %1 weather server timed out.", d->ion);
125 KMessageBox::error(0, message);
128 QString message = i18n("Cannot find '%1' using %2.", result.count() > 3 ? result[3] : source, d->ion);
131 KMessageBox::error(0, message);
135 emit finished(locations);
138 #include "moc_weathervalidator.cpp"