185b0db708f780dbb04329cefe64bafe5ccbdc7f
[apps/core/preloaded/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 #include <E_DBus.h>
29 #include <app_common.h>
30
31 #include "preference.h"
32 #include "common.h"
33
34
35 #include "quickpanel-ui.h"
36
37 #define PREFERENCE_FILE_NAME "preference.ini"
38
39 static char *_get_preference_file_path()
40 {
41         char *data_path = NULL;
42         char file_path[MAX_FILE_PATH_LEN] = {0, };
43
44
45         data_path = app_get_data_path();
46         retif(data_path == NULL, NULL, "failed to app_get_data_path()");
47
48         snprintf(file_path, sizeof(file_path), "%s%s", data_path, PREFERENCE_FILE_NAME);
49         free(data_path);
50
51         return strdup(file_path);
52 }
53
54 static const char *_default_preference_get(const char *key)
55 {
56         retif(key == NULL, NULL, "invalid parameter");
57
58         if (strcmp(key, PREF_BRIGHTNESS) == 0) {
59                 return "OFF";
60         } else if (strcmp(key, PREF_QUICKSETTING_ORDER) == 0){
61                 return "wifi,gps,sound,rotate,bluetooth,mobile_data,assisitvelight,u_power_saving,wifi_hotspot,flightmode";
62         } else if (strcmp(key, PREF_QUICKSETTING_FEATURED_NUM) == 0){
63                 return "11";
64         } else if (strcmp(key, PREF_SHORTCUT_ENABLE) == 0){
65                 return "ON";
66         } else if (strcmp(key, PREF_SHORTCUT_EARPHONE_ORDER) == 0){
67                 return "org.tizen.music-player,org.tizen.videos,org.tizen.phone,srfxzv8GKR.YouTube,org.tizen.voicerecorder";
68         }
69
70         return NULL;
71 }
72
73 static inline int _key_validation_check(const char *key)
74 {
75         if (strcmp(key, PREF_BRIGHTNESS) == 0) {
76                 return 1;
77         } else if (strcmp(key, PREF_QUICKSETTING_ORDER) == 0){
78                 return 1;
79         } else if (strcmp(key, PREF_QUICKSETTING_FEATURED_NUM) == 0){
80                 return 1;
81         } else if (strcmp(key, PREF_SHORTCUT_ENABLE) == 0){
82                 return 1;
83         } else if (strcmp(key, PREF_SHORTCUT_EARPHONE_ORDER) == 0){
84                 return 1;
85         }
86
87         return 0;
88 }
89
90 static inline int _is_file_exist(void)
91 {
92         char *file_path = _get_preference_file_path();
93         retif(file_path == NULL, 0, "failed to _get_preference_file_path()");
94
95         if (access(file_path, O_RDWR) == 0) {
96                 return 1;
97         }
98
99         if (file_path != NULL) {
100                 free(file_path);
101         }
102
103         return 0;
104 }
105
106 static void _default_file_create(void)
107 {
108         FILE    *fp = NULL ;
109         char *file_path = _get_preference_file_path();
110         retif(file_path == NULL, , "failed to _get_preference_file_path()");
111
112         fp = fopen(file_path, "w");
113         retif(fp == NULL, , "fatal:failed to create preference file %s", file_path);
114
115         fprintf(fp, "\n\
116                         [%s]\n\
117                         %s = %s ;\n\
118                         %s = %s ;\n\
119                         %s = %s ;\n\
120                         %s = %s ;\n\
121                         %s = %s ;\n\
122                         \n"
123                         , PREF_SECTION
124                         , PREF_BRIGHTNESS_KEY, _default_preference_get(PREF_BRIGHTNESS)
125                         , PREF_QUICKSETTING_ORDER_KEY, _default_preference_get(PREF_QUICKSETTING_ORDER)
126                         , PREF_QUICKSETTING_FEATURED_NUM_KEY, _default_preference_get(PREF_QUICKSETTING_FEATURED_NUM)
127                         , PREF_SHORTCUT_ENABLE_KEY, _default_preference_get(PREF_SHORTCUT_ENABLE)
128                         , PREF_SHORTCUT_EARPHONE_ORDER_KEY, _default_preference_get(PREF_SHORTCUT_EARPHONE_ORDER)
129                    );
130
131         fclose(fp);
132
133         if (file_path != NULL) {
134                 free(file_path);
135         }
136 }
137
138 HAPI int quickpanel_preference_get(const char *key, char *value)
139 {
140         int ret = QP_OK;
141         dictionary      *ini = NULL;
142         const char *value_r = NULL;
143         char *file_path = NULL;
144         retif(key == NULL, QP_FAIL, "Invalid parameter!");
145         retif(value == NULL, QP_FAIL, "Invalid parameter!");
146
147         file_path = _get_preference_file_path();
148         retif(file_path == NULL, QP_FAIL, "failed to _get_preference_file_path()");
149
150         ini = iniparser_load(file_path);
151         if (ini == NULL) {
152                 DBG("failed to load ini file");
153                 _default_file_create();
154                 value_r = _default_preference_get(key);
155                 goto END;
156         }
157
158         value_r = iniparser_getstring(ini, key, NULL);
159         if (value_r == NULL) {
160                 value_r = _default_preference_get(key);
161                 if (_key_validation_check(key) == 1) {
162                         _default_file_create();
163                 }
164         } else {
165                 DBG("get:[%s]", value_r);
166         }
167
168
169 END:
170         if (value_r != NULL) {
171                 strncpy(value, value_r, strlen(value_r));
172                 ret = QP_OK;
173         }
174
175         if (ini != NULL) {
176                 iniparser_freedict(ini);
177         }
178
179         if (file_path != NULL) {
180                 free(file_path);
181         }
182
183         return ret;
184 }
185
186 HAPI const char *quickpanel_preference_default_get(const char *key)
187 {
188         retif(key == NULL, NULL, "Invalid parameter!");
189
190         return _default_preference_get(key);
191 }
192
193 HAPI int quickpanel_preference_set(const char *key, char *value)
194 {
195         int ret = QP_FAIL;
196         FILE *fp = NULL;
197         dictionary      *ini = NULL;
198         retif(key == NULL, QP_FAIL, "Invalid parameter!");
199         retif(value == NULL, QP_FAIL, "Invalid parameter!");
200
201         if (_is_file_exist() == 0) {
202                 _default_file_create();
203         }
204
205         char *file_path = _get_preference_file_path();
206         retif(file_path == NULL, QP_FAIL, "failed to _get_preference_file_path()");
207
208         ini = iniparser_load(file_path);
209         retif(ini == NULL, QP_FAIL, "failed to load ini file");
210
211         if (iniparser_set(ini, (char *)key, value) == 0) {
212                 ret = QP_OK;
213         } else {
214                 ERR("failed to write %s=%s", key, value);
215         }
216
217         fp = fopen(file_path, "w");
218         if (fp != NULL) {
219                 iniparser_dump_ini(ini, fp);
220                 fclose(fp);
221         }
222
223         iniparser_freedict(ini);
224
225         if (file_path != NULL) {
226                 free(file_path);
227         }
228
229         return ret;
230 }
231
232
233