Thiago, please take a look to see if my changes makes sense, never
worked with QNetworkReply before.

this should work, and the code seems correct - but since I never
worked with network related stuff it's always good to have a pair of
eyes looking over us.

Tomaz
From 21167d91e05e53bf1d1e1a4aace3ce2f46a3f4c0 Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Wed, 16 Jul 2014 17:20:21 -0300
Subject: [PATCH] Rely on QNetworkReply finished() signal instead of
 AccessManager one

The access manager is only one, while we can make requests from
different parts of the application, so relying on the manager
finished() signal to see if something was done or not was a
not very good move.

The QNetworkReply is created when a get() is invocked on the
AccessManager and that's unique. connect it's finished()
signal instead.

bonus: code cleanup.

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-ui/subsurfacewebservices.cpp | 3 ++-
 qt-ui/subsurfacewebservices.h   | 2 +-
 qt-ui/updatemanager.cpp         | 8 +++-----
 qt-ui/updatemanager.h           | 4 +---
 qt-ui/usersurvey.cpp            | 8 +++-----
 qt-ui/usersurvey.h              | 5 +----
 6 files changed, 11 insertions(+), 19 deletions(-)

diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp
index 2397258..daeb538 100644
--- a/qt-ui/subsurfacewebservices.cpp
+++ b/qt-ui/subsurfacewebservices.cpp
@@ -917,10 +917,11 @@ UserSurveyServices::UserSurveyServices(QWidget *parent, Qt::WindowFlags f) : Web
 
 }
 
-void UserSurveyServices::sendSurvey(QString values)
+QNetworkReply* UserSurveyServices::sendSurvey(QString values)
 {
 	QNetworkRequest request;
 	request.setUrl(QString("http://subsurface.hohndel.org/survey?%1";).arg(values));
 	request.setRawHeader("Accept", "text/xml");
 	reply = manager()->get(request);
+	return reply;
 }
diff --git a/qt-ui/subsurfacewebservices.h b/qt-ui/subsurfacewebservices.h
index f2138ac..97f35f2 100644
--- a/qt-ui/subsurfacewebservices.h
+++ b/qt-ui/subsurfacewebservices.h
@@ -100,7 +100,7 @@ private:
 class UserSurveyServices : public WebServices {
 	Q_OBJECT
 public:
-	void sendSurvey(QString values);
+	QNetworkReply* sendSurvey(QString values);
 	explicit UserSurveyServices(QWidget *parent = 0, Qt::WindowFlags f = 0);
 private
 slots:
diff --git a/qt-ui/updatemanager.cpp b/qt-ui/updatemanager.cpp
index 51292fc..b790a61 100644
--- a/qt-ui/updatemanager.cpp
+++ b/qt-ui/updatemanager.cpp
@@ -6,8 +6,6 @@
 
 UpdateManager::UpdateManager(QObject *parent) : QObject(parent)
 {
-	manager = SubsurfaceWebServices::manager();
-	connect(manager, SIGNAL(finished(QNetworkReply *)), SLOT(requestReceived(QNetworkReply *)));
 }
 
 void UpdateManager::checkForUpdates()
@@ -26,16 +24,16 @@ void UpdateManager::checkForUpdates()
 
 	QString version = VERSION_STRING;
 	QString url = QString("http://subsurface.hohndel.org/updatecheck.html?os=%1&ver=%2";).arg(os, version);
-	manager->get(QNetworkRequest(QUrl(url)));
+	connect(SubsurfaceWebServices::manager()->get(QNetworkRequest(QUrl(url))), SIGNAL(finished()), this, SLOT(requestReceived()));
 }
 
-void UpdateManager::requestReceived(QNetworkReply *reply)
+void UpdateManager::requestReceived()
 {
 	QMessageBox msgbox;
 	QString msgTitle = tr("Check for updates.");
 	QString msgText = "<h3>" + tr("Subsurface was unable to check for updates.") + "</h3>";
 
-
+	QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
 	if (reply->error() != QNetworkReply::NoError) {
 		//Network Error
 		msgText = msgText + "<br/><b>" + tr("The following error occurred:") + "</b><br/>" + reply->errorString()
diff --git a/qt-ui/updatemanager.h b/qt-ui/updatemanager.h
index 561c53d..98b8402 100644
--- a/qt-ui/updatemanager.h
+++ b/qt-ui/updatemanager.h
@@ -12,11 +12,9 @@ public:
 	explicit UpdateManager(QObject *parent = 0);
 	void checkForUpdates();
 
-private:
-	QNetworkAccessManager *manager;
 public
 slots:
-	void requestReceived(QNetworkReply *reply);
+	void requestReceived();
 };
 
 #endif // UPDATEMANAGER_H
diff --git a/qt-ui/usersurvey.cpp b/qt-ui/usersurvey.cpp
index 9970640..24e2070 100644
--- a/qt-ui/usersurvey.cpp
+++ b/qt-ui/usersurvey.cpp
@@ -40,8 +40,6 @@ UserSurvey::UserSurvey(QWidget *parent) : QDialog(parent),
 	sysInfo.append(tr("\nLanguage: %1").arg(uiLanguage(NULL)));
 	os.append(QString("&uiLang=%1").arg(uiLanguage(NULL)));
 	ui->system->setPlainText(sysInfo);
-	manager = SubsurfaceWebServices::manager();
-	connect(manager, SIGNAL(finished(QNetworkReply *)), SLOT(requestReceived(QNetworkReply *)));
 }
 
 UserSurvey::~UserSurvey()
@@ -64,7 +62,7 @@ void UserSurvey::on_buttonBox_accepted()
 	ADD_OPTION(companion);
 	values.append(QString("&suggestion=%1").arg(ui->suggestions->toPlainText()));
 	UserSurveyServices uss(this);
-	uss.sendSurvey(values);
+	connect(uss.sendSurvey(values), SIGNAL(finished()), SLOT(requestReceived()));
 	hide();
 }
 
@@ -90,13 +88,13 @@ void UserSurvey::on_buttonBox_rejected()
 	hide();
 }
 
-void UserSurvey::requestReceived(QNetworkReply *reply)
+void UserSurvey::requestReceived()
 {
 	QMessageBox msgbox;
 	QString msgTitle = tr("Submit user survey.");
 	QString msgText = "<h3>" + tr("Subsurface was unable to submit the user survey.") + "</h3>";
 
-
+	QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
 	if (reply->error() != QNetworkReply::NoError) {
 		//Network Error
 		msgText = msgText + "<br/><b>" + tr("The following error occurred:") + "</b><br/>" + reply->errorString()
diff --git a/qt-ui/usersurvey.h b/qt-ui/usersurvey.h
index b922185..8dacb7b 100644
--- a/qt-ui/usersurvey.h
+++ b/qt-ui/usersurvey.h
@@ -20,13 +20,10 @@ private
 slots:
 	void on_buttonBox_accepted();
 	void on_buttonBox_rejected();
-	void requestReceived(QNetworkReply *reply);
+	void requestReceived();
 
 private:
 	Ui::UserSurvey *ui;
 	QString os;
-	QString checkboxes;
-	QString suggestions;
-	QNetworkAccessManager *manager;
 };
 #endif // USERSURVEY_H
-- 
2.0.1

_______________________________________________
subsurface mailing list
[email protected]
http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to