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)
30 IRRECO_RETURN_PTR(g_slice_new0(IrTransConfigDlg
));
33 void irtrans_config_dlg_destroy(IrTransConfigDlg
* config_dialog
)
37 g_slice_free(IrTransConfigDlg
, config_dialog
);
43 * Checks the format of given IP-address.
45 static gint
check_ip_addr(const gchar
*ip_address
)
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
){
66 sscanf(buffer
, "%d", &addr
);
68 if(addr
< 0 || addr
> 255)
71 buffer
= strtok(NULL
, ".");
82 static gboolean
irtrans_load_ip_list(IrTransConfigDlg
* config_dialog
)
85 IrrecoBackendStatus res_status
;
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
,
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
;
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
,
125 gtk_tree_store_set(config_dialog
->ip_treestore
,
127 irtrans_wrap_get_hostname(
128 config_dialog
->plugin
->irtrans_wrap
),
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
;
146 /* create GtkTreeStore and GtkTreeView */
147 config_dialog
->ip_treestore
= gtk_tree_store_new(N_COLUMNS
,
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
);
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
,
163 gtk_tree_store_set(config_dialog
->ip_treestore
,
165 irtrans_wrap_get_hostname(
166 config_dialog
->plugin
->irtrans_wrap
),
168 config_dialog
->ip_selection_index
= 0;
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(
184 g_object_unref(G_OBJECT(config_dialog
->ip_treestore
));
187 renderer
= gtk_cell_renderer_text_new();
188 column
= gtk_tree_view_column_new_with_attributes(NULL
,
192 gtk_tree_view_append_column(GTK_TREE_VIEW(
196 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(
200 IRRECO_RETURN_PTR(model
);
202 IRRECO_RETURN_PTR(NULL
);
206 static void if_remote_pressed(GtkWidget
* widget
, gpointer data
)
208 IrTransConfigDlg
*config_dialog
= data
;
212 irtrans_wrap_set_remote_server(
213 config_dialog
->plugin
->irtrans_wrap
, TRUE
);
218 static void if_local_pressed(GtkWidget
* widget
, gpointer data
)
220 IrTransConfigDlg
*config_dialog
= data
;
224 irtrans_wrap_set_remote_server(
225 config_dialog
->plugin
->irtrans_wrap
, FALSE
);
230 /****************************************************************/
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
;
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;
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
);
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
));
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*/,
315 0 /*padding_bottom*/,
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
),
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
),
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(
350 (gpointer
)config_dialog
);
351 g_signal_connect(G_OBJECT(local_check
), "clicked", G_CALLBACK(
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
),
363 /* create model and fill with data if it exists */
364 model
= irtrans_create_ip_list(config_dialog
);
368 /* irreco_info_dialog from irreco_util.h */
369 irreco_info_dlg(parent
, _("Couldn't create IP-list!"));
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(
400 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
406 gtk_window_resize(GTK_WINDOW(dialog
), 1, 300);
407 gtk_widget_show_all(dialog
);
410 switch(gtk_dialog_run(GTK_DIALOG(dialog
))){
411 case GTK_RESPONSE_REJECT
:
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."));
436 ok
= check_ip_addr(irtrans_wrap_get_hostname(
437 plugin
->irtrans_wrap
));
440 irreco_info_dlg(parent
,
441 _("Please check modules IP-address!"));
448 case IRTRANS_CONNECT_RETRY
:
453 model
= irtrans_create_ip_list(
457 /* irreco_info_dialog */
458 /* from irreco_util.h */
459 irreco_info_dlg(parent
,
460 _("Couldn't create IP-list!"));
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
);
481 irreco_info_dlg(parent
,
482 _("Cannot autodetect IRTrans modules!\n"
483 "Check your WLAN connection and retry."));
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."));
509 ok
= check_ip_addr(irtrans_wrap_get_hostname(
510 plugin
->irtrans_wrap
));
513 IrrecoBackendStatus status
;
514 status
= irtrans_wrap_connect(
515 plugin
->irtrans_wrap
);
516 if (status
!= IRRECO_BACKEND_OK
) {
520 /* enter to device learn dialog */
521 irtrans_remote_dlg(plugin
, parent
);
523 irtrans_wrap_disconnect(
524 plugin
->irtrans_wrap
);
527 irreco_info_dlg(parent
,
528 _("Please check modules IP-address!"));
534 irtrans_config_dlg_destroy(config_dialog
);
535 gtk_widget_destroy(dialog
);
536 IRRECO_RETURN_BOOL(rvalue
);