svn update: 48958 (latest:48959)
[framework/uifw/ecore.git] / src / lib / ecore_config / ecore_config_storage.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4
5 #ifdef HAVE_CONFIG_H
6 # include <config.h>
7 #endif
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <limits.h>
13
14 #include <sys/param.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18
19 #include "Ecore_Config.h"
20 #include "ecore_config_private.h"
21
22 /**
23  * Loads the default configuration.
24  * @return  @c ECORE_CONFIG_ERR_SUCC on success.  @c ECORE_CONFIG_ERR_NODATA
25  *          is returned if the file cannot be loaded.
26  * @ingroup Ecore_Config_File_Group
27  */
28 EAPI int
29 ecore_config_load(void)
30 {
31    char                file[PATH_MAX];
32
33    if (!__ecore_config_app_name)
34      return ECORE_CONFIG_ERR_FAIL;
35
36    snprintf(file, PATH_MAX, "%s/.e/apps/%s/config.eet", getenv("HOME"),
37             __ecore_config_app_name);
38    return ecore_config_file_load(file);
39 }
40
41 /**
42  * Saves the current configuration to the default file.
43  * @return  @c ECORE_CONFIG_ERR_SUCC is returned on success.
44  *          @c ECORE_CONFIG_ERR_FAIL is returned if the data cannot be
45  *          saved.
46  * @ingroup Ecore_Config_File_Group
47  */
48 EAPI int
49 ecore_config_save(void)
50 {
51    char                file[PATH_MAX];
52
53    if (!__ecore_config_app_name)
54      return ECORE_CONFIG_ERR_FAIL;
55
56    snprintf(file, PATH_MAX, "%s/.e/apps/%s/config.eet", getenv("HOME"),
57             __ecore_config_app_name);
58    return ecore_config_file_save(file);
59 }
60
61 /**
62  * Load the given configuration file to the local configuration.
63  * @param   file Name of the file to load.
64  * @return  @c ECORE_CONFIG_ERR_SUCC on success.  @c ECORE_CONFIG_ERR_NODATA
65  *          is returned if the file cannot be loaded.
66  * @ingroup Ecore_Config_File_Group
67  */
68 EAPI int
69 ecore_config_file_load(const char *file)
70 {
71    Ecore_Config_DB_File  *db;
72    char                 **keys;
73    int                    key_count;
74    int                    x;
75    // double                 ftmp;      UNUSED
76    // int                    pt;        UNUSED
77    // int                    itmp;      UNUSED
78    // Ecore_Config_Type      type;      UNUSED
79    char                   *data;
80
81    db = NULL;
82    data = NULL;
83
84    db = _ecore_config_db_open_read(file);
85    if (!db)
86      {
87         ERR("Cannot open database from file %s!", file);
88         return ECORE_CONFIG_ERR_NODATA;
89      }
90    key_count = 0;   
91    keys = _ecore_config_db_keys_get(db, &key_count);
92    if (keys)
93      {
94         for (x = 0; x < key_count; x++)
95           {
96             _ecore_config_db_read(db, keys[x]);
97           }
98      }
99    _ecore_config_db_close(db);
100    if (keys)
101      {
102         for (x = 0; x < key_count; x++)
103           {
104              free(keys[x]);
105           }
106         free(keys);
107      }
108    return ECORE_CONFIG_ERR_SUCC;
109 }
110
111 static void
112 _ecore_config_recurse_mkdir(const char *file)
113 {
114    char               *file_ptr;
115    char               *file_tmp;
116    struct stat         status;
117
118    file_tmp = strdup(file);
119    file_ptr = file_tmp + strlen(file_tmp);
120    while (*file_ptr != '/' && file_ptr > file_tmp)
121       file_ptr--;
122    *file_ptr = '\0';
123
124    if ((file_tmp[0] != 0) && stat(file_tmp, &status))
125      {
126         _ecore_config_recurse_mkdir(file_tmp);
127         mkdir(file_tmp, S_IRUSR | S_IWUSR | S_IXUSR);
128      }
129    free(file_tmp);
130 }
131
132 /**
133  * Saves the local configuration to the given file.
134  * @param   file Name of the file to save to.
135  * @return  @c ECORE_CONFIG_ERR_SUCC is returned on success.
136  *          @c ECORE_CONFIG_ERR_FAIL is returned if the data cannot be
137  *          saved.
138  * @ingroup Ecore_Config_File_Group
139  */
140 EAPI int
141 ecore_config_file_save(const char *file)
142 {
143    Ecore_Config_Prop    *next;
144    Ecore_Config_DB_File *db;
145    struct stat           status;
146
147    next = __ecore_config_bundle_local->data;
148    db = NULL;
149
150    /* if file does not exist check to see if the dirs exist, creating if not */
151    if (stat(file, &status))
152       _ecore_config_recurse_mkdir(file);
153
154    db = _ecore_config_db_open_write(file);
155    if (!db)
156      {
157         ERR("Cannot open database from file %s!", file);
158         return ECORE_CONFIG_ERR_FAIL;
159      }
160
161    while (next)
162      {
163         /* let the config_db deal with this
164          * handyande: hmm, not sure that it ever does - reinstating until
165          * further discussions satisfy me!
166          */
167         if (!(next->flags & ECORE_CONFIG_FLAG_MODIFIED) || next->flags & ECORE_CONFIG_FLAG_CMDLN)
168           {
169              next = next->next;
170              continue;
171           }
172
173         _ecore_config_db_write(db, next);
174
175         next = next->next;
176      }
177
178    _ecore_config_db_close(db);
179    return ECORE_CONFIG_ERR_SUCC;
180 }