Added README.
[irreco.git] / irreco / src / core / irreco_theme_bg.c
blob6fcf11370c80ff666d28279049f0c546dff25770
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);
171 irreco_theme_bg_print(self);
173 end:
174 if(keyfile != NULL) irreco_keyfile_destroy(keyfile);
175 if(name != NULL) g_free(name);
176 if(image != NULL) g_free(image);
177 IRRECO_RETURN
182 * Save bg to theme folder
184 gboolean irreco_theme_bg_save(IrrecoThemeBg *self, const gchar *path)
186 gboolean rvalue = FALSE;
187 GString *keyfile_path;
188 GString *image_path;
189 GString *folder;
190 gchar *folder_name;
191 GKeyFile *keyfile;
192 gchar *cp_cmd;
193 gchar *type;
194 GString *image_name;
195 IRRECO_ENTER
197 keyfile = g_key_file_new();
198 image_name = g_string_new(NULL);
199 image_path = g_string_new(path);
200 keyfile_path = g_string_new(path);
201 folder = g_string_new(self->image_name->str);
203 g_string_ascii_down(folder);
204 folder_name = g_strdup(folder->str);
205 g_strdelimit(folder_name, " ", '_');
206 g_strdelimit(folder_name, "-|> <.", 'a');
207 g_string_printf(folder, "%s", folder_name);
208 /* Create Folder */
209 g_string_append_printf(image_path, "/%s", folder_name);
210 g_string_append_printf(keyfile_path, "/%s", folder_name);
212 IRRECO_DEBUG("mkdir %s\n",image_path->str);
213 g_mkdir(image_path->str, 0777);
215 /* get file type */
216 type = g_strrstr(self->image_path->str, ".");
217 g_string_printf(image_name, "%s%s", "image", type);
218 g_string_append_printf(image_path, "/%s%s", "image", type);
220 /* Copy image to bg folder */
221 cp_cmd = g_strconcat("cp -f '", self->image_path->str, "' '",
222 image_path->str,"'", NULL);
223 system(cp_cmd);
225 /* Create keyfile and save it to folder*/
226 irreco_gkeyfile_set_string(keyfile, "theme-bg" , "name",
227 self->image_name->str);
228 irreco_gkeyfile_set_string(keyfile, "theme-bg", "image", image_name->str);
229 g_string_append_printf(keyfile_path, "/bg.conf");
230 irreco_write_keyfile(keyfile, keyfile_path->str);
232 /* No error occured. */
233 rvalue = TRUE;
235 g_free(cp_cmd);
236 /*g_free(type);*/
237 g_string_free(image_name, TRUE);
238 g_key_file_free(keyfile);
239 g_string_free(keyfile_path, TRUE);
240 g_string_free(image_path, TRUE);
242 IRRECO_RETURN_BOOL(rvalue);
245 /** @} */
247 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
248 /* Events and Callbacks */
249 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
251 /** @} */