]> git.deb.at Git - pkg/abook.git/blob - options.c
Merged first 0.4.13pre patch
[pkg/abook.git] / options.c
1
2 /*
3  * $Id$
4  *
5  * by JH <jheinonen@bigfoot.com>
6  *
7  * Copyright (C) Jaakko Heinonen
8  */
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include "abook_curses.h"
14 #include "abook.h"
15 #include "options.h"
16 #ifdef HAVE_CONFIG_H
17 #       include "config.h"
18 #endif
19
20 struct conff_node *abook_config;
21
22 static int      rcfile_exist();
23 static void     default_options();
24
25 extern char *rcfile;
26
27 static char *
28 abook_opt_conff_get_val(char *key)
29 {
30         int tried;
31         char *value = NULL;
32
33
34         for(tried = 0; tried < 2; ) {
35                 if( ( value = conff_get_value(abook_config, key) )
36                                 == 0 ) {
37                         tried ++;
38                         default_options(); /* try with defaults */
39                 } else
40                         return value;
41         }
42         return NULL;
43 }
44
45 int
46 options_get_int(char *key)
47 {
48         char *value;
49         int ret;
50         
51         if( ( value = abook_opt_conff_get_val(key) )
52                         == NULL)
53                 return 1;
54
55         if( !strcasecmp(value, "true") )
56                 ret = 1;
57         else
58         if( !strcasecmp(value, "false") )
59                 ret = 0;
60         else
61                 ret = safe_atoi(value);
62         
63         return ret;
64 }
65         
66 char *
67 options_get_str(char *key)
68 {
69         return abook_opt_conff_get_val(key); 
70 }
71                 
72 void
73 init_options()
74 {
75         abook_config = NULL;
76
77         if( rcfile_exist() )
78                 load_options();
79         else
80                 default_options();
81 }
82
83 void
84 close_config()
85 {
86         save_options();
87
88         conff_free_nodes(abook_config);
89 }
90
91 static int
92 rcfile_exist()
93 {
94         return ( (0 == access(SYSWIDE_RCFILE, F_OK)) ||
95                         (0 == access(rcfile, F_OK)) );
96 }
97
98 void
99 load_options()
100 {
101         int ret;
102         
103         if( (ret = conff_load_file(&abook_config, rcfile,
104                                 REPLACE_KEY)) > 0) {
105                 fprintf(stderr, "%s: parse error at line %d\n", rcfile, ret);
106                 exit(1);
107         }
108
109         if( (ret = conff_load_file(&abook_config, SYSWIDE_RCFILE,
110                                         DONT_REPLACE_KEY )) > 0) {
111                 fprintf(stderr, "%s: parse error at line %d\n",
112                                 SYSWIDE_RCFILE, ret);
113                 exit(1);
114         }
115 }
116
117 void
118 save_options()
119 {
120         if( rcfile_exist() ) /* don't overwrite existing config */
121                 return;
122
123         conff_save_file(abook_config, rcfile);
124 }
125
126 static void
127 options_add_key(char *key, char *value)
128 {
129         const int flags = DONT_REPLACE_KEY;
130
131         conff_add_key(&abook_config, key, value, flags);
132 }
133
134 static void
135 default_options()
136 {
137         options_add_key("autosave", "true");
138
139         options_add_key("show_all_emails", "true");
140         options_add_key("emailpos", "25");
141         options_add_key("extra_column", "7");
142         options_add_key("extra_alternative", "-1");
143         options_add_key("extrapos", "65");
144
145         options_add_key("mutt_command", "mutt");
146         options_add_key("mutt_return_all_emails", "true");
147
148         options_add_key("print_command", "lpr");
149
150         options_add_key("filesel_sort", "false");
151
152         options_add_key("www_command", "lynx");
153
154         options_add_key("address_style", "eu");
155 }