Clean code
[irreco.git] / irreco / src / core / irreco_theme_bg.c
blob0c071ca2eea792856bad8a67e027626bee1e938a
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2008 Joni Kokko (t5kojo01@students.oamk.fi)
4 * Pekka Gehör (pegu6@msn.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "irreco_theme_bg.h"
23 /**
24 * @addtogroup IrrecoThemeBg
25 * @ingroup Irreco
27 * Contains information of background.
29 * @{
32 /**
33 * @file
34 * Source file of @ref IrrecoThemeBg.
37 #define IRRECO_BACKGROUNDS_PREVIEW_WIDHT (IRRECO_SCREEN_WIDTH)
38 #define IRRECO_BACKGROUNDS_PREVIEW_HEIGHT (IRRECO_SCREEN_HEIGHT)
39 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
40 /* Prototypes */
41 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
43 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
44 /* Construction & Destruction */
45 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
47 /**
48 * @name Construction & Destruction
49 * @{
52 IrrecoThemeBg *irreco_theme_bg_new()
54 IrrecoThemeBg *self;
55 IRRECO_ENTER
57 self = g_slice_new0(IrrecoThemeBg);
59 self->image_name = g_string_new("");
60 self->image_path = g_string_new("");
62 IRRECO_RETURN_PTR(self);
65 void irreco_theme_bg_free(IrrecoThemeBg *self)
67 IRRECO_ENTER
69 g_assert(self != NULL);
71 g_string_free(self->image_name, TRUE);
72 self->image_name = NULL;
74 g_string_free(self->image_path, TRUE);
75 self->image_path = NULL;
77 g_slice_free(IrrecoThemeBg, self);
78 self = NULL;
80 IRRECO_RETURN
83 /** @} */
85 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
86 /* Private Functions */
87 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
89 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
90 /* Public Functions */
91 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
93 /**
94 * @name Public Functions
95 * @{
98 void irreco_theme_bg_set(IrrecoThemeBg *self,
99 const char *image_name,
100 const char *image_path)
102 IRRECO_ENTER
104 g_string_printf(self->image_name, "%s", image_name);
105 g_string_printf(self->image_path, "%s", image_path);
107 IRRECO_RETURN
110 void irreco_theme_bg_print(IrrecoThemeBg *self)
112 IRRECO_ENTER
114 IRRECO_DEBUG("--------------------------------------------\n");
115 IRRECO_DEBUG("Background-image: %s\n", self->image_name->str);
116 IRRECO_DEBUG("Background-image_path: %s\n", self->image_path->str);
117 IRRECO_DEBUG("--------------------------------------------\n");
119 IRRECO_RETURN
122 IrrecoThemeBg *irreco_theme_bg_copy(IrrecoThemeBg *self)
124 IrrecoThemeBg *new = NULL;
125 IRRECO_ENTER
127 new = irreco_theme_bg_new();
128 irreco_theme_bg_set(new, self->image_name->str, self->image_path->str);
129 IRRECO_RETURN_PTR(new);
133 * IrrecoThemeBg new from dir
136 IrrecoThemeBg *irreco_theme_bg_new_from_dir(const gchar *dir)
138 IrrecoThemeBg *self = NULL;
140 IRRECO_ENTER
142 self = irreco_theme_bg_new();
143 irreco_theme_bg_read(self, dir);
144 IRRECO_RETURN_PTR(self);
147 void irreco_theme_bg_read(IrrecoThemeBg *self, const gchar *dir)
150 IrrecoKeyFile *keyfile;
151 char *name = NULL;
152 char *image = NULL;
153 GString *conf = NULL;
154 IRRECO_ENTER
156 conf = g_string_new(dir);
157 g_string_append_printf(conf, "/bg.conf");
158 keyfile = irreco_keyfile_create(dir,
159 conf->str,
160 "theme-bg");
161 if (keyfile == NULL) goto end;
162 /* Required fields. */
163 if (!irreco_keyfile_get_str(keyfile, "name", &name) ||
164 !irreco_keyfile_get_path(keyfile, "image", &image)) {
165 IRRECO_PRINTF("Could not read style from group \"%s\"\n",
166 keyfile->group);
167 goto end;
170 irreco_theme_bg_set(self, name, image);
173 irreco_theme_bg_print(self);
175 end:
176 if(keyfile != NULL) irreco_keyfile_destroy(keyfile);
177 if(name != NULL) g_free(name);
178 if(image != NULL) g_free(image);
179 IRRECO_RETURN
184 * Save bg to theme folder
186 gboolean irreco_theme_bg_save(IrrecoThemeBg *self, const gchar *path)
188 gboolean rvalue = FALSE;
189 GString *keyfile_path;
190 GString *image_path;
191 GString *folder;
192 gchar *folder_name;
193 GKeyFile *keyfile;
194 gchar *cp_cmd;
195 gchar *type;
196 GString *image_name;
198 IRRECO_ENTER
200 keyfile = g_key_file_new();
201 image_name = g_string_new(NULL);
202 image_path = g_string_new(path);
203 keyfile_path = g_string_new(path);
204 folder = g_string_new(self->image_name->str);
206 g_string_ascii_down(folder);
207 folder_name = g_strdup(folder->str);
208 g_strdelimit(folder_name, " ", '_');
209 /*g_strdelimit(folder_name, "-|> <.", "a");*/
210 g_string_printf(folder, "%s", folder_name);
211 /* Create Folder */
212 g_string_append_printf(image_path, "/%s", folder_name);
213 g_string_append_printf(keyfile_path, "/%s", folder_name);
215 IRRECO_DEBUG("mkdir %s\n",image_path->str);
216 g_mkdir(image_path->str, 0777);
218 /* get file type */
219 type = g_strrstr(self->image_path->str, ".");
220 g_string_printf(image_name, "%s%s", "image", type);
221 g_string_append_printf(image_path, "/%s%s", "image", type);
223 /* Copy image to bg folder */
225 cp_cmd = g_strconcat("cp -f ", self->image_path->str, " ",
226 image_path->str, NULL);
227 system(cp_cmd);
229 /* Create keyfile and save it to folder*/
230 irreco_gkeyfile_set_string(keyfile, "theme-bg" , "name",
231 self->image_name->str);
232 irreco_gkeyfile_set_string(keyfile, "theme-bg", "image", image_name->str);
233 g_string_append_printf(keyfile_path, "/bg.conf");
234 irreco_write_keyfile(keyfile, keyfile_path->str);
236 /* No error occured. */
237 rvalue = TRUE;
239 g_free(cp_cmd);
240 /*g_free(type);*/
241 g_string_free(image_name, TRUE);
242 g_key_file_free(keyfile);
243 g_string_free(keyfile_path, TRUE);
244 g_string_free(image_path, TRUE);
246 IRRECO_RETURN_BOOL(rvalue);
249 /** @} */
251 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
252 /* Events and Callbacks */
253 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
255 /** @} */