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, 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
),
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
:
220 gtk_widget_grab_focus(self
->listbox
);
221 } while (loop
== TRUE
);
223 gtk_widget_destroy(GTK_WIDGET(self
));
227 void irreco_backend_dlg_set_irreco_data(IrrecoBackendDlg
*self
,
228 IrrecoData
*irreco_data
)
231 self
->irreco_data
= irreco_data
;
232 irreco_backend_dlg_populate_listbox(self
);
240 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
241 /* Events and Callbacks */
242 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
245 * @name Events and Callbacks
249 static void irreco_backend_dlg_response_new(IrrecoBackendDlg
*self
)
251 IrrecoBackendLib
*lib
;
252 IrrecoBackendManager
*manager
;
253 IrrecoBackendInstance
*instance
;
256 manager
= self
->irreco_data
->irreco_backend_manager
;
257 if (irreco_show_backend_select_dlg(self
->irreco_data
,
260 instance
= irreco_backend_manager_create_instance(
261 manager
, lib
, NULL
, NULL
);
262 if (instance
!= NULL
) {
263 irreco_backend_instance_configure(instance
, GTK_WINDOW(
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
);
275 static void irreco_backend_dlg_response_delete(IrrecoBackendDlg
*self
)
277 IrrecoBackendManager
*manager
;
278 IrrecoBackendInstance
*instance
;
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
),
293 g_string_free(message
, TRUE
);
296 g_string_free(message
, TRUE
);
298 if (!irreco_backend_manager_destroy_instance(manager
,
300 irreco_error_dlg(GTK_WINDOW(self
),
301 _("Delete failed."));
303 irreco_config_save_backends(manager
);
304 irreco_backend_dlg_populate_listbox(self
);
311 static void irreco_backend_dlg_response_configure(IrrecoBackendDlg
*self
)
313 IrrecoBackendManager
*manager
;
314 IrrecoBackendInstance
*instance
;
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(
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
);