60b78dbbb219de087e9b0a16081f4ef2d3f8bb94
[livewp] / applet / src / livewp-config.c
1 /* vim: set sw=4 ts=4 et: */
2 /*
3  * This file is part of Live Wallpaper (livewp)
4  * 
5  * Copyright (C) 2010 Vlad Vasiliev
6  * Copyright (C) 2010 Tanya Makova
7  *       for the code
8  * 
9  * This software is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  * 
14  * This software is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  * 
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this software; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23 */
24 /*******************************************************************************/
25 #include "livewp-config.h"
26 /*******************************************************************************/
27 gint 
28 current_active_view(void){
29     GConfClient *gconf_client = NULL;
30     gint result = -1;
31
32     gconf_client = gconf_client_get_default();
33     if (!gconf_client) {
34         fprintf(stderr, _("Failed to initialize GConf. Quitting.\n"));
35         return result;
36     }
37     result = gconf_client_get_int(gconf_client, "/apps/osso/hildon-desktop/views/current", NULL);
38
39     return result;
40 }
41
42 /*******************************************************************************/
43 #ifdef APPLICATION
44 void 
45 fill_priv(Animation_WallpaperPrivate *priv)
46 {
47     /* Reset data */
48     priv->hash_theme = NULL;
49     /* Load config */
50     read_config(priv);
51     /* Set function */
52     if (!strcmp(priv->theme, "Accel"))
53         priv->scene_func = (gpointer)&init_scene_Accel;
54     if (!strcmp(priv->theme, "Berlin"))
55         priv->scene_func = (gpointer)&init_scene_Berlin;
56     if (!strcmp(priv->theme, "Modern"))
57         priv->scene_func = (gpointer)&init_scene_Modern;
58     if (!strcmp(priv->theme, "Matrix"))
59         priv->scene_func = (gpointer)&init_scene_Matrix;
60     if (!strcmp(priv->theme, "Video"))
61         priv->scene_func = (gpointer)&init_scene_Video;
62     
63     priv->extheme_list = get_list_exthemes();
64     GSList *store = priv->extheme_list;
65     while (store){
66         if (!strcmp(priv->theme, g_hash_table_lookup(store->data, "name"))){
67             /* Default function for external themes init_scene_External */
68             if (priv->scene_func)
69                 priv->scene_func = (gpointer)&init_scene_External;
70             if (!strcmp(priv->theme, "Conky"))
71                 priv->scene_func = (gpointer)&init_scene_Conky;
72             if (!strcmp(priv->theme, "Flash"))
73                 priv->scene_func = (gpointer)&init_scene_Flash;
74             priv->hash_theme = store->data;
75             break;
76         }
77         store = g_slist_next(store);
78     }
79
80 }
81 #endif
82 /*******************************************************************************/
83 gint 
84 read_config(Animation_WallpaperPrivate *priv) {
85
86     GConfClient *gconf_client = NULL;
87     gchar *tmp = NULL;
88     GConfValue *value = NULL;
89     gint id = priv->view;
90     gchar * str = NULL;
91
92     gconf_client = gconf_client_get_default();
93     if (!gconf_client) {
94         fprintf(stderr, _("Failed to initialize GConf. Quitting.\n"));
95         return -1;
96     }
97     /* get Theme default Modern */
98     str = g_strdup_printf("%s%i", GCONF_KEY_THEME, id);
99     tmp = gconf_client_get_string(gconf_client,
100                                   str, NULL);
101     if (str){ 
102         g_free(str);
103         str = NULL;
104     }    
105     if (tmp){
106         priv->theme = tmp;
107     }else
108         priv->theme = g_strdup("Modern");
109     /* get Rich animation default TRUE */
110     str = g_strdup_printf("%s%i", GCONF_KEY_RANIMATION, id);
111     value = gconf_client_get(gconf_client, str, NULL);
112     if (str){ 
113         g_free(str);
114         str = NULL;
115     } 
116     if (value) {
117         priv->rich_animation = gconf_value_get_bool(value);
118         gconf_value_free(value);
119     } else
120         priv->rich_animation = TRUE;
121     /* get theme additional bool aparametr 1 default  TRUE */
122     str = g_strdup_printf("%s%i", GCONF_KEY_ADDITIONAL_BOOL_1, id);
123     value = gconf_client_get(gconf_client, str, NULL);
124     if (str){ 
125         g_free(str);
126         str = NULL;
127     } 
128     if (value) {
129         priv->theme_bool_parametr1 = gconf_value_get_bool(value);
130         gconf_value_free(value);
131     } else
132        priv->theme_bool_parametr1= TRUE;
133
134     /* get theme additional parameter 1  */
135     str = g_strdup_printf("%s%i", GCONF_KEY_ADDITIONAL_STRING_1 , id);
136     value = gconf_client_get(gconf_client, str, NULL);
137     if (str){ 
138         g_free(str);
139         str = NULL;
140     } 
141     if (value) {
142         priv->theme_string_parametr1 = g_strdup(gconf_value_get_string(value));
143         gconf_value_free(value);
144     } 
145     /* get parameter one theme in all view */
146     priv->one_in_all_view = get_one_in_all_views_from_config();
147     return 0;
148 }
149 /*******************************************************************************/
150 gboolean
151 get_one_in_all_views_from_config(void){
152     GConfClient *gconf_client;
153     GConfValue *value = NULL;
154     gboolean result;
155
156     gconf_client = gconf_client_get_default();
157     if (!gconf_client) {
158         fprintf(stderr, _("Failed to initialize GConf. Quitting.\n"));
159         return FALSE;
160     }
161     /* get parameter one theme in all view */
162     value = gconf_client_get(gconf_client, GCONF_KEY_ONE_IN_ALL_VIEW, NULL);
163     if (value) {
164         result = gconf_value_get_bool(value);
165         gconf_value_free(value);
166     } else
167         result = FALSE;
168     return result;
169 }
170 /*******************************************************************************/
171 void
172 save_one_in_all_views_to_config(gboolean one_in_all_views){
173     GConfClient *gconf_client;
174
175     gconf_client = gconf_client_get_default();
176     if (!gconf_client) {
177         fprintf(stderr, _("Failed to initialize GConf. Quitting.\n"));
178         return;
179     }
180
181     if (one_in_all_views)
182         gconf_client_set_bool(gconf_client,
183                               GCONF_KEY_ONE_IN_ALL_VIEW, TRUE, NULL);
184     else
185         gconf_client_set_bool(gconf_client,
186                               GCONF_KEY_ONE_IN_ALL_VIEW, FALSE, NULL);
187
188 }
189 /*******************************************************************************/
190 void
191 save_config(Animation_WallpaperPrivate *priv) {
192
193     GConfClient *gconf_client;
194     gchar * str = NULL;
195     gint id = priv->view;
196
197     gconf_client = gconf_client_get_default();
198     if (!gconf_client) {
199         fprintf(stderr, _("Failed to initialize GConf. Quitting.\n"));
200         return;
201     }
202     
203     if (priv->theme){
204         str = g_strdup_printf("%s%i", GCONF_KEY_THEME, id);
205         gconf_client_set_string(gconf_client,
206                   str,
207                   priv->theme, NULL);
208         if (str){
209             g_free(str);
210             str = NULL;
211         }
212     }
213
214     str = g_strdup_printf("%s%i", GCONF_KEY_RANIMATION, id);
215     if (priv->rich_animation)
216         gconf_client_set_bool(gconf_client,
217                               str, TRUE, NULL);
218     else
219         gconf_client_set_bool(gconf_client,
220                               str, FALSE, NULL);
221     if (str){
222         g_free(str);
223         str = NULL;
224     }
225     str = g_strdup_printf("%s%i", GCONF_KEY_ADDITIONAL_BOOL_1, id);
226     if (priv->theme_bool_parametr1)
227         gconf_client_set_bool(gconf_client,
228                               str, TRUE, NULL);
229     else
230         gconf_client_set_bool(gconf_client,
231                               str, FALSE, NULL);
232     if (str){
233         g_free(str);
234         str = NULL;
235     }
236
237     str = g_strdup_printf("%s%i",GCONF_KEY_ADDITIONAL_STRING_1, id);
238     if (priv->theme_string_parametr1){
239         gconf_client_set_string(gconf_client,
240                   str,
241                   priv->theme_string_parametr1, NULL);
242     }else
243         gconf_client_unset(gconf_client, str, NULL);
244     if (str){
245             g_free(str);
246             str = NULL;
247     }
248
249 }
250 /*******************************************************************************/
251 gint
252 get_count_themes_from_config(gchar *theme_name){
253     GConfClient *gconf_client;
254     gint i, count;
255     gchar *str, *value;
256
257     gconf_client = gconf_client_get_default();
258     if (!gconf_client) {
259         fprintf(stderr, _("Failed to initialize GConf. Quitting.\n"));
260         return FALSE;
261     }
262     /* get count such themes */
263     count = 0;
264     for (i=1; i<=9; i++){
265         str = g_strdup_printf("%s%i", GCONF_KEY_THEME, i);
266         value = gconf_client_get_string(gconf_client, str, NULL);
267         if (str){ 
268             g_free(str);
269             str = NULL;
270         }    
271         if (value && !strcmp(value, theme_name)){
272             count++;
273         }
274
275     }
276     //fprintf(stderr, "get count_theme from config = %d\n", count);
277     return count;
278 }
279