diff options
author | deva <deva> | 2010-05-06 11:46:57 +0000 |
---|---|---|
committer | deva <deva> | 2010-05-06 11:46:57 +0000 |
commit | c5bf762bc48466334309f78491bd9cf3c06c97ea (patch) | |
tree | 89628e9c15d92c0682e5ccad2e698a2e1003b6b2 | |
parent | 10db0da7067c93333ab2a9ad5bf15f8ce99b6b3b (diff) |
Dispose of old QHttp class in favor of the more recent QNetworkAccessManager class.
-rw-r--r-- | client/netcom.cc | 169 | ||||
-rw-r--r-- | client/netcom.h | 23 |
2 files changed, 82 insertions, 110 deletions
diff --git a/client/netcom.cc b/client/netcom.cc index 4126b02..a8f4da2 100644 --- a/client/netcom.cc +++ b/client/netcom.cc @@ -51,55 +51,85 @@ NetCom::NetCom(QString host, quint16 port, QString user, QString cpr) this->user = user; this->cpr = cpr; - connect(&http, SIGNAL(readyRead(const QHttpResponseHeader &)), - this, SLOT(readyRead(const QHttpResponseHeader &))); - connect(&http, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int))); + // + // Setup connection + // + QUrl url; + url.setHost(host); + url.setPort(port); + url.setScheme("http"); + // url.setScheme("https"); -#ifdef USE_SSL - connect(&http, SIGNAL(sslErrors(const QList<QSslError> &)), - this, SLOT(sslError(const QList<QSslError> &))); - http.setHost(host, QHttp::ConnectionModeHttps, port); -#else - http.setHost(host, QHttp::ConnectionModeHttp, port); -#endif + request.setUrl(url); - transfering = false; + manager = new QNetworkAccessManager(this); + connect(manager, SIGNAL(finished(QNetworkReply*)), + this, SLOT(replyFinished(QNetworkReply*))); - QHttpRequestHeader h("HTTP 1.1", "/"); - http.request(h, ""); + QDomDocument doc; + makeTransfer(doc, false, true); +} - transfering = true; - buffer = ""; - do { - qApp->processEvents(QEventLoop::WaitForMoreEvents); - } while(transfering); +NetCom::~NetCom() +{ + // + // Send commit + // + QDomDocument doc; + makeTransfer(doc, true, true); - sessionid = http.lastResponse().value("SessionID"); - printf("SESSION ID: %s\n", sessionid.toStdString().c_str()); + // + // Clean up + // + delete manager; } -NetCom::~NetCom() +void NetCom::replyFinished(QNetworkReply *reply) { - QHttpRequestHeader h("HTTP 1.1", "/"); - h.setValue("SessionCommit", ""); - h.setValue("SessionID", sessionid); - http.request(h, ""); - - transfering = true; - buffer = ""; - do { - qApp->processEvents(QEventLoop::WaitForMoreEvents); - } while(transfering); + finished[reply] = true; } -QDomDocument NetCom::send(QString templ, QString macro, bool lockgui) +QDomDocument NetCom::makeTransfer(QDomDocument &doc, bool commit, bool lockgui) { + printf("\nMaking transfer:\n%s", doc.toString().toStdString().c_str()); + if(lockgui && qApp->activeWindow()) qApp->activeWindow()->setEnabled(false); if(lockgui) QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); + if(sessionid != "") request.setRawHeader("SessionID", + sessionid.toStdString().c_str()); + if(commit) request.setRawHeader("SessionCommit", "yes"); + + // QNetworkReply *reply = manager->get(request); + QNetworkReply *reply = manager->put(request, doc.toByteArray()); + finished[reply] = false; + while(finished[reply] == false) { + qApp->processEvents(QEventLoop::WaitForMoreEvents); + } + finished.remove(reply); + + QDomDocument res_doc; + res_doc.setContent(reply->readAll()); + + printf("\nRecieved repons:\n%s", res_doc.toString().toStdString().c_str()); + + if(reply->hasRawHeader("SessionID")) { + sessionid = reply->rawHeader("SessionID"); + printf("SESSION ID: %s\n", sessionid.toStdString().c_str()); + } + + if(lockgui) QApplication::restoreOverrideCursor(); + if(lockgui && qApp->activeWindow()) qApp->activeWindow()->setEnabled(true); + + return res_doc; +} + +QDomDocument NetCom::send(QString templ, QString macro, bool lockgui) +{ QDomDocument doc; - QDomProcessingInstruction header = doc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); + QDomProcessingInstruction header = + doc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); doc.appendChild(header); QDomElement pracro_elem = doc.createElement("pracro"); @@ -112,36 +142,17 @@ QDomDocument NetCom::send(QString templ, QString macro, bool lockgui) request_elem.setAttribute("template", templ); if(macro != "") request_elem.setAttribute("macro", macro); pracro_elem.appendChild(request_elem); - - printf("\nSending request:\n%s", doc.toString().toStdString().c_str()); - QHttpRequestHeader h("HTTP 1.1", "/"); - h.setValue("SessionID", sessionid); - http.request(h, doc.toByteArray()); - - QDomDocument res_doc; - transfering = true; - buffer = ""; - do { - qApp->processEvents(QEventLoop::WaitForMoreEvents); - } while(transfering); - res_doc.setContent(buffer); - - QDomElement elem = res_doc.documentElement(); - - printf("\nRecieved request:\n%s", res_doc.toString().toStdString().c_str()); - - if(lockgui) QApplication::restoreOverrideCursor(); - if(lockgui && qApp->activeWindow()) qApp->activeWindow()->setEnabled(true); - - return res_doc; + return makeTransfer(doc, false, lockgui); } -void NetCom::send(QVector< Widget* > widgets, QString templ, QString macro, QString version) +void NetCom::send(QVector< Widget* > widgets, QString templ, + QString macro, QString version) { QDomDocument doc; - QDomProcessingInstruction header = doc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); + QDomProcessingInstruction header = + doc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); doc.appendChild(header); QDomElement pracro_elem = doc.createElement("pracro"); @@ -156,7 +167,8 @@ void NetCom::send(QVector< Widget* > widgets, QString templ, QString macro, QStr commit_elem.setAttribute("version", version); pracro_elem.appendChild(commit_elem); - // Iterate the different entries, and append their results to the commit string + // Iterate the different entries, and append their results to + // the commit string QVector< Widget* >::iterator i = widgets.begin(); while (i != widgets.end()) { Widget* w = *i; @@ -171,44 +183,5 @@ void NetCom::send(QVector< Widget* > widgets, QString templ, QString macro, QStr i++; } - if(qApp->activeWindow()) qApp->activeWindow()->setEnabled(false); - - printf("\nSending commit:\n%s", doc.toString().toStdString().c_str()); - - QHttpRequestHeader h("HTTP 1.1", "/"); - h.setValue("SessionID", sessionid); - http.request(h, doc.toByteArray()); - - transfering = true; - buffer = ""; - do { - qApp->processEvents(QEventLoop::WaitForMoreEvents); - } while(transfering); - - QApplication::restoreOverrideCursor(); - if(qApp->activeWindow()) qApp->activeWindow()->setEnabled(true); -} - -void NetCom::readyRead(const QHttpResponseHeader &) -{ - // printf("STATE: readyRead\n"); - buffer += http.readAll(); -} - -void NetCom::stateChanged(int state) -{ - // printf("STATE: %d\n", state); - if(transfering && state == QHttp::Connected) transfering = false; -} - -#ifdef USE_SSL -void NetCom::sslError(const QList<QSslError> &errlst) -{ - QList<QSslError>::const_iterator i = errlst.begin(); - while(i != errlst.end()) { - QMessageBox::warning(qApp->activeWindow(), "SSL Error", i->errorString()); - i++; - } - http.ignoreSslErrors(); + makeTransfer(doc, false, true); } -#endif diff --git a/client/netcom.h b/client/netcom.h index 718d5a6..32f5982 100644 --- a/client/netcom.h +++ b/client/netcom.h @@ -31,7 +31,8 @@ #include <QString> #include <QTcpSocket> #include <QDomDocument> -#include <QHttp> +#include <QNetworkAccessManager> +#include <QNetworkRequest> //#define USE_SSL @@ -45,26 +46,24 @@ public: ~NetCom(); QDomDocument send(QString templ, QString macro = "", bool lockgui = true); - void send(QVector< Widget* > widgets, QString templ, QString macro, QString version); + void send(QVector< Widget* > widgets, QString templ, QString macro, + QString version); public slots: - void stateChanged(int state); - void readyRead(const QHttpResponseHeader &resp); - -#ifdef USE_SSL - void sslError(const QList<QSslError> &errlst); -#endif + void replyFinished(QNetworkReply*); private: - volatile bool transfering; - QString user; QString cpr; - QHttp http; + QNetworkAccessManager *manager; + QNetworkRequest request; - QByteArray buffer; QString sessionid; + + QMap<QNetworkReply *, bool> finished; + + QDomDocument makeTransfer(QDomDocument &dom, bool commit, bool lockgui); }; #endif/*__PRACRO_NETCOM_H__*/ |