gpt4all/download.h

93 lines
2.5 KiB
C
Raw Normal View History

2023-04-18 21:10:06 -04:00
#ifndef DOWNLOAD_H
#define DOWNLOAD_H
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QFile>
#include <QVariant>
#include <QTemporaryFile>
#include <QThread>
2023-04-18 21:10:06 -04:00
struct ModelInfo {
Q_GADGET
Q_PROPERTY(QString filename MEMBER filename)
Q_PROPERTY(QString filesize MEMBER filesize)
2023-04-18 21:10:06 -04:00
Q_PROPERTY(QByteArray md5sum MEMBER md5sum)
Q_PROPERTY(bool calcHash MEMBER calcHash)
2023-04-18 21:10:06 -04:00
Q_PROPERTY(bool installed MEMBER installed)
Q_PROPERTY(bool isDefault MEMBER isDefault)
public:
QString filename;
QString filesize;
2023-04-18 21:10:06 -04:00
QByteArray md5sum;
bool calcHash = false;
2023-04-18 21:10:06 -04:00
bool installed = false;
bool isDefault = false;
};
Q_DECLARE_METATYPE(ModelInfo)
class HashAndSaveFile : public QObject
{
Q_OBJECT
public:
HashAndSaveFile();
public Q_SLOTS:
void hashAndSave(const QString &hash, const QString &saveFilePath,
QTemporaryFile *tempFile, QNetworkReply *modelReply);
Q_SIGNALS:
void hashAndSaveFinished(bool success,
QTemporaryFile *tempFile, QNetworkReply *modelReply);
private:
QThread m_hashAndSaveThread;
};
2023-04-18 21:10:06 -04:00
class Download : public QObject
{
Q_OBJECT
Q_PROPERTY(QList<ModelInfo> modelList READ modelList NOTIFY modelListChanged)
public:
static Download *globalInstance();
QList<ModelInfo> modelList() const;
Q_INVOKABLE void updateModelList();
Q_INVOKABLE void downloadModel(const QString &modelFile);
Q_INVOKABLE void cancelDownload(const QString &modelFile);
Q_INVOKABLE QString downloadLocalModelsPath() const;
2023-04-18 21:10:06 -04:00
2023-04-22 16:39:32 -04:00
private Q_SLOTS:
2023-04-18 21:10:06 -04:00
void handleJsonDownloadFinished();
void handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
void handleModelDownloadFinished();
void handleHashAndSaveFinished(bool success,
QTemporaryFile *tempFile, QNetworkReply *modelReply);
void handleReadyRead();
2023-04-18 21:10:06 -04:00
Q_SIGNALS:
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal, const QString &modelFile);
void downloadFinished(const QString &modelFile);
void modelListChanged();
void requestHashAndSave(const QString &hash, const QString &saveFilePath,
QTemporaryFile *tempFile, QNetworkReply *modelReply);
2023-04-18 21:10:06 -04:00
private:
void parseJsonFile(const QByteArray &jsonData);
HashAndSaveFile *m_hashAndSave;
2023-04-18 21:10:06 -04:00
QMap<QString, ModelInfo> m_modelMap;
QNetworkAccessManager m_networkManager;
QMap<QNetworkReply*, QTemporaryFile*> m_activeDownloads;
2023-04-18 21:10:06 -04:00
private:
explicit Download();
~Download() {}
friend class MyDownload;
};
#endif // DOWNLOAD_H