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>
24 * @addtogroup IrrecoThemeDownloadDlg
27 * @todo PURPOCE OF CLASS.
34 * Source file of @ref IrrecoThemeDownloadDlg.
38 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
40 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
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 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
54 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
65 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
66 /* Construction & Destruction */
67 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
70 * @name Construction & Destruction
74 G_DEFINE_TYPE (IrrecoThemeDownloadDlg
, irreco_theme_download_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
)
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");
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
),
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
,
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
),
134 if(irreco_is_dir("/media/mmc1")) {
135 gtk_box_pack_start_defaults(GTK_BOX(
136 GTK_DIALOG(self
)->vbox
),
140 if(irreco_is_dir("/media/mmc2")) {
141 gtk_box_pack_start_defaults(GTK_BOX(
142 GTK_DIALOG(self
)->vbox
),
146 gtk_widget_show_all(GTK_WIDGET(self
));
157 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
158 /* Private Functions */
159 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
164 irreco_theme_download_dlg_button_loader(IrrecoThemeDownloadDlg
*self
)
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.."),
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("");
192 IrrecoWebdbCache
*webdb_cache
= irreco_data_get_webdb_cache(
196 irreco_webdb_cache_get_buttons(webdb_cache
,
197 self
->webdb_theme
->id
,
199 if (button_list
== NULL
) {
200 self
->loader_state
= LOADER_STATE_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
,
214 self
->loader_state
= LOADER_STATE_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.."),
225 self
->loader_state
= LOADER_STATE_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
)
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..."),
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("");
272 IrrecoWebdbCache
*webdb_cache
= irreco_data_get_webdb_cache(
276 irreco_webdb_cache_get_backgrounds(webdb_cache
,
277 self
->webdb_theme
->id
,
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
),
289 self
->loader_state
= LOADER_STATE_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..."),
300 self
->loader_state
= LOADER_STATE_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
,
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
);
336 * Destroy banner, if it exists.
339 static void irreco_theme_download_dlg_hide_banner(IrrecoThemeDownloadDlg
*self
)
344 gtk_widget_destroy(self
->banner
);
351 static void irreco_theme_download_dlg_loader_stop(IrrecoThemeDownloadDlg
*self
)
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
;
367 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
368 /* Public Functions */
369 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
370 IrrecoThemeDownloadDlg
*irreco_theme_download_dlg_new(GtkWindow
*parent
)
372 IrrecoThemeDownloadDlg
*self
;
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
,
386 IrrecoThemeDownloadDlg
*self
;
387 IrrecoWebdbCache
*webdb_cache
= NULL
;
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
);
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
,
405 &self
->webdb_theme
)) {
406 irreco_error_dlg(GTK_WINDOW(self
),
407 irreco_webdb_cache_get_error(webdb_cache
));
411 /*self->webdb_theme = webdb_theme;*/
418 GTK_DIALOG(self
)) == GTK_RESPONSE_CANCEL
){
421 if (self
->loader_func_id
!= 0) {
424 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
426 self
->theme_folder
= "/media/mmc1/irreco/";
428 self
->theme_folder
= "/media/mmc2/irreco/";
430 if(!irreco_is_dir(self
->theme_folder
)) {
431 g_mkdir(self
->theme_folder
, 0777);
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
)
439 if (g_str_equal(self
->webdb_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"),
448 irreco_error_dlg(GTK_WINDOW(self
),
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"),
466 irreco_error_dlg(GTK_WINDOW(self
),
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?"),
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
,
494 /* Check whether a theme folder already exists */
495 if (delete_mode
!= 0) {
497 if (!irreco_yes_no_dlg(
498 GTK_WINDOW(self
), message
->str
)) {
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
);
508 if (delete_mode
== 2 || delete_mode
== 3) {
509 irreco_theme_manager_remove_theme(
510 irreco_data
->theme_manager
,
512 g_free(same_theme_folder
);
518 g_mkdir(path
->str
, 0777);
521 if (delete_mode
== 0 || delete_mode
== 2) {
522 theme
= irreco_theme_new(self
->webdb_theme
->name
->str
,
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
);
532 irreco_theme_set(same_theme
,
533 self
->webdb_theme
->name
->str
,
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
;
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
);
556 gtk_widget_destroy(GTK_WIDGET(self));
560 gtk_widget_destroy(GTK_WIDGET(self
));
562 IRRECO_RETURN_BOOL(self
->theme_downloaded_successfully
);
567 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
568 /* Events and Callbacks */
569 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/