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_command_dlg.h"
25 static IrTransCommandDlg
*irtrans_command_dlg_create(void)
28 IRRECO_RETURN_PTR(g_slice_new0(IrTransCommandDlg
));
31 static void irtrans_command_dlg_destroy(IrTransCommandDlg
* command_dialog
)
35 g_slice_free(IrTransCommandDlg
, command_dialog
);
41 irtrans_learn_command_callback(IrTransCommandDlg
*command_dialog
)
43 IrrecoBackendStatus status
;
47 status
= irtrans_wrap_learn_command(
48 command_dialog
->plugin
->irtrans_wrap
,
49 command_dialog
->plugin
->remote
,
50 command_dialog
->plugin
->command
,
51 IRTRANS_LEARN_TIMEOUT
);
53 if (command_dialog
->learn_dialog
!= NULL
) {
54 parent
= GTK_WINDOW(command_dialog
->learn_dialog
);
56 parent
= GTK_WINDOW(command_dialog
->relearn_dialog
);
59 gtk_widget_set_sensitive(GTK_WIDGET(parent
), TRUE
);
61 if (command_dialog
->hildon_banner
!= NULL
) {
62 gtk_widget_destroy(GTK_WIDGET(command_dialog
->hildon_banner
));
63 command_dialog
->hildon_banner
= NULL
;
66 /* TODO: For some reason the UI will freeze if we dont close the UI.*/
67 gtk_dialog_response(GTK_DIALOG(parent
), GTK_RESPONSE_REJECT
);
70 if (status == IRRECO_BACKEND_OK) {
71 gtk_dialog_response(GTK_DIALOG(parent), GTK_RESPONSE_REJECT);
73 irreco_info_dlg(parent, _("IR code was not recieved."));
77 IRRECO_RETURN_BOOL(FALSE
)
80 static void irtrans_learn_command(IrTransCommandDlg
*command_dialog
)
86 if (command_dialog
->learn_dialog
!= NULL
) {
87 parent
= GTK_WINDOW(command_dialog
->learn_dialog
);
89 parent
= GTK_WINDOW(command_dialog
->relearn_dialog
);
92 message
= g_string_new(NULL
);
93 g_string_printf(message
,
94 _("Press a button on your remote once!\n"
95 "Waiting button press for %.0f seconds."),
96 IRRECO_MSEC_TO_SECONDS(IRTRANS_LEARN_TIMEOUT
));
97 if (command_dialog
->hildon_banner
== NULL
) {
98 command_dialog
->hildon_banner
=
99 HILDON_BANNER(hildon_banner_show_information(
100 GTK_WIDGET(parent
), NULL
, message
->str
));
102 g_string_free(message
, TRUE
);
104 gtk_widget_set_sensitive(GTK_WIDGET(parent
), FALSE
);
105 g_idle_add((GSourceFunc
) irtrans_learn_command_callback
,
113 * Sends command name to the irserver which creates config-file (name.rem)
114 * in to the remotes-folder.
116 static void *irtrans_set_command(void * pointer
)
118 IrTransCommandDlg
*command_dialog
= pointer
;
119 IrrecoBackendStatus status
;
122 status
= irtrans_wrap_connect(command_dialog
->plugin
->irtrans_wrap
);
123 if (status
!= IRRECO_BACKEND_OK
) IRRECO_RETURN_PTR(0);
125 status
= irtrans_wrap_learn_command(
126 command_dialog
->plugin
->irtrans_wrap
,
127 command_dialog
->plugin
->remote
,
128 command_dialog
->plugin
->command
,
131 /* send "exit-signal" for gtk_dialog */
132 if(command_dialog
->learn_dialog
!= NULL
){
133 gtk_dialog_response(GTK_DIALOG(command_dialog
->learn_dialog
),
134 GTK_RESPONSE_REJECT
);
136 gtk_dialog_response(GTK_DIALOG(command_dialog
->relearn_dialog
),
137 GTK_RESPONSE_REJECT
);
140 IRRECO_RETURN_PTR(0);
146 * Get chosen command from the list pointed by selection_index.
148 static gchar
*get_command_iter(GtkWidget
*treeview
, gint selection_index
)
151 GtkTreeModel
*model
= NULL
;
152 GtkTreePath
*path
= NULL
;
153 gchar
*str_data
= NULL
;
157 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(treeview
));
158 path
= gtk_tree_path_new_from_indices(selection_index
, -1);
159 gtk_tree_model_get_iter(model
, &iter
, path
);
160 gtk_tree_model_get(model
, &iter
, COMMAND_COLUMN
, &str_data
, -1);
161 gtk_tree_path_free(path
);
163 IRRECO_RETURN_PTR(str_data
);
166 static void command_list_selection_changed(GtkTreeSelection
* selection
,
167 IrTransCommandDlg
* command_dialog
)
170 GtkTreeModel
*model
= NULL
;
171 GtkTreePath
*path
= NULL
;
172 gint
*path_indices
= NULL
;
176 if(gtk_tree_selection_get_selected(selection
, &model
, &iter
)){
178 path
= gtk_tree_model_get_path(model
, &iter
);
179 path_indices
= gtk_tree_path_get_indices(path
);
180 command_dialog
->command_selection_index
= path_indices
[0];
181 gtk_tree_path_free(path
);
183 command_dialog
->command_selection_index
= -1;
189 static gboolean
irtrans_load_command_list(IrTransCommandDlg
* command_dialog
)
192 IrrecoBackendStatus status
;
193 const gchar
*command
;
196 status
= irtrans_wrap_get_command_list(
197 command_dialog
->plugin
->irtrans_wrap
,
198 command_dialog
->plugin
->remote
,
200 if (status
!= IRRECO_BACKEND_OK
) IRRECO_RETURN_BOOL(FALSE
);
202 while (irtrans_wrap_get_from_list(
203 command_dialog
->plugin
->irtrans_wrap
, &command
)) {
204 gtk_tree_store_append(command_dialog
->command_treestore
,
206 gtk_tree_store_set(command_dialog
->command_treestore
,
207 &iter
, COMMAND_COLUMN
, command
, -1);
210 IRRECO_RETURN_BOOL(TRUE
);
213 static gboolean
irtrans_create_command_list(IrTransCommandDlg
* command_dialog
)
215 GtkTreeSelection
*select
= NULL
;
216 GtkTreeViewColumn
*column
= NULL
;
217 GtkCellRenderer
*renderer
= NULL
;
222 /* if former widget exists destroy it */
223 if(command_dialog
->command_treeview
!= NULL
){
225 gtk_widget_destroy(command_dialog
->command_treeview
);
228 /* create GtkTreeStore and GtkTreeView */
229 command_dialog
->command_treestore
= gtk_tree_store_new(
232 /* fill the treestore with data */
233 success
= irtrans_load_command_list(command_dialog
);
237 command_dialog
->command_treeview
= gtk_tree_view_new_with_model(
241 g_object_unref(G_OBJECT(command_dialog
->command_treestore
));
244 renderer
= gtk_cell_renderer_text_new();
245 column
= gtk_tree_view_column_new_with_attributes(
248 gtk_tree_view_append_column(GTK_TREE_VIEW(command_dialog
->
251 /* set selection callback */
252 select
= gtk_tree_view_get_selection(GTK_TREE_VIEW(
255 gtk_tree_selection_set_mode(select
, GTK_SELECTION_SINGLE
);
256 g_signal_connect(G_OBJECT(select
), "changed", G_CALLBACK(
257 command_list_selection_changed
),
259 IRRECO_RETURN_BOOL(TRUE
);
261 IRRECO_RETURN_BOOL(FALSE
);
266 static void show_command_learn_banner(IrTransCommandDlg * command_dialog,
271 command_dialog->hildon_banner = HILDON_BANNER(
272 hildon_banner_show_animation(
273 GTK_WIDGET(parent), NULL,
274 _("Press the button of your remote once!")));
279 /****************************************************************/
285 /****************************************************************/
287 static gboolean
irtrans_command_learn_dlg(IrTransCommandDlg
*command_dialog
,
290 GtkWidget
*command_table
= NULL
;
291 GtkWidget
*command_entry
= NULL
;
292 gchar
*command_buffer
= NULL
;
293 /*GThread *thread = NULL;*/
299 command_dialog
->learn_dialog
= gtk_dialog_new_with_buttons(
300 _("Command learn dialog"), parent
,
301 GTK_DIALOG_MODAL
| GTK_DIALOG_DESTROY_WITH_PARENT
|
302 GTK_DIALOG_NO_SEPARATOR
,
303 _("Learn Command"), IRTRANS_LEARN_CMD
,
304 GTK_STOCK_CANCEL
, GTK_RESPONSE_REJECT
, NULL
);
305 command_table
= gtk_table_new(2, 2, FALSE
);
306 command_entry
= gtk_entry_new();
307 gtk_table_attach_defaults(GTK_TABLE(command_table
),
308 gtk_label_new(_("Insert command name: ")),
310 gtk_table_attach_defaults(GTK_TABLE(command_table
),
313 gtk_table_set_row_spacings(GTK_TABLE(command_table
), 5);
314 gtk_table_set_col_spacings(GTK_TABLE(command_table
), 5);
315 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(
316 command_dialog
->learn_dialog
)->vbox
), command_table
);
317 gtk_widget_show_all(command_dialog
->learn_dialog
);
320 switch(gtk_dialog_run(GTK_DIALOG(command_dialog
->learn_dialog
))){
321 case GTK_RESPONSE_REJECT
:
326 case IRTRANS_LEARN_CMD
:
328 /* get command name */
329 command_buffer
= g_strdup(gtk_entry_get_text
333 if(g_utf8_strlen(command_buffer
, -1) < 1){
334 irreco_info_dlg(parent
,
335 _("Insert valid command name!"));
336 g_free(command_buffer
);
339 /* convert all characters to lowercase */
340 command_dialog
->plugin
->command
=
343 strlen(command_buffer
));
345 g_free(command_buffer
);
347 /*IRRECO_PRINTF("Remote: %s\n",
348 command_dialog->plugin->remote);
349 IRRECO_PRINTF("Command: %s\n",
350 command_dialog->plugin->command);*/
352 if(command_dialog
->plugin
->remote
!=NULL
){
354 irtrans_learn_command(command_dialog
);
358 show_command_learn_banner(
359 command_dialog, parent);
361 /* create thread for */
362 /* set_command function */
364 if((thread = g_thread_create(
367 (void *)command_dialog,
368 FALSE, NULL)) == NULL){
373 irreco_info_dlg(parent
,
374 _("Choose device!"));
382 /* destroy hildon_banner */
384 if(command_dialog->hildon_banner != NULL){
386 gtk_widget_destroy(GTK_WIDGET(command_dialog->hildon_banner));
390 gtk_widget_destroy(command_dialog
->learn_dialog
);
391 IRRECO_RETURN_BOOL(rvalue
);
394 static gboolean
irtrans_command_relearn_dlg(IrTransCommandDlg
*command_dialog
,
397 /*GThread *thread = NULL;*/
398 gchar
*header
= NULL
;
401 command_dialog
->learn_dialog
= NULL
;
405 header
= g_strjoin(" ", "Confirm relearn: ", command_dialog
->
410 command_dialog
->relearn_dialog
= gtk_dialog_new_with_buttons(
412 GTK_DIALOG_MODAL
| GTK_DIALOG_DESTROY_WITH_PARENT
|
413 GTK_DIALOG_NO_SEPARATOR
,
414 GTK_STOCK_OK
, GTK_RESPONSE_ACCEPT
,
415 GTK_STOCK_CANCEL
, GTK_RESPONSE_REJECT
, NULL
);
417 gtk_window_resize(GTK_WINDOW(command_dialog
->relearn_dialog
), 400, 1);
419 gtk_widget_show_all(command_dialog
->relearn_dialog
);
422 switch(gtk_dialog_run(GTK_DIALOG(
423 command_dialog
->relearn_dialog
))){
425 case GTK_RESPONSE_REJECT
:
429 case GTK_RESPONSE_ACCEPT
:
431 irtrans_learn_command(command_dialog
);
434 show_command_learn_banner(command_dialog, parent);
436 /* create thread for */
437 /* set_command function */
439 if((thread = g_thread_create(
440 (GThreadFunc)irtrans_set_command,
441 (void *)command_dialog,
442 FALSE, NULL)) == NULL){
449 /* destroy hildon_banner */
451 if(command_dialog->hildon_banner != NULL){
453 gtk_widget_destroy(GTK_WIDGET(command_dialog->hildon_banner));
458 gtk_widget_destroy(command_dialog
->relearn_dialog
);
459 IRRECO_RETURN_BOOL(rvalue
);
462 gboolean
irtrans_command_dlg(IrTransPlugin
* plugin
, GtkWindow
* parent
)
464 IrTransCommandDlg
*command_dialog
;
466 GtkWidget
*dialog
= NULL
;
467 GtkWidget
*sw
= NULL
;
474 command_dialog
= irtrans_command_dlg_create();
475 command_dialog
->plugin
= plugin
;
477 /* create objects. */
478 dialog
= gtk_dialog_new_with_buttons(
479 plugin
->remote
, parent
,
480 GTK_DIALOG_MODAL
| GTK_DIALOG_DESTROY_WITH_PARENT
|
481 GTK_DIALOG_NO_SEPARATOR
,
482 _("New Command"), IRTRANS_NEW_CMD
,
483 _("Command Relearn"), IRTRANS_RELEARN_CMD
,
484 _("Delete"), IRTRANS_DEL_CMD
,
485 _("Done"), GTK_RESPONSE_ACCEPT
, NULL
);
486 sw
= gtk_scrolled_window_new (NULL
, NULL
);
487 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw
),
488 GTK_SHADOW_ETCHED_IN
);
489 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw
),
490 GTK_POLICY_AUTOMATIC
,
491 GTK_POLICY_AUTOMATIC
);
493 /* create tree model */
494 success
= irtrans_create_command_list(command_dialog
);
498 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw
),
502 irreco_error_dlg(parent
, _("Unable to load commands list!"));
505 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog
)->vbox
), sw
);
506 gtk_widget_show_all(dialog
);
509 switch(gtk_dialog_run(GTK_DIALOG(dialog
))){
511 case GTK_RESPONSE_ACCEPT
:
516 case IRTRANS_NEW_CMD
:
518 /* learn dialog for new button */
519 irtrans_command_learn_dlg(
523 /* refresh command list */
524 success
= irtrans_create_command_list(
528 gtk_scrolled_window_add_with_viewport(
529 GTK_SCROLLED_WINDOW(sw
),
532 gtk_widget_show_all(dialog
);
534 irreco_error_dlg(parent
,
535 _("Unable to load command list!"));
539 case IRTRANS_RELEARN_CMD
:
541 if(command_dialog
->command_selection_index
!= 0) {
542 command_dialog
->plugin
->command
= g_strdup(
547 command_selection_index
));
549 /*IRRECO_PRINTF("Cmd_dlg command: %s\n",
550 command_dialog->plugin->command);*/
551 if(command_dialog
->plugin
->command
!= NULL
){
553 irtrans_command_relearn_dlg(
558 irreco_info_dlg(parent
,
559 _("No reteachable commands!"));
563 case IRTRANS_DEL_CMD
:
565 irreco_info_dlg(parent
, _("IRTrans does not "
566 "support deleting commands."));
571 gtk_widget_destroy(dialog
);
572 irtrans_command_dlg_destroy(command_dialog
);
573 IRRECO_RETURN_BOOL(rvalue
);