src/partdownloadrequest.cpp

changeset 1217
314e12e23c3a
parent 998
5be0ce31ce60
child 1222
34def2630300
--- a/src/partdownloadrequest.cpp	Thu Jan 04 19:40:52 2018 +0200
+++ b/src/partdownloadrequest.cpp	Thu Jan 04 19:44:26 2018 +0200
@@ -28,36 +28,36 @@
 #include "mainwindow.h"
 #include "documentmanager.h"
 
-PartDownloadRequest::PartDownloadRequest (QString url, QString dest, bool primary, PartDownloader* parent) :
-	QObject (parent),
-	HierarchyElement (parent),
-	m_state (State::Requesting),
-	m_prompt (parent),
-	m_url (url),
-	m_destination (dest),
-	m_filePath (parent->downloadPath() + DIRSLASH + dest),
-	m_networkManager (new QNetworkAccessManager),
-	m_isFirstUpdate (true),
-	m_isPrimary (primary),
-	m_filePointer (nullptr)
+PartDownloadRequest::PartDownloadRequest(QString url, QString dest, bool primary, PartDownloader* parent) :
+	QObject(parent),
+	HierarchyElement(parent),
+	m_state(State::Requesting),
+	m_prompt(parent),
+	m_url(url),
+	m_destination(dest),
+	m_filePath(parent->downloadPath() + DIRSLASH + dest),
+	m_networkManager(new QNetworkAccessManager),
+	m_isFirstUpdate(true),
+	m_isPrimary(primary),
+	m_filePointer(nullptr)
 {
 	// Make sure that we have a valid destination.
-	QString dirpath = Dirname (filePath());
-	QDir dir (dirpath);
+	QString dirpath = Dirname(filePath());
+	QDir dir(dirpath);
 
 	if (not dir.exists())
 	{
-		print ("Creating %1...\n", dirpath);
+		print("Creating %1...\n", dirpath);
 
-		if (not dir.mkpath (dirpath))
-			Critical (format (tr ("Couldn't create the directory %1!"), dirpath));
+		if (not dir.mkpath(dirpath))
+			Critical(format(tr("Couldn't create the directory %1!"), dirpath));
 	}
 
-	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)),
-		this, SLOT (updateDownloadProgress (qint64, qint64)));
+	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)),
+		this, SLOT(updateDownloadProgress(qint64, qint64)));
 }
 
 PartDownloadRequest::~PartDownloadRequest() {}
@@ -72,7 +72,7 @@
 	return m_tableRow;
 }
 
-void PartDownloadRequest::setTableRow (int value)
+void PartDownloadRequest::setTableRow(int value)
 {
 	m_tableRow = value;
 }
@@ -126,22 +126,22 @@
 {
 	QTableWidget* table = prompt()->progressTable();
 
-	switch (m_state)
+	switch(m_state)
 	{
 	case State::Requesting:
 	case State::Downloading:
 		{
-			QWidget* cellwidget = table->cellWidget (tableRow(), PartDownloader::ProgressColumn);
-			QProgressBar* progressBar = qobject_cast<QProgressBar*> (cellwidget);
+			QWidget* cellwidget = table->cellWidget(tableRow(), PartDownloader::ProgressColumn);
+			QProgressBar* progressBar = qobject_cast<QProgressBar*>(cellwidget);
 
 			if (not progressBar)
 			{
 				progressBar = new QProgressBar;
-				table->setCellWidget (tableRow(), PartDownloader::ProgressColumn, progressBar);
+				table->setCellWidget(tableRow(), PartDownloader::ProgressColumn, progressBar);
 			}
 
-			progressBar->setRange (0, numBytesTotal());
-			progressBar->setValue (numBytesRead());
+			progressBar->setRange(0, numBytesTotal());
+			progressBar->setValue(numBytesRead());
 		}
 		break;
 
@@ -152,24 +152,24 @@
 				? "<b><span style=\"color: #080\">FINISHED</span></b>"
 				: "<b><span style=\"color: #800\">FAILED</span></b>";
 
-			QLabel* lb = new QLabel (text);
-			lb->setAlignment (Qt::AlignCenter);
-			table->setCellWidget (tableRow(), PartDownloader::ProgressColumn, lb);
+			QLabel* lb = new QLabel(text);
+			lb->setAlignment(Qt::AlignCenter);
+			table->setCellWidget(tableRow(), PartDownloader::ProgressColumn, lb);
 		}
 		break;
 	}
 
