1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * irreco_theme_save_dlg.c
4 * Copyright (C) 2008 Pekka Gehör (pegu6@msn.com)
6 * irreco_theme_save_dlg.c is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * irreco_theme_save_dlg.c is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "irreco_theme_save_dlg.h"
21 #include "irreco_theme_creator_backgrounds.h"
22 #include "irreco_theme_creator_dlg.h"
23 #include <hildon/hildon-banner.h>
24 #include <hildon/hildon-color-button.h>
25 #include <hildon/hildon-file-chooser-dialog.h>
27 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
29 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
30 gboolean
_save_theme_to_dir(IrrecoThemeSaveDlg
*self
, IrrecoData
*irreco_data
,
31 IrrecoTheme
*irreco_theme
);
32 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
33 /* Construction & Destruction */
34 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
37 * @name Construction & Destruction
42 G_DEFINE_TYPE (IrrecoThemeSaveDlg
, irreco_theme_save_dlg
,
43 IRRECO_TYPE_INTERNAL_DLG
)
46 static void irreco_theme_save_dlg_constructed(GObject
*object
)
48 IrrecoData
*irreco_data
;
49 IrrecoThemeSaveDlg
*self
;
55 irreco_theme_save_dlg_parent_class
)->constructed(object
);
57 self
= IRRECO_THEME_SAVE_DLG(object
);
59 irreco_data
= irreco_internal_dlg_get_irreco_data(IRRECO_INTERNAL_DLG(
62 /* Construct dialog. */
63 gtk_window_set_title(GTK_WINDOW(self
), _("Theme Save"));
64 gtk_window_set_modal(GTK_WINDOW(self
), TRUE
);
65 gtk_window_set_destroy_with_parent(GTK_WINDOW(self
), TRUE
);
66 gtk_dialog_set_has_separator(GTK_DIALOG(self
), FALSE
);
69 gtk_dialog_add_buttons(GTK_DIALOG(self
),
70 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
71 GTK_STOCK_SAVE
, GTK_RESPONSE_OK
,
75 label
= gtk_label_new("Select save location");
77 self
->radio1
= gtk_radio_button_new_with_label (NULL
, "MMC1");
78 self
->radio2
= gtk_radio_button_new_with_label_from_widget(
80 self
->radio1
), "MMC2");
81 self
->radio3
= gtk_radio_button_new_with_label_from_widget(
83 self
->radio2
), "DEVICE");
85 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(self
)->vbox
), label
);
87 if(irreco_is_dir("/media/mmc1")) {
88 gtk_box_pack_start_defaults(GTK_BOX(
89 GTK_DIALOG(self
)->vbox
),
93 if(irreco_is_dir("/media/mmc2")) {
94 gtk_box_pack_start_defaults(GTK_BOX(
95 GTK_DIALOG(self
)->vbox
),
102 gtk_widget_show_all(GTK_WIDGET(self
));
107 irreco_theme_save_dlg_init (IrrecoThemeSaveDlg
*object
)
114 irreco_theme_save_dlg_finalize (GObject
*object
)
116 /* TODO: Add deinitalization code here */
117 IrrecoThemeSaveDlg
*self
;
120 self
= IRRECO_THEME_SAVE_DLG(object
);
121 G_OBJECT_CLASS (irreco_theme_save_dlg_parent_class
)->finalize (object
);
126 irreco_theme_save_dlg_class_init (IrrecoThemeSaveDlgClass
*klass
)
128 GObjectClass
* object_class
= G_OBJECT_CLASS (klass
);
129 object_class
->finalize
= irreco_theme_save_dlg_finalize
;
130 object_class
->constructed
= irreco_theme_save_dlg_constructed
;
134 irreco_theme_save_dlg_new(IrrecoData
*irreco_data
, GtkWindow
*parent
)
136 IrrecoThemeSaveDlg
*self
;
140 self
= g_object_new(IRRECO_TYPE_THEME_SAVE_DLG
,
141 "irreco-data", irreco_data
, NULL
);
142 irreco_dlg_set_parent(IRRECO_DLG(self
), parent
);
144 IRRECO_RETURN_PTR(self
);
148 irreco_theme_save_dlg_create(IrrecoData
*irreco_data
, GtkWindow
*parent_window
)
151 IRRECO_RETURN_PTR(irreco_theme_save_dlg_new(
152 irreco_data
, parent_window
));
157 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
158 /* Private Functions */
159 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
161 * @name Private Functions
165 void _save_bg_to_theme_dir(IrrecoThemeSaveDlg
*self
, IrrecoData
*irreco_data
,
166 IrrecoTheme
*irreco_theme
, const gchar
*theme_path
)
170 IrrecoStringTable
*bg_list
= NULL
;
173 const gchar
*image_name
;
177 path
= g_string_new("");
180 g_string_printf(path
, "%s/bg", theme_path
);
182 IRRECO_DEBUG("mkdir %s\n", path
->str
);
183 g_mkdir(path
->str
, 0777);
186 bg_list
= irreco_theme_get_backgrounds(irreco_theme
);
187 theme_bg_count
= irreco_string_table_lenght(bg_list
);
189 if (theme_bg_count
> 0) {
190 while(theme_bg_count
> 0) {
192 IrrecoThemeBg
*background
;
194 irreco_string_table_index(bg_list
, i
,
199 irreco_theme_bg_print(background
);
201 irreco_theme_bg_save(background
, irreco_data
,
202 irreco_theme
, path
->str
);
208 g_string_free(path
, TRUE
);
214 gboolean
_save_theme_to_dir(IrrecoThemeSaveDlg
*self
, IrrecoData
*irreco_data
,
215 IrrecoTheme
*irreco_theme
)
218 gboolean rvalue
= FALSE
;
220 gchar
*folder
= NULL
;
221 IrrecoThemeManager
*theme_manager
= irreco_data
->theme_manager
;
222 IrrecoTheme
*same_theme
= NULL
;
224 gint delete_mode
= 0;
225 gchar
*same_theme_folder
= NULL
;
226 GString
*message
= g_string_new(NULL
);
231 /* Create Theme Folder */
234 theme_path
= g_string_new("");
237 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(self
->radio1
))) {
238 folder
= "/media/mmc1/irreco/";
239 } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(self
->radio2
))) {
240 folder
= "/media/mmc2/irreco/";
242 folder
= IRRECO_THEME_DIR
;
245 /* Call IrrecoThemeSave */
246 if(!irreco_is_dir(folder
)) {
247 g_mkdir(folder
, 0777);
251 g_string_printf(theme_path
, "%s%s", folder
, irreco_theme
->name
->str
);
253 IRRECO_STRING_TABLE_FOREACH_DATA(theme_manager
->themes
,
254 IrrecoTheme
*, theme
)
256 if (g_str_equal(irreco_theme
->name
->str
,
258 if (g_str_equal(theme
->source
->str
, "deb")) {
259 g_string_printf(message
,
260 _("The \"%s\" theme already exists.\n"
261 "Can't overwrite \"Built In themes\"\n"
262 "Please remove it first from the\n"
263 "Application Manager or change the name of the theme"),
266 irreco_error_dlg(GTK_WINDOW(self
),
273 /*Check theme folder path*/
274 else if (g_str_equal(theme_path
->str
,
275 g_strconcat(theme
->path
->str
, "/", NULL
))){
276 if (g_str_equal(theme
->source
->str
, "deb")) {
277 g_string_printf(message
,
278 _("This theme replaces \"%s\" theme.\n"
279 "Can't overwrite \"Built In themes\"\n"
280 "Please remove it first from the\n"
281 "Application Manager or change the name of the theme"),
284 irreco_error_dlg(GTK_WINDOW(self
),
289 same_theme_folder
= g_strdup(theme
->name
->str
);
291 IRRECO_STRING_TABLE_FOREACH_END
293 if (delete_mode
== 1) {
294 g_string_printf(message
,
295 _("The \"%s\" theme already exists.\n"
296 "Do you want to replace this theme?"),
297 same_theme
->name
->str
);
298 } else if (delete_mode
== 2) {
299 g_string_printf(message
,
300 _("This theme replaces \"%s\" theme.\n"
301 "Do you want to continue?"),
303 } else if (delete_mode
== 3) {
304 g_string_printf(message
,
305 _("This theme replaces themes\n"
306 "\"%s\" and \"%s\"\n"
307 "Do you want to continue?"),
308 same_theme
->name
->str
,
311 /* Check whether a theme folder already exists */
312 if (delete_mode
!= 0) {
314 if (!irreco_yes_no_dlg(
315 GTK_WINDOW(self
), message
->str
)) {
318 /* Remove theme folder and then make it again*/
319 if (delete_mode
== 1 || delete_mode
== 3) {
320 gchar
*rm_cmd
= g_strconcat("rm -r ",
321 same_theme
->path
->str
, NULL
);
325 if (delete_mode
== 2 || delete_mode
== 3) {
326 irreco_theme_manager_remove_theme(
327 irreco_data
->theme_manager
,
329 g_free(same_theme_folder
);
335 g_mkdir(theme_path
->str
, 0777);
338 irreco_theme_save(irreco_theme
, irreco_data
, theme_path
->str
);
340 if (delete_mode
== 0 || delete_mode
== 2) {
342 irreco_theme_save(irreco_theme
, irreco_data
, theme_path
->str
);
345 irreco_theme_set(same_theme
,
346 irreco_theme
->name
->str
,
348 irreco_theme
->source
->str
,
349 irreco_theme
->author
->str
,
350 irreco_theme
->comment
->str
,
351 irreco_theme
->preview_button_name
->str
,
355 /* Get buttons and backgrounds */
356 _save_bg_to_theme_dir(self
, irreco_data
, irreco_theme
, theme_path
->str
);
361 g_string_free(message
, TRUE
);
362 g_string_free(theme_path
, TRUE
);
371 /* Save image_up to folder */
372 g_string_printf(file_path
, "%s/%s/%s", theme_button_dir
, folder
,
374 image_up
= (gchar
*) g_base64_decode(base64_image_up
, &image_up_len
);
375 irreco_write_file(file_path
->str
, image_up
, image_up_len
);
377 /* Save image_down to folder */
378 g_string_printf(file_path
, "%s/%s/%s", theme_button_dir
, folder
,
381 /*Check image hash data*/
382 image_down
= (gchar
*) g_base64_decode(base64_image_down
,
384 irreco_write_file(file_path
->str
, image_down
, image_down_len
);
385 image_down_hash_tmp
= sha_compute_checksum_for_string(G_CHECKSUM_SHA1
,
389 if (!g_str_equal(image_down_hash
, image_down_hash_tmp
)) {
391 g_string_printf(self
->error_msg
,
392 "ERROR: Button data is corrupted\n");
396 /* Create keyfile and save it to folder*/
397 irreco_gkeyfile_set_string(keyfile
, "theme-button" , "name", name
);
400 irreco_gkeyfile_set_string(keyfile
, "theme-button",
401 "allow-text", "true");
403 irreco_gkeyfile_set_string(keyfile
, "theme-button",
404 "allow-text", "false");
407 irreco_gkeyfile_set_string(keyfile
, "theme-button",
408 "up", image_up_name
);
410 irreco_gkeyfile_set_string(keyfile
, "theme-button",
411 "down", image_down_name
);
413 if (text_format_up
!= NULL
&& strlen(text_format_up
) > 0) {
414 irreco_gkeyfile_set_string(keyfile
, "theme-button",
415 "text-format-up", text_format_up
);
418 if (text_format_down
!= NULL
&& strlen(text_format_down
) > 0) {
419 irreco_gkeyfile_set_string(keyfile
, "theme-button",
420 "text-format-down", text_format_down
);
423 irreco_gkeyfile_set_glong(keyfile
, "theme-button",
424 "text-padding", (glong
)text_padding
);
426 irreco_gkeyfile_set_gfloat(keyfile
,
429 (gfloat
) text_h_align
);
431 irreco_gkeyfile_set_gfloat(keyfile
,
434 (gfloat
) text_v_align
);
436 g_string_printf(file_path
, "%s/%s/button.conf",
437 theme_button_dir
, folder
);
438 irreco_write_keyfile(keyfile
, file_path
->str
);
440 /* No error occured. */
448 IRRECO_RETURN_BOOL(rvalue
);
453 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
455 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
458 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
459 /* Public Functions */
460 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
463 irreco_theme_save_dlg_run(IrrecoData
*irreco_data
, IrrecoTheme
*irreco_theme
,
464 GtkWindow
*parent_window
)
466 IrrecoThemeSaveDlg
*self
;
468 gboolean loop
= TRUE
;
469 gboolean rvalue
= FALSE
;
473 self
= (IrrecoThemeSaveDlg
*)irreco_theme_save_dlg_create(
474 irreco_data
, parent_window
);
475 self
->irreco_data
= irreco_data
;
476 self
->theme
= irreco_theme
;
481 response
= gtk_dialog_run(GTK_DIALOG(self
));
483 case GTK_RESPONSE_OK
:
485 if (_save_theme_to_dir(self
, irreco_data
,
493 case GTK_RESPONSE_CANCEL
:
494 IRRECO_DEBUG("GTK_RESPONSE_CANCEL\n");
500 IRRECO_DEBUG("default\n");
506 gtk_widget_destroy(GTK_WIDGET(self
));
508 IRRECO_RETURN_BOOL(rvalue
);