Wed, 10 Jan 2018 23:07:01 +0200
simplify
src/partdownloadrequest.cpp | file | annotate | diff | comparison | revisions | |
src/partdownloadrequest.h | file | annotate | diff | comparison | revisions |
--- a/src/partdownloadrequest.cpp Wed Jan 10 22:54:29 2018 +0200 +++ b/src/partdownloadrequest.cpp Wed Jan 10 23:07:01 2018 +0200 @@ -42,7 +42,7 @@ m_filePointer(nullptr) { // Make sure that we have a valid destination. - QString dirpath = Dirname(filePath()); + QString dirpath = Dirname(m_filePath); QDir dir(dirpath); if (not dir.exists()) @@ -54,9 +54,9 @@ } m_networkReply = m_networkManager->get(QNetworkRequest(QUrl(url))); - connect(networkReply(), SIGNAL(finished()), this, SLOT(downloadFinished())); - connect(networkReply(), SIGNAL(readyRead()), this, SLOT(readFromNetworkReply())); - connect(networkReply(), SIGNAL(downloadProgress(qint64, qint64)), + connect(m_networkReply, SIGNAL(finished()), this, SLOT(downloadFinished())); + connect(m_networkReply, SIGNAL(readyRead()), this, SLOT(readFromm_networkReply)); + connect(m_networkReply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(updateDownloadProgress(qint64, qint64))); } @@ -67,81 +67,31 @@ return m_state == State::Failed; } -int PartDownloadRequest::tableRow() const -{ - return m_tableRow; -} - void PartDownloadRequest::setTableRow(int value) { m_tableRow = value; } -PartDownloader* PartDownloadRequest::prompt() const -{ - return m_prompt; -} - -QString PartDownloadRequest::url() const -{ - return m_url; -} - -QString PartDownloadRequest::destination() const -{ - return m_destination; -} - -QString PartDownloadRequest::filePath() const -{ - return m_filePath; -} - -bool PartDownloadRequest::isFirstUpdate() const -{ - return m_isFirstUpdate; -} - -qint64 PartDownloadRequest::numBytesRead() const -{ - return m_numBytesRead; -} - -qint64 PartDownloadRequest::numBytesTotal() const -{ - return m_numBytesTotal; -} - -bool PartDownloadRequest::isPrimary() const -{ - return m_isPrimary; -} - -QNetworkReply* PartDownloadRequest::networkReply() const -{ - return m_networkReply; -} - void PartDownloadRequest::updateToTable() { - QTableWidget* table = prompt()->progressTable(); + QTableWidget* table = m_prompt->progressTable(); switch(m_state) { case State::Requesting: case State::Downloading: { - QWidget* cellwidget = table->cellWidget(tableRow(), PartDownloader::ProgressColumn); + QWidget* cellwidget = table->cellWidget(m_tableRow, PartDownloader::ProgressColumn); QProgressBar* progressBar = qobject_cast<QProgressBar*>(cellwidget); if (not progressBar) { progressBar = new QProgressBar; - table->setCellWidget(tableRow(), PartDownloader::ProgressColumn, progressBar); + table->setCellWidget(m_tableRow, PartDownloader::ProgressColumn, progressBar); } - progressBar->setRange(0, numBytesTotal()); - progressBar->setValue(numBytesRead()); + progressBar->setRange(0, m_numBytesTotal); + progressBar->setValue(m_numBytesRead); } break; @@ -154,17 +104,17 @@ QLabel* lb = new QLabel(text); lb->setAlignment(Qt::AlignCenter); - table->setCellWidget(tableRow(), PartDownloader::ProgressColumn, lb); + table->setCellWidget(m_tableRow, PartDownloader::ProgressColumn, lb); } break; } - QLabel* label = qobject_cast<QLabel*>(table->cellWidget(tableRow(), PartDownloader::PartLabelColumn)); + QLabel* label = qobject_cast<QLabel*>(table->cellWidget(m_tableRow, PartDownloader::PartLabelColumn)); - if (isFirstUpdate()) + if (m_isFirstUpdate) { - label = new QLabel(format("<b>%1</b>", destination()), table); - table->setCellWidget(tableRow(), PartDownloader::PartLabelColumn, label); + label = new QLabel(format("<b>%1</b>", m_destination), table); + table->setCellWidget(m_tableRow, PartDownloader::PartLabelColumn, label); } // Make sure that the cell is big enough to contain the label @@ -176,12 +126,12 @@ void PartDownloadRequest::downloadFinished() { - if (networkReply()->error() != QNetworkReply::NoError) + if (m_networkReply->error() != QNetworkReply::NoError) { - if (isPrimary() and not prompt()->isAborted()) - Critical(networkReply()->errorString()); + if (m_isPrimary and not m_prompt->isAborted()) + Critical(m_networkReply->errorString()); - print("Unable to download %1: %2\n", destination(), networkReply()->errorString()); + print("Unable to download %1: %2\n", m_destination, m_networkReply->errorString()); m_state = State::Failed; } else if (m_state != State::Failed) @@ -189,7 +139,7 @@ m_state = State::Finished; } - m_numBytesRead = numBytesTotal(); + m_numBytesRead = m_numBytesTotal; updateToTable(); if (m_filePointer) @@ -199,17 +149,17 @@ m_filePointer = nullptr; if (m_state == State::Failed) - QFile::remove(filePath()); + QFile::remove(m_filePath); } if (m_state != State::Finished) { - prompt()->checkIfFinished(); + m_prompt->checkIfFinished(); return; } // Try to load this file now. - LDDocument* document = m_documents->openDocument(filePath(), false, not isPrimary()); + LDDocument* document = m_documents->openDocument(m_filePath, false, not m_isPrimary); if (document == nullptr) return; @@ -226,18 +176,18 @@ continue; QString dest = err->fileReferenced(); - prompt()->downloadFromPartsTracker(dest); + m_prompt->downloadFromPartsTracker(dest); } - prompt()->addFile(document); + m_prompt->addFile(document); - if (isPrimary()) + if (m_isPrimary) { - m_documents->addRecentFile(filePath()); - prompt()->setPrimaryFile(document); + m_documents->addRecentFile(m_filePath); + m_prompt->setPrimaryFile(document); } - prompt()->checkIfFinished(); + m_prompt->checkIfFinished(); } void PartDownloadRequest::updateDownloadProgress(int64 recv, int64 total) @@ -258,20 +208,20 @@ m_filePath.replace("\\", "/"); // We have already asked the user whether we can overwrite so we're good to go here. - m_filePointer = new QFile(filePath().toLocal8Bit()); + m_filePointer = new QFile(m_filePath.toLocal8Bit()); if (not m_filePointer->open(QIODevice::WriteOnly)) { - Critical(format(tr("Couldn't open %1 for writing: %2"), filePath(), strerror(errno))); + Critical(format(tr("Couldn't open %1 for writing: %2"), m_filePath, strerror(errno))); m_state = State::Failed; - networkReply()->abort(); + m_networkReply->abort(); updateToTable(); - prompt()->checkIfFinished(); + m_prompt->checkIfFinished(); return; } } - m_filePointer->write(networkReply()->readAll()); + m_filePointer->write(m_networkReply->readAll()); } bool PartDownloadRequest::isFinished() const @@ -281,5 +231,5 @@ void PartDownloadRequest::abort() { - networkReply()->abort(); -} \ No newline at end of file + m_networkReply->abort(); +}
--- a/src/partdownloadrequest.h Wed Jan 10 22:54:29 2018 +0200 +++ b/src/partdownloadrequest.h Wed Jan 10 23:07:01 2018 +0200 @@ -39,23 +39,13 @@ virtual ~PartDownloadRequest(); Q_SLOT void abort(); - QString destination() const; Q_SLOT void downloadFinished(); bool failed() const; - QString filePath() const; bool isFinished() const; - bool isFirstUpdate() const; - bool isPrimary() const; - QNetworkReply* networkReply() const; - qint64 numBytesRead() const; - qint64 numBytesTotal() const; - PartDownloader* prompt() const; Q_SLOT void readFromNetworkReply(); void setTableRow(int value); - int tableRow() const; Q_SLOT void updateDownloadProgress(qint64 recv, qint64 total); void updateToTable(); - QString url() const; private: int m_tableRow; @@ -71,4 +61,4 @@ qint64 m_numBytesRead; qint64 m_numBytesTotal; QFile* m_filePointer; -}; \ No newline at end of file +};