Added scrolled window
[irreco.git] / irreco / src / core / irreco_theme_save_dlg.c
blob08c62c65c8a3070c2f08ffdc915738796a0bc4c4
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, IrrecoData *irreco_data,
32 IrrecoTheme *irreco_theme);
33 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
34 /* Construction & Destruction */
35 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
37 /**
38 * @name Construction & Destruction
39 * @{
43 G_DEFINE_TYPE (IrrecoThemeSaveDlg, irreco_theme_save_dlg,
44 IRRECO_TYPE_INTERNAL_DLG)
47 static void irreco_theme_save_dlg_constructed(GObject *object)
49 IrrecoData *irreco_data;
50 IrrecoThemeSaveDlg *self;
51 GtkWidget *label;
53 IRRECO_ENTER
55 G_OBJECT_CLASS(
56 irreco_theme_save_dlg_parent_class)->constructed(object);
58 self = IRRECO_THEME_SAVE_DLG(object);
60 irreco_data = irreco_internal_dlg_get_irreco_data(IRRECO_INTERNAL_DLG(
61 self));
63 /* Construct dialog. */
64 gtk_window_set_title(GTK_WINDOW(self), _("Theme Save"));
65 gtk_window_set_modal(GTK_WINDOW(self), TRUE);
66 gtk_window_set_destroy_with_parent(GTK_WINDOW(self), TRUE);
67 gtk_dialog_set_has_separator(GTK_DIALOG(self), FALSE);
69 /*Buttons*/
70 gtk_dialog_add_buttons(GTK_DIALOG(self),
71 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
72 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
73 NULL);
76 label = gtk_label_new("Select save location");
78 self->radio1 = gtk_radio_button_new_with_label (NULL, "MMC1");
79 self->radio2 = gtk_radio_button_new_with_label_from_widget(
80 GTK_RADIO_BUTTON(
81 self->radio1), "MMC2");
82 self->radio3 = gtk_radio_button_new_with_label_from_widget(
83 GTK_RADIO_BUTTON(
84 self->radio2), "DEVICE");
86 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(self)->vbox), label);
88 if(irreco_is_dir("/media/mmc1")) {
89 gtk_box_pack_start_defaults(GTK_BOX(
90 GTK_DIALOG(self)->vbox),
91 self->radio1);
94 if(irreco_is_dir("/media/mmc2")) {
95 gtk_box_pack_start_defaults(GTK_BOX(
96 GTK_DIALOG(self)->vbox),
97 self->radio2);
103 gtk_widget_show_all(GTK_WIDGET(self));
104 IRRECO_RETURN
107 static void
108 irreco_theme_save_dlg_init (IrrecoThemeSaveDlg *object)
110 IRRECO_ENTER
111 IRRECO_RETURN
114 static void
115 irreco_theme_save_dlg_finalize (GObject *object)
117 /* TODO: Add deinitalization code here */
118 IrrecoThemeSaveDlg *self;
119 IRRECO_ENTER
121 self = IRRECO_THEME_SAVE_DLG(object);
122 G_OBJECT_CLASS (irreco_theme_save_dlg_parent_class)->finalize (object);
123 IRRECO_RETURN
126 static void
127 irreco_theme_save_dlg_class_init (IrrecoThemeSaveDlgClass *klass)
129 GObjectClass* object_class = G_OBJECT_CLASS (klass);
130 object_class->finalize = irreco_theme_save_dlg_finalize;
131 object_class->constructed = irreco_theme_save_dlg_constructed;
134 GtkWidget *
135 irreco_theme_save_dlg_new(IrrecoData *irreco_data, GtkWindow *parent)
137 IrrecoThemeSaveDlg *self;
139 IRRECO_ENTER
141 self = g_object_new(IRRECO_TYPE_THEME_SAVE_DLG,
142 "irreco-data", irreco_data, NULL);
143 irreco_dlg_set_parent(IRRECO_DLG(self), parent);
145 IRRECO_RETURN_PTR(self);
148 IrrecoThemeSaveDlg *
149 irreco_theme_save_dlg_create(IrrecoData *irreco_data, GtkWindow *parent_window)
151 IRRECO_ENTER
152 IRRECO_RETURN_PTR(irreco_theme_save_dlg_new(
153 irreco_data, parent_window));
156 /** @} */
158 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
159 /* Private Functions */
160 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
162 * @name Private Functions
163 * @{
166 gboolean _save_theme_to_dir(IrrecoThemeSaveDlg *self, IrrecoData *irreco_data,
167 IrrecoTheme *irreco_theme)
170 gboolean rvalue = FALSE;
171 GString *theme_path;
172 gchar *folder = NULL;
173 IrrecoThemeManager *theme_manager = irreco_data->theme_manager;
174 IrrecoTheme *same_theme = NULL;
175 gint delete_mode = 0;
176 gchar *same_theme_folder = NULL;
177 GString *message = g_string_new(NULL);
178 GString *theme_name;
179 gchar *redy_theme_name;
180 IRRECO_ENTER
183 /* Create Theme Folder */
185 theme_path = g_string_new("");
186 theme_name = g_string_new(irreco_theme->name->str);
188 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(self->radio1))) {
189 folder = "/media/mmc1/irreco/";
190 } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(self->radio2))) {
191 folder = "/media/mmc2/irreco/";
192 } else {
193 folder = IRRECO_THEME_DIR;
196 /* Call IrrecoThemeSave */
197 if(!irreco_is_dir(folder)) {
198 g_mkdir(folder, 0777);
199 } else {
203 /* parse the theme name */
204 g_string_ascii_down(theme_name);
205 redy_theme_name = g_strdup(theme_name->str);
206 g_strdelimit(redy_theme_name, " ", '_');
207 g_strdelimit(redy_theme_name, ",-|> <.", 'a');
208 g_string_printf(theme_path, "%s%s", folder, redy_theme_name);
210 IRRECO_STRING_TABLE_FOREACH_DATA(theme_manager->themes,
211 IrrecoTheme *, theme)
212 /*Check theme name*/
213 if (g_str_equal(irreco_theme->name->str,
214 theme->name->str)) {
215 if (g_str_equal(theme->source->str, "deb")) {
216 g_string_printf(message,
217 _("The \"%s\" theme already exists.\n"
218 "Can't overwrite \"Built In themes\"\n"
219 "Please remove it first from the\n"
220 "Application Manager or change the name of the theme"),
221 theme->name->str);
223 irreco_error_dlg(GTK_WINDOW(self),
224 message->str);
225 goto end;
227 delete_mode += 1;
228 same_theme = theme;
230 /*Check theme folder path*/
231 else if (g_str_equal(theme_path->str,
232 g_strconcat(theme->path->str, "/", NULL))){
233 if (g_str_equal(theme->source->str, "deb")) {
234 g_string_printf(message,
235 _("This theme replaces \"%s\" theme.\n"
236 "Can't overwrite \"Built In themes\"\n"
237 "Please remove it first from the\n"
238 "Application Manager or change the name of the theme"),
239 theme->name->str);
241 irreco_error_dlg(GTK_WINDOW(self),
242 message->str);
243 goto end;
245 delete_mode += 2;
246 same_theme_folder = g_strdup(theme->name->str);
248 IRRECO_STRING_TABLE_FOREACH_END
250 if (delete_mode == 1) {
251 g_string_printf(message,
252 _("The \"%s\" theme already exists.\n"
253 "Do you want to edit this theme?"),
254 same_theme->name->str);
255 } else if (delete_mode == 2) {
256 g_string_printf(message,
257 _("This theme replaces \"%s\" theme.\n"
258 "Do you want to continue?"),
259 same_theme_folder);
260 } else if (delete_mode == 3) {
261 g_string_printf(message,
262 _("This theme replaces themes\n"
263 "\"%s\" and \"%s\"\n"
264 "Do you want to continue?"),
265 same_theme->name->str,
266 same_theme_folder);
268 /* Check whether a theme folder already exists */
269 if (delete_mode != 0) {
270 /* Create dialog*/
271 if (!irreco_yes_no_dlg(
272 GTK_WINDOW(self), message->str)) {
273 goto end;
274 /*continue;*/
275 } else {
276 /* Remove theme folder and then make it again*/
277 if (delete_mode == 1 || delete_mode == 3) {
278 /*gchar *rm_cmd = g_strconcat("rm -r ",
279 same_theme->path->str, NULL);*/
280 gchar *rm_cmd = g_strconcat("rm -r ",
281 theme_path->str, NULL);
282 system(rm_cmd);
283 g_free(rm_cmd);
285 if (delete_mode == 2 || delete_mode == 3) {
286 irreco_theme_manager_remove_theme(
287 irreco_data->theme_manager,
288 same_theme_folder);
289 g_free(same_theme_folder);
293 /*Create folder */
294 g_mkdir(theme_path->str, 0777);
295 /*Create new theme*/
297 irreco_theme_save(irreco_theme, irreco_data, theme_path->str);
299 rvalue = TRUE;
301 end:
302 g_string_free(theme_name, TRUE);
303 g_string_free(message, TRUE);
304 g_string_free(theme_path, TRUE);
306 IRRECO_RETURN_BOOL(rvalue);
309 /** @} */
311 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
312 /* Functions */
313 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
316 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
317 /* Public Functions */
318 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
320 gboolean
321 irreco_theme_save_dlg_run(IrrecoData *irreco_data, IrrecoTheme *irreco_theme,
322 GtkWindow *parent_window)
324 IrrecoThemeSaveDlg *self;
325 gint response;
326 gboolean loop = TRUE;
327 gboolean rvalue = FALSE;
329 IRRECO_ENTER
331 self = (IrrecoThemeSaveDlg*)irreco_theme_save_dlg_create(
332 irreco_data, parent_window);
333 self->irreco_data = irreco_data;
334 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, irreco_data,
344 irreco_theme)) {
345 loop = FALSE;
346 rvalue = TRUE;
347 } else {
348 loop = TRUE;
351 break;
352 case GTK_RESPONSE_CANCEL:
353 IRRECO_DEBUG("GTK_RESPONSE_CANCEL\n");
354 rvalue = FALSE;
355 loop = FALSE;
356 break;
358 default:
359 IRRECO_DEBUG("default\n");
360 break;
363 } while (loop);
365 gtk_widget_destroy(GTK_WIDGET(self));
367 IRRECO_RETURN_BOOL(rvalue);