6 #include "plaqui/server/string.h"
8 #include "item_conduct.h"
9 #include "item_exclusa.h"
10 #include "item_tank.h"
11 #include "item_pump.h"
12 #include "item_union.h"
13 #include "item_drain.h"
16 Principal::Principal(BaseObjectType *co, const Glib::RefPtr<Gnome::Glade::Xml> &rg):Gtk::Window(co),refXml(rg)
18 Gtk::MenuItem *conect=0, *exit=0, *about=0, *mnu_prop=0, *mnu_disconnect=0;
19 Gtk::Button *btn_get=0, *bar_connect=0;
21 txt_target = txt_command = txt_args = 0;
23 lbl_cap_flujo = lbl_cap_extra = lbl_extra = lbl_nombre = lbl_color = lbl_flujo = 0;
25 rg->get_widget("btn_activar", btn_activar);
26 rg->get_widget("color_preview", color_preview);
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) );
54 btn_activar->signal_clicked().connect( SigC::slot(*this, &Principal::on_btn_activar_clicked) );
57 is_xml_loaded = false;
59 update_ui.connect( SigC::slot(*this, &Principal::update_items_prop ) );
60 load_xml_dispatch.connect( SigC::slot(*this, &Principal::loadXML ) );
63 Principal::~Principal()
69 void Principal::on_dlg_connect_ok()
71 if (conexion == NULL) {
74 conexion = new PlaQui::Server::ControlClient(dlg_conectar->get_server_name(), dlg_conectar->get_server_port());
77 txt_view->get_buffer()->insert_at_cursor("NO SE PUDO CREAR OBJETO\n");
83 // Conecto las señales
84 conexion->signal_ok_received().connect( SigC::slot(*this, &Principal::on_conexion_ok) );
85 conexion->signal_error_received().connect( SigC::slot(*this, &Principal::on_conexion_error) );
86 conexion->signal_connected().connect( SigC::slot(*this, &Principal::on_conexion_connected) );
87 conexion->signal_finished().connect( SigC::slot(*this, &Principal::on_conexion_finished) );
88 conexion->signal_frame_received().connect(SigC::slot(*this, &Principal::on_conexion_frame));
92 txt_view->get_buffer()->insert_at_cursor("YA ESTAS CONECTADO\n");
97 void Principal::on_mnu_file_exit()
102 void Principal::on_btn_activar_clicked()
104 if ((conexion == NULL) || (last_selected == NULL)) return;
106 PlaQui::Server::Command c("plant", "set");
107 c.add_arg("default");
108 c.add_arg(last_selected->get_name());
111 if (last_selected->get_open())
119 void Principal::on_mnu_file_disconnect()
121 if (conexion == NULL) return;
123 PlaQui::Server::Command c("connection", "stop");
124 c.add_arg(conexion->get_host());
129 void Principal::on_mnu_file_connect()
131 dlg_conectar->show();
134 void Principal::on_mnu_help_about()
136 // preparo para leer el archivo ChangeLog
137 /* Glib::RefPtr<Gnome::Glade::Xml> xml;
139 Glib::RefPtr<Gtk::TextBuffer> log_buffer;
140 Glib::RefPtr<Glib::IOChannel> log_io;
142 log_buffer = Gtk::TextBuffer::create();
143 log_io = Glib::IOChannel::create_from_file("../ChangeLog", "r");
144 while (log_io->read_line(line) != Glib::IO_STATUS_EOF) {
145 log_buffer->insert_at_cursor(line);
149 xml = Gnome::Glade::Xml::create("client.glade", "dlgAbout");
151 catch(const Gnome::Glade::XmlError &ex) {
152 std::cerr << ex.what() << std::endl;
155 Gtk::Window *dlg = 0;
156 Gtk::Button *btn_cerrar = 0;
157 Gtk::TextView *txt_changelog = 0;
158 xml->get_widget("dlgAbout", dlg);
159 xml->get_widget("btn_close", btn_cerrar);
160 xml->get_widget("txt_changelog", txt_changelog);
161 btn_cerrar->signal_clicked().connect(SigC::slot(*dlg, &Gtk::Dialog::hide));
162 txt_changelog->set_buffer(log_buffer);
166 bool Principal::on_item_clicked(GdkEventButton *e, ViewItem *i)
169 txt_view->get_buffer()->insert_at_cursor("Selecciono ");
170 txt_view->get_buffer()->insert_at_cursor(i->get_name());
171 txt_view->get_buffer()->insert_at_cursor("\n");
177 void Principal::update_items_prop()
179 if (last_selected == NULL) return;
181 lbl_nombre->set_text(last_selected->get_name());
182 lbl_flujo->set_text(last_selected->get_actual_flow());
183 lbl_extra->set_text(last_selected->get_extra());
185 lbl_cap_flujo->set_text(last_selected->get_cap_flow());
186 lbl_cap_extra->set_text(last_selected->get_cap_extra());
188 color_preview->modify_bg(Gtk::STATE_NORMAL, last_selected->get_color());
189 color_preview->queue_draw();
192 void Principal::on_conexion_connected()
194 txt_view->get_buffer()->insert_at_cursor("CONNECTED\n");
195 ico_conected->set( Gtk::Stock::YES , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
197 // Pido la planta por defecto
198 if (conexion != NULL) {
199 PlaQui::Server::Command c("plant", "get");
200 c.add_arg("default");
205 void Principal::on_conexion_frame(const std::string &frame)
207 if (conexion != NULL) {
208 read_status_xml(frame);
212 void Principal::on_conexion_finished()
214 txt_view->get_buffer()->insert_at_cursor("HANG UP\n");
215 ico_conected->set( Gtk::Stock::NO , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
219 void Principal::on_conexion_ok(const std::string &body)
221 /* lo paso a la carga del XML */
222 /* verifico que body este completo */
223 if ((body.find("</planta>")>0) && (body.find("<planta>")>0)) {
228 std::cout << body << std::endl;
229 txt_view->get_buffer()->insert_at_cursor("<IN>\n");
230 txt_view->get_buffer()->insert_at_cursor(Glib::locale_to_utf8(body));
231 txt_view->get_buffer()->insert_at_cursor("</IN>\n");
235 void Principal::on_conexion_error(unsigned code)
241 txt_view->get_buffer()->insert_at_cursor("El server dice que hay error : ");
242 txt_view->get_buffer()->insert_at_cursor(s);
243 txt_view->get_buffer()->insert_at_cursor("\n");
246 void Principal::on_get_clicked()
248 if (conexion == NULL) {
249 txt_view->get_buffer()->insert_at_cursor("SIN CONEXION\n");
253 PlaQui::Server::Command command(txt_target->get_text(), txt_command->get_text());
254 command.add_arg( txt_args->get_text() );
255 txt_view->get_buffer()->insert_at_cursor("Enviando comando\n");
257 conexion->send(command);
260 txt_view->get_buffer()->insert_at_cursor("EXCEPTION EN conexion->send !!\n");
265 void Principal::loadXML()
268 if (is_xml_loaded) return;
270 /* Parseo de ejemplo de un XML desde archivo */
272 document = xmlParseMemory(xml_body.c_str(),xml_body.size());
273 if (document == NULL) {
274 std::cout << "EEERRRRRRROOOOOOOOOO" << std::endl;
277 is_xml_loaded = true;
278 /* bien, el archivo se parseo bien! */
279 xmlNodePtr nodo, items;
280 nodo = document->children;
282 if (strcmp((char *)nodo->name, "planta") == 0) {
283 items = nodo->children;
284 while (items != NULL) {
285 if (items->type == XML_ELEMENT_NODE) {
286 if (xmlStrcmp(items->name, BAD_CAST"bomba")==0) {
288 } else if (xmlStrcmp(items->name, BAD_CAST"codo")==0) {
290 } else if (xmlStrcmp(items->name, BAD_CAST"tubo")==0) {
292 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
294 } else if (xmlStrcmp(items->name, BAD_CAST"tanque")==0) {
296 } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
298 } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
307 // Ya cargado el XML, mando un msg para empezar a recibir los frames!
308 PlaQui::Server::Command c("transmission", "start");
309 c.add_arg("default");
310 c.add_arg(conexion->get_host());
315 void Principal::loadBomba(xmlNodePtr nodo)
317 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
318 int orientacion=0, x, y;
320 nodo = nodo->children;
321 while (nodo != NULL) {
322 if (nodo->type == XML_ELEMENT_NODE) {
323 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
324 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
325 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
326 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
327 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
328 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
334 // listo, ya recolecte todos los datos, ahora creo el objeto!
335 ViewPump *b = new ViewPump(name, orientacion);
336 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
337 b->set_position(x,y);
338 work_place->put(*b, x, y);
340 // los agrego al hash
344 void Principal::loadCodo(xmlNodePtr nodo)
346 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
347 int orientacion=0, x, y;
349 nodo = nodo->children;
350 while (nodo != NULL) {
351 if (nodo->type == XML_ELEMENT_NODE) {
352 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
353 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
354 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
355 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
356 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
357 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
363 // listo, ya recolecte todos los datos, ahora creo el objeto!
364 ViewItem *b = new ViewCodo(name, orientacion);
365 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
366 b->set_position(x,y);
367 work_place->put(*b, x, y);
369 // los agrego al hash
373 void Principal::loadConduct(xmlNodePtr nodo)
375 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
376 int orientacion=0, x, y;
378 nodo = nodo->children;
379 while (nodo != NULL) {
380 if (nodo->type == XML_ELEMENT_NODE) {
381 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
382 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
383 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
384 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
385 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
386 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
392 // listo, ya recolecte todos los datos, ahora creo el objeto!
393 ViewConduct *b = new ViewConduct(name, orientacion);
394 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
395 b->set_position(x,y);
396 work_place->put(*b, x, y);
398 // los agrego al hash
402 void Principal::loadExclusa(xmlNodePtr nodo)
404 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
405 int orientacion=0, x, y;
407 nodo = nodo->children;
408 while (nodo != NULL) {
409 if (nodo->type == XML_ELEMENT_NODE) {
410 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
411 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
412 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
413 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
414 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
415 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
421 // listo, ya recolecte todos los datos, ahora creo el objeto!
422 ViewExclusa *b = new ViewExclusa(name, orientacion);
423 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
424 b->set_position(x,y);
425 work_place->put(*b, x, y);
427 // los agrego al hash
431 void Principal::loadTank(xmlNodePtr nodo)
433 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
434 int orientacion=0, x, y;
436 nodo = nodo->children;
437 while (nodo != NULL) {
438 if (nodo->type == XML_ELEMENT_NODE) {
439 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
440 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
441 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
442 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
443 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
444 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
450 // listo, ya recolecte todos los datos, ahora creo el objeto!
451 ViewTank *b = new ViewTank(name, orientacion);
452 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
453 b->set_position(x,y);
454 work_place->put(*b, x, y);
456 // los agrego al hash
460 void Principal::loadUnion(xmlNodePtr nodo)
462 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
463 int orientacion=0, x, y;
465 nodo = nodo->children;
466 while (nodo != NULL) {
467 if (nodo->type == XML_ELEMENT_NODE) {
468 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
469 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
470 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
471 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
472 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
473 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
479 // listo, ya recolecte todos los datos, ahora creo el objeto!
480 ViewUnion *b = new ViewUnion(name, orientacion);
481 b->signal_button_release_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
482 b->set_position(x,y);
483 work_place->put(*b, x, y);
485 // los agrego al hash
489 void Principal::loadDrain(xmlNodePtr nodo)
491 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
492 int orientacion=0, x, y;
494 nodo = nodo->children;
495 while (nodo != NULL) {
496 if (nodo->type == XML_ELEMENT_NODE) {
497 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
498 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
499 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
500 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
501 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
502 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
508 // listo, ya recolecte todos los datos, ahora creo el objeto!
509 ViewDrain *b = new ViewDrain(name, orientacion);
510 b->signal_button_release_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
511 b->set_position(x,y);
512 work_place->put(*b, x, y);
514 // los agrego al hash
518 void Principal::read_status_xml(const std::string &frame)
520 std::string item_name;
522 document = xmlParseMemory(frame.c_str(),frame.size());
523 if (document == NULL) {
524 std::cout << "read_status_xml::no se creo documento" << std::endl;
528 xmlNodePtr nodo, items, props;
529 nodo = document->children;
533 if (strcmp((char *)nodo->name, "plantstatus") == 0) {
534 items = nodo->children;
535 while (items != NULL) {
536 if (items->type == XML_ELEMENT_NODE) {
539 if (xmlStrcmp(items->name, BAD_CAST"float")==0) {
540 tmp = get_float_from_xml(items->children);
541 item_name = (char *)xmlGetProp(items, BAD_CAST"name");
542 mapItems[item_name]->set_actual_flow(tmp);
543 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
544 tmp_b = get_bool_from_xml(items->children);
545 item_name = (char *)xmlGetProp(items, BAD_CAST"name");
546 mapItems[item_name]->set_open(tmp_b);
547 } else if (xmlStrcmp(items->name, BAD_CAST"pump")==0) {
548 tmp_b = get_bool_from_xml(items->children);
549 item_name = (char *)xmlGetProp(items, BAD_CAST"name");
550 mapItems[item_name]->set_open(tmp_b);
551 } else if (xmlStrcmp(items->name, BAD_CAST"color")==0) {
552 item_name = (char *)xmlGetProp(items, BAD_CAST"name");
553 mapItems[item_name]->set_color( get_rgb_from_xml(items->children) );
554 } else if (xmlStrcmp(items->name, BAD_CAST"tank")==0) {
555 xmlNodePtr nodo_tmp = items->children;
558 while (nodo_tmp != NULL) {
559 if (nodo_tmp->type == XML_ELEMENT_NODE) {
560 if (xmlStrcmp(nodo_tmp->name, BAD_CAST"capacity")==0)
561 cap = atof( (char *)XML_GET_CONTENT(nodo_tmp->children) );
562 else if (xmlStrcmp(nodo_tmp->name, BAD_CAST"litros")==0)
563 lit= atof( (char *)XML_GET_CONTENT(nodo_tmp->children) );
565 nodo_tmp = nodo_tmp->next;
567 item_name = (char *)xmlGetProp(items, BAD_CAST"name");
568 mapItems[item_name]->set_actual_flow(cap);
569 mapItems[item_name]->set_extra(lit);
580 Gdk::Color Principal::get_rgb_from_xml(xmlNodePtr nodo)
583 while (nodo != NULL) {
584 if (nodo->type == XML_ELEMENT_NODE) {
585 if (xmlStrcmp(nodo->name, BAD_CAST"r")==0)
586 r = atoi( (char *)XML_GET_CONTENT(nodo->children) );
587 if (xmlStrcmp(nodo->name, BAD_CAST"g")==0)
588 g = atoi( (char *)XML_GET_CONTENT(nodo->children) );
589 if (xmlStrcmp(nodo->name, BAD_CAST"b")==0)
590 b = atoi( (char *)XML_GET_CONTENT(nodo->children) );
602 float Principal::get_float_from_xml(xmlNodePtr nodo)
605 while (nodo != NULL) {
606 if (nodo->type == XML_ELEMENT_NODE) {
607 if (xmlStrcmp(nodo->name, BAD_CAST"actual_flow")==0) {
608 tmp = atof( (char *)XML_GET_CONTENT(nodo->children) );
617 bool Principal::get_bool_from_xml(xmlNodePtr nodo)
620 while (nodo != NULL) {
621 if (nodo->type == XML_ELEMENT_NODE) {
622 if (xmlStrcmp(nodo->name, BAD_CAST"active")==0) {
623 tmp = (char *)XML_GET_CONTENT(nodo->children);
629 return tmp == "true";