]> git.deb.at Git - pkg/abook.git/blob - views.c
Bump Standards-Version to 3.9.3
[pkg/abook.git] / views.c
1 /*
2  * $Id: views.c,v 1.2 2006/08/07 15:06:53 cduval Exp $
3  *
4  * by Cedric Duval <cedricduval@free.fr>
5  *
6  * Copyright (C) Cedric Duval
7  *
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <assert.h>
14 #ifdef HAVE_CONFIG_H
15 #       include "config.h"
16 #endif
17 #include "gettext.h"
18 #include "misc.h"
19 #include "options.h"
20 #include "views.h"
21 #include "xmalloc.h"
22
23
24 abook_view *abook_views = NULL;
25 int views_count = 0;
26
27
28 extern abook_field standard_fields[];
29
30
31 static abook_view *
32 find_view(char *name)
33 {
34         abook_view *cur = abook_views;
35
36         for(; cur; cur = cur->next)
37                 if(0 == strcasecmp(cur->name, name))
38                         return cur;
39
40         return NULL;
41 }
42
43 static abook_view *
44 create_view(char *name) {
45         abook_view *v;
46
47         for(v = abook_views; v && v->next; v = v->next)
48                 ;
49
50         if(v) {
51                 v->next = xmalloc(sizeof(abook_view));
52                 v = v->next;            
53         } else
54                 abook_views = v = xmalloc(sizeof(abook_view));
55
56         v->name = xstrdup(name);
57         v->fields = NULL;
58         v->next = NULL;
59
60         views_count++;
61
62         return v;
63 }
64
65 static int
66 fields_in_view(abook_view *view)
67 {
68         int nb;
69         abook_field_list *f;
70
71         for(nb = 0, f = view->fields; f; f = f->next, nb++)
72                 ;
73
74         return nb;
75 }
76
77 char *
78 add_field_to_view(char *viewname, char *field)
79 {
80         abook_view *v;
81         abook_field *f;
82
83         if(
84                         !(f = find_declared_field(field)) &&
85                         !(f = find_standard_field(field, 1 /*do_declare*/))
86                 )
87                 return _("undeclared field");
88         
89         if((v = find_view(viewname)) == NULL)
90                 v = create_view(viewname);
91         else if(fields_in_view(v) == MAX_VIEW_FIELDS)
92                 return _("maximal number of fields per view reached");
93
94         if(v->fields && (find_field(field, v->fields)))
95                 return _("field already in this view");
96
97         add_field(&v->fields, f);
98         
99         return NULL;
100 }
101
102 void
103 view_info(int number, char **name, abook_field_list **fields)
104 {       int i = 0;
105         abook_view *cur = abook_views;
106
107         assert((number < views_count) && (number >= 0));
108         
109         while(i++ != number)
110                 cur = cur->next;
111
112         if(fields)
113                 *fields = cur->fields;
114
115         if(name)
116                 *name = cur->name;
117 }
118
119 #define MAX_DEFAULT_FIELDS_PER_VIEW 6
120
121 void
122 init_default_views()
123 {
124         char *str;
125         int i, j, add_custom_fields, add_custom_view = 0;
126         
127         add_custom_fields =
128                 !strcasecmp(opt_get_str(STR_PRESERVE_FIELDS), "standard");
129
130         /* if the user has configured views, no need to provide defaults */
131         if(abook_views)
132                 goto out;
133         add_custom_view = 1;
134
135         struct {
136                 char *name;
137                 int fields[MAX_DEFAULT_FIELDS_PER_VIEW + 1];
138         } default_views[] = {
139                 { N_("CONTACT"), {NAME, EMAIL, -1} },
140                 { N_("ADDRESS"),
141                         { ADDRESS, ADDRESS2, CITY, STATE, ZIP, COUNTRY, -1 } },
142                 { N_("PHONE"), { PHONE, WORKPHONE, FAX, MOBILEPHONE, -1 } },
143                 { N_("OTHER"), { NICK, URL, NOTES, -1 } },
144                 { 0 }
145         };
146
147         for(i = 0; default_views[i].name; i++) {
148                 for(j = 0; j < MAX_DEFAULT_FIELDS_PER_VIEW; j++) {
149                         if(default_views[i].fields[j] == -1)
150                                 break;
151                         str = standard_fields[default_views[i].fields[j]].key;
152                         add_field_to_view(gettext(default_views[i].name), str);
153                 }
154         }
155 out:
156
157 #define init_view(view, key, name) do { \
158         if(add_custom_fields || add_custom_view) \
159                 declare_new_field(key, name, "string", \
160                                 0 /*"standard" field already declared above*/);\
161         if(add_custom_view) \
162                 add_field_to_view(view, key); \
163 } while(0);
164
165         init_view(_("CUSTOM"), "custom1", _("Custom1"));
166         init_view(_("CUSTOM"), "custom2", _("Custom2"));
167         init_view(_("CUSTOM"), "custom3", _("Custom3"));
168         init_view(_("CUSTOM"), "custom4", _("Custom4"));
169         init_view(_("CUSTOM"), "custom5", _("Custom5"));
170 }