From 4060888474e49cc0716cdd59d4d1a73c06c5b3bc Mon Sep 17 00:00:00 2001 From: senator Date: Tue, 9 Oct 2007 08:07:55 +0000 Subject: Code cleanup and commenting --- client/builder.cc | 47 +++++++++++++----- client/macro.cc | 10 ++-- client/main.cc | 2 - client/sendrecieve.cc | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++ client/sendrecieve.h | 68 ++++++++++++++++++++++++++ 5 files changed, 243 insertions(+), 16 deletions(-) create mode 100644 client/sendrecieve.cc create mode 100644 client/sendrecieve.h diff --git a/client/builder.cc b/client/builder.cc index 48739a2..29c42c6 100644 --- a/client/builder.cc +++ b/client/builder.cc @@ -14,11 +14,11 @@ #include "widgets/checkbox.h" #include "widgets/window.h" #include +#include #include #include #include #include -#include Builder::Builder(QDomDocument *xml_doc) : QObject() @@ -39,13 +39,16 @@ void Builder::recurser(QDomNode xml_node, QWidget *parent) { QWidget *widget = NULL; QDomElement xml_elem = xml_node.toElement(); + if(xml_elem.tagName() == "macro") { // Assign the macro name and version to QStrings for use when comitting if(xml_elem.hasAttribute("name")) macro = xml_elem.attribute("name"); if(xml_elem.hasAttribute("version")) version = xml_elem.attribute("version"); + } else if(xml_elem.tagName() == "window") { Window *window = new Window(xml_elem); widget = window; + } else if(xml_elem.tagName() == "frame") { if(xml_elem.hasAttribute("caption")) { GroupBox *frame = new GroupBox(xml_elem); @@ -54,43 +57,52 @@ void Builder::recurser(QDomNode xml_node, QWidget *parent) Frame *frame = new Frame(xml_elem); widget = frame; } + } else if(xml_elem.tagName() == "label") { Label *label = new Label(xml_elem); widget = label; + } else if(xml_elem.tagName() == "lineedit") { LineEdit *lineedit = new LineEdit(xml_elem); widgets.push_back(lineedit); widget = lineedit; + } else if(xml_elem.tagName() == "button") { PushButton *pushbutton = new PushButton(xml_elem); connect(pushbutton, SIGNAL(act_commit()), this, SLOT(commit())); connect(pushbutton, SIGNAL(act_reset()), this, SLOT(reset())); connect(pushbutton, SIGNAL(act_cancel()), this, SLOT(cancel())); widget = pushbutton; + } else if(xml_elem.tagName() == "textedit") { TextEdit *textedit = new TextEdit(xml_elem); widgets.push_back(textedit); widget = textedit; + } else if(xml_elem.tagName() == "checkbox") { CheckBox *checkbox = new CheckBox(xml_elem); widgets.push_back(checkbox); widget = checkbox; + } else if(xml_elem.tagName() == "radiobuttons") { RadioButtons *radiobuttons = new RadioButtons(xml_elem); widgets.push_back(radiobuttons); widget = radiobuttons; //return; // Don't iterate children + } else if(xml_elem.tagName() == "combobox") { ComboBox *combobox = new ComboBox(xml_elem); widgets.push_back(combobox); widget = combobox; //return; // Don't iterate children + } else if(xml_elem.tagName() == "listbox") { ListBox *listbox = new ListBox(xml_elem); widgets.push_back(listbox); widget = listbox; //return; // Don't iterate children } + QDomNodeList children = xml_node.childNodes(); for (int i=0; i::iterator i=widgets.begin(); while (i != widgets.end()) { Widget* w = *i; - if(!w->isValid()) faulty++; + if(!w->isValid()) faulty++; // Regexp check, returns valid if entry passed // All selectable entries return "none" if nothing is selected - if(w->getValue() == "none") faulty++; + if(w->getValue() == "none") faulty++; // Faulty+1 if error detected i++; } + // If all entries passed validation, continue commit if(faulty == 0) { printf("Builder -> committing...\n"); + // Build the XML commit QString xml_string; xml_string.append("\n"); xml_string.append("\n"); @@ -138,26 +153,36 @@ void Builder::commit() xml_string.append(" \n"); xml_string.append("\n"); - //char *test = xml_array.data(); + // Print commit to stdout for debug purposes printf("%s\n", xml_string.toStdString().c_str()); + // Convert the commit to Utf-8 charset QByteArray xml_array = xml_string.toUtf8(); QDomDocument xml_result; - //QDomDocument xml_req; + + // Use setContent of QDomDocument to validate the xml commit if (!xml_result.setContent(xml_array)) { printf("Parse error: Invalid XML\n"); } - SendRecieve xml_acquire; - xml_acquire.makeConnection(&xml_result); - QByteArray ba = xml_acquire.getResult(); + // Commit the xml data to the server + SendRecieve macro_commit; + macro_commit.makeConnection(&xml_result); + // Recieve answer from server whether successful or not + QByteArray ba = macro_commit.getResult(); } else { - printf("ERROR!!! Some entries contain errors, aborting commit...\n"); + QMessageBox::critical(NULL, "Fejl", + "Makroen er ikke udfyldt korrekt, prøv igen.\n" + , QMessageBox::Ok); } } void Builder::reset() { +int ret = QMessageBox::warning(NULL, "Reset", + tr("Du har valgt at nulstille de indtastede data.\n" + "Er du sikker?"), + QMessageBox::Yes | QMessageBox::Cancel); printf("Builder -> resetting...\n"); } diff --git a/client/macro.cc b/client/macro.cc index bc97d1c..ec520f0 100644 --- a/client/macro.cc +++ b/client/macro.cc @@ -41,30 +41,34 @@ void macro(QString name) xml_acquire.makeConnection(&xml_req); QByteArray ba = xml_acquire.getResult(); + // Print to stdout, for debug purposes char *test = ba.data(); printf("%s\n", test); - // Parse the XML document + // Parse the XML document using setContent of QDomDocument QDomDocument xml_doc; if (!xml_doc.setContent(ba)) { printf("ERROR: Invalid XML recieved!\n"); } + // Initiate the macro builder with the xml document Builder *builder = new Builder(&xml_doc); } static QDomDocument xml_request(QString name) { - // Create the xml request document here + // Create the xml request array QByteArray xml_array; xml_array.append("\n"); xml_array.append("\n"); - xml_array.append(" \n"); + xml_array.append(" \n"); xml_array.append(""); + // Print to stdout for debug purposes char *test = xml_array.data(); printf("%s\n", test); + // Parse the XML document using setContent of QDomDocument QDomDocument xml_req; if (!xml_req.setContent(xml_array)) { printf("Error: Invalid XML found in request\n"); diff --git a/client/main.cc b/client/main.cc index da806bf..a37ed1e 100644 --- a/client/main.cc +++ b/client/main.cc @@ -28,8 +28,6 @@ #include "macro.h" #include "sendrecieve.h" #include -#include -#include int main(int argc, char *argv[]) { diff --git a/client/sendrecieve.cc b/client/sendrecieve.cc new file mode 100644 index 0000000..ab4d4c6 --- /dev/null +++ b/client/sendrecieve.cc @@ -0,0 +1,132 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * sendrecieve.cc + * + * Fri Jul 13 12:38:45 CEST 2007 + * Copyright 2007 Bent Bisballe Nyeng, Lars Bisballe Jensen and Peter Skaarup + * deva@aasimon.org, elsenator@gmail.com and piparum@piparum.dk + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Pracro is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +#include "sendrecieve.h" + +#include +#include +#include +#include +#include + +SendRecieve::SendRecieve() + : QObject() +{ + has_result = false; +} + +void SendRecieve::tcpConnect() +{ + tcpsocket->connectToHost("gargamel.j.auh.dk", 12345); + tcpConnected = TCP_CONNECTING; +} + +void SendRecieve::tcpDisconnect() +{ + tcpsocket->disconnectFromHost(); + while(tcpConnected != TCP_DISCONNECTED) { + if(tcpConnected == TCP_ERROR) { + tcpConnected = TCP_DISCONNECTED; + return; + } + usleep(5000); + printf("ERROR!!! Couldn't connect to host!\n"); + qApp->processEvents(); + } +} + +int SendRecieve::tcpStatus() +{ + return tcpConnected; +} + +void SendRecieve::makeConnection(QDomDocument *xml_req) +{ + this->xml_req = xml_req; + + tcpsocket = new QTcpSocket; + connect(tcpsocket, SIGNAL(hostFound()), this, SLOT(myHostFound())); + connect(tcpsocket, SIGNAL(readyRead()), this, SLOT(myReadyReadHandler())); + connect(tcpsocket, SIGNAL(connected()), this, SLOT(myConnected())); + connect(tcpsocket, SIGNAL(disconnected()), this, SLOT(myDisconnected())); + connect(tcpsocket, SIGNAL(error(QAbstractSocket::SocketError)), + this, SLOT(myError(QAbstractSocket::SocketError))); + tcpConnect(); +} + +void SendRecieve::myTcpWrite(char *msg, int len) +{ + if(tcpConnected == TCP_CONNECTED) + tcpsocket->write(msg, len); + else + printf("TCP socket not initialized!\n"); +} + +void SendRecieve::myHostFound() // slot +{ + printf("Host Found!\n"); +} + +void SendRecieve::myConnected() // slot +{ + tcpConnected = TCP_CONNECTED; + printf("TCP Connected!\n"); + + QByteArray ba = xml_req->toByteArray(); + char *request = ba.data(); + myTcpWrite(request, ba.length()); +} + +void SendRecieve::myDisconnected() // slot +{ + tcpConnected = TCP_DISCONNECTED; + printf("TCP Disconnected!\n"); + + // Result recieved, allow getResult to return ba_all + has_result = true; +} + +void SendRecieve::myError(QAbstractSocket::SocketError) // slot +{ + tcpConnected = TCP_ERROR; +} + +void SendRecieve::myReadyReadHandler() +{ + QByteArray ba; + + ba = tcpsocket->readAll(); + ba_all.append(ba); +} + +QByteArray SendRecieve::getResult() +{ + while(has_result == false) { + qApp->processEvents(); + } + return ba_all; +} diff --git a/client/sendrecieve.h b/client/sendrecieve.h new file mode 100644 index 0000000..a884b81 --- /dev/null +++ b/client/sendrecieve.h @@ -0,0 +1,68 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * sendrecieve.h + * + * Fri Jul 13 12:38:45 CEST 2007 + * Copyright 2007 Bent Bisballe Nyeng, Lars Bisballe Jensen and Peter Skaarup + * deva@aasimon.org, elsenator@gmail.com and piparum@piparum.dk + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Pracro is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +#ifndef _SENDRECIEVE_H +#define _SENDRECIEVE_H + +#include +#include +#include + +#define TCP_CONNECTED 1 +#define TCP_DISCONNECTED 2 +#define TCP_CONNECTING 3 +#define TCP_ERROR -1 +#define TCP_ERROR_SERVER_NOT_FOUND -2 + +class SendRecieve : public QObject +{ + Q_OBJECT +public: + SendRecieve(); + void makeConnection(QDomDocument *xml_req); + QByteArray getResult(); + +public slots: + void tcpConnect(); + void tcpDisconnect(); + int tcpStatus(); + void myTcpWrite(char *msg, int len); + void myHostFound(); + void myConnected(); + void myDisconnected(); + void myError(QAbstractSocket::SocketError); + void myReadyReadHandler(); + +private: + QTcpSocket *tcpsocket; + int tcpConnected; + QByteArray ba_all; + bool has_result; + QDomDocument *xml_req; +}; + +#endif/*_SENDRECIEVE_H_*/ -- cgit v1.2.3