-	QLabel* label = qobject_cast<QLabel*> (table->cellWidget (tableRow(), PartDownloader::PartLabelColumn));
+	QLabel* label = qobject_cast<QLabel*>(table->cellWidget(tableRow(), PartDownloader::PartLabelColumn));
 
 	if (isFirstUpdate())
 	{
-		label = new QLabel (format ("<b>%1</b>", destination()), table);
-		table->setCellWidget (tableRow(), PartDownloader::PartLabelColumn, label);
+		label = new QLabel(format("<b>%1</b>", destination()), table);
+		table->setCellWidget(tableRow(), PartDownloader::PartLabelColumn, label);
 	}
 
 	// Make sure that the cell is big enough to contain the label
-	if (table->columnWidth (PartDownloader::PartLabelColumn) < label->width())
-		table->setColumnWidth (PartDownloader::PartLabelColumn, label->width());
+	if (table->columnWidth(PartDownloader::PartLabelColumn) < label->width())
+		table->setColumnWidth(PartDownloader::PartLabelColumn, label->width());
 
 	m_isFirstUpdate = false;
 }
@@ -179,9 +179,9 @@
 	if (networkReply()->error() != QNetworkReply::NoError)
 	{
 		if (isPrimary() and not prompt()->isAborted())
-			Critical (networkReply()->errorString());
+			Critical(networkReply()->errorString());
 
-		print ("Unable to download %1: %2\n", destination(), networkReply()->errorString());
+		print("Unable to download %1: %2\n", destination(), networkReply()->errorString());
 		m_state = State::Failed;
 	}
 	else if (m_state != State::Failed)
@@ -199,7 +199,7 @@
 		m_filePointer = nullptr;
 
 		if (m_state == State::Failed)
-			QFile::remove (filePath());
+			QFile::remove(filePath());
 	}
 
 	if (m_state != State::Finished)
@@ -209,7 +209,7 @@
 	}
 
 	// Try to load this file now.
-	LDDocument* document = m_documents->openDocument (filePath(), false, not isPrimary());
+	LDDocument* document = m_documents->openDocument(filePath(), false, not isPrimary());
 
 	if (document == nullptr)
 		return;
@@ -220,27 +220,27 @@
 	// it resolves dependencies.
 	for (LDObject* obj : document->objects())
 	{
-		LDError* err = dynamic_cast<LDError*> (obj);
+		LDError* err = dynamic_cast<LDError*>(obj);
 
 		if (err == nullptr or err->fileReferenced().isEmpty())
 			continue;
 
 		QString dest = err->fileReferenced();
-		prompt()->downloadFromPartsTracker (dest);
+		prompt()->downloadFromPartsTracker(dest);
 	}
 
-	prompt()->addFile (document);
+	prompt()->addFile(document);
 
 	if (isPrimary())
 	{
-		m_documents->addRecentFile (filePath());
-		prompt()->setPrimaryFile (document);
+		m_documents->addRecentFile(filePath());
+		prompt()->setPrimaryFile(document);
 	}
 
 	prompt()->checkIfFinished();
 }
 
-void PartDownloadRequest::updateDownloadProgress (int64 recv, int64 total)
+void PartDownloadRequest::updateDownloadProgress(int64 recv, int64 total)
 {
 	m_numBytesRead = recv;
 	m_numBytesTotal = total;
@@ -255,14 +255,14 @@
 
 	if (m_filePointer == nullptr)
 	{
-		m_filePath.replace ("\\", "/");
+		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(filePath().toLocal8Bit());
 
-		if (not m_filePointer->open (QIODevice::WriteOnly))
+		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"), filePath(), strerror(errno)));
 			m_state = State::Failed;
 			networkReply()->abort();
 			updateToTable();
@@ -271,12 +271,12 @@
 		}
 	}
 
-	m_filePointer->write (networkReply()->readAll());
+	m_filePointer->write(networkReply()->readAll());
 }
 
 bool PartDownloadRequest::isFinished() const
 {
-	return isOneOf (m_state, State::Finished, State::Failed);
+	return isOneOf(m_state, State::Finished, State::Failed);
 }
 
 void PartDownloadRequest::abort()

mercurial