File indexing completed on 2024-05-12 05:37:24

0001 /*
0002     SPDX-FileCopyrightText: 2007-2009 Shawn Starr <shawn.starr@rogers.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QHash>
0010 #include <QNetworkInformation>
0011 #include <QTimer>
0012 
0013 #include <Plasma5Support/DataEngine>
0014 #include <Plasma5Support/DataEngineConsumer>
0015 
0016 #include "ions/ion.h"
0017 
0018 /**
0019  * @author Shawn Starr
0020  * This class is DataEngine. It handles loading, unloading, updating any data the ions wish to send. It is a gateway for datasources (ions) to
0021  * communicate with the WeatherEngine.
0022  *
0023  * To search for a city:
0024  * ion|validate|name such as noaa|validate|washington it will return a | separated list of valid places
0025  *
0026  * To fetch the weather:
0027  * ion|weather|place name  where the place name is a name returned by the former validate
0028  * noaa|weather|Claxton Evans County Airport, GA
0029  *
0030  * Some ions may have a longer syntax, for instance wetter.com requires two extra params
0031  * for instance:
0032  *
0033  * wettercom|validate|turin may return a list with items on the form
0034  *    Turin, Piemont, IT|extra|IT0PI0397;Turin
0035  *
0036  * Thus the query for weather will be on the form:
0037  *
0038  * wettercom|weather|Turin, Piemont, IT|IT0PI0397;Turin
0039  *
0040  * with the extra strings appended after extra
0041  */
0042 
0043 class WeatherEngine : public Plasma5Support::DataEngine, public Plasma5Support::DataEngineConsumer
0044 {
0045     Q_OBJECT
0046 
0047 public:
0048     /** Constructor
0049      * @param parent The parent object.
0050      * @param args Argument list, unused.
0051      */
0052     WeatherEngine(QObject *parent);
0053 
0054     ~WeatherEngine() override;
0055 
0056 protected: // Plasma5Support::DataEngine API
0057     /**
0058      * We use it to communicate to the Ion plugins to set the data sources.
0059      * @param source The datasource name.
0060      */
0061     bool sourceRequestEvent(const QString &source) override;
0062 
0063     /**
0064      * @param source The datasource to update.
0065      */
0066     bool updateSourceEvent(const QString &source) override;
0067 
0068 protected Q_SLOTS: // expected DataEngine class method
0069     /**
0070      * Slot method with this signature expected in a DataEngine class.
0071      * @param source The datasource to be updated.
0072      * @param data The new data updated.
0073      */
0074     void dataUpdated(const QString &source, const Plasma5Support::DataEngine::Data &data);
0075 
0076 private Q_SLOTS:
0077     void forceUpdate(IonInterface *ion, const QString &source);
0078 
0079     /**
0080      * Notify WeatherEngine a datasource is being removed.
0081      * @arg source datasource name.
0082      */
0083     void removeIonSource(const QString &source);
0084 
0085     /**
0086      * Whenever networking changes, take action
0087      */
0088     void onOnlineStateChanged(QNetworkInformation::Reachability reachability);
0089     void startReconnect();
0090 
0091     /**
0092      * updates the list of ions whenever KSycoca changes (as well as on init
0093      */
0094     void updateIonList();
0095 
0096 private:
0097     /**
0098      * Get instance of a loaded ion.
0099      * @returns a IonInterface instance of a loaded plugin.
0100      */
0101     IonInterface *ionForSource(const QString &source, QString *ionName = nullptr);
0102 
0103 private:
0104     QHash<QString, int> m_ionUsage;
0105     QTimer m_reconnectTimer;
0106 };