File indexing completed on 2024-03-24 15:17:23

0001 /*
0002     SPDX-FileCopyrightText: 2016 Artem Fedoskin <afedoskin3@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #pragma once
0007 
0008 #include "dms.h"
0009 
0010 #include <QHash>
0011 #include <QSqlDatabase>
0012 #include <QStringListModel>
0013 
0014 class QGeoPositionInfoSource;
0015 class QNetworkAccessManager;
0016 class QNetworkSession;
0017 class QNetworkReply;
0018 
0019 class GeoLocation;
0020 
0021 /**
0022  * @class LocationDialogLite
0023  * A backend of location dialog declared in QML.
0024  *
0025  * @author Artem Fedoskin, Jason Harris
0026  * @version 1.0
0027  */
0028 class LocationDialogLite : public QObject
0029 {
0030     Q_OBJECT
0031 
0032     Q_PROPERTY(QString currentLocation READ getCurrentLocation WRITE setCurrentLocation NOTIFY currentLocationChanged)
0033     Q_PROPERTY(QStringList TZList MEMBER m_TZList NOTIFY TZListChanged)
0034     Q_PROPERTY(QStringList DSTRules MEMBER m_DSTRules NOTIFY DSTRulesChanged)
0035     Q_PROPERTY(int currLocIndex MEMBER m_currLocIndex NOTIFY currLocIndexChanged)
0036   public:
0037     typedef enum { CITY_ADD, CITY_UPDATE, CITY_REMOVE } CityOperation;
0038 
0039     LocationDialogLite();
0040 
0041     Q_INVOKABLE void filterCity(const QString &city, const QString &province, const QString &country);
0042 
0043     void setCurrentLocation(const QString &loc);
0044     QString getCurrentLocation() { return m_currentLocation; }
0045     Q_INVOKABLE bool editCity(const QString &fullName, const QString &city, const QString &province,
0046                               const QString &country, const QString &latitude,
0047                               const QString &longitude, const QString &TimeZoneString, const QString &TZRule);
0048 
0049     Q_INVOKABLE bool setLocation(const QString &fullName);
0050 
0051     Q_INVOKABLE bool addCity(const QString &city, const QString &province, const QString &country,
0052                              const QString &latitude, const QString &longitude,
0053                              const QString &TimeZoneString, const QString &TZRule);
0054     Q_INVOKABLE bool deleteCity(const QString &fullName);
0055 
0056     Q_INVOKABLE bool isReadOnly(const QString &fullName);
0057 
0058     Q_INVOKABLE QString getCity(const QString &fullName);
0059     Q_INVOKABLE QString getProvince(const QString &fullName);
0060     Q_INVOKABLE QString getCountry(const QString &fullName);
0061     Q_INVOKABLE double getLatitude(const QString &fullName);
0062     Q_INVOKABLE double getLongitude(const QString &fullName);
0063     Q_INVOKABLE int getTZ(const QString &fullName);
0064     Q_INVOKABLE int getDST(const QString &fullName);
0065     Q_INVOKABLE bool isDuplicate(const QString &city, const QString &province, const QString &country);
0066 
0067     /**
0068      * @brief checkLongLat checks whether given longitude and latitude are valid
0069      */
0070     Q_INVOKABLE bool checkLongLat(const QString &longitude, const QString &latitude);
0071 
0072     /**
0073      * TODO - port dmsBox to QML
0074      * @brief createDms creates dms from string
0075      * @param degree string that should be converted to degree
0076      * @param deg if true, the value is in degrees.  Otherwise, it is in hours.
0077      * @param ok
0078      * @return angle in dms
0079      */
0080     dms createDms(const QString &degree, bool deg, bool *ok);
0081 
0082     /**
0083      * @short Retrieve name of location by latitude and longitude. Name will be sent with
0084      * sendNameFromCoordinates signal
0085      */
0086     Q_INVOKABLE void getNameFromCoordinates(double latitude, double longitude);
0087 
0088   public slots:
0089     void initCityList();
0090     void updateCurrentLocation();
0091 
0092     void processLocationNameData(QNetworkReply *rep);
0093 
0094   signals:
0095     void currentLocationChanged(QString);
0096     void TZListChanged(QStringList);
0097     void DSTRulesChanged(QStringList);
0098     void currLocIndexChanged(int);
0099     void newNameFromCoordinates(QString city, QString region, QString country);
0100 
0101   private:
0102     /**
0103      * @short checks whether database with cities is already created. Creates a new otherwise
0104      * @return city database
0105      */
0106     QSqlDatabase getDB();
0107 
0108     QStringListModel m_cityList;
0109     QHash<QString, GeoLocation *> filteredCityList;
0110     GeoLocation *SelectedCity { nullptr };
0111     GeoLocation *currentGeo { nullptr };
0112     QString m_currentLocation;
0113     int m_currLocIndex { 0 };
0114 
0115     QStringList m_TZList;
0116     QStringList m_DSTRules;
0117 
0118     //Retrieve the name of city
0119     QNetworkAccessManager *nam { nullptr };
0120 };