c522c8b72f21492470fe4d6e253243321994623e
[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 "CicoGKeyFileConfig.h"
20 #include "CicoResourceConfig.h"
21
22 //==========================================================================
23 //  public functions
24 //==========================================================================
25 //--------------------------------------------------------------------------
26 /**
27  * @brief   CicoGKeyFileConfig::CicoGKeyFileConfig
28  *          Constractor
29  */
30 //--------------------------------------------------------------------------
31 CicoGKeyFileConfig::CicoGKeyFileConfig()
32     : m_gKeyFile(NULL)
33 {
34 }
35
36 //--------------------------------------------------------------------------
37 /**
38  * @brief   CicoGKeyFileConfig::CicoGKeyFileConfig
39  *          destructor
40  */
41 //--------------------------------------------------------------------------
42 CicoGKeyFileConfig::~CicoGKeyFileConfig()
43 {
44     if (NULL != m_gKeyFile) {
45         g_key_file_free(m_gKeyFile);
46     }
47 }
48
49 //--------------------------------------------------------------------------
50 /**
51  * @brief   CicoGKeyFileConfig::Initialize
52  *          read and initialize for homescreen configuration
53  *
54  * @param[in]   conf    configuration file name
55  * @return      result
56  * @retval      true    success
57  * @retval      false   error
58  */
59 //--------------------------------------------------------------------------
60 bool
61 CicoGKeyFileConfig::Initialize(const char *conf)
62 {
63     GError *error = NULL;
64     char path[ICO_TEMP_BUF_SIZE];
65     GString *filepath;
66
67     if (NULL != m_gKeyFile) {
68         /*if initialize was done*/
69         return true;
70     }
71
72     m_gKeyFile = g_key_file_new();
73     
74     filepath = g_string_new("test");
75     CicoResourceConfig::GetConfPath(path, sizeof(path));
76     g_string_printf(filepath, "%s/%s", path, conf);
77     if (!g_file_test(filepath->str, G_FILE_TEST_IS_REGULAR)) {
78         return false;
79     }
80
81     // load config file
82     g_key_file_load_from_file(m_gKeyFile, filepath->str,
83                               (GKeyFileFlags)(G_KEY_FILE_KEEP_COMMENTS |
84                                               G_KEY_FILE_KEEP_TRANSLATIONS),
85                               &error);
86     if (error != NULL) {
87         ICO_WRN("%s: %s", filepath->str, error->message);
88         g_error_free(error);
89         return false;
90     }
91
92     g_string_free(filepath, TRUE);
93
94     return true;
95 }
96
97 /*--------------------------------------------------------------------------*/
98 /**
99  * @brief   CicoGKeyFileConfig::ConfigGetInteger
100  *          Return integer value in homescreen configuration file. 
101  *          If not exist,return defaultvlaue in params.
102  *
103  * @param[in]   group_name          configuration group name
104  * @param[in]   key                 configuration name
105  * @param[in]   default_value       default value
106  * @return      config integer
107  */
108 /*--------------------------------------------------------------------------*/
109 int
110 CicoGKeyFileConfig::ConfigGetInteger(const char *group_name, const char *key, int default_value)
111 {
112     GError *error = NULL;
113
114     if (m_gKeyFile == NULL) {
115         return default_value;
116     }
117
118     int value = g_key_file_get_integer(m_gKeyFile, group_name, key, &error);
119     if (error != NULL) {
120         ICO_WRN("%s", error->message);
121         g_error_free(error);
122         return default_value;
123     }
124     return value;
125 }
126
127 /*--------------------------------------------------------------------------*/
128 /**
129  * @brief   CicoGKeyFileConfig::ConfigGetString
130  *          Return string value in homescreen configuration file. 
131  *          If not exist,return defaultvlaue in params.
132  *
133  * @param[in]   group_name          configuration group name
134  * @param[in]   key                 configuration name
135  * @param[in]   default_value       default value
136  * @return      config string
137  */
138 /*--------------------------------------------------------------------------*/
139 const char *
140 CicoGKeyFileConfig::ConfigGetString(const char *group_name, const char *key,
141                                     const char *default_value)
142 {
143     GError *error = NULL;
144
145     if (m_gKeyFile == NULL) {
146         return default_value;
147     }
148
149     const char *value = g_key_file_get_string(m_gKeyFile, group_name, key,
150                                               &error);
151     if (error != NULL) {
152         ICO_WRN("%s", error->message);
153         g_error_free(error);
154         return default_value;
155     }
156     return value;
157 }
158 // vim:set expandtab ts=4 sw=4: