view ui/downloader.h @ 169:701b7036c5dc

Windows implementation of port_realpath: NULL if file does not exist.
author Sascha Wilde <wilde@intevation.de>
date Tue, 25 Mar 2014 13:20:23 +0100
parents 09cd242d8443
children 17e1c8f37d72
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 <QDateTime>
#include <QString>
#include <QByteArray>

#include "sslconnection.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
     * @param[in] resourceSW the path where the software is to be found
     * @param[in] resourceList the path where the list is to be found
     */
    Downloader(QObject* parent, const QString& url,
               const QByteArray& certificate = QByteArray(),
               const QDateTime& newestSW = QDateTime(),
               const QDateTime& newestList = QDateTime(),
               const QString& resourceSW = QString(),
               const QString& resourceList = QString());

    ~Downloader();

    /**
     * @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.
     **/
    SSLConnection::ErrorCode getErrorState();

protected:
    void run();

private:
    QDateTime mLastModSW;
    QDateTime mLastModList;

    QString mResourceSW;
    QString mResourceList;

    SSLConnection mSSLConnection;

    /** @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);
    /**
     * @brief parses the Headers of a repsonse.
     *
     * This removes the headers from the byte array passed as
     * parameter.
     *
     * @param[inout] data: The response to parse.
     *
     * @returns: A map of the header fields. Or an empty map on error.
     */
    QMap<QString, QString> parseHeaders(QByteArray *data);

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

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