clean code and changed version nro.
[irreco.git] / irreco / src / core / irreco_theme_save_dlg.c
blob2209b27aa8d39713ab1fbc935e0cc0ba3adfd583
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 "irreco_theme_button.h"
24 #include <hildon/hildon-banner.h>
25 #include <hildon/hildon-color-button.h>
26 #include <hildon/hildon-file-chooser-dialog.h>
28 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
29 /* Prototypes */
30 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
31 gboolean _save_theme_to_dir(IrrecoThemeSaveDlg *self);
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 IrrecoThemeSaveDlg *self;
49 GtkWidget *label;
51 IRRECO_ENTER
53 G_OBJECT_CLASS(
54 irreco_theme_save_dlg_parent_class)->constructed(object);
56 self = IRRECO_THEME_SAVE_DLG(object);
58 /* Construct dialog. */
59 gtk_window_set_title(GTK_WINDOW(self), _("Theme Save"));
60 gtk_window_set_modal(GTK_WINDOW(self), TRUE);
61 gtk_window_set_destroy_with_parent(GTK_WINDOW(self), TRUE);
62 gtk_dialog_set_has_separator(GTK_DIALOG(self), FALSE);
64 /*Buttons*/
65 gtk_dialog_add_buttons(GTK_DIALOG(self),
66 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
67 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
68 NULL);
71 label = gtk_label_new("Select save location");
73 self->radio1 = gtk_radio_button_new_with_label (NULL, "MMC1");
74 self->radio2 = gtk_radio_button_new_with_label_from_widget(
75 GTK_RADIO_BUTTON(
76 self->radio1), "MMC2");
77 self->radio3 = gtk_radio_button_new_with_label_from_widget(
78 GTK_RADIO_BUTTON(
79 self->radio2), "DEVICE");
81 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(self)->vbox), label);
83 if(irreco_is_dir("/media/mmc1")) {
84 gtk_box_pack_start_defaults(GTK_BOX(
85 GTK_DIALOG(self)->vbox),
86 self->radio1);
89 if(irreco_is_dir("/media/mmc2")) {
90 gtk_box_pack_start_defaults(GTK_BOX(
91 GTK_DIALOG(self)->vbox),
92 self->radio2);
98 gtk_widget_show_all(GTK_WIDGET(self));
99 IRRECO_RETURN
102 static void
103 irreco_theme_save_dlg_init (IrrecoThemeSaveDlg *object)
105 IRRECO_ENTER
106 IRRECO_RETURN
109 static void
110 irreco_theme_save_dlg_finalize (GObject *object)
112 /* TODO: Add deinitalization code here */
113 IrrecoThemeSaveDlg *self;
114 IRRECO_ENTER
116 self = IRRECO_THEME_SAVE_DLG(object);
117 G_OBJECT_CLASS (irreco_theme_save_dlg_parent_class)->finalize (object);
118 IRRECO_RETURN
121 static void
122 irreco_theme_save_dlg_class_init (IrrecoThemeSaveDlgClass *klass)
124 GObjectClass* object_class = G_OBJECT_CLASS (klass);
125 object_class->finalize = irreco_theme_save_dlg_finalize;
126 object_class->constructed = irreco_theme_save_dlg_constructed;
129 GtkWidget *
130 irreco_theme_save_dlg_new(IrrecoData *irreco_data, GtkWindow *parent)
132 IrrecoThemeSaveDlg *self;
134 IRRECO_ENTER
136 self = g_object_new(IRRECO_TYPE_THEME_SAVE_DLG,
137 "irreco-data", irreco_data, NULL);
138 irreco_dlg_set_parent(IRRECO_DLG(self), parent);
140 IRRECO_RETURN_PTR(self);
143 IrrecoThemeSaveDlg *
144 irreco_theme_save_dlg_create(IrrecoData *irreco_data, GtkWindow *parent_window)
146 IRRECO_ENTER
147 IRRECO_RETURN_PTR(irreco_theme_save_dlg_new(
148 irreco_data, parent_window));
151 /** @} */
153 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
154 /* Private Functions */
155 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
157 * @name Private Functions
158 * @{
161 gboolean _save_theme_to_dir(IrrecoThemeSaveDlg *self)
163 gboolean rvalue = FALSE;
164 GString *theme_path;
165 gchar *folder = NULL;
166 IrrecoThemeManager *theme_manager = self->irreco_data->theme_manager;
167 IrrecoTheme *same_theme = NULL;
168 gint delete_mode = 0;
169 gchar *same_theme_folder = NULL;
170 GString *message = g_string_new(NULL);
171 GString *theme_name;
172 gchar *redy_theme_name;
173 IRRECO_ENTER
175 /* Create Theme Folder */
176 theme_path = g_string_new("");
177 theme_name = g_string_new(self->theme->name->str);
179 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(self->radio1))) {
180 folder = "/media/mmc1/irreco/";
181 } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(self->radio2))) {
182 folder = "/media/mmc2/irreco/";
183 } else {
184 folder = IRRECO_THEME_DIR;
187 /* Call IrrecoThemeSave */
188 if(!irreco_is_dir(folder)) {
189 g_mkdir(folder, 0777);
190 } else {
194 /* parse the theme name */
195 g_string_ascii_down(theme_name);
196 redy_theme_name = g_strdup(theme_name->str);
197 g_strdelimit(redy_theme_name, " ", '_');
198 g_strdelimit(redy_theme_name, ",-|> <.", 'a');
199 g_string_printf(theme_path, "%s%s", folder, redy_theme_name);
201 IRRECO_STRING_TABLE_FOREACH_DATA(theme_manager->themes,
202 IrrecoTheme *, theme)
203 /*Check theme name*/
204 if (g_str_equal(self->theme->name->str,
205 theme->name->str)) {
206 if (g_str_equal(theme->source->str, "deb")) {
207 g_string_printf(message,
208 _("The \"%s\" theme already exists.\n"
209 "Can't overwrite \"Built In themes\"\n"
210 "Please remove it first from the\n"
211 "Application Manager or change the name of the theme"),
212 theme->name->str);
213 irreco_error_dlg(GTK_WINDOW(self),
214 message->str);
215 goto end;
217 delete_mode += 1;
218 same_theme = theme;
220 /*Check theme folder path*/
221 else if (g_str_equal(theme_path->str,
222 g_strconcat(theme->path->str, "/", NULL))){
223 if (g_str_equal(theme->source->str, "deb")) {
224 g_string_printf(message,
225 _("This theme replaces \"%s\" theme.\n"
226 "Can't overwrite \"Built In themes\"\n"
227 "Please remove it first from the\n"
228 "Application Manager or change the name of the theme"),
229 theme->name->str);
231 irreco_error_dlg(GTK_WINDOW(self),
232 message->str);
233 goto end;
235 delete_mode += 2;
236 same_theme_folder = g_strdup(theme->name->str);
238 IRRECO_STRING_TABLE_FOREACH_END
240 if (delete_mode == 1) {
241 g_string_printf(message,
242 _("The \"%s\" theme already exists.\n"
243 "Do you want to edit this theme?"),
244 same_theme->name->str);
245 } else if (delete_mode == 2) {
246 g_string_printf(message,
247 _("This theme replaces \"%s\" theme.\n"
248 "Do you want to continue?"),
249 same_theme_folder);
250 } else if (delete_mode == 3) {
251 g_string_printf(message,
252 _("This theme replaces themes\n"
253 "\"%s\" and \"%s\"\n"
254 "Do you want to continue?"),
255 same_theme->name->str,
256 same_theme_folder);
258 /* Check whether a theme folder already exists */
259 if (delete_mode != 0) {
260 /* Create dialog*/
261 if (!irreco_yes_no_dlg(
262 GTK_WINDOW(self), message->str)) {
263 goto end;
264 /*continue;*/
265 } else {
266 /* Check and remove theme folder*/
267 if (delete_mode == 1 || delete_mode == 3) {
268 gchar *rm_cmd;
269 rm_cmd = g_strconcat("rm -r ",
270 "/media/mmc1/irreco/",
271 redy_theme_name, NULL);
272 if (irreco_is_dir(g_strconcat(
273 "/media/mmc1/irreco/",
274 redy_theme_name, NULL))) {
275 system(rm_cmd);
277 rm_cmd = g_strconcat("rm -r ",
278 "/media/mmc2/irreco/",
279 redy_theme_name, NULL);
280 if (irreco_is_dir(g_strconcat(
281 "/media/mmc2/irreco/",
282 redy_theme_name, NULL))) {
283 system(rm_cmd);
285 g_free(rm_cmd);
287 if (delete_mode == 2 || delete_mode == 3) {
288 irreco_theme_manager_remove_theme(
289 self->irreco_data->theme_manager,
290 same_theme_folder);
291 g_free(same_theme_folder);
295 /*Create folder */
296 g_mkdir(theme_path->str, 0777);
297 /*Create new theme*/
299 irreco_theme_save(self->theme, theme_path->str);
301 rvalue = TRUE;
303 end:
304 g_string_free(theme_name, TRUE);
305 g_string_free(message, TRUE);
306 g_string_free(theme_path, TRUE);
308 IRRECO_RETURN_BOOL(rvalue);
311 /** @} */
313 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
314 /* Functions */
315 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
318 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
319 /* Public Functions */
320 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
322 gboolean
323 irreco_theme_save_dlg_run(IrrecoData *irreco_data, IrrecoTheme *irreco_theme,
324 GtkWindow *parent_window)
326 IrrecoThemeSaveDlg *self;
327 gint response;
328 gboolean loop = TRUE;
329 gboolean rvalue = FALSE;
331 IRRECO_ENTER
333 self = (IrrecoThemeSaveDlg*)irreco_theme_save_dlg_create(
334 irreco_data, parent_window);
335 self->irreco_data = irreco_data;
336 self->theme = irreco_theme;
338 do {
339 response = gtk_dialog_run(GTK_DIALOG(self));
340 switch (response) {
341 case GTK_RESPONSE_OK:
343 if (_save_theme_to_dir(self)) {
344 loop = FALSE;
345 rvalue = TRUE;
346 } else {
347 loop = TRUE;
350 break;
351 case GTK_RESPONSE_CANCEL:
352 IRRECO_DEBUG("GTK_RESPONSE_CANCEL\n");
353 rvalue = FALSE;
354 loop = FALSE;
355 break;
357 default:
358 IRRECO_DEBUG("default\n");
359 break;
362 } while (loop);
364 gtk_widget_destroy(GTK_WIDGET(self));
366 IRRECO_RETURN_BOOL(rvalue);