02fc71bdc301b2d76fbf780c8035fa6fa9b02019
[apps/core/preloaded/ug-camera-efl.git] / src / cam_config.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *        http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 /*
24 #define DEBUG_CONFIG
25 #include "cam.h"
26 #include "cam_error.h"
27 */
28 #include "cam_debug.h"
29 #include "cam_config.h"
30 static gchar **g_group_name = NULL;     /*  config information group name for save */
31 static GKeyFile *g_key_file = NULL;
32 static gboolean disable_set_mode = FALSE;       /* if disable_set_mode is true unable to set setmode use for scene mode and ncr case ... */
33 gboolean cam_config_set_group_name(CamConfigType config_type,
34                                    const gchar *set_group_name)
35 {
36         g_return_val_if_fail(set_group_name, FALSE);
37         g_return_val_if_fail(g_group_name, FALSE);/*fix crash*/
38         if (g_group_name[config_type]) {
39                 g_free(g_group_name[config_type]);
40                 g_group_name[config_type] = NULL;
41         }
42         g_group_name[config_type] = g_strdup(set_group_name);
43         return TRUE;
44 }
45
46 gboolean cam_config_init(GError **error)
47 {
48         GError *err = NULL;
49         debug_fenter(LOG_CONFIG);
50         if (!g_group_name) {
51                 g_group_name = g_new0(gchar *, CAM_CONFIG_MAX);
52                 cam_config_set_group_name(CAM_CONFIG_TYPE_COMMON, "common");
53                 cam_config_set_group_name(CAM_CONFIG_TYPE_SHORTCUTS, "shortcuts");
54         }
55         if (g_key_file) {
56                 warn_msg(LOG_UI, "already initialized.");
57                 return TRUE;
58         }
59         g_key_file = g_key_file_new();
60         if (!g_key_file_load_from_file
61             (g_key_file, CONFIG_PATH, G_KEY_FILE_NONE, &err)) {
62                 if (err != NULL) {
63                         warn_msg(LOG_UI, "config file not exists. %s",
64                                  err->message);
65                         g_error_free(err);
66                         err = NULL;
67                 }
68         }
69         if (err != NULL) {
70                 g_error_free(err);
71                 err = NULL;
72         }
73         debug_fleave(LOG_UI);
74         return TRUE;
75 }
76
77 void cam_config_finalize(void)
78 {
79         debug_fenter(LOG_UI);
80         cam_config_save();
81         if (g_group_name) {
82                 int i;
83                 for (i = 0; i < CAM_CONFIG_MAX; i++) {
84                         if (g_group_name[i]) {
85                                 g_free(g_group_name[i]);
86                                 g_group_name[i] = NULL;
87                         }
88                 }
89                 g_free(g_group_name);
90                 g_group_name = NULL;
91         }
92         if (g_key_file) {
93                 g_key_file_free(g_key_file);
94                 g_key_file = NULL;
95         }
96         debug_fleave(LOG_UI);
97 }
98
99 void cam_config_save(void)
100 {
101         debug_fenter(LOG_UI);
102         if (g_key_file != NULL) {
103                 GError *err = NULL;
104                 gchar *buf = NULL;
105                 gsize len = 0;
106                 buf = g_key_file_to_data(g_key_file, &len, &err);
107                 if (buf) {
108                         if (err) {
109                                 debug_msg(LOG_UI, "%s", err->message);
110                                 g_error_free(err);
111                                 err = NULL;
112                         } else {
113 #if 1
114                                 FILE *fp = fopen(CONFIG_PATH, "w");
115                                 if (fp != NULL) {
116                                         if (fwrite
117                                             ((const void *)buf, len, 1,
118                                              fp) != 1) {
119                                              /**fwrite return count(unsigned int) if write correct.
120                                                 the-return-value is always >=0*/
121                                                 critical_msg(LOG_CONFIG,
122                                                              "fwrite failed");
123                                         } else {
124                                                 cam_debug(LOG_CONFIG,
125                                                           "save success");
126                                         }
127                                         fclose(fp);
128                                 } else {
129                                         critical_msg(LOG_CONFIG,
130                                                      "fopen failed");
131                                 }
132
133 #else
134                                 g_file_set_contents(CONFIG_PATH,
135                                                     (const gchar *)buf, len,
136                                                     &err);
137                                 if (err) {
138                                         critical_msg(LOG_CONFIG, "%s",
139                                                      err->message);
140                                         g_error_free(err);
141                                         err = NULL;
142                                 }
143 #endif
144                         }
145                         g_free(buf);
146                 }
147                 if (err != NULL) {
148                         g_error_free(err);
149                         err = NULL;
150                 }
151         }
152 }
153
154 void cam_config_set_control(gboolean enable)
155 {
156         cam_debug(LOG_UI, "%d ", enable);
157         disable_set_mode = !enable;
158 }
159
160 void cam_config_set_int(const gchar *key, int nval)
161 {
162         g_return_if_fail(g_key_file);
163         g_return_if_fail(g_group_name);
164         g_return_if_fail(key);
165         if (disable_set_mode) {
166                 /* cam_warning(LOG_UI," disable_set_mode is true "); */
167                 return;
168         }
169         cam_config_set_int_by_type(CAM_CONFIG_TYPE_PREVIEW, key, nval);
170         return;
171 }
172
173 void cam_config_set_string(const gchar *key, const gchar *strval)
174 {
175         g_return_if_fail(g_key_file);
176         g_return_if_fail(g_group_name);
177         g_return_if_fail(key);
178         if (disable_set_mode) {
179                 cam_warning(LOG_UI, " disable_set_mode is true ");
180                 return;
181         }
182         cam_config_set_string_by_type(CAM_CONFIG_TYPE_PREVIEW, key, strval);
183         return;
184 }
185
186 void cam_config_set_boolean(const gchar *key, gboolean bval)
187 {
188         g_return_if_fail(g_key_file);
189         g_return_if_fail(g_group_name);
190         g_return_if_fail(key);
191         if (disable_set_mode) {
192                 cam_warning(LOG_UI, " disable_set_mode is true ");
193                 return;
194         }
195 #ifdef DEBUG_CONFIG
196         debug_msg(LOG_UI, "%s", bval ? "TRUE" : "FALSE");
197
198 #endif                          /* \r */
199         cam_config_set_boolean_by_type(CAM_CONFIG_TYPE_PREVIEW, key, bval);
200         return;
201 }
202
203 int cam_config_get_int(const gchar *key, int default_value)
204 {
205         g_return_val_if_fail(g_key_file, -1);
206         g_return_val_if_fail(g_group_name, -1);
207         g_return_val_if_fail(key, -1);
208         return cam_config_get_int_by_type(CAM_CONFIG_TYPE_PREVIEW, key,
209                                           default_value);
210 }
211
212 gchar *cam_config_get_string(const gchar *key, const gchar *default_value)
213 {
214         g_return_val_if_fail(g_key_file, NULL);
215         g_return_val_if_fail(g_group_name, NULL);
216         g_return_val_if_fail(key, NULL);
217         return cam_config_get_string_by_type(CAM_CONFIG_TYPE_PREVIEW, key,
218                                              default_value);
219 }
220
221 gboolean cam_config_get_boolean(const gchar *key, gboolean default_value)
222 {
223         g_return_val_if_fail(g_key_file, FALSE);
224         g_return_val_if_fail(g_group_name, FALSE);
225         g_return_val_if_fail(key, FALSE);
226         return cam_config_get_boolean_by_type(CAM_CONFIG_TYPE_PREVIEW, key,
227                                               default_value);
228 }
229
230 void cam_config_set_int_by_type(CamConfigType config_type, const gchar *key,
231                                 int nval)
232 {
233         g_return_if_fail(g_key_file);
234         g_return_if_fail(g_group_name);
235         g_return_if_fail(g_group_name[config_type]);
236         g_return_if_fail(key);
237         if (disable_set_mode) {
238                 /* cam_warning(LOG_UI," disable_set_mode is true "); */
239                 return;
240         }
241 #ifdef DEBUG_CONFIG
242         debug_msg(LOG_UI, "%s,%s,%d", g_group_name[config_type], key, nval);
243
244 #endif                          /* \r */
245         g_key_file_set_integer(g_key_file, g_group_name[config_type], key,
246                                nval);
247 }
248
249 void cam_config_set_string_by_type(CamConfigType config_type, const gchar *key,
250                                    const gchar *strval)
251 {
252         g_return_if_fail(g_key_file);
253         g_return_if_fail(g_group_name);
254         g_return_if_fail(g_group_name[config_type]);
255         g_return_if_fail(key);
256         if (disable_set_mode) {
257                 cam_warning(LOG_UI, " disable_set_mode is true ");
258                 return;
259         }
260 #ifdef DEBUG_CONFIG
261         debug_msg(LOG_UI, "%s", strval);
262
263 #endif                          /* \r */
264         g_key_file_set_string(g_key_file, g_group_name[config_type], key,
265                               strval);
266 }
267
268 void cam_config_set_boolean_by_type(CamConfigType config_type, const gchar *key,
269                                     gboolean bval)
270 {
271         g_return_if_fail(g_key_file);
272         g_return_if_fail(g_group_name);
273         g_return_if_fail(g_group_name[config_type]);
274         g_return_if_fail(key);
275         if (disable_set_mode) {
276                 cam_warning(LOG_UI, " disable_set_mode is true ");
277                 return;
278         }
279 #ifdef DEBUG_CONFIG
280         debug_msg(LOG_UI, "%s", bval ? "TRUE" : "FALSE");
281
282 #endif                          /* \r */
283         g_key_file_set_boolean(g_key_file, g_group_name[config_type], key,
284                                bval);
285 }
286
287 int cam_config_get_int_by_type(CamConfigType config_type, const gchar *key,
288                                int default_value)
289 {
290         g_return_val_if_fail(g_key_file, -1);
291         g_return_val_if_fail(g_group_name, -1);
292         g_return_val_if_fail(g_group_name[config_type], -1);
293         g_return_val_if_fail(key, -1);
294         GError *error = NULL;
295         gint nval =
296             g_key_file_get_integer(g_key_file, g_group_name[config_type], key,
297                                    &error);
298         if (error) {
299
300 /*
301                 if (error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND)
302                         critical_msg("error:%s", error->message);
303 */
304                 cam_config_set_int_by_type(config_type, key, default_value);
305                 g_error_free(error);
306                 error = NULL;
307                 DEBUG_TRACE("-------- key[%s], value[%d]", key, default_value);
308                 return default_value;
309         } else {
310                 DEBUG_TRACE("-------- key[%s], value[%d]", key, nval);
311                 return nval;
312         }
313 }
314
315 gchar *cam_config_get_string_by_type(CamConfigType config_type,
316                                      const gchar *key,
317                                      const gchar *default_value)
318 {
319         g_return_val_if_fail(g_key_file, NULL);
320         g_return_val_if_fail(g_group_name, NULL);
321         g_return_val_if_fail(g_group_name[config_type], NULL);
322         g_return_val_if_fail(key, NULL);
323         GError *error = NULL;
324         const gchar *strval =
325             g_key_file_get_string(g_key_file, g_group_name[config_type], key,
326                                   &error);
327         if (error) {
328                 if (error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND)
329                         critical_msg(LOG_CONFIG, "error:%s", error->message);
330                 cam_config_set_string_by_type(config_type, key, default_value);
331                 g_error_free(error);
332                 error = NULL;
333                 return default_value ? g_strdup(default_value) : NULL;
334         } else {
335                 return strval ? g_strdup(strval) : NULL;
336         }
337 }
338
339 gboolean cam_config_get_boolean_by_type(CamConfigType config_type,
340                                         const gchar *key,
341                                         gboolean default_value)
342 {
343         g_return_val_if_fail(g_key_file, FALSE);
344         g_return_val_if_fail(g_group_name, FALSE);
345         g_return_val_if_fail(g_group_name[config_type], FALSE);
346         g_return_val_if_fail(key, FALSE);
347         GError *error = NULL;
348         gboolean bval =
349             g_key_file_get_boolean(g_key_file, g_group_name[config_type], key,
350                                    &error);
351         if (error) {
352                 if (error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND)
353                         critical_msg(LOG_CONFIG, "error:%s", error->message);
354                 cam_config_set_boolean_by_type(config_type, key, default_value);
355                 g_error_free(error);
356                 error = NULL;
357                 return default_value;
358         } else {
359                 return bval;
360         }
361 }