use automake 1.11, autoconf 2.65
[abook.git] / views.c
blob5d6cc654c4a88cc58c2861a793a3a5445457c8d1
1 /*
2 * $Id: views.c,v 1.2 2006/08/07 15:06:53 cduval Exp $
4 * by Cedric Duval <cedricduval@free.fr>
6 * Copyright (C) Cedric Duval
8 */
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"
24 abook_view *abook_views = NULL;
25 int views_count = 0;
28 extern abook_field standard_fields[];
31 static abook_view *
32 find_view(char *name)
34 abook_view *cur = abook_views;
36 for(; cur; cur = cur->next)
37 if(0 == strcasecmp(cur->name, name))
38 return cur;
40 return NULL;
43 static abook_view *
44 create_view(char *name) {
45 abook_view *v;
47 for(v = abook_views; v && v->next; v = v->next)
50 if(v) {
51 v->next = xmalloc(sizeof(abook_view));
52 v = v->next;
53 } else
54 abook_views = v = xmalloc(sizeof(abook_view));
56 v->name = xstrdup(name);
57 v->fields = NULL;
58 v->next = NULL;
60 views_count++;
62 return v;
65 static int
66 fields_in_view(abook_view *view)
68 int nb;
69 abook_field_list *f;
71 for(nb = 0, f = view->fields; f; f = f->next, nb++)
74 return nb;
77 char *
78 add_field_to_view(char *viewname, char *field)
80 abook_view *v;
81 abook_field *f;
83 if(
84 !(f = find_declared_field(field)) &&
85 !(f = find_standard_field(field, 1 /*do_declare*/))
87 return _("undeclared field");
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");
94 if(v->fields && (find_field(field, v->fields)))
95 return _("field already in this view");
97 add_field(&v->fields, f);
99 return NULL;
102 void
103 view_info(int number, char **name, abook_field_list **fields)
104 { int i = 0;
105 abook_view *cur = abook_views;
107 assert((number < views_count) && (number >= 0));
109 while(i++ != number)
110 cur = cur->next;
112 if(fields)
113 *fields = cur->fields;
115 if(name)
116 *name = cur->name;
119 #define MAX_DEFAULT_FIELDS_PER_VIEW 6
121 void
122 init_default_views()
124 char *str;
125 int i, j, add_custom_fields, add_custom_view = 0;
127 add_custom_fields =
128 !strcasecmp(opt_get_str(STR_PRESERVE_FIELDS), "standard");
130 /* if the user has configured views, no need to provide defaults */
131 if(abook_views)
132 goto out;
133 add_custom_view = 1;
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 }
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);
155 out:
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);
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"));