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_nombre = lbl_color = lbl_flujo = 0;
27 rg->get_widget("lbl_nombre", lbl_nombre);
28 rg->get_widget("mnu_file_connect", conect);
29 rg->get_widget("mnu_file_disconnect", mnu_disconnect);
30 rg->get_widget("mnu_file_exit", exit);
31 rg->get_widget("mnu_help_about", about);
32 rg->get_widget("mnu_prop", mnu_prop);
33 rg->get_widget_derived("dlgConectar", dlg_conectar);
34 rg->get_widget("btn_get", btn_get);
35 rg->get_widget("txt_view", txt_view);
36 rg->get_widget("txt_target", txt_target);
37 rg->get_widget("txt_command", txt_command);
38 rg->get_widget("txt_args", txt_args);
39 rg->get_widget("bar_connect", bar_connect);
40 rg->get_widget("work_place", work_place);
41 rg->get_widget("ico_conected", ico_conected);
43 dlg_conectar->get_ok_button()->signal_clicked().connect( SigC::slot(*this, &Principal::on_dlg_connect_ok) );
44 mnu_disconnect->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_file_disconnect));
45 conect->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_file_connect));
46 bar_connect->signal_clicked().connect( SigC::slot(*this, &Principal::on_mnu_file_connect));
47 exit->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_file_exit));
48 about->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_help_about));
49 btn_get->signal_clicked().connect( SigC::slot(*this, &Principal::on_get_clicked) );
52 is_xml_loaded = false;
54 load_xml_dispatch.connect( SigC::slot(*this, &Principal::loadXML ) );
57 Principal::~Principal()
62 void Principal::on_dlg_connect_ok()
64 if (conexion == NULL) {
67 conexion = new PlaQui::Server::ControlClient(dlg_conectar->get_server_name(), dlg_conectar->get_server_port());
70 txt_view->get_buffer()->insert_at_cursor("NO SE PUDO CREAR OBJETO\n");
76 // Conecto las señales
77 conexion->signal_ok_received().connect( SigC::slot(*this, &Principal::on_conexion_ok) );
78 conexion->signal_error_received().connect( SigC::slot(*this, &Principal::on_conexion_error) );
79 conexion->signal_connected().connect( SigC::slot(*this, &Principal::on_conexion_connected) );
80 conexion->signal_finished().connect( SigC::slot(*this, &Principal::on_conexion_finished) );
81 conexion->signal_frame_received().connect(SigC::slot(*this, &Principal::on_conexion_frame));
87 txt_view->get_buffer()->insert_at_cursor("no se puede correr conexion->run()!!!\n");
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("connection", "stop");
105 c.add_arg(conexion->get_host());
106 c.add_arg(conexion->get_port());
110 void Principal::on_mnu_file_connect()
112 dlg_conectar->show();
115 void Principal::on_mnu_help_about()
117 // preparo para leer el archivo ChangeLog
118 /* Glib::RefPtr<Gnome::Glade::Xml> xml;
120 Glib::RefPtr<Gtk::TextBuffer> log_buffer;
121 Glib::RefPtr<Glib::IOChannel> log_io;
123 log_buffer = Gtk::TextBuffer::create();
124 log_io = Glib::IOChannel::create_from_file("../ChangeLog", "r");
125 while (log_io->read_line(line) != Glib::IO_STATUS_EOF) {
126 log_buffer->insert_at_cursor(line);
130 xml = Gnome::Glade::Xml::create("client.glade", "dlgAbout");
132 catch(const Gnome::Glade::XmlError &ex) {
133 std::cerr << ex.what() << std::endl;
136 Gtk::Window *dlg = 0;
137 Gtk::Button *btn_cerrar = 0;
138 Gtk::TextView *txt_changelog = 0;
139 xml->get_widget("dlgAbout", dlg);
140 xml->get_widget("btn_close", btn_cerrar);
141 xml->get_widget("txt_changelog", txt_changelog);
142 btn_cerrar->signal_clicked().connect(SigC::slot(*dlg, &Gtk::Dialog::hide));
143 txt_changelog->set_buffer(log_buffer);
147 bool Principal::on_item_clicked(GdkEventButton *e, ViewItem *i)
149 lbl_nombre->set_text(i->get_name());
150 txt_view->get_buffer()->insert_at_cursor("Selecciono ");
151 txt_view->get_buffer()->insert_at_cursor(i->get_name());
152 txt_view->get_buffer()->insert_at_cursor("\n");
155 void Principal::on_conexion_connected()
157 txt_view->get_buffer()->insert_at_cursor("CONNECTED\n");
158 ico_conected->set( Gtk::Stock::YES , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
160 // Pido la planta por defecto
161 PlaQui::Server::Command c("plant", "get");
162 c.add_arg("default");
166 void Principal::on_conexion_frame(const std::string &frame)
168 std::cout << "LLEGO : " << frame << std::endl;
171 void Principal::on_conexion_finished()
173 txt_view->get_buffer()->insert_at_cursor("HANG UP\n");
174 ico_conected->set( Gtk::Stock::NO , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
180 void Principal::on_conexion_ok(const std::string &body)
182 /* lo paso a la carga del XML */
183 /* verifico que body este completo */
184 if ((body.find("</planta>")>0) && (body.find("<planta>")>0)) {
189 txt_view->get_buffer()->insert_at_cursor("<IN>\n");
190 txt_view->get_buffer()->insert_at_cursor(Glib::locale_to_utf8(body));
191 txt_view->get_buffer()->insert_at_cursor("</IN>\n");
195 void Principal::on_conexion_error(unsigned code)
201 txt_view->get_buffer()->insert_at_cursor("El server dice que hay error : ");
202 txt_view->get_buffer()->insert_at_cursor(s);
203 txt_view->get_buffer()->insert_at_cursor("\n");
207 void Principal::on_get_clicked()
209 if (conexion == NULL) {
210 txt_view->get_buffer()->insert_at_cursor("SIN CONEXION\n");
215 PlaQui::Server::Command command(txt_target->get_text(), txt_command->get_text());
216 command.add_arg( txt_args->get_text() );
217 txt_view->get_buffer()->insert_at_cursor("Enviando comando\n");
219 conexion->send(command);
222 txt_view->get_buffer()->insert_at_cursor("EXCEPTION EN conexion->send !!\n");
227 void Principal::loadXML()
230 if (is_xml_loaded) return;
232 /* Parseo de ejemplo de un XML desde archivo */
234 document = xmlParseMemory(xml_body.c_str(),xml_body.size());
235 if (document == NULL) {
236 std::cout << "EEERRRRRRROOOOOOOOOO" << std::endl;
239 is_xml_loaded = true;
240 /* bien, el archivo se parseo bien! */
241 xmlNodePtr nodo, items;
242 nodo = document->children;
244 if (strcmp((char *)nodo->name, "planta") == 0) {
245 items = nodo->children;
246 while (items != NULL) {
247 if (items->type == XML_ELEMENT_NODE) {
248 if (xmlStrcmp(items->name, BAD_CAST"bomba")==0) {
250 } else if (xmlStrcmp(items->name, BAD_CAST"codo")==0) {
252 } else if (xmlStrcmp(items->name, BAD_CAST"tubo")==0) {
254 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
256 } else if (xmlStrcmp(items->name, BAD_CAST"tanque")==0) {
258 } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
260 } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
270 void Principal::loadBomba(xmlNodePtr nodo)
272 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
273 int orientacion=0, x, y;
275 nodo = nodo->children;
276 while (nodo != NULL) {
277 if (nodo->type == XML_ELEMENT_NODE) {
278 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
279 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
280 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
281 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
282 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
283 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
289 // listo, ya recolecte todos los datos, ahora creo el objeto!
290 ViewPump *b = new ViewPump(name, orientacion);
291 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
292 b->set_position(x,y);
293 work_place->put(*b, x, y);
295 // los agrego al hash
299 void Principal::loadCodo(xmlNodePtr nodo)
301 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
302 int orientacion=0, x, y;
304 nodo = nodo->children;
305 while (nodo != NULL) {
306 if (nodo->type == XML_ELEMENT_NODE) {
307 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
308 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
309 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
310 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
311 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
312 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
318 // listo, ya recolecte todos los datos, ahora creo el objeto!
319 ViewItem *b = new ViewCodo(name, orientacion);
320 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
321 b->set_position(x,y);
322 work_place->put(*b, x, y);
324 // los agrego al hash
328 void Principal::loadConduct(xmlNodePtr nodo)
330 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
331 int orientacion=0, x, y;
333 nodo = nodo->children;
334 while (nodo != NULL) {
335 if (nodo->type == XML_ELEMENT_NODE) {
336 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
337 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
338 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
339 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
340 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
341 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
347 // listo, ya recolecte todos los datos, ahora creo el objeto!
348 ViewConduct *b = new ViewConduct(name, orientacion);
349 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
350 b->set_position(x,y);
351 work_place->put(*b, x, y);
353 // los agrego al hash
357 void Principal::loadExclusa(xmlNodePtr nodo)
359 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
360 int orientacion=0, x, y;
362 nodo = nodo->children;
363 while (nodo != NULL) {
364 if (nodo->type == XML_ELEMENT_NODE) {
365 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
366 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
367 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
368 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
369 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
370 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
376 // listo, ya recolecte todos los datos, ahora creo el objeto!
377 ViewExclusa *b = new ViewExclusa(name, orientacion);
378 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
379 b->set_position(x,y);
380 work_place->put(*b, x, y);
382 // los agrego al hash
386 void Principal::loadTank(xmlNodePtr nodo)
388 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
389 int orientacion=0, x, y;
391 nodo = nodo->children;
392 while (nodo != NULL) {
393 if (nodo->type == XML_ELEMENT_NODE) {
394 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
395 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
396 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
397 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
398 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
399 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
405 // listo, ya recolecte todos los datos, ahora creo el objeto!
406 ViewTank *b = new ViewTank(name, orientacion);
407 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
408 b->set_position(x,y);
409 work_place->put(*b, x, y);
411 // los agrego al hash
415 void Principal::loadUnion(xmlNodePtr nodo)
417 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
418 int orientacion=0, x, y;
420 nodo = nodo->children;
421 while (nodo != NULL) {
422 if (nodo->type == XML_ELEMENT_NODE) {
423 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
424 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
425 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
426 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
427 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
428 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
434 // listo, ya recolecte todos los datos, ahora creo el objeto!
435 ViewUnion *b = new ViewUnion(name, orientacion);
436 b->signal_button_release_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
437 b->set_position(x,y);
438 work_place->put(*b, x, y);
440 // los agrego al hash
444 void Principal::loadDrain(xmlNodePtr nodo)
446 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
447 int orientacion=0, x, y;
449 nodo = nodo->children;
450 while (nodo != NULL) {
451 if (nodo->type == XML_ELEMENT_NODE) {
452 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
453 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
454 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
455 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
456 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
457 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
463 // listo, ya recolecte todos los datos, ahora creo el objeto!
464 ViewDrain *b = new ViewDrain(name, orientacion);
465 b->signal_button_release_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
466 b->set_position(x,y);
467 work_place->put(*b, x, y);
469 // los agrego al hash