2 * irreco - Ir Remote Control
3 * Copyright (C) 2007 Arto Karppinen (arto.karppinen@iki.fi)
4 * & 2008 Joni Kokko (t5kojo01@students.oamk.fi)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "irreco_device_dlg.h"
22 #include "irreco_backend_device.h"
23 #include "irreco_select_instance_dlg.h"
24 #include "irreco_webdb_upload_dlg.h"
27 * @addtogroup IrrecoDeviceDlg
30 * Dialog for creating, deleting and editing backend devices.
37 * Source file of @ref IrrecoDeviceDlg.
41 G_DEFINE_TYPE (IrrecoDeviceDlg
, irreco_device_dlg
, IRRECO_TYPE_DLG
)
44 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
46 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
53 IRRECO_DEVICE_REFERESH
,
59 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
61 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
63 static void irreco_device_dlg_populate(IrrecoDeviceDlg
*self
);
64 static void irreco_device_dlg_selection_changed(GtkTreeSelection
* selection
,
65 IrrecoDeviceDlg
*self
);
69 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
70 /* Construction & Destruction */
71 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
74 * @name Construction & Destruction
78 static void irreco_device_dlg_dispose (GObject
*object
)
80 if (G_OBJECT_CLASS (irreco_device_dlg_parent_class
)->dispose
)
81 G_OBJECT_CLASS (irreco_device_dlg_parent_class
)->dispose (object
);
84 static void irreco_device_dlg_finalize (GObject
*object
)
86 if (G_OBJECT_CLASS (irreco_device_dlg_parent_class
)->finalize
)
87 G_OBJECT_CLASS (irreco_device_dlg_parent_class
)->finalize (object
);
90 static void irreco_device_dlg_class_init (IrrecoDeviceDlgClass
*klass
)
92 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
93 object_class
->dispose
= irreco_device_dlg_dispose
;
94 object_class
->finalize
= irreco_device_dlg_finalize
;
97 static void irreco_device_dlg_init (IrrecoDeviceDlg
*self
)
100 GtkTreeSelection
*select
;
103 /* Construct dialog. */
104 gtk_window_set_title(GTK_WINDOW(self
), _("Devices"));
105 gtk_window_set_modal(GTK_WINDOW(self
), TRUE
);
106 gtk_window_set_destroy_with_parent(GTK_WINDOW(self
), TRUE
);
107 gtk_dialog_set_has_separator(GTK_DIALOG(self
), FALSE
);
108 gtk_dialog_add_buttons(GTK_DIALOG(self
),
109 GTK_STOCK_NEW
, IRRECO_DEVICE_NEW
,
110 GTK_STOCK_EDIT
, IRRECO_DEVICE_EDIT
,
111 GTK_STOCK_DELETE
, IRRECO_DEVICE_DELETE
,
112 _("Upload"), IRRECO_DEVICE_UPLOAD
,
113 GTK_STOCK_REFRESH
, IRRECO_DEVICE_REFERESH
,
114 GTK_STOCK_OK
, IRRECO_DEVICE_OK
,
118 self
->vbox
= gtk_vbox_new(FALSE
, 0);
119 self
->listbox
= IRRECO_LISTBOX(
120 irreco_listbox_text_new_with_autosize(0, 700, 100, 250));
122 gtk_container_add(GTK_CONTAINER(self
->vbox
), GTK_WIDGET(self
->listbox
));
123 padding
= irreco_gtk_pad(self
->vbox
, 8, 8, 8, 8);
124 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(self
)->vbox
), padding
);
126 /* Selection signal. */
127 select
= gtk_tree_view_get_selection(
128 GTK_TREE_VIEW(self
->listbox
->tree_view
));
129 g_signal_connect(G_OBJECT(select
), "changed",
130 G_CALLBACK(irreco_device_dlg_selection_changed
),
136 GtkWidget
* irreco_device_dlg_new(GtkWindow
*parent
)
138 IrrecoDeviceDlg
*self
;
141 self
= g_object_new(IRRECO_TYPE_DEVICE_DLG
, NULL
);
142 irreco_dlg_set_parent(IRRECO_DLG(self
), parent
);
144 IRRECO_RETURN_PTR(self
);
151 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
152 /* Private Functions */
153 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
156 * @name Private Functions
161 * Populate listbox with devices.
163 static void irreco_device_dlg_populate(IrrecoDeviceDlg
*self
)
168 str
= g_string_new(NULL
);
170 irreco_listbox_clear(self
->listbox
);
171 IRRECO_BACKEND_MANAGER_FOREACH_INSTANCE(
172 self
->irreco_data
->irreco_backend_manager
, instance
)
173 irreco_backend_instance_get_name_and_description(instance
);
174 IRRECO_BACKEND_INSTANCE_FOREACH(instance
, device
)
176 g_string_printf(str
, "%s - %s",
177 irreco_backend_instance_get_name_and_description(
180 IRRECO_DEBUG("Adding \"%s\" to listbox.\n", str
->str
);
181 irreco_listbox_text_append(IRRECO_LISTBOX_TEXT(
185 IRRECO_BACKEND_INSTANCE_FOREACH_END
186 IRRECO_BACKEND_MANAGER_FOREACH_END
188 g_string_free(str
, TRUE
);
192 void irreco_device_dlg_responce_new(IrrecoDeviceDlg
*self
)
194 IrrecoBackendInstance
*instance
= NULL
;
197 if (irreco_show_select_instance_dlg(self
->irreco_data
,
201 if (irreco_backend_instance_api_edit(instance
)) {
202 irreco_backend_instance_create_device(instance
,
204 irreco_backend_instance_get_devcmd_list(instance
);
205 irreco_device_dlg_populate(self
);
207 const gchar
*desc
= NULL
;
208 desc
= irreco_backend_instance_get_name_and_description(
210 irreco_info_dlg_printf( GTK_WINDOW( self
),
211 _( "%s\ndoes not support\n"
212 "creating a new device." ),
220 void irreco_device_dlg_responce_edit(IrrecoDeviceDlg
*self
)
222 IrrecoBackendDevice
*device
= NULL
;
225 device
= (IrrecoBackendDevice
*) irreco_listbox_get_selection_data(
226 IRRECO_LISTBOX(self
->listbox
));
227 if (device
== NULL
) IRRECO_RETURN
;
228 if (!irreco_backend_device_is_editable(device
)) IRRECO_RETURN
;
230 irreco_backend_device_edit(device
, GTK_WINDOW(self
));
231 irreco_backend_instance_save_to_conf(device
->backend_instance
);
232 irreco_backend_instance_get_devcmd_list(device
->backend_instance
);
233 irreco_device_dlg_populate(self
);
238 void irreco_device_dlg_responce_delete(IrrecoDeviceDlg
*self
)
240 IrrecoBackendDevice
*device
= NULL
;
243 device
= (IrrecoBackendDevice
*) irreco_listbox_get_selection_data(
244 IRRECO_LISTBOX(self
->listbox
));
245 if (device
== NULL
) IRRECO_RETURN
;
246 if (!irreco_backend_device_is_editable(device
)) IRRECO_RETURN
;
248 irreco_backend_device_delete(device
, GTK_WINDOW(self
));
249 irreco_backend_instance_get_devcmd_list(device
->backend_instance
);
250 irreco_device_dlg_populate(self
);
255 void irreco_device_dlg_responce_upload(IrrecoDeviceDlg
*self
)
257 IrrecoBackendFileContainer
*file_container
= NULL
;
258 IrrecoBackendDevice
*device
= NULL
;
261 device
= (IrrecoBackendDevice
*) irreco_listbox_get_selection_data(
262 IRRECO_LISTBOX(self
->listbox
));
263 if (device
== NULL
) IRRECO_RETURN
;
265 if (irreco_backend_instance_export_conf(device
->backend_instance
,
268 irreco_show_webdb_upload_dlg(self
->irreco_data
,
271 irreco_backend_file_container_free(file_container
);
277 void irreco_device_dlg_responce_referesh(IrrecoDeviceDlg
*self
)
281 irreco_backend_manager_get_devcmd_lists(
282 self
->irreco_data
->irreco_backend_manager
);
283 irreco_device_dlg_populate(self
);
292 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
294 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
297 * @name Public Functions
301 void irreco_show_device_dlg(IrrecoData
*irreco_data
,
304 IrrecoDeviceDlg
*self
= NULL
;
305 gboolean loop
= TRUE
;
308 self
= IRRECO_DEVICE_DLG( irreco_device_dlg_new( parent
));
309 self
->irreco_data
= irreco_data
;
310 irreco_device_dlg_populate( self
);
311 gtk_widget_show_all( GTK_WIDGET( self
));
315 switch ( gtk_dialog_run( GTK_DIALOG( self
))) {
316 case IRRECO_DEVICE_NEW
:
317 irreco_device_dlg_responce_new( self
);
320 case IRRECO_DEVICE_EDIT
:
321 irreco_device_dlg_responce_edit( self
);
324 case IRRECO_DEVICE_DELETE
:
325 irreco_device_dlg_responce_delete( self
);
328 case IRRECO_DEVICE_UPLOAD
:
329 irreco_device_dlg_responce_upload( self
);
332 case IRRECO_DEVICE_REFERESH
:
333 irreco_device_dlg_responce_referesh( self
);
336 case IRRECO_DEVICE_OK
:
341 gtk_widget_grab_focus( self
->listbox
->tree_view
);
344 gtk_widget_destroy( GTK_WIDGET( self
));
351 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
352 /* Events and Callbacks */
353 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
356 * @name Events and Callbacks
361 * Enable / disable buttons depending on editability.
363 static void irreco_device_dlg_selection_changed(GtkTreeSelection
*selection
,
364 IrrecoDeviceDlg
*self
)
366 IrrecoBackendDevice
*device
= NULL
;
367 GtkWidget
*edit_button
= NULL
;
368 GtkWidget
*delete_button
= NULL
;
369 GtkWidget
*upload_button
= NULL
;
372 device
= (IrrecoBackendDevice
*) irreco_listbox_get_selection_data(
373 IRRECO_LISTBOX(self
->listbox
));
374 if (device
== NULL
) IRRECO_RETURN
;
376 edit_button
= irreco_gtk_dialog_get_button(GTK_WIDGET(self
), 1);
377 delete_button
= irreco_gtk_dialog_get_button(GTK_WIDGET(self
), 2);
378 upload_button
= irreco_gtk_dialog_get_button(GTK_WIDGET(self
), 3);
380 if (irreco_backend_device_is_editable(device
)) {
381 gtk_widget_set_sensitive(edit_button
, TRUE
);
382 gtk_widget_set_sensitive(delete_button
, TRUE
);
384 gtk_widget_set_sensitive(edit_button
, FALSE
);
385 gtk_widget_set_sensitive(delete_button
, FALSE
);
388 if (irreco_backend_device_is_exportable(device
)) {
389 gtk_widget_set_sensitive(upload_button
, TRUE
);
391 gtk_widget_set_sensitive(upload_button
, FALSE
);