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

0001 /*
0002     SPDX-FileCopyrightText: 2016 Akarsh Simha <akarsh.simha@kdemail.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "ksdssimage.h"
0010 
0011 #include <QObject>
0012 #include <QString>
0013 #include <QStringList>
0014 #include <QTemporaryFile>
0015 #include <QUrl>
0016 
0017 #include <functional>
0018 
0019 class FileDownloader;
0020 class SkyPoint;
0021 class dms;
0022 
0023 /**
0024  * @class KSDssDownloader
0025  * @short Helps download a DSS image
0026  * @author Akarsh Simha <akarsh.simha@kdemail.net>
0027  *
0028  * @note This object is designed to commit suicide (calls
0029  * QObject::deleteLater() )! Never allocate this using anything but
0030  * new -- do not allocate it on the stack! This is ideal for its
0031  * operation, as it deletes itself after downloading.
0032  */
0033 
0034 class KSDssDownloader : public QObject
0035 {
0036     Q_OBJECT
0037 
0038   public:
0039     /** @short Constructor */
0040     explicit KSDssDownloader(QObject *parent = nullptr);
0041 
0042     /**
0043      * @short Constructor that initiates a "standard" DSS download job, calls the downloadReady slot, and finally self destructs
0044      * @note Very important that if you create with this constructor,
0045      * the object will self-destruct. Avoid keeping pointers to it, or
0046      * things may segfault!
0047      */
0048     KSDssDownloader(const SkyPoint *const p, const QString &destFileName,
0049                     const std::function<void(bool)> &slotDownloadReady, QObject *parent = nullptr);
0050 
0051     /**
0052      * @short Stateful single-download of a supplied URL. Use when the flexibility is required
0053      * @note Does not self-delete this object. Construct with default constructor, and delete as usual.
0054      * @param srcUrl source DSS URL to download
0055      * @param destFileName destination image file (will be of PNG format)
0056      * @param md DSS image metadata to write into image file
0057      * @note emits downloadComplete with success state when done
0058      */
0059     void startSingleDownload(const QUrl srcUrl, const QString &destFileName, KSDssImage::Metadata &md);
0060 
0061     /**
0062      * @short High-level method to create a URL to obtain a DSS image for a given SkyPoint
0063      * @note If SkyPoint is a DeepSkyObject, this method automatically
0064      * decides the image size required to fit the object.
0065      * @note Moved from namespace KSUtils (--asimha, Jan 5 2016)
0066      */
0067     static QString getDSSURL(const SkyPoint *const p, const QString &version = "all",
0068                              struct KSDssImage::Metadata *md = nullptr);
0069 
0070     /**
0071      * @short High-level method to create a URL to obtain a DSS image for a given SkyPoint
0072      * @note This method includes an option to set the height, but uses default values for many parameters
0073      */
0074     static QString getDSSURL(const SkyPoint *const p, float width, float height = 0, const QString &version = "all",
0075                              struct KSDssImage::Metadata *md = nullptr);
0076 
0077     /**
0078      * @short Create a URL to obtain a DSS image for a given RA, Dec
0079      * @param ra The J2000.0 Right Ascension of the point
0080      * @param dec The J2000.0 Declination of the point
0081      * @param width The width of the image in arcminutes
0082      * @param height The height of the image in arcminutes
0083      * @param type_ The image type, either gif or fits.
0084      * @param version_ string describing which version to get
0085      * @param md If a valid pointer is provided, fill with metadata
0086      * @note This method resets height and width to fall within the range accepted by DSS
0087      * @note Moved from namespace KSUtils (--asimha, Jan 5 2016)
0088      *
0089      * @note Valid versions are: dss1, poss2ukstu_red, poss2ukstu_ir,
0090      * poss2ukstu_blue, poss1_blue, poss1_red, all, quickv,
0091      * phase2_gsc2, phase2_gsc1. Of these, dss1 uses POSS1 Red in the
0092      * north and POSS2/UKSTU Blue in the south. all uses the best of a
0093      * combined list of all plates.
0094      *
0095      */
0096     static QString getDSSURL(const dms &ra, const dms &dec, float width = 0, float height = 0,
0097                              const QString &type_ = "gif", const QString &version_ = "all",
0098                              struct KSDssImage::Metadata *md = nullptr);
0099 
0100     /** @short Write image metadata into file */
0101     static bool writeImageWithMetadata(const QString &srcFile, const QString &destFile, const KSDssImage::Metadata &md);
0102 
0103   signals:
0104     void downloadComplete(bool success);
0105     void downloadCanceled();
0106 
0107   private slots:
0108     void downloadAttemptFinished();
0109     void singleDownloadFinished();
0110     void downloadError(const QString &errorString);
0111 
0112   private:
0113     void startDownload(const SkyPoint *const p, const QString &destFileName);
0114     void initiateSingleDownloadAttempt(QUrl srcUrl);
0115     bool writeImageFile();
0116 
0117     QStringList m_VersionPreference;
0118     int m_attempt { 0 };
0119     struct KSDssImage::Metadata m_AttemptData;
0120     QString m_FileName;
0121     QTemporaryFile m_TempFile;
0122     FileDownloader *downloadJob { nullptr };
0123 };