Mostly minor fixes up until version 0.8.10.
[irreco.git] / irreco / src / core / irreco_backend_dlg.c
blob7bc3cdc8eb4b6c862dca68e45f6729c22d288919
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, 250, 350);
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 case GTK_RESPONSE_DELETE_EVENT:
218 loop = FALSE;
219 break;
221 gtk_widget_grab_focus(self->listbox);
222 } while (loop == TRUE);
224 gtk_widget_destroy(GTK_WIDGET(self));
225 IRRECO_RETURN
228 void irreco_backend_dlg_set_irreco_data(IrrecoBackendDlg *self,
229 IrrecoData *irreco_data)
231 IRRECO_ENTER
232 self->irreco_data = irreco_data;
233 irreco_backend_dlg_populate_listbox(self);
234 IRRECO_RETURN
237 /**@} */
241 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
242 /* Events and Callbacks */
243 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
246 * @name Events and Callbacks
247 * @{
250 static void irreco_backend_dlg_response_new(IrrecoBackendDlg *self)
252 IrrecoBackendLib *lib;
253 IrrecoBackendManager *manager;
254 IrrecoBackendInstance *instance;
255 IRRECO_ENTER
257 manager = self->irreco_data->irreco_backend_manager;
258 if (irreco_show_backend_select_dlg(self->irreco_data,
259 GTK_WINDOW(self),
260 &lib)) {
261 instance = irreco_backend_manager_create_instance(
262 manager, lib, NULL, NULL);
263 if (instance != NULL) {
264 irreco_backend_instance_configure(instance, GTK_WINDOW(
265 self));
266 irreco_backend_instance_save_to_conf(instance);
267 irreco_backend_instance_get_devcmd_list(instance);
268 irreco_backend_dlg_populate_listbox(self);
269 irreco_config_save_backends(manager);
273 IRRECO_RETURN
276 static void irreco_backend_dlg_response_delete(IrrecoBackendDlg *self)
278 IrrecoBackendManager *manager;
279 IrrecoBackendInstance *instance;
280 IRRECO_ENTER
282 manager = self->irreco_data->irreco_backend_manager;
283 if (irreco_backend_dlg_get_instance(self, &instance)) {
285 GString *message = g_string_new(NULL);
286 g_string_append_printf(
287 message, _("Delete %s?\n\n%i commands in command chains"
288 " depend on this backend. Are you sure you want to "
289 "delete this backend."),
290 irreco_backend_instance_get_name_and_description(
291 instance), instance->irreco_cmd_dependencies->len);
292 if (!irreco_yes_no_dlg(GTK_WINDOW(self),
293 message->str)) {
294 g_string_free(message, TRUE);
295 IRRECO_RETURN
297 g_string_free(message, TRUE);
299 if (!irreco_backend_manager_destroy_instance(manager,
300 instance)) {
301 irreco_error_dlg(GTK_WINDOW(self),
302 _("Delete failed."));
303 } else {
304 irreco_config_save_backends(manager);
305 irreco_backend_dlg_populate_listbox(self);
309 IRRECO_RETURN
312 static void irreco_backend_dlg_response_configure(IrrecoBackendDlg *self)
314 IrrecoBackendManager *manager;
315 IrrecoBackendInstance *instance;
316 IRRECO_ENTER
318 manager = self->irreco_data->irreco_backend_manager;
319 if (irreco_backend_dlg_get_instance(self, &instance)) {
320 irreco_backend_instance_configure(instance, GTK_WINDOW(
321 self));
322 irreco_backend_instance_save_to_conf(instance);
323 irreco_backend_instance_get_devcmd_list(instance);
324 irreco_backend_dlg_populate_listbox(self);
325 irreco_config_save_backends(manager);
328 IRRECO_RETURN
331 /** @} */
332 /** @} */