8 #include "plaqui/server/string.h"
10 #include "item_conduct.h"
11 #include "item_exclusa.h"
12 #include "item_tank.h"
13 #include "item_pump.h"
14 #include "item_union.h"
15 #include "item_drain.h"
18 Principal::Principal(BaseObjectType *co, const Glib::RefPtr<Gnome::Glade::Xml> &rg):Gtk::Window(co),refXml(rg)
20 Gtk::MenuItem *conect=0, *exit=0, *about=0, *mnu_prop=0, *mnu_disconnect;
21 Gtk::Button *btn_get=0, *bar_connect=0;
23 txt_target = txt_command = txt_args = 0;
25 lbl_cap_flujo = lbl_cap_extra = lbl_extra = lbl_nombre = lbl_color = lbl_flujo = 0;
27 rg->get_widget("lbl_nombre", lbl_nombre);
28 rg->get_widget("lbl_extra", lbl_extra);
29 rg->get_widget("lbl_cap_extra", lbl_cap_extra);
30 rg->get_widget("lbl_cap_flujo", lbl_cap_flujo);
31 rg->get_widget("lbl_flujo", lbl_flujo);
32 rg->get_widget("mnu_file_connect", conect);
33 rg->get_widget("mnu_file_disconnect", mnu_disconnect);
34 rg->get_widget("mnu_file_exit", exit);
35 rg->get_widget("mnu_help_about", about);
36 rg->get_widget("mnu_prop", mnu_prop);
37 rg->get_widget_derived("dlgConectar", dlg_conectar);
38 rg->get_widget("btn_get", btn_get);
39 rg->get_widget("txt_view", txt_view);
40 rg->get_widget("txt_target", txt_target);
41 rg->get_widget("txt_command", txt_command);
42 rg->get_widget("txt_args", txt_args);
43 rg->get_widget("bar_connect", bar_connect);
44 rg->get_widget("work_place", work_place);
45 rg->get_widget("ico_conected", ico_conected);
47 dlg_conectar->get_ok_button()->signal_clicked().connect( SigC::slot(*this, &Principal::on_dlg_connect_ok) );
48 mnu_disconnect->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_file_disconnect));
49 conect->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_file_connect));
50 bar_connect->signal_clicked().connect( SigC::slot(*this, &Principal::on_mnu_file_connect));
51 exit->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_file_exit));
52 about->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_help_about));
53 btn_get->signal_clicked().connect( SigC::slot(*this, &Principal::on_get_clicked) );
56 is_xml_loaded = false;
58 load_xml_dispatch.connect( SigC::slot(*this, &Principal::loadXML ) );
61 Principal::~Principal()
67 void Principal::on_dlg_connect_ok()
69 if (conexion == NULL) {
72 conexion = new PlaQui::Server::ControlClient(dlg_conectar->get_server_name(), dlg_conectar->get_server_port());
75 txt_view->get_buffer()->insert_at_cursor("NO SE PUDO CREAR OBJETO\n");
81 // Conecto las señales
82 conexion->signal_ok_received().connect( SigC::slot(*this, &Principal::on_conexion_ok) );
83 conexion->signal_error_received().connect( SigC::slot(*this, &Principal::on_conexion_error) );
84 conexion->signal_connected().connect( SigC::slot(*this, &Principal::on_conexion_connected) );
85 conexion->signal_finished().connect( SigC::slot(*this, &Principal::on_conexion_finished) );
86 conexion->signal_frame_received().connect(SigC::slot(*this, &Principal::on_conexion_frame));
90 txt_view->get_buffer()->insert_at_cursor("YA ESTAS CONECTADO\n");
95 void Principal::on_mnu_file_exit()
100 void Principal::on_mnu_file_disconnect()
102 if (conexion == NULL) return;
104 PlaQui::Server::Command c("transmission", "stop");
105 c.add_arg(conexion->get_host());
111 void Principal::on_mnu_file_connect()
113 dlg_conectar->show();
116 void Principal::on_mnu_help_about()
118 // preparo para leer el archivo ChangeLog
119 /* Glib::RefPtr<Gnome::Glade::Xml> xml;
121 Glib::RefPtr<Gtk::TextBuffer> log_buffer;
122 Glib::RefPtr<Glib::IOChannel> log_io;
124 log_buffer = Gtk::TextBuffer::create();
125 log_io = Glib::IOChannel::create_from_file("../ChangeLog", "r");
126 while (log_io->read_line(line) != Glib::IO_STATUS_EOF) {
127 log_buffer->insert_at_cursor(line);
131 xml = Gnome::Glade::Xml::create("client.glade", "dlgAbout");
133 catch(const Gnome::Glade::XmlError &ex) {
134 std::cerr << ex.what() << std::endl;
137 Gtk::Window *dlg = 0;
138 Gtk::Button *btn_cerrar = 0;
139 Gtk::TextView *txt_changelog = 0;
140 xml->get_widget("dlgAbout", dlg);
141 xml->get_widget("btn_close", btn_cerrar);
142 xml->get_widget("txt_changelog", txt_changelog);
143 btn_cerrar->signal_clicked().connect(SigC::slot(*dlg, &Gtk::Dialog::hide));
144 txt_changelog->set_buffer(log_buffer);
148 bool Principal::on_item_clicked(GdkEventButton *e, ViewItem *i)
150 lbl_nombre->set_text(i->get_name());
151 lbl_flujo->set_text(i->get_actual_flow());
152 lbl_extra->set_text(i->get_extra());
154 lbl_cap_flujo->set_text(i->get_cap_flow());
155 lbl_cap_extra->set_text(i->get_cap_extra());
157 txt_view->get_buffer()->insert_at_cursor("Selecciono ");
158 txt_view->get_buffer()->insert_at_cursor(i->get_name());
159 txt_view->get_buffer()->insert_at_cursor("\n");
162 void Principal::on_conexion_connected()
164 txt_view->get_buffer()->insert_at_cursor("CONNECTED\n");
165 ico_conected->set( Gtk::Stock::YES , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
167 // Pido la planta por defecto
168 if (conexion != NULL) {
169 PlaQui::Server::Command c("plant", "get");
170 c.add_arg("default");
175 void Principal::on_conexion_frame(const std::string &frame)
177 read_status_xml(frame);
180 void Principal::on_conexion_finished()
182 txt_view->get_buffer()->insert_at_cursor("HANG UP\n");
183 ico_conected->set( Gtk::Stock::NO , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
187 void Principal::on_conexion_ok(const std::string &body)
189 /* lo paso a la carga del XML */
190 /* verifico que body este completo */
191 if ((body.find("</planta>")>0) && (body.find("<planta>")>0)) {
196 std::cout << body << std::endl;
197 txt_view->get_buffer()->insert_at_cursor("<IN>\n");
198 txt_view->get_buffer()->insert_at_cursor(Glib::locale_to_utf8(body));
199 txt_view->get_buffer()->insert_at_cursor("</IN>\n");
203 void Principal::on_conexion_error(unsigned code)
209 txt_view->get_buffer()->insert_at_cursor("El server dice que hay error : ");
210 txt_view->get_buffer()->insert_at_cursor(s);
211 txt_view->get_buffer()->insert_at_cursor("\n");
214 void Principal::on_get_clicked()
216 if (conexion == NULL) {
217 txt_view->get_buffer()->insert_at_cursor("SIN CONEXION\n");
221 PlaQui::Server::Command command(txt_target->get_text(), txt_command->get_text());
222 command.add_arg( txt_args->get_text() );
223 txt_view->get_buffer()->insert_at_cursor("Enviando comando\n");
225 conexion->send(command);
228 txt_view->get_buffer()->insert_at_cursor("EXCEPTION EN conexion->send !!\n");
233 void Principal::loadXML()
236 if (is_xml_loaded) return;
238 /* Parseo de ejemplo de un XML desde archivo */
240 document = xmlParseMemory(xml_body.c_str(),xml_body.size());
241 if (document == NULL) {
242 std::cout << "EEERRRRRRROOOOOOOOOO" << std::endl;
245 is_xml_loaded = true;
246 /* bien, el archivo se parseo bien! */
247 xmlNodePtr nodo, items;
248 nodo = document->children;
250 if (strcmp((char *)nodo->name, "planta") == 0) {
251 items = nodo->children;
252 while (items != NULL) {
253 if (items->type == XML_ELEMENT_NODE) {
254 if (xmlStrcmp(items->name, BAD_CAST"bomba")==0) {
256 } else if (xmlStrcmp(items->name, BAD_CAST"codo")==0) {
258 } else if (xmlStrcmp(items->name, BAD_CAST"tubo")==0) {
260 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
262 } else if (xmlStrcmp(items->name, BAD_CAST"tanque")==0) {
264 } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
266 } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
275 // Ya cargado el XML, mando un msg para empezar a recibir los frames!
276 PlaQui::Server::Command c("transmission", "start");
277 c.add_arg("default");
278 c.add_arg(conexion->get_host());
283 void Principal::loadBomba(xmlNodePtr nodo)
285 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
286 int orientacion=0, x, y;
288 nodo = nodo->children;
289 while (nodo != NULL) {
290 if (nodo->type == XML_ELEMENT_NODE) {
291 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
292 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
293 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
294 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
295 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
296 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
302 // listo, ya recolecte todos los datos, ahora creo el objeto!
303 ViewPump *b = new ViewPump(name, orientacion);
304 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
305 b->set_position(x,y);
306 work_place->put(*b, x, y);
308 // los agrego al hash
312 void Principal::loadCodo(xmlNodePtr nodo)
314 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
315 int orientacion=0, x, y;
317 nodo = nodo->children;
318 while (nodo != NULL) {
319 if (nodo->type == XML_ELEMENT_NODE) {
320 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
321 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
322 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
323 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
324 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
325 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
331 // listo, ya recolecte todos los datos, ahora creo el objeto!
332 ViewItem *b = new ViewCodo(name, orientacion);
333 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
334 b->set_position(x,y);
335 work_place->put(*b, x, y);
337 // los agrego al hash
341 void Principal::loadConduct(xmlNodePtr nodo)
343 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
344 int orientacion=0, x, y;
346 nodo = nodo->children;
347 while (nodo != NULL) {
348 if (nodo->type == XML_ELEMENT_NODE) {
349 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
350 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
351 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
352 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
353 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
354 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
360 // listo, ya recolecte todos los datos, ahora creo el objeto!
361 ViewConduct *b = new ViewConduct(name, orientacion);
362 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
363 b->set_position(x,y);
364 work_place->put(*b, x, y);
366 // los agrego al hash
370 void Principal::loadExclusa(xmlNodePtr nodo)
372 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
373 int orientacion=0, x, y;
375 nodo = nodo->children;
376 while (nodo != NULL) {
377 if (nodo->type == XML_ELEMENT_NODE) {
378 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
379 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
380 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
381 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
382 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
383 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
389 // listo, ya recolecte todos los datos, ahora creo el objeto!
390 ViewExclusa *b = new ViewExclusa(name, orientacion);
391 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
392 b->set_position(x,y);
393 work_place->put(*b, x, y);
395 // los agrego al hash
399 void Principal::loadTank(xmlNodePtr nodo)
401 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
402 int orientacion=0, x, y;
404 nodo = nodo->children;
405 while (nodo != NULL) {
406 if (nodo->type == XML_ELEMENT_NODE) {
407 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
408 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
409 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
410 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
411 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
412 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
418 // listo, ya recolecte todos los datos, ahora creo el objeto!
419 ViewTank *b = new ViewTank(name, orientacion);
420 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
421 b->set_position(x,y);
422 work_place->put(*b, x, y);
424 // los agrego al hash
428 void Principal::loadUnion(xmlNodePtr nodo)
430 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
431 int orientacion=0, x, y;
433 nodo = nodo->children;
434 while (nodo != NULL) {
435 if (nodo->type == XML_ELEMENT_NODE) {
436 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
437 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
438 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
439 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
440 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
441 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
447 // listo, ya recolecte todos los datos, ahora creo el objeto!
448 ViewUnion *b = new ViewUnion(name, orientacion);
449 b->signal_button_release_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
450 b->set_position(x,y);
451 work_place->put(*b, x, y);
453 // los agrego al hash
457 void Principal::loadDrain(xmlNodePtr nodo)
459 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
460 int orientacion=0, x, y;
462 nodo = nodo->children;
463 while (nodo != NULL) {
464 if (nodo->type == XML_ELEMENT_NODE) {
465 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
466 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
467 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
468 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
469 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
470 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
476 // listo, ya recolecte todos los datos, ahora creo el objeto!
477 ViewDrain *b = new ViewDrain(name, orientacion);
478 b->signal_button_release_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
479 b->set_position(x,y);
480 work_place->put(*b, x, y);
482 // los agrego al hash
486 void Principal::read_status_xml(const std::string &frame)
488 std::string item_name;
490 document = xmlParseMemory(frame.c_str(),frame.size());
491 if (document == NULL) {
492 std::cout << "read_status_xml::no se creo documento" << std::endl;
496 xmlNodePtr nodo, items, props;
497 nodo = document->children;
501 if (strcmp((char *)nodo->name, "plantstatus") == 0) {
502 std::cout << "LEGO EL XML!" << std::endl;
503 items = nodo->children;
504 while (items != NULL) {
505 if (items->type == XML_ELEMENT_NODE) {
508 if (xmlStrcmp(items->name, BAD_CAST"float")==0) {
509 tmp = get_float_from_xml(items->children);
510 item_name = (char *)xmlGetProp(items, BAD_CAST"name");
511 mapItems[item_name]->set_actual_flow(tmp);
512 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
513 tmp_b = get_bool_from_xml(items->children);
514 item_name = (char *)xmlGetProp(items, BAD_CAST"name");
515 mapItems[item_name]->set_open(tmp_b);
524 float Principal::get_float_from_xml(xmlNodePtr nodo)
527 while (nodo != NULL) {
528 if (nodo->type == XML_ELEMENT_NODE) {
529 if (xmlStrcmp(nodo->name, BAD_CAST"actual_flow")==0) {
530 tmp = atof( (char *)XML_GET_CONTENT(nodo->children) );
539 bool Principal::get_bool_from_xml(xmlNodePtr nodo)
542 while (nodo != NULL) {
543 if (nodo->type == XML_ELEMENT_NODE) {
544 if (xmlStrcmp(nodo->name, BAD_CAST"active")==0) {
545 tmp = (char *)XML_GET_CONTENT(nodo->children);
551 return tmp == "true";