aheinecke@10: #ifndef DOWNLOADER_H aheinecke@10: #define DOWNLOADER_H aheinecke@10: /** aheinecke@10: * @file downloader.h aheinecke@10: * @brief High level API to download necessary data. aheinecke@10: * aheinecke@10: */ aheinecke@10: aheinecke@10: #include aheinecke@10: #include aheinecke@10: #include aheinecke@15: #include aheinecke@15: aheinecke@15: #ifdef Q_OS_WIN aheinecke@15: #include aheinecke@15: #include aheinecke@15: #endif aheinecke@15: aheinecke@10: aheinecke@10: class Downloader: public QThread aheinecke@10: { aheinecke@10: Q_OBJECT aheinecke@10: aheinecke@10: public: aheinecke@10: /** aheinecke@15: * @brief Construct a downloader with a specific certificate aheinecke@10: * aheinecke@15: * The downloader will check the last-modified date of the aheinecke@15: * certificate list / sw on the server at the specified url aheinecke@15: * and download those accordingly. If newestSW or newestList is aheinecke@15: * are valid datetimes only files modified after the respective date aheinecke@15: * are downloaded. aheinecke@15: * aheinecke@15: * Downloaded files are placed in QStandardPaths::DataLocation aheinecke@10: * aheinecke@10: * @param[in] parent the parent object. aheinecke@10: * @param[in] url the Url to download data from aheinecke@15: * @param[in] certificate optional certificate to validate https connection aheinecke@15: * @param[in] newestSW datetime after which software should be downloaded aheinecke@15: * @param[in] newestList datetime after which the list should be downloaded aheinecke@10: */ aheinecke@10: Downloader(QObject* parent, const QString& url, aheinecke@15: const QByteArray& certificate = QByteArray(), aheinecke@15: const QDateTime& newestSW = QDateTime(), aheinecke@15: const QDateTime& newestList = QDateTime()); aheinecke@10: aheinecke@10: aheinecke@10: enum ErrorCode { aheinecke@10: NoConnection, aheinecke@10: InvalidCertificate, aheinecke@10: ConnectionLost, aheinecke@10: Timeout, aheinecke@15: ErrUnknown aheinecke@10: }; aheinecke@10: aheinecke@10: /** aheinecke@15: * @brief get the directory where the downloader saves data aheinecke@10: * aheinecke@15: * If the directory does not exist this function ensures that it aheinecke@15: * is created. aheinecke@15: * aheinecke@15: * @returns The directory in which downloaded files are placed. aheinecke@15: **/ aheinecke@15: QString getDataDirectory(); aheinecke@15: aheinecke@10: aheinecke@10: protected: aheinecke@10: void run(); aheinecke@10: aheinecke@10: private: aheinecke@10: QString mUrl; aheinecke@10: QByteArray mCert; aheinecke@10: aheinecke@15: QDateTime mLastModSW; aheinecke@15: QDateTime mLastModList; aheinecke@15: aheinecke@15: #ifdef Q_OS_WIN aheinecke@15: /** @brief Download a file from the Internet aheinecke@15: * aheinecke@15: * @param[in] HSession the session to work in. aheinecke@15: * @param[in] HConnect the connection to use. aheinecke@15: * @param[in] resource the resource to download. aheinecke@15: * @param[in] filename where the file should be saved. aheinecke@15: * @param[in] maxSize maximum amount of bytes to download aheinecke@15: * aheinecke@15: * @returns True if the download was successful. aheinecke@15: */ aheinecke@15: bool downloadFile(HINTERNET hSession, HINTERNET hConnect, aheinecke@15: LPCWSTR resource, const QString &filename, DWORD maxSize); aheinecke@15: aheinecke@15: /** @brief get the last modified header of a resource. aheinecke@15: * aheinecke@15: * On error call getLastError to get extended error information. aheinecke@15: * This function still does not do any networking but only initializes aheinecke@15: * it. aheinecke@15: * aheinecke@15: * @param[in] HSession the session to work in. aheinecke@15: * @param[in] HConnect the connection to use. aheinecke@15: * @param[in] resource the resource to check the last-modified date on aheinecke@15: * aheinecke@15: * @returns the last modified date or a null datetime in case of errors aheinecke@15: */ aheinecke@15: QDateTime getLastModifiedHeader(HINTERNET hSession, aheinecke@15: HINTERNET hConnect, LPCWSTR resource); aheinecke@15: aheinecke@15: /** @brief verify that the certificate of the request matches aheinecke@15: * aheinecke@15: * Validates the certificate against the member variable certificate aheinecke@15: * aheinecke@15: * @param[in] hRequest: The request from which to get the certificate aheinecke@15: * aheinecke@15: * @returns True if the certificate exactly matches the one in hRequest aheinecke@15: */ aheinecke@15: aheinecke@15: bool verifyCertificate(HINTERNET hRequest); aheinecke@15: #endif aheinecke@15: aheinecke@10: Q_SIGNALS: aheinecke@10: /** aheinecke@15: * @brief software update is available aheinecke@15: */ aheinecke@15: void newSoftwareAvailable(const QString &fileName, aheinecke@15: const QDateTime &lastMod); aheinecke@15: aheinecke@15: /** aheinecke@15: * @brief new certificate list available aheinecke@15: */ aheinecke@15: void newListAvailable(const QString &fileName, const QDateTime &lastMod); aheinecke@15: aheinecke@15: /** aheinecke@10: * @brief Some progress has been made. aheinecke@10: * aheinecke@10: * @param[out] message: A message to show. Can be empty. aheinecke@10: * @param[out] current: Value of current progress. aheinecke@10: * @param[out] total: Total value of possible progress aheinecke@10: */ aheinecke@10: void progress(const QString &message, int current, int total); aheinecke@10: aheinecke@10: /** aheinecke@10: * @brief An error happened aheinecke@10: * aheinecke@10: * @param[out] message: A message to show. Can be empty. aheinecke@10: * @param[out] errorCode: ErrorCode of this error. aheinecke@10: */ aheinecke@10: void error(const QString &message, ErrorCode error); aheinecke@10: }; aheinecke@10: #endif