Syncing to 0.8.1
[geeqie.git] / src / rcfile.c
1 /*
2  * GQview image viewer
3  * (C)2000 John Ellis
4  *
5  * Author: John Ellis
6  *
7  */
8
9 #include "gqview.h"
10
11 static gchar *quoted_value(gchar *text);
12 static void write_char_option(FILE *f, gchar *label, gchar *text);
13 static gchar *read_char_option(FILE *f, gchar *option, gchar *label, gchar *value, gchar *text);
14 static void write_int_option(FILE *f, gchar *label, gint n);
15 static gint read_int_option(FILE *f, gchar *option, gchar *label, gchar *value, gint n);
16 static void write_bool_option(FILE *f, gchar *label, gint n);
17 static gint read_bool_option(FILE *f, gchar *option, gchar *label, gchar *value, gint n);
18
19 /*
20  *-----------------------------------------------------------------------------
21  * line write/parse routines (private)
22  *-----------------------------------------------------------------------------
23  */ 
24
25 static gchar *quoted_value(gchar *text)
26 {
27         gchar *ptr;
28         gint c = 0;
29         gint l = strlen(text);
30
31         if (l == 0) return NULL;
32
33         while (c < l && text[c] !='"') c++;
34         if (text[c] == '"')
35                 {
36                 c++;
37                 ptr = text + c;
38                 while (c < l && text[c] !='"') c++;
39                 if (text[c] == '"')
40                         {
41                         text[c] = '\0';
42                         if (strlen(ptr) > 0)
43                                 {
44                                 return g_strdup(ptr);
45                                 }
46                         }
47                 }
48         else
49                 /* for compatibility with older formats (<0.3.7)
50                  * read a line without quotes too */
51                 {
52                 c = 0;
53                 while (c < l && text[c] !=' ' && text[c] !=8 && text[c] != '\n') c++;
54                 if (c != 0)
55                         {
56                         text[c] = '\0';
57                         return g_strdup(text);
58                         }
59                 }
60
61         return NULL;
62 }
63
64 static void write_char_option(FILE *f, gchar *label, gchar *text)
65 {
66         if (text)
67                 fprintf(f,"%s: \"%s\"\n", label, text);
68         else
69                 fprintf(f,"%s: \n", label);
70 }
71
72 static gchar *read_char_option(FILE *f, gchar *option, gchar *label, gchar *value, gchar *text)
73 {
74         if (!strcasecmp(option, label))
75                 {
76                 g_free(text);
77                 text = quoted_value(value);
78                 }
79         return text;
80 }
81
82 static void write_int_option(FILE *f, gchar *label, gint n)
83 {
84         fprintf(f,"%s: %d\n\n", label, n);
85 }
86
87 static gint read_int_option(FILE *f, gchar *option, gchar *label, gchar *value, gint n)
88 {
89         if (!strcasecmp(option, label))
90                 {
91                 n = strtol(value,NULL,0);
92                 }
93         return n;
94 }
95
96 static void write_bool_option(FILE *f, gchar *label, gint n)
97 {
98         fprintf(f,"%s: ", label);
99         if (n) fprintf(f,"true\n"); else fprintf(f,"false\n");
100         fprintf(f,"\n");
101 }
102
103 static gint read_bool_option(FILE *f, gchar *option, gchar *label, gchar *value, gint n)
104 {
105         if (!strcasecmp(option, label))
106                 {
107                 if (!strcasecmp(value, "true"))
108                         n = TRUE;
109                 else
110                         n = FALSE;
111                 }
112         return n;
113 }
114
115 /*
116  *-----------------------------------------------------------------------------
117  * save configuration (public)
118  *-----------------------------------------------------------------------------
119  */ 
120
121 void save_options()
122 {
123         FILE *f;
124         gchar *rc_path;
125         gint i;
126
127         rc_path = g_strconcat(homedir(), "/", RC_FILE_NAME, NULL);
128
129         f = fopen(rc_path,"w");
130         if (!f)
131                 {
132                 printf(_("error saving config file: %s\n"), rc_path);
133                 g_free(rc_path);
134                 return;
135                 }
136
137         fprintf(f,"######################################################################\n");
138         fprintf(f,"#                         GQview config file         version %7s #\n", VERSION);
139         fprintf(f,"#                                                                    #\n");
140         fprintf(f,"#  Everything in this file can be changed in the option dialogs.     #\n");
141         fprintf(f,"#      (so there should be no need to edit this file by hand)        #\n");
142         fprintf(f,"#                                                                    #\n");
143         fprintf(f,"######################################################################\n");
144         fprintf(f,"\n");
145         fprintf(f,"##### General Options #####\n\n");
146
147         write_bool_option(f, "enable_startup_path", startup_path_enable);
148         write_char_option(f, "startup_path", startup_path);
149         fprintf(f,"\n");
150
151         fprintf(f,"zoom_mode: ");
152         if (zoom_mode == ZOOM_RESET_ORIGINAL) fprintf(f,"original\n");
153         if (zoom_mode == ZOOM_RESET_FIT_WINDOW) fprintf(f,"fit\n");
154         if (zoom_mode == ZOOM_RESET_NONE) fprintf(f,"dont_change\n");
155         fprintf(f,"\n");
156
157         write_bool_option(f, "fit_window_to_image", fit_window);
158         write_bool_option(f, "limit_window_size", limit_window_size);
159         write_int_option(f, "max_window_size", max_window_size);
160         fprintf(f,"\n");
161
162         write_bool_option(f, "progressive_keyboard_scrolling", progressive_key_scrolling);
163         fprintf(f,"\n");
164
165         write_bool_option(f, "enable_thumbnails", thumbnails_enabled);
166         write_int_option(f, "thumbnail_width", thumb_max_width);
167         write_int_option(f, "thumbnail_height", thumb_max_height);
168         write_bool_option(f, "cache_thumbnails", enable_thumb_caching);
169         write_bool_option(f, "use_xvpics_thumbnails", use_xvpics_thumbnails);
170         fprintf(f,"\n");
171
172         write_bool_option(f, "confirm_delete", confirm_delete);
173         fprintf(f,"\n");
174         write_bool_option(f, "tools_float", tools_float);
175         write_bool_option(f, "tools_hidden", tools_hidden);
176         write_bool_option(f, "restore_tool_state", restore_tool);
177
178         write_bool_option(f, "mouse_wheel_scrolls", mousewheel_scrolls);
179
180         fprintf(f,"\n##### Slideshow Options #####\n\n");
181
182         write_int_option(f, "slideshow_delay", slideshow_delay);
183
184         write_bool_option(f, "slideshow_random", slideshow_random);
185         write_bool_option(f, "slideshow_repeat", slideshow_repeat);
186
187         fprintf(f,"\n##### Filtering Options #####\n\n");
188
189         write_bool_option(f, "show_dotfiles", show_dot_files);
190         write_bool_option(f, "disable_filtering", file_filter_disable);
191         fprintf(f,"\n");
192         write_bool_option(f, "filter_ppm", filter_include_ppm);
193         write_bool_option(f, "filter_png", filter_include_png);
194         write_bool_option(f, "filter_jpg", filter_include_jpg);
195         write_bool_option(f, "filter_tif", filter_include_tif);
196         write_bool_option(f, "filter_pgm", filter_include_pgm);
197         write_bool_option(f, "filter_xpm", filter_include_xpm);
198         write_bool_option(f, "filter_gif", filter_include_gif);
199         write_bool_option(f, "filter_pcx", filter_include_pcx);
200         write_bool_option(f, "filter_bmp", filter_include_bmp);
201         fprintf(f,"\n");
202         write_char_option(f, "custom_filter", custom_filter);
203
204         fprintf(f,"\n##### External Programs #####\n");
205         fprintf(f,"# Maximum of 8 programs (external_1 through external 8)\n");
206         fprintf(f,"# format: external_n: \"menu name\" \"command line\"\n\n");
207
208         for (i=0; i<8; i++)
209                 {
210                 fprintf(f,"external_%d: \"", i+1);
211                 if (editor_name[i]) fprintf(f, editor_name[i]);
212                 fprintf(f, "\" \"");
213                 if (editor_command[i]) fprintf(f, editor_command[i]);
214                 fprintf(f, "\"\n");
215                 }
216
217         fprintf(f,"\n##### Window Positions #####\n\n");
218
219         write_bool_option(f, "restore_window_positions", save_window_positions);
220         fprintf(f,"\n");
221         write_int_option(f, "main_window_x", main_window_x);
222         write_int_option(f, "main_window_y", main_window_y);
223         write_int_option(f, "main_window_width", main_window_w);
224         write_int_option(f, "main_window_height", main_window_h);
225         write_int_option(f, "float_window_x", float_window_x);
226         write_int_option(f, "float_window_y", float_window_y);
227         write_int_option(f, "float_window_width", float_window_w);
228         write_int_option(f, "float_window_height", float_window_h);
229
230         fprintf(f,"######################################################################\n");
231         fprintf(f,"#                      end of GQview config file                     #\n");
232         fprintf(f,"######################################################################\n");
233
234         fclose(f);
235
236         g_free(rc_path);
237 }
238
239 /*
240  *-----------------------------------------------------------------------------
241  * load configuration (public)
242  *-----------------------------------------------------------------------------
243  */ 
244
245 void load_options()
246 {
247         FILE *f;
248         gchar *rc_path;
249         gchar s_buf[1024];
250         gchar *s_buf_ptr;
251         gchar option[1024];
252         gchar value[1024];
253         gchar value_all[1024];
254         gint c,l,i;
255         rc_path = g_strconcat(homedir(), "/", RC_FILE_NAME, NULL);
256
257         f = fopen(rc_path,"r");
258         if (!f)
259                 {
260                 g_free(rc_path);
261                 return;
262                 }
263
264         while (fgets(s_buf,1024,f))
265                 {
266                 if (s_buf[0]=='#') continue;
267                 if (s_buf[0]=='\n') continue;
268                 c = 0;
269                 l = strlen(s_buf);
270                 while (s_buf[c] != ':' && c < l) c++;
271                 if (c >= l) continue;
272                 s_buf[c] = '\0';
273                 c++;
274                 while (s_buf[c] == ' ' && c < l) c++;
275                 while (s_buf[c] == 8 && c < l) c++;
276                 while (s_buf[c] == ' ' && c < l) c++;
277                 s_buf_ptr = s_buf + c;
278                 strcpy(value_all,s_buf_ptr);
279                 while (s_buf[c] != 8 && s_buf[c] != ' ' && s_buf[c] != '\n' && c < l) c++;
280                 s_buf[c] = '\0';
281                 strcpy(option,s_buf);
282                 strcpy(value,s_buf_ptr);
283
284                 /* general options */
285
286                 startup_path_enable = read_bool_option(f, option,
287                         "enable_startup_path", value, startup_path_enable);
288                 startup_path = read_char_option(f, option,
289                         "startup_path", value_all, startup_path);
290
291                 if (!strcasecmp(option,"zoom_mode"))
292                         {
293                         if (!strcasecmp(value,"original")) zoom_mode = ZOOM_RESET_ORIGINAL;
294                         if (!strcasecmp(value,"fit")) zoom_mode = ZOOM_RESET_FIT_WINDOW;
295                         if (!strcasecmp(value,"dont_change")) zoom_mode = ZOOM_RESET_NONE;
296                         }
297
298                 fit_window = read_bool_option(f, option,
299                         "fit_window_to_image", value, fit_window);
300                 limit_window_size = read_bool_option(f, option,
301                         "limit_window_size", value, limit_window_size);
302                 max_window_size = read_int_option(f, option,
303                         "max_window_size", value, max_window_size);
304                 progressive_key_scrolling = read_bool_option(f, option,
305                         "progressive_keyboard_scrolling", value, progressive_key_scrolling);
306
307                 thumbnails_enabled = read_bool_option(f, option,
308                         "enable_thumbnails", value, thumbnails_enabled);
309                 thumb_max_width = read_int_option(f, option,
310                         "thumbnail_width", value, thumb_max_width);
311                 thumb_max_height = read_int_option(f, option,
312                         "thumbnail_height", value, thumb_max_height);
313                 enable_thumb_caching = read_bool_option(f, option,
314                         "cache_thumbnails", value, enable_thumb_caching);
315                 use_xvpics_thumbnails = read_bool_option(f, option,
316                         "use_xvpics_thumbnails", value, use_xvpics_thumbnails);
317
318                 confirm_delete = read_bool_option(f, option,
319                         "confirm_delete", value, confirm_delete);
320
321                 tools_float = read_bool_option(f, option,
322                         "tools_float", value, tools_float);
323                 tools_hidden = read_bool_option(f, option,
324                         "tools_hidden", value, tools_hidden);
325                 restore_tool = read_bool_option(f, option,
326                         "restore_tool_state", value, restore_tool);
327
328                 mousewheel_scrolls = read_bool_option(f, option,
329                         "mouse_wheel_scrolls", value, mousewheel_scrolls);
330
331
332                 /* slideshow opitons */
333
334                 slideshow_delay = read_int_option(f, option,
335                         "slideshow_delay", value, slideshow_delay);
336                 slideshow_random = read_bool_option(f, option,
337                         "slideshow_random", value, slideshow_random);
338                 slideshow_repeat = read_bool_option(f, option,
339                         "slideshow_repeat", value, slideshow_repeat);
340
341                 /* filtering options */
342
343                 show_dot_files = read_bool_option(f, option,
344                         "show_dotfiles", value, show_dot_files);
345                 file_filter_disable = read_bool_option(f, option,
346                         "disable_filtering", value, file_filter_disable);
347
348                 filter_include_ppm = read_bool_option(f, option,
349                         "filter_ppm", value, filter_include_ppm);
350                 filter_include_png = read_bool_option(f, option,
351                         "filter_png", value, filter_include_png);
352                 filter_include_jpg = read_bool_option(f, option,
353                         "filter_jpg", value, filter_include_jpg);
354                 filter_include_tif = read_bool_option(f, option,
355                         "filter_tif", value, filter_include_tif);
356                 filter_include_pgm = read_bool_option(f, option,
357                         "filter_pgm", value, filter_include_pgm);
358                 filter_include_xpm = read_bool_option(f, option,
359                         "filter_xpm", value, filter_include_xpm);
360                 filter_include_gif = read_bool_option(f, option,
361                         "filter_gif", value, filter_include_gif);
362                 filter_include_pcx = read_bool_option(f, option,
363                         "filter_pcx", value, filter_include_pcx);
364                 filter_include_bmp = read_bool_option(f, option,
365                         "filter_bmp", value, filter_include_bmp);
366
367                 custom_filter = read_char_option(f, option,
368                         "custom_filter", value_all, custom_filter);
369
370                 /* External Programs */
371
372                 if (!strncasecmp(option,"external_",9))
373                         {
374                         i = strtol(option + 9, NULL, 0);
375                         if (i>0 && i<9)
376                                 {
377                                 gchar *ptr1, *ptr2;
378                                 i--;
379                                 c = 0;
380                                 l = strlen(value_all);
381                                 ptr1 = value_all;
382
383                                 g_free(editor_name[i]);
384                                 editor_name[i] = NULL;
385                                 g_free(editor_command[i]);
386                                 editor_command[i] = NULL;
387
388                                 while (c<l && value_all[c] !='"') c++;
389                                 if (ptr1[c] == '"')
390                                         {
391                                         c++;
392                                         ptr2 = ptr1 + c;
393                                         while (c<l && value_all[c] !='"') c++;
394                                         if (ptr1[c] == '"')
395                                                 {
396                                                 ptr1[c] = '\0';
397                                                 if (ptr1 + c - 1 != ptr2)
398                                                         editor_name[i] = g_strdup(ptr2);
399                                                 c++;
400                                                 while (c<l && value_all[c] !='"') c++;
401                                                 if (ptr1[c] == '"')
402                                                         {
403                                                         c++;
404                                                         ptr2 = ptr1 + c;
405                                                         while (c<l && value_all[c] !='"') c++;
406                                                         if (ptr1[c] == '"' && ptr1 + c - 1 != ptr2)
407                                                                 {
408                                                                 ptr1[c] = '\0';
409                                                                 editor_command[i] = g_strdup(ptr2);
410                                                                 }
411                                                         }
412                                                 }
413                                         }
414                                 }
415                         }
416
417                 /* window positions */
418
419                 save_window_positions = read_bool_option(f, option,
420                         "restore_window_positions", value, save_window_positions);
421
422                 main_window_x = read_int_option(f, option,
423                         "main_window_x", value, main_window_x);
424                 main_window_y = read_int_option(f, option,
425                         "main_window_y", value, main_window_y);
426                 main_window_w = read_int_option(f, option,
427                         "main_window_width", value, main_window_w);
428                 main_window_h = read_int_option(f, option,
429                         "main_window_height", value, main_window_h);
430                 float_window_x = read_int_option(f, option,
431                         "float_window_x", value, float_window_x);
432                 float_window_y = read_int_option(f, option,
433                         "float_window_y", value, float_window_y);
434                 float_window_w = read_int_option(f, option,
435                         "float_window_width", value, float_window_w);
436                 float_window_h = read_int_option(f, option,
437                         "float_window_height", value, float_window_h);
438                 }
439
440         fclose(f);
441         g_free(rc_path);
442 }
443