src/download.cpp

changeset 428
53e577061dc8
parent 427
d308149fbc90
child 429
3488534b2b31
--- a/src/download.cpp	Fri Aug 02 22:02:40 2013 +0300
+++ b/src/download.cpp	Fri Aug 02 22:36:51 2013 +0300
@@ -63,11 +63,10 @@
 	const Source src = getSource();
 	
 	switch (src) {
-	case OfficialLibrary:
+/*	case OfficialLibrary:
+		return str (PartDownloader::k_OfficialURL) + getDest();
+*/
 	case PartsTracker:
-		if (src == OfficialLibrary)
-			return str (PartDownloader::k_OfficialURL) + getDest();
-		
 		return str (PartDownloader::k_UnofficialURL) + getDest();
 	
 	case CustomURL:
@@ -96,6 +95,16 @@
 		fname += ".dat";
 	}
 	
+	// If the part starts with s\ or s/, then use parts/s/. Same goes with
+	// 48\ and p/48/.
+	if (fname.left (2) == "s\\" || fname.left (2) == "s/") {
+		fname.remove (0, 2);
+		fname.prepend ("parts/s/");
+	} elif (fname.left (3) == "48\\" || fname.left (3) == "48/") {
+		fname.remove (0, 3);
+		fname.prepend ("p/48/");
+	}
+	
 	/* Try determine where to put this part. We have four directories:
 	 * parts/, parts/s/, p/, and p/48/. If we haven't already specified
 	 * either parts/ or p/, we need to add it automatically. Part files
@@ -157,14 +166,14 @@
 	ui->fname->setEnabled (false);
 	ui->source->setEnabled (false);
 	
-	PartDownloadRequest* req = new PartDownloadRequest (getURL(), getDest(), this);
+	PartDownloadRequest* req = new PartDownloadRequest (getURL(), getDest(), true, this);
 	req->setTableRow (row);
 	req->updateToTable();
 }
 
 // =============================================================================
 // -----------------------------------------------------------------------------
-PartDownloadRequest::PartDownloadRequest (str url, str dest, PartDownloadPrompt* parent) :
+PartDownloadRequest::PartDownloadRequest (str url, str dest, bool primary, PartDownloadPrompt* parent) :
 	QObject (parent),
 	m_prompt (parent),
 	m_url (url),
@@ -172,7 +181,8 @@
 	m_fpath (m_prompt->fullFilePath()),
 	m_nam (new QNetworkAccessManager),
 	m_firstUpdate (true),
-	m_state (Requesting)
+	m_state (Requesting),
+	m_primary (primary)
 {
 	// Make sure that we have a valid destination.
 	str dirpath = dirname (m_fpath);
@@ -188,6 +198,7 @@
 	connect (m_reply, SIGNAL (finished()), this, SLOT (downloadFinished()));
 	connect (m_reply, SIGNAL (readyRead()), this, SLOT (readyRead()));
 	connect (m_reply, SIGNAL (downloadProgress (qint64, qint64)), this, SLOT (downloadProgress (qint64, qint64)));
+	connect (m_reply, SIGNAL (error (QNetworkReply::NetworkError)), this, SLOT (downloadError()));
 }
 
 // =============================================================================
@@ -200,16 +211,31 @@
 // -----------------------------------------------------------------------------
 void PartDownloadRequest::updateToTable() {
 	QTableWidget* table = m_prompt->ui->progress;
-	QProgressBar* progressBar = qobject_cast<QProgressBar*> (table->cellWidget (tableRow(), ProgressColumn));
+	QProgressBar* prog;
+	
+	switch (m_state) {
+	case Requesting:
+	case Downloading:
+		prog = qobject_cast<QProgressBar*> (table->cellWidget (tableRow(), ProgressColumn));
 	
-	if (!progressBar) {
-		progressBar = new QProgressBar;
-		table->setCellWidget (tableRow(), ProgressColumn, progressBar);
+		if (!prog) {
+			prog = new QProgressBar;
+			table->setCellWidget (tableRow(), ProgressColumn, prog);
+		}
+		
+		prog->setRange (0, m_bytesTotal);
+		prog->setValue (m_bytesRead);
+		break;
+	
+	case Finished:
+	case Error:
+	case Aborted:
+		table->setCellWidget (tableRow(), ProgressColumn, new QLabel (
+			(m_state == Finished) ? "<b><span style=\"color: #080\">FINISHED</span></b>" :
+			                        "<b><span style=\"color: #800\">FAILED</span></b>"));
+		break;
 	}
 	
-	progressBar->setRange (0, m_bytesTotal);
-	progressBar->setValue (m_bytesRead);
-	
 	if (m_firstUpdate) {
 		QLabel* lb = new QLabel (fmt ("<b>%1</b><br>%2", m_dest, m_url), table);
 		table->setCellWidget (tableRow(), PartLabelColumn, lb);
@@ -245,6 +271,15 @@
 	f.close();
 }
 
+void PartDownloadRequest::downloadError() {
+	if (m_primary)
+		critical (m_reply->errorString());
+	
+	QFile::remove (m_fpath);
+	m_state = Error;
+	updateToTable();
+}
+
 // =============================================================================
 // -----------------------------------------------------------------------------
 DEFINE_ACTION (DownloadFrom, 0) {

mercurial