Clean code
[irreco.git] / irreco / src / core / irreco_theme_manager.c
blob6e008eacbac2ab48e21f6a2684d195aaf7ff93c6
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2008 Joni Kokko (t5kojo01@students.oamk.fi)
4 * Sami Mäki (kasmra@xob.kapsi.fi)
5 * Pekka Gehör (pegu6@msn.com)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "irreco_theme_manager.h"
24 /**
25 * @addtogroup IrrecoThemeManager
26 * @ingroup Irreco
28 * Contains information of themes.
30 * @{
33 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
34 /* Prototypes */
35 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
37 void irreco_theme_manager_read_file_foreach(IrrecoDirForeachData * dir_data);
39 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
40 /* Construction & Destruction */
41 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
43 /**
44 * @name Construction & Destruction
45 * @{
48 IrrecoThemeManager *irreco_theme_manager_new(IrrecoData * irreco_data)
50 IrrecoThemeManager *self;
51 IRRECO_ENTER
53 self = g_slice_new0(IrrecoThemeManager);
55 self->irreco_data = irreco_data;
57 self->themes = irreco_string_table_new(
58 (GDestroyNotify)irreco_theme_free, NULL);
60 irreco_theme_manager_read_themes_from_dir(self, IRRECO_THEME_DIR);
61 irreco_theme_manager_read_themes_from_dir(self, "/media/mmc1/irreco");
62 irreco_theme_manager_read_themes_from_dir(self, "/media/mmc2/irreco");
64 IRRECO_RETURN_PTR(self);
67 void irreco_theme_manager_free(IrrecoThemeManager *self)
69 IRRECO_ENTER
71 g_assert(self != NULL);
73 irreco_string_table_free(self->themes);
74 self->themes = NULL;
76 g_slice_free(IrrecoThemeManager, self);
77 self = NULL;
79 IRRECO_RETURN
82 /** @} */
84 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
85 /* Private Functions */
86 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
88 /**
89 * @name Private Functions
90 * @{
93 void irreco_theme_manager_read_file_foreach(IrrecoDirForeachData * dir_data)
95 IrrecoTheme *theme = NULL;
96 IrrecoThemeManager *self = (IrrecoThemeManager*) dir_data->user_data_1;
97 IRRECO_ENTER
99 theme = irreco_theme_new_from_dir(dir_data->filepath);
101 if (irreco_string_table_exists(self->themes, theme->name->str)) {
102 IRRECO_ERROR("Error: Theme %s has already been read. "
103 "You cannot have two themes with the same name.\n",
104 theme->name->str);
105 irreco_theme_free(theme);
106 } else {
107 irreco_string_table_add(self->themes, theme->name->str, theme);
110 IRRECO_RETURN
113 /** @} */
115 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
116 /* Public Functions */
117 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
120 * @name Public Functions
121 * @{
124 void irreco_theme_manager_print(IrrecoThemeManager *self)
126 IRRECO_ENTER
128 #if 1
129 irreco_string_table_print(self->themes);
130 #endif
132 #if 0
133 IRRECO_STRING_TABLE_FOREACH_DATA(self->themes, void *, theme)
134 IRRECO_PRINTF("Theme: %s \n", theme->name->str);
135 irreco_theme_print(theme);
136 IRRECO_STRING_TABLE_FOREACH_END
137 #endif
139 IRRECO_RETURN
142 IrrecoStringTable *irreco_theme_manager_get_themes(IrrecoThemeManager *self)
144 IRRECO_ENTER
146 IRRECO_RETURN_PTR(self->themes);
149 IrrecoStringTable *irreco_theme_manager_get_buttons(IrrecoThemeManager *self,
150 const gchar *theme_name)
152 IrrecoStringTable *buttons = NULL;
153 IrrecoTheme *theme = NULL;
154 IRRECO_ENTER
156 IRRECO_PRINTF("theme_name: %s\n", theme_name);
157 if (irreco_string_table_get(self->themes, theme_name,
158 (gpointer*) &theme)) {
159 buttons = theme->buttons;
162 IRRECO_RETURN_PTR(buttons);
165 IrrecoStringTable *irreco_theme_manager_get_backgrounds(IrrecoThemeManager *self,
166 const gchar *theme_name)
168 IrrecoStringTable *backgrounds = NULL;
169 IrrecoTheme *theme = NULL;
170 IRRECO_ENTER
172 if (irreco_string_table_get(self->themes, theme_name,
173 (gpointer*)&theme)) {
174 backgrounds = theme->backgrounds;
177 IRRECO_RETURN_PTR(backgrounds);
180 void irreco_theme_manager_read_themes_from_dir(IrrecoThemeManager *self,
181 const gchar *dir)
183 IrrecoDirForeachData themes;
184 IRRECO_ENTER
186 IRRECO_DEBUG("THEME_DIR = %s\n", dir);
187 themes.directory = dir;
189 themes.filesuffix = "";
190 themes.user_data_1 = self;
192 irreco_dir_foreach(&themes, irreco_theme_manager_read_file_foreach);
194 irreco_string_table_sort_abc(self->themes);
195 IRRECO_RETURN
198 gboolean irreco_theme_manager_get_button_style(IrrecoThemeManager *self,
199 const gchar *style_name,
200 IrrecoThemeButton **style)
202 IrrecoThemeButton *button = NULL;
203 gboolean rvalue = FALSE;
204 IRRECO_ENTER
205 IRRECO_STRING_TABLE_FOREACH_DATA(self->themes, void *, theme) {
206 IrrecoStringTable* buttons = irreco_theme_get_buttons(theme);
208 if (irreco_string_table_get(buttons, style_name,
209 (gpointer *) &button)) {
210 *style = button;
211 rvalue = TRUE;
214 IRRECO_STRING_TABLE_FOREACH_END
215 IRRECO_RETURN_BOOL(rvalue);
218 gboolean irreco_theme_manager_does_deb_exist(IrrecoThemeManager *self)
220 gboolean rvalue = FALSE;
221 IRRECO_ENTER
222 IRRECO_STRING_TABLE_FOREACH_DATA(self->themes, IrrecoTheme *, theme) {
223 if (g_utf8_collate(theme->source->str, "deb") == 0 ||
224 g_utf8_collate(theme->source->str, "DEB") == 0) {
225 rvalue = TRUE;
228 IRRECO_STRING_TABLE_FOREACH_END
229 IRRECO_RETURN_BOOL(rvalue);
232 gboolean irreco_theme_manager_does_web_exist(IrrecoThemeManager *self)
234 gboolean rvalue = FALSE;
235 IRRECO_ENTER
236 IRRECO_STRING_TABLE_FOREACH_DATA(self->themes, IrrecoTheme *, theme) {
237 if (g_utf8_collate(theme->source->str, "web") == 0 ||
238 g_utf8_collate(theme->source->str, "WEB") == 0) {
239 rvalue = TRUE;
242 IRRECO_STRING_TABLE_FOREACH_END
243 IRRECO_RETURN_BOOL(rvalue);
246 gboolean irreco_theme_manager_does_user_exist(IrrecoThemeManager *self)
248 gboolean rvalue = FALSE;
249 IRRECO_ENTER
250 IRRECO_STRING_TABLE_FOREACH_DATA(self->themes, IrrecoTheme *, theme) {
251 if (g_utf8_collate(theme->source->str, "user") == 0 ||
252 g_utf8_collate(theme->source->str, "USER") == 0) {
253 rvalue = TRUE;
256 IRRECO_STRING_TABLE_FOREACH_END
257 IRRECO_RETURN_BOOL(rvalue);
260 gboolean irreco_theme_manager_remove_theme(IrrecoThemeManager *self,
261 const gchar *theme_name)
263 IrrecoTheme *rmtheme;
264 gchar *rm_cmd;
265 gboolean rvalue = FALSE;
266 IrrecoStringTable *table;
268 IRRECO_ENTER
270 if (irreco_string_table_get(self->themes, theme_name,
271 (gpointer *) &rmtheme)) {
272 /* Remove styles from layouts */
273 table = self->irreco_data->irreco_layout_array;
274 IRRECO_STRING_TABLE_FOREACH(table, key,
275 IrrecoButtonLayout *, layout)
276 IRRECO_PTR_ARRAY_FORWARDS(layout->button_array,
277 IrrecoButton *, button)
278 if (button->style != NULL &&
279 g_str_equal(button->style->theme_name->str,
280 theme_name)) {
281 button->type = IRRECO_BTYPE_GTK_BUTTON;
282 irreco_button_set_style(button, NULL);
283 irreco_button_create_widget(button);
286 IRRECO_PTR_ARRAY_FORWARDS_END
287 IRRECO_STRING_TABLE_FOREACH_END
289 /* Save layouts */
290 irreco_config_save_layouts(self->irreco_data);
292 /* Build remove command */
293 rm_cmd = g_strconcat("rm -r ", rmtheme->path->str, NULL);
295 /* Remove theme directory and its contents from device */
296 system(rm_cmd);
298 g_free(rm_cmd);
300 /* And from the string table */
301 irreco_string_table_remove(self->themes, theme_name);
303 rvalue = TRUE;
306 IRRECO_RETURN_BOOL(rvalue);
309 void irreco_theme_manager_update_theme_manager(IrrecoThemeManager *self)
311 IRRECO_ENTER
313 irreco_theme_manager_read_themes_from_dir(self, IRRECO_THEME_DIR);
314 irreco_theme_manager_read_themes_from_dir(self, "/media/mmc1/irreco");
315 irreco_theme_manager_read_themes_from_dir(self, "/media/mmc2/irreco");
317 /* Check if some theme is deleted */
318 IRRECO_STRING_TABLE_FOREACH(self->themes, key, IrrecoTheme *,
319 theme)
320 if(!irreco_is_dir(theme->path->str)) {
321 irreco_string_table_remove(self->themes, key);
323 IRRECO_STRING_TABLE_FOREACH_END
325 IRRECO_RETURN
328 /** @} */
330 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
331 /* Events and Callbacks */
332 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
334 /** @} */