]> git.deb.at Git - pkg/abook.git/blob - options.c
Ids added
[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 <unistd.h>
14 #include "abook_curses.h"
15 #include "abook.h"
16 #include "options.h"
17 #ifdef HAVE_CONFIG_H
18 #       include "config.h"
19 #endif
20
21 struct conff_node *abook_config;
22
23 static int      rcfile_exist();
24 static void     default_options();
25
26 extern char *rcfile;
27
28 static char *
29 abook_opt_conff_get_val(char *key)
30 {
31         int tried;
32         char *value = NULL;
33
34
35         for(tried = 0; tried < 2; ) {
36                 if( ( value = conff_get_value(abook_config, key) )
37                                 == 0 ) {
38                         tried ++;
39                         default_options(); /* try with defaults */
40                 } else
41                         return value;
42         }
43         return NULL;
44 }
45
46 int
47 options_get_int(char *key)
48 {
49         char *value;
50         int ret;
51         
52         if( ( value = abook_opt_conff_get_val(key) )
53                         == NULL)
54                 return 1;
55
56         if( !strcasecmp(value, "true") )
57                 ret = 1;
58         else
59         if( !strcasecmp(value, "false") )
60                 ret = 0;
61         else
62                 ret = safe_atoi(value);
63         
64         return ret;
65 }
66         
67 char *
68 options_get_str(char *key)
69 {
70         return abook_opt_conff_get_val(key); 
71 }
72                 
73 void
74 init_options()
75 {
76         abook_config = NULL;
77
78         if( rcfile_exist() )
79                 load_options();
80         else
81                 default_options();
82 }
83
84 void
85 close_config()
86 {
87         save_options();
88
89         conff_free_nodes(abook_config);
90 }
91
92 void
93 edit_options()
94 {
95         /*
96          * not yet implemented
97          * edit your ~/.abook.conf manually
98          */
99 }
100
101 static int
102 rcfile_exist()
103 {
104         return ( (0 == access(SYSWIDE_RCFILE, F_OK)) ||
105                         (0 == access(rcfile, F_OK)) );
106 }
107
108 void
109 load_options()
110 {
111         int ret;
112         
113         if( (ret = conff_load_file(&abook_config, rcfile,
114                                 REPLACE_KEY)) > 0) {
115                 fprintf(stderr, "%s: parse error at line %d\n", rcfile, ret);
116                 exit(1);
117         }
118
119         if( (ret = conff_load_file(&abook_config, SYSWIDE_RCFILE,
120                                         DONT_REPLACE_KEY )) > 0) {
121                 fprintf(stderr, "%s: parse error at line %d\n",
122                                 SYSWIDE_RCFILE, ret);
123                 exit(1);
124         }
125 }
126
127 void
128 save_options()
129 {
130         if( rcfile_exist() ) /* don't overwrite existing config */
131                 return;
132
133         conff_save_file(abook_config, rcfile);
134 }
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", "7");
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 }