// bytran -|- spectral calculations for portable devices using the HITRAN database
//
// Developed by Dzianis Pliutau (Denis Plutov) and Kostya Roslyakov
// www.bytran.org | bytran@bytran.org
// Copyright (c) 2016 Dzianis Pliutau
// Distributed under the MIT license
//
// The MIT License (MIT)
// Copyright (c) 2016 Dzianis Pliutau
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



// Object to carry out downloading of a remote file
// This implementation is based on a tutorial at "http://www.bogotobogo.com/Qt/Qt5_Downloading_Files_QNetworkAccessManager_QNetworkRequest.php"
#ifndef FILEDOWNLOADER_H
#define FILEDOWNLOADER_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QByteArray>
#include <QUrl>
#include <QDateTime>
#include <QFile>

class FileDownloader : public QObject
{
    Q_OBJECT

public:

    explicit FileDownloader(QObject *parent = 0);

    // Public method to carry out download of a remote file
    void download();

    // Method to get and set the save path
    QString getSavePath();
    void setSavePath(QString newSavePath);

    // Getting and setting the download path
    QString getDownloadPath();
    void setDownloadPath(QString newDownloadPath);

    // Getting and setting the download URL
    QUrl getDownloadUrl();
    void setDownloadUrl(QUrl newDownloadUrl);

    // Methods to get and set a variable indicating if the data is to be saved to local storage
    bool getSaveToHarddive();
    void setSaveToHarddrive(bool newSaveToHarddrive);

    // Method sets a variable indicating if a URL or String is passed to the downloader with the download address
    bool getDownloadIsQtUrl();
    void setDownloadIsQtUrl(bool isUrl);

signals:

    void dataDownloaded(QByteArray *data, bool success);

public slots:

    // Slot is called after a reply from a remote server has been received
    void replyFinished (QNetworkReply *reply);

private:

    // Manager to access the network
    QNetworkAccessManager *netManager;

    // Method to create actions
    void createActions();

    // Path to save the downloaded data to
    QString savePath;

    // Path to the file online to download from
    QString downloadPath;
    QUrl downloadUrl;

    // Variable indicates if the download path is specified as a URL or as a string
    bool downloadIsQtUrl;

    // Boolean variable indicating if the files need to be saved to harddrive in addition to returning a string of retrieved data
    bool saveToHarddrive;

};

#endif // FILEDOWNLOADER_H
