Updated Danish translation
[gcalctool.git] / src / get.c
blob25b70bb578d5619fdadfc99e9c60cfd18088166d
1 /* Copyright (c) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
2 * Copyright (c) 2008-2009 Robert Ancell
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
20 #include <stdio.h>
21 #include <string.h>
22 #include <ctype.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <langinfo.h>
26 #include <locale.h>
27 #include <sys/types.h>
28 #include <sys/file.h>
29 #include <sys/param.h>
30 #include <assert.h>
31 #include <gconf/gconf-client.h>
33 #include "get.h"
34 #include "register.h"
35 #include "mp.h"
37 #define EQUAL(a, b) (strlen(a)==strlen(b)) & !strcmp(a, b)
39 /* Various string values read/written as X resources. */
41 static GConfClient *client = NULL;
44 char *
45 get_resource(const char *key)
47 char key_name[MAXLINE];
48 SNPRINTF(key_name, MAXLINE, "/apps/gcalctool/%s", key);
49 return gconf_client_get_string(client, key_name, NULL);
53 void
54 set_resource(const char *key, const char *value)
56 char key_name[MAXLINE];
57 SNPRINTF(key_name, MAXLINE, "/apps/gcalctool/%s", key);
58 gconf_client_set_string(client, key_name, value, NULL);
62 void
63 set_int_resource(const char *key, int value)
65 char key_name[MAXLINE];
66 SNPRINTF(key_name, MAXLINE, "/apps/gcalctool/%s", key);
67 gconf_client_set_int(client, key_name, value, NULL);
71 void
72 set_boolean_resource(const char *key, int value)
74 char key_name[MAXLINE];
75 SNPRINTF(key_name, MAXLINE, "/apps/gcalctool/%s", key);
76 gconf_client_set_bool(client, key_name, value, NULL);
80 void set_enumerated_resource(const char *key, const char *values[], int value)
82 set_resource(key, values[value]);
86 int
87 get_int_resource(const char *key, int *intval)
89 char key_name[MAXLINE];
90 GError *error = NULL;
91 gint v;
93 SNPRINTF(key_name, MAXLINE, "/apps/gcalctool/%s", key);
94 v = gconf_client_get_int(client, key_name, &error);
95 if (error)
96 return FALSE;
97 *intval = v;
99 return TRUE;
104 get_boolean_resource(const char *key, int *boolval)
106 char key_name[MAXLINE];
107 GError *error = NULL;
108 gboolean v;
110 SNPRINTF(key_name, MAXLINE, "/apps/gcalctool/%s", key);
111 v = gconf_client_get_bool(client, key_name, &error);
112 if (error)
113 return FALSE;
114 *boolval = v;
116 return TRUE;
121 get_enumerated_resource(const char *key, const char *values[], int *value)
123 char *val;
124 int i, retval = FALSE;
126 val = get_resource(key);
127 if (!val)
128 return FALSE;
130 for (i = 0; values[i]; i++) {
131 if (strcmp(values[i], val) == 0) {
132 *value = i;
133 retval = TRUE;
134 break;
137 free(val);
139 return retval;
143 /* Return the radix character. For most locales, this is a period.
144 * If nl_langinfo(RADIXCHAR) returns an empty string, return ",".
146 const char *
147 get_radix()
149 const char *radix;
151 setlocale(LC_NUMERIC, "");
152 if ((radix = nl_langinfo(RADIXCHAR)) != NULL) {
153 radix = g_locale_to_utf8(radix, -1, NULL, NULL, NULL);
156 if (radix == NULL || radix[0] == '\0') {
157 return(".");
158 } else {
159 return(radix);
164 /* Return the thousands separator string. For most locales, this is a
165 * comma.
167 const char *
168 get_tsep()
170 char *tsep;
172 setlocale(LC_NUMERIC, "");
173 if ((tsep = nl_langinfo(THOUSEP)) != NULL) {
174 tsep = g_locale_to_utf8(tsep, -1, NULL, NULL, NULL);
177 if (tsep == NULL) {
178 return("");
179 } else {
180 return(tsep);
186 get_tsep_count()
188 return 3;
192 void
193 resources_init()
195 assert(client == NULL);
196 client = gconf_client_get_default();
197 gconf_client_add_dir(client, "/apps/gcalctool", GCONF_CLIENT_PRELOAD_NONE, NULL);