2023-04-18 21:10:06 -04:00
|
|
|
#ifndef DOWNLOAD_H
|
|
|
|
#define DOWNLOAD_H
|
|
|
|
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QCryptographicHash>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QHash>
|
2024-06-06 11:59:28 -04:00
|
|
|
#include <QList>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QMap>
|
2023-04-18 21:10:06 -04:00
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QObject>
|
2024-06-06 11:59:28 -04:00
|
|
|
#include <QSslError>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QString>
|
2023-04-23 19:43:20 -04:00
|
|
|
#include <QThread>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QtGlobal>
|
|
|
|
|
|
|
|
class QByteArray;
|
2023-04-18 21:10:06 -04:00
|
|
|
|
2023-04-28 10:54:05 -04:00
|
|
|
struct ReleaseInfo {
|
|
|
|
Q_GADGET
|
|
|
|
Q_PROPERTY(QString version MEMBER version)
|
|
|
|
Q_PROPERTY(QString notes MEMBER notes)
|
|
|
|
Q_PROPERTY(QString contributors MEMBER contributors)
|
|
|
|
|
|
|
|
public:
|
|
|
|
QString version;
|
|
|
|
QString notes;
|
|
|
|
QString contributors;
|
|
|
|
};
|
|
|
|
|
2023-04-23 19:43:20 -04:00
|
|
|
class HashAndSaveFile : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
HashAndSaveFile();
|
|
|
|
|
|
|
|
public Q_SLOTS:
|
2024-03-05 11:31:31 -05:00
|
|
|
void hashAndSave(const QString &hash, QCryptographicHash::Algorithm a, const QString &saveFilePath,
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile, QNetworkReply *modelReply);
|
2023-04-23 19:43:20 -04:00
|
|
|
|
|
|
|
Q_SIGNALS:
|
2023-06-22 15:44:49 -04:00
|
|
|
void hashAndSaveFinished(bool success, const QString &error,
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile, QNetworkReply *modelReply);
|
2023-04-23 19:43:20 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
QThread m_hashAndSaveThread;
|
|
|
|
};
|
|
|
|
|
2023-04-18 21:10:06 -04:00
|
|
|
class Download : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2023-04-28 10:54:05 -04:00
|
|
|
Q_PROPERTY(bool hasNewerRelease READ hasNewerRelease NOTIFY hasNewerReleaseChanged)
|
|
|
|
Q_PROPERTY(ReleaseInfo releaseInfo READ releaseInfo NOTIFY releaseInfoChanged)
|
2024-06-24 18:49:23 -04:00
|
|
|
Q_PROPERTY(QString latestNews READ latestNews NOTIFY latestNewsChanged)
|
2023-04-18 21:10:06 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
static Download *globalInstance();
|
|
|
|
|
2024-07-30 13:20:52 -04:00
|
|
|
static std::strong_ordering compareAppVersions(const QString &a, const QString &b);
|
2023-04-28 10:54:05 -04:00
|
|
|
ReleaseInfo releaseInfo() const;
|
|
|
|
bool hasNewerRelease() const;
|
2024-06-24 18:49:23 -04:00
|
|
|
QString latestNews() const { return m_latestNews; }
|
2023-04-18 21:10:06 -04:00
|
|
|
Q_INVOKABLE void downloadModel(const QString &modelFile);
|
|
|
|
Q_INVOKABLE void cancelDownload(const QString &modelFile);
|
2023-05-14 20:12:15 -04:00
|
|
|
Q_INVOKABLE void installModel(const QString &modelFile, const QString &apiKey);
|
2024-07-25 10:02:52 -04:00
|
|
|
Q_INVOKABLE void installCompatibleModel(const QString &modelName, const QString &apiKey, const QString &baseUrl);
|
2023-05-16 09:32:01 -04:00
|
|
|
Q_INVOKABLE void removeModel(const QString &modelFile);
|
2024-04-25 13:16:52 -04:00
|
|
|
Q_INVOKABLE bool isFirstStart(bool writeVersion = false) const;
|
2023-04-18 21:10:06 -04:00
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
public Q_SLOTS:
|
2024-06-24 18:49:23 -04:00
|
|
|
void updateLatestNews();
|
2023-06-22 15:44:49 -04:00
|
|
|
void updateReleaseNotes();
|
|
|
|
|
2023-04-22 16:39:32 -04:00
|
|
|
private Q_SLOTS:
|
2023-04-24 17:52:19 -04:00
|
|
|
void handleSslErrors(QNetworkReply *reply, const QList<QSslError> &errors);
|
2023-04-28 10:54:05 -04:00
|
|
|
void handleReleaseJsonDownloadFinished();
|
2024-06-24 18:49:23 -04:00
|
|
|
void handleLatestNewsDownloadFinished();
|
2023-05-02 20:31:17 -04:00
|
|
|
void handleErrorOccurred(QNetworkReply::NetworkError code);
|
2023-04-18 21:10:06 -04:00
|
|
|
void handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
|
|
|
|
void handleModelDownloadFinished();
|
2023-06-22 15:44:49 -04:00
|
|
|
void handleHashAndSaveFinished(bool success, const QString &error,
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile, QNetworkReply *modelReply);
|
2023-04-23 19:43:20 -04:00
|
|
|
void handleReadyRead();
|
2023-04-18 21:10:06 -04:00
|
|
|
|
|
|
|
Q_SIGNALS:
|
2023-04-28 10:54:05 -04:00
|
|
|
void releaseInfoChanged();
|
|
|
|
void hasNewerReleaseChanged();
|
2024-03-05 11:31:31 -05:00
|
|
|
void requestHashAndSave(const QString &hash, QCryptographicHash::Algorithm a, const QString &saveFilePath,
|
2023-05-02 18:24:16 -04:00
|
|
|
QFile *tempFile, QNetworkReply *modelReply);
|
2024-06-24 18:49:23 -04:00
|
|
|
void latestNewsChanged();
|
2024-07-25 10:02:52 -04:00
|
|
|
void toastMessage(const QString &message);
|
2023-04-18 21:10:06 -04:00
|
|
|
|
|
|
|
private:
|
2023-04-28 10:54:05 -04:00
|
|
|
void parseReleaseJsonFile(const QByteArray &jsonData);
|
2023-05-02 18:24:16 -04:00
|
|
|
QString incompleteDownloadPath(const QString &modelFile);
|
2023-11-21 15:24:42 -05:00
|
|
|
bool hasRetry(const QString &filename) const;
|
|
|
|
bool shouldRetry(const QString &filename);
|
|
|
|
void clearRetry(const QString &filename);
|
2023-04-18 21:10:06 -04:00
|
|
|
|
2023-04-23 19:43:20 -04:00
|
|
|
HashAndSaveFile *m_hashAndSave;
|
2023-04-28 10:54:05 -04:00
|
|
|
QMap<QString, ReleaseInfo> m_releaseMap;
|
2024-06-24 18:49:23 -04:00
|
|
|
QString m_latestNews;
|
2023-04-18 21:10:06 -04:00
|
|
|
QNetworkAccessManager m_networkManager;
|
2023-05-02 18:24:16 -04:00
|
|
|
QMap<QNetworkReply*, QFile*> m_activeDownloads;
|
2023-11-21 15:24:42 -05:00
|
|
|
QHash<QString, int> m_activeRetries;
|
2023-05-02 18:24:16 -04:00
|
|
|
QDateTime m_startTime;
|
2023-04-18 21:10:06 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
explicit Download();
|
|
|
|
~Download() {}
|
|
|
|
friend class MyDownload;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // DOWNLOAD_H
|