summaryrefslogtreecommitdiff
path: root/server/src/widgetgenerator.cc
diff options
context:
space:
mode:
authordeva <deva>2010-07-05 07:00:08 +0000
committerdeva <deva>2010-07-05 07:00:08 +0000
commitca15ae4d605a3bcd11435d92eda0800d28edceb8 (patch)
tree6791e47299d6600d12c27e0123212679705d97fd /server/src/widgetgenerator.cc
parentda6bfc301203b0181dcf70a9798a7d840d514126 (diff)
Partial commit: Move all value prefill code to its own file.
Diffstat (limited to 'server/src/widgetgenerator.cc')
-rw-r--r--server/src/widgetgenerator.cc136
1 files changed, 19 insertions, 117 deletions
diff --git a/server/src/widgetgenerator.cc b/server/src/widgetgenerator.cc
index 425c71e..f257228 100644
--- a/server/src/widgetgenerator.cc
+++ b/server/src/widgetgenerator.cc
@@ -24,131 +24,33 @@
* along with Pracro; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
-#include "debug.h"
#include "widgetgenerator.h"
-#include "configuration.h"
-#include "xml_encode_decode.h"
-
-static std::string automap(std::string name)
-{
- std::string group;
- std::string groupcheck = "if(";
-
- for(size_t i = 0; i < name.length(); i++) {
- group += name[i];
- if(name[i] == '.') groupcheck += " and " + group;
- else groupcheck += name[i];
- }
- groupcheck += " and " + name + ".value and " + name + ".timestamp and " + name + ".source";
- groupcheck += ")\n";
-
- std::string automapstring =
- "-- Returning 0, 0 invalidates the result\n"
- "value = 0\n"
- "timestamp = 0\n"
- "source = 0\n"
- "\n"
- + groupcheck +
- "then\n"
- " value = " + name + ".value\n"
- " timestamp = " + name + ".timestamp\n"
- " source = " + name + ".source\n"
- "end\n"
- "return value, timestamp, source\n";
+#include "widgetvalue.h"
- PRACRO_DEBUG(widget, "Automap:\n%s\n", automapstring.c_str());
-
- return automapstring;
-}
+#include "xml_encode_decode.h"
-static std::string send_macro_widget(Macro &macro,
- Widget &widget,
- std::string tabs,
- LUAQueryMapper &mapper,
- Values &values)
+static std::string getWidgetString(Macro &macro,
+ Widget &widget,
+ std::string tabs,
+ LUAQueryMapper &mapper,
+ Values &values)
{
std::string result;
- std::string prefilled;
- time_t timestamp = 0;
- time_t now = time(NULL);
-
result = tabs + "<" + widget.attributes["tagname"];
- std::map< std::string, std::string >::iterator p = widget.attributes.begin();
+ attr_t::iterator p = widget.attributes.begin();
- PRACRO_DEBUG(prefill, "%s: %s\n",
+ PRACRO_DEBUG(prefill, "TAG: %s - NAME: %s\n",
widget.attributes["tagname"].c_str(),
widget.attributes["name"].c_str());
- PRACRO_DEBUG(prefill, "0: (%s, %s, %d)\n",
- prefilled.c_str(),
- widget.attributes["value"].c_str(),
- (int)timestamp);
-
- // Check if the field has a map, and fill in the value if it has...
- if(widget.attributes.find("map") != widget.attributes.end()) {
- std::string luamap;
-
- std::vector< Map >::iterator li = macro.maps.begin();
- while(li != macro.maps.end()) {
- Map &map = *li;
- if(map.attributes["name"] == widget.attributes["map"]) {
- luamap = map.attributes["lua"];
- }
- li++;
- }
-
- // Check to see if we should automap
- if(luamap == "") {
- luamap = automap(widget.attributes["map"]);
- }
-
- // printf("LUAMAP: %s\n", luamap.c_str()); fflush(stdout);
-
- if(luamap != "") {
- Value value = mapper.map(luamap);
- if(value.timestamp > now - Conf::pentominos_max_ttl) {
- widget.attributes["value"] = value.value;
- timestamp = value.timestamp;
- prefilled = value.source;
- }
-
- PRACRO_DEBUG(prefill, "map: (%s, %d)\n",
- value.value.c_str(),
- (int)value.timestamp);
-
- }
-
- // widget.attributes.erase(widget.attributes.find("map"));
+ Value value;
+ if(getValue(value, widget.attributes, macro.maps, mapper, values)) {
+ widget.attributes["value"] = value.value;
+ if(value.source != "") widget.attributes["prefilled"] = value.source;
}
- PRACRO_DEBUG(prefill, "1: (%s, %s, %d)\n",
- prefilled.c_str(),
- widget.attributes["value"].c_str(),
- (int)timestamp);
-
- // Check if there is a previously stored value in the db...
- if(values.find(widget.attributes["name"]) != values.end()) {
-
- PRACRO_DEBUG(prefill, "db: (%s, %d)\n",
- values[widget.attributes["name"]].value.c_str(),
- (int)values[widget.attributes["name"]].timestamp);
-
- if(values[widget.attributes["name"]].timestamp > timestamp) {
- if(values[widget.attributes["name"]].timestamp > now - Conf::db_max_ttl) {
- widget.attributes["value"] = values[widget.attributes["name"]].value;
- timestamp = values[widget.attributes["name"]].timestamp;
- prefilled = "pracro";
- }
- }
- }
-
- PRACRO_DEBUG(prefill, "2: (%s, %s, %d)\n",
- prefilled.c_str(),
- widget.attributes["value"].c_str(),
- (int)timestamp);
-
while(p != widget.attributes.end()) {
if(p->first != "tagname" && p->first != "map") {
if( ! (p->first == "name" && p->second == "") )
@@ -157,8 +59,6 @@ static std::string send_macro_widget(Macro &macro,
p++;
}
- if(prefilled != "") result += " prefilled=\"" + prefilled + "\"";
-
if(widget.widgets.size() == 0) { // If node is empty, use short tag form
result += "/>\n";
return result;
@@ -168,7 +68,7 @@ static std::string send_macro_widget(Macro &macro,
std::vector< Widget >::iterator w = widget.widgets.begin();
while(w != widget.widgets.end()) {
- result += send_macro_widget(macro, *w, tabs + " ", mapper, values);
+ result += getWidgetString(macro, *w, tabs + " ", mapper, values);
w++;
}
result += tabs + "</" + widget.attributes["tagname"] + ">\n";
@@ -179,7 +79,8 @@ static std::string send_macro_widget(Macro &macro,
static void get_fields(Widget &widget, Fieldnames &fields)
{
if(widget.attributes.find("value") != widget.attributes.end()) {
- if(widget.attributes["name"] != "") fields.push_back(widget.attributes["name"]);
+ if(widget.attributes["name"] != "")
+ fields.push_back(widget.attributes["name"]);
}
std::vector< Widget >::iterator w = widget.widgets.begin();
@@ -189,14 +90,15 @@ static void get_fields(Widget &widget, Fieldnames &fields)
}
}
-std::string widgetgenerator(std::string cpr, Macro &macro, LUAQueryMapper &mapper, Database &db)
+std::string widgetgenerator(std::string cpr, Macro &macro,
+ LUAQueryMapper &mapper, Database &db)
{
Fieldnames fields;
get_fields(macro.widgets, fields);
Values values = db.getValues(cpr, fields);
- return send_macro_widget(macro, macro.widgets, " ", mapper, values);
+ return getWidgetString(macro, macro.widgets, " ", mapper, values);
}
#ifdef TEST_WIDGETGENERATOR