weathervalidator.cpp 4.07 KB
Newer Older
Petri Damstén's avatar
Petri Damstén committed
1
/*
Petri Damstén's avatar
Petri Damstén committed
2
 *   Copyright (C) 2009 Petri Damstén <damu@iki.fi>
Petri Damstén's avatar
Petri Damstén committed
3
 *
Petri Damstén's avatar
Petri Damstén committed
4 5 6 7
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as
 *   published by the Free Software Foundation; either version 2, or
 *   (at your option) any later version.
Petri Damstén's avatar
Petri Damstén committed
8
 *
Petri Damstén's avatar
Petri Damstén committed
9 10 11 12
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details
Petri Damstén's avatar
Petri Damstén committed
13
 *
Petri Damstén's avatar
Petri Damstén committed
14 15 16 17
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Petri Damstén's avatar
Petri Damstén committed
18 19 20
 */

#include "weathervalidator.h"
21

22
#include <KLocalizedString>
Petri Damstén's avatar
Petri Damstén committed
23 24
#include <KMessageBox>

25 26
namespace Plasma {

27
class WeatherValidatorPrivate
Petri Damstén's avatar
Petri Damstén committed
28 29
{
public:
30
    WeatherValidatorPrivate()
31
        : dataengine(nullptr)
Petri Damstén's avatar
Petri Damstén committed
32
    {}
33

Petri Damstén's avatar
Petri Damstén committed
34
    Plasma::DataEngine* dataengine;
35 36
    QString ion;
    QString validating;
Petri Damstén's avatar
Petri Damstén committed
37 38 39
    bool silent;
};

40
WeatherValidator::WeatherValidator(QObject *parent)
Petri Damstén's avatar
Petri Damstén committed
41
    : QObject(parent)
42
    , d(new WeatherValidatorPrivate())
Petri Damstén's avatar
Petri Damstén committed
43 44 45
{
}

46
WeatherValidator::~WeatherValidator() = default;
Petri Damstén's avatar
Petri Damstén committed
47

48 49 50 51 52 53 54 55 56 57 58
QString WeatherValidator::ion() const
{
    return d->ion;
}

void WeatherValidator::setIon(const QString &ion)
{
    d->ion = ion;
}

void WeatherValidator::validate(const QString& location, bool silent)
Petri Damstén's avatar
Petri Damstén committed
59
{
60 61 62 63
    if (d->ion.isEmpty() || !d->dataengine) {
        return;
    }

Petri Damstén's avatar
Petri Damstén committed
64
    d->silent = silent;
65
    const QString validation = d->ion + QStringLiteral("|validate|") + location;
66 67 68 69 70
    if (d->validating != validation) {
        d->dataengine->disconnectSource(d->validating, this);
    }

    d->validating = validation;
Petri Damstén's avatar
Petri Damstén committed
71 72 73
    d->dataengine->connectSource(validation, this);
}

74 75 76 77 78 79
void WeatherValidator::validate(const QString& ion, const QString& location, bool silent)
{
    d->ion = ion;
    validate(location, silent);
}

Petri Damstén's avatar
Petri Damstén committed
80 81 82 83 84 85 86
void WeatherValidator::setDataEngine(Plasma::DataEngine* dataengine)
{
    d->dataengine = dataengine;
}

void WeatherValidator::dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
{
87
    QMap<QString, QString> locations;
88

Petri Damstén's avatar
Petri Damstén committed
89
    d->dataengine->disconnectSource(source, this);
90 91

    const QStringList result = data[QStringLiteral("validate")].toString().split(QLatin1Char('|'));
Petri Damstén's avatar
Petri Damstén committed
92

93
    if (result.count() < 2) {
94 95
        QString message = i18n("Cannot find '%1' using %2.", source, d->ion);
        emit error(message);
96
        if (!d->silent) {
97
            KMessageBox::error(nullptr, message);
98
        }
99
    } else if (result[1] == QLatin1String( "valid" ) && result.count() > 2) {
100
        const QString weatherSource = result[0] + QLatin1String( "|weather|");
Petri Damstén's avatar
Petri Damstén committed
101
        int i = 3;
102
        //kDebug() << d->ion << result.count() << result;
103
        while (i < result.count() - 1) {
104
            if (result[i] == QLatin1String( "place" )) {
105
                // not enough fields left for place data?
106
                if (i + 1 > result.count()) {
107
                    break;
108 109
                }

110
                const QString& name = result[i + 1];
111
                if (i + 2 < result.count() && result[i + 2] == QLatin1String( "extra" )) {
112 113
                    const QString& id = result[i + 3];
                    locations.insert(name, weatherSource + name + QLatin1Char('|') + id);
Petri Damstén's avatar
Petri Damstén committed
114 115
                    i += 4;
                } else {
116
                    locations.insert(name, weatherSource + name);
Petri Damstén's avatar
Petri Damstén committed
117 118
                    i += 2;
                }
119 120
            } else {
                ++i;
Petri Damstén's avatar
Petri Damstén committed
121 122
            }
        }
123

124
    } else if (result[1] == QLatin1String( "timeout" )) {
125 126 127
        QString message = i18n("Connection to %1 weather server timed out.", d->ion);
        emit error(message);
        if (!d->silent) {
128
            KMessageBox::error(nullptr, message);
Petri Damstén's avatar
Petri Damstén committed
129
        }
130 131 132 133
    } else {
        QString message = i18n("Cannot find '%1' using %2.", result.count() > 3 ? result[3] : source, d->ion);
        emit error(message);
        if (!d->silent) {
134
            KMessageBox::error(nullptr, message);
Petri Damstén's avatar
Petri Damstén committed
135 136
        }
    }
137 138

    emit finished(locations);
Petri Damstén's avatar
Petri Damstén committed
139 140
}

141
}