Trunk cleanup: dummy
[irreco.git] / backend / irtrans / trunk / src / irtrans_config_dlg.c
blob08ef8148e06d9953bf329e390979b964f0d4dbfb
1 /*
2 Irtrans_config_dlg - part of Ir Remote Control for N800
3 Copyright (C) 2007 Jussi Pyykkö (jupyykko@netti.fi)
5 This is based on the irreco_button_dlg by Arto Karppinen
6 (arto.karppinen@iki.fi)
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "irtrans_config_dlg.h"
24 #include "irtrans_remote_dlg.h"
26 IrTransConfigDlg *irtrans_config_dlg_create(void)
28 IRRECO_ENTER
30 IRRECO_RETURN_PTR(g_slice_new0(IrTransConfigDlg));
33 void irtrans_config_dlg_destroy(IrTransConfigDlg * config_dialog)
35 IRRECO_ENTER
37 g_slice_free(IrTransConfigDlg, config_dialog);
39 IRRECO_RETURN
43 * Checks the format of given IP-address.
45 static gint check_ip_addr(const gchar *ip_address)
47 gchar temp[SIZE];
48 gchar *buffer = NULL;
49 gint i = 0;
50 gint addr = 0;
51 gint ok = 1;
53 IRRECO_ENTER
55 if (ip_address == NULL) IRRECO_RETURN_INT(0);
57 strcpy(temp, ip_address);
59 /* split string into tokens */
60 buffer = strtok(temp, "." );
62 /* check splitted string */
63 while(buffer != NULL){
65 i++;
66 sscanf(buffer, "%d", &addr);
68 if(addr < 0 || addr > 255)
69 ok = 0;
71 buffer = strtok(NULL, ".");
74 if(i != 4 || !ok){
76 IRRECO_RETURN_INT(0);
77 } else {
78 IRRECO_RETURN_INT(1);
82 static gboolean irtrans_load_ip_list(IrTransConfigDlg * config_dialog)
84 const gchar *address;
85 IrrecoBackendStatus res_status;
86 gint i = 0;
87 GtkTreeIter iter;
88 IRRECO_ENTER
90 /* Attempt to autodetect IRTrans devices. */
91 res_status = irtrans_wrap_get_autodetect_list(
92 config_dialog->plugin->irtrans_wrap,
93 &config_dialog->ip_count);
94 if (res_status != IRRECO_BACKEND_OK) IRRECO_RETURN_BOOL(FALSE);
96 while (irtrans_wrap_get_from_list(
97 config_dialog->plugin->irtrans_wrap, &address)) {
99 gtk_tree_store_append(config_dialog->ip_treestore,
100 &iter, NULL);
101 gtk_tree_store_set(config_dialog->ip_treestore,
102 &iter, IP_COLUMN, address, -1);
104 /* find index of the address */
105 /* restored from the config file */
106 if(irtrans_wrap_get_hostname(
107 config_dialog->plugin->irtrans_wrap) != NULL){
109 if(strcmp(address, irtrans_wrap_get_hostname(
110 config_dialog->plugin->irtrans_wrap)) == 0){
111 config_dialog->ip_selection_index = i;
115 i++;
118 /* restored address didn't found from the autodetected list */
119 if(config_dialog->ip_selection_index == -1 &&
120 irtrans_wrap_get_hostname(
121 config_dialog->plugin->irtrans_wrap) != NULL){
123 gtk_tree_store_append(config_dialog->ip_treestore,
124 &iter, NULL);
125 gtk_tree_store_set(config_dialog->ip_treestore,
126 &iter, IP_COLUMN,
127 irtrans_wrap_get_hostname(
128 config_dialog->plugin->irtrans_wrap),
129 -1);
130 config_dialog->ip_selection_index = i;
133 IRRECO_RETURN_BOOL(TRUE);
136 static GtkTreeModel *irtrans_create_ip_list(IrTransConfigDlg * config_dialog)
138 GtkTreeViewColumn *column = NULL;
139 GtkCellRenderer *renderer = NULL;
140 GtkTreeModel *model = NULL;
141 GtkTreeIter iter;
142 gboolean success;
144 IRRECO_ENTER
146 /* create GtkTreeStore and GtkTreeView */
147 config_dialog->ip_treestore = gtk_tree_store_new(N_COLUMNS,
148 G_TYPE_STRING);
149 if(!irtrans_wrap_get_remote_server(config_dialog->plugin->irtrans_wrap)){
151 /* load ip-list if local server is set */
152 /* fill the list store with data */
153 success = irtrans_load_ip_list(config_dialog);
154 } else {
155 success = TRUE;
157 /* append IP-address of the remote server */
158 if(irtrans_wrap_get_hostname(
159 config_dialog->plugin->irtrans_wrap) != NULL){
161 gtk_tree_store_append(config_dialog->ip_treestore,
162 &iter, NULL);
163 gtk_tree_store_set(config_dialog->ip_treestore,
164 &iter, IP_COLUMN,
165 irtrans_wrap_get_hostname(
166 config_dialog->plugin->irtrans_wrap),
167 -1);
168 config_dialog->ip_selection_index = 0;
172 if (success){
174 /* if former widget exists destroy it */
175 if(config_dialog->ip_treeview != NULL){
177 gtk_widget_destroy(config_dialog->ip_treeview);
180 config_dialog->ip_treeview = gtk_tree_view_new_with_model(
181 GTK_TREE_MODEL(
182 config_dialog->
183 ip_treestore));
184 g_object_unref(G_OBJECT(config_dialog->ip_treestore));
186 /* setup column */
187 renderer = gtk_cell_renderer_text_new();
188 column = gtk_tree_view_column_new_with_attributes(NULL,
189 renderer,
190 "text",
191 0, NULL);
192 gtk_tree_view_append_column(GTK_TREE_VIEW(
193 config_dialog->
194 ip_treeview),
195 column);
196 model = gtk_tree_view_get_model(GTK_TREE_VIEW(
197 config_dialog->
198 ip_treeview));
200 IRRECO_RETURN_PTR(model);
201 } else {
202 IRRECO_RETURN_PTR(NULL);
206 static void if_remote_pressed(GtkWidget* widget, gpointer data)
208 IrTransConfigDlg *config_dialog = data;
210 IRRECO_ENTER
212 irtrans_wrap_set_remote_server(
213 config_dialog->plugin->irtrans_wrap, TRUE);
215 IRRECO_RETURN
218 static void if_local_pressed(GtkWidget* widget, gpointer data)
220 IrTransConfigDlg *config_dialog = data;
222 IRRECO_ENTER
224 irtrans_wrap_set_remote_server(
225 config_dialog->plugin->irtrans_wrap, FALSE);
227 IRRECO_RETURN
230 /****************************************************************/
231 /* */
232 /* */
233 /* DIALOGS */
234 /* */
235 /* */
236 /****************************************************************/
239 * Dialog to configure IrTrans plugin.
241 gboolean irtrans_config_dlg(IrTransPlugin * plugin, GtkWindow * parent)
243 IrTransConfigDlg *config_dialog;
245 GtkTreeModel *model = NULL;
246 GtkTable *table = NULL;
247 GSList* check_group = NULL;
249 GtkWidget *dialog = NULL;
250 GtkWidget *combo = NULL;
251 GtkWidget *description_entry = NULL;
252 GtkWidget* remote_check = NULL;
253 GtkWidget* local_check = NULL;
254 GtkWidget *label_up = NULL;
255 GtkWidget *label_down = NULL;
256 GtkWidget *scrolled = NULL;
258 GtkWidget *align_1 = NULL;
259 GtkWidget *align_2 = NULL;
260 GtkWidget *align_3 = NULL;
261 GtkWidget *align_4 = NULL;
263 GtkWidget *description_label = NULL;
264 GtkWidget *ip_label = NULL;
266 gint rvalue = -1;
267 gint ok;
268 gint retry = 0;
269 /*gboolean start;*/
271 IRRECO_ENTER
273 config_dialog = irtrans_config_dlg_create();
274 config_dialog->plugin = plugin;
275 config_dialog->ip_selection_index = -1;
276 config_dialog->ip_count = 0;
278 /* create objects */
279 dialog = gtk_dialog_new_with_buttons(
280 _("IRTrans configure"), parent,
281 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT |
282 GTK_DIALOG_NO_SEPARATOR,
283 _("Find Modules"), IRTRANS_CONNECT_RETRY,
284 _("Learn Device"), IRTRANS_LEARN_DEVICE,
285 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
286 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
288 /* create widgets */
289 label_up = irreco_gtk_label_bold(_("General"), 0, 0, 6, 0, 0, 0);
290 label_down = irreco_gtk_label_bold(_("IrTrans Server"), 0, 0, 6, 0, 0, 0);
292 description_label = gtk_label_new(_("Module description: "));
293 ip_label = gtk_label_new(_("Module IP-address: "));
295 remote_check = gtk_radio_button_new_with_label(NULL,
296 _("Use remote server"));
297 check_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(remote_check));
298 local_check = gtk_radio_button_new_with_label(check_group,
299 _("Start local server"));
300 check_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(local_check));
302 /* build dialog */
303 table = GTK_TABLE(gtk_table_new(6, 2, FALSE));
304 combo = gtk_combo_box_entry_new_text();
305 description_entry = gtk_entry_new();
307 /*gtk_table_set_row_spacings(table, 6);
308 gtk_table_set_col_spacings(table, 110);*/
310 align_1 = irreco_gtk_align(description_label, 0 /*xalign*/,
311 0.5 /*yalign*/,
312 0 /*xscale*/,
313 0 /*yscale*/,
314 0 /*padding_top*/,
315 0 /*padding_bottom*/,
316 12 /*padding_left*/,
317 0 /*padding_right*/);
319 align_2 = irreco_gtk_align(ip_label, 0, 0.5, 0, 0, 0, 0, 12, 0);
321 align_3 = irreco_gtk_align(local_check, 0, 0.5, 0, 0, 0, 0, 12, 0);
323 align_4 = irreco_gtk_align(remote_check, 0, 0.5, 0, 0, 0, 0, 12, 0);
325 gtk_table_attach_defaults(GTK_TABLE(table), label_up, 0, 1, 0, 1);
326 gtk_table_attach_defaults(GTK_TABLE(table), align_1, 0, 1, 1, 2);
327 gtk_table_attach_defaults(GTK_TABLE(table), description_entry, 1, 2, 1, 2);
328 gtk_table_attach_defaults(GTK_TABLE(table), align_2, 0, 1, 2, 3);
329 gtk_table_attach_defaults(GTK_TABLE(table),GTK_WIDGET(combo),
330 1, 2, 2, 3);
331 gtk_table_attach_defaults(GTK_TABLE(table), label_down, 0, 1, 3, 4);
332 gtk_table_attach_defaults(GTK_TABLE(table), align_3, 0, 1, 4, 5);
333 gtk_table_attach_defaults(GTK_TABLE(table), align_4, 0, 1, 5, 6);
335 scrolled = gtk_scrolled_window_new(NULL, NULL);
336 /*gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled),
337 GTK_SHADOW_ETCHED_IN);*/
338 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
339 GTK_POLICY_AUTOMATIC,
340 GTK_POLICY_AUTOMATIC);
341 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled),
342 GTK_WIDGET(table));
344 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox),
345 GTK_WIDGET(scrolled));
347 /* radiobutton signals */
348 g_signal_connect(G_OBJECT(remote_check), "clicked", G_CALLBACK(
349 if_remote_pressed),
350 (gpointer)config_dialog);
351 g_signal_connect(G_OBJECT(local_check), "clicked", G_CALLBACK(
352 if_local_pressed),
353 (gpointer)config_dialog);
355 /* if backend config-file doesn't exists set local server */
356 if(irtrans_wrap_get_hostname(
357 config_dialog->plugin->irtrans_wrap) == NULL){
359 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(local_check),
360 TRUE);
363 /* create model and fill with data if it exists */
364 model = irtrans_create_ip_list(config_dialog);
366 if(model == NULL){
368 /* irreco_info_dialog from irreco_util.h */
369 irreco_info_dlg(parent, _("Couldn't create IP-list!"));
371 } else {
372 gtk_combo_box_set_model(GTK_COMBO_BOX(combo), model);
374 /* ip-list creation succeed but no LAN modules found*/
375 if(config_dialog->ip_count == 0 &&
376 !irtrans_wrap_get_remote_server(config_dialog->plugin->irtrans_wrap)){
378 irreco_info_dlg(parent,
379 _("Cannot autodetect IRTrans modules!\n"
380 "Check your IRTrans modules and retry."));
384 /* set values if backend config-file exists*/
385 if(irtrans_wrap_get_hostname(config_dialog->plugin->irtrans_wrap)
386 != NULL && config_dialog->plugin->description != NULL) {
388 gtk_entry_set_text(GTK_ENTRY(description_entry),
389 config_dialog->plugin->description);
390 gtk_combo_box_set_active(GTK_COMBO_BOX(combo),
391 config_dialog->ip_selection_index);
393 if(irtrans_wrap_get_remote_server(config_dialog->plugin->irtrans_wrap)){
395 /* set remote button */
396 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
397 remote_check),
398 TRUE);
399 } else {
400 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
401 local_check),
402 TRUE);
406 gtk_window_resize(GTK_WINDOW(dialog), 1, 300);
407 gtk_widget_show_all(dialog);
409 while(rvalue == -1){
410 switch(gtk_dialog_run(GTK_DIALOG(dialog))){
411 case GTK_RESPONSE_REJECT:
412 rvalue = FALSE;
413 break;
415 case GTK_RESPONSE_ACCEPT:
417 /* get instance hostname and hostaddress */
418 plugin->description = g_strdup(
419 gtk_entry_get_text(GTK_ENTRY(
420 description_entry)));
422 irtrans_wrap_set_hostname(
423 plugin->irtrans_wrap,
424 gtk_combo_box_get_active_text(
425 GTK_COMBO_BOX(combo)));
427 /* check hostname and hostaddress */
428 if(g_utf8_strlen(plugin->description, -1) < 1){
430 irreco_error_dlg(parent,
431 _("Empty module description is not valid."
432 "Type something, or press cancel."));
433 break;
436 ok = check_ip_addr(irtrans_wrap_get_hostname(
437 plugin->irtrans_wrap));
439 if(!ok){
440 irreco_info_dlg(parent,
441 _("Please check modules IP-address!"));
442 break;
445 rvalue = TRUE;
446 break;
448 case IRTRANS_CONNECT_RETRY:
449 if(retry < 2){
451 retry++;
453 model = irtrans_create_ip_list(
454 config_dialog);
455 if(model == NULL){
457 /* irreco_info_dialog */
458 /* from irreco_util.h */
459 irreco_info_dlg(parent,
460 _("Couldn't create IP-list!"));
461 } else {
462 gtk_combo_box_set_model(
463 GTK_COMBO_BOX(combo), model);
465 /* ip-list creation succeed */
466 /* but no modules found */
467 if(config_dialog->ip_count == 0
468 && !irtrans_wrap_get_remote_server(
469 config_dialog->plugin->irtrans_wrap)) {
471 irreco_info_dlg(parent,
472 _("Cannot autodetect IRTrans modules!\n"
473 "Check your IRTrans modules and retry."));
477 gtk_widget_show_all(dialog);
478 break;
479 } else {
481 irreco_info_dlg(parent,
482 _("Cannot autodetect IRTrans modules!\n"
483 "Check your WLAN connection and retry."));
485 rvalue = FALSE;
486 break;
489 case IRTRANS_LEARN_DEVICE:
491 /* get instance hostname and hostaddress */
492 plugin->description = g_strdup(
493 gtk_entry_get_text(GTK_ENTRY(
494 description_entry)));
496 irtrans_wrap_set_hostname(
497 plugin->irtrans_wrap,
498 gtk_combo_box_get_active_text(
499 GTK_COMBO_BOX(combo)));
501 /*check_host_name and host_address*/
502 if(g_utf8_strlen(plugin->description, -1) < 1){
503 irreco_error_dlg(parent,
504 _("Empty module description is not valid."
505 "Type something, or press cancel."));
506 break;
509 ok = check_ip_addr(irtrans_wrap_get_hostname(
510 plugin->irtrans_wrap));
511 if (ok) {
513 IrrecoBackendStatus status;
514 status = irtrans_wrap_connect(
515 plugin->irtrans_wrap);
516 if (status != IRRECO_BACKEND_OK) {
517 break;
520 /* enter to device learn dialog */
521 irtrans_remote_dlg(plugin, parent);
523 irtrans_wrap_disconnect(
524 plugin->irtrans_wrap);
525 break;
526 } else {
527 irreco_info_dlg(parent,
528 _("Please check modules IP-address!"));
529 break;
534 irtrans_config_dlg_destroy(config_dialog);
535 gtk_widget_destroy(dialog);
536 IRRECO_RETURN_BOOL(rvalue);