tizen 2.4 release
[apps/home/quickpanel.git] / daemon / preference.c
1 /*
2  * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 #include <iniparser.h>
24 #include <Elementary.h>
25
26 #include <tzsh.h>
27 #include <tzsh_quickpanel_service.h>
28
29 #include "preference.h"
30 #include "common.h"
31 #include "quickpanel-ui.h"
32
33 #define FILE_PREFERENCE DATADIR_RW"/preference.ini"
34
35 static const char *_default_preference_get(const char *key)
36 {
37         retif(key == NULL, NULL, "invalid parameter");
38
39         if (strcmp(key, PREF_BRIGHTNESS) == 0) {
40                 return "OFF";
41         } else if (strcmp(key, PREF_QUICKSETTING_ORDER) == 0){
42                 return "wifi,gps,sound,rotate,bluetooth,mobile_data,assisitvelight,u_power_saving,wifi_hotspot,flightmode";
43         } else if (strcmp(key, PREF_QUICKSETTING_FEATURED_NUM) == 0){
44                 return "11";
45         } else if (strcmp(key, PREF_SHORTCUT_ENABLE) == 0){
46                 return "ON";
47         } else if (strcmp(key, PREF_SHORTCUT_EARPHONE_ORDER) == 0){
48                 return "org.tizen.music-player,org.tizen.videos,org.tizen.phone,srfxzv8GKR.YouTube,org.tizen.voicerecorder";
49         }
50
51         return NULL;
52 }
53
54 static inline int _key_validation_check(const char *key)
55 {
56         if (strcmp(key, PREF_BRIGHTNESS) == 0) {
57                 return 1;
58         } else if (strcmp(key, PREF_QUICKSETTING_ORDER) == 0){
59                 return 1;
60         } else if (strcmp(key, PREF_QUICKSETTING_FEATURED_NUM) == 0){
61                 return 1;
62         } else if (strcmp(key, PREF_SHORTCUT_ENABLE) == 0){
63                 return 1;
64         } else if (strcmp(key, PREF_SHORTCUT_EARPHONE_ORDER) == 0){
65                 return 1;
66         }
67
68         return 0;
69 }
70
71 static inline int _is_file_exist(void)
72 {
73
74         if (access(FILE_PREFERENCE, O_RDWR) == 0) {
75                 return 1;
76         }
77
78         return 0;
79 }
80
81 static void _default_file_create(void)
82 {
83         FILE    *fp = NULL ;
84
85         fp = fopen(FILE_PREFERENCE, "w");
86         retif(fp == NULL, , "fatal:failed to create preference file");
87
88         fprintf(fp, "\n\
89                         [%s]\n\
90                         %s = %s ;\n\
91                         %s = %s ;\n\
92                         %s = %s ;\n\
93                         %s = %s ;\n\
94                         %s = %s ;\n\
95                         \n"
96                         , PREF_SECTION
97                         , PREF_BRIGHTNESS_KEY, _default_preference_get(PREF_BRIGHTNESS)
98                         , PREF_QUICKSETTING_ORDER_KEY, _default_preference_get(PREF_QUICKSETTING_ORDER)
99                         , PREF_QUICKSETTING_FEATURED_NUM_KEY, _default_preference_get(PREF_QUICKSETTING_FEATURED_NUM)
100                         , PREF_SHORTCUT_ENABLE_KEY, _default_preference_get(PREF_SHORTCUT_ENABLE)
101                         , PREF_SHORTCUT_EARPHONE_ORDER_KEY, _default_preference_get(PREF_SHORTCUT_EARPHONE_ORDER)
102                    );
103
104         fclose(fp);
105 }
106
107 HAPI int quickpanel_preference_get(const char *key, char *value)
108 {
109         int ret = QP_OK;
110         dictionary      *ini = NULL;
111         const char *value_r = NULL;
112         retif(key == NULL, QP_FAIL, "Invalid parameter!");
113         retif(value == NULL, QP_FAIL, "Invalid parameter!");
114
115         ini = iniparser_load(FILE_PREFERENCE);
116         if (ini == NULL) {
117                 DBG("failed to load ini file");
118                 _default_file_create();
119                 value_r = _default_preference_get(key);
120                 goto END;
121         }
122
123         value_r = iniparser_getstr(ini, key);
124         if (value_r == NULL) {
125                 value_r = _default_preference_get(key);
126                 if (_key_validation_check(key) == 1) {
127                         _default_file_create();
128                 }
129                 goto END;
130         } else {
131                 DBG("get:[%s]", value_r);
132         }
133
134 END:
135         if (value_r != NULL) {
136                 strcpy(value, value_r);
137                 ret = QP_OK;
138         }
139
140         if (ini != NULL) {
141                 iniparser_freedict(ini);
142         }
143
144         return ret;
145 }
146
147 HAPI const char *quickpanel_preference_default_get(const char *key)
148 {
149         retif(key == NULL, NULL, "Invalid parameter!");
150
151         return _default_preference_get(key);
152 }
153
154 HAPI int quickpanel_preference_set(const char *key, char *value)
155 {
156         int ret = QP_FAIL;
157         FILE *fp = NULL;
158         dictionary      *ini = NULL;
159         retif(key == NULL, QP_FAIL, "Invalid parameter!");
160         retif(value == NULL, QP_FAIL, "Invalid parameter!");
161
162         if (_is_file_exist() == 0) {
163                 _default_file_create();
164         }
165
166         ini = iniparser_load(FILE_PREFERENCE);
167         retif(ini == NULL, QP_FAIL, "failed to load ini file");
168
169         if (iniparser_setstr(ini, (char *)key, value) == 0) {
170                 ret = QP_OK;
171         } else {
172                 ERR("failed to write %s=%s", key, value);
173         }
174
175         fp = fopen(FILE_PREFERENCE, "w");
176         if (fp != NULL) {
177                 iniparser_dump_ini(ini, fp);
178                 fclose(fp);
179         }
180
181         iniparser_freedict(ini);
182
183         return ret;
184 }