OSDN Git Service

kdeplasma-addons: use KMessageWidget instead of KMessageBox for the weather configura...
[kde/kde-extraapps.git] / kdeplasma-addons / libs / plasmaweather / weathervalidator.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 "weathervalidator.h"
21 #include "weatheri18ncatalog.h"
22 #include <KInputDialog>
23 #include <KMessageBox>
24
25 class WeatherValidator::Private
26 {
27 public:
28     Private()
29         : dataengine(nullptr),
30         ion(QLatin1String("wettercom")),
31         silent(false)
32     {
33     }
34
35     Plasma::DataEngine* dataengine;
36     QString ion;
37     QString validating;
38     bool silent;
39 };
40
41 WeatherValidator::WeatherValidator(QObject *parent)
42     : QObject(parent),
43     d(new Private())
44 {
45     Weatheri18nCatalog::loadCatalog();
46 }
47
48 WeatherValidator::~WeatherValidator()
49 {
50     delete d;
51 }
52
53 QString WeatherValidator::ion() const
54 {
55     return d->ion;
56 }
57
58 void WeatherValidator::setIon(const QString &ion)
59 {
60     d->ion = ion;
61 }
62
63 void WeatherValidator::validate(const QString &location, bool silent)
64 {
65     if (d->ion.isEmpty() || !d->dataengine) {
66         return;
67     }
68
69     d->silent = silent;
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);
73     }
74
75     d->validating = validation;
76     d->dataengine->connectSource(validation, this);
77 }
78
79 void WeatherValidator::setDataEngine(Plasma::DataEngine* dataengine)
80 {
81     d->dataengine = dataengine;
82 }
83
84 void WeatherValidator::dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
85 {
86     QMap<QString, QString> locations;
87     d->dataengine->disconnectSource(source, this);
88     QStringList result = data[QLatin1String("validate")].toString().split(QLatin1Char('|'));
89
90     if (result.count() < 2) {
91         QString message = i18n("Cannot find '%1' using %2.", source, d->ion);
92         emit error(message);
93         if (!d->silent) {
94             KMessageBox::error(0, message);
95         }
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" );
99         int i = 3;
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()) {
104                     continue;
105                 }
106
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));
111                     i += 4;
112                 } else {
113                     locations.insert(name, singleWeatherSource.arg(name));
114                     i += 2;
115                 }
116             } else {
117                 ++i;
118             }
119         }
120
121     } else if (result[1] == QLatin1String("timeout")) {
122         QString message = i18n("Connection to %1 weather server timed out.", d->ion);
123         emit error(message);
124         if (!d->silent) {
125             KMessageBox::error(0, message);
126         }
127     } else {
128         QString message = i18n("Cannot find '%1' using %2.", result.count() > 3 ? result[3] : source, d->ion);
129         emit error(message);
130         if (!d->silent) {
131             KMessageBox::error(0, message);
132         }
133     }
134
135     emit finished(locations);
136 }
137
138 #include "moc_weathervalidator.cpp"