File indexing completed on 2024-03-24 15:14:59

0001 /*
0002     SPDX-FileCopyrightText: 2012 Rishab Arora <ra.rishab@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "auxiliary/profileinfo.h"
0010 #ifndef KSTARS_LITE
0011 #include "oal/oal.h"
0012 #endif
0013 #include <oal/filter.h>
0014 
0015 #include <QFile>
0016 #include <QSqlDatabase>
0017 #include <QSqlError>
0018 #include <QStringList>
0019 #include <QVariantMap>
0020 #include <QXmlStreamReader>
0021 
0022 #include <memory>
0023 
0024 class LineList;
0025 class ArtificialHorizonEntity;
0026 class ImageOverlay;
0027 
0028 /**
0029  * @brief Single class to delegate all User database I/O
0030  *
0031  * usage: Call QSqlDatabase::removeDatabase("userdb"); after the object
0032  * of this class is deallocated
0033  * @author Rishab Arora
0034  * @author Jasem Mutlaq
0035  * @version 1.2
0036  **/
0037 // cppcheck-suppress noConstructor
0038 class KSUserDB
0039 {
0040     public:
0041         ~KSUserDB();
0042 
0043         /**
0044          * Initialize KStarsDB while running splash screen
0045          * @return true on success
0046          */
0047         bool Initialize();
0048 
0049         const QString &connectionName() const
0050         {
0051             return m_ConnectionName;
0052         }
0053 
0054         /************************************************************************
0055          ********************************* Drivers ******************************
0056          ************************************************************************/
0057 
0058         int AddProfile(const QString &name);
0059 
0060         bool DeleteProfile(const QSharedPointer<ProfileInfo> &pi);
0061 
0062         // Delete profile and all related settings.
0063         bool PurgeProfile(const QSharedPointer<ProfileInfo> &pi);
0064 
0065         bool SaveProfile(const QSharedPointer<ProfileInfo> &pi);
0066 
0067         /**
0068          * @brief GetAllProfiles Return all profiles in a QList
0069          * @return QMap with the keys as profile names and values are profile ids.
0070          */
0071         bool GetAllProfiles(QList<QSharedPointer<ProfileInfo> > &profiles);
0072 
0073         /************************************************************************
0074          ******************************* Dark Library****************************
0075          ************************************************************************/
0076 
0077         bool AddDarkFrame(const QVariantMap &oneFrame);
0078         bool UpdateDarkFrame(const QVariantMap &oneFrame);
0079         bool DeleteDarkFrame(const QString &filename);
0080         bool GetAllDarkFrames(QList<QVariantMap> &darkFrames);
0081 
0082 
0083         /************************************************************************
0084          ******************************* Effective FOVs *************************
0085          ************************************************************************/
0086 
0087         bool AddEffectiveFOV(const QVariantMap &oneFOV);
0088         bool DeleteEffectiveFOV(const QString &id);
0089         bool GetAllEffectiveFOVs(QList<QVariantMap> &effectiveFOVs);
0090 
0091 
0092         /************************************************************************
0093          ******************************* Driver Alias *************************
0094          ************************************************************************/
0095 
0096         bool AddCustomDriver(const QVariantMap &oneDriver);
0097         bool DeleteCustomDriver(const QString &id);
0098         bool GetAllCustomDrivers(QList<QVariantMap> &CustomDrivers);
0099 
0100         /************************************************************************
0101          *********************************** HiPS *******************************
0102          ************************************************************************/
0103 
0104         bool AddHIPSSource(const QMap<QString, QString> &oneSource);
0105         bool DeleteHIPSSource(const QString &ID);
0106         bool GetAllHIPSSources(QList<QMap<QString, QString>> &HIPSSources);
0107 
0108         /************************************************************************
0109          *********************************** DSLR *******************************
0110          ************************************************************************/
0111 
0112         bool AddDSLRInfo(const QMap<QString, QVariant> &oneInfo);
0113         bool DeleteDSLRInfo(const QString &model);
0114         bool DeleteAllDSLRInfo();
0115         bool GetAllDSLRInfos(QList<QMap<QString, QVariant>> &DSLRInfos);
0116 
0117         /************************************************************************
0118          ******************************* Observers ******************************
0119          ************************************************************************/
0120 
0121         /** @brief Adds a new observer into the database **/
0122         bool AddObserver(const QString &name, const QString &surname, const QString &contact);
0123 
0124         /**
0125          * @brief Returns the unique id of the user with given name & surname
0126          *
0127          * @return true if found, false otherwise
0128          **/
0129         bool FindObserver(const QString &name, const QString &surname);
0130         /**
0131          * @brief Removes the user with unique id as given by FindObserver
0132          * Returns false if the user is not found
0133          *
0134          * @return bool
0135          **/
0136         bool DeleteObserver(const QString &id);
0137 
0138 #ifndef KSTARS_LITE
0139         /**
0140          * @brief Updates the passed reference of observer_list with all observers
0141          * The original content of the list is cleared.
0142          *
0143          * @return true if database read was successfull, false otherwise.
0144          **/
0145         bool GetAllObservers(QList<OAL::Observer *> &observer_list);
0146 #endif
0147         /************************************************************************
0148          ********************************* Horizon ******************************
0149          ************************************************************************/
0150 
0151         /** @brief Deletes all artificial horizon rows from the database **/
0152         bool DeleteAllHorizons();
0153 
0154         /** @brief Adds a new artificial horizon row into the database **/
0155         bool AddHorizon(ArtificialHorizonEntity *horizon);
0156 
0157         /** @brief Gets all the artificial horizon rows from the database **/
0158         bool GetAllHorizons(QList<ArtificialHorizonEntity *> &horizonList);
0159 
0160         /************************************************************************
0161          ****************************** ImageOverlay ****************************
0162          ************************************************************************/
0163 
0164         /** @brief Deletes all image overlay rows from the database **/
0165         bool DeleteAllImageOverlays();
0166 
0167         /** @brief Adds a new image overlay row into the database **/
0168         bool AddImageOverlay(const ImageOverlay &overlay);
0169 
0170         /** @brief Gets all the image overlay rows from the database **/
0171         bool GetAllImageOverlays(QList<ImageOverlay> *imageOverlayList);
0172 
0173         /************************************************************************
0174          ********************************* Flags ********************************
0175          ************************************************************************/
0176 
0177         /**
0178          * @brief Erases all the flags from the database
0179          *
0180          * @return void
0181          **/
0182         bool DeleteAllFlags();
0183 
0184         /**
0185          * @brief Add a new Flag with given parameters
0186          *
0187          * @param ra Right Ascension
0188          * @param dec Declination
0189          * @param epoch Epoch
0190          * @param image_name Name of the image used
0191          * @param label Content of display label on screen
0192          * @param labelColor Color of the label (name or hex code) eg #00FF00
0193          * @return True if database transaction is successful, false otherwise
0194          **/
0195         bool AddFlag(const QString &ra, const QString &dec, const QString &epoch, const QString &image_name,
0196                      const QString &label, const QString &labelColor);
0197         /**
0198          * @brief Returns a QList populated with all stored flags
0199          * Order: const QString &ra, const QString &dec, const QString &epoch,
0200          *        const QString &imageName, const QString &label, const QString &labelColor
0201          * @return
0202          **/
0203         bool GetAllFlags(QList<QStringList> &flagList);
0204 
0205         /************************************************************************
0206           ******************************* Equipment ******************************
0207          ************************************************************************/
0208 
0209         /**
0210          * @brief Erase the equipment with given type and unique id
0211          * Valid equipment types: "telescope","lens","filter"
0212          *
0213          * @param type Equipment type (same as table name)
0214          * @param id Unique id (same as row number)
0215          * @return void
0216          **/
0217         bool DeleteEquipment(const QString &type, const QString &id);
0218         /**
0219          * @brief Erases the whole equipment table of given type
0220          *
0221          * @param type Equipment type (same as table name)
0222          * @return void
0223          **/
0224         bool DeleteAllEquipment(const QString &type);
0225 
0226         /************************************************************************
0227          ********************************** Scope *******************************
0228          ************************************************************************/
0229 
0230         /**
0231          * @brief Appends the scope with given details in the database
0232          *
0233          * @return void
0234          **/
0235         bool AddScope(const QString &model, const QString &vendor, const QString &type,
0236                       const double &aperture, const double &focalLength);
0237         /**
0238          * @brief Replaces the scope with given ID with provided content
0239          *
0240          * @return void
0241          **/
0242         bool AddScope(const QString &model, const QString &vendor, const QString &type, const double &aperture,
0243                       const double &focalLength, const QString &id);
0244 #ifndef KSTARS_LITE
0245         /**
0246          * @brief updates the scope list with all scopes from database
0247          * List is cleared and then filled with content.
0248          *
0249          * @param m_scopeList Reference to list to be updated
0250          * @return void
0251          **/
0252         bool GetAllScopes(QList<OAL::Scope *> &m_scopeList);
0253 #endif
0254 
0255         /************************************************************************
0256          ******************************* Optical Elements ***********************
0257          ************************************************************************/
0258         bool getOpticalElementByID(int id, QJsonObject &element);
0259         bool getOpticalElementByName(const QString &name, QJsonObject &element);
0260         /**
0261          * @brief getLastOpticalElement Return last inserted scope or lens
0262          * @param element JSON object to fill with scope or lens metadata
0263          * @return True if found, false if none found.
0264          */
0265         bool getLastOpticalElement(QJsonObject &element);
0266         QStringList getOpticalElementNames();
0267 
0268         /************************************************************************
0269          ******************************* Eye Piece ******************************
0270          ************************************************************************/
0271 
0272         /**
0273          * @brief Add new eyepiece to database
0274          *
0275          * @return void
0276          **/
0277         bool AddEyepiece(const QString &vendor, const QString &model, const double &focalLength, const double &fov,
0278                          const QString &fovunit);
0279         /**
0280          * @brief Replace eyepiece at position (ID) with new content
0281          *
0282          * @return void
0283          **/
0284         bool AddEyepiece(const QString &vendor, const QString &model, const double &focalLength, const double &fov,
0285                          const QString &fovunit, const QString &id);
0286 #ifndef KSTARS_LITE
0287         /**
0288          * @brief Populate the reference passed with all eyepieces
0289          *
0290          * @param m_eyepieceList Reference to list of eyepieces
0291          * @return void
0292          **/
0293         bool GetAllEyepieces(QList<OAL::Eyepiece *> &m_eyepieceList);
0294 #endif
0295         /************************************************************************
0296          ********************************** Lens ********************************
0297          ************************************************************************/
0298 
0299         /**
0300          * @brief Add a new lens to the database
0301          *
0302          * @return void
0303          **/
0304         bool AddLens(const QString &vendor, const QString &model, const double &factor);
0305         /**
0306          * @brief Replace a lens at given ID with new content
0307          *
0308          * @return void
0309          **/
0310         bool AddLens(const QString &vendor, const QString &model, const double &factor, const QString &id);
0311 #ifndef KSTARS_LITE
0312         /**
0313          * @brief Populate the reference passed with all lenses
0314          *
0315          * @param m_lensList Reference to list of lenses
0316          * @return void
0317          **/
0318         bool GetAllLenses(QList<OAL::Lens *> &m_lensList);
0319 #endif
0320 
0321         /************************************************************************
0322          ********************************** DSLR Lens ***************************
0323          ************************************************************************/
0324 
0325         /**
0326          * @brief Appends the DSLR lens with given details in the database
0327          *
0328          * @return void
0329          **/
0330         bool AddDSLRLens(const QString &model, const QString &vendor, const double focalLength, const double focalRatio);
0331         /**
0332          * @brief Replaces the scope with given ID with provided content
0333          *
0334          * @return void
0335          **/
0336         bool AddDSLRLens(const QString &model, const QString &vendor, const double focalLength, const double focalRatio,
0337                          const QString &id);
0338 #ifndef KSTARS_LITE
0339         /**
0340          * @brief updates the dslr list with all DSLR lenses from database
0341          * List is cleared and then filled with content.
0342          *
0343          * @param dslrlens_list Reference to list to be updated
0344          * @return void
0345          **/
0346         bool GetAllDSLRLenses(QList<OAL::DSLRLens *> &dslrlens_list);
0347 #endif
0348 
0349         /************************************************************************
0350          ******************************** Filters *******************************
0351          ************************************************************************/
0352 
0353         /**
0354          * @brief Add a new filter to the database
0355          *
0356          * @return void
0357          **/
0358         bool AddFilter(const filterProperties *fp);
0359         /**
0360          * @brief Replace a filter at given ID with new content
0361          *
0362          * @return void
0363          **/
0364         bool AddFilter(const filterProperties *fp, const QString &id);
0365         /**
0366          * @brief Populate the reference passed with all filters
0367          *
0368          * @param m_filterList Reference to list of filters
0369          * @return void
0370          **/
0371         bool GetAllFilters(QList<OAL::Filter *> &m_filterList);
0372 
0373         /************************************************************************
0374          ******************************** Optical Trains ************************
0375          ************************************************************************/
0376 
0377         /**
0378          * @brief Add a new optical train to the database
0379          * @param oneTrain optical train data
0380          **/
0381         bool AddOpticalTrain(const QVariantMap &oneTrain);
0382 
0383         /**
0384          * @brief Update an existing optical train
0385          * @param oneTrain optical train data
0386          * @param id ID of train to replace in database
0387          **/
0388         bool UpdateOpticalTrain(const QVariantMap &oneTrain, int id);
0389 
0390         bool DeleteOpticalTrain(int id);
0391         /**
0392          * @brief Populate the reference passed with all optical trains
0393          * @param opticalTrains Reference to all trains list
0394          **/
0395         bool GetOpticalTrains(uint32_t profileID, QList<QVariantMap> &opticalTrains);
0396 
0397 
0398         /************************************************************************
0399          ******************************** Profile Settings **********************
0400          ************************************************************************/
0401 
0402         /**
0403          * @brief Add new profile settings to the database
0404          * @param settings JSON settings
0405          **/
0406         void AddProfileSettings(uint32_t profile, const QByteArray &settings);
0407 
0408         void UpdateProfileSettings(uint32_t profileID, const QByteArray &settings);
0409 
0410         void DeleteProfileSettings(uint32_t profile);
0411         /**
0412          * @brief Populate the reference passed with settings for one paritcular profile
0413          * @param profile id of profile
0414          * @param settings populate settings with parsed profile settings.
0415          **/
0416         bool GetProfileSettings(uint32_t profile, QVariantMap &settings);
0417 
0418         /************************************************************************
0419          ******************************** Train Settings **********************
0420          ************************************************************************/
0421 
0422         /**
0423          * @brief Add new Train settings to the database
0424          * @param settings JSON settings
0425          **/
0426         bool AddOpticalTrainSettings(uint32_t train, const QByteArray &settings);
0427 
0428         bool UpdateOpticalTrainSettings(uint32_t train, const QByteArray &settings);
0429 
0430         bool DeleteOpticalTrainSettings(uint32_t train);
0431         /**
0432          * @brief Populate the reference passed with settings for one paritcular Train
0433          * @param TrainID id of Train
0434          * @param settings populate settings with parsed Train settings.
0435          **/
0436         bool GetOpticalTrainSettings(uint32_t train, QVariantMap &settings);
0437 
0438         /************************************************************************
0439          *********************** Collimation Overlay Elements *******************
0440          ************************************************************************/
0441 
0442         /**
0443          * @brief Add a new collimation overlay element to the database
0444          * @param oneElement collimation overlay element data
0445          **/
0446         bool AddCollimationOverlayElement(const QVariantMap &oneElement);
0447 
0448         /**
0449          * @brief Update an existing collimation overlay element
0450          * @param oneElement collimation overlay element data
0451          * @param id ID of element to replace in database
0452          **/
0453         bool UpdateCollimationOverlayElement(const QVariantMap &oneElement, int id);
0454 
0455         bool DeleteCollimationOverlayElement(int id);
0456 
0457         /**
0458          * @brief Populate the reference passed with all collimation overlay elements
0459          * @param collimationOverlayElements Reference to all elements list
0460          **/
0461         bool GetCollimationOverlayElements(QList<QVariantMap> &collimationOverlayElements);
0462 
0463     private:
0464         /**
0465          * @brief This function initializes a new database in the user's directory.
0466          * To be run only when a new db is needed. Should not be run over existing database file.
0467          *
0468          * @return bool
0469          **/
0470         bool RebuildDB();
0471         /**
0472          * @brief Rebuilds the User DB from scratch using RebuildDB.
0473          * Also, loads any previous user data into the DB.
0474          *
0475          * @return bool
0476          **/
0477         bool FirstRun();
0478 
0479         /** @brief creates the image overlay table if it doesn't already exist **/
0480         void CreateImageOverlayTableIfNecessary();
0481 
0482 #if 0
0483         /**
0484          * @brief Imports flags data from previous format
0485          *
0486          * @return bool
0487          **/
0488         bool ImportFlags();
0489         /**
0490          * @brief Imports users from previous format
0491          *
0492          * @return bool
0493          **/
0494         bool ImportUsers();
0495         /**
0496          * @brief Imports equipment from previous format
0497          *
0498          * @return bool
0499          **/
0500         bool ImportEquipment();
0501 
0502 #endif
0503         // Helper functions
0504         void readScopes();
0505         void readScope();
0506         void readEyepieces();
0507         void readEyepiece();
0508         void readLenses();
0509         void readLens();
0510         void readFilters();
0511         void readFilter();
0512 
0513         bool DeleteProfileDrivers(const QSharedPointer<ProfileInfo> &pi);
0514         bool GetProfileDrivers(const QSharedPointer<ProfileInfo> &pi);
0515         //void GetProfileCustomDrivers(ProfileInfo *pi);
0516 
0517         /** XML reader for importing old formats **/
0518         QXmlStreamReader *reader_ { nullptr };
0519 
0520         QString m_ConnectionName;
0521 
0522         static const uint16_t SCHEMA_VERSION = 314;
0523 };