2 * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <iniparser.h>
24 #include <Elementary.h>
27 #include <tzsh_quickpanel_service.h>
29 #include <app_common.h>
31 #include "preference.h"
35 #include "quickpanel-ui.h"
37 #define PREFERENCE_FILE_NAME "preference.ini"
39 static char *_get_preference_file_path()
41 char *data_path = NULL;
42 char file_path[MAX_FILE_PATH_LEN] = {0, };
45 data_path = app_get_data_path();
46 retif(data_path == NULL, NULL, "failed to app_get_data_path()");
48 snprintf(file_path, sizeof(file_path), "%s%s", data_path, PREFERENCE_FILE_NAME);
51 return strdup(file_path);
54 static const char *_default_preference_get(const char *key)
56 retif(key == NULL, NULL, "invalid parameter");
58 if (strcmp(key, PREF_BRIGHTNESS) == 0) {
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){
64 } else if (strcmp(key, PREF_SHORTCUT_ENABLE) == 0){
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";
73 static inline int _key_validation_check(const char *key)
75 if (strcmp(key, PREF_BRIGHTNESS) == 0) {
77 } else if (strcmp(key, PREF_QUICKSETTING_ORDER) == 0){
79 } else if (strcmp(key, PREF_QUICKSETTING_FEATURED_NUM) == 0){
81 } else if (strcmp(key, PREF_SHORTCUT_ENABLE) == 0){
83 } else if (strcmp(key, PREF_SHORTCUT_EARPHONE_ORDER) == 0){
90 static inline int _is_file_exist(void)
92 char *file_path = _get_preference_file_path();
93 retif(file_path == NULL, 0, "failed to _get_preference_file_path()");
95 if (access(file_path, O_RDWR) == 0) {
99 if (file_path != NULL) {
106 static void _default_file_create(void)
109 char *file_path = _get_preference_file_path();
110 retif(file_path == NULL, , "failed to _get_preference_file_path()");
112 fp = fopen(file_path, "w");
113 retif(fp == NULL, , "fatal:failed to create preference file %s", file_path);
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)
133 if (file_path != NULL) {
138 HAPI int quickpanel_preference_get(const char *key, char *value)
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!");
147 file_path = _get_preference_file_path();
148 retif(file_path == NULL, QP_FAIL, "failed to _get_preference_file_path()");
150 ini = iniparser_load(file_path);
152 DBG("failed to load ini file");
153 _default_file_create();
154 value_r = _default_preference_get(key);
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();
165 DBG("get:[%s]", value_r);
170 if (value_r != NULL) {
171 strncpy(value, value_r, strlen(value_r));
176 iniparser_freedict(ini);
179 if (file_path != NULL) {
186 HAPI const char *quickpanel_preference_default_get(const char *key)
188 retif(key == NULL, NULL, "Invalid parameter!");
190 return _default_preference_get(key);
193 HAPI int quickpanel_preference_set(const char *key, char *value)
197 dictionary *ini = NULL;
198 retif(key == NULL, QP_FAIL, "Invalid parameter!");
199 retif(value == NULL, QP_FAIL, "Invalid parameter!");
201 if (_is_file_exist() == 0) {
202 _default_file_create();
205 char *file_path = _get_preference_file_path();
206 retif(file_path == NULL, QP_FAIL, "failed to _get_preference_file_path()");
208 ini = iniparser_load(file_path);
209 retif(ini == NULL, QP_FAIL, "failed to load ini file");
211 if (iniparser_set(ini, (char *)key, value) == 0) {
214 ERR("failed to write %s=%s", key, value);
217 fp = fopen(file_path, "w");
219 iniparser_dump_ini(ini, fp);
223 iniparser_freedict(ini);
225 if (file_path != NULL) {