diff options
-rw-r--r-- | client/formatparser.cc | 18 | ||||
-rw-r--r-- | client/formatparser.h | 2 | ||||
-rw-r--r-- | client/lua.cc | 92 | ||||
-rw-r--r-- | client/lua.h | 12 | ||||
-rw-r--r-- | client/macrowindow.cc | 2 | ||||
-rw-r--r-- | client/widgets/metawidget.cc | 8 | ||||
-rw-r--r-- | client/widgets/metawidget.h | 1 | ||||
-rw-r--r-- | client/widgets/widget.cc | 5 |
8 files changed, 116 insertions, 24 deletions
diff --git a/client/formatparser.cc b/client/formatparser.cc index 0f04d0a..751c83e 100644 --- a/client/formatparser.cc +++ b/client/formatparser.cc @@ -30,7 +30,16 @@ #include <QVariant> #include <string.h> -QString format_parser(QString format, QVector< Widget *> widgets) +#include "lua.h" + +static QString format_parser_lua(QString format, QVector< Widget *> widgets) +{ + LUA lua(&widgets); + + return lua.runParser(format); +} + +static QString format_parser_pracro(QString format, QVector< Widget *> widgets) { QString resume; QString var; @@ -172,3 +181,10 @@ QString format_parser(QString format, QSqlQuery &query) return resume; } + +QString format_parser(QString format, QVector< Widget *> widgets, QString language) +{ + if(language == "pracro") return format_parser_pracro(format, widgets); + if(language == "lua") return format_parser_lua(format, widgets); + return ""; +} diff --git a/client/formatparser.h b/client/formatparser.h index 5d900b0..92f978f 100644 --- a/client/formatparser.h +++ b/client/formatparser.h @@ -32,7 +32,7 @@ #include "widgets/widget.h" #include <QSqlQuery> -QString format_parser(QString format, QVector< Widget *> widgets); +QString format_parser(QString format, QVector< Widget *> widgets, QString language); QString format_parser(QString format, QSqlQuery &query); #endif/*__PRACRO_FORMATPARSER_H__*/ diff --git a/client/lua.cc b/client/lua.cc index 2dcd279..5e6850b 100644 --- a/client/lua.cc +++ b/client/lua.cc @@ -175,9 +175,10 @@ static int _setVisible(lua_State *L) } -LUA::LUA(MacroWindow *macrowindow) +LUA::LUA(QVector< Widget *> *widgets, QVector< Widget *> *auxwidgets) { - this->macrowindow = macrowindow; + this->widgets = widgets; + this->auxwidgets = auxwidgets; L = luaL_newstate(); if(L == NULL) { @@ -204,36 +205,36 @@ LUA::~LUA() QString LUA::getValue(QString name) { - Widget *widget = macrowindow->getWidget(name); + Widget *widget = getWidget(name); if(widget) return widget->getValue(); return ""; } void LUA::setValue(QString name, QString value) { - Widget *widget = macrowindow->getWidget(name); + Widget *widget = getWidget(name); if(widget) widget->setValue(value); } void LUA::enable(QString name) { - Widget *widget = macrowindow->getWidget(name); + Widget *widget = getWidget(name); if(widget) widget->enable(); } void LUA::disable(QString name) { - Widget *widget = macrowindow->getWidget(name); + Widget *widget = getWidget(name); if(widget) widget->disable(); } void LUA::setVisible(QString name, bool value) { - Widget *widget = macrowindow->getWidget(name); + Widget *widget = getWidget(name); if(widget) widget->setVisibility(value); } -bool LUA::run(QString program, QString name, QString value) +bool LUA::runValidator(QString program, QString name, QString value) { if(L == NULL) { error("LUA state not initialized!"); @@ -245,10 +246,6 @@ bool LUA::run(QString program, QString name, QString value) name.toStdString().c_str(), value.toStdString().c_str() ); - if(macrowindow->luaprograms.contains(program) == false) return false; - - // printf("%s\n", macrowindow->luaprograms.value(program).toStdString().c_str()); - lua_pushstring(L, value.toStdString().c_str()); lua_setglobal(L, "value"); @@ -257,11 +254,10 @@ bool LUA::run(QString program, QString name, QString value) int top = lua_gettop(L); - QString luaprogram = macrowindow->luaprograms.value(program); if(luaL_loadbuffer(L, - luaprogram.toStdString().c_str(), - luaprogram.size(), - program.toStdString().c_str())) { + program.toStdString().c_str(), + program.size(), + name.toStdString().c_str())) { error(lua_tostring(L, lua_gettop(L))); return false; } @@ -288,7 +284,71 @@ bool LUA::run(QString program, QString name, QString value) return res; } +QString LUA::runParser(QString program) +{ + if(L == NULL) { + error("LUA state not initialized!"); + return false; + } + + printf("Running %s\n", program.toStdString().c_str()); + + int top = lua_gettop(L); + + if(luaL_loadbuffer(L, + program.toStdString().c_str(), + program.size(), + "parser")) { + error(lua_tostring(L, lua_gettop(L))); + return false; + } + + // Run the loaded code + if(lua_pcall(L, 0, LUA_MULTRET, 0)) { + error(lua_tostring(L, lua_gettop(L))); + return false; + } + + if(top != lua_gettop(L) - 1) { + error("Program did not return a single value.\n"); + return false; + } + + if(lua_isstring(L, lua_gettop(L)) == false) { + error("Program did not return a boolean value.\n"); + return false; + } + + QString res = lua_tostring(L, lua_gettop(L)); + lua_pop(L, 1); + + return res; +} + void LUA::error(QString message) { printf("LUA ERROR: %s\n", message.toStdString().c_str()); } + +Widget *LUA::getWidget(QString name) +{ + QVector< Widget* >::iterator i = widgets->begin(); + while (i != widgets->end()) { + Widget* w = *i; + if(name == w->getName()) return w; + i++; + } + + if(auxwidgets) { + QVector< Widget* >::iterator j = auxwidgets->begin(); + while (j != auxwidgets->end()) { + Widget* w = *j; + if(name == w->getName()) return w; + j++; + } + } + + printf("WARNING: Widget %s not found\n", name.toStdString().c_str()); + + return NULL; +} diff --git a/client/lua.h b/client/lua.h index 4777dd4..ba074dd 100644 --- a/client/lua.h +++ b/client/lua.h @@ -32,14 +32,17 @@ #include <QString> +#include "widgets/widget.h" + class MacroWindow; class LUA { public: - LUA(MacroWindow *macrowindow); + LUA(QVector< Widget *> *widgets, QVector< Widget *> *auxwidgets = NULL); ~LUA(); - bool run(QString program, QString name, QString value); + bool runValidator(QString program, QString name, QString value); + QString runParser(QString program); QString getValue(QString name); void setValue(QString name, QString value); @@ -49,9 +52,12 @@ public: void error(QString message); + Widget *getWidget(QString name); + private: lua_State *L; - MacroWindow *macrowindow; + QVector< Widget *> *widgets; + QVector< Widget *> *auxwidgets; }; #endif/*__PRACRO_LUA_H__*/ diff --git a/client/macrowindow.cc b/client/macrowindow.cc index 2206929..6beba85 100644 --- a/client/macrowindow.cc +++ b/client/macrowindow.cc @@ -54,7 +54,7 @@ MacroWindow::MacroWindow(NetCom *netcom, QDomNode &xml_doc, QString course, setCollapsedWidget(new ResumeWidget(compact)); - this->lua = new LUA(this); + this->lua = new LUA(&this->widgets, &this->auxwidgets); update(xml_doc); diff --git a/client/widgets/metawidget.cc b/client/widgets/metawidget.cc index 5102a04..c085f10 100644 --- a/client/widgets/metawidget.cc +++ b/client/widgets/metawidget.cc @@ -55,6 +55,12 @@ MetaWidget::MetaWidget(QDomNode &node, MacroWindow *macrowindow) else macrowindow->addAuxWidgets(widgets); // Setup format string + if(elem.hasAttribute("formatlanguage")) { + formatlanguage = elem.attribute("formatlanguage"); + } else { + formatlanguage = "pracro"; + } + if(elem.hasAttribute("format")) { format = elem.attribute("format"); } else { @@ -83,7 +89,7 @@ void MetaWidget::changed() QString MetaWidget::getValue() { - return format_parser(format, widgets); + return format_parser(format, widgets, formatlanguage); } void MetaWidget::setValue(QString, QString) diff --git a/client/widgets/metawidget.h b/client/widgets/metawidget.h index 77b79d7..022197a 100644 --- a/client/widgets/metawidget.h +++ b/client/widgets/metawidget.h @@ -70,6 +70,7 @@ private: QListWidget *list; QVector< Widget* > widgets; QString format; + QString formatlanguage; bool storechildren; }; diff --git a/client/widgets/widget.cc b/client/widgets/widget.cc index 0857d67..870fc4b 100644 --- a/client/widgets/widget.cc +++ b/client/widgets/widget.cc @@ -81,8 +81,11 @@ bool Widget::regexpValidator() bool Widget::luaValidator() { if(!hasluaprogram) return true; + + if(macrowindow->luaprograms.contains(luaprogram) == false) return false; + QString program = macrowindow->luaprograms.value(luaprogram); - return macrowindow->lua->run(luaprogram, getName(), getValue()); + return macrowindow->lua->runValidator(program, getName(), getValue()); } void Widget::setInitialValue(QString value) |