Clean code and added a few amendments (if()).
[irreco.git] / irreco / src / core / irreco_theme_manager.c
blobf5161729bd0f637422b24ea6f0dd4eb8dbad78d9
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2008 Joni Kokko (t5kojo01@students.oamk.fi)
4 * Sami Mäki (kasmra@xob.kapsi.fi)
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_manager.h"
23 /**
24 * @addtogroup IrrecoThemeManager
25 * @ingroup Irreco
27 * Contains information of themes.
29 * @{
32 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
33 /* Prototypes */
34 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
36 void irreco_theme_manager_read_file_foreach(IrrecoDirForeachData * dir_data);
38 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
39 /* Construction & Destruction */
40 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
42 /**
43 * @name Construction & Destruction
44 * @{
47 IrrecoThemeManager *irreco_theme_manager_new(IrrecoData * irreco_data)
49 IrrecoThemeManager *self;
50 IRRECO_ENTER
52 self = g_slice_new0(IrrecoThemeManager);
54 self->irreco_data = irreco_data;
56 self->themes = irreco_string_table_new(
57 (GDestroyNotify)irreco_theme_free, NULL);
59 irreco_theme_manager_read_themes_from_dir(self, IRRECO_THEME_DIR);
60 irreco_theme_manager_read_themes_from_dir(self, "/media/mmc1/irreco");
61 irreco_theme_manager_read_themes_from_dir(self, "/media/mmc2/irreco");
63 IRRECO_RETURN_PTR(self);
66 void irreco_theme_manager_free(IrrecoThemeManager *self)
68 IRRECO_ENTER
70 g_assert(self != NULL);
72 irreco_string_table_free(self->themes);
73 self->themes = NULL;
75 g_slice_free(IrrecoThemeManager, self);
76 self = NULL;
78 IRRECO_RETURN
81 /** @} */
83 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
84 /* Private Functions */
85 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
87 /**
88 * @name Private Functions
89 * @{
92 void irreco_theme_manager_read_file_foreach(IrrecoDirForeachData * dir_data)
94 IrrecoThemeManager *self = (IrrecoThemeManager*) dir_data->user_data_1;
95 IrrecoTheme *theme;
96 IrrecoKeyFile *keyfile = NULL;
97 char *name = NULL;
98 char *source = NULL;
99 char *author = NULL;
100 char *comment = NULL;
101 char *preview_button = NULL;
102 char *version = NULL;
103 GString *conf = g_string_new(dir_data->filepath);
104 IRRECO_ENTER
106 g_string_append_printf(conf, "/theme.conf");
107 keyfile = irreco_keyfile_create(dir_data->filepath,
108 conf->str,
109 "theme");
110 if (keyfile == NULL) goto end;
112 /* Required fields. */
113 if (!irreco_keyfile_get_str(keyfile, "name", &name)) {
114 IRRECO_PRINTF("Could not read theme \"%s\"\n",
115 dir_data->filename);
116 goto end;
119 /* Optional fields. */
120 irreco_keyfile_get_str(keyfile, "source", &source);
121 irreco_keyfile_get_str(keyfile, "author", &author);
122 irreco_keyfile_get_str(keyfile, "comment", &comment);
123 irreco_keyfile_get_str(keyfile, "preview-button", &preview_button);
124 irreco_keyfile_get_str(keyfile, "version", &version);
126 if (irreco_string_table_get(self->themes, name, (gpointer *) &theme)) {
127 irreco_theme_set(theme, name, dir_data->filepath, source,
128 author, comment, preview_button, version);
129 } else {
130 theme = irreco_theme_new(name, dir_data->filepath, source,
131 author, comment, preview_button, version);
132 irreco_string_table_add(self->themes, theme->name->str, theme);
135 irreco_theme_print(theme);
137 end:
138 g_string_free(conf, TRUE);
139 if (keyfile != NULL) irreco_keyfile_destroy(keyfile);
140 if (name != NULL) g_free(name);
141 if (source != NULL) g_free(source);
142 if (author != NULL) g_free(author);
143 if (comment != NULL) g_free(comment);
144 if (preview_button != NULL) g_free(preview_button);
145 if (version != NULL) g_free(version);
147 IRRECO_RETURN
150 /** @} */
152 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
153 /* Public Functions */
154 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
157 * @name Public Functions
158 * @{
161 void irreco_theme_manager_print(IrrecoThemeManager *self)
163 IRRECO_ENTER
165 #if 1
166 irreco_string_table_print(self->themes);
167 #endif
169 #if 0
170 IRRECO_STRING_TABLE_FOREACH_DATA(self->themes, void *, theme)
171 IRRECO_PRINTF("Theme: %s \n", theme->name->str);
172 irreco_theme_print(theme);
173 IRRECO_STRING_TABLE_FOREACH_END
174 #endif
176 IRRECO_RETURN
179 IrrecoStringTable *irreco_theme_manager_get_themes(IrrecoThemeManager *self)
181 IRRECO_ENTER
183 IRRECO_RETURN_PTR(self->themes);
186 IrrecoStringTable *irreco_theme_manager_get_buttons(IrrecoThemeManager *self,
187 const gchar *theme_name)
189 IrrecoStringTable *buttons = NULL;
190 IrrecoTheme *theme = NULL;
191 IRRECO_ENTER
193 IRRECO_PRINTF("theme_name: %s\n", theme_name);
194 if (irreco_string_table_get(self->themes, theme_name,
195 (gpointer*) &theme)) {
196 buttons = theme->buttons;
199 IRRECO_RETURN_PTR(buttons);
202 IrrecoStringTable *irreco_theme_manager_get_backgrounds(IrrecoThemeManager *self,
203 const gchar *theme_name)
205 IrrecoStringTable *backgrounds = NULL;
206 IrrecoTheme *theme = NULL;
207 IRRECO_ENTER
209 if (irreco_string_table_get(self->themes, theme_name,
210 (gpointer*)&theme)) {
211 backgrounds = theme->backgrounds;
214 IRRECO_RETURN_PTR(backgrounds);
217 void irreco_theme_manager_read_themes_from_dir(IrrecoThemeManager *self,
218 const gchar *dir)
220 IrrecoDirForeachData themes;
221 IRRECO_ENTER
223 IRRECO_DEBUG("THEME_DIR = %s\n", dir);
224 themes.directory = dir;
226 themes.filesuffix = "";
227 themes.user_data_1 = self;
229 irreco_dir_foreach(&themes, irreco_theme_manager_read_file_foreach);
231 irreco_string_table_sort_abc(self->themes);
232 IRRECO_RETURN
235 gboolean irreco_theme_manager_get_button_style(IrrecoThemeManager *self,
236 const gchar *style_name,
237 IrrecoThemeButton **style)
239 IrrecoThemeButton *button = NULL;
240 gboolean rvalue = FALSE;
241 IRRECO_ENTER
242 IRRECO_STRING_TABLE_FOREACH_DATA(self->themes, void *, theme) {
243 IrrecoStringTable* buttons = irreco_theme_get_buttons(theme);
245 if (irreco_string_table_get(buttons, style_name,
246 (gpointer *) &button)) {
247 *style = button;
248 rvalue = TRUE;
251 IRRECO_STRING_TABLE_FOREACH_END
252 IRRECO_RETURN_BOOL(rvalue);
255 gboolean irreco_theme_manager_does_deb_exist(IrrecoThemeManager *self)
257 gboolean rvalue = FALSE;
258 IRRECO_ENTER
259 IRRECO_STRING_TABLE_FOREACH_DATA(self->themes, IrrecoTheme *, theme) {
260 if (g_utf8_collate(theme->source->str, "deb") == 0 ||
261 g_utf8_collate(theme->source->str, "DEB") == 0) {
262 rvalue = TRUE;
265 IRRECO_STRING_TABLE_FOREACH_END
266 IRRECO_RETURN_BOOL(rvalue);
269 gboolean irreco_theme_manager_does_web_exist(IrrecoThemeManager *self)
271 gboolean rvalue = FALSE;
272 IRRECO_ENTER
273 IRRECO_STRING_TABLE_FOREACH_DATA(self->themes, IrrecoTheme *, theme) {
274 if (g_utf8_collate(theme->source->str, "web") == 0 ||
275 g_utf8_collate(theme->source->str, "WEB") == 0) {
276 rvalue = TRUE;
279 IRRECO_STRING_TABLE_FOREACH_END
280 IRRECO_RETURN_BOOL(rvalue);
283 gboolean irreco_theme_manager_does_user_exist(IrrecoThemeManager *self)
285 gboolean rvalue = FALSE;
286 IRRECO_ENTER
287 IRRECO_STRING_TABLE_FOREACH_DATA(self->themes, IrrecoTheme *, theme) {
288 if (g_utf8_collate(theme->source->str, "user") == 0 ||
289 g_utf8_collate(theme->source->str, "USER") == 0) {
290 rvalue = TRUE;
293 IRRECO_STRING_TABLE_FOREACH_END
294 IRRECO_RETURN_BOOL(rvalue);
297 gboolean irreco_theme_manager_remove_theme(IrrecoThemeManager *self,
298 const gchar *theme_name)
300 IrrecoTheme *rmtheme;
301 gchar *rm_cmd;
302 gboolean rvalue = FALSE;
303 IrrecoStringTable *table;
305 IRRECO_ENTER
307 if (irreco_string_table_get(self->themes, theme_name,
308 (gpointer *) &rmtheme)) {
309 /* Remove styles from layouts */
310 table = self->irreco_data->irreco_layout_array;
311 IRRECO_STRING_TABLE_FOREACH(table, key,
312 IrrecoButtonLayout *, layout)
313 IRRECO_PTR_ARRAY_FORWARDS(layout->button_array,
314 IrrecoButton *, button)
315 if (button->style != NULL &&
316 g_str_equal(button->style->theme_name->str,
317 theme_name)) {
318 button->type = IRRECO_BTYPE_GTK_BUTTON;
319 irreco_button_set_style(button, NULL);
320 irreco_button_create_widget(button);
323 IRRECO_PTR_ARRAY_FORWARDS_END
324 IRRECO_STRING_TABLE_FOREACH_END
326 /* Save layouts */
327 irreco_config_save_layouts(self->irreco_data);
329 /* Build remove command */
330 rm_cmd = g_strconcat("rm -r ", rmtheme->path->str, NULL);
332 /* Remove theme directory and its contents from device */
333 system(rm_cmd);
335 g_free(rm_cmd);
337 /* And from the string table */
338 irreco_string_table_remove(self->themes, theme_name);
340 rvalue = TRUE;
343 IRRECO_RETURN_BOOL(rvalue);
346 void irreco_theme_manager_update_theme_manager(IrrecoThemeManager *self)
348 IRRECO_ENTER
350 irreco_theme_manager_read_themes_from_dir(self, IRRECO_THEME_DIR);
351 irreco_theme_manager_read_themes_from_dir(self, "/media/mmc1/irreco");
352 irreco_theme_manager_read_themes_from_dir(self, "/media/mmc2/irreco");
354 /* Check if some theme is deleted */
355 IRRECO_STRING_TABLE_FOREACH(self->themes, key, IrrecoTheme *,
356 theme)
357 if(!irreco_is_dir(theme->path->str)) {
358 irreco_string_table_remove(self->themes, key);
360 IRRECO_STRING_TABLE_FOREACH_END
362 IRRECO_RETURN
365 /** @} */
367 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
368 /* Events and Callbacks */
369 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
371 /** @} */