Skip to content

Commit

Permalink
refactor: DownloadHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Super committed Oct 3, 2024
1 parent 38c5439 commit 1aef272
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
21 changes: 13 additions & 8 deletions src/Logic/DownloadHistory.cpp → src/logic/DownloadHistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
#include "DownloadHistory.h"

#include <QFile>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>

#include "Logger.hpp"
#include "Path.h"

DownloadHistory::DownloadHistory()
struct DownloadHistory::Impl {
QSqlDatabase db_;
};

DownloadHistory::DownloadHistory() : impl(std::make_unique<Impl>())
{
if (!QFile::exists(utils::Path::instance().sqlFilePath())) {
createDB();
Expand All @@ -23,13 +28,13 @@ DownloadHistory::DownloadHistory()
spdlog::error("Could not connect to database. Error:{}", db.lastError().text());
return;
}
db_ = QSqlDatabase::database();
impl->db_ = QSqlDatabase::database();
}
}

DownloadHistory::~DownloadHistory()
{
db_.close();
impl->db_.close();
}

bool DownloadHistory::createDB()
Expand All @@ -40,7 +45,7 @@ bool DownloadHistory::createDB()
spdlog::error("Could not connect to database. Error:{}", db.lastError().text());
return false;
}
db_ = QSqlDatabase::database();
impl->db_ = QSqlDatabase::database();

// 创建下载任务表
QString createDownloadsTable = R"(
Expand Down Expand Up @@ -86,7 +91,7 @@ bool DownloadHistory::createDB()
}

bool DownloadHistory::insertDownloadTask(const QString& url, const QString& fileName, const QString& filePath,
int64_t totalSize, int threadCount)
qint64 totalSize, int threadCount)
{
QSqlQuery query;
query.prepare(R"(
Expand All @@ -109,8 +114,8 @@ bool DownloadHistory::insertDownloadTask(const QString& url, const QString& file
return true;
}

bool DownloadHistory::insertDownloadSegment(const QString& url, int64_t rangeStart, int64_t rangeEnd,
int64_t downloadedSize)
bool DownloadHistory::insertDownloadSegment(const QString& url, qint64 rangeStart, qint64 rangeEnd,
qint64 downloadedSize)
{
QSqlQuery query;
query.prepare(R"(
Expand Down Expand Up @@ -218,7 +223,7 @@ QList<QPair<int, int>> DownloadHistory::getDownloadSegments(const QString& url)
return segments;
}

bool DownloadHistory::updateSegmentProgress(const QString& url, int64_t rangeStart, int64_t downloadedSize,
bool DownloadHistory::updateSegmentProgress(const QString& url, qint64 rangeStart, qint64 downloadedSize,
bool isCompleted)
{
QSqlQuery query;
Expand Down
18 changes: 11 additions & 7 deletions src/Logic/DownloadHistory.h → src/logic/DownloadHistory.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
//

#pragma once
#include <QSqlDatabase>
#include <QString>

#include "DownloadItem.h"

#include <QList>
#include <QString>
#include <QVector>

class DownloadHistory {
public:
/**
Expand All @@ -18,7 +20,7 @@ class DownloadHistory {
* @param threadCount
* @return
*/
bool insertDownloadTask(const QString& url, const QString& fileName, const QString& filePath, int64_t totalSize,
bool insertDownloadTask(const QString& url, const QString& fileName, const QString& filePath, qint64 totalSize,
int threadCount);
/**
* 插入下载分段
Expand All @@ -28,7 +30,7 @@ class DownloadHistory {
* @param downloadedSize
* @return
*/
bool insertDownloadSegment(const QString& url, int64_t rangeStart, int64_t rangeEnd, int64_t downloadedSize);
bool insertDownloadSegment(const QString& url, qint64 rangeStart, qint64 rangeEnd, qint64 downloadedSize);
/**
* 查询未完成的下载任务
* @return
Expand All @@ -38,7 +40,7 @@ class DownloadHistory {
* 查询未完成的下载任务
* @return
*/
QVector<DownloadItem> getCompleteDownloadTasks();
QVector<DownloadItem> getCompleteDownloadTasks();
/**
* 查询某个下载任务的分段信息
* @param url
Expand All @@ -53,7 +55,7 @@ class DownloadHistory {
* @param isCompleted
* @return
*/
bool updateSegmentProgress(const QString& url, int64_t rangeStart, int64_t downloadedSize, bool isCompleted);
bool updateSegmentProgress(const QString& url, qint64 rangeStart, qint64 downloadedSize, bool isCompleted);
/**
* 删除下载任务
* @param url
Expand All @@ -74,6 +76,8 @@ class DownloadHistory {
private:
DownloadHistory();
~DownloadHistory();

private:
QSqlDatabase db_;
struct Impl;
std::unique_ptr<Impl> impl;
};

0 comments on commit 1aef272

Please sign in to comment.