diff ui/downloader.h @ 15:95e1b6edf2fc

Implement more downloader functionality for Windows
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 19 Feb 2014 10:45:06 +0000
parents fe39d93f1261
children 62cd56cea09b
line wrap: on
line diff
--- a/ui/downloader.h	Wed Feb 19 10:44:40 2014 +0000
+++ b/ui/downloader.h	Wed Feb 19 10:45:06 2014 +0000
@@ -9,6 +9,13 @@
 #include <QThread>
 #include <QString>
 #include <QByteArray>
+#include <QDateTime>
+
+#ifdef Q_OS_WIN
+#include <windows.h>
+#include <winhttp.h>
+#endif
+
 
 class Downloader: public QThread
 {
@@ -16,47 +23,46 @@
 
 public:
     /**
-     * @brief Construct a downloader to download data from url
-     *
-     * Takes the builtin certificate for https verification.
+     * @brief Construct a downloader with a specific certificate
      *
-     * @param[in] parent the parent object.
-     * @param[in] url the Url to download data from.
-     */
-    Downloader(QObject* parent, const QString& url);
-
-    /**
-     * @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 to accept https connection 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);
+               const QByteArray& certificate = QByteArray(),
+               const QDateTime& newestSW = QDateTime(),
+               const QDateTime& newestList = QDateTime());
 
-    enum Status {
-        NewSoftwareAvailable, // A Software Update has been downloaded
-        NewListAvailable, // A certificate list has been downloaded
-        UpToDate, // Nothing changed
-        Error // An error happened
-    };
 
     enum ErrorCode {
         NoConnection,
         InvalidCertificate,
         ConnectionLost,
         Timeout,
-        Unknown
+        ErrUnknown
     };
 
     /**
-     * @brief Construct a downloader with a specific certificate
+     * @brief get the directory where the downloader saves data
      *
-     * @param[in] url the Url to download data from
-     * @param[in] certificate to accept https connection from
-     */
-    QString getDownloadedFileName();
+     * 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();
@@ -65,8 +71,63 @@
     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.
@@ -80,7 +141,6 @@
      *
      * @param[out] message: A message to show. Can be empty.
      * @param[out] errorCode: ErrorCode of this error.
-     * @param[out] total: Total value of possible progress.
      */
     void error(const QString &message, ErrorCode error);
 };

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