5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
28 list_item *database = NULL;
32 #define INITIAL_LIST_CAPACITY 30
34 static int list_capacity = 0;
36 extern int first_list_item;
38 extern char *selected;
40 extern char *datafile;
49 * notes about adding fields:
50 * - do not change any fields in TAB_CONTACT
51 * - do not add fields to contact tab
52 * - 6 fields per tab is maximum
53 * - reorganize the field numbers in database.h
56 struct abook_field abook_fields[ITEM_FIELDS] = {
57 {N_("Name"), "name", TAB_CONTACT},/* NAME */
58 {N_("E-mails"), "email", TAB_CONTACT},/* EMAIL */
59 {N_("Address"), "address", TAB_ADDRESS},/* ADDRESS */
60 {N_("Address2"), "address2", TAB_ADDRESS},/* ADDRESS2 */
61 {N_("City"), "city", TAB_ADDRESS},/* CITY */
62 {N_("State/Province"),"state", TAB_ADDRESS},/* STATE */
63 {N_("ZIP/Postal Code"),"zip", TAB_ADDRESS},/* ZIP */
64 {N_("Country"), "country", TAB_ADDRESS},/* COUNTRY */
65 {N_("Home Phone"), "phone", TAB_PHONE},/* PHONE */
66 {N_("Work Phone"), "workphone", TAB_PHONE},/* WORKPHONE */
67 {N_("Fax"), "fax", TAB_PHONE},/* FAX */
68 {N_("Mobile"), "mobile", TAB_PHONE},/* MOBILEPHONE */
69 {N_("Nickname/Alias"), "nick", TAB_OTHER},/* NICK */
70 {N_("URL"), "url", TAB_OTHER},/* URL */
71 {N_("Notes"), "notes", TAB_OTHER},/* NOTES */
72 {N_("Custom1"), "custom1", TAB_CUSTOM},/* CUSTOM1 */
73 {N_("Custom2"), "custom2", TAB_CUSTOM},/* CUSTOM2 */
74 {N_("Custom3"), "custom3", TAB_CUSTOM},/* CUSTOM3 */
75 {N_("Custom4"), "custom4", TAB_CUSTOM},/* CUSTOM4 */
76 {N_("Custom5"), "custom5", TAB_CUSTOM},/* CUSTOM5 */
81 find_field(const char *field)
85 for(i = 0; i < ITEM_FIELDS; i++)
86 if( !strcmp(abook_fields[i].key, field) )
93 parse_database(FILE *in)
100 memset(&item, 0, sizeof(item));
105 if(item[NAME] && sec)
106 add_item2database(item);
108 free_list_item(item);
112 if(!*line || *line == '\n' || *line == '#') {
114 } else if(*line == '[') {
115 if( item[NAME] && sec )
116 add_item2database(item);
118 free_list_item(item);
119 memset(&item, 0, sizeof(item));
121 if(!(tmp = strchr(line, ']')))
122 sec = 0; /*incorrect section lines are skipped*/
123 } else if((tmp = strchr(line, '=') ) && sec) {
125 for(i = 0; i < ITEM_FIELDS; i++)
126 if(!strcmp(abook_fields[i].key, line)) {
127 item[i] = xstrdup(tmp);
142 load_database(char *filename)
149 if ((in = abook_fopen(filename, "r")) == NULL)
154 return (items == 0) ? 2 : 0;
158 write_database(FILE *out, struct db_enumerator e)
164 "# abook addressbook file\n\n"
166 "program=" PACKAGE "\n"
167 "version=" VERSION "\n"
171 db_enumerate_items(e) {
172 fprintf(out, "[%d]\n", i);
173 for(j = 0; j < ITEM_FIELDS; j++) {
174 if( database[e.item][j] != NULL &&
175 *database[e.item][j] )
176 fprintf(out, "%s=%s\n",
192 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
194 if( (out = abook_fopen(datafile, "w")) == NULL )
197 if(list_is_empty()) {
204 write_database(out, e);
214 free_list_item(database[item]);
218 free_list_item(list_item item)
222 for(i=0; i<ITEM_FIELDS; i++)
231 for(i=0; i < items; i++)
241 first_list_item = curitem = -1;
245 #define _MAX_FIELD_LEN(X) (X == EMAIL ? MAX_EMAILSTR_LEN:MAX_FIELD_LEN)
248 validate_item(list_item item)
253 if(item[EMAIL] == NULL)
254 item[EMAIL] = xstrdup("");
256 for(i=0; i<ITEM_FIELDS; i++)
257 if( item[i] && ((int)strlen(item[i]) > _MAX_FIELD_LEN(i) ) ) {
259 item[i][_MAX_FIELD_LEN(i)-1] = 0;
260 item[i] = xstrdup(item[i]);
267 adjust_list_capacity()
269 if(list_capacity < 1)
270 list_capacity = INITIAL_LIST_CAPACITY;
271 else if(items >= list_capacity)
273 else if(list_capacity / 2 > items)
278 database = xrealloc(database, sizeof(list_item) * list_capacity);
279 selected = xrealloc(selected, list_capacity);
283 add_item2database(list_item item)
285 if(item[NAME] == NULL || ! *item[NAME]) {
286 free_list_item(item);
290 if(++items > list_capacity)
291 adjust_list_capacity();
295 selected[LAST_ITEM] = 0;
296 itemcpy(database[LAST_ITEM], item);
302 remove_selected_items()
309 if(!selected_items())
310 selected[curitem] = 1;
312 for(j = LAST_ITEM; j >= 0; j--) {
314 free_item(j); /* added for .4 data_s_ */
315 for(i = j; i < LAST_ITEM; i++) {
316 itemcpy(database[i], database[i + 1]);
317 selected[i] = selected[i + 1];
323 if(curitem > LAST_ITEM && items > 0)
326 adjust_list_capacity();
334 char *p = s + strlen(s);
338 while(p > s && *(p - 1) != ' ')
345 surnamecmp(const void *i1, const void *i2)
354 s1 = get_surname(a[NAME]);
355 s2 = get_surname(b[NAME]);
357 if( !(ret = safe_strcoll(s1, s2)) )
358 ret = safe_strcoll(a[NAME], b[NAME]);
366 static int sort_field = -1;
369 namecmp(const void *i1, const void *i2)
373 assert(sort_field >= 0 && sort_field <= LAST_FIELD);
378 return safe_strcoll(a[sort_field], b[sort_field]);
382 name2field(char *name)
386 for(i = 0; i < ITEM_FIELDS; i++) {
387 if(!strcasecmp(name, abook_fields[i].key)) {
397 sort_by_field(int field)
401 assert(field <= LAST_FIELD);
404 field = name2field(opt_get_str(STR_SORT_FIELD));
406 statusline_msg(_("Not valid field value defined "
407 "in configuration"));
414 qsort((void *)database, items, sizeof(list_item), namecmp);
424 qsort((void *)database, items, sizeof(list_item), surnamecmp);
430 find_item(char *str, int start, int search_fields[])
433 char *findstr = NULL;
435 int ret = -1; /* not found */
436 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
438 if(list_is_empty() || !is_valid_item(start))
439 return -2; /* error */
441 findstr = xstrdup(str);
442 findstr = strlower(findstr);
444 e.item = start - 1; /* must be "real start" - 1 */
445 db_enumerate_items(e) {
446 for(i = 0; search_fields[i] >= 0; i++) {
447 if(database[e.item][search_fields[i]] == NULL)
449 tmp = xstrdup(database[e.item][search_fields[i]]);
450 if( tmp && strstr(strlower(tmp), findstr) ) {
466 is_selected(int item)
468 return selected[item];
472 is_valid_item(int item)
474 return item <= LAST_ITEM && item >= 0;
478 real_db_enumerate_items(struct db_enumerator e)
480 int item = max(0, e.item + 1);
489 for(i = item; i < items; i++) {
498 fprintf(stderr, "real_db_enumerate_items() "
499 "BUG: unknown db_enumerator mode: %d\n",
505 return (item > LAST_ITEM || item < 0) ? -1 : item;
509 init_db_enumerator(int mode)
511 struct db_enumerator e;
513 e.item = -1; /* important - means "start from beginning" */
520 assign_fieldname(const char *name, int i)
525 assert(i >= 0 && i < ITEM_FIELDS);
527 if(strcasecmp(abook_fields[i].name, name)) { /* name differs */
529 * check if we are overwriting statically allocated default
531 if(strcasecmp(abook_fields[i].name, abook_fields[i].key))
532 xfree(abook_fields[i].name);
534 s = xmalloc_inc(MAX_FIELDNAME_LEN, 1);
535 snprintf(s, MAX_FIELDNAME_LEN, "%s", name);
536 abook_fields[i].name = s;
543 change_custom_field_name(const char *name, int n)
550 snprintf(keyname, sizeof(keyname), "custom%d", n);
552 for(i = CUSTOM_MIN; i <= CUSTOM_MAX; i++) {
553 if(!strcasecmp(abook_fields[i].key, keyname)) {
554 assign_fieldname(name, i);