2023-04-19 01:10:06 +00:00
|
|
|
#ifndef DOWNLOAD_H
|
|
|
|
#define DOWNLOAD_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QVariant>
|
2023-04-23 23:43:20 +00:00
|
|
|
#include <QTemporaryFile>
|
|
|
|
#include <QThread>
|
2023-04-19 01:10:06 +00:00
|
|
|
|
|
|
|
struct ModelInfo {
|
|
|
|
Q_GADGET
|
|
|
|
Q_PROPERTY(QString filename MEMBER filename)
|
2023-04-20 13:32:51 +00:00
|
|
|
Q_PROPERTY(QString filesize MEMBER filesize)
|
2023-04-19 01:10:06 +00:00
|
|
|
Q_PROPERTY(QByteArray md5sum MEMBER md5sum)
|
2023-04-23 23:43:20 +00:00
|
|
|
Q_PROPERTY(bool calcHash MEMBER calcHash)
|
2023-04-19 01:10:06 +00:00
|
|
|
Q_PROPERTY(bool installed MEMBER installed)
|
|
|
|
Q_PROPERTY(bool isDefault MEMBER isDefault)
|
2023-04-27 18:52:40 +00:00
|
|
|
Q_PROPERTY(bool bestGPTJ MEMBER bestGPTJ)
|
|
|
|
Q_PROPERTY(bool bestLlama MEMBER bestLlama)
|
2023-05-09 23:15:18 +00:00
|
|
|
Q_PROPERTY(bool bestMPT MEMBER bestMPT)
|
2023-05-15 00:12:15 +00:00
|
|
|
Q_PROPERTY(bool isChatGPT MEMBER isChatGPT)
|
2023-04-27 18:52:40 +00:00
|
|
|
Q_PROPERTY(QString description MEMBER description)
|
2023-05-25 19:22:45 +00:00
|
|
|
Q_PROPERTY(QString requiresVersion MEMBER requiresVersion)
|
2023-06-04 23:02:43 +00:00
|
|
|
Q_PROPERTY(QString url MEMBER url)
|
2023-04-19 01:10:06 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
QString filename;
|
2023-04-20 13:32:51 +00:00
|
|
|
QString filesize;
|
2023-04-19 01:10:06 +00:00
|
|
|
QByteArray md5sum;
|
2023-04-23 23:43:20 +00:00
|
|
|
bool calcHash = false;
|
2023-04-19 01:10:06 +00:00
|
|
|
bool installed = false;
|
|
|
|
bool isDefault = false;
|
2023-04-27 18:52:40 +00:00
|
|
|
bool bestGPTJ = false;
|
|
|
|
bool bestLlama = false;
|
2023-05-09 16:09:49 +00:00
|
|
|
bool bestMPT = false;
|
2023-05-15 00:12:15 +00:00
|
|
|
bool isChatGPT = false;
|
2023-04-27 18:52:40 +00:00
|
|
|
QString description;
|
2023-05-25 19:22:45 +00:00
|
|
|
QString requiresVersion;
|
2023-06-04 23:02:43 +00:00
|
|
|
QString url;
|
2023-04-19 01:10:06 +00:00
|
|
|
};
|
|
|
|
Q_DECLARE_METATYPE(ModelInfo)
|
|
|
|
|
2023-04-28 14:54:05 +00: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 23:43:20 +00:00
|
|
|
class HashAndSaveFile : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
HashAndSaveFile();
|
|
|
|
|
|
|
|
public Q_SLOTS:
|
|
|
|
void hashAndSave(const QString &hash, const QString &saveFilePath,
|
2023-05-02 22:24:16 +00:00
|
|
|
QFile *tempFile, QNetworkReply *modelReply);
|
2023-04-23 23:43:20 +00:00
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
void hashAndSaveFinished(bool success,
|
2023-05-02 22:24:16 +00:00
|
|
|
QFile *tempFile, QNetworkReply *modelReply);
|
2023-04-23 23:43:20 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
QThread m_hashAndSaveThread;
|
|
|
|
};
|
|
|
|
|
2023-04-19 01:10:06 +00:00
|
|
|
class Download : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY(QList<ModelInfo> modelList READ modelList NOTIFY modelListChanged)
|
2023-04-28 14:54:05 +00:00
|
|
|
Q_PROPERTY(bool hasNewerRelease READ hasNewerRelease NOTIFY hasNewerReleaseChanged)
|
|
|
|
Q_PROPERTY(ReleaseInfo releaseInfo READ releaseInfo NOTIFY releaseInfoChanged)
|
2023-04-25 14:57:40 +00:00
|
|
|
Q_PROPERTY(QString downloadLocalModelsPath READ downloadLocalModelsPath
|
|
|
|
WRITE setDownloadLocalModelsPath
|
|
|
|
NOTIFY downloadLocalModelsPathChanged)
|
2023-04-19 01:10:06 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static Download *globalInstance();
|
|
|
|
|
|
|
|
QList<ModelInfo> modelList() const;
|
2023-04-28 14:54:05 +00:00
|
|
|
ReleaseInfo releaseInfo() const;
|
|
|
|
bool hasNewerRelease() const;
|
2023-04-19 01:10:06 +00:00
|
|
|
Q_INVOKABLE void updateModelList();
|
2023-04-28 14:54:05 +00:00
|
|
|
Q_INVOKABLE void updateReleaseNotes();
|
2023-04-19 01:10:06 +00:00
|
|
|
Q_INVOKABLE void downloadModel(const QString &modelFile);
|
|
|
|
Q_INVOKABLE void cancelDownload(const QString &modelFile);
|
2023-05-15 00:12:15 +00:00
|
|
|
Q_INVOKABLE void installModel(const QString &modelFile, const QString &apiKey);
|
2023-05-16 13:32:01 +00:00
|
|
|
Q_INVOKABLE void removeModel(const QString &modelFile);
|
2023-04-25 14:57:40 +00:00
|
|
|
Q_INVOKABLE QString defaultLocalModelsPath() const;
|
2023-04-23 15:28:17 +00:00
|
|
|
Q_INVOKABLE QString downloadLocalModelsPath() const;
|
2023-04-25 14:57:40 +00:00
|
|
|
Q_INVOKABLE void setDownloadLocalModelsPath(const QString &modelPath);
|
2023-04-28 14:54:05 +00:00
|
|
|
Q_INVOKABLE bool isFirstStart() const;
|
2023-04-19 01:10:06 +00:00
|
|
|
|
2023-04-22 20:39:32 +00:00
|
|
|
private Q_SLOTS:
|
2023-04-24 21:52:19 +00:00
|
|
|
void handleSslErrors(QNetworkReply *reply, const QList<QSslError> &errors);
|
2023-04-28 14:54:05 +00:00
|
|
|
void handleModelsJsonDownloadFinished();
|
|
|
|
void handleReleaseJsonDownloadFinished();
|
2023-05-03 00:31:17 +00:00
|
|
|
void handleErrorOccurred(QNetworkReply::NetworkError code);
|
2023-04-19 01:10:06 +00:00
|
|
|
void handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
|
|
|
|
void handleModelDownloadFinished();
|
2023-04-23 23:43:20 +00:00
|
|
|
void handleHashAndSaveFinished(bool success,
|
2023-05-02 22:24:16 +00:00
|
|
|
QFile *tempFile, QNetworkReply *modelReply);
|
2023-04-23 23:43:20 +00:00
|
|
|
void handleReadyRead();
|
2023-04-19 01:10:06 +00:00
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal, const QString &modelFile);
|
|
|
|
void downloadFinished(const QString &modelFile);
|
|
|
|
void modelListChanged();
|
2023-04-28 14:54:05 +00:00
|
|
|
void releaseInfoChanged();
|
|
|
|
void hasNewerReleaseChanged();
|
2023-04-25 14:57:40 +00:00
|
|
|
void downloadLocalModelsPathChanged();
|
2023-04-23 23:43:20 +00:00
|
|
|
void requestHashAndSave(const QString &hash, const QString &saveFilePath,
|
2023-05-02 22:24:16 +00:00
|
|
|
QFile *tempFile, QNetworkReply *modelReply);
|
2023-04-19 01:10:06 +00:00
|
|
|
|
|
|
|
private:
|
2023-04-28 14:54:05 +00:00
|
|
|
void parseModelsJsonFile(const QByteArray &jsonData);
|
|
|
|
void parseReleaseJsonFile(const QByteArray &jsonData);
|
2023-05-02 22:24:16 +00:00
|
|
|
QString incompleteDownloadPath(const QString &modelFile);
|
2023-04-19 01:10:06 +00:00
|
|
|
|
2023-04-23 23:43:20 +00:00
|
|
|
HashAndSaveFile *m_hashAndSave;
|
2023-04-19 01:10:06 +00:00
|
|
|
QMap<QString, ModelInfo> m_modelMap;
|
2023-04-28 14:54:05 +00:00
|
|
|
QMap<QString, ReleaseInfo> m_releaseMap;
|
2023-04-19 01:10:06 +00:00
|
|
|
QNetworkAccessManager m_networkManager;
|
2023-05-02 22:24:16 +00:00
|
|
|
QMap<QNetworkReply*, QFile*> m_activeDownloads;
|
2023-04-25 14:57:40 +00:00
|
|
|
QString m_downloadLocalModelsPath;
|
2023-05-02 22:24:16 +00:00
|
|
|
QDateTime m_startTime;
|
2023-04-19 01:10:06 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
explicit Download();
|
|
|
|
~Download() {}
|
|
|
|
friend class MyDownload;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // DOWNLOAD_H
|