Debian Codegraph @

src/prefs.c

raw symbols SCIP
1 /* prefs.c - preferences handling functions
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
7 */
8 #include <gtk/gtk.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <errno.h>
13
14 #include "game.h"
15 #include "gtkballs.h" /* for _() */
16
17 #define BUFFER_SIZE 1024
18 #define CONFIG_FILE_NAME "gtkballs.ini"
19
20 gint _show_next_colors = TRUE;
21 gint _show_path = TRUE;
22 gint _show_footprints = TRUE;
23 gint _show_destroy = TRUE;
24 gint _show_highlight = TRUE;
25 gint _hl_dr = 64;
26 gint _hl_dg = 64;
27 gint _hl_db = 64;
28
29 /* name of default theme. TODO: make "internal" theme, so, when
30 * theme name is NULL we can use it... */
31 gchar _default_theme_name[] = "Default";
32
33 /* This is the name of theme to use */
34 gchar *_theme_name = NULL;
35
36 gint pref_get_show_next_colors(void) {
37 return _show_next_colors;
38 }
39
40 void pref_set_show_next_colors(gint val) {
41 _show_next_colors = val;
42 }
43
44 gint pref_get_show_path(void) {
45 return _show_path;
46 }
47
48 void pref_set_show_path(gint val) {
49 _show_path = val;
50 }
51
52 gint pref_get_show_footprints(void) {
53 return _show_footprints;
54 }
55
56 void pref_set_show_footprints(gint val) {
57 _show_footprints = val;
58 }
59
60 gint pref_get_show_destroy(void) {
61 return _show_destroy;
62 }
63
64 void pref_set_show_destroy(gint val) {
65 _show_destroy = val;
66 }
67
68 gint pref_get_show_highlight(void) {
69 return _show_highlight;
70 }
71
72 void pref_set_show_highlight(gint val) {
73 _show_highlight = val;
74 }
75
76 gint prefs_get_hl_dr(void) {
77 return _hl_dr;
78 }
79
80 void prefs_set_hl_dr(gint dr) {
81 _hl_dr = dr;
82 }
83
84 gint prefs_get_hl_dg(void) {
85 return _hl_dg;
86 }
87
88 void prefs_set_hl_dg(gint dg) {
89 _hl_dg = dg;
90 }
91
92 gint prefs_get_hl_db(void) {
93 return _hl_db;
94 }
95
96 void prefs_set_hl_db(gint db) {
97 _hl_db = db;
98 }
99
100 gchar *pref_get_theme_name(void) {
101 return _theme_name;
102 }
103
104 gchar *pref_get_default_theme_name(void) {
105 return _default_theme_name;
106 }
107
108 void pref_set_theme_name(gchar *name) {
109 if (_theme_name) {
110 g_free(_theme_name);
111 }
112 _theme_name = g_strdup(name);
113 }
114
115
116 /* converts string to TRUE/FALSE. "true", "yes" or "1" is TRUE, otherwise - FALSE */
117 gboolean pref_str_to_bool(gchar *val) {
118 if (!g_ascii_strcasecmp(val, "true") ||
119 !g_ascii_strcasecmp(val, "yes") ||
120 !g_ascii_strcasecmp(val, "1")) {
121 return TRUE;
122 }
123 return FALSE;
124 }
125
126 /* converts boolean to string
127 returns _pointer_ to "yes" or "no". returned string should NOT be free()'d! */
128 gchar *pref_bool_to_str(gboolean val) {
129 return val ? "yes" : "no";
130 }
131
132 /* we use very lame preferences file format: ``property=value\n''.
133 so - there must be no whitespaces on begin/end of line =) */
134 void load_preferences(void)
135 {
136 FILE *fp;
137 gchar *rc_file;
138 gchar buffer[BUFFER_SIZE];
139 gchar **prop_val;
140
141 if (!_theme_name) {
142 _theme_name = g_strdup(_default_theme_name);
143 }
144 rc_file = get_config_dir_file (CONFIG_FILE_NAME);
145 if ((fp = fopen(rc_file, "r")))
146 {
147 while(fgets(buffer, BUFFER_SIZE, fp))
148 {
149 g_strchomp(buffer);
150 prop_val = g_strsplit(buffer, "=", 2);
151 if (prop_val[0] && prop_val[0][0] && prop_val[1] && prop_val[1][0])
152 {
153 if (!g_ascii_strcasecmp(prop_val[0], "show_hints")) {
154 _show_next_colors = pref_str_to_bool(prop_val[1]);
155 } else if (!g_ascii_strcasecmp(prop_val[0], "show_path")) {
156 _show_path = pref_str_to_bool(prop_val[1]);
157 } else if (!g_ascii_strcasecmp(prop_val[0], "show_footprints")) {
158 _show_footprints = pref_str_to_bool(prop_val[1]);
159 } else if (!g_ascii_strcasecmp(prop_val[0], "show_destroy")) {
160 _show_destroy = pref_str_to_bool(prop_val[1]);
161 } else if (!g_ascii_strcasecmp(prop_val[0], "show_highlight")) {
162 _show_highlight = pref_str_to_bool(prop_val[1]);
163 } else if (!g_ascii_strcasecmp(prop_val[0], "theme_name")) {
164 if (_theme_name) {
165 g_free(_theme_name);
166 }
167 _theme_name = g_strdup(prop_val[1]);
168 } else if (!g_ascii_strcasecmp(prop_val[0], "rules_xsize")) {
169 rules_set_width(atoi(prop_val[1]));
170 } else if (!g_ascii_strcasecmp(prop_val[0], "rules_ysize")) {
171 rules_set_height(atoi(prop_val[1]));
172 } else if (!g_ascii_strcasecmp(prop_val[0], "rules_colors")) {
173 rules_set_colors(atoi(prop_val[1]));
174 } else if (!g_ascii_strcasecmp(prop_val[0], "rules_next")) {
175 rules_set_next(atoi(prop_val[1]));
176 } else if (!g_ascii_strcasecmp(prop_val[0], "rules_destroy")) {
177 rules_set_destroy(atoi(prop_val[1]));
178 } else if (!g_ascii_strcasecmp(prop_val[0], "time_limit")) {
179 timer_set_limit(atoi(prop_val[1]));
180 } else if (!g_ascii_strcasecmp(prop_val[0], "highlight_dr")) {
181 prefs_set_hl_dr(atoi(prop_val[1]));
182 } else if (!g_ascii_strcasecmp(prop_val[0], "highlight_dg")) {
183 prefs_set_hl_dg(atoi(prop_val[1]));
184 } else if (!g_ascii_strcasecmp(prop_val[0], "highlight_db")) {
185 prefs_set_hl_db(atoi(prop_val[1]));
186 }
187 }
188 g_strfreev(prop_val);
189 }
190 fclose(fp);
191 }
192 g_free(rc_file);
193 }
194
195 /* writes ``property=value\n'' to fp.
196 why i made it separate function? for "feature purposes" =) */
197 void write_pref_string(FILE *fp, gchar *property, gchar *value) {
198 fprintf (fp, "%s=%s\n", property, value);
199 }
200
201 void write_pref_int(FILE *fp, gchar *property, gint value) {
202 fprintf (fp, "%s=%d\n", property, value);
203 }
204
205 gchar *save_preferences (void)
206 {
207 FILE *fp;
208 gchar *rc_file/*, *err*/;
209 gchar *ret = NULL;
210
211 rc_file = get_config_dir_file (CONFIG_FILE_NAME);
212 if ((fp = fopen(rc_file, "w"))) {
213 write_pref_string(fp, "show_hints", pref_bool_to_str(_show_next_colors));
214 write_pref_string(fp, "show_path", pref_bool_to_str(_show_path));
215 write_pref_string(fp, "show_footprints", pref_bool_to_str(_show_footprints));
216 write_pref_string(fp, "show_destroy", pref_bool_to_str(_show_destroy));
217 write_pref_string(fp, "show_highlight", pref_bool_to_str(_show_highlight));
218 write_pref_string(fp, "theme_name", _theme_name);
219 write_pref_int(fp, "rules_xsize", rules_get_width());
220 write_pref_int(fp, "rules_ysize", rules_get_height());
221 write_pref_int(fp, "rules_colors", rules_get_colors());
222 write_pref_int(fp, "rules_next", rules_get_next());
223 write_pref_int(fp, "rules_destroy", rules_get_destroy());
224 write_pref_int(fp, "time_limit", timer_get_limit());
225 write_pref_int(fp, "highlight_dr", prefs_get_hl_dr());
226 write_pref_int(fp, "highlight_dg", prefs_get_hl_dg());
227 write_pref_int(fp, "highlight_db", prefs_get_hl_db());
228 fclose(fp);
229 } else {
230 ret = g_strdup_printf(_("Can't write to %s: %s"), rc_file, strerror(errno));
231 }
232 g_free(rc_file);
233 return ret;
234 }