2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2012 Match Grun and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 /* General functions for accessing address book files */
24 #include "claws-features.h"
38 #include "addrcache.h"
40 #include "adbookbase.h"
41 #include "file-utils.h"
43 #ifndef DEV_STANDALONE
44 #include "prefs_gtk.h"
48 #define ADDRBOOK_MAX_SEARCH_COUNT 1000
49 #define ADDRBOOK_PREFIX "addrbook-"
50 #define ADDRBOOK_SUFFIX ".xml"
51 #define FILE_NUMDIGITS 6
53 #define ID_TIME_OFFSET 998000000
55 static void addrbook_print_book ( AddressBookFile
*book
, FILE *stream
);
58 * Create new address book
59 * \return Address book.
61 AddressBookFile
*addrbook_create_book()
63 AddressBookFile
*book
;
65 book
= g_new0(AddressBookFile
, 1);
66 book
->type
= ADBOOKTYPE_BOOK
;
67 book
->addressCache
= addrcache_create();
68 book
->retVal
= MGU_SUCCESS
;
70 book
->fileName
= NULL
;
72 book
->tempList
= NULL
;
73 book
->tempHash
= NULL
;
74 book
->addressCache
->modified
= TRUE
;
80 * Specify name to be used
81 * \param book Address book.
84 void addrbook_set_name(AddressBookFile
*book
, const gchar
*value
)
86 cm_return_if_fail(book
!= NULL
);
87 addrcache_set_name(book
->addressCache
, value
);
90 gchar
*addrbook_get_name(AddressBookFile
*book
)
92 cm_return_val_if_fail(book
!= NULL
, NULL
);
93 return addrcache_get_name(book
->addressCache
);
97 * Specify path to address book file.
98 * \param book Address book.
101 void addrbook_set_path(AddressBookFile
*book
, const gchar
*value
)
103 cm_return_if_fail(book
!= NULL
);
104 book
->path
= mgu_replace_string(book
->path
, value
);
105 addrcache_set_dirty(book
->addressCache
, TRUE
);
109 * Specify filename to be used
110 * \param book Address book.
111 * \param value Filename.
113 void addrbook_set_file(AddressBookFile
*book
, const gchar
*value
)
115 cm_return_if_fail(book
!= NULL
);
116 book
->fileName
= mgu_replace_string(book
->fileName
, value
);
117 addrcache_set_dirty(book
->addressCache
, TRUE
);
120 gboolean
addrbook_get_modified(AddressBookFile
*book
)
122 cm_return_val_if_fail(book
!= NULL
, FALSE
);
123 return book
->addressCache
->modified
;
126 gboolean
addrbook_get_accessed(AddressBookFile
*book
)
128 cm_return_val_if_fail(book
!= NULL
, FALSE
);
129 return book
->addressCache
->accessFlag
;
133 * Specify address book as accessed.
134 * \param book Address book.
135 * \param value Value.
137 void addrbook_set_accessed(AddressBookFile
*book
, const gboolean value
)
139 cm_return_if_fail(book
!= NULL
);
140 book
->addressCache
->accessFlag
= value
;
143 gboolean
addrbook_get_read_flag(AddressBookFile
*book
)
145 cm_return_val_if_fail(book
!= NULL
, FALSE
);
146 return book
->addressCache
->dataRead
;
149 gint
addrbook_get_status(AddressBookFile
*book
)
151 cm_return_val_if_fail(book
!= NULL
, -1);
155 ItemFolder
*addrbook_get_root_folder(AddressBookFile
*book
)
157 cm_return_val_if_fail(book
!= NULL
, NULL
);
158 return addrcache_get_root_folder(book
->addressCache
);
161 GList
*addrbook_get_list_folder(AddressBookFile
*book
)
163 cm_return_val_if_fail(book
!= NULL
, NULL
);
164 return addrcache_get_list_folder(book
->addressCache
);
167 GList
*addrbook_get_list_person(AddressBookFile
*book
)
169 cm_return_val_if_fail(book
!= NULL
, NULL
);
170 return addrcache_get_list_person(book
->addressCache
);
173 gboolean
addrbook_get_dirty(AddressBookFile
*book
)
175 cm_return_val_if_fail(book
!= NULL
, FALSE
);
176 return addrcache_get_dirty(book
->addressCache
);
180 * Set address book as dirty (needs to be written to file).
181 * \param book Address book.
182 * \param value Dirty flag.
184 void addrbook_set_dirty(AddressBookFile
*book
, const gboolean value
)
186 cm_return_if_fail(book
!= NULL
);
187 addrcache_set_dirty(book
->addressCache
, value
);
192 * \param book Address book.
194 void addrbook_free_book(AddressBookFile
*book
)
196 cm_return_if_fail(book
!= NULL
);
199 addrcache_free(book
->addressCache
);
201 /* Free up internal objects */
203 g_free(book
->fileName
);
204 g_list_free(book
->tempList
);
207 book
->fileName
= NULL
;
209 book
->tempList
= NULL
;
210 book
->tempHash
= NULL
;
212 book
->type
= ADBOOKTYPE_NONE
;
213 book
->addressCache
= NULL
;
214 book
->retVal
= MGU_SUCCESS
;
220 * Print address book header.
221 * \param book Address book.
222 * \param stream Output stream.
224 static void addrbook_print_book(AddressBookFile
*book
, FILE *stream
)
226 cm_return_if_fail(book
!= NULL
);
228 fprintf(stream
, "AddressBook:\n");
229 fprintf(stream
, "\tpath : '%s'\n", book
->path
);
230 fprintf(stream
, "\tfile : '%s'\n", book
->fileName
);
231 fprintf(stream
, "\tstatus : %d\n", book
->retVal
);
232 addrcache_print(book
->addressCache
, stream
);
236 * Dump entire address book traversing folders.
237 * \param book Address book.
238 * \param stream Output stream.
240 void addrbook_dump_book(AddressBookFile
*book
, FILE *stream
)
244 cm_return_if_fail(book
!= NULL
);
246 addrbook_print_book(book
, stream
);
247 folder
= book
->addressCache
->rootFolder
;
248 addritem_print_item_folder(folder
, stream
);
252 * Remove specified group from address book. Note that object should still
254 * Specify name to be used
255 * \param book Address book.
256 * \param group Group to remove.
258 * \return Group, or NULL if not found.
260 ItemGroup
*addrbook_remove_group(AddressBookFile
*book
, ItemGroup
*group
)
262 cm_return_val_if_fail(book
!= NULL
, NULL
);
263 return addrcache_remove_group(book
->addressCache
, group
);
267 * Remove specified person from address book. Note that object should still
269 * \param book Address book.
270 * \param person Person to remove.
271 * \return Person, or NULL if not found.
273 ItemPerson
*addrbook_remove_person(AddressBookFile
*book
, ItemPerson
*person
)
275 cm_return_val_if_fail(book
!= NULL
, NULL
);
276 return addrcache_remove_person(book
->addressCache
, person
);
280 * Remove specified email address in address book for specified person.
281 * Note that object should still be freed.
282 * \param book Address book.
283 * \param person Person.
284 * \param email EMail to remove.
285 * \return EMail object, or NULL if not found.
287 ItemEMail
*addrbook_person_remove_email(AddressBookFile
*book
,
288 ItemPerson
*person
, ItemEMail
*email
)
290 cm_return_val_if_fail(book
!= NULL
, NULL
);
291 return addrcache_person_remove_email(book
->addressCache
, person
, email
);
295 * ***********************************************************************
296 * Read/Write XML data file...
297 * ===========================
299 * 1) The address book is structured as follows:
314 * 2) This sequence of elements was chosen so that the most important
315 * elements (person and their email addresses) appear first.
317 * 3) Groups then appear. When groups are loaded, person's email
318 * addresses have already been loaded and can be found.
320 * 4) Finally folders are loaded. Any forward and backward references
321 * to folders, groups and persons in the folders are resolved after
324 * ***********************************************************************
327 /* Element tag names */
328 #define AB_ELTAG_ADDRESS "address"
329 #define AB_ELTAG_ATTRIBUTE "attribute"
330 #define AB_ELTAG_ATTRIBUTE_LIST "attribute-list"
331 #define AB_ELTAG_ADDRESS_LIST "address-list"
332 #define AB_ELTAG_MEMBER "member"
333 #define AB_ELTAG_MEMBER_LIST "member-list"
334 #define AB_ELTAG_ITEM "item"
335 #define AB_ELTAG_ITEM_LIST "item-list"
336 #define AB_ELTAG_ADDRESS_BOOK "address-book"
337 #define AB_ELTAG_PERSON "person"
338 #define AB_ELTAG_GROUP "group"
339 #define AB_ELTAG_FOLDER "folder"
341 /* Attribute tag names */
342 #define AB_ATTAG_TYPE "type"
343 #define AB_ATTAG_UID "uid"
344 #define AB_ATTAG_NAME "name"
345 #define AB_ATTAG_REMARKS "remarks"
346 #define AB_ATTAG_FIRST_NAME "first-name"
347 #define AB_ATTAG_LAST_NAME "last-name"
348 #define AB_ATTAG_NICK_NAME "nick-name"
349 #define AB_ATTAG_COMMON_NAME "cn"
350 #define AB_ATTAG_ALIAS "alias"
351 #define AB_ATTAG_EMAIL "email"
352 #define AB_ATTAG_EID "eid"
353 #define AB_ATTAG_PID "pid"
355 /* Attribute values */
356 #define AB_ATTAG_VAL_PERSON "person"
357 #define AB_ATTAG_VAL_GROUP "group"
358 #define AB_ATTAG_VAL_FOLDER "folder"
361 * Parse address item for person from XML file.
362 * \param book Address book.
363 * \param file XML file handle.
364 * \param person Person.
366 static void addrbook_parse_address(AddressBookFile
*book
, XMLFile
*file
,
371 ItemEMail
*email
= NULL
;
373 attr
= xml_get_current_tag_attr(file
);
375 name
= ((XMLAttr
*)attr
->data
)->name
;
376 value
= ((XMLAttr
*)attr
->data
)->value
;
378 email
= addritem_create_item_email();
379 if (strcmp(name
, AB_ATTAG_UID
) == 0)
380 ADDRITEM_ID(email
) = g_strdup(value
);
381 else if (strcmp(name
, AB_ATTAG_ALIAS
) == 0)
382 ADDRITEM_NAME(email
) = g_strdup(value
);
383 else if (strcmp(name
, AB_ATTAG_EMAIL
) == 0)
384 email
->address
= g_strdup(value
);
385 else if (strcmp(name
, AB_ATTAG_REMARKS
) == 0)
386 email
->remarks
= g_strdup(value
);
387 attr
= g_list_next(attr
);
391 addrcache_person_add_email(book
->addressCache
, person
,
395 addritem_free_item_email(email
);
402 * Parse list of email address for person from XML file.
403 * \param book Address book.
404 * \param file XML file handle.
405 * \param person Person.
407 static void addrbook_parse_addr_list(AddressBookFile
*book
, XMLFile
*file
,
413 prev_level
= file
->level
;
414 if (xml_parse_next_tag(file
)) {
415 longjmp(book
->jumper
, 1);
417 if (file
->level
< prev_level
) return;
418 if (xml_compare_tag(file
, AB_ELTAG_ADDRESS
)) {
419 addrbook_parse_address(book
, file
, person
);
420 addrbook_parse_addr_list(book
, file
, person
);
426 * Parse attribute for person from XML file.
427 * \param book Address book.
428 * \param file XML file handle.
429 * \param person Person.
431 static void addrbook_parse_attribute(XMLFile
*file
, ItemPerson
*person
)
436 UserAttribute
*uAttr
= NULL
;
438 attr
= xml_get_current_tag_attr(file
);
440 name
= ((XMLAttr
*)attr
->data
)->name
;
441 value
= ((XMLAttr
*)attr
->data
)->value
;
442 if (!uAttr
) uAttr
= addritem_create_attribute();
443 if (strcmp(name
, AB_ATTAG_UID
) == 0)
444 addritem_attrib_set_id(uAttr
, value
);
445 else if (strcmp(name
, AB_ATTAG_NAME
) == 0)
446 addritem_attrib_set_name(uAttr
, value
);
447 attr
= g_list_next(attr
);
450 element
= xml_get_element(file
);
451 addritem_attrib_set_value(uAttr
, element
);
456 addritem_person_add_attribute(person
, uAttr
);
459 addritem_free_attribute(uAttr
);
466 * Parse list of attributes for person from XML file.
467 * \param book Address book.
468 * \param file XML file handle.
469 * \param person Person.
471 static void addrbook_parse_attr_list(AddressBookFile
*book
, XMLFile
*file
,
477 prev_level
= file
->level
;
478 if (xml_parse_next_tag(file
)) {
479 longjmp( book
->jumper
, 1 );
481 if (file
->level
< prev_level
) return;
482 if (xml_compare_tag(file
, AB_ELTAG_ATTRIBUTE
)) {
483 addrbook_parse_attribute(file
, person
);
484 addrbook_parse_attr_list(book
, file
, person
);
490 * Parse person from XML file.
491 * \param book Address book.
492 * \param file XML file handle.
494 static void addrbook_parse_person(AddressBookFile
*book
, XMLFile
*file
)
498 ItemPerson
*person
= NULL
;
500 attr
= xml_get_current_tag_attr(file
);
502 name
= ((XMLAttr
*)attr
->data
)->name
;
503 value
= ((XMLAttr
*)attr
->data
)->value
;
505 person
= addritem_create_item_person();
506 if (strcmp(name
, AB_ATTAG_UID
) == 0) {
507 ADDRITEM_ID(person
) = g_strdup(value
);
508 person
->picture
= g_strdup(value
);
510 else if (strcmp(name
, AB_ATTAG_FIRST_NAME
) == 0)
511 person
->firstName
= g_strdup(value
);
512 else if (strcmp(name
, AB_ATTAG_LAST_NAME
) == 0)
513 person
->lastName
= g_strdup(value
);
514 else if (strcmp(name
, AB_ATTAG_NICK_NAME
) == 0)
515 person
->nickName
= g_strdup(value
);
516 else if (strcmp(name
, AB_ATTAG_COMMON_NAME
) == 0)
517 ADDRITEM_NAME(person
) = g_strdup(value
);
518 attr
= g_list_next(attr
);
520 if (xml_parse_next_tag(file
)) { /* Consume closing tag */
521 longjmp(book
->jumper
, 1);
523 if (xml_compare_tag(file
, AB_ELTAG_ADDRESS_LIST
)) {
524 addrbook_parse_addr_list(book
, file
, person
);
526 addrcache_hash_add_person(book
->addressCache
, person
);
529 if (xml_parse_next_tag(file
)) { /* Consume closing tag */
530 longjmp(book
->jumper
, 1);
532 if (xml_compare_tag(file
, AB_ELTAG_ATTRIBUTE_LIST
)) {
533 addrbook_parse_attr_list(book
, file
, person
);
538 * Parse group member from XML file.
539 * \param book Address book.
540 * \param file XML file handle.
541 * \param group Group.
543 static void addrbook_parse_member(AddressBookFile
*book
, XMLFile
*file
,
549 /* gchar *pid = NULL; */
550 ItemEMail
*email
= NULL
;
552 attr
= xml_get_current_tag_attr(file
);
554 name
= ((XMLAttr
*)attr
->data
)->name
;
555 value
= ((XMLAttr
*)attr
->data
)->value
;
556 if( strcmp( name
, AB_ATTAG_EID
) == 0 )
557 eid
= g_strdup( value
);
558 attr
= g_list_next(attr
);
560 /* email = addrcache_get_email( book->addressCache, pid, eid ); */
561 email
= addrcache_get_email(book
->addressCache
, eid
);
565 addrcache_group_add_email(book
->addressCache
, group
,
569 addritem_free_item_email(email
);
576 * Parse list of group members from XML file.
577 * \param book Address book.
578 * \param file XML file handle.
579 * \param group Group.
581 static void addrbook_parse_member_list(AddressBookFile
*book
, XMLFile
*file
,
587 prev_level
= file
->level
;
588 if (xml_parse_next_tag(file
)) {
589 longjmp(book
->jumper
, 1);
591 if (file
->level
< prev_level
)
593 if (xml_compare_tag(file
, AB_ELTAG_MEMBER
)) {
594 addrbook_parse_member(book
, file
, group
);
595 addrbook_parse_member_list(book
, file
, group
);
601 * Parse group object from XML file.
602 * \param book Address book.
603 * \param file XML file handle.
605 static void addrbook_parse_group(AddressBookFile
*book
, XMLFile
*file
)
609 ItemGroup
*group
= NULL
;
611 attr
= xml_get_current_tag_attr(file
);
613 name
= ((XMLAttr
*)attr
->data
)->name
;
614 value
= ((XMLAttr
*)attr
->data
)->value
;
616 group
= addritem_create_item_group();
617 if (strcmp(name
, AB_ATTAG_UID
) == 0)
618 ADDRITEM_ID(group
) = g_strdup(value
);
619 else if (strcmp(name
, AB_ATTAG_NAME
) == 0)
620 ADDRITEM_NAME(group
) = g_strdup(value
);
621 else if (strcmp(name
, AB_ATTAG_REMARKS
) == 0)
622 group
->remarks
= g_strdup(value
);
623 attr
= g_list_next(attr
);
625 if (xml_parse_next_tag(file
)) { /* Consume closing tag */
626 longjmp(book
->jumper
, 1);
628 if (xml_compare_tag(file
, AB_ELTAG_MEMBER_LIST
)) {
630 addrcache_hash_add_group(book
->addressCache
, group
);
632 addrbook_parse_member_list(book
, file
, group
);
637 * Parse folder item from XML file.
638 * \param book Address book.
639 * \param file XML file handle.
640 * \param folder Folder.
642 static void addrbook_parse_folder_item(AddressBookFile
*book
, XMLFile
*file
,
649 attr
= xml_get_current_tag_attr(file
);
651 name
= ((XMLAttr
*)attr
->data
)->name
;
652 value
= ((XMLAttr
*)attr
->data
)->value
;
653 if (strcmp(name
, AB_ATTAG_UID
) == 0) {
654 uid
= g_strdup(value
);
656 attr
= g_list_next(attr
);
660 folder
->listItems
= g_list_append(folder
->listItems
, uid
);
666 * Parse list of folder items from XML file.
667 * \param book Address book.
668 * \param file XML file handle.
669 * \param folder Folder.
671 static void addrbook_parse_folder_list(AddressBookFile
*book
, XMLFile
*file
,
677 prev_level
= file
->level
;
678 if (xml_parse_next_tag(file
)) {
679 longjmp(book
->jumper
, 1);
681 if (file
->level
< prev_level
)
683 if (xml_compare_tag(file
, AB_ELTAG_ITEM
)) {
684 addrbook_parse_folder_item(book
, file
, folder
);
685 addrbook_parse_folder_list(book
, file
, folder
);
691 * Parse folder from XML file.
692 * \param book Address book.
693 * \param file XML file handle.
695 static void addrbook_parse_folder(AddressBookFile
*book
, XMLFile
*file
)
699 ItemFolder
*folder
= NULL
;
701 attr
= xml_get_current_tag_attr(file
);
703 name
= ((XMLAttr
*)attr
->data
)->name
;
704 value
= ((XMLAttr
*)attr
->data
)->value
;
706 folder
= addritem_create_item_folder();
707 if (strcmp(name
, AB_ATTAG_UID
) == 0)
708 ADDRITEM_ID(folder
) = g_strdup(value
);
709 else if (strcmp(name
, AB_ATTAG_NAME
) == 0)
710 ADDRITEM_NAME(folder
) = g_strdup(value
);
711 else if (strcmp(name
, AB_ATTAG_REMARKS
) == 0)
712 folder
->remarks
= g_strdup(value
);
713 attr
= g_list_next(attr
);
715 if (xml_parse_next_tag(file
)) { /* Consume closing tag */
716 longjmp(book
->jumper
, 1);
718 if (xml_compare_tag(file
, AB_ELTAG_ITEM_LIST
)) {
720 if (addrcache_hash_add_folder(book
->addressCache
,
722 book
->tempList
= g_list_append(book
->tempList
,
724 /* We will resolve folder later */
725 ADDRITEM_PARENT(folder
) = NULL
;
728 addrbook_parse_folder_list(book
, file
, folder
);
733 * Read address book (DOM) tree from file.
734 * \param book Address book.
735 * \param file XML file handle.
736 * \return <i>TRUE</i> if data read successfully, <i>FALSE</i> if error
739 static gboolean
addrbook_read_tree(AddressBookFile
*book
, XMLFile
*file
)
745 book
->retVal
= MGU_BAD_FORMAT
;
746 if (xml_get_dtd(file
))
748 if (xml_parse_next_tag(file
))
749 longjmp(book
->jumper
, 1);
750 if (!xml_compare_tag(file
, AB_ELTAG_ADDRESS_BOOK
))
753 attr
= xml_get_current_tag_attr(file
);
755 name
= ((XMLAttr
*)attr
->data
)->name
;
756 value
= ((XMLAttr
*)attr
->data
)->value
;
757 if (strcmp( name
, AB_ATTAG_NAME
) == 0)
758 addrbook_set_name( book
, value
);
759 attr
= g_list_next( attr
);
766 /* Get next item tag (person, group or folder) */
767 if (xml_parse_next_tag(file
))
768 longjmp( book
->jumper
, 1 );
770 if (xml_compare_tag(file
, AB_ELTAG_PERSON
))
771 addrbook_parse_person(book
, file
);
772 else if (xml_compare_tag(file
, AB_ELTAG_GROUP
))
773 addrbook_parse_group(book
, file
);
774 else if (xml_compare_tag(file
, AB_ELTAG_FOLDER
))
775 addrbook_parse_folder(book
, file
);
777 if (retVal
) book
->retVal
= MGU_SUCCESS
;
782 * Resolve folder items callback function.
783 * \param key Table key.
784 * \param value Reference to object contained in folder.
785 * \param data Reference to address book.
787 static void addrbook_res_items_vis(gpointer key
, gpointer value
, gpointer data
)
789 AddressBookFile
*book
= data
;
790 AddrItemObject
*obj
= (AddrItemObject
*) value
;
791 ItemFolder
*rootFolder
= book
->addressCache
->rootFolder
;
792 if (obj
->parent
== NULL
) {
793 if (ADDRITEM_TYPE(obj
) == ITEMTYPE_PERSON
) {
794 rootFolder
->listPerson
= g_list_append(rootFolder
->listPerson
,
796 ADDRITEM_PARENT(obj
) = ADDRITEM_OBJECT(rootFolder
);
798 else if (ADDRITEM_TYPE(obj
) == ITEMTYPE_GROUP
) {
799 rootFolder
->listGroup
= g_list_append(rootFolder
->listGroup
,
801 ADDRITEM_PARENT(obj
) = ADDRITEM_OBJECT(rootFolder
);
807 * Resolve folder items. Lists of UID's are replaced with pointers to
809 * \param book Address book.
811 static void addrbook_resolve_folder_items(AddressBookFile
*book
)
813 GList
*nodeFolder
= NULL
;
814 GList
*listRemove
= NULL
;
816 ItemFolder
*rootFolder
= book
->addressCache
->rootFolder
;
817 nodeFolder
= book
->tempList
;
820 ItemFolder
*folder
= nodeFolder
->data
;
822 node
= folder
->listItems
;
824 gchar
*uid
= node
->data
;
825 AddrItemObject
*aio
= addrcache_get_object(book
->addressCache
,
828 if (aio
->type
== ITEMTYPE_FOLDER
) {
829 ItemFolder
*item
= (ItemFolder
*) aio
;
830 folder
->listFolder
= g_list_append(folder
->listFolder
, item
);
831 ADDRITEM_PARENT(item
) = ADDRITEM_OBJECT(folder
);
832 addrcache_hash_add_folder(book
->addressCache
, folder
);
834 else if (aio
->type
== ITEMTYPE_PERSON
) {
835 ItemPerson
*item
= (ItemPerson
*) aio
;
836 folder
->listPerson
= g_list_append(folder
->listPerson
, item
);
837 ADDRITEM_PARENT(item
) = ADDRITEM_OBJECT(folder
);
839 else if (aio
->type
== ITEMTYPE_GROUP
) {
840 ItemGroup
*item
= (ItemGroup
*) aio
;
841 folder
->listGroup
= g_list_append(folder
->listGroup
, item
);
842 ADDRITEM_PARENT(item
) = ADDRITEM_OBJECT(folder
);
844 /* Replace data with pointer to item */
848 else { /* Not found, append to remove list. */
849 listRemove
= g_list_append(listRemove
, uid
);
851 node
= g_list_next(node
);
853 rootFolder
->listFolder
= g_list_append(rootFolder
->listFolder
,
855 /* Process remove list */
858 gchar
*uid
= node
->data
;
859 folder
->listItems
= g_list_remove(folder
->listItems
,
862 node
= g_list_next(node
);
864 g_list_free(listRemove
);
865 nodeFolder
= g_list_next(nodeFolder
);
867 /* Remove folders with parents. */
869 node
= rootFolder
->listFolder
;
871 ItemFolder
*folder
= (ItemFolder
*) node
->data
;
872 if (ADDRITEM_PARENT(folder
))
873 /* Remove folders with parents */
874 listRemove
= g_list_append(listRemove
, folder
);
875 else /* Add to root folder */
876 ADDRITEM_PARENT(folder
) = ADDRITEM_OBJECT(book
->addressCache
->rootFolder
);
878 node
= g_list_next( node
);
880 /* Process remove list */
883 rootFolder
->listFolder
= g_list_remove(rootFolder
->listFolder
,
885 node
= g_list_next(node
);
887 g_list_free(listRemove
);
889 /* Move all unparented persons and groups into root folder */
890 g_hash_table_foreach(book
->addressCache
->itemHash
,
891 addrbook_res_items_vis
, book
);
893 /* Free up some more */
894 nodeFolder
= book
->tempList
;
896 ItemFolder
*folder
= nodeFolder
->data
;
897 g_list_free(folder
->listItems
);
898 folder
->listItems
= NULL
;
899 nodeFolder
= g_list_next(nodeFolder
);
901 g_list_free(book
->tempList
);
902 book
->tempList
= NULL
;
907 * \param book Address book.
908 * \return Status code.
910 gint
addrbook_read_data(AddressBookFile
*book
)
912 XMLFile
*file
= NULL
;
913 gchar
*fileSpec
= NULL
;
915 cm_return_val_if_fail(book
!= NULL
, -1);
918 g_print( "...addrbook_read_data :%s:\t:%s:\n", book->fileName,
919 addrcache_get_name( book->addressCache ) );
922 fileSpec
= g_strconcat(book
->path
, G_DIR_SEPARATOR_S
,
923 book
->fileName
, NULL
);
924 book
->retVal
= MGU_OPEN_FILE
;
925 addrcache_clear(book
->addressCache
);
926 book
->addressCache
->modified
= FALSE
;
927 book
->addressCache
->accessFlag
= FALSE
;
928 file
= xml_open_file(fileSpec
);
931 book
->tempList
= NULL
;
932 /* Trap for parsing errors. */
933 if (setjmp( book
->jumper
)) {
934 xml_close_file(file
);
937 addrbook_read_tree(book
, file
);
938 xml_close_file(file
);
939 /* Resolve folder items */
940 addrbook_resolve_folder_items(book
);
941 book
->tempList
= NULL
;
942 book
->addressCache
->modified
= FALSE
;
943 book
->addressCache
->dataRead
= TRUE
;
944 addrcache_set_dirty(book
->addressCache
, FALSE
);
950 * Write start element to file.
951 * \param fp File handle.
952 * \param lvl Indent level.
953 * \param name Element name.
955 static int addrbook_write_elem_s(FILE *fp
, gint lvl
, gchar
*name
)
958 for (i
= 0; i
< lvl
; i
++)
959 if (claws_fputs(" ", fp
) == EOF
)
961 if (claws_fputs("<", fp
) == EOF
)
963 if (claws_fputs(name
, fp
) == EOF
)
970 * Write end element to file.
971 * \param fp File handle.
972 * \param lvl Indent level.
973 * \param name Element name.
975 static int addrbook_write_elem_e(FILE *fp
, gint lvl
, gchar
*name
)
978 for(i
= 0; i
< lvl
; i
++)
979 if (claws_fputs(" ", fp
) == EOF
)
981 if (claws_fputs("</", fp
) == EOF
)
983 if (claws_fputs(name
, fp
) == EOF
)
985 if (claws_fputs(">\n", fp
) == EOF
)
992 * Write attribute name/value pair to file.
993 * \param fp File handle.
994 * \param name Attribute name.
995 * \param value Attribute value.
997 static int addrbook_write_attr(FILE *fp
, gchar
*name
, gchar
*value
)
999 if (claws_fputs(" ", fp
) == EOF
)
1001 if (claws_fputs(name
, fp
) == EOF
)
1003 if (claws_fputs("=\"", fp
) == EOF
)
1005 if (xml_file_put_escape_str(fp
, value
) < 0)
1007 if (claws_fputs("\"", fp
) == EOF
)
1013 typedef struct _HashLoopData
{
1019 * Write person and associated addresses and attributes to file.
1020 * file hash table visitor function.
1021 * \param key Table key.
1022 * \param value Reference to person.
1023 * \param data File pointer.
1025 static void addrbook_write_item_person_vis(gpointer key
, gpointer value
,
1028 AddrItemObject
*obj
= (AddrItemObject
*) value
;
1029 HashLoopData
*data
= (HashLoopData
*)d
;
1030 FILE *fp
= data
->fp
;
1035 if (ADDRITEM_TYPE(obj
) == ITEMTYPE_PERSON
) {
1036 ItemPerson
*person
= (ItemPerson
*) value
;
1038 if (addrbook_write_elem_s(fp
, 1, AB_ELTAG_PERSON
) < 0)
1040 if (addrbook_write_attr(fp
, AB_ATTAG_UID
, ADDRITEM_ID(person
)) < 0)
1042 if (addrbook_write_attr(fp
, AB_ATTAG_FIRST_NAME
, person
->firstName
) < 0)
1044 if (addrbook_write_attr(fp
, AB_ATTAG_LAST_NAME
, person
->lastName
) < 0)
1046 if (addrbook_write_attr(fp
, AB_ATTAG_NICK_NAME
, person
->nickName
) < 0)
1048 if (addrbook_write_attr(fp
, AB_ATTAG_COMMON_NAME
, ADDRITEM_NAME(person
)) < 0)
1050 if (claws_fputs(" >\n", fp
) == EOF
)
1053 /* Output email addresses */
1054 if (addrbook_write_elem_s(fp
, 2, AB_ELTAG_ADDRESS_LIST
) < 0)
1056 if (claws_fputs(">\n", fp
) == EOF
)
1058 node
= person
->listEMail
;
1060 ItemEMail
*email
= node
->data
;
1061 if (addrbook_write_elem_s(fp
, 3, AB_ELTAG_ADDRESS
) < 0)
1063 if (addrbook_write_attr(fp
, AB_ATTAG_UID
, ADDRITEM_ID(email
)) < 0)
1065 if (addrbook_write_attr(fp
, AB_ATTAG_ALIAS
, ADDRITEM_NAME(email
)) < 0)
1067 if (addrbook_write_attr(fp
, AB_ATTAG_EMAIL
, email
->address
) < 0)
1069 if (addrbook_write_attr(fp
, AB_ATTAG_REMARKS
, email
->remarks
) < 0)
1071 if (claws_fputs(" />\n", fp
) == EOF
)
1073 node
= g_list_next(node
);
1075 if (addrbook_write_elem_e(fp
, 2, AB_ELTAG_ADDRESS_LIST
) < 0)
1078 /* Output user attributes */
1079 if (addrbook_write_elem_s(fp
, 2, AB_ELTAG_ATTRIBUTE_LIST
) < 0)
1081 if (claws_fputs(">\n", fp
) == EOF
)
1083 node
= person
->listAttrib
;
1085 UserAttribute
*attrib
= node
->data
;
1086 if (addrbook_write_elem_s(fp
, 3, AB_ELTAG_ATTRIBUTE
) < 0)
1088 if (addrbook_write_attr(fp
, AB_ATTAG_UID
, attrib
->uid
) < 0)
1090 if (addrbook_write_attr(fp
, AB_ATTAG_NAME
, attrib
->name
) < 0)
1092 if (claws_fputs(" >", fp
) == EOF
)
1094 if (xml_file_put_escape_str(fp
, attrib
->value
) < 0)
1096 if (addrbook_write_elem_e(fp
, 0, AB_ELTAG_ATTRIBUTE
) < 0)
1098 node
= g_list_next(node
);
1100 if (addrbook_write_elem_e(fp
, 2, AB_ELTAG_ATTRIBUTE_LIST
) < 0)
1102 if (addrbook_write_elem_e(fp
, 1, AB_ELTAG_PERSON
) < 0)
1109 * Write group and associated references to addresses to file.
1110 * file hash table visitor function.
1111 * \param key Table key.
1112 * \param value Reference to group.
1113 * \param data File pointer.
1115 static void addrbook_write_item_group_vis(gpointer key
, gpointer value
,
1118 AddrItemObject
*obj
= (AddrItemObject
*) value
;
1119 HashLoopData
*data
= (HashLoopData
*)d
;
1120 FILE *fp
= data
->fp
;
1126 if (ADDRITEM_TYPE(obj
) == ITEMTYPE_GROUP
) {
1127 ItemGroup
*group
= (ItemGroup
*) value
;
1129 if (addrbook_write_elem_s(fp
, 1, AB_ELTAG_GROUP
) < 0)
1131 if (addrbook_write_attr(fp
, AB_ATTAG_UID
, ADDRITEM_ID(group
)) < 0)
1133 if (addrbook_write_attr(fp
, AB_ATTAG_NAME
, ADDRITEM_NAME(group
)) < 0)
1135 if (addrbook_write_attr(fp
, AB_ATTAG_REMARKS
, group
->remarks
) < 0)
1137 if (claws_fputs(" >\n", fp
) == EOF
)
1140 /* Output email address links */
1141 if (addrbook_write_elem_s(fp
, 2, AB_ELTAG_MEMBER_LIST
) < 0)
1143 if (claws_fputs(">\n", fp
) == EOF
)
1145 node
= group
->listEMail
;
1147 ItemEMail
*email
= node
->data
;
1148 ItemPerson
*person
= (ItemPerson
*) ADDRITEM_PARENT(email
);
1149 if (addrbook_write_elem_s(fp
, 3, AB_ELTAG_MEMBER
) < 0)
1151 if (addrbook_write_attr(fp
, AB_ATTAG_PID
, ADDRITEM_ID(person
)) < 0)
1153 if (addrbook_write_attr(fp
, AB_ATTAG_EID
, ADDRITEM_ID(email
)) < 0)
1155 if (claws_fputs(" />\n", fp
) == EOF
)
1157 node
= g_list_next(node
);
1159 if (addrbook_write_elem_e(fp
, 2, AB_ELTAG_MEMBER_LIST
) < 0)
1161 if (addrbook_write_elem_e(fp
, 1, AB_ELTAG_GROUP
) < 0)
1168 * Write folder and associated references to addresses to file.
1169 * file hash table visitor function.
1170 * \param key Table key.
1171 * \param value Reference to folder.
1172 * \param data File pointer.
1174 static void addrbook_write_item_folder_vis(gpointer key
, gpointer value
,
1177 AddrItemObject
*obj
= (AddrItemObject
*) value
;
1178 HashLoopData
*data
= (HashLoopData
*)d
;
1179 FILE *fp
= data
->fp
;
1184 if (ADDRITEM_TYPE(obj
) == ITEMTYPE_FOLDER
) {
1185 ItemFolder
*folder
= (ItemFolder
*) value
;
1187 if (addrbook_write_elem_s(fp
, 1, AB_ELTAG_FOLDER
) < 0)
1189 if (addrbook_write_attr(fp
, AB_ATTAG_UID
, ADDRITEM_ID(folder
)) < 0)
1191 if (addrbook_write_attr(fp
, AB_ATTAG_NAME
, ADDRITEM_NAME(folder
)) < 0)
1193 if (addrbook_write_attr(fp
, AB_ATTAG_REMARKS
, folder
->remarks
) < 0)
1195 if (claws_fputs(" >\n", fp
) == EOF
)
1197 if (addrbook_write_elem_s(fp
, 2, AB_ELTAG_ITEM_LIST
) < 0)
1199 if (claws_fputs(">\n", fp
) == EOF
)
1202 /* Output persons */
1203 node
= folder
->listPerson
;
1205 ItemPerson
*item
= node
->data
;
1206 if (addrbook_write_elem_s(fp
, 3, AB_ELTAG_ITEM
) < 0)
1208 if (addrbook_write_attr(fp
, AB_ATTAG_TYPE
, AB_ATTAG_VAL_PERSON
) < 0)
1210 if (addrbook_write_attr(fp
, AB_ATTAG_UID
, ADDRITEM_ID(item
)) < 0)
1212 if (claws_fputs(" />\n", fp
) == EOF
)
1214 node
= g_list_next(node
);
1218 node
= folder
->listGroup
;
1220 ItemGroup
*item
= node
->data
;
1221 if (addrbook_write_elem_s(fp
, 3, AB_ELTAG_ITEM
) < 0)
1223 if (addrbook_write_attr(fp
, AB_ATTAG_TYPE
, AB_ATTAG_VAL_GROUP
) < 0)
1225 if (addrbook_write_attr(fp
, AB_ATTAG_UID
, ADDRITEM_ID(item
)) < 0)
1227 if (claws_fputs(" />\n", fp
) == EOF
)
1229 node
= g_list_next(node
);
1232 /* Output folders */
1233 node
= folder
->listFolder
;
1235 ItemFolder
*item
= node
->data
;
1236 if (addrbook_write_elem_s(fp
, 3, AB_ELTAG_ITEM
) < 0)
1238 if (addrbook_write_attr(fp
, AB_ATTAG_TYPE
, AB_ATTAG_VAL_FOLDER
) < 0)
1240 if (addrbook_write_attr(fp
, AB_ATTAG_UID
, ADDRITEM_ID(item
)) < 0)
1242 if (claws_fputs(" />\n", fp
) == EOF
)
1244 node
= g_list_next(node
);
1246 if (addrbook_write_elem_e(fp
, 2, AB_ELTAG_ITEM_LIST
) < 0)
1248 if (addrbook_write_elem_e(fp
, 1, AB_ELTAG_FOLDER
) < 0)
1255 * Output address book data to specified file.
1256 * \param book Address book.
1257 * \param newFile Filename of new file (in book's filepath).
1258 * \return Status code.
1260 static gint
addrbook_write_to(AddressBookFile
*book
, gchar
*newFile
)
1265 #ifndef DEV_STANDALONE
1269 cm_return_val_if_fail(book
!= NULL
, -1);
1270 cm_return_val_if_fail(newFile
!= NULL
, -1);
1272 fileSpec
= g_strconcat(book
->path
, G_DIR_SEPARATOR_S
, newFile
, NULL
);
1274 book
->retVal
= MGU_OPEN_FILE
;
1275 #ifdef DEV_STANDALONE
1276 fp
= claws_fopen(fileSpec
, "wb");
1279 if (claws_fputs("<?xml version=\"1.0\" ?>\n", fp
) == EOF
) {
1280 book
->retVal
= MGU_ERROR_WRITE
;
1281 return book
->retVal
;
1284 pfile
= prefs_write_open(fileSpec
);
1288 if (fprintf( fp
, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n", CS_INTERNAL
) < 0)
1291 if (addrbook_write_elem_s(fp
, 0, AB_ELTAG_ADDRESS_BOOK
) < 0)
1293 if (addrbook_write_attr(fp
, AB_ATTAG_NAME
,
1294 addrcache_get_name(book
->addressCache
)) < 0)
1296 if (claws_fputs(" >\n", fp
) == EOF
)
1299 /* Output all persons */
1303 g_hash_table_foreach(book
->addressCache
->itemHash
,
1304 addrbook_write_item_person_vis
, &data
);
1308 /* Output all groups */
1309 g_hash_table_foreach(book
->addressCache
->itemHash
,
1310 addrbook_write_item_group_vis
, &data
);
1315 /* Output all folders */
1316 g_hash_table_foreach(book
->addressCache
->itemHash
,
1317 addrbook_write_item_folder_vis
, &data
);
1322 if (addrbook_write_elem_e(fp
, 0, AB_ELTAG_ADDRESS_BOOK
) < 0)
1325 book
->retVal
= MGU_SUCCESS
;
1326 #ifdef DEV_STANDALONE
1327 claws_safe_fclose(fp
);
1329 if (prefs_file_close( pfile
) < 0)
1330 book
->retVal
= MGU_ERROR_WRITE
;
1335 return book
->retVal
;
1337 g_warning("error writing AB");
1338 book
->retVal
= MGU_ERROR_WRITE
;
1340 prefs_file_close_revert( pfile
);
1341 return book
->retVal
;
1345 * Output address book data to original file.
1346 * \param book Address book.
1347 * \return Status code.
1349 gint
addrbook_save_data(AddressBookFile
*book
)
1351 cm_return_val_if_fail(book
!= NULL
, -1);
1353 book
->retVal
= MGU_NO_FILE
;
1354 if (book
->fileName
== NULL
|| *book
->fileName
== '\0')
1355 return book
->retVal
;
1356 if (book
->path
== NULL
|| *book
->path
== '\0')
1357 return book
->retVal
;
1359 addrbook_write_to(book
, book
->fileName
);
1360 if (book
->retVal
== MGU_SUCCESS
)
1361 addrcache_set_dirty(book
->addressCache
, FALSE
);
1362 return book
->retVal
;
1366 * **********************************************************************
1367 * Address book edit interface functions.
1368 * **********************************************************************
1372 * Hash table callback function for simple deletion of hashtable entries.
1373 * \param key Table key (will be freed).
1374 * \param value Value stored in table.
1375 * \param data User data.
1376 * \return <i>TRUE</i> to indicate that entry freed.
1378 static gboolean
addrbook_free_simple_hash_vis(gpointer
*key
, gpointer
*value
,
1388 * Update address book email list for specified person. Note: The existing
1389 * email addresses are replaced with the new addresses. Any references to
1390 * old addresses in the groups are re-linked to the new addresses. All old
1391 * addresses linked to the person are removed.
1392 * \param book Address book.
1393 * \param person Person to update.
1394 * \param listEMail List of new email addresses.
1396 void addrbook_update_address_list(AddressBookFile
*book
, ItemPerson
*person
,
1403 cm_return_if_fail(book
!= NULL
);
1404 cm_return_if_fail(person
!= NULL
);
1406 /* Get groups where person's existing email addresses are listed */
1407 listGroup
= addrcache_get_group_for_person(book
->addressCache
, person
);
1409 GHashTable
*hashEMail
;
1410 GHashTable
*hashEMailAlias
;
1413 /* Load hash table with new address entries */
1414 hashEMail
= g_hash_table_new(g_str_hash
, g_str_equal
);
1415 hashEMailAlias
= g_hash_table_new(g_str_hash
, g_str_equal
);
1418 ItemEMail
*email
= node
->data
;
1419 gchar
*alias
= email
->obj
.name
;
1420 gchar
*addr
= g_utf8_strdown(email
->address
, -1);
1421 if (!g_hash_table_lookup(hashEMail
, addr
)) {
1422 g_hash_table_insert(hashEMail
, addr
, email
);
1424 if (*alias
!= '\0' && ! g_hash_table_lookup(hashEMailAlias
,
1426 g_hash_table_insert(hashEMailAlias
, alias
, email
);
1428 node
= g_list_next(node
);
1431 /* Re-parent new addresses to existing groups, where email address match. */
1432 nodeGrp
= listGroup
;
1434 ItemGroup
*group
= (ItemGroup
*) nodeGrp
->data
;
1435 GList
*groupEMail
= group
->listEMail
;
1437 GList
*listRemove
= NULL
;
1439 /* Process each email item linked to group */
1440 nodeGrpEM
= groupEMail
;
1442 ItemEMail
*emailGrp
= (ItemEMail
*) nodeGrpEM
->data
;
1444 if (ADDRITEM_PARENT(emailGrp
) == ADDRITEM_OBJECT(person
)) {
1445 /* Found an email address for this person */
1446 ItemEMail
*emailNew
= NULL
;
1447 gchar
*alias
= emailGrp
->obj
.name
;
1448 gchar
*addr
= g_utf8_strdown(emailGrp
->address
, -1);
1449 emailNew
= (ItemEMail
*)
1450 g_hash_table_lookup(hashEMail
, addr
);
1452 /* If no match by e-mail, try to match by e-mail alias */
1453 if (!emailNew
&& *alias
!= '\0') {
1454 emailNew
= (ItemEMail
*)
1455 g_hash_table_lookup(hashEMailAlias
, alias
);
1459 /* Point to this entry */
1460 nodeGrpEM
->data
= emailNew
;
1461 else if (g_hash_table_size(hashEMail
)==1)
1462 /* If the person has just one e-mail address, then
1463 change e-mail address in group list */
1464 nodeGrpEM
->data
= listEMail
->data
;
1466 /* Mark for removal */
1467 listRemove
= g_list_append(listRemove
, emailGrp
);
1469 /* Move on to next email link */
1470 nodeGrpEM
= g_list_next(nodeGrpEM
);
1473 /* Process all removed links in current group */
1474 nodeGrpEM
= listRemove
;
1476 ItemEMail
*emailGrp
= nodeGrpEM
->data
;
1477 groupEMail
= g_list_remove(groupEMail
, emailGrp
);
1478 nodeGrpEM
= g_list_next(nodeGrpEM
);
1481 g_list_free(listRemove
);
1483 /* Move on to next group */
1484 nodeGrp
= g_list_next(nodeGrp
);
1487 /* Clear hash table */
1488 g_hash_table_foreach_remove(hashEMail
, (GHRFunc
)
1489 addrbook_free_simple_hash_vis
, NULL
);
1490 g_hash_table_destroy(hashEMail
);
1492 g_hash_table_destroy(hashEMailAlias
);
1493 hashEMailAlias
= NULL
;
1494 g_list_free(listGroup
);
1497 /* Remove old addresses from person and cache */
1499 node
= person
->listEMail
;
1501 ItemEMail
*email
= node
->data
;
1503 if (addrcache_person_remove_email(book
->addressCache
, person
, email
))
1504 addrcache_remove_email(book
->addressCache
, email
);
1506 listDelete
= g_list_append(listDelete
, email
);
1507 node
= person
->listEMail
;
1509 /* Add new address entries */
1512 ItemEMail
*email
= node
->data
;
1514 if (ADDRITEM_ID(email
) == NULL
)
1515 /* Allocate an ID for new address */
1516 addrcache_id_email(book
->addressCache
, email
);
1518 addrcache_person_add_email( book
->addressCache
, person
, email
);
1519 node
= g_list_next( node
);
1522 addrcache_set_dirty(book
->addressCache
, TRUE
);
1524 /* Free up memory */
1525 g_list_free(listEMail
);
1530 ItemEMail
*email
= node
->data
;
1532 addritem_free_item_email(email
);
1533 node
= g_list_next(node
);
1535 g_list_free(listDelete
);
1541 * Create person object and add person with specified address data to address
1542 * book. Note: A new person is created with specified list of email addresses.
1543 * All objects inserted into address book.
1545 * \param book Address book.
1546 * \param folder Parent folder where to add person, or <i>NULL</i> for
1548 * \param listEMail List of new email addresses to associate with person.
1549 * \return Person object created.
1551 ItemPerson
*addrbook_add_address_list(AddressBookFile
*book
, ItemFolder
*folder
,
1555 ItemFolder
*f
= folder
;
1558 cm_return_val_if_fail(book
!= NULL
, NULL
);
1561 f
= book
->addressCache
->rootFolder
;
1562 person
= addritem_create_item_person();
1563 addrcache_id_person(book
->addressCache
, person
);
1564 addrcache_folder_add_person(book
->addressCache
, f
, person
);
1568 ItemEMail
*email
= node
->data
;
1569 if (ADDRITEM_ID(email
) == NULL
)
1570 addrcache_id_email(book
->addressCache
, email
);
1572 addrcache_person_add_email(book
->addressCache
, person
, email
);
1573 node
= g_list_next(node
);
1579 * Build available email list visitor function.
1580 * \param key Table key.
1581 * \param value Value stored in table.
1582 * \param data Reference to address book.
1584 static void addrbook_build_avail_email_vis(gpointer key
, gpointer value
,
1587 AddrItemObject
*obj
= (AddrItemObject
*) value
;
1589 if (ADDRITEM_TYPE(obj
) == ITEMTYPE_PERSON
) {
1590 AddressBookFile
*book
= data
;
1591 ItemPerson
*person
= (ItemPerson
*) obj
;
1592 GList
*node
= person
->listEMail
;
1594 ItemEMail
*email
= node
->data
;
1595 /* gchar *newKey = g_strdup( ADDRITEM_ID(email) ); */
1597 if (!g_hash_table_lookup(book
->tempHash
,
1598 ADDRITEM_ID(email
)))
1599 book
->tempList
= g_list_append(book
->tempList
, email
);
1601 node
= g_list_next(node
);
1607 * Return link list of available email items that have not already been linked
1608 * to groups. Note that the list contains references to items and should be
1609 * <code>g_list_free()</code> when done. Do <b>*NOT*</b> attempt to used the
1610 * <code>addrbook_free_xxx()<code> functions... this will destroy the
1613 * \param book Address book.
1614 * \param group Group to process.
1615 * \return List of items, or <i>NULL</i> if none.
1617 GList
*addrbook_get_available_email_list(AddressBookFile
*book
, ItemGroup
*group
)
1622 cm_return_val_if_fail(book
!= NULL
, NULL
);
1624 /* Load hash table with group email entries */
1625 table
= g_hash_table_new(g_str_hash
, g_str_equal
);
1627 list
= group
->listEMail
;
1629 ItemEMail
*email
= list
->data
;
1630 g_hash_table_insert(table
, ADDRITEM_ID(email
), email
);
1631 list
= g_list_next(list
);
1635 /* Build list of available email addresses which exclude those already in groups */
1636 book
->tempList
= NULL
;
1637 book
->tempHash
= table
;
1638 g_hash_table_foreach(book
->addressCache
->itemHash
,
1639 addrbook_build_avail_email_vis
, book
);
1640 list
= book
->tempList
;
1641 book
->tempList
= NULL
;
1642 book
->tempHash
= NULL
;
1644 /* Clear hash table */
1645 g_hash_table_destroy(table
);
1652 * Update address book email list for specified group. Note: The existing email
1653 * addresses are replaced with the new addresses. Any references to old addresses
1654 * in the groups are re-linked to the new addresses. All old addresses linked to
1655 * the person are removed.
1657 * \param book Address book.
1658 * \param group Group to process.
1659 * \param listEMail List of email items. This should <b>*NOT*</b> be
1660 * <code>g_free()</code> when done.
1662 void addrbook_update_group_list(AddressBookFile
*book
, ItemGroup
*group
,
1667 cm_return_if_fail(book
!= NULL
);
1668 cm_return_if_fail(group
!= NULL
);
1670 addrcache_set_dirty(book
->addressCache
, TRUE
);
1672 /* Remember old list */
1673 oldData
= group
->listEMail
;
1674 group
->listEMail
= listEMail
;
1675 g_list_free(oldData
);
1679 * Create group object and add with specifed list of email addresses to
1680 * address book. Note: The existing email addresses are replaced with the new
1681 * addresses. Any references to old addresses in the groups are re-linked to
1682 * the new addresses. All old addresses linked to the person are removed.
1684 * \param book Address book.
1685 * \param folder Parent folder where to add group, or <i>NULL</i> for
1687 * \param listEMail List of email items. This should <b>*NOT*</b> be
1688 * <code>g_free()</code> when done.
1689 * \return Group object created.
1691 ItemGroup
*addrbook_add_group_list(AddressBookFile
*book
, ItemFolder
*folder
,
1694 ItemGroup
*group
= NULL
;
1695 ItemFolder
*f
= folder
;
1697 cm_return_val_if_fail(book
!= NULL
, NULL
);
1700 f
= book
->addressCache
->rootFolder
;
1701 group
= addritem_create_item_group();
1702 addrcache_id_group(book
->addressCache
, group
);
1703 addrcache_folder_add_group(book
->addressCache
, f
, group
);
1704 group
->listEMail
= listEMail
;
1709 * Create a new folder and add to address book.
1710 * \param book Address book.
1711 * \param folder Parent folder where to add folder, or <i>NULL</i> for
1713 * \return Folder that was created. This should <b>*NOT*</b> be
1714 * <code>g_free()</code> when done.
1716 ItemFolder
*addrbook_add_new_folder(AddressBookFile
*book
, ItemFolder
*parent
)
1718 cm_return_val_if_fail(book
!= NULL
, NULL
);
1719 return addrcache_add_new_folder( book
->addressCache
, parent
);
1723 * Update address book attribute list for specified person. Note: The existing
1724 * attributes are replaced with the new addresses. All old attributes linked
1725 * to the person are removed.
1727 * \param book Address book.
1728 * \param person Person to receive attributes.
1729 * \param listAttrib New list of attributes.
1731 void addrbook_update_attrib_list(AddressBookFile
*book
, ItemPerson
*person
,
1737 cm_return_if_fail(book
!= NULL
);
1738 cm_return_if_fail(person
!= NULL
);
1740 /* Remember old list */
1741 oldData
= person
->listAttrib
;
1743 /* Attach new address list to person. */
1746 UserAttribute
*attrib
= node
->data
;
1747 if (attrib
->uid
== NULL
) {
1748 /* Allocate an ID */
1749 addrcache_id_attribute(book
->addressCache
, attrib
);
1751 node
= g_list_next(node
);
1753 person
->listAttrib
= listAttrib
;
1754 addrcache_set_dirty(book
->addressCache
, TRUE
);
1756 /* Free up old data */
1757 addritem_free_list_attribute(oldData
);
1762 * Add attribute data for specified person to address book. Note: Only
1763 * attributes are inserted into address book.
1764 * \param book Address book.
1765 * \param person Person to receive attributes.
1766 * \param listAttrib List of attributes.
1768 void addrbook_add_attrib_list( AddressBookFile
*book
, ItemPerson
*person
, GList
*listAttrib
) {
1771 cm_return_if_fail( book
!= NULL
);
1772 cm_return_if_fail( person
!= NULL
);
1776 UserAttribute
*attrib
= node
->data
;
1777 if( attrib
->uid
== NULL
) {
1778 addrcache_id_attribute( book
->addressCache
, attrib
);
1780 addritem_person_add_attribute( person
, attrib
);
1781 node
= g_list_next( node
);
1783 addrcache_set_dirty( book
->addressCache
, TRUE
);
1786 #define WORK_BUFLEN 1024
1787 #define ADDRBOOK_DIGITS "0123456789"
1790 * Return list of existing address book files.
1791 * \param book Address book.
1792 * \return List of files (as strings).
1794 GList
*addrbook_get_bookfile_list(AddressBookFile
*book
) {
1797 const gchar
*dir_name
;
1799 gchar buf
[WORK_BUFLEN
+ 1];
1800 gchar numbuf
[WORK_BUFLEN
];
1801 gint len
, lenpre
, lensuf
, lennum
;
1802 long int val
, maxval
;
1803 GList
*fileList
= NULL
;
1805 cm_return_val_if_fail(book
!= NULL
, NULL
);
1807 if (book
->path
== NULL
|| *book
->path
== '\0') {
1808 book
->retVal
= MGU_NO_PATH
;
1812 strncpy(buf
, book
->path
, WORK_BUFLEN
);
1815 if (buf
[len
-1] != G_DIR_SEPARATOR
) {
1816 buf
[len
] = G_DIR_SEPARATOR
;
1821 adbookdir
= g_strdup(buf
);
1822 strncat(buf
, ADDRBOOK_PREFIX
, WORK_BUFLEN
- strlen(buf
));
1824 if( ( dir
= g_dir_open( adbookdir
, 0, NULL
) ) == NULL
) {
1825 book
->retVal
= MGU_OPEN_DIRECTORY
;
1830 lenpre
= strlen(ADDRBOOK_PREFIX
);
1831 lensuf
= strlen(ADDRBOOK_SUFFIX
);
1832 lennum
= FILE_NUMDIGITS
+ lenpre
;
1835 while( ( dir_name
= g_dir_read_name( dir
) ) != NULL
) {
1836 gchar
*endptr
= NULL
;
1840 strncpy(buf
, adbookdir
, WORK_BUFLEN
);
1841 strncat(buf
, dir_name
, WORK_BUFLEN
- strlen(buf
));
1842 r
= g_stat(buf
, &statbuf
);
1843 if (r
== 0 && S_ISREG(statbuf
.st_mode
)) {
1846 ADDRBOOK_PREFIX
, lenpre
) == 0)
1849 (dir_name
) + lennum
,
1850 ADDRBOOK_SUFFIX
, lensuf
) == 0)
1853 (dir_name
) + lenpre
,
1855 numbuf
[FILE_NUMDIGITS
] = '\0';
1857 for(i
= 0; i
< FILE_NUMDIGITS
; i
++) {
1858 if(!strchr(ADDRBOOK_DIGITS
, numbuf
[i
])) {
1865 val
= strtol(numbuf
, &endptr
, 10);
1866 if (endptr
&& val
> -1) {
1867 if (val
> maxval
) maxval
= val
;
1868 fileList
= g_list_append(
1870 g_strdup(dir_name
));
1880 book
->maxValue
= maxval
;
1881 book
->retVal
= MGU_SUCCESS
;
1886 * Return file name for specified file number.
1887 * \param fileNum File number.
1888 * \return File name, or <i>NULL</i> if file number too large. Should be
1889 * <code>g_free()</code> when done.
1891 gchar
*addrbook_gen_new_file_name(gint fileNum
) {
1893 gchar buf
[WORK_BUFLEN
];
1899 nmax
= -1 + (long int) pow(10, FILE_NUMDIGITS
);
1902 g_snprintf(fmt
, sizeof(fmt
), "%%s%%0%dd%%s", FILE_NUMDIGITS
);
1903 g_snprintf(buf
, sizeof(buf
), fmt
, ADDRBOOK_PREFIX
, n
, ADDRBOOK_SUFFIX
);
1904 return g_strdup(buf
);
1908 * **********************************************************************
1909 * Address book test functions...
1910 * **********************************************************************
1914 * Attempt to parse list of email address from file.
1915 * \param book Address book.
1916 * \param file XML file handle.
1918 static void addrbook_chkparse_addr_list( AddressBookFile
*book
, XMLFile
*file
)
1924 prev_level
= file
->level
;
1925 if (xml_parse_next_tag(file
))
1926 longjmp(book
->jumper
, 1);
1927 if (file
->level
< prev_level
)
1929 /* attr = xml_get_current_tag_attr(file); */
1930 /* addrbook_show_attribs( attr ); */
1931 if (xml_compare_tag(file
, AB_ELTAG_ADDRESS
))
1932 addrbook_chkparse_addr_list(book
, file
);
1937 * Attempt to parse attributes for person address from file.
1938 * \param book Address book.
1939 * \param file XML file handle.
1941 static void addrbook_chkparse_attribute(AddressBookFile
*book
, XMLFile
*file
)
1944 /* gchar *element; */
1946 /* attr = xml_get_current_tag_attr(file); */
1947 /* addrbook_show_attribs( attr ); */
1948 /* element = xml_get_element(file); */
1949 /* g_print( "\t\tattrib value : %s\n", element ); */
1953 * Attempt to parse list of attributes for person address from file.
1954 * \param book Address book.
1955 * \param file XML file handle.
1957 static void addrbook_chkparse_attr_list(AddressBookFile
*book
, XMLFile
*file
)
1962 prev_level
= file
->level
;
1963 if (xml_parse_next_tag(file
))
1964 longjmp(book
->jumper
, 1);
1965 if (file
->level
< prev_level
)
1967 if (xml_compare_tag(file
, AB_ELTAG_ATTRIBUTE
)) {
1968 addrbook_chkparse_attribute(book
, file
);
1969 addrbook_chkparse_attr_list(book
, file
);
1975 * Attempt to parse person from file.
1976 * \param book Address book.
1977 * \param file XML file handle.
1979 static void addrbook_chkparse_person(AddressBookFile
*book
, XMLFile
*file
)
1983 /* attr = xml_get_current_tag_attr(file); */
1984 /* addrbook_show_attribs( attr ); */
1985 if (xml_parse_next_tag(file
)) /* Consume closing tag */
1986 longjmp(book
->jumper
, 1);
1988 if (xml_compare_tag(file
, AB_ELTAG_ADDRESS_LIST
))
1989 addrbook_chkparse_addr_list(book
, file
);
1991 if (xml_parse_next_tag(file
)) /* Consume closing tag */
1992 longjmp(book
->jumper
, 1);
1994 if (xml_compare_tag(file
, AB_ELTAG_ATTRIBUTE_LIST
))
1995 addrbook_chkparse_attr_list(book
, file
);
1999 * Attempt to parse list of members from file.
2000 * \param book Address book.
2001 * \param file XML file handle.
2003 static void addrbook_chkparse_member_list(AddressBookFile
*book
, XMLFile
*file
)
2009 prev_level
= file
->level
;
2010 if (xml_parse_next_tag(file
))
2011 longjmp(book
->jumper
, 1);
2013 if (file
->level
< prev_level
)
2016 if (xml_compare_tag(file
, AB_ELTAG_MEMBER
)) {
2017 /* attr = xml_get_current_tag_attr(file); */
2018 /* addrbook_show_attribs( attr ); */
2019 addrbook_chkparse_member_list(book
, file
);
2022 /* attr = xml_get_current_tag_attr(file); */
2023 /* addrbook_show_attribs( attr ); */
2029 * Attempt to parse group from file.
2030 * \param book Address book.
2031 * \param file XML file handle.
2033 static void addrbook_chkparse_group(AddressBookFile
*book
, XMLFile
*file
)
2037 /* attr = xml_get_current_tag_attr(file); */
2038 /* addrbook_show_attribs( attr ); */
2039 if (xml_parse_next_tag(file
)) /* Consume closing tag */
2040 longjmp(book
->jumper
, 1);
2042 if (xml_compare_tag(file
, AB_ELTAG_MEMBER_LIST
))
2043 addrbook_chkparse_member_list(book
, file
);
2047 * Attempt to parse list of folders from file.
2048 * \param book Address book.
2049 * \param file XML file handle.
2051 static void addrbook_chkparse_folder_list(AddressBookFile
*book
, XMLFile
*file
)
2057 prev_level
= file
->level
;
2058 if (xml_parse_next_tag(file
))
2059 longjmp(book
->jumper
, 1);
2061 if (file
->level
< prev_level
)
2064 if (xml_compare_tag(file
, AB_ELTAG_ITEM
)) {
2065 /* attr = xml_get_current_tag_attr(file); */
2066 /* addrbook_show_attribs( attr ); */
2067 addrbook_chkparse_folder_list(book
, file
);
2070 /* attr = xml_get_current_tag_attr(file); */
2071 /* addrbook_show_attribs( attr ); */
2077 * Attempt to parse a folder from file.
2078 * \param book Address book.
2079 * \param file XML file handle.
2081 static void addrbook_chkparse_folder(AddressBookFile
*book
, XMLFile
*file
)
2085 /* attr = xml_get_current_tag_attr(file); */
2086 /* addrbook_show_attribs( attr ); */
2087 if (xml_parse_next_tag(file
)) /* Consume closing tag */
2088 longjmp(book
->jumper
, 1);
2090 if (xml_compare_tag(file
, AB_ELTAG_ITEM_LIST
))
2091 addrbook_chkparse_folder_list(book
, file
);
2095 * Attempt to parse (DOM) tree from file.
2096 * \param book Address book.
2097 * \param file XML file handle.
2099 static gboolean
addrbook_chkread_tree(AddressBookFile
*book
, XMLFile
*file
)
2104 if (xml_get_dtd(file
))
2107 if (xml_parse_next_tag(file
))
2110 if (!xml_compare_tag(file
, AB_ELTAG_ADDRESS_BOOK
))
2113 /* attr = xml_get_current_tag_attr(file); */
2114 /* addrbook_show_attribs( attr ); */
2121 if (xml_parse_next_tag(file
))
2122 longjmp(book
->jumper
, 1);
2124 /* Get next tag (person, group or folder) */
2125 if (xml_compare_tag(file
, AB_ELTAG_PERSON
))
2126 addrbook_chkparse_person( book
, file
);
2127 else if (xml_compare_tag(file
, AB_ELTAG_GROUP
))
2128 addrbook_chkparse_group(book
, file
);
2129 else if (xml_compare_tag(file
, AB_ELTAG_FOLDER
))
2130 addrbook_chkparse_folder(book
, file
);
2136 * Test address book file by parsing contents.
2137 * \param book Address book.
2138 * \param fileName Filename of XML file.
2139 * \return Status code <i>MGU_SUCCESS</i> if file appears to be valid format.
2141 gint
addrbook_test_read_file(AddressBookFile
*book
, gchar
*fileName
)
2143 XMLFile
*file
= NULL
;
2144 gchar
*fileSpec
= NULL
;
2146 cm_return_val_if_fail(book
!= NULL
, -1);
2148 fileSpec
= g_strconcat(book
->path
, G_DIR_SEPARATOR_S
, fileName
, NULL
);
2149 book
->retVal
= MGU_OPEN_FILE
;
2150 file
= xml_open_file(fileSpec
);
2153 book
->retVal
= MGU_BAD_FORMAT
;
2154 if (setjmp(book
->jumper
)) {
2155 /* g_print( "Caught Ya!!!\n" ); */
2156 xml_close_file(file
);
2157 return book
->retVal
;
2159 if (addrbook_chkread_tree(book
, file
))
2160 book
->retVal
= MGU_SUCCESS
;
2162 xml_close_file( file
);
2164 return book
->retVal
;
2168 * Return link list of all persons in address book. Note that the list
2169 * contains references to items. Do <b>*NOT*</b> attempt to use the
2170 * <code>addrbook_free_xxx()</code> functions... this will destroy the
2172 * \param book Address book.
2173 * \return List of persons, or NULL if none.
2175 GList
*addrbook_get_all_persons(AddressBookFile
*book
)
2177 cm_return_val_if_fail(book
!= NULL
, NULL
);
2178 return addrcache_get_all_persons(book
->addressCache
);
2181 GList
*addrbook_get_all_groups(AddressBookFile
*book
)
2183 cm_return_val_if_fail(book
!= NULL
, NULL
);
2184 return addrcache_get_all_groups(book
->addressCache
);
2188 * Add person and address data to address book.
2189 * \param book Address book.
2190 * \param folder Folder where to add person, or NULL for root folder.
2191 * \param name Common name.
2192 * \param address EMail address.
2193 * \param remarks Remarks.
2194 * \return Person added. Do not <b>*NOT*</b> to use the
2195 * <code>addrbook_free_xxx()</code> functions... this will destroy
2196 * the address book data.
2198 ItemPerson
*addrbook_add_contact(AddressBookFile
*book
, ItemFolder
*folder
,
2199 const gchar
*name
,const gchar
*address
,
2200 const gchar
*remarks
)
2204 cm_return_val_if_fail(book
!= NULL
, NULL
);
2205 person
= addrcache_add_contact(
2206 book
->addressCache
, folder
, name
, address
, remarks
);
2211 * Return file name for next address book file.
2212 * \param book Address book.
2213 * \return File name, or <i>NULL</i> if could not create. This should be
2214 * <code>g_free()</code> when done.
2216 gchar
*addrbook_guess_next_file(AddressBookFile
*book
)
2218 gchar
*newFile
= NULL
;
2219 GList
*fileList
= NULL
;
2221 fileList
= addrbook_get_bookfile_list(book
);
2223 fileNum
= 1 + book
->maxValue
;
2225 newFile
= addrbook_gen_new_file_name(fileNum
);
2226 g_list_free(fileList
);
2231 void addrbook_delete_book_file(AddressBookFile
*book
)
2235 if (!book
->path
|| !book
->fileName
)
2238 book_path
= g_strconcat(book
->path
, G_DIR_SEPARATOR_S
,
2239 book
->fileName
, NULL
);
2240 claws_unlink(book_path
);