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"
25 * @addtogroup IrrecoBackendDlg
28 * Show a dialog where the user can create, destroy and configure backend
36 * Source file of @ref IrrecoBackendDlg.
41 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
42 /* Prototypes & Datatypes */
43 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
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 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
63 * @name Construction & Destruction
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
)
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,*/
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
),
102 gtk_widget_show_all(GTK_WIDGET(self
));
106 GtkWidget
*irreco_backend_dlg_new(IrrecoData
*irreco_data
,
109 IrrecoBackendDlg
*self
;
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
);
122 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
123 /* Private Functions */
124 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
127 * @name Private Functions
132 * Generate the listbox.
134 static void irreco_backend_dlg_populate_listbox(IrrecoBackendDlg
*self
)
136 IrrecoBackendManager
*manager
;
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(
148 IRRECO_STRING_TABLE_FOREACH_END
153 * Get the IrrecoBackendInstance which matches the current selection.
156 irreco_backend_dlg_get_instance(IrrecoBackendDlg
*self
,
157 IrrecoBackendInstance
**instance
)
160 IrrecoBackendManager
*manager
;
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
,
170 (gpointer
*)instance
)) {
171 IRRECO_RETURN_BOOL(TRUE
);
173 IRRECO_ERROR("No instance found for index \"%i\"\n",
177 irreco_info_dlg(GTK_WINDOW(self
),
178 _("Please select something."));
180 IRRECO_RETURN_BOOL(FALSE
);
185 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
187 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
190 * @name Public Functions
194 void irreco_show_backend_dlg(IrrecoData
*irreco_data
,
197 gboolean loop
= TRUE
;
198 IrrecoBackendDlg
*self
;
201 self
= IRRECO_BACKEND_DLG(irreco_backend_dlg_new(irreco_data
, parent
));
203 switch (gtk_dialog_run(GTK_DIALOG(self
))) {
204 case IRRECO_BACKEND_NEW
:
205 irreco_backend_dlg_response_new(self
);
208 case IRRECO_BACKEND_DELETE
:
209 irreco_backend_dlg_response_delete(self
);
212 case IRRECO_BACKEND_CONFIGURE
:
213 irreco_backend_dlg_response_configure(self
);
216 /*case GTK_RESPONSE_OK:*/
217 case GTK_RESPONSE_DELETE_EVENT
:
221 gtk_widget_grab_focus(self
->listbox
);
222 } while (loop
== TRUE
);
224 gtk_widget_destroy(GTK_WIDGET(self
));
228 void irreco_backend_dlg_set_irreco_data(IrrecoBackendDlg
*self
,
229 IrrecoData
*irreco_data
)
232 self
->irreco_data
= irreco_data
;
233 irreco_backend_dlg_populate_listbox(self
);
241 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
242 /* Events and Callbacks */
243 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
246 * @name Events and Callbacks
250 static void irreco_backend_dlg_response_new(IrrecoBackendDlg
*self
)
252 IrrecoBackendLib
*lib
;
253 IrrecoBackendManager
*manager
;
254 IrrecoBackendInstance
*instance
;
257 manager
= self
->irreco_data
->irreco_backend_manager
;
258 if (irreco_show_backend_select_dlg(self
->irreco_data
,
261 instance
= irreco_backend_manager_create_instance(
262 manager
, lib
, NULL
, NULL
);
263 if (instance
!= NULL
) {
264 irreco_backend_instance_configure(instance
, GTK_WINDOW(
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
);
276 static void irreco_backend_dlg_response_delete(IrrecoBackendDlg
*self
)
278 IrrecoBackendManager
*manager
;
279 IrrecoBackendInstance
*instance
;
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
),
294 g_string_free(message
, TRUE
);
297 g_string_free(message
, TRUE
);
299 if (!irreco_backend_manager_destroy_instance(manager
,
301 irreco_error_dlg(GTK_WINDOW(self
),
302 _("Delete failed."));
304 irreco_config_save_backends(manager
);
305 irreco_backend_dlg_populate_listbox(self
);
312 static void irreco_backend_dlg_response_configure(IrrecoBackendDlg
*self
)
314 IrrecoBackendManager
*manager
;
315 IrrecoBackendInstance
*instance
;
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(
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
);