Trunk cleanup: irtrans
[irreco.git] / backend / irtrans / src / irtrans_command_dlg.c
bloba29745ec3e7dc1f5a1a9b56e95be3a0dd99ff1b4
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_command_dlg.h"
25 static IrTransCommandDlg *irtrans_command_dlg_create(void)
27 IRRECO_ENTER
28 IRRECO_RETURN_PTR(g_slice_new0(IrTransCommandDlg));
31 static void irtrans_command_dlg_destroy(IrTransCommandDlg * command_dialog)
33 IRRECO_ENTER
35 g_slice_free(IrTransCommandDlg, command_dialog);
37 IRRECO_RETURN
40 static gboolean
41 irtrans_learn_command_callback(IrTransCommandDlg *command_dialog)
43 IrrecoBackendStatus status;
44 GtkWindow *parent;
45 IRRECO_ENTER
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);
55 } else {
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);
69 /*
70 if (status == IRRECO_BACKEND_OK) {
71 gtk_dialog_response(GTK_DIALOG(parent), GTK_RESPONSE_REJECT);
72 } else {
73 irreco_info_dlg(parent, _("IR code was not recieved."));
77 IRRECO_RETURN_BOOL(FALSE)
80 static void irtrans_learn_command(IrTransCommandDlg *command_dialog)
82 GString *message;
83 GtkWindow *parent;
84 IRRECO_ENTER
86 if (command_dialog->learn_dialog != NULL) {
87 parent = GTK_WINDOW(command_dialog->learn_dialog);
88 } else {
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,
106 command_dialog);
108 IRRECO_RETURN
111 #if 0
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;
120 IRRECO_ENTER
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,
129 TIMEOUT);
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);
135 } else {
136 gtk_dialog_response(GTK_DIALOG(command_dialog->relearn_dialog),
137 GTK_RESPONSE_REJECT);
140 IRRECO_RETURN_PTR(0);
142 #endif
146 * Get chosen command from the list pointed by selection_index.
148 static gchar *get_command_iter(GtkWidget *treeview, gint selection_index)
150 GtkTreeIter iter;
151 GtkTreeModel *model = NULL;
152 GtkTreePath *path = NULL;
153 gchar *str_data = NULL;
155 IRRECO_ENTER
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)
169 GtkTreeIter iter;
170 GtkTreeModel *model = NULL;
171 GtkTreePath *path = NULL;
172 gint *path_indices = NULL;
174 IRRECO_ENTER
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);
182 } else {
183 command_dialog->command_selection_index = -1;
186 IRRECO_RETURN
189 static gboolean irtrans_load_command_list(IrTransCommandDlg * command_dialog)
191 GtkTreeIter iter;
192 IrrecoBackendStatus status;
193 const gchar *command;
194 IRRECO_ENTER
196 status = irtrans_wrap_get_command_list(
197 command_dialog->plugin->irtrans_wrap,
198 command_dialog->plugin->remote,
199 NULL);
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,
205 &iter, NULL);
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;
218 gboolean success;
220 IRRECO_ENTER
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(
230 NRO_COLUMNS,
231 G_TYPE_STRING);
232 /* fill the treestore with data */
233 success = irtrans_load_command_list(command_dialog);
235 if (success){
237 command_dialog->command_treeview = gtk_tree_view_new_with_model(
238 GTK_TREE_MODEL(
239 command_dialog->
240 command_treestore));
241 g_object_unref(G_OBJECT(command_dialog->command_treestore));
243 /* setup column */
244 renderer = gtk_cell_renderer_text_new();
245 column = gtk_tree_view_column_new_with_attributes(
246 NULL, renderer,
247 "text", 0, NULL);
248 gtk_tree_view_append_column(GTK_TREE_VIEW(command_dialog->
249 command_treeview),
250 column);
251 /* set selection callback */
252 select = gtk_tree_view_get_selection(GTK_TREE_VIEW(
253 command_dialog->
254 command_treeview));
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),
258 command_dialog);
259 IRRECO_RETURN_BOOL(TRUE);
260 } else {
261 IRRECO_RETURN_BOOL(FALSE);
266 static void show_command_learn_banner(IrTransCommandDlg * command_dialog,
267 GtkWindow * parent)
269 IRRECO_ENTER
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!")));
275 IRRECO_RETURN
279 /****************************************************************/
280 /* */
281 /* */
282 /* DIALOGS */
283 /* */
284 /* */
285 /****************************************************************/
287 static gboolean irtrans_command_learn_dlg(IrTransCommandDlg *command_dialog,
288 GtkWindow * parent)
290 GtkWidget *command_table = NULL;
291 GtkWidget *command_entry = NULL;
292 gchar *command_buffer = NULL;
293 /*GThread *thread = NULL;*/
294 gint rvalue = -1;
296 IRRECO_ENTER
298 /* create objects */
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: ")),
309 0, 1, 0, 1);
310 gtk_table_attach_defaults(GTK_TABLE(command_table),
311 command_entry,
312 1, 2, 0, 1);
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);
319 while(rvalue == -1){
320 switch(gtk_dialog_run(GTK_DIALOG(command_dialog->learn_dialog))){
321 case GTK_RESPONSE_REJECT:
323 rvalue = FALSE;
324 break;
326 case IRTRANS_LEARN_CMD:
328 /* get command name */
329 command_buffer = g_strdup(gtk_entry_get_text
330 (GTK_ENTRY(
331 command_entry)));
333 if(g_utf8_strlen(command_buffer, -1) < 1){
334 irreco_info_dlg(parent,
335 _("Insert valid command name!"));
336 g_free(command_buffer);
337 break;
338 } else {
339 /* convert all characters to lowercase */
340 command_dialog->plugin->command =
341 g_utf8_strdown(
342 command_buffer,
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);
357 #if 0
358 show_command_learn_banner(
359 command_dialog, parent);
361 /* create thread for */
362 /* set_command function */
364 if((thread = g_thread_create(
365 (GThreadFunc)
366 irtrans_set_command,
367 (void *)command_dialog,
368 FALSE, NULL)) == NULL){
372 } else {
373 irreco_info_dlg(parent,
374 _("Choose device!"));
375 rvalue = TRUE;
377 break;
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,
395 GtkWindow * parent)
397 /*GThread *thread = NULL;*/
398 gchar *header = NULL;
399 gint rvalue = -1;
401 command_dialog->learn_dialog = NULL;
403 IRRECO_ENTER
405 header = g_strjoin(" ", "Confirm relearn: ", command_dialog->
406 plugin->command,
407 "?", NULL);
409 /* create objects */
410 command_dialog->relearn_dialog = gtk_dialog_new_with_buttons(
411 header, parent,
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);
421 while(rvalue == -1){
422 switch(gtk_dialog_run(GTK_DIALOG(
423 command_dialog->relearn_dialog))){
425 case GTK_RESPONSE_REJECT:
426 rvalue = FALSE;
427 break;
429 case GTK_RESPONSE_ACCEPT:
431 irtrans_learn_command(command_dialog);
433 #if 0
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){
445 break;
449 /* destroy hildon_banner */
451 if(command_dialog->hildon_banner != NULL){
453 gtk_widget_destroy(GTK_WIDGET(command_dialog->hildon_banner));
457 g_free(header);
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;
468 gint rvalue = -1;
469 gboolean success;
470 /*gboolean start;*/
472 IRRECO_ENTER
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);
496 if (success){
498 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw),
499 command_dialog->
500 command_treeview);
501 } else {
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);
508 while(rvalue == -1){
509 switch(gtk_dialog_run(GTK_DIALOG(dialog))){
511 case GTK_RESPONSE_ACCEPT:
513 rvalue = TRUE;
514 break;
516 case IRTRANS_NEW_CMD:
518 /* learn dialog for new button */
519 irtrans_command_learn_dlg(
520 command_dialog,
521 parent);
523 /* refresh command list */
524 success = irtrans_create_command_list(
525 command_dialog);
526 if (success){
528 gtk_scrolled_window_add_with_viewport(
529 GTK_SCROLLED_WINDOW(sw),
530 command_dialog->
531 command_treeview);
532 gtk_widget_show_all(dialog);
533 } else {
534 irreco_error_dlg(parent,
535 _("Unable to load command list!"));
537 break;
539 case IRTRANS_RELEARN_CMD:
541 if(command_dialog->command_selection_index != 0) {
542 command_dialog->plugin->command = g_strdup(
543 get_command_iter(
544 command_dialog->
545 command_treeview,
546 command_dialog->
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(
554 command_dialog,
555 parent);
556 break;
557 } else {
558 irreco_info_dlg(parent,
559 _("No reteachable commands!"));
560 break;
563 case IRTRANS_DEL_CMD:
565 irreco_info_dlg(parent, _("IRTrans does not "
566 "support deleting commands."));
567 break;
571 gtk_widget_destroy(dialog);
572 irtrans_command_dlg_destroy(command_dialog);
573 IRRECO_RETURN_BOOL(rvalue);