view ui/downloader.h @ 32:d8e93fa1fc93

Downloader logic
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 12 Mar 2014 21:26:07 +0100
parents 62cd56cea09b
children c6125d73faf4
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>
#include <QUrl>

#include <polarssl/x509_crt.h>
#include <polarssl/entropy.h>
#include <polarssl/ctr_drbg.h>
#include <polarssl/ssl.h>

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());

    ~Downloader();

    enum ErrorCode {
        NoError,
        NoConnection,
        SSLHandshakeFailed,
        InvalidCertificate,
        InvalidPinnedCertificate,
        InvalidResponse,
        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();

    /**
     * @brief get the current error state
     *
     * @returns The current error state.
     **/
    ErrorCode getErrorState();

protected:
    void run();

private:
    QUrl mUrl;
    QByteArray mPinnedCert;
    x509_crt mX509PinnedCert;
    entropy_context mEntropy;
    ctr_drbg_context mCtr_drbg;
    ssl_context mSSL;

    QDateTime mLastModSW;
    QDateTime mLastModList;

    /* Convienience to avoid having to parse all
     * PolarSSL errors */
    ErrorCode mErrorState;

    bool mInitialized;
    bool mConnectionEstablished;

    int mServerFD;


    /* @brief: Initialize polarssl structures
     *
     * This wraps polarssl initialization functions
     * that can return an error.
     * Sets the error state accordingly.
     *
     * @returns: 0 on success a polarssl error otherwise.
     */
    int init();

    /** @brief: Establish the ssl connection
     *
     * Modifies the error state. Before calling this the mServerFD should
     * be set to a valid socket.
     *
     * @returns 0 on success otherwise a polarssl error or -1 is returned
     */
    int establishSSLConnection();

    /** @brief get the last modified header of a resource.
     *
     * Connection should be established beforehand.
     * Modifies the error state.
     *
     * @param[in] resource The resource to check
     *
     * @returns the last modified date or a null datetime in case of errors
     */
    QDateTime getLastModifiedHeader(const QString &resource);

    /** @brief Download resource
     *
     * Download a resource with the established connection.
     * Modifies the error state.
     *
     * @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(const QString &resource, const QString &filename,
                      size_t maxSize);

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, Downloader::ErrorCode error);
};
#endif

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