Added theme_bg_save functio and clean code
[irreco.git] / irreco / src / core / irreco_theme_save_dlg.c
blobb9da96df558b4b069ec3428892da1a170a8b7815
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
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 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
28 /* Prototypes */
29 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
30 gboolean _save_theme_to_dir(IrrecoThemeSaveDlg *self, IrrecoData *irreco_data,
31 IrrecoTheme *irreco_theme);
32 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
33 /* Construction & Destruction */
34 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
36 /**
37 * @name Construction & Destruction
38 * @{
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;
50 GtkWidget *label;
52 IRRECO_ENTER
54 G_OBJECT_CLASS(
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(
60 self));
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);
68 /*Buttons*/
69 gtk_dialog_add_buttons(GTK_DIALOG(self),
70 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
71 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
72 NULL);
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(
79 GTK_RADIO_BUTTON(
80 self->radio1), "MMC2");
81 self->radio3 = gtk_radio_button_new_with_label_from_widget(
82 GTK_RADIO_BUTTON(
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),
90 self->radio1);
93 if(irreco_is_dir("/media/mmc2")) {
94 gtk_box_pack_start_defaults(GTK_BOX(
95 GTK_DIALOG(self)->vbox),
96 self->radio2);
102 gtk_widget_show_all(GTK_WIDGET(self));
103 IRRECO_RETURN
106 static void
107 irreco_theme_save_dlg_init (IrrecoThemeSaveDlg *object)
109 IRRECO_ENTER
110 IRRECO_RETURN
113 static void
114 irreco_theme_save_dlg_finalize (GObject *object)
116 /* TODO: Add deinitalization code here */
117 IrrecoThemeSaveDlg *self;
118 IRRECO_ENTER
120 self = IRRECO_THEME_SAVE_DLG(object);
121 G_OBJECT_CLASS (irreco_theme_save_dlg_parent_class)->finalize (object);
122 IRRECO_RETURN
125 static void
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;
133 GtkWidget *
134 irreco_theme_save_dlg_new(IrrecoData *irreco_data, GtkWindow *parent)
136 IrrecoThemeSaveDlg *self;
138 IRRECO_ENTER
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);
147 IrrecoThemeSaveDlg *
148 irreco_theme_save_dlg_create(IrrecoData *irreco_data, GtkWindow *parent_window)
150 IRRECO_ENTER
151 IRRECO_RETURN_PTR(irreco_theme_save_dlg_new(
152 irreco_data, parent_window));
155 /** @} */
157 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
158 /* Private Functions */
159 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
161 * @name Private Functions
162 * @{
165 void _save_bg_to_theme_dir(IrrecoThemeSaveDlg *self, IrrecoData *irreco_data,
166 IrrecoTheme *irreco_theme, const gchar *theme_path)
169 GString *path;
170 IrrecoStringTable *bg_list = NULL;
171 gint theme_bg_count;
172 gint i=0;
173 const gchar *image_name;
175 IRRECO_ENTER
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);
185 /* Get backrounds */
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,
195 &image_name,
196 (gpointer *)
197 &background);
199 irreco_theme_bg_print(background);
201 irreco_theme_bg_save(background, irreco_data,
202 irreco_theme, path->str);
204 theme_bg_count -=1;
205 i++;
208 g_string_free(path, TRUE);
210 IRRECO_RETURN
214 gboolean _save_theme_to_dir(IrrecoThemeSaveDlg *self, IrrecoData *irreco_data,
215 IrrecoTheme *irreco_theme)
218 gboolean rvalue = FALSE;
219 GString *theme_path;
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);
228 IRRECO_ENTER
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/";
241 } else {
242 folder = IRRECO_THEME_DIR;
245 /* Call IrrecoThemeSave */
246 if(!irreco_is_dir(folder)) {
247 g_mkdir(folder, 0777);
248 } else {
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)
255 /*Check theme name*/
256 if (g_str_equal(irreco_theme->name->str,
257 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"),
264 theme->name->str);
266 irreco_error_dlg(GTK_WINDOW(self),
267 message->str);
268 goto end;
270 delete_mode += 1;
271 same_theme = theme;
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"),
282 theme->name->str);
284 irreco_error_dlg(GTK_WINDOW(self),
285 message->str);
286 goto end;
288 delete_mode += 2;
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?"),
302 same_theme_folder);
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,
309 same_theme_folder);
311 /* Check whether a theme folder already exists */
312 if (delete_mode != 0) {
313 /* Create dialog*/
314 if (!irreco_yes_no_dlg(
315 GTK_WINDOW(self), message->str)) {
316 /*continue;*/
317 } else {
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);
322 system(rm_cmd);
323 g_free(rm_cmd);
325 if (delete_mode == 2 || delete_mode == 3) {
326 irreco_theme_manager_remove_theme(
327 irreco_data->theme_manager,
328 same_theme_folder);
329 g_free(same_theme_folder);
334 /*Create folder */
335 g_mkdir(theme_path->str, 0777);
337 /*Create new theme*/
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);
344 } else {
345 irreco_theme_set(same_theme,
346 irreco_theme->name->str,
347 theme_path->str,
348 irreco_theme->source->str,
349 irreco_theme->author->str,
350 irreco_theme->comment->str,
351 irreco_theme->preview_button_name->str,
352 NULL);
355 /* Get buttons and backgrounds */
356 _save_bg_to_theme_dir(self, irreco_data, irreco_theme, theme_path->str);
358 rvalue = TRUE;
360 end:
361 g_string_free(message, TRUE);
362 g_string_free(theme_path, TRUE);
365 # if 0
371 /* Save image_up to folder */
372 g_string_printf(file_path, "%s/%s/%s", theme_button_dir, folder,
373 image_up_name);
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,
379 image_down_name);
381 /*Check image hash data*/
382 image_down = (gchar*) g_base64_decode(base64_image_down,
383 &image_down_len);
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,
386 image_down,
387 image_down_len);
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");
393 goto end;
396 /* Create keyfile and save it to folder*/
397 irreco_gkeyfile_set_string(keyfile, "theme-button" , "name", name);
399 if (allow_text) {
400 irreco_gkeyfile_set_string(keyfile, "theme-button",
401 "allow-text", "true");
402 } else {
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,
427 "theme-button",
428 "text-h-align",
429 (gfloat) text_h_align);
431 irreco_gkeyfile_set_gfloat(keyfile,
432 "theme-button",
433 "text-v-align",
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. */
441 rvalue = TRUE;
442 #endif
448 IRRECO_RETURN_BOOL(rvalue);
451 /** @} */
453 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
454 /* Functions */
455 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
458 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
459 /* Public Functions */
460 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
462 gboolean
463 irreco_theme_save_dlg_run(IrrecoData *irreco_data, IrrecoTheme *irreco_theme,
464 GtkWindow *parent_window)
466 IrrecoThemeSaveDlg *self;
467 gint response;
468 gboolean loop = TRUE;
469 gboolean rvalue = FALSE;
471 IRRECO_ENTER
473 self = (IrrecoThemeSaveDlg*)irreco_theme_save_dlg_create(
474 irreco_data, parent_window);
475 self->irreco_data = irreco_data;
476 self->theme = irreco_theme;
480 do {
481 response = gtk_dialog_run(GTK_DIALOG(self));
482 switch (response) {
483 case GTK_RESPONSE_OK:
485 if (_save_theme_to_dir(self, irreco_data,
486 irreco_theme)) {
489 loop = FALSE;
490 rvalue = TRUE;
492 break;
493 case GTK_RESPONSE_CANCEL:
494 IRRECO_DEBUG("GTK_RESPONSE_CANCEL\n");
495 rvalue = FALSE;
496 loop = FALSE;
497 break;
499 default:
500 IRRECO_DEBUG("default\n");
501 break;
504 } while (loop);
506 gtk_widget_destroy(GTK_WIDGET(self));
508 IRRECO_RETURN_BOOL(rvalue);