]> git.deb.at Git - pkg/abook.git/blob - options.c
7 -> phone in rcfile
[pkg/abook.git] / options.c
1
2 /*
3  * $Id$
4  *
5  * by JH <jheinonen@users.sourceforge.net>
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
84 #if 1
85 extern bool alternative_rcfile;
86 #endif
87
88 void
89 close_config()
90 {
91 #if 1
92         if(!alternative_rcfile)
93                 save_options();
94 #endif
95
96         conff_free_nodes(abook_config);
97 }
98
99 static int
100 rcfile_exist()
101 {
102         return ( (0 == access(SYSWIDE_RCFILE, F_OK)) ||
103                         (0 == access(rcfile, F_OK)) );
104 }
105
106 void
107 load_options()
108 {
109         int ret;
110         
111         if( (ret = conff_load_file(&abook_config, rcfile,
112                                 REPLACE_KEY)) > 0) {
113                 fprintf(stderr, "%s: parse error at line %d\n", rcfile, ret);
114                 exit(1);
115         }
116
117         if( (ret = conff_load_file(&abook_config, SYSWIDE_RCFILE,
118                                         DONT_REPLACE_KEY )) > 0) {
119                 fprintf(stderr, "%s: parse error at line %d\n",
120                                 SYSWIDE_RCFILE, ret);
121                 exit(1);
122         }
123 }
124
125 #if 1
126 void
127 save_options()
128 {
129         if( rcfile_exist() ) /* don't overwrite existing config */
130                 return;
131
132         conff_save_file(abook_config, rcfile);
133 }
134 #endif
135
136 static void
137 options_add_key(char *key, char *value)
138 {
139         const int flags = DONT_REPLACE_KEY;
140
141         conff_add_key(&abook_config, key, value, flags);
142 }
143
144 static void
145 default_options()
146 {
147         options_add_key("autosave", "true");
148
149         options_add_key("show_all_emails", "true");
150         options_add_key("emailpos", "25");
151         options_add_key("extra_column", "phone");
152         options_add_key("extra_alternative", "-1");
153         options_add_key("extrapos", "65");
154
155         options_add_key("mutt_command", "mutt");
156         options_add_key("mutt_return_all_emails", "true");
157
158         options_add_key("print_command", "lpr");
159
160         options_add_key("filesel_sort", "false");
161
162         options_add_key("www_command", "lynx");
163
164         options_add_key("address_style", "eu");
165
166         options_add_key("use_ascii_only", "false");
167 }