[PATCH 3/3] Use QUrl and QUrlQuery to encode the user survey

Thiago Macieira thiago at macieira.org
Sun Aug 17 12:01:00 PDT 2014


Most of the data we send are simple identifiers, so there wouldn't be a
problem. The problem is the free-form user suggestion field, which needs
to be properly encoded before it can be sent over the HTTP request.

I could have used QUrl even in Qt 5, but using addQueryItem in QUrl in
Qt 5 is deprecated and slow.

Signed-off-by: Thiago Macieira <thiago at macieira.org>
---
 qt-ui/subsurfacewebservices.cpp |  6 ++++--
 qt-ui/subsurfacewebservices.h   |  2 +-
 qt-ui/usersurvey.cpp            | 39 +++++++++++++++++++++++++++------------
 qt-ui/usersurvey.h              |  1 -
 4 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp
index c3661e0..8cf535a 100644
--- a/qt-ui/subsurfacewebservices.cpp
+++ b/qt-ui/subsurfacewebservices.cpp
@@ -927,10 +927,12 @@ UserSurveyServices::UserSurveyServices(QWidget *parent, Qt::WindowFlags f) : Web
 
 }
 
-QNetworkReply* UserSurveyServices::sendSurvey(QString values)
+QNetworkReply* UserSurveyServices::sendSurvey(const QUrl &queryValues)
 {
 	QNetworkRequest request;
-	request.setUrl(QString("http://subsurface.hohndel.org/survey?%1").arg(values));
+	QUrl url("http://subsurface.hohndel.org/survey");
+	url = url.resolved(queryValues);
+	request.setUrl(url);
 	request.setRawHeader("Accept", "text/xml");
 	request.setRawHeader("User-Agent", userAgent.toUtf8());
 	reply = manager()->get(request);
diff --git a/qt-ui/subsurfacewebservices.h b/qt-ui/subsurfacewebservices.h
index a973538..b7531e0 100644
--- a/qt-ui/subsurfacewebservices.h
+++ b/qt-ui/subsurfacewebservices.h
@@ -101,7 +101,7 @@ private:
 class UserSurveyServices : public WebServices {
 	Q_OBJECT
 public:
-	QNetworkReply* sendSurvey(QString values);
+	QNetworkReply* sendSurvey(const QUrl &queryValues);
 	explicit UserSurveyServices(QWidget *parent = 0, Qt::WindowFlags f = 0);
 private
 slots:
diff --git a/qt-ui/usersurvey.cpp b/qt-ui/usersurvey.cpp
index b68589f..c047dfc 100644
--- a/qt-ui/usersurvey.cpp
+++ b/qt-ui/usersurvey.cpp
@@ -2,6 +2,10 @@
 #include <QMessageBox>
 #include <QDebug>
 #include <QSettings>
+#include <QUrl>
+#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
+#  include <QUrlQuery>
+#endif
 
 #include "usersurvey.h"
 #include "ui_usersurvey.h"
@@ -21,14 +25,6 @@ UserSurvey::UserSurvey(QWidget *parent) : QDialog(parent),
 	QShortcut *quitKey = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
 	connect(quitKey, SIGNAL(activated()), parent, SLOT(close()));
 
-	os = QString("ssrfVers=%1").arg(VERSION_STRING);
-	os.append(QString("&kernelType=%1").arg(SubsurfaceSysInfo::kernelType()));
-	os.append(QString("&kernelVersion=%1").arg(SubsurfaceSysInfo::kernelVersion()));
-	os.append(QString("&productType=%1").arg(SubsurfaceSysInfo::productType()));
-	os.append(QString("&productVersion=%1").arg(SubsurfaceSysInfo::productVersion()));
-	os.append(QString("&buildCpuArchitecture=%1").arg(SubsurfaceSysInfo::buildCpuArchitecture()));
-	os.append(QString("&currentCpuArchitecture=%1").arg(SubsurfaceSysInfo::currentCpuArchitecture()));
-	os.append(QString("&uiLang=%1").arg(uiLanguage(NULL)));
 	ui->system->setPlainText(getVersion());
 }
 
@@ -63,12 +59,25 @@ UserSurvey::~UserSurvey()
 	delete ui;
 }
 
-#define ADD_OPTION(_name) values.append(ui->_name->isChecked() ? "&" #_name "=1" : "&" #_name "=0")
+#define ADD_OPTION(_name) values.addQueryItem(#_name, QString(QLatin1Char('0' + int(ui->_name->isChecked()))));
 
 void UserSurvey::on_buttonBox_accepted()
 {
+#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
+	QUrlQuery values;
+#else
+	QUrl values;
+#endif
 	// now we need to collect the data and submit it
-	QString values = os;
+	values.addQueryItem("ssrfVers", VERSION_STRING);
+	values.addQueryItem("kernelType", SubsurfaceSysInfo::kernelType());
+	values.addQueryItem("kernelVersion", SubsurfaceSysInfo::kernelVersion());
+	values.addQueryItem("productType", SubsurfaceSysInfo::productType());
+	values.addQueryItem("productVersion", SubsurfaceSysInfo::productVersion());
+	values.addQueryItem("buildCpuArchitecture", SubsurfaceSysInfo::buildCpuArchitecture());
+	values.addQueryItem("currentCpuArchitecture", SubsurfaceSysInfo::currentCpuArchitecture());
+	values.addQueryItem("uiLang", uiLanguage(NULL));
+	values.addQueryItem("suggestion", ui->suggestions->toPlainText());
 	ADD_OPTION(recreational);
 	ADD_OPTION(tech);
 	ADD_OPTION(planning);
@@ -76,9 +85,15 @@ void UserSurvey::on_buttonBox_accepted()
 	ADD_OPTION(divecomputer);
 	ADD_OPTION(manual);
 	ADD_OPTION(companion);
-	values.append(QString("&suggestion=%1").arg(ui->suggestions->toPlainText()));
 	UserSurveyServices uss(this);
-	connect(uss.sendSurvey(values), SIGNAL(finished()), SLOT(requestReceived()));
+
+#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
+	QUrl urlvalues;
+	urlvalues.setQuery(values);
+#else
+	QUrl urlvalues = values;
+#endif
+	connect(uss.sendSurvey(urlvalues), SIGNAL(finished()), SLOT(requestReceived()));
 	hide();
 }
 
diff --git a/qt-ui/usersurvey.h b/qt-ui/usersurvey.h
index 5514052..3725f65 100644
--- a/qt-ui/usersurvey.h
+++ b/qt-ui/usersurvey.h
@@ -26,6 +26,5 @@ slots:
 
 private:
 	Ui::UserSurvey *ui;
-	QString os;
 };
 #endif // USERSURVEY_H
-- 
1.8.4.5



More information about the subsurface mailing list