mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-23 03:58:56 -04:00
RowNode stops deriving from DownloadState
Now DownloadState is an optional element of the state of RowNode and is set in it only in case of an active (even if paused) download associated with the that entry.
This commit is contained in:
parent
ad09ee10e1
commit
9ce484a664
@ -107,8 +107,8 @@ void ContentManager::onCustomContextMenu(const QPoint &point)
|
|||||||
QAction menuCancelBook(gt("cancel-download"), this);
|
QAction menuCancelBook(gt("cancel-download"), this);
|
||||||
QAction menuOpenFolder(gt("open-folder"), this);
|
QAction menuOpenFolder(gt("open-folder"), this);
|
||||||
|
|
||||||
if (bookNode->isDownloading()) {
|
if (DownloadState* download = bookNode->getDownloadState()) {
|
||||||
if (bookNode->getDownloadInfo().paused) {
|
if (download->getDownloadInfo().paused) {
|
||||||
contextMenu.addAction(&menuResumeBook);
|
contextMenu.addAction(&menuResumeBook);
|
||||||
} else {
|
} else {
|
||||||
contextMenu.addAction(&menuPauseBook);
|
contextMenu.addAction(&menuPauseBook);
|
||||||
|
@ -177,8 +177,8 @@ void ContentManagerDelegate::paint(QPainter *painter, const QStyleOptionViewItem
|
|||||||
}
|
}
|
||||||
QStyleOptionViewItem eOpt = option;
|
QStyleOptionViewItem eOpt = option;
|
||||||
if (index.column() == 5) {
|
if (index.column() == 5) {
|
||||||
if (node->isDownloading()) {
|
if (DownloadState* downloadState = node->getDownloadState()) {
|
||||||
auto downloadInfo = node->getDownloadInfo();
|
auto downloadInfo = downloadState->getDownloadInfo();
|
||||||
showDownloadProgress(painter, r, downloadInfo);
|
showDownloadProgress(painter, r, downloadInfo);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -244,8 +244,8 @@ void ContentManagerDelegate::handleLastColumnClicked(const QModelIndex& index, Q
|
|||||||
int x = r.left();
|
int x = r.left();
|
||||||
int w = r.width();
|
int w = r.width();
|
||||||
|
|
||||||
if (node->isDownloading()) {
|
if (DownloadState* downloadState = node->getDownloadState()) {
|
||||||
if (node->getDownloadInfo().paused) {
|
if (downloadState->getDownloadInfo().paused) {
|
||||||
if (clickX < (x + w/2)) {
|
if (clickX < (x + w/2)) {
|
||||||
KiwixApp::instance()->getContentManager()->cancelBook(id, index);
|
KiwixApp::instance()->getContentManager()->cancelBook(id, index);
|
||||||
} else {
|
} else {
|
||||||
|
@ -239,10 +239,10 @@ std::shared_ptr<RowNode> getSharedPointer(RowNode* ptr)
|
|||||||
void ContentManagerModel::startDownload(QModelIndex index)
|
void ContentManagerModel::startDownload(QModelIndex index)
|
||||||
{
|
{
|
||||||
auto node = getSharedPointer(static_cast<RowNode*>(index.internalPointer()));
|
auto node = getSharedPointer(static_cast<RowNode*>(index.internalPointer()));
|
||||||
node->setIsDownloading(true); // this starts the internal timer
|
node->setDownloadState(new DownloadState);
|
||||||
QTimer *timer = node->getDownloadUpdateTimer();
|
QTimer *timer = node->getDownloadState()->getDownloadUpdateTimer();
|
||||||
connect(timer, &QTimer::timeout, this, [=]() {
|
connect(timer, &QTimer::timeout, this, [=]() {
|
||||||
node->updateDownloadStatus(node->getBookId());
|
node->getDownloadState()->updateDownloadStatus(node->getBookId());
|
||||||
emit dataChanged(index, index);
|
emit dataChanged(index, index);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -250,21 +250,21 @@ void ContentManagerModel::startDownload(QModelIndex index)
|
|||||||
void ContentManagerModel::pauseDownload(QModelIndex index)
|
void ContentManagerModel::pauseDownload(QModelIndex index)
|
||||||
{
|
{
|
||||||
auto node = static_cast<RowNode*>(index.internalPointer());
|
auto node = static_cast<RowNode*>(index.internalPointer());
|
||||||
node->pauseDownload();
|
node->getDownloadState()->pauseDownload();
|
||||||
emit dataChanged(index, index);
|
emit dataChanged(index, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ContentManagerModel::resumeDownload(QModelIndex index)
|
void ContentManagerModel::resumeDownload(QModelIndex index)
|
||||||
{
|
{
|
||||||
auto node = static_cast<RowNode*>(index.internalPointer());
|
auto node = static_cast<RowNode*>(index.internalPointer());
|
||||||
node->resumeDownload();
|
node->getDownloadState()->resumeDownload();
|
||||||
emit dataChanged(index, index);
|
emit dataChanged(index, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ContentManagerModel::cancelDownload(QModelIndex index)
|
void ContentManagerModel::cancelDownload(QModelIndex index)
|
||||||
{
|
{
|
||||||
auto node = static_cast<RowNode*>(index.internalPointer());
|
auto node = static_cast<RowNode*>(index.internalPointer());
|
||||||
node->setIsDownloading(false); // this stops & deletes the timer
|
node->setDownloadState(nullptr);
|
||||||
emit dataChanged(index, index);
|
emit dataChanged(index, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,24 +10,8 @@
|
|||||||
DownloadState::DownloadState()
|
DownloadState::DownloadState()
|
||||||
: m_downloadInfo({0, "", "", false})
|
: m_downloadInfo({0, "", "", false})
|
||||||
{
|
{
|
||||||
}
|
m_downloadUpdateTimer.reset(new QTimer);
|
||||||
|
m_downloadUpdateTimer->start(1000);
|
||||||
void DownloadState::setIsDownloading(bool val)
|
|
||||||
{
|
|
||||||
assert(val != isDownloading());
|
|
||||||
if ( val ) {
|
|
||||||
m_downloadUpdateTimer.reset(new QTimer);
|
|
||||||
m_downloadUpdateTimer->start(1000);
|
|
||||||
} else {
|
|
||||||
m_downloadUpdateTimer->stop();
|
|
||||||
|
|
||||||
// Deleting the timer object immediately instead of via
|
|
||||||
// QObject::deleteLater() seems to be safe since it is not a recipient
|
|
||||||
// of any events that may be in the process of being delivered to it
|
|
||||||
// from another thread.
|
|
||||||
m_downloadUpdateTimer.reset();
|
|
||||||
m_downloadInfo = {0, "", "", false};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
@ -59,7 +43,14 @@ void DownloadState::updateDownloadStatus(QString id)
|
|||||||
auto downloadSpeed = convertToUnits(downloadInfos["downloadSpeed"].toString()) + "/s";
|
auto downloadSpeed = convertToUnits(downloadInfos["downloadSpeed"].toString()) + "/s";
|
||||||
m_downloadInfo = {percent, completedLength, downloadSpeed, false};
|
m_downloadInfo = {percent, completedLength, downloadSpeed, false};
|
||||||
if (!downloadInfos["status"].isValid()) {
|
if (!downloadInfos["status"].isValid()) {
|
||||||
setIsDownloading(false); // this stops & deletes the timer
|
m_downloadUpdateTimer->stop();
|
||||||
|
|
||||||
|
// Deleting the timer object immediately instead of via
|
||||||
|
// QObject::deleteLater() seems to be safe since it is not a recipient
|
||||||
|
// of any events that may be in the process of being delivered to it
|
||||||
|
// from another thread.
|
||||||
|
m_downloadUpdateTimer.reset();
|
||||||
|
m_downloadInfo = {0, "", "", false};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,3 +141,8 @@ bool RowNode::isChild(Node *candidate)
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RowNode::setDownloadState(DownloadState* ds)
|
||||||
|
{
|
||||||
|
m_downloadState.reset(ds);
|
||||||
|
}
|
||||||
|
@ -22,7 +22,6 @@ public:
|
|||||||
bool isDownloading() const { return m_downloadUpdateTimer.get() != nullptr; }
|
bool isDownloading() const { return m_downloadUpdateTimer.get() != nullptr; }
|
||||||
DownloadInfo getDownloadInfo() const { return m_downloadInfo; }
|
DownloadInfo getDownloadInfo() const { return m_downloadInfo; }
|
||||||
QTimer* getDownloadUpdateTimer() const { return m_downloadUpdateTimer.get(); }
|
QTimer* getDownloadUpdateTimer() const { return m_downloadUpdateTimer.get(); }
|
||||||
void setIsDownloading(bool val);
|
|
||||||
void pauseDownload();
|
void pauseDownload();
|
||||||
void resumeDownload();
|
void resumeDownload();
|
||||||
void updateDownloadStatus(QString id);
|
void updateDownloadStatus(QString id);
|
||||||
@ -34,7 +33,7 @@ protected:
|
|||||||
DownloadInfo m_downloadInfo;
|
DownloadInfo m_downloadInfo;
|
||||||
};
|
};
|
||||||
|
|
||||||
class RowNode : public Node, public DownloadState
|
class RowNode : public Node
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit RowNode(QList<QVariant> itemData, QString bookId, std::weak_ptr<RowNode> parentItem);
|
explicit RowNode(QList<QVariant> itemData, QString bookId, std::weak_ptr<RowNode> parentItem);
|
||||||
@ -50,11 +49,16 @@ public:
|
|||||||
void setIconData(QByteArray iconData) { m_itemData[0] = iconData; }
|
void setIconData(QByteArray iconData) { m_itemData[0] = iconData; }
|
||||||
bool isChild(Node* candidate);
|
bool isChild(Node* candidate);
|
||||||
|
|
||||||
|
|
||||||
|
void setDownloadState(DownloadState* ds);
|
||||||
|
DownloadState* getDownloadState() { return m_downloadState.get(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QList<QVariant> m_itemData;
|
QList<QVariant> m_itemData;
|
||||||
QList<std::shared_ptr<Node>> m_childItems;
|
QList<std::shared_ptr<Node>> m_childItems;
|
||||||
std::weak_ptr<RowNode> m_parentItem;
|
std::weak_ptr<RowNode> m_parentItem;
|
||||||
QString m_bookId;
|
QString m_bookId;
|
||||||
|
std::unique_ptr<DownloadState> m_downloadState;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user