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 <tomaz.canabrava@intel.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2014-07-16 17:20:21 -03:00 committed by Dirk Hohndel
parent bbbb4ced24
commit 0dd40b7a51
6 changed files with 11 additions and 19 deletions

View file

@ -917,10 +917,11 @@ UserSurveyServices::UserSurveyServices(QWidget *parent, Qt::WindowFlags f) : Web
} }
void UserSurveyServices::sendSurvey(QString values) QNetworkReply* UserSurveyServices::sendSurvey(QString values)
{ {
QNetworkRequest request; QNetworkRequest request;
request.setUrl(QString("http://subsurface.hohndel.org/survey?%1").arg(values)); request.setUrl(QString("http://subsurface.hohndel.org/survey?%1").arg(values));
request.setRawHeader("Accept", "text/xml"); request.setRawHeader("Accept", "text/xml");
reply = manager()->get(request); reply = manager()->get(request);
return reply;
} }

View file

@ -100,7 +100,7 @@ private:
class UserSurveyServices : public WebServices { class UserSurveyServices : public WebServices {
Q_OBJECT Q_OBJECT
public: public:
void sendSurvey(QString values); QNetworkReply* sendSurvey(QString values);
explicit UserSurveyServices(QWidget *parent = 0, Qt::WindowFlags f = 0); explicit UserSurveyServices(QWidget *parent = 0, Qt::WindowFlags f = 0);
private private
slots: slots:

View file

@ -6,8 +6,6 @@
UpdateManager::UpdateManager(QObject *parent) : QObject(parent) UpdateManager::UpdateManager(QObject *parent) : QObject(parent)
{ {
manager = SubsurfaceWebServices::manager();
connect(manager, SIGNAL(finished(QNetworkReply *)), SLOT(requestReceived(QNetworkReply *)));
} }
void UpdateManager::checkForUpdates() void UpdateManager::checkForUpdates()
@ -26,16 +24,16 @@ void UpdateManager::checkForUpdates()
QString version = VERSION_STRING; QString version = VERSION_STRING;
QString url = QString("http://subsurface.hohndel.org/updatecheck.html?os=%1&ver=%2").arg(os, version); 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; QMessageBox msgbox;
QString msgTitle = tr("Check for updates."); QString msgTitle = tr("Check for updates.");
QString msgText = "<h3>" + tr("Subsurface was unable to check for updates.") + "</h3>"; QString msgText = "<h3>" + tr("Subsurface was unable to check for updates.") + "</h3>";
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
if (reply->error() != QNetworkReply::NoError) { if (reply->error() != QNetworkReply::NoError) {
//Network Error //Network Error
msgText = msgText + "<br/><b>" + tr("The following error occurred:") + "</b><br/>" + reply->errorString() msgText = msgText + "<br/><b>" + tr("The following error occurred:") + "</b><br/>" + reply->errorString()

View file

@ -12,11 +12,9 @@ public:
explicit UpdateManager(QObject *parent = 0); explicit UpdateManager(QObject *parent = 0);
void checkForUpdates(); void checkForUpdates();
private:
QNetworkAccessManager *manager;
public public
slots: slots:
void requestReceived(QNetworkReply *reply); void requestReceived();
}; };
#endif // UPDATEMANAGER_H #endif // UPDATEMANAGER_H

View file

@ -40,8 +40,6 @@ UserSurvey::UserSurvey(QWidget *parent) : QDialog(parent),
sysInfo.append(tr("\nLanguage: %1").arg(uiLanguage(NULL))); sysInfo.append(tr("\nLanguage: %1").arg(uiLanguage(NULL)));
os.append(QString("&uiLang=%1").arg(uiLanguage(NULL))); os.append(QString("&uiLang=%1").arg(uiLanguage(NULL)));
ui->system->setPlainText(sysInfo); ui->system->setPlainText(sysInfo);
manager = SubsurfaceWebServices::manager();
connect(manager, SIGNAL(finished(QNetworkReply *)), SLOT(requestReceived(QNetworkReply *)));
} }
UserSurvey::~UserSurvey() UserSurvey::~UserSurvey()
@ -64,7 +62,7 @@ void UserSurvey::on_buttonBox_accepted()
ADD_OPTION(companion); ADD_OPTION(companion);
values.append(QString("&suggestion=%1").arg(ui->suggestions->toPlainText())); values.append(QString("&suggestion=%1").arg(ui->suggestions->toPlainText()));
UserSurveyServices uss(this); UserSurveyServices uss(this);
uss.sendSurvey(values); connect(uss.sendSurvey(values), SIGNAL(finished()), SLOT(requestReceived()));
hide(); hide();
} }
@ -90,13 +88,13 @@ void UserSurvey::on_buttonBox_rejected()
hide(); hide();
} }
void UserSurvey::requestReceived(QNetworkReply *reply) void UserSurvey::requestReceived()
{ {
QMessageBox msgbox; QMessageBox msgbox;
QString msgTitle = tr("Submit user survey."); QString msgTitle = tr("Submit user survey.");
QString msgText = "<h3>" + tr("Subsurface was unable to submit the user survey.") + "</h3>"; QString msgText = "<h3>" + tr("Subsurface was unable to submit the user survey.") + "</h3>";
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
if (reply->error() != QNetworkReply::NoError) { if (reply->error() != QNetworkReply::NoError) {
//Network Error //Network Error
msgText = msgText + "<br/><b>" + tr("The following error occurred:") + "</b><br/>" + reply->errorString() msgText = msgText + "<br/><b>" + tr("The following error occurred:") + "</b><br/>" + reply->errorString()

View file

@ -20,13 +20,10 @@ private
slots: slots:
void on_buttonBox_accepted(); void on_buttonBox_accepted();
void on_buttonBox_rejected(); void on_buttonBox_rejected();
void requestReceived(QNetworkReply *reply); void requestReceived();
private: private:
Ui::UserSurvey *ui; Ui::UserSurvey *ui;
QString os; QString os;
QString checkboxes;
QString suggestions;
QNetworkAccessManager *manager;
}; };
#endif // USERSURVEY_H #endif // USERSURVEY_H