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
24 abook_view *abook_views = NULL;
28 extern abook_field standard_fields[];
34 abook_view *cur = abook_views;
36 for(; cur; cur = cur->next)
37 if(0 == strcasecmp(cur->name, name))
44 create_view(char *name) {
47 for(v = abook_views; v && v->next; v = v->next)
51 v->next = xmalloc(sizeof(abook_view));
54 abook_views = v = xmalloc(sizeof(abook_view));
56 v->name = xstrdup(name);
66 fields_in_view(abook_view *view)
71 for(nb = 0, f = view->fields; f; f = f->next, nb++)
78 add_field_to_view(char *viewname, char *field)
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);
103 view_info(int number, char **name, abook_field_list **fields)
105 abook_view *cur = abook_views;
107 assert((number < views_count) && (number >= 0));
113 *fields = cur->fields;
119 #define MAX_DEFAULT_FIELDS_PER_VIEW 6
125 int i, j, add_custom_fields, add_custom_view = 0;
128 !strcasecmp(opt_get_str(STR_PRESERVE_FIELDS), "standard");
130 /* if the user has configured views, no need to provide defaults */
137 int fields[MAX_DEFAULT_FIELDS_PER_VIEW + 1];
138 } default_views[] = {
139 { N_("CONTACT"), {NAME, EMAIL, -1} },
141 { ADDRESS, ADDRESS2, CITY, STATE, ZIP, COUNTRY, -1 } },
142 { N_("PHONE"), { PHONE, WORKPHONE, FAX, MOBILEPHONE, -1 } },
143 { N_("OTHER"), { NICK, URL, NOTES, -1 } },
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)
151 str = standard_fields[default_views[i].fields[j]].key;
152 add_field_to_view(gettext(default_views[i].name), str);
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); \
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"));