Update NEWS for v0.9.5
[quvi-tool.git] / src / opts / config.c
blob9455755204acca3a04dd7503a76a4a94c2db9620
1 /* quvi
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of quvi <http://quvi.sourceforge.net/>.
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <stdlib.h>
24 #include <string.h>
25 #include <glib/gi18n.h>
27 #include "lopts.h"
29 gchar **search_dirs()
31 const gchar* const *data_dirs;
32 gchar **r;
33 gint i,j;
35 data_dirs = g_get_system_data_dirs();
36 /* 3=user data, user config, trailing NULL. */
37 r = g_new(gchar*, g_strv_length((gchar**) data_dirs)+3);
38 i = 0;
39 j = 0;
41 while (data_dirs[j] != NULL)
42 r[i++] = g_strdup(data_dirs[j++]);
44 r[i++] = g_strdup(g_get_user_data_dir());
45 r[i++] = g_strdup(g_get_user_config_dir());
46 r[i] = NULL;
48 return (r);
51 extern gboolean show_config;
53 static void _keyfile_printerr(const gchar *fpath, GError *e)
55 if (e->domain == G_KEY_FILE_ERROR)
57 g_printerr(_("error: %s: while reading config: %s (code=%d)\n"),
58 fpath, e->message, e->code);
60 else
62 if (show_config == TRUE)
64 g_message(_("rejected: %s: %s (code=%d)"),
65 fpath, e->message, e->code);
68 g_error_free(e);
71 static void _keyfile_parse(lopts_t lopts, const gchar *fpath)
73 GKeyFile *f;
74 GError *e;
76 e = NULL;
77 f = g_key_file_new();
79 g_key_file_set_list_separator(f, ',');
81 if (g_key_file_load_from_file(f, fpath, G_KEY_FILE_NONE, &e) == TRUE)
83 if (show_config == TRUE)
84 g_message(_("parse: %s"), fpath);
86 if (lopts->cb.parse_keyfile_values != NULL)
87 lopts->cb.parse_keyfile_values(f, fpath);
89 else
90 _keyfile_printerr(fpath, e);
92 g_key_file_free(f);
95 void keyfile_read(lopts_t lopts)
97 gchar *rcpath, **dirs, *p;
98 const gchar *v;
99 gint i;
101 v = g_getenv("QUVI_SHOW_CONFIG");
102 show_config = (v != NULL && strlen(v) >0) ? TRUE:FALSE;
104 if (lopts->cb.get_config_fpath != NULL)
106 p = lopts->cb.get_config_fpath();
107 if (p != NULL) /* QUVI_CONFIG was defined. */
109 _keyfile_parse(lopts, p);
110 g_free(p);
111 return;
115 rcpath = g_build_path(G_DIR_SEPARATOR_S, "quvi", "quvirc", NULL);
116 dirs = search_dirs();
118 i = 0;
119 while (dirs[i] != NULL)
121 p = g_build_path(G_DIR_SEPARATOR_S, dirs[i++], rcpath, NULL);
122 _keyfile_parse(lopts, p);
123 g_free(p);
127 /* vim: set ts=2 sw=2 tw=72 expandtab: */