OSDN Git Service

kdeplasma-addons: emit all valid sources from WeatherLocation
authorIvailo Monev <xakepa10@gmail.com>
Sat, 15 Jul 2023 23:26:21 +0000 (02:26 +0300)
committerIvailo Monev <xakepa10@gmail.com>
Sat, 15 Jul 2023 23:26:21 +0000 (02:26 +0300)
so that all default and validated sources can (potentially, eventually) be
shown in the configuration location widget

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
kdeplasma-addons/libs/plasmaweather/weatherconfig.cpp
kdeplasma-addons/libs/plasmaweather/weatherlocation.cpp
kdeplasma-addons/libs/plasmaweather/weatherlocation.h
kdeplasma-addons/libs/plasmaweather/weatherpopupapplet.cpp
kdeplasma-addons/libs/plasmaweather/weatherpopupapplet.h
kdeplasma-addons/libs/plasmaweather/weathervalidator.cpp
kdeplasma-addons/libs/plasmaweather/weathervalidator.h

index 065eca8..2a4f3e7 100644 (file)
@@ -27,7 +27,6 @@
 #include <KPixmapSequence>
 #include <kpixmapsequencewidget.h>
 #include <kunitconversion.h>
-#include <Plasma/DataEngineManager>
 
 #include "weathervalidator.h"
 #include "weatheri18ncatalog.h"
index 7fb4bf3..98d77a0 100644 (file)
@@ -30,7 +30,6 @@ public:
         : q(location),
           locationEngine(nullptr),
           weatherEngine(nullptr),
-          foundSource(false),
           ion("wettercom")
     {
     }
@@ -38,25 +37,23 @@ public:
     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;
 };
@@ -86,7 +83,7 @@ void WeatherLocation::getDefault(const QString &ion)
     if (d->locationEngine && d->locationEngine->isValid()) {
         d->locationEngine->connectSource(QLatin1String("location"), this);
     } else {
-        emit finished(QString());
+        emit finished();
     }
 }
 
@@ -97,7 +94,6 @@ void WeatherLocation::dataUpdated(const QString &source, const Plasma::DataEngin
     }
 
     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;
@@ -127,7 +123,7 @@ void WeatherLocation::dataUpdated(const QString &source, const Plasma::DataEngin
     }
 
     if (d->validators.isEmpty()) {
-        emit finished(QString());
+        emit finished();
     } else {
         foreach (WeatherValidator* validator, d->validators.keys()) {
             validator->validate(d->validators.value(validator), true);
index 590f9af..efafbe3 100644 (file)
@@ -51,9 +51,14 @@ public:
 
 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);
index 205cc77..995b4bc 100644 (file)
@@ -39,7 +39,7 @@ public:
         , weatherEngine(0)
         , timeEngine(0)
         , updateInterval(0)
-        , location(0)
+        , location(nullptr)
     {
         busyTimer = new QTimer(q);
         busyTimer->setInterval(2*60*1000);
@@ -70,21 +70,27 @@ public:
 
     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()
@@ -202,7 +208,8 @@ void WeatherPopupApplet::connectToEngine()
     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();
@@ -210,8 +217,6 @@ void WeatherPopupApplet::connectToEngine()
         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);
index ab4e5c7..d4fe97c 100644 (file)
@@ -119,6 +119,7 @@ class PLASMAWEATHER_EXPORT WeatherPopupApplet : public Plasma::PopupApplet
         Private * const d;
 
         Q_PRIVATE_SLOT(d, void locationReady(const QString &source))
+        Q_PRIVATE_SLOT(d, void locationFinished())
         Q_PRIVATE_SLOT(d, void giveUpBeingBusy())
 };
 
index d1138db..bdb2ead 100644 (file)
@@ -27,8 +27,10 @@ class WeatherValidator::Private
 public:
     Private()
         : dataengine(nullptr),
-          ion(QLatin1String( "wettercom" ))
-    {}
+        ion(QLatin1String("wettercom")),
+        silent(false)
+    {
+    }
 
     Plasma::DataEngine* dataengine;
     QString ion;
@@ -58,14 +60,14 @@ void WeatherValidator::setIon(const QString &ion)
     d->ion = ion;
 }
 
-void WeatherValidator::validate(const QStringlocation, 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);
     }
@@ -83,7 +85,7 @@ void WeatherValidator::dataUpdated(const QString &source, const Plasma::DataEngi
 {
     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);
@@ -91,19 +93,19 @@ void WeatherValidator::dataUpdated(const QString &source, const Plasma::DataEngi
         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;
@@ -116,7 +118,7 @@ void WeatherValidator::dataUpdated(const QString &source, const Plasma::DataEngi
             }
         }
 
-    } 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) {
index 7283371..b85e2f2 100644 (file)
@@ -33,7 +33,7 @@ class PLASMAWEATHER_EXPORT WeatherValidator : public QObject
 {
     Q_OBJECT
 public:
-    WeatherValidator(QObject *parent = 0);
+    WeatherValidator(QObject *parent = nullptr);
     virtual ~WeatherValidator();
 
     /**
@@ -42,7 +42,7 @@ public:
      * @param location the name of the location to find
      * @param silent if true don't show any dialogs
      */
-    void validate(const QStringlocation, bool silent = false);
+    void validate(const QString &location, bool silent = false);
 
     /**
      * Sets the ion to use
@@ -60,13 +60,13 @@ public:
      *
      * @param dataengine use this dataengine
      **/
-    void setDataEngine(Plasma::DataEnginedataengine);
+    void setDataEngine(Plasma::DataEngine *dataengine);
 
 Q_SIGNALS:
     /**
      * Emitted when an error in validation occurs
      **/
-    void error(const QStringmessage);
+    void error(const QString &message);
 
     /**
      * Emitted when validation is done