handle errors

Fri, 02 Aug 2013 22:36:51 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Fri, 02 Aug 2013 22:36:51 +0300
changeset 428
53e577061dc8
parent 427
d308149fbc90
child 429
3488534b2b31

handle errors

src/download.cpp file | annotate | diff | comparison | revisions
src/download.h file | annotate | diff | comparison | revisions
src/ui/downloadfrom.ui file | annotate | diff | comparison | revisions
--- 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) {
--- a/src/download.h	Fri Aug 02 22:02:40 2013 +0300
+++ b/src/download.h	Fri Aug 02 22:36:51 2013 +0300
@@ -47,8 +47,8 @@
 	
 public:
 	enum Source {
-		OfficialLibrary,
-		PartsTracker,
+/*		OfficialLibrary,
+ */		PartsTracker,
 		CustomURL,
 	};
 	
@@ -84,10 +84,11 @@
 		Requesting,
 		Downloading,
 		Finished,
+		Error,
 		Aborted,
 	};
 	
-	explicit PartDownloadRequest (str url, str dest, PartDownloadPrompt* parent);
+	explicit PartDownloadRequest (str url, str dest, bool primary, PartDownloadPrompt* parent);
 	         PartDownloadRequest (const PartDownloadRequest&) = delete;
 	virtual ~PartDownloadRequest();
 	void updateToTable();
@@ -98,6 +99,7 @@
 	void downloadFinished();
 	void readyRead();
 	void downloadProgress (qint64 recv, qint64 total);
+	void downloadError();
 	
 private:
 	PartDownloadPrompt* m_prompt;
@@ -107,6 +109,7 @@
 	bool m_firstUpdate;
 	State m_state;
 	int64 m_bytesRead, m_bytesTotal;
+	bool m_primary;
 };
 
 #endif // LDFORGE_DOWNLOAD_H
\ No newline at end of file
--- a/src/ui/downloadfrom.ui	Fri Aug 02 22:02:40 2013 +0300
+++ b/src/ui/downloadfrom.ui	Fri Aug 02 22:36:51 2013 +0300
@@ -53,11 +53,6 @@
        </property>
        <item>
         <property name="text">
-         <string>Official parts</string>
-        </property>
-       </item>
-       <item>
-        <property name="text">
          <string>Parts tracker</string>
         </property>
        </item>

mercurial