summaryrefslogtreecommitdiff
path: root/client/macro.cc
diff options
context:
space:
mode:
authordeva <deva>2008-08-18 14:54:03 +0000
committerdeva <deva>2008-08-18 14:54:03 +0000
commit3c199adf6c317ade446120db1cdb51b473edca57 (patch)
tree8b865ad2fd9208be60333bf234bf8a8d5929d8b9 /client/macro.cc
parentcbb2adb8f35dfc11ac58bff6fa56b99f071a76cd (diff)
Rewrite of all network code, with a reusable socket. Still needs some error checking.
Diffstat (limited to 'client/macro.cc')
-rw-r--r--client/macro.cc87
1 files changed, 24 insertions, 63 deletions
diff --git a/client/macro.cc b/client/macro.cc
index aa82d50..b0943d0 100644
--- a/client/macro.cc
+++ b/client/macro.cc
@@ -25,9 +25,15 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "macro.h"
-#include "sendrecieve.h"
-#include <QDomDocument>
+
#include <QApplication>
+#include <QDomDocument>
+#include <QObject>
+#include <QEvent>
+#include <QLinkedList>
+
+#include "macrowindow.h"
+#include "netcom.h"
#define MACRO_EVENT_ID 65432
@@ -63,63 +69,15 @@ protected:
* The single global macro event filter.
* It is created the first time new_macro is called.
*/
-MacroEventFilter *macro_event_filter = NULL;
-
-/**
- * This function sends a request to the praco server, and returns the
- * parsed answer.
- */
-static QDomDocument xml_request(QString course, QString macro)
-{
- // Create the xml request array
- QByteArray xml_array;
- printf("course: %s, macro: %s, cpr: %s, user: %s\n",
- course.toStdString().c_str(),
- macro.toStdString().c_str(),
- cpr.toStdString().c_str(),
- user.toStdString().c_str());
- xml_array.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
- xml_array.append("<pracro version=\"1.0\" cpr=\"" + cpr + "\" user=\"" + user + "\">\n");
- xml_array.append(" <request course=\"" + course + "\" macro=\"" + macro + "\"/>\n");
- xml_array.append("</pracro>");
-
- // 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");
- }
-
- return xml_req;
-}
+static MacroEventFilter *macro_event_filter = NULL;
/**
* Create the new macro
*/
static void create_macro(QString course, QString macro)
{
- // Build the XML request
- QDomDocument xml_req = xml_request(course, macro);
-
- // Fetch the XML document
- SendRecieve xml_acquire(host, port);
- 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 using setContent of QDomDocument
- QDomDocument xml_doc;
- if (!xml_doc.setContent(ba)) {
- printf("ERROR: Invalid XML recieved!\n");
- fwrite(ba.data(), ba.size(), 1, stdout);
- return;
- }
+ NetCom netcom(host, port, user, cpr);
+ QDomDocument xml_doc = netcom.send(course, macro);
cleanup_macros();
@@ -130,7 +88,7 @@ static void create_macro(QString course, QString macro)
// Initiate the new macro window with the xml document and push
// it to the window list
QDomNodeList courses = xml_doc.documentElement().childNodes();
- QDomNode coursenode = courses.at(0); // There can be only one!
+ QDomNode coursenode = courses.at(0); // There can be only one! (Swush, flomp)
QDomNodeList macros = coursenode.childNodes();
for(int j = 0; j < macros.count(); j++) {
QDomNode macronode = macros.at(j);
@@ -140,9 +98,12 @@ static void create_macro(QString course, QString macro)
}
}
-bool MacroEventFilter::eventFilter( QObject *, QEvent *e )
+/**
+ * Event filter callback method
+ */
+bool MacroEventFilter::eventFilter(QObject *, QEvent *e)
{
- if ( e->type() == MACRO_EVENT_ID ) {
+ if(e->type() == MACRO_EVENT_ID) {
MacroEvent *event = (MacroEvent*)e;
create_macro(event->course, event->macro);
return TRUE; // eat event
@@ -151,26 +112,26 @@ bool MacroEventFilter::eventFilter( QObject *, QEvent *e )
}
}
-// Delete all closed windows from window list
+/**
+ * Delete all closed windows from window list
+ */
void cleanup_macros()
{
- int dead = 0;
- int live = 0;
-
QLinkedList< MacroWindow * >::iterator i = macrowindows.begin();
while(i != macrowindows.end()) {
if( (*i)->isClosed() ) {
- dead++;
delete *i;
i = macrowindows.erase(i);
} else {
- live++;
i++;
}
}
- printf("Found %d live ones and %d dead ones.\n", live, dead);
}
+/**
+ * Public macro creation function.
+ * Initiates the creation of a new macro.
+ */
void new_macro(QString course, QString macro)
{
if(macro_event_filter == NULL) {