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_remote_dlg.h"
24 #include "irtrans_command_dlg.h"
26 static IrTransRemoteDlg
*irtrans_remote_dlg_create(void)
29 IRRECO_RETURN_PTR(g_slice_new0(IrTransRemoteDlg
));
32 static void irtrans_remote_dlg_destroy(IrTransRemoteDlg
* remote_dialog
)
36 g_slice_free(IrTransRemoteDlg
, remote_dialog
);
42 * Sends remote name to the irserver which creates config-file (name.rem)
43 * into the remotes folder.
45 static gboolean
irtrans_set_device_name(IrTransPlugin
* plugin
)
49 /* This command will return error code, but it will create a new
50 remote regardless of that. */
51 irtrans_wrap_learn_command(plugin
->irtrans_wrap
, plugin
->remote
, "", 0);
52 IRRECO_RETURN_BOOL(TRUE
);
55 IrrecoBackendStatus status;
58 status = irtrans_wrap_learn_command(plugin->irtrans_wrap,
59 plugin->remote, "", 0);
60 if (status != IRRECO_BACKEND_OK) IRRECO_RETURN_BOOL(FALSE);
61 IRRECO_RETURN_BOOL(TRUE);
65 static gboolean
irtrans_load_device_list(IrTransRemoteDlg
* remote_dialog
)
69 IrrecoBackendStatus status
;
72 status
= irtrans_wrap_get_remote_list(
73 remote_dialog
->plugin
->irtrans_wrap
, NULL
);
74 if (status
!= IRRECO_BACKEND_OK
) IRRECO_RETURN_BOOL(FALSE
);
76 while (irtrans_wrap_get_from_list(
77 remote_dialog
->plugin
->irtrans_wrap
, &remote
)) {
78 gtk_tree_store_append(remote_dialog
->remote_treestore
,
80 gtk_tree_store_set(remote_dialog
->remote_treestore
,
81 &iter
, REMOTE_COLUMN
, remote
, -1);
84 IRRECO_RETURN_BOOL(TRUE
);
88 * Get chosen device name from the list pointed by selection_index.
90 static gchar
*get_remote_iter(GtkWidget
*treeview
, gint selection_index
)
93 GtkTreeModel
*model
= NULL
;
94 GtkTreePath
*path
= NULL
;
95 gchar
*str_data
= NULL
;
99 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(treeview
));
100 path
= gtk_tree_path_new_from_indices(selection_index
, -1);
101 gtk_tree_model_get_iter(model
, &iter
, path
);
102 gtk_tree_model_get(model
, &iter
, REMOTE_COLUMN
, &str_data
, -1);
103 gtk_tree_path_free(path
);
105 IRRECO_RETURN_STR(str_data
);
108 static void irtrans_remote_list_selection_changed(GtkTreeSelection
* selection
,
109 IrTransRemoteDlg
* remote_dialog
)
112 GtkTreeModel
*model
= NULL
;
113 GtkTreePath
*path
= NULL
;
114 gint
*path_indices
= NULL
;
118 if(gtk_tree_selection_get_selected(selection
, &model
, &iter
)){
120 path
= gtk_tree_model_get_path(model
, &iter
);
121 path_indices
= gtk_tree_path_get_indices(path
);
122 remote_dialog
->remote_selection_index
= path_indices
[0];
123 gtk_tree_path_free(path
);
125 remote_dialog
->remote_selection_index
= -1;
131 static gboolean
irtrans_create_remote_list(IrTransRemoteDlg
* remote_dialog
)
133 GtkTreeSelection
*select
= NULL
;
134 GtkTreeViewColumn
*column
= NULL
;
135 GtkCellRenderer
*renderer
= NULL
;
140 /* if former widget exists destroy it */
141 if(remote_dialog
->remote_treeview
!= NULL
){
143 gtk_widget_destroy(remote_dialog
->remote_treeview
);
146 /* create GtkTreeStore and GtkTreeView */
147 remote_dialog
->remote_treestore
= gtk_tree_store_new(NR_COLUMNS
,
149 /* fill the treestore with data */
150 success
= irtrans_load_device_list(remote_dialog
);
154 remote_dialog
->remote_treeview
= gtk_tree_view_new_with_model(
158 g_object_unref(G_OBJECT(remote_dialog
->remote_treestore
));
161 renderer
= gtk_cell_renderer_text_new();
162 column
= gtk_tree_view_column_new_with_attributes(NULL
,
166 gtk_tree_view_append_column(GTK_TREE_VIEW(remote_dialog
->
169 /* set selection callback */
170 select
= gtk_tree_view_get_selection(GTK_TREE_VIEW(
173 gtk_tree_selection_set_mode(select
, GTK_SELECTION_SINGLE
);
174 g_signal_connect(G_OBJECT(select
), "changed", G_CALLBACK(
175 irtrans_remote_list_selection_changed
),
177 IRRECO_RETURN_BOOL(TRUE
);
179 IRRECO_RETURN_BOOL(FALSE
);
183 /****************************************************************/
189 /****************************************************************/
191 gboolean
irtrans_remote_learn_dlg(IrTransPlugin
* plugin
, GtkWindow
* parent
)
193 GtkWidget
*dialog
= NULL
;
194 GtkWidget
*table
= NULL
;
195 GtkWidget
*remote_entry
= NULL
;
196 gchar
*device_buffer
= NULL
;
202 /* create objects. */
203 dialog
= gtk_dialog_new_with_buttons(
204 _("IRTrans device learn dialog"), parent
,
205 GTK_DIALOG_MODAL
| GTK_DIALOG_DESTROY_WITH_PARENT
|
206 GTK_DIALOG_NO_SEPARATOR
,
207 GTK_STOCK_OK
, GTK_RESPONSE_ACCEPT
,
208 GTK_STOCK_CANCEL
, GTK_RESPONSE_REJECT
,
210 table
= gtk_table_new(2, 2, FALSE
);
211 remote_entry
= gtk_entry_new();
214 gtk_table_attach_defaults(GTK_TABLE(table
),
215 gtk_label_new(_("Insert device name: ")), 0, 1, 0, 1);
216 gtk_table_attach_defaults(GTK_TABLE(table
), remote_entry
, 1, 2, 0, 1);
217 gtk_table_set_row_spacings(GTK_TABLE(table
), 5);
218 gtk_table_set_col_spacings(GTK_TABLE(table
), 5);
219 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog
)->vbox
), table
);
220 gtk_widget_show_all(dialog
);
223 switch(gtk_dialog_run(GTK_DIALOG(dialog
))){
225 case GTK_RESPONSE_REJECT
:
230 case GTK_RESPONSE_ACCEPT
:
232 /* get device name from the entry */
233 device_buffer
= g_strdup(gtk_entry_get_text(
237 if(g_utf8_strlen(device_buffer
, -1) < 1){
239 irreco_info_dlg(parent
,
240 _("Insert valid devicename!"));
241 g_free(device_buffer
);
244 /* convert all characters to lowercase */
245 plugin
->remote
= g_utf8_strdown(
249 g_free(device_buffer
);
251 /* learn remotename */
252 success
= irtrans_set_device_name(
257 irreco_error_dlg(parent
,
258 _("Remote learn failed!"));
267 gtk_widget_destroy(dialog
);
268 IRRECO_RETURN_BOOL(rvalue
);
271 gboolean
irtrans_remote_dlg(IrTransPlugin
* plugin
, GtkWindow
* parent
)
273 IrTransRemoteDlg
*remote_dialog
;
275 GtkWidget
*dialog
= NULL
;
276 GtkWidget
*sw
= NULL
;
282 remote_dialog
= irtrans_remote_dlg_create();
283 remote_dialog
->plugin
= plugin
;
284 remote_dialog
->remote_treeview
= NULL
;
286 /* create objects. */
287 dialog
= gtk_dialog_new_with_buttons(_("Devices"), parent
,
288 GTK_DIALOG_MODAL
| GTK_DIALOG_DESTROY_WITH_PARENT
|
289 GTK_DIALOG_NO_SEPARATOR
,
290 _("New"), IRTRANS_NEW_DEV
,
291 _("Edit"), IRTRANS_EDIT_DEV
,
292 _("Delete"), IRTRANS_DEL_DEV
,
293 _("Done"), GTK_RESPONSE_ACCEPT
, NULL
);
294 sw
= gtk_scrolled_window_new (NULL
, NULL
);
295 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw
),
296 GTK_SHADOW_ETCHED_IN
);
297 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw
),
299 GTK_POLICY_AUTOMATIC
);
301 success
= irtrans_create_remote_list(remote_dialog
);
306 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw
),
310 irreco_info_dlg(parent
, _("Unable to load devices list!\n"));
312 /* exit from the dialog */
316 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog
)->vbox
), sw
);
317 gtk_widget_show_all(dialog
);
320 switch(gtk_dialog_run(GTK_DIALOG(dialog
))){
322 case GTK_RESPONSE_ACCEPT
:
327 case IRTRANS_NEW_DEV
:
329 /* learn dialog for new device */
330 irtrans_remote_learn_dlg(plugin
, parent
);
332 /* refresh devicelist */
333 success
= irtrans_create_remote_list(
338 gtk_scrolled_window_add_with_viewport(
339 GTK_SCROLLED_WINDOW(sw
),
343 gtk_widget_show_all(dialog
);
345 irreco_info_dlg(parent
,
346 _("Unable to load devices list!\n"));
350 case IRTRANS_EDIT_DEV
:
352 /* get chosen device */
353 plugin
->remote
= g_strdup(
358 remote_selection_index
));
360 if(plugin
->remote
!= NULL
){
362 /* enter editing remote file */
363 irtrans_command_dlg(plugin
, parent
);
366 irreco_info_dlg(parent
,
367 _("No editable devices!!"));
372 case IRTRANS_DEL_DEV
:
374 irreco_info_dlg(parent
, _("IRTrans does not "
375 "support deleting devices."));
380 irtrans_remote_dlg_destroy(remote_dialog
);
381 gtk_widget_destroy(dialog
);
382 IRRECO_RETURN_BOOL(rvalue
);