Trunk cleanup: dummy
[irreco.git] / backend / irtrans / trunk / src / irtrans_remote_dlg.c
blobc1235c96a3d1840e0fcba1cac0b5cd2503360548
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_remote_dlg.h"
24 #include "irtrans_command_dlg.h"
26 static IrTransRemoteDlg *irtrans_remote_dlg_create(void)
28 IRRECO_ENTER
29 IRRECO_RETURN_PTR(g_slice_new0(IrTransRemoteDlg));
32 static void irtrans_remote_dlg_destroy(IrTransRemoteDlg * remote_dialog)
34 IRRECO_ENTER
36 g_slice_free(IrTransRemoteDlg, remote_dialog);
38 IRRECO_RETURN
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)
47 IRRECO_ENTER
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;
56 IRRECO_ENTER
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)
67 GtkTreeIter iter;
68 const gchar *remote;
69 IrrecoBackendStatus status;
70 IRRECO_ENTER
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,
79 &iter, NULL);
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)
92 GtkTreeIter iter;
93 GtkTreeModel *model = NULL;
94 GtkTreePath *path = NULL;
95 gchar *str_data = NULL;
97 IRRECO_ENTER
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)
111 GtkTreeIter iter;
112 GtkTreeModel *model = NULL;
113 GtkTreePath *path = NULL;
114 gint *path_indices = NULL;
116 IRRECO_ENTER
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);
124 } else {
125 remote_dialog->remote_selection_index = -1;
128 IRRECO_RETURN
131 static gboolean irtrans_create_remote_list(IrTransRemoteDlg * remote_dialog)
133 GtkTreeSelection *select = NULL;
134 GtkTreeViewColumn *column = NULL;
135 GtkCellRenderer *renderer = NULL;
136 gboolean success;
138 IRRECO_ENTER
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,
148 G_TYPE_STRING);
149 /* fill the treestore with data */
150 success = irtrans_load_device_list(remote_dialog);
152 if (success){
154 remote_dialog->remote_treeview = gtk_tree_view_new_with_model(
155 GTK_TREE_MODEL(
156 remote_dialog->
157 remote_treestore));
158 g_object_unref(G_OBJECT(remote_dialog->remote_treestore));
160 /* setup column */
161 renderer = gtk_cell_renderer_text_new();
162 column = gtk_tree_view_column_new_with_attributes(NULL,
163 renderer,
164 "text",
165 0, NULL);
166 gtk_tree_view_append_column(GTK_TREE_VIEW(remote_dialog->
167 remote_treeview),
168 column);
169 /* set selection callback */
170 select = gtk_tree_view_get_selection(GTK_TREE_VIEW(
171 remote_dialog->
172 remote_treeview));
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),
176 remote_dialog);
177 IRRECO_RETURN_BOOL(TRUE);
178 } else {
179 IRRECO_RETURN_BOOL(FALSE);
183 /****************************************************************/
184 /* */
185 /* */
186 /* DIALOGS */
187 /* */
188 /* */
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;
197 gint rvalue = -1;
198 gboolean success;
200 IRRECO_ENTER
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,
209 NULL);
210 table = gtk_table_new(2, 2, FALSE);
211 remote_entry = gtk_entry_new();
213 /* build dialog. */
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);
222 while(rvalue == -1){
223 switch(gtk_dialog_run(GTK_DIALOG(dialog))){
225 case GTK_RESPONSE_REJECT:
227 rvalue = FALSE;
228 break;
230 case GTK_RESPONSE_ACCEPT:
232 /* get device name from the entry */
233 device_buffer = g_strdup(gtk_entry_get_text(
234 GTK_ENTRY(
235 remote_entry)));
237 if(g_utf8_strlen(device_buffer, -1) < 1){
239 irreco_info_dlg(parent,
240 _("Insert valid devicename!"));
241 g_free(device_buffer);
242 break;
243 } else {
244 /* convert all characters to lowercase */
245 plugin->remote = g_utf8_strdown(
246 device_buffer,
247 strlen(
248 device_buffer));
249 g_free(device_buffer);
251 /* learn remotename */
252 success = irtrans_set_device_name(
253 plugin);
255 if(!success){
257 irreco_error_dlg(parent,
258 _("Remote learn failed!"));
259 break;
261 rvalue = TRUE;
262 break;
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;
277 gint rvalue = -1;
278 gboolean success;
280 IRRECO_ENTER
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),
298 GTK_POLICY_NEVER,
299 GTK_POLICY_AUTOMATIC);
301 success = irtrans_create_remote_list(remote_dialog);
303 /* build dialog */
304 if (success){
306 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw),
307 remote_dialog->
308 remote_treeview);
309 } else {
310 irreco_info_dlg(parent, _("Unable to load devices list!\n"));
312 /* exit from the dialog */
313 rvalue = TRUE;
316 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), sw);
317 gtk_widget_show_all(dialog);
319 while(rvalue == -1){
320 switch(gtk_dialog_run(GTK_DIALOG(dialog))){
322 case GTK_RESPONSE_ACCEPT:
324 rvalue = TRUE;
325 break;
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(
334 remote_dialog);
336 if (success){
338 gtk_scrolled_window_add_with_viewport(
339 GTK_SCROLLED_WINDOW(sw),
340 remote_dialog->
341 remote_treeview);
343 gtk_widget_show_all(dialog);
344 } else {
345 irreco_info_dlg(parent,
346 _("Unable to load devices list!\n"));
348 break;
350 case IRTRANS_EDIT_DEV:
352 /* get chosen device */
353 plugin->remote = g_strdup(
354 get_remote_iter(
355 remote_dialog->
356 remote_treeview,
357 remote_dialog->
358 remote_selection_index));
360 if(plugin->remote != NULL){
362 /* enter editing remote file */
363 irtrans_command_dlg(plugin, parent);
365 } else {
366 irreco_info_dlg(parent,
367 _("No editable devices!!"));
368 break;
370 break;
372 case IRTRANS_DEL_DEV:
374 irreco_info_dlg(parent, _("IRTrans does not "
375 "support deleting devices."));
376 break;
380 irtrans_remote_dlg_destroy(remote_dialog);
381 gtk_widget_destroy(dialog);
382 IRRECO_RETURN_BOOL(rvalue);