created new functions inside irreco_theme.c
[irreco.git] / irreco / src / core / irreco_theme_manager.c
blob0222fa8e3ed388812445c4e38042ee273e6f42c8
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 IrrecoTheme *theme = NULL;
95 IrrecoThemeManager *self = (IrrecoThemeManager*) dir_data->user_data_1;
96 char *name = NULL;
97 GString *conf = NULL;
98 IrrecoKeyFile *keyfile = NULL;
99 IRRECO_ENTER
101 conf = g_string_new(dir_data->filepath);
103 g_string_append_printf(conf, "/theme.conf");
104 keyfile = irreco_keyfile_create(dir_data->filepath,
105 conf->str,
106 "theme");
107 if (keyfile == NULL) goto end;
109 /* Required fields. */
110 if (!irreco_keyfile_get_str(keyfile, "name", &name)) {
111 IRRECO_PRINTF("Could not read theme \"%s\"\n",
112 dir_data->filename);
113 goto end;
116 if (irreco_string_table_get(self->themes, name, (gpointer *) &theme)) {
117 irreco_theme_read(theme, dir_data->filepath);
118 } else {
119 theme = irreco_theme_new_from_dir(dir_data->filepath);
120 irreco_string_table_add(self->themes, theme->name->str, theme);
122 end:
123 g_string_free(conf, TRUE);
124 if (keyfile != NULL) irreco_keyfile_destroy(keyfile);
125 if (name != NULL) g_free(name);
128 IRRECO_RETURN
131 /** @} */
133 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
134 /* Public Functions */
135 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
138 * @name Public Functions
139 * @{
142 void irreco_theme_manager_print(IrrecoThemeManager *self)
144 IRRECO_ENTER
146 #if 1
147 irreco_string_table_print(self->themes);
148 #endif
150 #if 0
151 IRRECO_STRING_TABLE_FOREACH_DATA(self->themes, void *, theme)
152 IRRECO_PRINTF("Theme: %s \n", theme->name->str);
153 irreco_theme_print(theme);
154 IRRECO_STRING_TABLE_FOREACH_END
155 #endif
157 IRRECO_RETURN
160 IrrecoStringTable *irreco_theme_manager_get_themes(IrrecoThemeManager *self)
162 IRRECO_ENTER
164 IRRECO_RETURN_PTR(self->themes);
167 IrrecoStringTable *irreco_theme_manager_get_buttons(IrrecoThemeManager *self,
168 const gchar *theme_name)
170 IrrecoStringTable *buttons = NULL;
171 IrrecoTheme *theme = NULL;
172 IRRECO_ENTER
174 IRRECO_PRINTF("theme_name: %s\n", theme_name);
175 if (irreco_string_table_get(self->themes, theme_name,
176 (gpointer*) &theme)) {
177 buttons = theme->buttons;
180 IRRECO_RETURN_PTR(buttons);
183 IrrecoStringTable *irreco_theme_manager_get_backgrounds(IrrecoThemeManager *self,
184 const gchar *theme_name)
186 IrrecoStringTable *backgrounds = NULL;
187 IrrecoTheme *theme = NULL;
188 IRRECO_ENTER
190 if (irreco_string_table_get(self->themes, theme_name,
191 (gpointer*)&theme)) {
192 backgrounds = theme->backgrounds;
195 IRRECO_RETURN_PTR(backgrounds);
198 void irreco_theme_manager_read_themes_from_dir(IrrecoThemeManager *self,
199 const gchar *dir)
201 IrrecoDirForeachData themes;
202 IRRECO_ENTER
204 IRRECO_DEBUG("THEME_DIR = %s\n", dir);
205 themes.directory = dir;
207 themes.filesuffix = "";
208 themes.user_data_1 = self;
210 irreco_dir_foreach(&themes, irreco_theme_manager_read_file_foreach);
212 irreco_string_table_sort_abc(self->themes);
213 IRRECO_RETURN
216 gboolean irreco_theme_manager_get_button_style(IrrecoThemeManager *self,
217 const gchar *style_name,
218 IrrecoThemeButton **style)
220 IrrecoThemeButton *button = NULL;
221 gboolean rvalue = FALSE;
222 IRRECO_ENTER
223 IRRECO_STRING_TABLE_FOREACH_DATA(self->themes, void *, theme) {
224 IrrecoStringTable* buttons = irreco_theme_get_buttons(theme);
226 if (irreco_string_table_get(buttons, style_name,
227 (gpointer *) &button)) {
228 *style = button;
229 rvalue = TRUE;
232 IRRECO_STRING_TABLE_FOREACH_END
233 IRRECO_RETURN_BOOL(rvalue);
236 gboolean irreco_theme_manager_does_deb_exist(IrrecoThemeManager *self)
238 gboolean rvalue = FALSE;
239 IRRECO_ENTER
240 IRRECO_STRING_TABLE_FOREACH_DATA(self->themes, IrrecoTheme *, theme) {
241 if (g_utf8_collate(theme->source->str, "deb") == 0 ||
242 g_utf8_collate(theme->source->str, "DEB") == 0) {
243 rvalue = TRUE;
246 IRRECO_STRING_TABLE_FOREACH_END
247 IRRECO_RETURN_BOOL(rvalue);
250 gboolean irreco_theme_manager_does_web_exist(IrrecoThemeManager *self)
252 gboolean rvalue = FALSE;
253 IRRECO_ENTER
254 IRRECO_STRING_TABLE_FOREACH_DATA(self->themes, IrrecoTheme *, theme) {
255 if (g_utf8_collate(theme->source->str, "web") == 0 ||
256 g_utf8_collate(theme->source->str, "WEB") == 0) {
257 rvalue = TRUE;
260 IRRECO_STRING_TABLE_FOREACH_END
261 IRRECO_RETURN_BOOL(rvalue);
264 gboolean irreco_theme_manager_does_user_exist(IrrecoThemeManager *self)
266 gboolean rvalue = FALSE;
267 IRRECO_ENTER
268 IRRECO_STRING_TABLE_FOREACH_DATA(self->themes, IrrecoTheme *, theme) {
269 if (g_utf8_collate(theme->source->str, "user") == 0 ||
270 g_utf8_collate(theme->source->str, "USER") == 0) {
271 rvalue = TRUE;
274 IRRECO_STRING_TABLE_FOREACH_END
275 IRRECO_RETURN_BOOL(rvalue);
278 gboolean irreco_theme_manager_remove_theme(IrrecoThemeManager *self,
279 const gchar *theme_name)
281 IrrecoTheme *rmtheme;
282 gchar *rm_cmd;
283 gboolean rvalue = FALSE;
284 IrrecoStringTable *table;
286 IRRECO_ENTER
288 if (irreco_string_table_get(self->themes, theme_name,
289 (gpointer *) &rmtheme)) {
290 /* Remove styles from layouts */
291 table = self->irreco_data->irreco_layout_array;
292 IRRECO_STRING_TABLE_FOREACH(table, key,
293 IrrecoButtonLayout *, layout)
294 IRRECO_PTR_ARRAY_FORWARDS(layout->button_array,
295 IrrecoButton *, button)
296 if (button->style != NULL &&
297 g_str_equal(button->style->theme_name->str,
298 theme_name)) {
299 button->type = IRRECO_BTYPE_GTK_BUTTON;
300 irreco_button_set_style(button, NULL);
301 irreco_button_create_widget(button);
304 IRRECO_PTR_ARRAY_FORWARDS_END
305 IRRECO_STRING_TABLE_FOREACH_END
307 /* Save layouts */
308 irreco_config_save_layouts(self->irreco_data);
310 /* Build remove command */
311 rm_cmd = g_strconcat("rm -r ", rmtheme->path->str, NULL);
313 /* Remove theme directory and its contents from device */
314 system(rm_cmd);
316 g_free(rm_cmd);
318 /* And from the string table */
319 irreco_string_table_remove(self->themes, theme_name);
321 rvalue = TRUE;
324 IRRECO_RETURN_BOOL(rvalue);
327 void irreco_theme_manager_update_theme_manager(IrrecoThemeManager *self)
329 IRRECO_ENTER
331 irreco_theme_manager_read_themes_from_dir(self, IRRECO_THEME_DIR);
332 irreco_theme_manager_read_themes_from_dir(self, "/media/mmc1/irreco");
333 irreco_theme_manager_read_themes_from_dir(self, "/media/mmc2/irreco");
335 /* Check if some theme is deleted */
336 IRRECO_STRING_TABLE_FOREACH(self->themes, key, IrrecoTheme *,
337 theme)
338 if(!irreco_is_dir(theme->path->str)) {
339 irreco_string_table_remove(self->themes, key);
341 IRRECO_STRING_TABLE_FOREACH_END
343 IRRECO_RETURN
346 /** @} */
348 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
349 /* Events and Callbacks */
350 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
352 /** @} */