clean code and changed version nro.
[irreco.git] / irreco / src / core / irreco_button_browser_dlg.c
blob3288e567bf8b1082776708688cc2451215d9897b
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007 Arto Karppinen (arto.karppinen@iki.fi)
4 * 2008 Joni Kokko (t5kojo01@students.oamk.fi)
5 * 2008 Harri Vattulainen (t5vaha01@students.oamk.fi)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "irreco_button_browser_dlg.h"
23 #include <hildon/hildon-banner.h>
24 #include "irreco_webdb_client.h"
28 /**
29 * @addtogroup IrrecoButtonBrowserDlg
30 * @ingroup Irreco
32 * Allow user to select one button from one theme.
34 * @{
37 /**
38 * @file
39 * Source file of @ref IrrecoButtonBrowserDlg.
44 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
45 /* Datatypes */
46 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
48 #define IRRECO_BUTTON_BROWSER_PREVIEW_WIDHT (IRRECO_SCREEN_WIDTH / 6)
49 #define IRRECO_BUTTON_BROWSER_PREVIEW_HEIGHT (IRRECO_SCREEN_HEIGHT / 6)
53 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
54 /* Prototypes */
55 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
57 static void
58 _irreco_button_browser_dlg_set_banner(IrrecoButtonBrowserDlg *self,
59 const gchar *text,
60 gdouble fraction);
61 static void
62 _irreco_button_browser_dlg_hide_banner(IrrecoButtonBrowserDlg *self);
63 static gboolean _irreco_button_browser_dlg_loader_images(
64 IrrecoButtonBrowserDlg *self);
65 static void irreco_button_browser_dlg_image_selection_changed(
66 GtkTreeSelection * selection,
67 IrrecoButtonBrowserDlg *self);
68 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
69 /* Construction & Destruction */
70 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
72 /**
73 * @name Construction & Destruction
74 * @{
77 G_DEFINE_TYPE(IrrecoButtonBrowserDlg, irreco_button_browser_dlg,
78 IRRECO_TYPE_INTERNAL_DLG)
80 static void irreco_button_browser_dlg_constructed(GObject *object)
82 IrrecoButtonBrowserDlg *self;
84 IRRECO_ENTER
86 G_OBJECT_CLASS(irreco_button_browser_dlg_parent_class)->constructed(
87 object);
88 self = IRRECO_BUTTON_BROWSER_DLG(object);
90 self->current_image = NULL;
92 /* Construct dialog. */
93 gtk_window_set_title(GTK_WINDOW(self), _("Select preview button"));
94 gtk_window_set_modal(GTK_WINDOW(self), TRUE);
95 gtk_window_set_destroy_with_parent(GTK_WINDOW(self), TRUE);
96 gtk_dialog_set_has_separator(GTK_DIALOG(self), FALSE);
97 gtk_dialog_add_buttons(GTK_DIALOG(self),
98 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
99 NULL);
101 /* Create list for images */
102 self->images = IRRECO_LISTBOX_IMAGE(
103 irreco_listbox_image_new_with_autosize(200, 450, 180, 180));
104 irreco_listbox_set_select_new_rows(IRRECO_LISTBOX(self->images), FALSE);
105 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(self)->vbox),
106 GTK_WIDGET(self->images));
108 g_signal_connect(G_OBJECT(IRRECO_LISTBOX(self->images)->tree_selection),
109 "changed",
110 G_CALLBACK(irreco_button_browser_dlg_image_selection_changed),
111 self);
113 gtk_widget_show_all(GTK_WIDGET(self));
115 IRRECO_RETURN
118 static void irreco_button_browser_dlg_init(IrrecoButtonBrowserDlg *self)
120 IRRECO_ENTER
121 IRRECO_RETURN
124 static void irreco_button_browser_dlg_finalize(GObject *object)
126 IrrecoButtonBrowserDlg *self;
128 IRRECO_ENTER
130 self = IRRECO_BUTTON_BROWSER_DLG(object);
131 G_OBJECT_CLASS(irreco_button_browser_dlg_parent_class)->finalize(object);
133 IRRECO_RETURN
136 static void
137 irreco_button_browser_dlg_class_init(IrrecoButtonBrowserDlgClass *klass)
139 GObjectClass* object_class = G_OBJECT_CLASS (klass);
140 object_class->finalize = irreco_button_browser_dlg_finalize;
141 object_class->constructed = irreco_button_browser_dlg_constructed;
144 GtkWidget *irreco_button_browser_dlg_new(IrrecoData *irreco_data,
145 GtkWindow *parent_window)
147 IrrecoButtonBrowserDlg *self;
149 IRRECO_ENTER
151 self = g_object_new(IRRECO_TYPE_BUTTON_BROWSER_DLG,
152 "irreco-data", irreco_data,
153 NULL);
154 irreco_dlg_set_parent(IRRECO_DLG(self), parent_window);
156 IRRECO_RETURN_PTR(self);
159 /** @} */
163 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
164 /* Private Functions */
165 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
167 * @name Private Functions
168 * @{
172 * Show hildon progressbar banner.
174 * This function will create a new banner if one has not been created yet,
175 * if banner already exists, it's properties will be changed.
177 * @param text Text to show.
178 * @param fraction Value of progress.
180 static void
181 _irreco_button_browser_dlg_set_banner(IrrecoButtonBrowserDlg *self,
182 const gchar *text,
183 gdouble fraction)
185 IRRECO_ENTER
187 if (self->banner == NULL) {
188 self->banner = hildon_banner_show_progress(
189 GTK_WIDGET(self), NULL, "");
192 hildon_banner_set_text(HILDON_BANNER(self->banner), text);
193 hildon_banner_set_fraction(HILDON_BANNER(self->banner), fraction);
195 IRRECO_RETURN
199 * Destroy banner, if it exists.
201 static void
202 _irreco_button_browser_dlg_hide_banner(IrrecoButtonBrowserDlg *self)
204 IRRECO_ENTER
206 if (self->banner) {
207 gtk_widget_destroy(self->banner);
208 self->banner = NULL;
211 IRRECO_RETURN
215 * Background-image loader.
217 * This loader will create a list of background-images from the
218 * given theme and update the TreeView accordingly.
220 static gboolean _irreco_button_browser_dlg_loader_images(
221 IrrecoButtonBrowserDlg *self)
223 gint theme_button_count;
224 gfloat banner;
225 gboolean rvalue;
227 IRRECO_ENTER
229 theme_button_count = irreco_string_table_lenght(self->theme->buttons);
231 if (theme_button_count > 0) {
232 if(self->loader_index == 0) {
233 _irreco_button_browser_dlg_set_banner(self,
234 _("Loading ..."),
237 rvalue = TRUE;
238 } else {
239 rvalue = FALSE;
242 if(self->loader_index < theme_button_count){
243 IrrecoThemeButton *button_image;
244 const gchar *image_name;
245 /* get button */
246 irreco_string_table_index(self->theme->buttons,
247 self->loader_index,
248 &image_name,
249 (gpointer *) &button_image);
250 /* add button to list */
251 irreco_listbox_image_append(self->images,
252 button_image->name->str,
253 button_image,
254 button_image->image_up->str);
255 /* get and set banner fraction */
256 banner = (gfloat) self->loader_index /
257 (gfloat) theme_button_count;
258 _irreco_button_browser_dlg_set_banner(self,
259 _("Loading ..."),
260 banner);
261 /* get next button, next time */
262 self->loader_index++;
265 if(self->loader_index == theme_button_count) {
266 _irreco_button_browser_dlg_hide_banner(self);
267 rvalue = FALSE;
270 IRRECO_RETURN_BOOL(rvalue);
273 /** @} */
277 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
278 /* Public Functions */
279 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
282 * Show dialog, and ask user for input.
285 IrrecoThemeButton *irreco_button_browser_dlg_run(GtkWindow *parent_window,
286 IrrecoData *irreco_data,
287 IrrecoTheme * irreco_theme)
289 IrrecoButtonBrowserDlg *self;
290 gint response;
291 gboolean loop = TRUE;
292 IrrecoThemeButton *rvalue = NULL;
294 IRRECO_ENTER
296 self = (IrrecoButtonBrowserDlg *)irreco_button_browser_dlg_new(
297 irreco_data,
298 parent_window);
300 self->theme = irreco_theme;
302 /* init and start loading buttons */
303 self->loader_index = 0;
304 irreco_listbox_clear(IRRECO_LISTBOX(self->images));
305 g_idle_add((GSourceFunc)_irreco_button_browser_dlg_loader_images, self);
307 do {
308 response = gtk_dialog_run(GTK_DIALOG(self));
310 switch (response) {
312 case GTK_RESPONSE_CANCEL:
313 IRRECO_DEBUG("GTK_RESPONSE_CANCEL\n");
314 rvalue = NULL;
315 loop = FALSE;
316 break;
318 case GTK_RESPONSE_ACCEPT:
319 IRRECO_DEBUG("GTK_RESPONSE_ACCEPT\n");
320 /*rvalue = g_strdup(self->current_image->name->str);*/
321 rvalue = self->current_image;
322 loop = FALSE;
323 break;
325 default:
326 IRRECO_DEBUG("Something went horribly wrong\n");
327 break;
329 } while (loop);
331 gtk_widget_destroy(GTK_WIDGET(self));
333 IRRECO_RETURN_PTR(rvalue);
336 /** @} */
340 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
341 /* Events and Callbacks */
342 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
345 * @name Events and Callbacks
346 * @{
350 * Respond to image selection change.
352 * This function will store selected button to self->current_image
354 static void irreco_button_browser_dlg_image_selection_changed(
355 GtkTreeSelection *selection,
356 IrrecoButtonBrowserDlg *self)
358 gint sel_index = -1;
359 gchar *sel_label = NULL;
360 IrrecoThemeButton *button = NULL;
362 IRRECO_ENTER
364 if (irreco_listbox_get_selection(IRRECO_LISTBOX(self->images),
365 &sel_index, &sel_label, (gpointer *)&button)) {
366 self->current_image = button;
367 } else {
368 self->current_image = NULL;
371 g_free(sel_label);
372 gtk_dialog_response(GTK_DIALOG(self), GTK_RESPONSE_ACCEPT);
374 IRRECO_RETURN
377 /** @} */
381 /** @} */