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_button.h"
24 * @addtogroup IrrecoThemeButton
27 * Contains information of button.
34 * Source file of @ref IrrecoThemeButton.
37 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
39 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
41 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
42 /* Construction & Destruction */
43 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
46 * @name Construction & Destruction
50 IrrecoThemeButton
*irreco_theme_button_new(const char *theme_name
)
52 IrrecoThemeButton
*self
;
55 self
= g_slice_new0(IrrecoThemeButton
);
57 self
->theme_name
= g_string_new(theme_name
);
58 self
->style_name
= g_string_new("");
59 self
->name
= g_string_new("");
60 self
->image_up
= g_string_new("");
61 self
->image_down
= g_string_new("");
62 self
->text_format_up
= g_string_new("");
63 self
->text_format_down
= g_string_new("");
64 self
->text_padding
= 5;
66 /* Default to center alignment. */
67 self
->text_h_align
= 0.5;
68 self
->text_v_align
= 0.5;
70 IRRECO_RETURN_PTR(self
);
73 void irreco_theme_button_free(IrrecoThemeButton
*self
)
77 g_assert(self
!= NULL
);
79 g_string_free(self
->theme_name
, TRUE
);
80 self
->theme_name
= NULL
;
82 g_string_free(self
->style_name
, TRUE
);
83 self
->style_name
= NULL
;
85 g_string_free(self
->name
, TRUE
);
88 g_string_free(self
->image_up
, TRUE
);
89 self
->image_up
= NULL
;
91 g_string_free(self
->image_down
, TRUE
);
92 self
->image_down
= NULL
;
94 g_string_free(self
->text_format_up
, TRUE
);
95 self
->text_format_up
= NULL
;
97 g_string_free(self
->text_format_down
, TRUE
);
98 self
->text_format_down
= NULL
;
100 g_slice_free(IrrecoThemeButton
, self
);
108 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
109 /* Private Functions */
110 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
112 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
113 /* Public Functions */
114 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
117 * @name Public Functions
121 void irreco_theme_button_set(IrrecoThemeButton
*self
,
122 const char *style_name
,
125 const char *image_up
,
126 const char *image_down
,
127 const char *text_format_up
,
128 const char *text_format_down
,
135 if (style_name
!= NULL
) {
136 g_string_printf(self
->style_name
, "%s", style_name
);
139 g_string_printf(self
->name
, "%s", name
);
141 self
->allow_text
= allow_text
;
142 if (image_up
!= NULL
) {
143 g_string_printf(self
->image_up
, "%s", image_up
);
145 if (image_down
!= NULL
) {
146 g_string_printf(self
->image_down
, "%s", image_down
);
148 g_string_printf(self
->image_down
, "%s", image_up
);
150 if (text_format_up
!= NULL
) {
151 g_string_printf(self
->text_format_up
, "%s", text_format_up
);
153 if (text_format_down
!= NULL
) {
154 g_string_printf(self
->text_format_down
, "%s", text_format_down
);
157 self
->text_padding
= text_padding
;
159 /* Set Horisontal aligment. */
160 if (self
->text_h_align
< 0) {
161 self
->text_h_align
= 0;
162 } else if (self
->text_h_align
> 1) {
163 self
->text_h_align
= 1;
165 self
->text_h_align
= text_h_align
;
168 /* Set vertical alignment. */
169 if (self
->text_v_align
< 0) {
170 self
->text_v_align
= 0;
171 } else if (self
->text_v_align
> 1) {
172 self
->text_v_align
= 1;
174 self
->text_v_align
= text_v_align
;
179 void irreco_theme_button_set_from_button(IrrecoThemeButton
*self
,
180 IrrecoThemeButton
*button
)
183 irreco_theme_button_set(self
,
184 button
->style_name
->str
,
187 button
->image_up
->str
,
188 button
->image_down
->str
,
189 button
->text_format_up
->str
,
190 button
->text_format_down
->str
,
191 button
->text_padding
,
192 button
->text_h_align
,
193 button
->text_v_align
);
196 void irreco_theme_button_print(IrrecoThemeButton
*self
)
200 IRRECO_DEBUG("--------------------------------------------\n");
201 IRRECO_DEBUG("theme_name: %s\n", self
->theme_name
->str
);
202 IRRECO_DEBUG("style_name: %s\n", self
->style_name
->str
);
203 IRRECO_DEBUG("button_name: %s\n", self
->name
->str
);
204 IRRECO_DEBUG("allow_text: %d\n", self
->allow_text
);
205 IRRECO_DEBUG("image_up: %s\n",self
->image_up
->str
);
206 IRRECO_DEBUG("image_down: %s\n", self
->image_down
->str
);
207 IRRECO_DEBUG("text_format_up: %s\n", self
->text_format_up
->str
);
208 IRRECO_DEBUG("text_format_down: %s\n", self
->text_format_down
->str
);
209 IRRECO_DEBUG("text_padding: %d\n", self
->text_padding
);
210 IRRECO_DEBUG("--------------------------------------------\n");
215 IrrecoThemeButton
*irreco_theme_button_copy(IrrecoThemeButton
*self
)
217 IrrecoThemeButton
*new = NULL
;
220 new = irreco_theme_button_new(self
->theme_name
->str
);
221 irreco_theme_button_set(new, self
->style_name
->str
, self
->name
->str
,
222 self
->allow_text
, self
->image_up
->str
,
223 self
->image_down
->str
, self
->text_format_up
->str
,
224 self
->text_format_down
->str
, self
->text_padding
,
225 self
->text_h_align
, self
->text_v_align
);
227 IRRECO_RETURN_PTR(new);
231 * IrrecoThemeBg new from dir
235 irreco_theme_button_new_from_dir(const gchar
*dir
, const gchar
*theme_name
)
237 IrrecoThemeButton
*self
= NULL
;
241 self
= irreco_theme_button_new(theme_name
);
242 irreco_theme_button_read(self
, dir
);
243 IRRECO_RETURN_PTR(self
);
246 void irreco_theme_button_read(IrrecoThemeButton
*self
, const gchar
*dir
)
248 IrrecoKeyFile
*keyfile
;
249 GString
*conf
= NULL
;
254 gchar
*text_format_up
= NULL
;
255 gchar
*text_format_down
= NULL
;
256 gint text_padding
= 5;
257 gfloat text_h_align
= 0.5;
258 gfloat text_v_align
= 0.5;
259 GString
*style_name
= g_string_new(self
->theme_name
->str
);
262 conf
= g_string_new(dir
);
263 g_string_append_printf(conf
, "/button.conf");
264 keyfile
= irreco_keyfile_create(dir
,
267 if (keyfile
== NULL
) goto end
;
269 /* Required fields. */
270 if (!irreco_keyfile_get_str(keyfile
, "name", &name
) ||
271 !irreco_keyfile_get_path(keyfile
, "up", &up
)) {
272 IRRECO_PRINTF("Could not read style from group \"%s\"\n",
277 /* Optional fields. */
278 irreco_keyfile_get_path(keyfile
, "down", &down
);
279 irreco_keyfile_get_bool(keyfile
, "allow-text", &allow_text
);
280 irreco_keyfile_get_str(keyfile
, "text-format-up", &text_format_up
);
281 irreco_keyfile_get_str(keyfile
, "text-format-down", &text_format_down
);
282 irreco_keyfile_get_int(keyfile
, "text-padding", &text_padding
);
283 irreco_keyfile_get_float(keyfile
, "text-h-align", &text_h_align
);
284 irreco_keyfile_get_float(keyfile
, "text-v-align", &text_v_align
);
286 g_string_append_printf(style_name
,"/%s", name
);
288 irreco_theme_button_set(self
, style_name
->str
,
291 text_format_up
, text_format_down
,
296 irreco_theme_button_print(self
);
299 if(keyfile
!= NULL
) irreco_keyfile_destroy(keyfile
);
300 if(name
!= NULL
) g_free(name
);
301 if(name
!= NULL
) g_free(up
);
302 if(name
!= NULL
) g_free(down
);
303 if(name
!= NULL
) g_free(text_format_up
);
304 if(name
!= NULL
) g_free(text_format_down
);
305 g_string_free(style_name
, TRUE
);
311 * Save button to theme folder
315 irreco_theme_button_save(IrrecoThemeButton
*self
, const gchar
*path
)
317 gboolean rvalue
= FALSE
;
318 GString
*keyfile_path
;
319 GString
*unpressed_image_path
;
320 GString
*pressed_image_path
;
324 gchar
*cp_cmd_unpressed
;
325 gchar
*cp_cmd_pressed
;
326 gchar
*type_unpressed
;
328 GString
*unpressed_image_name
;
329 GString
*pressed_image_name
;
332 keyfile
= g_key_file_new();
333 unpressed_image_name
= g_string_new(NULL
);
334 pressed_image_name
= g_string_new(NULL
);
335 unpressed_image_path
= g_string_new(path
);
336 pressed_image_path
= g_string_new(path
);
337 keyfile_path
= g_string_new(path
);
338 folder
= g_string_new(self
->name
->str
);
339 g_string_ascii_down(folder
);
340 folder_name
= g_strdup(folder
->str
);
341 g_strdelimit(folder_name
, " ", '_');
342 g_strdelimit(folder_name
, ",-|> <.", 'a');
344 g_string_printf(folder
, "%s", folder_name
);
347 type_unpressed
= g_strrstr(self
->image_up
->str
, ".");
348 type_pressed
= g_strrstr(self
->image_down
->str
, ".");
350 g_string_printf(unpressed_image_name
, "%s%s", "unpressed",
352 g_string_printf(pressed_image_name
, "%s%s", "pressed", type_pressed
);
355 g_string_append_printf(unpressed_image_path
, "/%s", folder_name
);
356 g_string_append_printf(pressed_image_path
, "/%s", folder_name
);
358 g_string_append_printf(keyfile_path
, "/%s", folder_name
);
360 g_mkdir(unpressed_image_path
->str
, 0777);
362 g_string_append_printf(unpressed_image_path
, "/%s%s", "unpressed",
364 g_string_append_printf(pressed_image_path
, "/%s%s", "pressed",
367 /* Copy images to button folder */
369 cp_cmd_unpressed
= g_strconcat("cp -f ", self
->image_up
->str
, " ",
370 unpressed_image_path
->str
, NULL
);
371 cp_cmd_pressed
= g_strconcat("cp -f ", self
->image_down
->str
, " ",
372 pressed_image_path
->str
, NULL
);
373 system(cp_cmd_unpressed
);
374 system(cp_cmd_pressed
);
375 g_free(cp_cmd_unpressed
);
376 g_free(cp_cmd_pressed
);
378 /* Create keyfile and save it to folder*/
380 irreco_gkeyfile_set_string(keyfile
, "theme-button",
381 "up", unpressed_image_name
->str
);
383 irreco_gkeyfile_set_string(keyfile
, "theme-button",
384 "down", pressed_image_name
->str
);
386 irreco_gkeyfile_set_string(keyfile
, "theme-button" , "name",
389 if (self
->allow_text
) {
390 irreco_gkeyfile_set_string(keyfile
, "theme-button",
391 "allow-text", "true");
393 irreco_gkeyfile_set_string(keyfile
, "theme-button",
394 "allow-text", "false");
397 if (self
->text_format_up
!= NULL
&& strlen(self
->text_format_up
->str
) > 0) {
398 irreco_gkeyfile_set_string(keyfile
, "theme-button",
399 "text-format-up", self
->text_format_up
->str
);
402 if (self
->text_format_down
!= NULL
&& strlen(self
->text_format_down
->str
) > 0) {
403 irreco_gkeyfile_set_string(keyfile
, "theme-button",
404 "text-format-down", self
->text_format_down
->str
);
407 irreco_gkeyfile_set_glong(keyfile
, "theme-button",
408 "text-padding", (glong
) self
->text_padding
);
410 irreco_gkeyfile_set_gfloat(keyfile
,
413 (gfloat
) self
->text_h_align
);
415 irreco_gkeyfile_set_gfloat(keyfile
,
418 (gfloat
) self
->text_v_align
);
420 g_string_append_printf(keyfile_path
, "/button.conf");
421 irreco_write_keyfile(keyfile
, keyfile_path
->str
);
424 g_key_file_free(keyfile
);
425 g_string_free(unpressed_image_name
, TRUE
);
426 g_string_free(pressed_image_name
, TRUE
);
427 g_string_free(keyfile_path
, TRUE
);
428 g_string_free(unpressed_image_path
, TRUE
);
429 g_string_free(pressed_image_path
, TRUE
);
430 IRRECO_RETURN_BOOL(rvalue
);
435 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
436 /* Events and Callbacks */
437 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/