Let configuration mechanism be extensible
[cascardo/atompub.git] / config / gkeyfile.c
1 /*
2  *  Copyright (C) 2007-2008 Thadeu Lima de Souza Cascardo
3  *  <cascardo@holoscopio.com>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20
21 #include <atompub/atom.h>
22 #include <glib.h>
23
24 static gchar *
25 gkeyfile_atom_config_get_str (AtomCtx *ctx, gchar *group, gchar * key)
26 {
27   GKeyFile *keyfile;
28   gchar *value;
29   GError *error = NULL;
30   keyfile = atom_config_data (atom_config (ctx));
31   value = g_key_file_get_string (keyfile, group, key, &error);
32   if (value == NULL)
33     {
34       AtomError *aerr = atom_error_new ();
35       atom_error_code_set (aerr, 500);
36       atom_error_message_set (aerr, error->message);
37       atom_error_set (ctx, aerr);
38       g_error_free (error);
39     }
40   return value;
41 }
42
43 AtomConfig *
44 gkeyfile_atom_config_init (AtomCtx *ctx, char *name)
45 {
46   AtomConfig *config;
47   GKeyFile *keyfile;
48   GError *error = NULL;
49   keyfile = g_key_file_new ();
50   if (!g_key_file_load_from_file (keyfile, name,
51                                   G_KEY_FILE_NONE, &error))
52     {
53       AtomError *aerr = atom_error_new ();
54       atom_error_code_set (aerr, 500);
55       atom_error_message_set (aerr, error->message);
56       atom_error_set (ctx, aerr);
57       g_error_free (error);
58       return;
59     }
60   config = atom_config_new ();
61   atom_config_get_str_set (config, gkeyfile_atom_config_get_str);
62   atom_config_data_set (config, keyfile);
63 }