whitespaces fixed
[irreco.git] / irreco / src / core / irreco_theme_download_dlg.c
blobf073aa75fcf7b85dc893d78e815d6437a92a2730
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007,2008 Sami Parttimaa (t5pasa02@students.oamk.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_theme_download_dlg.h"
21 #include <hildon/hildon-banner.h>
23 /**
24 * @addtogroup IrrecoThemeDownloadDlg
25 * @ingroup Irreco
27 * @todo PURPOCE OF CLASS.
29 * @{
32 /**
33 * @file
34 * Source file of @ref IrrecoThemeDownloadDlg.
38 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
39 /* Prototypes */
40 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
42 static gboolean
43 irreco_theme_download_dlg_button_loader(IrrecoThemeDownloadDlg *self);
44 static gboolean irreco_theme_download_dlg_bg_loader(
45 IrrecoThemeDownloadDlg *self);
46 static void irreco_theme_download_dlg_set_banner(IrrecoThemeDownloadDlg *self,
47 const gchar *text, gdouble fraction);
48 static void irreco_theme_download_dlg_hide_banner(IrrecoThemeDownloadDlg *self);
49 static void irreco_theme_download_dlg_loader_stop(
50 IrrecoThemeDownloadDlg *self);
52 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
53 /* Datatypes */
54 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
56 /** Loader states. */
57 enum
59 LOADER_STATE_INIT,
60 LOADER_STATE_LOOP,
61 LOADER_STATE_END,
62 LOADER_STATE_CLEANUP
65 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
66 /* Construction & Destruction */
67 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
69 /**
70 * @name Construction & Destruction
71 * @{
74 G_DEFINE_TYPE (IrrecoThemeDownloadDlg, irreco_theme_download_dlg,
75 IRRECO_TYPE_DLG)
79 static void irreco_theme_download_dlg_dispose(GObject *object)
81 G_OBJECT_CLASS(irreco_theme_download_dlg_parent_class)->dispose(object);
86 static void irreco_theme_download_dlg_finalize(GObject *object)
88 G_OBJECT_CLASS(
89 irreco_theme_download_dlg_parent_class)->finalize(object);
94 static void irreco_theme_download_dlg_class_init(
95 IrrecoThemeDownloadDlgClass *klass)
97 GObjectClass *object_class = G_OBJECT_CLASS (klass);
98 object_class->dispose = irreco_theme_download_dlg_dispose;
99 object_class->finalize = irreco_theme_download_dlg_finalize;
104 static void irreco_theme_download_dlg_init(IrrecoThemeDownloadDlg *self)
107 GtkWidget *label = gtk_label_new("Select save location");
109 IRRECO_ENTER
111 self->theme_downloaded_successfully = FALSE;
112 gtk_widget_set_size_request(GTK_WIDGET(self), 340, 160);
114 gtk_window_set_title(GTK_WINDOW(self),
115 _("Download theme"));
116 gtk_window_set_modal(GTK_WINDOW(self), TRUE);
117 gtk_window_set_destroy_with_parent(GTK_WINDOW(self),
118 TRUE);
119 gtk_dialog_set_has_separator(GTK_DIALOG(self), FALSE);
120 gtk_dialog_add_buttons(GTK_DIALOG(self),
121 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
122 GTK_STOCK_OK, GTK_RESPONSE_OK,
123 NULL);
125 self->radio1 = gtk_radio_button_new_with_label (NULL, "MMC1");
126 self->radio2 = gtk_radio_button_new_with_label_from_widget(
127 GTK_RADIO_BUTTON(self->radio1), "MMC2");
130 gtk_box_pack_start_defaults(GTK_BOX(
131 GTK_DIALOG(self)->vbox),
132 label);
134 if(irreco_is_dir("/media/mmc1")) {
135 gtk_box_pack_start_defaults(GTK_BOX(
136 GTK_DIALOG(self)->vbox),
137 self->radio1);
140 if(irreco_is_dir("/media/mmc2")) {
141 gtk_box_pack_start_defaults(GTK_BOX(
142 GTK_DIALOG(self)->vbox),
143 self->radio2);
146 gtk_widget_show_all(GTK_WIDGET(self));
148 IRRECO_RETURN
157 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
158 /* Private Functions */
159 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
163 static gboolean
164 irreco_theme_download_dlg_button_loader(IrrecoThemeDownloadDlg *self)
166 IRRECO_ENTER
168 switch (self->loader_state) {
169 case LOADER_STATE_INIT:{
170 GString *path = g_string_new("");
171 irreco_theme_download_dlg_set_banner(self,
172 _("Loading buttons.."),
173 0.0);
174 self->loader_state = LOADER_STATE_LOOP;
175 self->theme_loader_index = 0;
177 g_string_printf(path, "%s%s/buttons/", self->theme_folder,
178 self->webdb_theme->folder->str);
180 IRRECO_DEBUG("mkdir %s\n", path->str);
181 g_mkdir(path->str, 0777);
182 g_string_free(path, TRUE);
184 IRRECO_RETURN_BOOL(TRUE);
187 case LOADER_STATE_LOOP: {
188 IrrecoStringTable *button_list = NULL;
189 GString *path = g_string_new("");
190 gfloat banner;
191 const gchar *id;
192 IrrecoWebdbCache *webdb_cache = irreco_data_get_webdb_cache(
193 self->irreco_data,
194 FALSE);
195 /* Get buttons */
196 irreco_webdb_cache_get_buttons(webdb_cache,
197 self->webdb_theme->id,
198 &button_list);
199 if (button_list == NULL) {
200 self->loader_state = LOADER_STATE_END;
201 goto end;
204 g_string_printf(path, "%s%s/buttons/", self->theme_folder,
205 self->webdb_theme->folder->str);
207 if (irreco_string_table_index(button_list,
208 (guint)self->theme_loader_index, &id, NULL) &&
209 !g_str_equal(id, "0")) {
210 irreco_webdb_cache_get_button_by_id(webdb_cache,
211 atoi(id),
212 path->str);
213 } else {
214 self->loader_state = LOADER_STATE_END;
215 goto end;
218 banner = ++self->theme_loader_index;
219 banner /= (gfloat) irreco_string_table_lenght (button_list);
221 irreco_theme_download_dlg_set_banner(self,
222 _("Loading buttons.."),
223 banner);
224 if (banner >= 1.0) {
225 self->loader_state = LOADER_STATE_END;
228 end:
229 g_string_free(path, TRUE);
230 IRRECO_RETURN_BOOL(TRUE);
232 case LOADER_STATE_END:
233 irreco_theme_download_dlg_hide_banner(self);
234 irreco_theme_download_dlg_loader_stop(self);
236 /* Get backgrounds */
237 self->loader_func_id = g_idle_add(G_SOURCEFUNC(
238 irreco_theme_download_dlg_bg_loader), self);
240 IRRECO_RETURN_BOOL(FALSE);
244 static gboolean irreco_theme_download_dlg_bg_loader(IrrecoThemeDownloadDlg *self)
246 IRRECO_ENTER
248 switch (self->loader_state) {
249 case LOADER_STATE_INIT:{
250 GString *path = g_string_new("");
251 irreco_theme_download_dlg_set_banner(self,
252 _("Loading backgrounds..."),
253 0.0);
254 self->loader_state = LOADER_STATE_LOOP;
255 self->theme_loader_index = 0;
257 g_string_printf(path, "%s%s/bg/", self->theme_folder,
258 self->webdb_theme->folder->str);
260 IRRECO_DEBUG("mkdir %s\n", path->str);
261 g_mkdir(path->str, 0777);
262 g_string_free(path, TRUE);
264 IRRECO_RETURN_BOOL(TRUE);
267 case LOADER_STATE_LOOP: {
268 IrrecoStringTable *bg_list = NULL;
269 GString *path = g_string_new("");
270 gfloat banner;
271 const gchar *id;
272 IrrecoWebdbCache *webdb_cache = irreco_data_get_webdb_cache(
273 self->irreco_data,
274 FALSE);
275 /* Get backrounds */
276 irreco_webdb_cache_get_backgrounds(webdb_cache,
277 self->webdb_theme->id,
278 &bg_list);
280 g_string_printf(path, "%s%s/bg/", self->theme_folder,
281 self->webdb_theme->folder->str);
283 if (irreco_string_table_index(bg_list,
284 (guint)self->theme_loader_index, &id, NULL) &&
285 !g_str_equal(id, "0")) {
286 irreco_webdb_cache_get_bg_by_id(webdb_cache, atoi(id),
287 path->str);
288 } else {
289 self->loader_state = LOADER_STATE_END;
290 goto end;
293 banner = ++self->theme_loader_index;
294 banner /= (gfloat) irreco_string_table_lenght (bg_list);
296 irreco_theme_download_dlg_set_banner(self,
297 _("Loading backgrounds..."),
298 banner);
299 if (banner >= 1.0) {
300 self->loader_state = LOADER_STATE_END;
302 end:
303 g_string_free(path, TRUE);
304 IRRECO_RETURN_BOOL(TRUE);
306 case LOADER_STATE_END:
307 irreco_theme_download_dlg_hide_banner(self);
308 irreco_theme_download_dlg_loader_stop(self);
309 gtk_dialog_response(GTK_DIALOG(self),
310 GTK_RESPONSE_CANCEL);
311 self->theme_downloaded_successfully = TRUE;
313 IRRECO_RETURN_BOOL(FALSE);
318 static void irreco_theme_download_dlg_set_banner(IrrecoThemeDownloadDlg *self,
319 const gchar *text,
320 gdouble fraction)
322 IRRECO_ENTER
324 if (self->banner == NULL) {
325 self->banner = hildon_banner_show_progress(
326 GTK_WIDGET(self), NULL, "");
329 hildon_banner_set_text(HILDON_BANNER(self->banner), text);
330 hildon_banner_set_fraction(HILDON_BANNER(self->banner), fraction);
332 IRRECO_RETURN
336 * Destroy banner, if it exists.
339 static void irreco_theme_download_dlg_hide_banner(IrrecoThemeDownloadDlg *self)
341 IRRECO_ENTER
343 if (self->banner) {
344 gtk_widget_destroy(self->banner);
345 self->banner = NULL;
348 IRRECO_RETURN
351 static void irreco_theme_download_dlg_loader_stop(IrrecoThemeDownloadDlg *self)
353 IRRECO_ENTER
355 if (self->loader_func_id != 0) {
356 g_source_remove(self->loader_func_id);
357 self->loader_func_id = 0;
358 self->loader_state = 0;
359 if (self->loader_parent_iter) {
360 gtk_tree_iter_free(self->loader_parent_iter);
362 self->loader_parent_iter = NULL;
364 IRRECO_RETURN
367 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
368 /* Public Functions */
369 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
370 IrrecoThemeDownloadDlg *irreco_theme_download_dlg_new(GtkWindow *parent)
372 IrrecoThemeDownloadDlg *self;
374 IRRECO_ENTER
376 self = g_object_new(IRRECO_TYPE_THEME_DOWNLOAD_DLG, NULL);
378 IRRECO_RETURN_PTR(self);
382 gboolean irreco_theme_download_dlg_run(IrrecoData *irreco_data,
383 gint theme_id,
384 GtkWindow *parent)
386 IrrecoThemeDownloadDlg *self;
387 IrrecoWebdbCache *webdb_cache = NULL;
388 IrrecoTheme *theme;
389 gboolean theme_is_loaded = FALSE;
390 gint delete_mode = 0;
391 IrrecoTheme *same_theme = NULL;
392 gchar *same_theme_folder = NULL;
393 IrrecoThemeManager *theme_manager = irreco_data->theme_manager;
394 GString *message = g_string_new(NULL);
395 GString *path = g_string_new(NULL);
397 IRRECO_ENTER
399 self = irreco_theme_download_dlg_new(parent);
400 self->irreco_data = irreco_data;
401 webdb_cache = irreco_data_get_webdb_cache(irreco_data, FALSE);
403 if (!irreco_webdb_cache_get_theme_by_id(webdb_cache,
404 theme_id,
405 &self->webdb_theme)) {
406 irreco_error_dlg(GTK_WINDOW(self),
407 irreco_webdb_cache_get_error(webdb_cache));
408 goto end;
411 /*self->webdb_theme = webdb_theme;*/
414 while (1) {
415 delete_mode = 0;
417 if (gtk_dialog_run(
418 GTK_DIALOG(self)) == GTK_RESPONSE_CANCEL){
419 break;
421 if (self->loader_func_id != 0) {
422 continue;
424 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
425 self->radio1))) {
426 self->theme_folder = "/media/mmc1/irreco/";
427 } else {
428 self->theme_folder = "/media/mmc2/irreco/";
430 if(!irreco_is_dir(self->theme_folder)) {
431 g_mkdir(self->theme_folder, 0777);
433 /* Create theme */
434 g_string_printf(path, "%s%s/", self->theme_folder,
435 self->webdb_theme->folder->str);
436 IRRECO_STRING_TABLE_FOREACH_DATA(theme_manager->themes,
437 IrrecoTheme *, theme)
438 /*Check theme name*/
439 if (g_str_equal(self->webdb_theme->name->str,
440 theme->name->str)) {
441 if (g_str_equal(theme->source->str, "deb")) {
442 g_string_printf(message,
443 _("The \"%s\" theme already exists.\n"
444 "Can't overwrite \"Built In themes\"\n"
445 "Please remove it first from the\n"
446 "Application Manager"),
447 theme->name->str);
448 irreco_error_dlg(GTK_WINDOW(self),
449 message->str);
450 goto end;
452 delete_mode += 1;
453 same_theme = theme;
455 /*Check theme folder path*/
456 else if (g_str_equal(path->str,
457 g_strconcat(theme->path->str, "/", NULL))){
458 if (g_str_equal(theme->source->str, "deb")) {
459 g_string_printf(message,
460 _("This theme replaces \"%s\" theme.\n"
461 "Can't overwrite \"Built In themes\"\n"
462 "Please remove it first from the\n"
463 "Application Manager"),
464 theme->name->str);
466 irreco_error_dlg(GTK_WINDOW(self),
467 message->str);
468 goto end;
470 delete_mode += 2;
471 same_theme_folder = g_strdup(theme->name->str);
474 IRRECO_STRING_TABLE_FOREACH_END
476 if (delete_mode == 1) {
477 g_string_printf(message,
478 _("The \"%s\" theme already exists.\n"
479 "Do you want to replace this theme?"),
480 same_theme->name->str);
481 } else if (delete_mode == 2) {
482 g_string_printf(message,
483 _("This theme replaces \"%s\" theme.\n"
484 "Do you want to continue?"),
485 same_theme_folder);
486 } else if (delete_mode == 3) {
487 g_string_printf(message,
488 _("This theme replaces themes\n"
489 "\"%s\" and \"%s\"\n"
490 "Do you want to continue?"),
491 same_theme->name->str,
492 same_theme_folder);
494 /* Check whether a theme folder already exists */
495 if (delete_mode != 0) {
496 /* Create dialog*/
497 if (!irreco_yes_no_dlg(
498 GTK_WINDOW(self), message->str)) {
499 continue;
500 } else {
501 /* Remove theme folder and then make it again*/
502 if (delete_mode == 1 || delete_mode == 3) {
503 gchar *rm_cmd = g_strconcat("rm -r ",
504 same_theme->path->str, NULL);
505 system(rm_cmd);
506 g_free(rm_cmd);
508 if (delete_mode == 2 || delete_mode == 3) {
509 irreco_theme_manager_remove_theme(
510 irreco_data->theme_manager,
511 same_theme_folder);
512 g_free(same_theme_folder);
517 /*Create folder */
518 g_mkdir(path->str, 0777);
520 /*Create new theme*/
521 if (delete_mode == 0 || delete_mode == 2) {
522 theme = irreco_theme_new(self->webdb_theme->name->str,
523 path->str, "web",
524 self->webdb_theme->creator->str,
525 self->webdb_theme->comment->str,
526 self->webdb_theme->preview_button_name->str,
527 self->webdb_theme->uploaded->str);
529 irreco_theme_update_keyfile(theme);
530 irreco_theme_free(theme);
531 } else {
532 irreco_theme_set(same_theme,
533 self->webdb_theme->name->str,
534 path->str, "web",
535 self->webdb_theme->creator->str,
536 self->webdb_theme->comment->str,
537 self->webdb_theme->preview_button_name->str,
538 self->webdb_theme->uploaded->str);
541 /* Get buttons and backgrounds */
542 self->loader_func_id = g_idle_add(G_SOURCEFUNC(
543 irreco_theme_download_dlg_button_loader), self);
544 theme_is_loaded = TRUE;
548 end:
549 g_string_free(message, TRUE);
550 g_string_free(path, TRUE);
552 irreco_theme_download_dlg_hide_banner(self);
553 irreco_theme_download_dlg_loader_stop(self);
555 /*if(self != NULL) {
556 gtk_widget_destroy(GTK_WIDGET(self));
557 self = NULL;
560 gtk_widget_destroy(GTK_WIDGET(self));
562 IRRECO_RETURN_BOOL(self->theme_downloaded_successfully);
567 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
568 /* Events and Callbacks */
569 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
573 /** @} */
575 /** @} */