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;
55 Principal::~Principal()
60 void Principal::on_dlg_connect_ok()
62 if (conexion == NULL) {
65 conexion = new PlaQui::Server::ControlClient(dlg_conectar->get_server_name(), dlg_conectar->get_server_port());
68 txt_view->get_buffer()->insert_at_cursor("NO SE PUDO CREAR OBJETO\n");
74 // Conecto las señales
75 conexion->signal_ok_received().connect( SigC::slot(*this, &Principal::on_conexion_ok) );
76 conexion->signal_error_received().connect( SigC::slot(*this, &Principal::on_conexion_error) );
77 conexion->signal_connected().connect( SigC::slot(*this, &Principal::on_conexion_connected) );
78 conexion->signal_finished().connect( SigC::slot(*this, &Principal::on_conexion_finished) );
79 conexion->signal_frame_received().connect(SigC::slot(*this, &Principal::on_conexion_frame));
85 txt_view->get_buffer()->insert_at_cursor("no se puede correr conexion->run()!!!\n");
88 txt_view->get_buffer()->insert_at_cursor("YA ESTAS CONECTADO\n");
93 void Principal::on_mnu_file_exit()
98 void Principal::on_mnu_file_disconnect()
100 if (conexion == NULL) return;
102 PlaQui::Server::Command c("connection", "stop");
103 c.add_arg(conexion->get_host());
104 c.add_arg(conexion->get_port());
108 void Principal::on_mnu_file_connect()
110 dlg_conectar->show();
113 void Principal::on_mnu_help_about()
115 // preparo para leer el archivo ChangeLog
116 /* Glib::RefPtr<Gnome::Glade::Xml> xml;
118 Glib::RefPtr<Gtk::TextBuffer> log_buffer;
119 Glib::RefPtr<Glib::IOChannel> log_io;
121 log_buffer = Gtk::TextBuffer::create();
122 log_io = Glib::IOChannel::create_from_file("../ChangeLog", "r");
123 while (log_io->read_line(line) != Glib::IO_STATUS_EOF) {
124 log_buffer->insert_at_cursor(line);
128 xml = Gnome::Glade::Xml::create("client.glade", "dlgAbout");
130 catch(const Gnome::Glade::XmlError &ex) {
131 std::cerr << ex.what() << std::endl;
134 Gtk::Window *dlg = 0;
135 Gtk::Button *btn_cerrar = 0;
136 Gtk::TextView *txt_changelog = 0;
137 xml->get_widget("dlgAbout", dlg);
138 xml->get_widget("btn_close", btn_cerrar);
139 xml->get_widget("txt_changelog", txt_changelog);
140 btn_cerrar->signal_clicked().connect(SigC::slot(*dlg, &Gtk::Dialog::hide));
141 txt_changelog->set_buffer(log_buffer);
145 bool Principal::on_item_clicked(GdkEventButton *e, ViewItem *i)
147 lbl_nombre->set_text(i->get_name());
148 txt_view->get_buffer()->insert_at_cursor("Selecciono ");
149 txt_view->get_buffer()->insert_at_cursor(i->get_name());
150 txt_view->get_buffer()->insert_at_cursor("\n");
153 void Principal::on_conexion_connected()
155 txt_view->get_buffer()->insert_at_cursor("CONNECTED\n");
156 ico_conected->set( Gtk::Stock::YES , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
158 // Pido la planta por defecto
159 PlaQui::Server::Command c("plant", "get");
160 c.add_arg("default");
164 void Principal::on_conexion_frame(const std::string &frame)
166 std::cout << "LLEGO : " << frame << std::endl;
169 void Principal::on_conexion_finished()
171 txt_view->get_buffer()->insert_at_cursor("HANG UP\n");
172 ico_conected->set( Gtk::Stock::NO , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
176 void Principal::on_conexion_ok(const std::string &body)
178 /* lo paso a la carga del XML */
179 /* verifico que body este completo */
180 if ((body.find("</planta>")>0) && (body.find("<planta>")>0)) {
183 txt_view->get_buffer()->insert_at_cursor("<IN>\n");
184 txt_view->get_buffer()->insert_at_cursor(Glib::locale_to_utf8(body));
185 txt_view->get_buffer()->insert_at_cursor("</IN>\n");
189 void Principal::on_conexion_error(unsigned code)
195 txt_view->get_buffer()->insert_at_cursor("El server dice que hay error : ");
196 txt_view->get_buffer()->insert_at_cursor(s);
197 txt_view->get_buffer()->insert_at_cursor("\n");
201 void Principal::on_get_clicked()
203 if (conexion == NULL) {
204 txt_view->get_buffer()->insert_at_cursor("SIN CONEXION\n");
209 PlaQui::Server::Command command(txt_target->get_text(), txt_command->get_text());
210 command.add_arg( txt_args->get_text() );
211 txt_view->get_buffer()->insert_at_cursor("Enviando comando\n");
213 conexion->send(command);
216 txt_view->get_buffer()->insert_at_cursor("EXCEPTION EN conexion->send !!\n");
221 void Principal::loadXML(const std::string &s)
224 if (is_xml_loaded) return;
226 /* Parseo de ejemplo de un XML desde archivo */
228 std::cout << s.c_str() << std::endl;
229 document = xmlParseMemory(s.c_str(),s.size());
230 if (document == NULL) {
231 std::cout << "EEERRRRRRROOOOOOOOOO" << std::endl;
234 is_xml_loaded = true;
235 /* bien, el archivo se parseo bien! */
236 xmlNodePtr nodo, items;
237 nodo = document->children;
239 if (strcmp((char *)nodo->name, "planta") == 0) {
240 items = nodo->children;
241 while (items != NULL) {
242 if (items->type == XML_ELEMENT_NODE) {
243 if (xmlStrcmp(items->name, BAD_CAST"bomba")==0) {
245 } else if (xmlStrcmp(items->name, BAD_CAST"codo")==0) {
247 } else if (xmlStrcmp(items->name, BAD_CAST"tubo")==0) {
249 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
251 } else if (xmlStrcmp(items->name, BAD_CAST"tanque")==0) {
253 } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
255 } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
266 void Principal::loadBomba(xmlNodePtr nodo)
268 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
269 int orientacion=0, x, y;
271 nodo = nodo->children;
272 while (nodo != NULL) {
273 if (nodo->type == XML_ELEMENT_NODE) {
274 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
275 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
276 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
277 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
278 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
279 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
285 // listo, ya recolecte todos los datos, ahora creo el objeto!
286 ViewPump *b = new ViewPump(name, orientacion);
287 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
288 b->set_position(x,y);
289 work_place->put(*b, x, y);
291 // los agrego al hash
295 void Principal::loadCodo(xmlNodePtr nodo)
297 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
298 int orientacion=0, x, y;
300 nodo = nodo->children;
301 while (nodo != NULL) {
302 if (nodo->type == XML_ELEMENT_NODE) {
303 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
304 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
305 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
306 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
307 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
308 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
314 // listo, ya recolecte todos los datos, ahora creo el objeto!
315 ViewItem *b = new ViewCodo(name, orientacion);
316 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
317 b->set_position(x,y);
318 work_place->put(*b, x, y);
320 // los agrego al hash
324 void Principal::loadConduct(xmlNodePtr nodo)
326 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
327 int orientacion=0, x, y;
329 nodo = nodo->children;
330 while (nodo != NULL) {
331 if (nodo->type == XML_ELEMENT_NODE) {
332 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
333 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
334 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
335 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
336 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
337 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
343 // listo, ya recolecte todos los datos, ahora creo el objeto!
344 ViewConduct *b = new ViewConduct(name, orientacion);
345 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
346 b->set_position(x,y);
347 work_place->put(*b, x, y);
349 // los agrego al hash
353 void Principal::loadExclusa(xmlNodePtr nodo)
355 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
356 int orientacion=0, x, y;
358 nodo = nodo->children;
359 while (nodo != NULL) {
360 if (nodo->type == XML_ELEMENT_NODE) {
361 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
362 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
363 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
364 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
365 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
366 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
372 // listo, ya recolecte todos los datos, ahora creo el objeto!
373 ViewExclusa *b = new ViewExclusa(name, orientacion);
374 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
375 b->set_position(x,y);
376 work_place->put(*b, x, y);
378 // los agrego al hash
382 void Principal::loadTank(xmlNodePtr nodo)
384 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
385 int orientacion=0, x, y;
387 nodo = nodo->children;
388 while (nodo != NULL) {
389 if (nodo->type == XML_ELEMENT_NODE) {
390 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
391 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
392 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
393 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
394 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
395 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
401 // listo, ya recolecte todos los datos, ahora creo el objeto!
402 ViewTank *b = new ViewTank(name, orientacion);
403 b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
404 b->set_position(x,y);
405 work_place->put(*b, x, y);
407 // los agrego al hash
411 void Principal::loadUnion(xmlNodePtr nodo)
413 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
414 int orientacion=0, x, y;
416 nodo = nodo->children;
417 while (nodo != NULL) {
418 if (nodo->type == XML_ELEMENT_NODE) {
419 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
420 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
421 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
422 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
423 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
424 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
430 // listo, ya recolecte todos los datos, ahora creo el objeto!
431 ViewUnion *b = new ViewUnion(name, orientacion);
432 b->signal_button_release_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
433 b->set_position(x,y);
434 work_place->put(*b, x, y);
436 // los agrego al hash
440 void Principal::loadDrain(xmlNodePtr nodo)
442 Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
443 int orientacion=0, x, y;
445 nodo = nodo->children;
446 while (nodo != NULL) {
447 if (nodo->type == XML_ELEMENT_NODE) {
448 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
449 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
450 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
451 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
452 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
453 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
459 // listo, ya recolecte todos los datos, ahora creo el objeto!
460 ViewDrain *b = new ViewDrain(name, orientacion);
461 b->signal_button_release_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
462 b->set_position(x,y);
463 work_place->put(*b, x, y);
465 // los agrego al hash