2 * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
10 //==========================================================================
12 * @file CicoGKeyFileConfig
14 * @brief This file is implementation of CicoGKeyFileConfig class
16 //==========================================================================
20 #include <tzplatform_config.h>
21 #include "CicoGKeyFileConfig.h"
22 #include "CicoResourceConfig.h"
24 //==========================================================================
26 //==========================================================================
27 //--------------------------------------------------------------------------
29 * @brief CicoGKeyFileConfig::CicoGKeyFileConfig
32 //--------------------------------------------------------------------------
33 CicoGKeyFileConfig::CicoGKeyFileConfig()
38 //--------------------------------------------------------------------------
40 * @brief CicoGKeyFileConfig::CicoGKeyFileConfig
43 //--------------------------------------------------------------------------
44 CicoGKeyFileConfig::~CicoGKeyFileConfig()
46 if (NULL != m_gKeyFile) {
47 g_key_file_free(m_gKeyFile);
51 //--------------------------------------------------------------------------
53 * @brief CicoGKeyFileConfig::Initialize
54 * read and initialize for homescreen configuration
56 * @param[in] conf configuration file name or path
57 * @param[in] pkgname package name for default config file
59 * @retval true success
62 //--------------------------------------------------------------------------
64 CicoGKeyFileConfig::Initialize(const char *conf, const char *pkgname)
69 if (NULL != m_gKeyFile) {
70 /*if initialize was done*/
74 m_gKeyFile = g_key_file_new();
75 filepath = g_string_new("xx");
77 // if absolute path, fix file path
79 g_string_assign(filepath, conf);
80 if (! g_file_test(filepath->str, G_FILE_TEST_IS_REGULAR)) {
81 g_string_free(filepath, TRUE);
86 // search user dir ($HOME/ico/config)
87 const char *userpath = tzplatform_mkpath3(TZ_USER_HOME,
88 ICO_SYC_CONFIGPATH_HOME_CONFIG, conf);
89 if (g_file_test(userpath, G_FILE_TEST_IS_REGULAR)) {
90 // exist user config file
91 g_string_assign(filepath, userpath);
95 pkgname = getenv("PKG_NAME");
97 g_string_free(filepath, TRUE);
101 // search package dir ($TZ_SYS_RO_APP/pkgname/res/config)
102 const char *pkgpath = tzplatform_mkpath4(TZ_SYS_RO_APP, pkgname,
103 ICO_SYC_CONFIGPATH_PACKAGE_CONFIG,
105 if (g_file_test(pkgpath, G_FILE_TEST_IS_REGULAR)) {
106 // exist package config file
107 g_string_assign(filepath, pkgpath);
110 g_string_free(filepath, TRUE);
117 ICO_DBG("CicoGKeyFileConfig::Initialize load config file<%s>", filepath->str);
118 g_key_file_load_from_file(m_gKeyFile, filepath->str,
119 (GKeyFileFlags)(G_KEY_FILE_KEEP_COMMENTS |
120 G_KEY_FILE_KEEP_TRANSLATIONS), &error);
121 g_string_free(filepath, TRUE);
124 ICO_WRN("%s: %s", filepath->str, error->message);
131 /*--------------------------------------------------------------------------*/
133 * @brief CicoGKeyFileConfig::ConfigGetInteger
134 * Return integer value in homescreen configuration file.
135 * If not exist,return defaultvlaue in params.
137 * @param[in] group_name configuration group name
138 * @param[in] key configuration name
139 * @param[in] default_value default value
140 * @return config integer
142 /*--------------------------------------------------------------------------*/
144 CicoGKeyFileConfig::ConfigGetInteger(const char *group_name, const char *key, int default_value)
146 GError *error = NULL;
148 if (m_gKeyFile == NULL) {
149 return default_value;
152 int value = g_key_file_get_integer(m_gKeyFile, group_name, key, &error);
154 ICO_WRN("%s", error->message);
156 return default_value;
161 /*--------------------------------------------------------------------------*/
163 * @brief CicoGKeyFileConfig::ConfigGetString
164 * Return string value in homescreen configuration file.
165 * If not exist,return defaultvlaue in params.
167 * @param[in] group_name configuration group name
168 * @param[in] key configuration name
169 * @param[in] default_value default value
170 * @return config string
172 /*--------------------------------------------------------------------------*/
174 CicoGKeyFileConfig::ConfigGetString(const char *group_name, const char *key,
175 const char *default_value)
177 GError *error = NULL;
179 if (m_gKeyFile == NULL) {
180 return default_value;
183 const char *value = g_key_file_get_string(m_gKeyFile, group_name, key,
186 ICO_WRN("%s:%s> %s", group_name, key, error->message);
188 return default_value;
193 /*--------------------------------------------------------------------------*/
195 * @brief CicoGKeyFileConfig::ConfigGetFilePath
196 * Return string value of homescreen configuration file path.
197 * If not exist, return default vlaue in params.
199 * @param[in] group_name configuration group name
200 * @param[in] key configuration name
201 * @param[in] subdir $HOME sub directory
202 * @param[in] confdir package configuration sub directory
203 * @param[in] default_path default file path
204 * @return config file path
206 /*--------------------------------------------------------------------------*/
208 CicoGKeyFileConfig::ConfigGetFilePath(const char *group_name, const char *key,
209 const char *subdir, const char *confdir,
210 const char *default_path)
212 GError *error = NULL;
214 if (m_gKeyFile != NULL) {
215 const char *value = g_key_file_get_string(m_gKeyFile, group_name, key, &error);
222 // check user home directory
223 const char *homepath = tzplatform_mkpath3(TZ_USER_HOME, subdir, value);
224 if (g_file_test(homepath, G_FILE_TEST_IS_REGULAR)) {
228 // check default config path
229 const char *confpath = tzplatform_mkpath3(TZ_SYS_RO_APP, confdir, value);
230 if (g_file_test(confpath, G_FILE_TEST_IS_REGULAR)) {
235 ICO_WRN("%s", error->message);
239 if (! default_path) {
242 if (*default_path == '/') {
245 return tzplatform_mkpath(TZ_SYS_RO_APP, default_path);
247 // vim:set expandtab ts=4 sw=4: