Engauge Digitizer  2
NetworkClient.cpp
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "Logger.h"
8 #include "NetworkClient.h"
9 #include <QNetworkReply>
10 #include <QNetworkRequest>
11 
12 const QString SERVER_URL ("http://engaugedigitizer.net/receive_crash_report.php");
13 
14 NetworkClient::NetworkClient (QObject *parent) :
15  QNetworkAccessManager (parent)
16 {
17  connect (this, SIGNAL (finished (QNetworkReply *)), this, SLOT (slotFinished (QNetworkReply *)));
18 }
19 
20 QString NetworkClient::cleanXml (const QString &before)
21 {
22  LOG4CPP_INFO_S ((*mainCat)) << "NetworkClient::cleanXml";
23 
24  // Remove characters that are known to break the xml parsing in Document. Bad characters AFTER the
25  // CDATA will break the parsing of the CDATA, which is a very hard bug to track down, so this
26  // method should prevent that specific issue
27 
28  QString after;
29  for (int i = 0; i < before.size(); i++) {
30 
31  if (before.at (i).unicode() < 128) {
32 
33  after += before.at (i);
34  }
35  }
36 
37  return after;
38 }
39 
40 void NetworkClient::slotFinished (QNetworkReply *reply)
41 {
42  reply->deleteLater();
43 }
44 
45 void NetworkClient::uploadErrorReport (const QString &report)
46 {
47  QString reportClean = cleanXml (report);
48 
49  // Put report into byte array, which must persist until the finished signal
50  // is received according to QNetworkAccessManager::post documentation
51  QByteArray postData = reportClean.toLatin1();
52 
53  QNetworkRequest request (SERVER_URL);
54  request.setHeader (QNetworkRequest::ContentTypeHeader,
55  QVariant (QString ("text/xml")));
56  request.setHeader (QNetworkRequest::ContentLengthHeader,
57  QVariant (qulonglong (postData.size())));
58 
59  post(request,
60  postData);
61 }
void slotFinished(QNetworkReply *)
Cleanup after response is returned.
NetworkClient(QObject *parent)
Single constructor.
void uploadErrorReport(const QString &report)
Upload the error report asynchronously.