From b76db8a6385f3eb2a5f91367789e857fb7fe86ae Mon Sep 17 00:00:00 2001 From: deva Date: Thu, 29 May 2008 12:30:47 +0000 Subject: Cleaned up the new_macro functions. Did some GPL header correction. --- client/macro.cc | 117 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 73 insertions(+), 44 deletions(-) (limited to 'client/macro.cc') diff --git a/client/macro.cc b/client/macro.cc index 7f8ea27..82c68a4 100644 --- a/client/macro.cc +++ b/client/macro.cc @@ -3,8 +3,8 @@ * macro.cc * * Fri Aug 31 13:40:17 CEST 2007 - * Copyright 2007 Bent Bisballe Nyeng, Lars Bisballe Jensen and Peter Skaarup - * deva@aasimon.org, elsenator@gmail.com and piparum@piparum.dk + * Copyright 2007 Bent Bisballe Nyeng and Lars Bisballe Jensen + * deva@aasimon.org and elsenator@gmail.com ****************************************************************************/ /* @@ -30,28 +30,78 @@ #include #include -#define MY_EVENT_ID 65432 +#define MACRO_EVENT_ID 65432 extern QString cpr; extern QString user; extern QString host; extern quint16 port; -class MyEvent : public QEvent { +/** + * Macro Event used to trigger the creation of a new macro + */ +class MacroEvent : public QEvent { public: - MyEvent(QString macro) : QEvent((QEvent::Type)MY_EVENT_ID) - { - this->macro = macro; - } + MacroEvent(QString course, QString macro) : + QEvent((QEvent::Type)MACRO_EVENT_ID), + macro(macro), + course(course) {} QString macro; + QString course; +}; + +/** + * Macro Event Filter used to catch the Macro Events. + */ +class MacroEventFilter : public QObject { +protected: + bool eventFilter( QObject *o, QEvent *e ); }; -static QDomDocument xml_request(QString name); +/** + * 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("\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"); + } + + return xml_req; +} -void create_macro(QString name) +/** + * Create the new macro + */ +static void create_macro(QString course, QString macro) { // Build the XML request - QDomDocument xml_req = xml_request(name); + QDomDocument xml_req = xml_request(course, macro); // Fetch the XML document SendRecieve xml_acquire(host, port); @@ -70,50 +120,29 @@ void create_macro(QString name) } // Initiate the macro builder with the xml document + // FIXME: This should be done in some other way, to prevent the memory leak. new Builder(&xml_doc); - //Builder *builder = new Builder(&xml_doc); } -bool MyEventHandler::eventFilter( QObject *, QEvent *e ) +bool MacroEventFilter::eventFilter( QObject *, QEvent *e ) { - if ( e->type() == MY_EVENT_ID ) { - MyEvent *event = (MyEvent*)e; - create_macro(event->macro); - // ... DO SOMETHING WITH EVENT + if ( e->type() == MACRO_EVENT_ID ) { + MacroEvent *event = (MacroEvent*)e; + create_macro(event->course, event->macro); return TRUE; // eat event } else { - // standard event processing return FALSE; } } -void new_macro(QString macro) -{ - MyEvent *event = new MyEvent(macro); - qApp->postEvent(qApp, event); - qApp->processEvents(); /* To prevent QT from closing when - no windows are present */ -} - -static QDomDocument xml_request(QString name) +void new_macro(QString course, QString macro) { - // Create the xml request array - QByteArray xml_array; - printf("macro: %s, cpr: %s, user: %s\n", name.toStdString().c_str(), cpr.toStdString().c_str(), user.toStdString().c_str()); - 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"); + if(macro_event_filter == NULL) { + macro_event_filter = new MacroEventFilter(); + qApp->installEventFilter( macro_event_filter ); } - return xml_req; + MacroEvent *event = new MacroEvent(course, macro); + qApp->postEvent(qApp, event); + qApp->processEvents(); // To prevent QT from closing when no windows are present } -- cgit v1.2.3