Add glade proects (autogenerated)
[mediadatabase.git] / libfrontend / conf.c
blob3e1c03fa1f0e4dc47d5dcd920932fec157a34f66
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * $Id: conf.c,v 1.2 2004/05/21 23:42:49 nedko Exp $
6 * DESCRIPTION:
7 * Mediadatabase configuration file handling.
9 * AUTHOR:
10 * Nedko Arnaudov <nedko@users.sourceforge.net>
12 * LICENSE:
13 * GNU GENERAL PUBLIC LICENSE version 2
15 *****************************************************************************/
17 #include <stdlib.h>
18 #include <string.h>
19 #include <cfl.h>
21 #include "../result.h"
22 #include "conf.h"
23 #include "error.h"
24 #include "db.h"
25 #include "disk.h"
27 #define CONF_FILE ".mediadatabase"
28 #define CFL_SECTION_GLOBAL NULL
29 #define CFL_SECTION_CUI "cui"
30 #define CFL_SECTION_MYSQL "MySQL"
31 #define CFL_SECTION_SQLITE "SQLite"
32 #define CFL_VALUE_MYSQL_HOST "host"
33 #define CFL_VALUE_MYSQL_USER "user"
34 #define CFL_VALUE_MYSQL_PASS "pass"
35 #define CFL_VALUE_MYSQL_DB "db"
36 #define CFL_VALUE_MOUNT "mount"
37 #define CFL_VALUE_UNMOUNT "unmount"
38 #define CFL_VALUE_MOUNTDIR "mountdir"
39 #define CFL_VALUE_SQLITE_DB "dbfile"
40 #define CFL_VALUE_BACKEND "backend"
42 mediadb_result
43 conf_parse()
45 mediadb_result r;
46 size_t s;
47 const char *pszHOME;
48 char *pszConf;
49 int nRet;
50 cflh_t hConf;
51 FILE *hFile;
52 char *pszCFLValue;
53 cfl_value_type_t CFLValueType;
55 pszHOME = getenv("HOME");
56 hFile = NULL;
57 hConf = NULL;
58 pszConf = NULL;
60 if (pszHOME == NULL)
62 mediadb_error_callback(
63 MEDIADB_ERROR_CRITICAL,
64 "HOME environment variable not set.");
65 r = MEDIADB_NO_HOME;
66 goto Exit;
69 s = strlen(pszHOME) + 1 + strlen(CONF_FILE) + 1;
71 pszConf = (char *)malloc(s);
72 if (pszConf == NULL)
74 mediadb_error_callback(
75 MEDIADB_ERROR_CRITICAL,
76 "Out of memory.");
77 r = MEDIADB_MEM;
78 goto Exit;
81 sprintf(pszConf, "%s/%s", pszHOME, CONF_FILE);
83 hFile = fopen(pszConf, "r");
84 if (hFile == NULL)
86 mediadb_error_printf(
87 MEDIADB_ERROR_CRITICAL,
88 "Cannot open configuration file \"%s\".",
89 pszConf);
90 r = MEDIADB_FAIL;
91 goto Exit;
94 hConf = cfl_alloc();
95 if (hConf == NULL)
97 mediadb_error_callback(
98 MEDIADB_ERROR_CRITICAL,
99 "Out of memory.");
100 r = MEDIADB_MEM;
101 goto Exit;
104 nRet = cfl_init_from_file(
105 hConf,
106 hFile,
107 CFL_FILE_TYPE_DEFAULT);
108 if (nRet != 0)
110 mediadb_error_printf(
111 MEDIADB_ERROR_CRITICAL,
112 "cfl_init_from_file() failed. (%s)",
113 cfl_errcode_to_message(nRet));
114 goto Exit;
117 pszCFLValue = cfl_value_get_by_name(
118 hConf,
119 CFL_SECTION_GLOBAL,
120 CFL_VALUE_MOUNT,
121 &CFLValueType);
123 if (pszCFLValue != NULL &&
124 CFLValueType == CFL_TYPE_STRING)
126 r = disk_set_mount_command(pszCFLValue);
127 if (MEDIADB_IS_ERROR(r))
129 goto Exit;
133 pszCFLValue = cfl_value_get_by_name(
134 hConf,
135 CFL_SECTION_GLOBAL,
136 CFL_VALUE_UNMOUNT,
137 &CFLValueType);
139 if (pszCFLValue != NULL &&
140 CFLValueType == CFL_TYPE_STRING)
142 r = disk_set_unmount_command(pszCFLValue);
143 if (MEDIADB_IS_ERROR(r))
145 goto Exit;
149 pszCFLValue = cfl_value_get_by_name(
150 hConf,
151 CFL_SECTION_GLOBAL,
152 CFL_VALUE_MOUNTDIR,
153 &CFLValueType);
155 if (pszCFLValue != NULL &&
156 CFLValueType == CFL_TYPE_STRING)
158 r = disk_set_path(pszCFLValue);
159 if (MEDIADB_IS_ERROR(r))
161 goto Exit;
165 pszCFLValue = cfl_value_get_by_name(
166 hConf,
167 CFL_SECTION_MYSQL,
168 CFL_VALUE_MYSQL_HOST,
169 &CFLValueType);
171 if (pszCFLValue != NULL &&
172 CFLValueType == CFL_TYPE_STRING)
174 r = db_set_mysql_host(pszCFLValue);
175 if (MEDIADB_IS_ERROR(r))
177 goto Exit;
181 pszCFLValue = cfl_value_get_by_name(
182 hConf,
183 CFL_SECTION_MYSQL,
184 CFL_VALUE_MYSQL_DB,
185 &CFLValueType);
187 if (pszCFLValue != NULL &&
188 CFLValueType == CFL_TYPE_STRING)
190 r = db_set_mysql_database(pszCFLValue);
191 if (MEDIADB_IS_ERROR(r))
193 goto Exit;
197 pszCFLValue = cfl_value_get_by_name(
198 hConf,
199 CFL_SECTION_MYSQL,
200 CFL_VALUE_MYSQL_USER,
201 &CFLValueType);
203 if (pszCFLValue != NULL &&
204 CFLValueType == CFL_TYPE_STRING)
206 r = db_set_mysql_user(pszCFLValue);
207 if (MEDIADB_IS_ERROR(r))
209 goto Exit;
213 pszCFLValue = cfl_value_get_by_name(
214 hConf,
215 CFL_SECTION_MYSQL,
216 CFL_VALUE_MYSQL_PASS,
217 &CFLValueType);
219 if (pszCFLValue != NULL &&
220 CFLValueType == CFL_TYPE_STRING)
222 r = db_set_mysql_pass(pszCFLValue);
223 if (MEDIADB_IS_ERROR(r))
225 goto Exit;
229 pszCFLValue = cfl_value_get_by_name(
230 hConf,
231 CFL_SECTION_SQLITE,
232 CFL_VALUE_SQLITE_DB,
233 &CFLValueType);
235 if (pszCFLValue != NULL &&
236 CFLValueType == CFL_TYPE_STRING)
238 r = db_set_sqlite_database(pszCFLValue);
239 if (MEDIADB_IS_ERROR(r))
241 goto Exit;
245 pszCFLValue = cfl_value_get_by_name(
246 hConf,
247 CFL_SECTION_GLOBAL,
248 CFL_VALUE_BACKEND,
249 &CFLValueType);
251 if (pszCFLValue != NULL &&
252 CFLValueType == CFL_TYPE_STRING)
254 if (strcmp(pszCFLValue, CFL_SECTION_SQLITE) == 0)
256 db_use_sqlite();
258 else if (strcmp(pszCFLValue, CFL_SECTION_MYSQL) == 0)
260 db_use_mysql();
264 r = MEDIADB_OK;
266 Exit:
267 if (hConf != NULL)
269 cfl_free(hConf);
272 if (hFile != NULL)
274 fclose(hFile);
277 if (pszConf != NULL)
279 free(pszConf);
282 return r;
285 /*****************************************************************************
287 * Modifications log:
289 * !!! WARNING !!! Following lines are automatically updated by the CVS system.
291 * $Log: conf.c,v $
292 * Revision 1.2 2004/05/21 23:42:49 nedko
293 * mediadb_error_callback() now tells if error is critical.
295 * Revision 1.1 2004/05/16 19:01:17 nedko
296 * libfrontend holds code common to frontends but not in libdb.
298 *****************************************************************************/