view ui/downloader.h @ 20:fec1a610d43f

Mark usage of goto with TODOs to remove them
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 20 Feb 2014 10:57:20 +0000
parents 95e1b6edf2fc
children 62cd56cea09b
line wrap: on
line source
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
/**
 * @file downloader.h
 * @brief High level API to download necessary data.
 *
 */

#include <QThread>
#include <QString>
#include <QByteArray>
#include <QDateTime>

#ifdef Q_OS_WIN
#include <windows.h>
#include <winhttp.h>
#endif


class Downloader: public QThread
{
    Q_OBJECT

public:
    /**
     * @brief Construct a downloader with a specific certificate
     *
     * The downloader will check the last-modified date of the
     * certificate list / sw on the server at the specified url
     * and download those accordingly. If newestSW or newestList is
     * are valid datetimes only files modified after the respective date
     * are downloaded.
     *
     * Downloaded files are placed in QStandardPaths::DataLocation
     *
     * @param[in] parent the parent object.
     * @param[in] url the Url to download data from
     * @param[in] certificate optional certificate to validate https connection
     * @param[in] newestSW datetime after which software should be downloaded
     * @param[in] newestList datetime after which the list should be downloaded
     */
    Downloader(QObject* parent, const QString& url,
               const QByteArray& certificate = QByteArray(),
               const QDateTime& newestSW = QDateTime(),
               const QDateTime& newestList = QDateTime());


    enum ErrorCode {
        NoConnection,
        InvalidCertificate,
        ConnectionLost,
        Timeout,
        ErrUnknown
    };

    /**
     * @brief get the directory where the downloader saves data
     *
     * If the directory does not exist this function ensures that it
     * is created.
     *
     * @returns The directory in which downloaded files are placed.
     **/
    QString getDataDirectory();


protected:
    void run();

private:
    QString mUrl;
    QByteArray mCert;

    QDateTime mLastModSW;
    QDateTime mLastModList;

#ifdef Q_OS_WIN
    /** @brief Download a file from the Internet
     *
     * @param[in] HSession the session to work in.
     * @param[in] HConnect the connection to use.
     * @param[in] resource the resource to download.
     * @param[in] filename where the file should be saved.
     * @param[in] maxSize maximum amount of bytes to download
     *
     * @returns True if the download was successful.
     */
    bool downloadFile(HINTERNET hSession, HINTERNET hConnect,
            LPCWSTR resource, const QString &filename, DWORD maxSize);

    /** @brief get the last modified header of a resource.
     *
     * On error call getLastError to get extended error information.
     * This function still does not do any networking but only initializes
     * it.
     *
     * @param[in] HSession the session to work in.
     * @param[in] HConnect the connection to use.
     * @param[in] resource the resource to check the last-modified date on
     *
     * @returns the last modified date or a null datetime in case of errors
     */
    QDateTime getLastModifiedHeader(HINTERNET hSession,
        HINTERNET hConnect, LPCWSTR resource);

    /** @brief verify that the certificate of the request matches
     *
     * Validates the certificate against the member variable certificate
     *
     * @param[in] hRequest: The request from which to get the certificate
     *
     * @returns True if the certificate exactly matches the one in hRequest
     */

    bool verifyCertificate(HINTERNET hRequest);
#endif

Q_SIGNALS:
    /**
     * @brief software update is available
     */
    void newSoftwareAvailable(const QString &fileName,
            const QDateTime &lastMod);

    /**
     * @brief new certificate list available
     */
    void newListAvailable(const QString &fileName, const QDateTime &lastMod);

    /**
     * @brief Some progress has been made.
     *
     * @param[out] message: A message to show. Can be empty.
     * @param[out] current: Value of current progress.
     * @param[out] total: Total value of possible progress
     */
    void progress(const QString &message, int current, int total);

    /**
     * @brief An error happened
     *
     * @param[out] message: A message to show. Can be empty.
     * @param[out] errorCode: ErrorCode of this error.
     */
    void error(const QString &message, ErrorCode error);
};
#endif

http://wald.intevation.org/projects/trustbridge/