71 lines
1.6 KiB
C++
71 lines
1.6 KiB
C++
#ifndef DOWNLOADWORKER_H
|
|
#define DOWNLOADWORKER_H
|
|
|
|
#include <QObject>
|
|
#include <QList>
|
|
#include <QFile>
|
|
#include <QNetworkReply>
|
|
#include <QMutex>
|
|
|
|
class DownloadWorker : public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
explicit DownloadWorker(QObject *parent = nullptr);
|
|
void setIdentifier(int identifier);
|
|
void setParamter(const QString &url, QPair<qint64, qint64> range, QFile *flle);
|
|
|
|
public slots:
|
|
void doWork();
|
|
void dataReady();
|
|
void slotFinish();
|
|
|
|
signals:
|
|
void resultReady(int identifier, QByteArray data);
|
|
void testSignals();
|
|
|
|
|
|
private:
|
|
int identifier;
|
|
QString url;
|
|
// QPair<qint64, qint64> range;
|
|
qint64 startPos;
|
|
qint64 endPos;
|
|
qint64 receivedPos = 0;
|
|
QNetworkReply *reply;
|
|
QFile *file;
|
|
};
|
|
|
|
class DownloadController : public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
explicit DownloadController(QObject *parent = nullptr);
|
|
~DownloadController();
|
|
void setFilename(QString filename);
|
|
void setThreadNum(int threadNum);
|
|
void startDownload(const QString &url, qint64 fileSize);
|
|
void paruseDownload();
|
|
void stopDownload();
|
|
|
|
public slots:
|
|
void handleResults(int identifier, QByteArray data);
|
|
// void handleTest();
|
|
|
|
signals:
|
|
void operate(const QString &url, const QPair<qint64, qint64> &downloadRange);
|
|
void errorOccur(const QString& msg);
|
|
void receivedProcess(qint64 number);
|
|
|
|
private:
|
|
int threadNum;
|
|
QString filename;
|
|
QVector<QPair<qint64, qint64>> ranges;
|
|
QFile *file;
|
|
qint64 bytesReceived;
|
|
QVector<qint64> writePosList;
|
|
QMutex mutex;
|
|
};
|
|
|
|
#endif // FILEDOWNLOADWORKER_H
|