clean code and changed version nro.
[irreco.git] / irreco / src / core / irreco_backend_dlg.c
blobd2ac399172b8b4873d33f125be8b244189177da7
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007 Arto Karppinen (arto.karppinen@iki.fi)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "irreco_backend_dlg.h"
21 #include "irreco_config.h"
22 #include "irreco_backend_select_dlg.h"
24 /**
25 * @addtogroup IrrecoBackendDlg
26 * @ingroup Irreco
28 * Show a dialog where the user can create, destroy and configure backend
29 * instances.
31 * @{
34 /**
35 * @file
36 * Source file of @ref IrrecoBackendDlg.
41 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
42 /* Prototypes & Datatypes */
43 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
46 enum {
47 IRRECO_BACKEND_NEW = 1,
48 IRRECO_BACKEND_DELETE,
49 IRRECO_BACKEND_CONFIGURE
52 static void irreco_backend_dlg_response_new(IrrecoBackendDlg *self);
53 static void irreco_backend_dlg_response_delete(IrrecoBackendDlg *self);
54 static void irreco_backend_dlg_response_configure(IrrecoBackendDlg *self);
58 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
59 /* Construction & Destruction */
60 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
62 /**
63 * @name Construction & Destruction
64 * @{
67 G_DEFINE_TYPE(IrrecoBackendDlg, irreco_backend_dlg, IRRECO_TYPE_DLG)
69 static void irreco_backend_dlg_finalize(GObject * object)
71 G_OBJECT_CLASS(irreco_backend_dlg_parent_class)->finalize(object);
74 static void irreco_backend_dlg_class_init(IrrecoBackendDlgClass * klass)
76 GObjectClass *object_class = G_OBJECT_CLASS(klass);
77 object_class->finalize = irreco_backend_dlg_finalize;
80 static void irreco_backend_dlg_init(IrrecoBackendDlg * self)
82 GtkWidget *align;
83 IRRECO_ENTER
85 /* Construct dialog. */
86 gtk_window_set_title(GTK_WINDOW(self), _("Device controllers"));
87 gtk_window_set_modal(GTK_WINDOW(self), TRUE);
88 gtk_window_set_destroy_with_parent(GTK_WINDOW(self), TRUE);
89 gtk_dialog_set_has_separator(GTK_DIALOG(self), FALSE);
90 gtk_dialog_add_buttons(GTK_DIALOG(self),
91 GTK_STOCK_NEW, IRRECO_BACKEND_NEW,
92 GTK_STOCK_DELETE, IRRECO_BACKEND_DELETE,
93 _("Configure"), IRRECO_BACKEND_CONFIGURE,
94 GTK_STOCK_OK, GTK_RESPONSE_OK,
95 NULL);
97 /* Create new listbox and add it to the dialog. */
98 self->listbox = irreco_listbox_text_new_with_autosize(0, 600, 100, 250);
99 align = irreco_gtk_align(self->listbox, 0, 0, 1, 1, 8, 8, 8, 8);
100 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(self)->vbox),
101 align, 0, 0, 0);
102 gtk_widget_show_all(GTK_WIDGET(self));
103 IRRECO_RETURN
106 GtkWidget *irreco_backend_dlg_new(IrrecoData *irreco_data,
107 GtkWindow *parent)
109 IrrecoBackendDlg *self;
110 IRRECO_ENTER
112 self = g_object_new(IRRECO_TYPE_BACKEND_DLG, NULL);
113 irreco_dlg_set_parent(IRRECO_DLG(self), parent);
114 irreco_backend_dlg_set_irreco_data(self, irreco_data);
115 IRRECO_RETURN_PTR(self);
118 /** @} */
122 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
123 /* Private Functions */
124 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
127 * @name Private Functions
128 * @{
132 * Generate the listbox.
134 static void irreco_backend_dlg_populate_listbox(IrrecoBackendDlg *self)
136 IrrecoBackendManager *manager;
137 IRRECO_ENTER
139 irreco_listbox_clear(IRRECO_LISTBOX(self->listbox));
140 manager = self->irreco_data->irreco_backend_manager;
141 IRRECO_BACKEND_MANAGER_FOREACH_INSTANCE(manager, instance)
142 IRRECO_DEBUG("Adding backend instance \"%s\" to list.\n",
143 irreco_backend_instance_get_name(instance));
144 irreco_backend_instance_get_description(instance);
145 irreco_listbox_text_append(IRRECO_LISTBOX_TEXT(self->listbox),
146 irreco_backend_instance_get_name_and_description(
147 instance), NULL);
148 IRRECO_STRING_TABLE_FOREACH_END
149 IRRECO_RETURN
153 * Get the IrrecoBackendInstance which matches the current selection.
155 static gboolean
156 irreco_backend_dlg_get_instance(IrrecoBackendDlg *self,
157 IrrecoBackendInstance **instance)
159 gint sel_index;
160 IrrecoBackendManager *manager;
161 IRRECO_ENTER
163 manager = self->irreco_data->irreco_backend_manager;
164 sel_index = irreco_listbox_get_selection_index(
165 IRRECO_LISTBOX(self->listbox));
167 if (sel_index >= 0) {
168 if (irreco_string_table_index(manager->instance_table,
169 sel_index, NULL,
170 (gpointer *)instance)) {
171 IRRECO_RETURN_BOOL(TRUE);
172 } else {
173 IRRECO_ERROR("No instance found for index \"%i\"\n",
174 sel_index);
176 } else {
177 irreco_info_dlg(GTK_WINDOW(self),
178 _("Please select something."));
180 IRRECO_RETURN_BOOL(FALSE);
183 /** @} */
185 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
186 /* Functions */
187 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
190 * @name Public Functions
191 * @{
194 void irreco_show_backend_dlg(IrrecoData *irreco_data,
195 GtkWindow *parent)
197 gboolean loop = TRUE;
198 IrrecoBackendDlg *self;
199 IRRECO_ENTER
201 self = IRRECO_BACKEND_DLG(irreco_backend_dlg_new(irreco_data, parent));
202 do {
203 switch (gtk_dialog_run(GTK_DIALOG(self))) {
204 case IRRECO_BACKEND_NEW:
205 irreco_backend_dlg_response_new(self);
206 break;
208 case IRRECO_BACKEND_DELETE:
209 irreco_backend_dlg_response_delete(self);
210 break;
212 case IRRECO_BACKEND_CONFIGURE:
213 irreco_backend_dlg_response_configure(self);
214 break;
216 case GTK_RESPONSE_OK:
217 loop = FALSE;
218 break;
220 gtk_widget_grab_focus(self->listbox);
221 } while (loop == TRUE);
223 gtk_widget_destroy(GTK_WIDGET(self));
224 IRRECO_RETURN
227 void irreco_backend_dlg_set_irreco_data(IrrecoBackendDlg *self,
228 IrrecoData *irreco_data)
230 IRRECO_ENTER
231 self->irreco_data = irreco_data;
232 irreco_backend_dlg_populate_listbox(self);
233 IRRECO_RETURN
236 /**@} */
240 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
241 /* Events and Callbacks */
242 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
245 * @name Events and Callbacks
246 * @{
249 static void irreco_backend_dlg_response_new(IrrecoBackendDlg *self)
251 IrrecoBackendLib *lib;
252 IrrecoBackendManager *manager;
253 IrrecoBackendInstance *instance;
254 IRRECO_ENTER
256 manager = self->irreco_data->irreco_backend_manager;
257 if (irreco_show_backend_select_dlg(self->irreco_data,
258 GTK_WINDOW(self),
259 &lib)) {
260 instance = irreco_backend_manager_create_instance(
261 manager, lib, NULL, NULL);
262 if (instance != NULL) {
263 irreco_backend_instance_configure(instance, GTK_WINDOW(
264 self));
265 irreco_backend_instance_save_to_conf(instance);
266 irreco_backend_instance_get_devcmd_list(instance);
267 irreco_backend_dlg_populate_listbox(self);
268 irreco_config_save_backends(manager);
272 IRRECO_RETURN
275 static void irreco_backend_dlg_response_delete(IrrecoBackendDlg *self)
277 IrrecoBackendManager *manager;
278 IrrecoBackendInstance *instance;
279 IRRECO_ENTER
281 manager = self->irreco_data->irreco_backend_manager;
282 if (irreco_backend_dlg_get_instance(self, &instance)) {
284 GString *message = g_string_new(NULL);
285 g_string_append_printf(
286 message, _("Delete %s?\n\n%i commands in command chains"
287 " depend on this backend. Are you sure you want to "
288 "delete this backend."),
289 irreco_backend_instance_get_name_and_description(
290 instance), instance->irreco_cmd_dependencies->len);
291 if (!irreco_yes_no_dlg(GTK_WINDOW(self),
292 message->str)) {
293 g_string_free(message, TRUE);
294 IRRECO_RETURN
296 g_string_free(message, TRUE);
298 if (!irreco_backend_manager_destroy_instance(manager,
299 instance)) {
300 irreco_error_dlg(GTK_WINDOW(self),
301 _("Delete failed."));
302 } else {
303 irreco_config_save_backends(manager);
304 irreco_backend_dlg_populate_listbox(self);
308 IRRECO_RETURN
311 static void irreco_backend_dlg_response_configure(IrrecoBackendDlg *self)
313 IrrecoBackendManager *manager;
314 IrrecoBackendInstance *instance;
315 IRRECO_ENTER
317 manager = self->irreco_data->irreco_backend_manager;
318 if (irreco_backend_dlg_get_instance(self, &instance)) {
319 irreco_backend_instance_configure(instance, GTK_WINDOW(
320 self));
321 irreco_backend_instance_save_to_conf(instance);
322 irreco_backend_instance_get_devcmd_list(instance);
323 irreco_backend_dlg_populate_listbox(self);
324 irreco_config_save_backends(manager);
327 IRRECO_RETURN
330 /** @} */
331 /** @} */