Clean code
[irreco.git] / irreco / src / core / irreco_device_dlg.c
blob10e613f1e99bb6be62f41576e5a35210c23eb04e
1 /*
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"
26 /**
27 * @addtogroup IrrecoDeviceDlg
28 * @ingroup Irreco
30 * Dialog for creating, deleting and editing backend devices.
32 * @{
35 /**
36 * @file
37 * Source file of @ref IrrecoDeviceDlg.
41 G_DEFINE_TYPE (IrrecoDeviceDlg, irreco_device_dlg, IRRECO_TYPE_DLG)
44 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
45 /* Datatypes */
46 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
48 enum {
49 IRRECO_DEVICE_NEW,
50 IRRECO_DEVICE_EDIT,
51 IRRECO_DEVICE_DELETE,
52 IRRECO_DEVICE_UPLOAD,
53 IRRECO_DEVICE_REFERESH,
54 IRRECO_DEVICE_OK
59 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
60 /* Prototypes */
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 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
73 /**
74 * @name Construction & Destruction
75 * @{
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)
99 GtkWidget *padding;
100 GtkTreeSelection *select;
101 IRRECO_ENTER
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,
115 NULL);
117 /* Add widgets. */
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),
131 self);
133 IRRECO_RETURN
136 GtkWidget * irreco_device_dlg_new(GtkWindow *parent)
138 IrrecoDeviceDlg *self;
139 IRRECO_ENTER
141 self = g_object_new(IRRECO_TYPE_DEVICE_DLG, NULL);
142 irreco_dlg_set_parent(IRRECO_DLG(self), parent);
144 IRRECO_RETURN_PTR(self);
147 /** @} */
151 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
152 /* Private Functions */
153 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
156 * @name Private Functions
157 * @{
161 * Populate listbox with devices.
163 static void irreco_device_dlg_populate(IrrecoDeviceDlg *self)
165 GString *str = NULL;
166 IRRECO_ENTER
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(
178 instance),
179 device->name);
180 IRRECO_DEBUG("Adding \"%s\" to listbox.\n", str->str);
181 irreco_listbox_text_append(IRRECO_LISTBOX_TEXT(
182 self->listbox),
183 str->str, device);
185 IRRECO_BACKEND_INSTANCE_FOREACH_END
186 IRRECO_BACKEND_MANAGER_FOREACH_END
188 g_string_free(str, TRUE);
189 IRRECO_RETURN
192 void irreco_device_dlg_responce_new(IrrecoDeviceDlg *self)
194 IrrecoBackendInstance *instance = NULL;
195 IRRECO_ENTER
197 if (irreco_show_select_instance_dlg(self->irreco_data,
198 GTK_WINDOW(self),
199 NULL,
200 &instance)) {
201 if (irreco_backend_instance_api_edit(instance)) {
202 irreco_backend_instance_create_device(instance,
203 GTK_WINDOW(self));
204 irreco_backend_instance_get_devcmd_list(instance);
205 irreco_device_dlg_populate(self);
206 } else {
207 const gchar *desc = NULL;
208 desc = irreco_backend_instance_get_name_and_description(
209 instance);
210 irreco_info_dlg_printf( GTK_WINDOW( self ),
211 _( "%s\ndoes not support\n"
212 "creating a new device." ),
213 desc);
217 IRRECO_RETURN
220 void irreco_device_dlg_responce_edit(IrrecoDeviceDlg *self)
222 IrrecoBackendDevice *device = NULL;
223 IRRECO_ENTER
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);
235 IRRECO_RETURN
238 void irreco_device_dlg_responce_delete(IrrecoDeviceDlg *self)
240 IrrecoBackendDevice *device = NULL;
241 IRRECO_ENTER
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);
252 IRRECO_RETURN
255 void irreco_device_dlg_responce_upload(IrrecoDeviceDlg *self)
257 IrrecoBackendFileContainer *file_container = NULL;
258 IrrecoBackendDevice *device = NULL;
259 IRRECO_ENTER
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,
266 device->name,
267 &file_container)) {
268 irreco_show_webdb_upload_dlg(self->irreco_data,
269 GTK_WINDOW(self),
270 file_container);
271 irreco_backend_file_container_free(file_container);
274 IRRECO_RETURN
277 void irreco_device_dlg_responce_referesh(IrrecoDeviceDlg *self)
279 IRRECO_ENTER
281 irreco_backend_manager_get_devcmd_lists(
282 self->irreco_data->irreco_backend_manager);
283 irreco_device_dlg_populate(self);
285 IRRECO_RETURN
288 /** @} */
292 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
293 /* Functions */
294 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
297 * @name Public Functions
298 * @{
301 void irreco_show_device_dlg(IrrecoData *irreco_data,
302 GtkWindow *parent)
304 IrrecoDeviceDlg *self = NULL;
305 gboolean loop = TRUE;
306 IRRECO_ENTER
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 ));
313 while (loop == TRUE)
315 switch ( gtk_dialog_run( GTK_DIALOG( self ))) {
316 case IRRECO_DEVICE_NEW:
317 irreco_device_dlg_responce_new( self );
318 break;
320 case IRRECO_DEVICE_EDIT:
321 irreco_device_dlg_responce_edit( self );
322 break;
324 case IRRECO_DEVICE_DELETE:
325 irreco_device_dlg_responce_delete( self );
326 break;
328 case IRRECO_DEVICE_UPLOAD:
329 irreco_device_dlg_responce_upload( self );
330 break;
332 case IRRECO_DEVICE_REFERESH:
333 irreco_device_dlg_responce_referesh( self );
334 break;
336 case IRRECO_DEVICE_OK:
337 loop = FALSE;
338 break;
341 gtk_widget_grab_focus( self->listbox->tree_view );
344 gtk_widget_destroy( GTK_WIDGET( self ));
347 /** @} */
351 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
352 /* Events and Callbacks */
353 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
356 * @name Events and Callbacks
357 * @{
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;
370 IRRECO_ENTER
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);
383 } else {
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);
390 } else {
391 gtk_widget_set_sensitive(upload_button, FALSE);
394 IRRECO_RETURN
397 /** @} */
398 /** @} */