bug fix: TC-1152
[profile/ivi/ico-uxf-homescreen.git] / lib / common / CicoGKeyFileConfig.cpp
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
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
7  *
8  */
9
10 //==========================================================================
11 /**
12  *  @file   CicoGKeyFileConfig
13  *
14  *  @brief  This file is implementation of CicoGKeyFileConfig class
15  */
16 //==========================================================================
17
18 #include <ico_log.h>
19 #include <string.h>
20 #include <tzplatform_config.h>
21 #include "CicoGKeyFileConfig.h"
22 #include "CicoResourceConfig.h"
23
24 //==========================================================================
25 //  public functions
26 //==========================================================================
27 //--------------------------------------------------------------------------
28 /**
29  * @brief   CicoGKeyFileConfig::CicoGKeyFileConfig
30  *          Constractor
31  */
32 //--------------------------------------------------------------------------
33 CicoGKeyFileConfig::CicoGKeyFileConfig()
34     : m_gKeyFile(NULL)
35 {
36 }
37
38 //--------------------------------------------------------------------------
39 /**
40  * @brief   CicoGKeyFileConfig::CicoGKeyFileConfig
41  *          destructor
42  */
43 //--------------------------------------------------------------------------
44 CicoGKeyFileConfig::~CicoGKeyFileConfig()
45 {
46     if (NULL != m_gKeyFile) {
47         g_key_file_free(m_gKeyFile);
48     }
49 }
50
51 //--------------------------------------------------------------------------
52 /**
53  * @brief   CicoGKeyFileConfig::Initialize
54  *          read and initialize for homescreen configuration
55  *
56  * @param[in]   conf    configuration file name or path
57  * @param[in]   pkgname package name for default config file
58  * @return      result
59  * @retval      true    success
60  * @retval      false   error
61  */
62 //--------------------------------------------------------------------------
63 bool
64 CicoGKeyFileConfig::Initialize(const char *conf, const char *pkgname)
65 {
66     GError *error = NULL;
67     GString *filepath;
68
69     if (NULL != m_gKeyFile) {
70         /*if initialize was done*/
71         return true;
72     }
73
74     m_gKeyFile = g_key_file_new();
75     filepath = g_string_new("xx");
76
77     // if absolute path, fix file path
78     if (*conf == '/')   {
79         g_string_assign(filepath, conf);
80         if (! g_file_test(filepath->str, G_FILE_TEST_IS_REGULAR)) {
81             g_string_free(filepath, TRUE);
82             return false;
83         }
84     }
85     else    {
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);
92         }
93         else    {
94             if (! pkgname)  {
95                 pkgname = getenv("PKG_NAME");
96                 if (! pkgname)  {
97                     g_string_free(filepath, TRUE);
98                     return false;
99                 }
100             }
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,
104                                                      conf);
105             if (g_file_test(pkgpath, G_FILE_TEST_IS_REGULAR))   {
106                 // exist package config file
107                 g_string_assign(filepath, pkgpath);
108             }
109             else    {
110                 g_string_free(filepath, TRUE);
111                 return false;
112             }
113         }
114     }
115
116     // load config file
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);
122
123     if (error != NULL) {
124         ICO_WRN("%s: %s", filepath->str, error->message);
125         g_error_free(error);
126         return false;
127     }
128     return true;
129 }
130
131 /*--------------------------------------------------------------------------*/
132 /**
133  * @brief   CicoGKeyFileConfig::ConfigGetInteger
134  *          Return integer value in homescreen configuration file.
135  *          If not exist,return defaultvlaue in params.
136  *
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
141  */
142 /*--------------------------------------------------------------------------*/
143 int
144 CicoGKeyFileConfig::ConfigGetInteger(const char *group_name, const char *key, int default_value)
145 {
146     GError *error = NULL;
147
148     if (m_gKeyFile == NULL) {
149         return default_value;
150     }
151
152     int value = g_key_file_get_integer(m_gKeyFile, group_name, key, &error);
153     if (error != NULL) {
154         ICO_WRN("%s", error->message);
155         g_error_free(error);
156         return default_value;
157     }
158     return value;
159 }
160
161 /*--------------------------------------------------------------------------*/
162 /**
163  * @brief   CicoGKeyFileConfig::ConfigGetString
164  *          Return string value in homescreen configuration file.
165  *          If not exist,return defaultvlaue in params.
166  *
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
171  */
172 /*--------------------------------------------------------------------------*/
173 const char *
174 CicoGKeyFileConfig::ConfigGetString(const char *group_name, const char *key,
175                                     const char *default_value)
176 {
177     GError *error = NULL;
178
179     if (m_gKeyFile == NULL) {
180         return default_value;
181     }
182
183     const char *value = g_key_file_get_string(m_gKeyFile, group_name, key,
184                                               &error);
185     if (error != NULL) {
186         ICO_WRN("%s:%s> %s", group_name, key, error->message);
187         g_error_free(error);
188         return default_value;
189     }
190     return value;
191 }
192
193 /*--------------------------------------------------------------------------*/
194 /**
195  * @brief   CicoGKeyFileConfig::ConfigGetFilePath
196  *          Return string value of homescreen configuration file path.
197  *          If not exist, return default vlaue in params.
198  *
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
205  */
206 /*--------------------------------------------------------------------------*/
207 const char *
208 CicoGKeyFileConfig::ConfigGetFilePath(const char *group_name, const char *key,
209                                       const char *subdir, const char *confdir,
210                                       const char *default_path)
211 {
212     GError *error = NULL;
213
214     if (m_gKeyFile != NULL) {
215         const char *value = g_key_file_get_string(m_gKeyFile, group_name, key, &error);
216         if (error == NULL) {
217             if (*value == '/')  {
218                 // absolute path
219                 return value;
220             }
221
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))  {
225                 return homepath;
226             }
227
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))  {
231                 return confpath;
232             }
233         }
234         else    {
235             ICO_WRN("%s", error->message);
236             g_error_free(error);
237         }
238     }
239     if (! default_path) {
240         return NULL;
241     }
242     if (*default_path == '/')   {
243         return default_path;
244     }
245     return tzplatform_mkpath(TZ_SYS_RO_APP, default_path);
246 }
247 // vim:set expandtab ts=4 sw=4: