diff options
Diffstat (limited to 'client/netcom.cc')
-rw-r--r-- | client/netcom.cc | 177 |
1 files changed, 110 insertions, 67 deletions
diff --git a/client/netcom.cc b/client/netcom.cc index 3b3abb7..97f9ee1 100644 --- a/client/netcom.cc +++ b/client/netcom.cc @@ -26,64 +26,95 @@ */ #include "netcom.h" +#include <QtNetwork> + #include <QApplication> #include <QByteArray> +#include <QHttp> + #include "widgets/widget.h" -NetCom::NetCom(QString host, quint16 port, QString user, QString cpr) +#ifdef USE_SSL +#include <QMessageBox> +#include <QList> +#include <QSslError> +#include <QSslSocket> + +#ifdef QT_NO_OPENSSL +#error "QT not compiled with SSL support." +#endif +#endif + +NetCom::NetCom(QString host, quint16 port) { - this->user = user; - this->cpr = cpr; - socket.connectToHost(host, port); - connect(&socket, SIGNAL(readyRead()), this, SLOT(readyRead())); - socket.waitForConnected(); - transmitting = false; + // + // Setup connection + // + QUrl url; + url.setHost(host); + url.setPort(port); + url.setScheme("http"); + // url.setScheme("https"); + + request.setUrl(url); + + manager = new QNetworkAccessManager(this); + connect(manager, SIGNAL(finished(QNetworkReply*)), + this, SLOT(replyFinished(QNetworkReply*))); } NetCom::~NetCom() { - socket.disconnectFromHost(); + // + // Clean up + // + delete manager; } -QDomDocument NetCom::send(QString templ, QString macro, bool lockgui) +void NetCom::replyFinished(QNetworkReply *reply) +{ + finished[reply] = true; +} + +QDomDocument NetCom::makeTransfer(QDomDocument &doc, + bool commit, bool lockgui, bool discard) { - printf("Socket state: %d\n", socket.state()); - if(socket.state() != 3) printf("Socket state not connected: %s\n", socket.errorString().toStdString().c_str()); + 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)); - QDomDocument doc; - - QDomProcessingInstruction header = doc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); - doc.appendChild(header); - - QDomElement pracro_elem = doc.createElement("pracro"); - pracro_elem.setAttribute("version", "1.0"); - pracro_elem.setAttribute("cpr", cpr); - pracro_elem.setAttribute("user", user); - doc.appendChild(pracro_elem); + if(sessionid != "") request.setRawHeader("SessionID", + sessionid.toStdString().c_str()); + if(commit) { + request.setRawHeader("SessionCommit", "yes"); + } - QDomElement request_elem = doc.createElement("request"); - 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()); + if(discard) { + request.setRawHeader("SessionDiscard", "yes"); + } - socket.write(doc.toByteArray()); - // socket.waitForReadyRead(); + // QNetworkReply *reply = manager->get(request); + QNetworkReply *reply = manager->post(request, doc.toByteArray()); + finished[reply] = false; + while(finished[reply] == false) { + qApp->processEvents(QEventLoop::WaitForMoreEvents, 100); + } + finished.remove(reply); - do { - qApp->processEvents(QEventLoop::WaitForMoreEvents); - } while(!res_doc.setContent(buffer)); + QByteArray data = reply->readAll(); + QDomDocument res_doc; + res_doc.setContent(data); - buffer = ""; + printf("\nRecieved reponse:\n%s", data.data()); - QDomElement elem = res_doc.documentElement(); + printf("\nRecieved reponse (Parsed):\n%s", res_doc.toByteArray().data()); - printf("\nRecieved request:\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); @@ -91,26 +122,58 @@ QDomDocument NetCom::send(QString templ, QString macro, bool lockgui) return res_doc; } -void NetCom::readyRead() +QDomDocument NetCom::initConnection() { - buffer.append(socket.readAll()); + QDomDocument doc; + return makeTransfer(doc, false, true); } -void NetCom::send(QVector< Widget* > widgets, QString templ, QString macro, QString version) +QDomDocument NetCom::commit() { - printf("Socket state: %d\n", socket.state()); - if(socket.state() != 3) printf("Socket state not connected: %s\n", socket.errorString().toStdString().c_str()); + QDomDocument doc; + return makeTransfer(doc, true, true); +} - // if(qApp->activeWindow()) qApp->activeWindow()->setEnabled(false); // Moved down! +QDomDocument NetCom::discard() +{ + QDomDocument doc; + return makeTransfer(doc, false, true, true); +} + +QDomDocument NetCom::send(QString templ, QString macro, bool lockgui) +{ + QDomDocument doc; + + QDomProcessingInstruction header = + doc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); + doc.appendChild(header); + + QDomElement pracro_elem = doc.createElement("pracro"); + pracro_elem.setAttribute("version", "1.0"); + pracro_elem.setAttribute("cpr", patientid); + pracro_elem.setAttribute("user", user); + doc.appendChild(pracro_elem); + + QDomElement request_elem = doc.createElement("request"); + request_elem.setAttribute("template", templ); + if(macro != "") request_elem.setAttribute("macro", macro); + pracro_elem.appendChild(request_elem); + + return makeTransfer(doc, false, lockgui); +} +QDomDocument 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"); pracro_elem.setAttribute("version", "1.0"); - pracro_elem.setAttribute("cpr", cpr); + pracro_elem.setAttribute("cpr", patientid); pracro_elem.setAttribute("user", user); doc.appendChild(pracro_elem); @@ -120,7 +183,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; @@ -134,27 +198,6 @@ 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()); - - socket.write(doc.toByteArray()); - // socket.waitForReadyRead(); - - // - // Wait for the (hopefully) empty answer. - // - do { - qApp->processEvents(QEventLoop::WaitForMoreEvents); - } while(!res_doc.setContent(buffer)); - - buffer = ""; - - //QDomElement elem = res_doc.documentElement(); - - printf("\nRecieved commit:\n%s", res_doc.toString().toStdString().c_str()); - - QApplication::restoreOverrideCursor(); - if(qApp->activeWindow()) qApp->activeWindow()->setEnabled(true); + + return makeTransfer(doc, false, true); } |