#include <KPixmapSequence>
#include <kpixmapsequencewidget.h>
#include <kunitconversion.h>
-#include <Plasma/DataEngineManager>
#include "weathervalidator.h"
#include "weatheri18ncatalog.h"
: q(location),
locationEngine(nullptr),
weatherEngine(nullptr),
- foundSource(false),
ion("wettercom")
{
}
void validatorFinished(const QMap<QString, QString> &results)
{
WeatherValidator* validator = qobject_cast<WeatherValidator*>(q->sender());
- QString source;
- if (!results.isEmpty()) {
- source = results.begin().value();
+ foreach (const QString &source, results.values()) {
+ if (source.isEmpty()) {
+ continue;
+ }
+ // qDebug() << Q_FUNC_INFO << source;
+ emit q->valid(source);
}
validators.remove(validator);
- if (!source.isEmpty() && !foundSource) {
- foundSource = true;
- emit q->finished(source);
- }
- if (!foundSource && validators.isEmpty()) {
- emit q->finished(QString());
+ if (validators.isEmpty()) {
+ emit q->finished();
}
}
WeatherLocation *q;
Plasma::DataEngine *locationEngine;
Plasma::DataEngine *weatherEngine;
- bool foundSource;
QString ion;
QMap<WeatherValidator*,QString> validators;
};
if (d->locationEngine && d->locationEngine->isValid()) {
d->locationEngine->connectSource(QLatin1String("location"), this);
} else {
- emit finished(QString());
+ emit finished();
}
}
}
Q_ASSERT(d->validators.size() == 0);
- d->foundSource = false;
d->locationEngine->disconnectSource(source, this);
// sort the data by accuracy, the lower the accuracy number the higher the accuracy is
QMap<int, QVariant> accuratedata;
}
if (d->validators.isEmpty()) {
- emit finished(QString());
+ emit finished();
} else {
foreach (WeatherValidator* validator, d->validators.keys()) {
validator->validate(d->validators.value(validator), true);
Q_SIGNALS:
/**
+ * Emitted when valid source is found
+ **/
+ void valid(const QString &source);
+
+ /**
* Emitted when validation is done
**/
- void finished(const QString &source);
+ void finished();
private Q_SLOTS:
void dataUpdated(const QString &source, const Plasma::DataEngine::Data &data);
, weatherEngine(0)
, timeEngine(0)
, updateInterval(0)
- , location(0)
+ , location(nullptr)
{
busyTimer = new QTimer(q);
busyTimer->setInterval(2*60*1000);
void locationReady(const QString &src)
{
- if (!src.isEmpty()) {
+ // set the location to the first valid one (the most accurate one)
+ if (source.isEmpty()) {
source = src;
KConfigGroup cfg = q->config();
cfg.writeEntry("source", source);
emit q->configNeedsSaving();
q->connectToEngine();
q->setConfigurationRequired(false);
- } else {
- busyTimer->stop();
- q->setBusy(false);
+ }
+ }
+
+ void locationFinished()
+ {
+ busyTimer->stop();
+ q->setBusy(false);
+ if (source.isEmpty()) {
q->showMessage(QIcon(), QString(), Plasma::ButtonNone);
q->setConfigurationRequired(true);
}
delete location;
- location = 0;
+ location = nullptr;
}
void giveUpBeingBusy()
if (missingLocation) {
if (!d->location) {
d->location = new WeatherLocation(this);
- connect(d->location, SIGNAL(finished(QString)), this, SLOT(locationReady(QString)));
+ connect(d->location, SIGNAL(valid(QString)), this, SLOT(locationReady(QString)));
+ connect(d->location, SIGNAL(finished()), this, SLOT(locationFinished()));
}
d->busyTimer->stop();
d->location->setDataEngines(dataEngine(QLatin1String("geolocation")), d->weatherEngine);
d->location->getDefault(d->defaultIon);
} else {
- delete d->location;
- d->location = 0;
d->busyTimer->start();
setBusy(true);
d->weatherEngine->connectSource(d->source, this, d->updateInterval * 60 * 1000);
Private * const d;
Q_PRIVATE_SLOT(d, void locationReady(const QString &source))
+ Q_PRIVATE_SLOT(d, void locationFinished())
Q_PRIVATE_SLOT(d, void giveUpBeingBusy())
};
public:
Private()
: dataengine(nullptr),
- ion(QLatin1String( "wettercom" ))
- {}
+ ion(QLatin1String("wettercom")),
+ silent(false)
+ {
+ }
Plasma::DataEngine* dataengine;
QString ion;
d->ion = ion;
}
-void WeatherValidator::validate(const QString& location, bool silent)
+void WeatherValidator::validate(const QString &location, bool silent)
{
if (d->ion.isEmpty() || !d->dataengine) {
return;
}
d->silent = silent;
- QString validation = QString(QLatin1String( "%1|validate|%2" )).arg(d->ion).arg(location);
+ QString validation = QString(QLatin1String("%1|validate|%2")).arg(d->ion).arg(location);
if (d->validating != validation) {
d->dataengine->disconnectSource(d->validating, this);
}
{
QMap<QString, QString> locations;
d->dataengine->disconnectSource(source, this);
- QStringList result = data[QLatin1String( "validate" )].toString().split(QLatin1Char( '|' ));
+ QStringList result = data[QLatin1String("validate")].toString().split(QLatin1Char('|'));
if (result.count() < 2) {
QString message = i18n("Cannot find '%1' using %2.", source, d->ion);
if (!d->silent) {
KMessageBox::error(0, message);
}
- } else if (result[1] == QLatin1String( "valid" ) && result.count() > 2) {
+ } else if (result[1] == QLatin1String("valid") && result.count() > 2) {
QString weatherSource = result[0] + QLatin1String( "|weather|%1|%2" );
QString singleWeatherSource = result[0] + QLatin1String( "|weather|%1" );
int i = 3;
//kDebug() << d->ion << result.count() << result;
while (i < result.count() - 1) {
- if (result[i] == QLatin1String( "place" )) {
+ if (result[i] == QLatin1String("place")) {
if (i + 1 > result.count()) {
continue;
}
QString name = result[i + 1];
- if (i + 2 < result.count() && result[i + 2] == QLatin1String( "extra" )) {
+ if (i + 2 < result.count() && result[i + 2] == QLatin1String("extra")) {
QString id = result[i + 3];
locations.insert(name, weatherSource.arg(name, id));
i += 4;
}
}
- } else if (result[1] == QLatin1String( "timeout" )) {
+ } else if (result[1] == QLatin1String("timeout")) {
QString message = i18n("Connection to %1 weather server timed out.", d->ion);
emit error(message);
if (!d->silent) {
{
Q_OBJECT
public:
- WeatherValidator(QObject *parent = 0);
+ WeatherValidator(QObject *parent = nullptr);
virtual ~WeatherValidator();
/**
* @param location the name of the location to find
* @param silent if true don't show any dialogs
*/
- void validate(const QString& location, bool silent = false);
+ void validate(const QString &location, bool silent = false);
/**
* Sets the ion to use
*
* @param dataengine use this dataengine
**/
- void setDataEngine(Plasma::DataEngine* dataengine);
+ void setDataEngine(Plasma::DataEngine *dataengine);
Q_SIGNALS:
/**
* Emitted when an error in validation occurs
**/
- void error(const QString& message);
+ void error(const QString &message);
/**
* Emitted when validation is done