view ui/downloader.h @ 804:65c3fae22fdd

Merged
author Sascha Wilde <wilde@intevation.de>
date Tue, 15 Jul 2014 15:51:26 +0200
parents 5834b340c54c
children d1c951b3012d
line wrap: on
line source
/* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU GPL (v>=2)
 * and comes with ABSOLUTELY NO WARRANTY!
 * See LICENSE.txt for details.
 */
#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(),
               bool downloadSW = true);

    ~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;

    bool mDownloadSW;

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

    /**
     * @brief Found the last modified date for software.
     *
     * @param[out] date The last modified date.
     */
    void lastModifiedDate(const QDateTime &date);
};
#endif

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