Fix bug #3574: Template addressing
[claws.git] / src / addritem.c
blobae3dc4e4e1caa1848e82b96dd1d6147b5a7e9465
1 /*
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/>.
21 * General primitive address item objects.
24 #include <glib.h>
25 #include <stdio.h>
26 #include <string.h>
28 #include "utils.h"
29 #include "addritem.h"
30 #include "mgutils.h"
31 #include "codeconv.h"
33 /**
34 * Create new email address item.
35 * \return Initialized email item.
37 ItemEMail *addritem_create_item_email( void ) {
38 ItemEMail *item;
39 item = g_new0( ItemEMail, 1 );
40 ADDRITEM_TYPE(item) = ITEMTYPE_EMAIL;
41 ADDRITEM_ID(item) = NULL;
42 ADDRITEM_NAME(item) = NULL;
43 ADDRITEM_PARENT(item) = NULL;
44 ADDRITEM_SUBTYPE(item) = 0;
45 item->address = NULL;
46 item->remarks = NULL;
47 return item;
50 /**
51 * Create a shallow copy of specified email address item.
52 * \param item E-Mail to copy.
53 * \return Copy of email, or <i>NULL</i> if null argument supplied.
55 ItemEMail *addritem_copy_item_email( ItemEMail *item ) {
56 ItemEMail *itemNew = NULL;
57 if( item ) {
58 itemNew = addritem_create_item_email();
59 ADDRITEM_NAME(itemNew) = g_strdup( ADDRITEM_NAME(item) );
60 itemNew->address = g_strdup( item->address );
61 itemNew->remarks = g_strdup( item->remarks );
63 return itemNew;
66 /**
67 * Create a full copy (deep copy) of specified email address item.
68 * \param item E-Mail to copy.
69 * \return Copy of email.
71 ItemEMail *addritem_copyfull_item_email( ItemEMail *item ) {
72 ItemEMail *itemNew = NULL;
73 if( item ) {
74 itemNew = addritem_create_item_email();
75 ADDRITEM_ID(itemNew) = g_strdup( ADDRITEM_ID(item) );
76 ADDRITEM_NAME(itemNew) = g_strdup( ADDRITEM_NAME(item) );
77 ADDRITEM_PARENT(itemNew) = ADDRITEM_PARENT(item);
78 itemNew->address = g_strdup( item->address );
79 itemNew->remarks = g_strdup( item->remarks );
81 return itemNew;
84 /**
85 * Specify alias for email.
86 * \param email E-Mail item.
87 * \param value Alias.
89 void addritem_email_set_alias( ItemEMail *email, const gchar *value ) {
90 ADDRITEM_NAME(email) = mgu_replace_string( ADDRITEM_NAME(email), value );
93 /**
94 * Specify address for email.
95 * \param email E-Mail item.
96 * \param value Address.
98 void addritem_email_set_address( ItemEMail *email, const gchar *value ) {
99 email->address = mgu_replace_string( email->address, value );
103 * Specify remarks for email.
104 * \param email E-Mail item.
105 * \param value Remarks.
107 void addritem_email_set_remarks( ItemEMail *email, const gchar *value ) {
108 email->remarks = mgu_replace_string( email->remarks, value );
112 * Free address item email object.
113 * \param item E-Mail item to free.
115 void addritem_free_item_email( ItemEMail *item ) {
116 cm_return_if_fail( item != NULL );
118 /* Free internal stuff */
119 g_free( ADDRITEM_ID(item) );
120 g_free( ADDRITEM_NAME(item) );
121 g_free( item->address );
122 g_free( item->remarks );
124 ADDRITEM_OBJECT(item)->type = ITEMTYPE_NONE;
125 ADDRITEM_ID(item) = NULL;
126 ADDRITEM_NAME(item) = NULL;
127 ADDRITEM_PARENT(item) = NULL;
128 ADDRITEM_SUBTYPE(item) = 0;
129 item->address = NULL;
130 item->remarks = NULL;
131 g_free( item );
135 * Create new attribute object.
136 * \return Initialized attribute object.
138 UserAttribute *addritem_create_attribute( void ) {
139 UserAttribute *item;
140 item = g_new0( UserAttribute, 1 );
141 item->uid = NULL;
142 item->name = NULL;
143 item->value = NULL;
144 return item;
148 * Create copy (deep copy) of specified attribute.
149 * \param item Attribute to copy.
150 * \return Copy of attribute, or <i>NULL</i> if null argument supplied.
152 UserAttribute *addritem_copy_attribute( UserAttribute *item ) {
153 UserAttribute *itemNew = NULL;
154 if( item ) {
155 itemNew = addritem_create_attribute();
156 itemNew->uid = g_strdup( item->uid );
157 itemNew->name = g_strdup( item->name );
158 itemNew->value = g_strdup( item->value );
160 return itemNew;
164 * Specify ID for attribute.
165 * \param item Attribute object.
166 * \param value ID.
168 void addritem_attrib_set_id( UserAttribute *item, const gchar *value ) {
169 cm_return_if_fail( item != NULL );
170 item->uid = mgu_replace_string( item->uid, value );
174 * Specify name for attribute.
175 * \param item Attribute object.
176 * \param value Name.
178 void addritem_attrib_set_name( UserAttribute *item, const gchar *value ) {
179 cm_return_if_fail( item != NULL );
180 item->name = mgu_replace_string( item->name, value );
184 * Specify value for attribute.
185 * \param item Attribute object.
186 * \param value Value.
188 void addritem_attrib_set_value( UserAttribute *item, const gchar *value ) {
189 cm_return_if_fail( item != NULL );
190 item->value = mgu_replace_string( item->value, value );
194 * Free user attribute.
195 * \param item Attribute object to free.
197 void addritem_free_attribute( UserAttribute *item ) {
198 cm_return_if_fail( item != NULL );
199 g_free( item->uid );
200 g_free( item->name );
201 g_free( item->value );
202 item->uid = NULL;
203 item->name = NULL;
204 item->value = NULL;
205 g_free( item );
209 * Create new address book person.
210 * \return Initialized person object.
212 ItemPerson *addritem_create_item_person( void ) {
213 ItemPerson *person;
214 person = g_new0( ItemPerson, 1 );
215 ADDRITEM_TYPE(person) = ITEMTYPE_PERSON;
216 ADDRITEM_ID(person) = NULL;
217 ADDRITEM_NAME(person) = NULL;
218 ADDRITEM_PARENT(person) = NULL;
219 ADDRITEM_SUBTYPE(person) = 0;
220 person->picture = NULL;
221 person->firstName = NULL;
222 person->lastName = NULL;
223 person->nickName = NULL;
224 person->listEMail = NULL;
225 person->listAttrib = NULL;
226 person->externalID = NULL;
227 person->isOpened = FALSE;
228 person->status = NONE;
229 return person;
233 * Create a shallow copy of address book person object.
234 * \param item Person to copy.
235 * \return Copy of person, or <i>NULL</i> if null argument supplied.
237 ItemPerson *addritem_copy_item_person( ItemPerson *item ) {
238 ItemPerson *itemNew;
240 itemNew = NULL;
241 if( item ) {
242 itemNew = addritem_create_item_person();
243 ADDRITEM_NAME(itemNew) = g_strdup( ADDRITEM_NAME(item) );
244 itemNew->picture = g_strdup( ADDRITEM_ID(itemNew) );
245 itemNew->firstName = g_strdup( item->firstName );
246 itemNew->lastName = g_strdup( item->lastName );
247 itemNew->nickName = g_strdup( item->nickName );
248 itemNew->externalID = g_strdup( item->externalID );
249 itemNew->status = item->status;
251 return itemNew;
255 * Specify picture for person object.
256 * \param person Person object.
257 * \param value Picture.
259 void addritem_person_set_picture( ItemPerson *person, const gchar *value ) {
260 if (!value || g_utf8_validate(value, -1, NULL))
261 person->picture = mgu_replace_string( person->picture, value );
262 else {
263 gchar *out = conv_codeset_strdup(value,
264 conv_get_locale_charset_str_no_utf8(),
265 CS_INTERNAL);
266 if (out)
267 person->picture = mgu_replace_string( person->picture, out );
268 g_free(out);
273 * Get picture for person object.
274 * \param person Person object.
275 * \param value Picture.
277 gchar *addritem_person_get_picture( ItemPerson *person) {
278 if (person->picture)
279 return g_strdup(person->picture);
280 return NULL;
283 * Specify first name for person object.
284 * \param person Person object.
285 * \param value Name.
287 void addritem_person_set_first_name( ItemPerson *person, const gchar *value ) {
288 if (!value || g_utf8_validate(value, -1, NULL))
289 person->firstName = mgu_replace_string( person->firstName, value );
290 else {
291 gchar *out = conv_codeset_strdup(value,
292 conv_get_locale_charset_str_no_utf8(),
293 CS_INTERNAL);
294 if (out)
295 person->firstName = mgu_replace_string( person->firstName, out );
296 g_free(out);
301 * Specify last name for person object.
302 * \param person Person object.
303 * \param value name.
305 void addritem_person_set_last_name( ItemPerson *person, const gchar *value ) {
306 if (!value || g_utf8_validate(value, -1, NULL))
307 person->lastName = mgu_replace_string( person->lastName, value );
308 else {
309 gchar *out = conv_codeset_strdup(value,
310 conv_get_locale_charset_str_no_utf8(),
311 CS_INTERNAL);
312 if (out)
313 person->lastName = mgu_replace_string( person->lastName, out );
314 g_free(out);
319 * Specify nick name for person object.
320 * \param person Person object.
321 * \param value name.
323 void addritem_person_set_nick_name( ItemPerson *person, const gchar *value ) {
324 if (!value || g_utf8_validate(value, -1, NULL))
325 person->nickName = mgu_replace_string( person->nickName, value );
326 else {
327 gchar *out = conv_codeset_strdup(value,
328 conv_get_locale_charset_str_no_utf8(),
329 CS_INTERNAL);
330 if (out)
331 person->nickName = mgu_replace_string( person->nickName, out );
332 g_free(out);
337 * Specify common name for person object.
338 * \param person Person object.
339 * \param value name.
341 void addritem_person_set_common_name( ItemPerson *person, const gchar *value ) {
342 if (!value || g_utf8_validate(value, -1, NULL))
343 ADDRITEM_NAME(person) = mgu_replace_string( ADDRITEM_NAME(person), value );
344 else {
345 gchar *out = conv_codeset_strdup(value,
346 conv_get_locale_charset_str_no_utf8(),
347 CS_INTERNAL);
348 if (out)
349 ADDRITEM_NAME(person) = mgu_replace_string( ADDRITEM_NAME(person), out );
350 g_free(out);
355 * Specify external ID for person object.
356 * \param person Person object.
357 * \param value ID.
359 void addritem_person_set_external_id( ItemPerson *person, const gchar *value ) {
360 person->externalID = mgu_replace_string( person->externalID, value );
364 * Specify value of open indicator for person object. This is typically used to
365 * simplify open/close folders in the address book GUI.
366 * \param person Person object.
367 * \param value Value for indicator. Set to <i>TRUE</i> if opened.
369 void addritem_person_set_opened( ItemPerson *person, const gboolean value ) {
370 person->isOpened = value;
374 * Free linked list of item addresses; both addresses and the list are freed.
375 * It is assumed that addresses are *NOT* contained within some other
376 * container.
377 * \param list List of addresses to be freed.
379 void addritem_free_list_email( GList *list ) {
380 GList *node = list;
381 while( node ) {
382 ItemEMail *email = node->data;
384 addritem_free_item_email( email );
385 node->data = NULL;
386 node = g_list_next( node );
388 g_list_free( list );
389 list = NULL;
393 * Free linked list of attributes; both attributes and the list are freed.
394 * It is assumed that attributes are *NOT* contained within some other
395 * container.
396 * \param list List of attributes to be freed.
398 void addritem_free_list_attribute( GList *list ) {
399 GList *node = list;
400 while( node ) {
401 addritem_free_attribute( node->data );
402 node->data = NULL;
403 node = g_list_next( node );
405 g_list_free( list );
409 * Free address person object.
410 * \param person Person object to free.
412 void addritem_free_item_person( ItemPerson *person ) {
413 cm_return_if_fail( person != NULL );
415 /* Free internal stuff */
416 g_free( ADDRITEM_ID(person) );
417 g_free( ADDRITEM_NAME(person) );
418 g_free( person->picture );
419 g_free( person->firstName );
420 g_free( person->lastName );
421 g_free( person->nickName );
422 g_free( person->externalID );
423 g_list_free( person->listEMail );
424 addritem_free_list_attribute( person->listAttrib );
426 ADDRITEM_OBJECT(person)->type = ITEMTYPE_NONE;
427 ADDRITEM_ID(person) = NULL;
428 ADDRITEM_NAME(person) = NULL;
429 ADDRITEM_PARENT(person) = NULL;
430 ADDRITEM_SUBTYPE(person) = 0;
431 person->picture = NULL;
432 person->firstName = NULL;
433 person->lastName = NULL;
434 person->nickName = NULL;
435 person->externalID = NULL;
436 person->listEMail = NULL;
437 person->listAttrib = NULL;
439 g_free( person );
443 * Print E-Mail address object for debug.
444 * \param item Item to print.
445 * \param stream Output stream.
447 void addritem_print_item_email( ItemEMail *item, FILE *stream ) {
448 cm_return_if_fail( item != NULL );
449 fprintf( stream, "\t\tt/id: %d : '%s'\n", ADDRITEM_TYPE(item), ADDRITEM_ID(item) );
450 fprintf( stream, "\t\tsubty: %d\n", ADDRITEM_SUBTYPE(item) );
451 fprintf( stream, "\t\talis: '%s'\n", ADDRITEM_NAME(item) );
452 fprintf( stream, "\t\taddr: '%s'\n", item->address );
453 fprintf( stream, "\t\trems: '%s'\n", item->remarks );
454 fprintf( stream, "\t\t---\n" );
458 * Print user attribute object for debug.
459 * \param item Attribute to print.
460 * \param stream Output stream.
462 static void addritem_print_attribute( UserAttribute *item, FILE *stream ) {
463 cm_return_if_fail( item != NULL );
464 fprintf( stream, "\t\tuid : '%s'\n", item->uid );
465 fprintf( stream, "\t\tname : '%s'\n", item->name );
466 fprintf( stream, "\t\tvalue: '%s'\n", item->value );
467 fprintf( stream, "\t\t---\n" );
471 * Print person item for debug.
472 * \param person Person to print.
473 * \param stream Output stream.
475 void addritem_print_item_person( ItemPerson *person, FILE *stream ) {
476 GList *node;
477 cm_return_if_fail( person != NULL );
478 fprintf( stream, "Person:\n" );
479 fprintf( stream, "\tt/uid: %d : '%s'\n", ADDRITEM_TYPE(person), ADDRITEM_ID(person) );
480 fprintf( stream, "\tsubty: %d\n", ADDRITEM_SUBTYPE(person) );
481 fprintf( stream, "\tcommn: '%s'\n", ADDRITEM_NAME(person) );
482 fprintf( stream, "\tphoto: '%s'\n", person->picture );
483 fprintf( stream, "\tfirst: '%s'\n", person->firstName );
484 fprintf( stream, "\tlast : '%s'\n", person->lastName );
485 fprintf( stream, "\tnick : '%s'\n", person->nickName );
486 fprintf( stream, "\textID: '%s'\n", person->externalID );
487 fprintf( stream, "\teMail:\n" );
488 fprintf( stream, "\t---\n" );
489 node = person->listEMail;
490 while( node ) {
491 addritem_print_item_email( node->data, stream );
492 node = g_list_next( node );
494 fprintf( stream, "\tuAttr:\n" );
495 fprintf( stream, "\t---\n" );
496 node = person->listAttrib;
497 while( node ) {
498 addritem_print_attribute( node->data, stream );
499 node = g_list_next( node );
501 gchar *current_status;
502 switch (person->status) {
503 case NONE: current_status = g_strdup("Unchanged"); break;
504 case ADD_ENTRY: current_status = g_strdup("New"); break;
505 case UPDATE_ENTRY: current_status = g_strdup("Updated"); break;
506 case DELETE_ENTRY: current_status = g_strdup("Deleted"); break;
507 default: current_status = g_strdup("Unknown");
509 fprintf( stream, "\t\tStatus: %s\n", current_status );
510 if ( current_status )
511 g_free(current_status);
512 fprintf( stream, "\t===\n" );
516 * Add E-Mail address object to person.
517 * \param person Person.
518 * \param email E-Mail object to add.
519 * \return <i>TRUE</i> if E-Mail added.
521 gboolean addritem_person_add_email( ItemPerson *person, ItemEMail *email ) {
522 GList *node;
524 cm_return_val_if_fail( person != NULL, FALSE );
525 cm_return_val_if_fail( email != NULL, FALSE );
527 node = person->listEMail;
528 while( node ) {
529 if( node->data == email ) return FALSE;
530 node = g_list_next( node );
532 person->listEMail = g_list_append( person->listEMail, email );
533 ADDRITEM_PARENT(email) = ADDRITEM_OBJECT(person);
534 return TRUE;
538 * Remove email address for specified person.
539 * \param person Person.
540 * \param email EMail to remove.
541 * \return EMail object, or <i>NULL</i> if not found. Note that object should
542 * still be freed after calling this method.
544 ItemEMail *addritem_person_remove_email( ItemPerson *person, ItemEMail *email ) {
545 gboolean found = FALSE;
546 GList *node;
548 cm_return_val_if_fail( person != NULL, NULL );
549 if( email == NULL ) return NULL;
551 /* Look for email */
552 node = person->listEMail;
553 while( node ) {
554 if( node-> data == email ) {
555 found = TRUE;
556 break;
558 node = g_list_next( node );
561 if( found ) {
562 /* Remove email from person's address list */
563 if( person->listEMail ) {
564 person->listEMail = g_list_remove( person->listEMail, email );
566 /* Unlink reference to person. */
567 ADDRITEM_PARENT(email) = NULL;
568 return email;
570 return NULL;
574 * Add user attribute to specified person.
575 * \param person Person.
576 * \param attrib Attribute to add.
577 * \return <i>TRUE</i> if item added.
579 void addritem_person_add_attribute(
580 ItemPerson *person, UserAttribute *attrib )
582 cm_return_if_fail( person != NULL );
583 person->listAttrib = g_list_append( person->listAttrib, attrib );
587 * Remove user attribute from specified person.
588 * \param person Person.
589 * \param attrib Attribute to remove.
591 void addritem_person_remove_attribute( ItemPerson *person, const gchar *attrib ) {
592 cm_return_if_fail( person != NULL && attrib != NULL );
593 GList *attrib_list;
595 attrib_list = person->listAttrib;
596 while (attrib_list) {
597 UserAttribute *user_attrib = attrib_list->data;
598 if (strcmp(user_attrib->name, attrib) == 0) {
599 person->listAttrib = g_list_remove(person->listAttrib, (gconstpointer) user_attrib);
600 break;
602 attrib_list = g_list_next(attrib_list);
608 * Create new address book group object.
609 * \return Initialized group object.
611 ItemGroup *addritem_create_item_group( void ) {
612 ItemGroup *group;
614 group = g_new0( ItemGroup, 1 );
615 ADDRITEM_TYPE(group) = ITEMTYPE_GROUP;
616 ADDRITEM_ID(group) = NULL;
617 ADDRITEM_NAME(group) = NULL;
618 ADDRITEM_PARENT(group) = NULL;
619 ADDRITEM_SUBTYPE(group) = 0;
620 group->remarks = NULL;
621 group->listEMail = NULL;
622 return group;
626 * Copy (deep copy) address book group.
627 * \param item Group to copy.
628 * \return Copy of the group object, or <i>NULL</i> if null argument supplied.
630 ItemGroup *addritem_copy_item_group( ItemGroup *item ) {
631 ItemGroup *itemNew;
633 itemNew = NULL;
634 if( item ) {
635 itemNew = addritem_create_item_group();
636 ADDRITEM_NAME(itemNew) = g_strdup( ADDRITEM_NAME(item) );
637 itemNew->remarks = g_strdup( item->remarks );
639 return itemNew;
643 * Specify name to be used for group.
644 * \param group Group object.
645 * \param value Name of group.
647 void addritem_group_set_name( ItemGroup *group, const gchar *value ) {
648 ADDRITEM_NAME(group) = mgu_replace_string( ADDRITEM_NAME(group), value );
652 * Free address group object.
653 * \param group Group to free.
655 void addritem_free_item_group( ItemGroup *group ) {
656 cm_return_if_fail( group != NULL );
658 /* Free internal stuff */
659 g_free( ADDRITEM_ID(group) );
660 g_free( ADDRITEM_NAME(group) );
661 g_free( group->remarks );
662 mgu_clear_list( group->listEMail );
663 g_list_free( group->listEMail );
665 ADDRITEM_TYPE(group) = ITEMTYPE_NONE;
666 ADDRITEM_ID(group) = NULL;
667 ADDRITEM_NAME(group) = NULL;
668 ADDRITEM_PARENT(group) = NULL;
669 ADDRITEM_SUBTYPE(group) = 0;
670 group->remarks = NULL;
671 group->listEMail = NULL;
673 g_free( group );
677 * Add EMail address to group. Note that a reference to an E-Mail item is
678 * added to a group. A person object is the only container that for an
679 * address.
680 * \param group Group.
681 * \param email E-Mail object.
682 * \return <i>TRUE</i> if email item added.
684 gboolean addritem_group_add_email( ItemGroup *group, ItemEMail *email ) {
685 GList *node;
687 cm_return_val_if_fail( group != NULL, FALSE );
688 cm_return_val_if_fail( email != NULL, FALSE );
690 node = group->listEMail;
691 while( node ) {
692 if( node->data == email ) return FALSE;
693 node = g_list_next( node );
695 group->listEMail = g_list_append( group->listEMail, email );
696 return TRUE;
700 * Remove person object for specified group.
701 * \param group Group from which to remove address.
702 * \param email EMail to remove
703 * \return EMail object, or <i>NULL if email not found in group. Note that
704 * this object is referenced (linked) to a group and should *NOT*
705 * be freed. An E-Mail object object should only be freed after
706 * removing from a person.
708 ItemPerson *addritem_folder_remove_person( ItemFolder *group, ItemPerson *person ) {
709 if( group && person ) {
710 GList *node = group->listPerson;
711 while( node ) {
712 if( node->data == person ) {
713 group->listPerson = g_list_remove( group->listPerson, person );
714 return person;
716 node = g_list_next( node );
719 return NULL;
723 * Print address group item for debug.
724 * \param group Group to print.
725 * \param stream Output stream.
727 void addritem_print_item_group( ItemGroup *group, FILE *stream ) {
728 GList *node;
729 ItemPerson *person;
730 ItemEMail *item;
731 cm_return_if_fail( group != NULL );
732 fprintf( stream, "Group:\n" );
733 fprintf( stream, "\tt/u: %d : '%s'\n", ADDRITEM_TYPE(group), ADDRITEM_ID(group) );
734 fprintf( stream, "\tsub: %d\n", ADDRITEM_SUBTYPE(group) );
735 fprintf( stream, "\tgrp: '%s'\n", ADDRITEM_NAME(group) );
736 fprintf( stream, "\trem: '%s'\n", group->remarks );
737 fprintf( stream, "\t---\n" );
738 node = group->listEMail;
739 while( node ) {
740 item = node->data;
741 person = ( ItemPerson * ) ADDRITEM_PARENT(item);
742 if( person ) {
743 fprintf( stream, "\t\tpid : '%s'\n", ADDRITEM_ID(person) );
744 fprintf( stream, "\t\tcomn: '%s'\n", ADDRITEM_NAME(person) );
746 else {
747 fprintf( stream, "\t\tpid : ???\n" );
748 fprintf( stream, "\t\tcomn: ???\n" );
750 addritem_print_item_email( item, stream );
751 node = g_list_next( node );
753 fprintf( stream, "\t***\n" );
757 * Create new address folder.
758 * \return Initialized address folder object.
760 ItemFolder *addritem_create_item_folder( void ) {
761 ItemFolder *folder;
762 folder = g_new0( ItemFolder, 1 );
763 ADDRITEM_TYPE(folder) = ITEMTYPE_FOLDER;
764 ADDRITEM_ID(folder) = NULL;
765 ADDRITEM_NAME(folder) = NULL;
766 ADDRITEM_PARENT(folder) = NULL;
767 ADDRITEM_SUBTYPE(folder) = 0;
768 folder->remarks = NULL;
769 folder->isRoot = FALSE;
770 folder->listItems = NULL;
771 folder->listFolder = NULL;
772 folder->listPerson = NULL;
773 folder->listGroup = NULL;
774 folder->folderType = ADDRFOLDER_NONE;
775 folder->folderData = NULL;
776 folder->isHidden = FALSE;
777 return folder;
781 * Copy address book folder. Note that only the folder and not its contents are
782 * copied.
783 * \param item Folder to copy.
784 * \return A copy of the folder, or <i>NULL</i> if null argument supplied.
786 ItemFolder *addritem_copy_item_folder( ItemFolder *item ) {
787 ItemFolder *itemNew;
789 itemNew = g_new0( ItemFolder, 1 );
790 if( item ) {
791 itemNew = addritem_create_item_folder();
792 ADDRITEM_NAME(itemNew) = g_strdup( ADDRITEM_NAME(item) );
793 itemNew->folderType = item->folderType;
795 return itemNew;
799 * Specify name to be used for folder.
800 * \param folder Folder.
801 * \param value Name.
803 void addritem_folder_set_name( ItemFolder *folder, const gchar *value ) {
804 ADDRITEM_NAME(folder) = mgu_replace_string( ADDRITEM_NAME(folder), value );
808 * Specify remarks to be used for folder.
809 * \param folder Folder.
810 * \param value Remarks.
812 void addritem_folder_set_remarks( ItemFolder *folder, const gchar *value ) {
813 folder->remarks = mgu_replace_string( folder->remarks, value );
817 * Specify visibility of folder.
818 * \param folder Folder.
819 * \param value Set to <code>TRUE</code> to hide folder.
821 void addritem_folder_set_hidden( ItemFolder *folder, const gboolean value ) {
822 folder->isHidden = value;
826 * Free address folder. Note: this does not free up the lists of children
827 * (folders, groups and person). This should be done prior to calling this
828 * function.
829 * \param folder Folder to free.
831 void addritem_free_item_folder( ItemFolder *folder ) {
832 cm_return_if_fail( folder != NULL );
834 /* Free internal stuff */
835 g_free( ADDRITEM_ID(folder) );
836 g_free( ADDRITEM_NAME(folder) );
837 g_free( folder->remarks );
838 mgu_clear_list( folder->listItems );
839 g_list_free( folder->listItems );
841 ADDRITEM_TYPE(folder) = ITEMTYPE_NONE;
842 ADDRITEM_ID(folder) = NULL;
843 ADDRITEM_NAME(folder) = NULL;
844 ADDRITEM_PARENT(folder) = NULL;
845 ADDRITEM_SUBTYPE(folder) = 0;
846 folder->isRoot = FALSE;
847 folder->remarks = NULL;
848 folder->listItems = NULL;
849 folder->listFolder = NULL;
850 folder->listGroup = NULL;
851 folder->listPerson = NULL;
852 folder->folderType = ADDRFOLDER_NONE;
853 folder->folderData = NULL;
854 folder->isHidden = FALSE;
856 g_free( folder );
860 * Add person into folder.
861 * \param folder Folder.
862 * \param item Person to add.
863 * \return <i>TRUE</i> if person added.
865 gboolean addritem_folder_add_person( ItemFolder *folder, ItemPerson *item ) {
866 cm_return_val_if_fail( folder != NULL, FALSE );
867 cm_return_val_if_fail( item != NULL, FALSE );
869 folder->listPerson = g_list_append( folder->listPerson, item );
870 ADDRITEM_PARENT(item) = ADDRITEM_OBJECT(folder);
871 return TRUE;
875 * Add folder into folder.
876 * \param folder Folder.
877 * \param item Folder to add.
878 * \return <i>TRUE</i> if folder added.
880 gboolean addritem_folder_add_folder( ItemFolder *folder, ItemFolder *item ) {
881 cm_return_val_if_fail( folder != NULL, FALSE );
882 cm_return_val_if_fail( item != NULL, FALSE );
884 folder->listFolder = g_list_append( folder->listFolder, item );
885 ADDRITEM_PARENT(item) = ADDRITEM_OBJECT(folder);
886 return TRUE;
890 * Add group into folder.
891 * \param folder Folder.
892 * \param item Group to add.
893 * \return <i>TRUE</i> if group added.
895 gboolean addritem_folder_add_group( ItemFolder *folder, ItemGroup *item ) {
896 cm_return_val_if_fail( folder != NULL, FALSE );
897 cm_return_val_if_fail( item != NULL, FALSE );
899 folder->listGroup = g_list_append( folder->listGroup, item );
900 ADDRITEM_PARENT(item) = ADDRITEM_OBJECT(folder);
901 return TRUE;
905 * Print address folder item contents for debug.
906 * \param folder Folder to process.
907 * \param stream Output stream.
909 void addritem_print_item_folder( ItemFolder *folder, FILE *stream ) {
910 GList *node;
911 /* ItemPerson *person; */
912 ItemFolder *parent;
914 cm_return_if_fail( folder != NULL );
916 fprintf( stream, "Folder:\n" );
917 fprintf( stream, "\tt/u: %d : '%s'\n", ADDRITEM_TYPE(folder), ADDRITEM_ID(folder) );
918 fprintf( stream, "\tsub: %d\n", ADDRITEM_SUBTYPE(folder) );
919 fprintf( stream, "\tnam: '%s'\n", ADDRITEM_NAME(folder) );
920 fprintf( stream, "\trem: '%s'\n", folder->remarks );
921 fprintf( stream, "\ttyp: %d\n", folder->folderType );
922 fprintf( stream, "\thid: %s\n", folder->isHidden ? "hidden" : "visible" );
923 fprintf( stream, "\t---\n" );
924 parent = ( ItemFolder * ) ADDRITEM_PARENT(folder);
925 if( parent ) {
926 fprintf( stream, "\tpar: %s : %s\n", ADDRITEM_ID(parent), ADDRITEM_NAME(parent) );
928 else {
929 fprintf( stream, "\tpar: NULL\n" );
931 node = folder->listFolder;
932 while( node ) {
933 AddrItemObject *aio = node->data;
934 if( aio ) {
935 if( aio->type == ITEMTYPE_FOLDER ) {
936 ItemFolder *item = ( ItemFolder * ) aio;
937 addritem_print_item_folder( item, stream );
940 else {
941 fprintf( stream, "\t\tpid : ???\n" );
944 node = g_list_next( node );
947 node = folder->listPerson;
948 while( node ) {
949 AddrItemObject *aio = node->data;
950 if( aio ) {
951 if( aio->type == ITEMTYPE_PERSON ) {
952 ItemPerson *item = ( ItemPerson * ) aio;
953 addritem_print_item_person( item, stream );
956 else {
957 fprintf( stream, "\t\tpid : ???\n" );
960 node = g_list_next( node );
963 node = folder->listGroup;
964 while( node ) {
965 AddrItemObject *aio = node->data;
966 if( aio ) {
967 if( aio->type == ITEMTYPE_GROUP ) {
968 ItemGroup *item = ( ItemGroup * ) aio;
969 addritem_print_item_group( item, stream );
972 else {
973 fprintf( stream, "\t\tpid : ???\n" );
975 node = g_list_next( node );
977 fprintf( stream, "\t###\n" );
981 * Return link list of persons for specified folder. Note that the list contains
982 * references to items and should be g_free() when done. Do *NOT* attempt to use the
983 * addritem_free_xxx() functions... this will destroy the addressbook data!
985 * \param folder Folder to process.
986 * \return List of items, or <i>NULL</i> if none.
988 GList *addritem_folder_get_person_list( ItemFolder *folder ) {
989 GList *list = NULL;
990 GList *node = NULL;
992 cm_return_val_if_fail( folder != NULL, NULL );
994 node = folder->listPerson;
995 while( node ) {
996 ItemPerson *person = node->data;
997 list = g_list_prepend( list, person );
998 node = g_list_next( node );
1000 return g_list_reverse(list);
1004 * Return link list of groups for specified folder. Note that the list contains
1005 * references to items and should be g_free() when done. Do *NOT* attempt to use the
1006 * addritem_free_xxx() functions... this will destroy the addressbook data!
1008 * \param folder Folder to process.
1009 * \return List of items, or <i>NULL</i> if none.
1011 GList *addritem_folder_get_group_list( ItemFolder *folder ) {
1012 GList *list = NULL;
1013 GList *node = NULL;
1015 cm_return_val_if_fail( folder != NULL, NULL );
1017 node = folder->listGroup;
1018 while( node ) {
1019 ItemGroup *group = node->data;
1020 list = g_list_prepend( list, group );
1021 node = g_list_next( node );
1023 return g_list_reverse(list);
1027 * Parse first and last names for person from common name.
1028 * \param person Person to process.
1030 void addritem_parse_first_last( ItemPerson *person ) {
1031 gchar *name;
1032 gchar *fName, *lName;
1033 gchar *p;
1034 gint len, i;
1036 cm_return_if_fail( person != NULL );
1038 name = ADDRITEM_NAME(person);
1039 if( name == NULL ) return;
1041 fName = NULL;
1042 lName = NULL;
1043 p = strchr( name, ',' );
1044 if( p ) {
1045 len = ( size_t ) ( p - name );
1046 lName = g_strndup( name, len );
1047 fName = g_strdup( p + 1 );
1049 else {
1050 /* Other way around */
1051 i = strlen( name );
1052 while( i >= 0 ) {
1053 if( name[i] == ' ' ) {
1054 fName = g_strndup( name, i );
1055 lName = g_strdup( &name[i] );
1056 break;
1058 i--;
1060 if( fName == NULL ) {
1061 fName = g_strdup( name );
1065 g_free( person->firstName );
1066 person->firstName = fName;
1067 if( person->firstName )
1068 g_strstrip( person->firstName );
1070 g_free( person->lastName );
1071 person->lastName = lName;
1072 if( person->lastName )
1073 g_strstrip( person->lastName );
1077 * Build a path of all ancestor folders for specified folder.
1078 * \param folder Folder.
1079 * \param seq Path sequence, FALSE top down, TRUE bottom up.
1080 * \return List of folders from the top down.
1082 GList *addritem_folder_path( const ItemFolder *folder, const gboolean seq ) {
1083 GList *list;
1084 AddrItemObject *item;
1086 list = NULL;
1087 item = ( AddrItemObject * ) folder;
1088 if( seq ) {
1089 while( item ) {
1090 list = g_list_prepend( list, item );
1091 item = ADDRITEM_PARENT( item );
1094 else {
1095 while( item ) {
1096 list = g_list_append( list, item );
1097 item = ADDRITEM_PARENT( item );
1100 return list;
1104 * Format E-Mail address.
1105 * \param email EMail item to format.
1106 * \return Formatted string. Should be freed after use.
1108 gchar *addritem_format_email( ItemEMail *email ) {
1109 gchar *address;
1110 gchar *name;
1111 ItemPerson *person;
1113 address = NULL;
1114 name = NULL;
1115 if( ADDRITEM_NAME( email ) ) {
1116 if( strlen( ADDRITEM_NAME( email ) ) ) {
1117 name = ADDRITEM_NAME( email );
1120 if( ! name ) {
1121 person = ( ItemPerson * ) ADDRITEM_PARENT( email );
1122 name = ADDRITEM_NAME( person );
1125 if( name ) {
1126 if( strchr_with_skip_quote( name, '"', ',' ) ) {
1127 address = g_strdup_printf( "\"%s\" <%s>", name, email->address );
1129 else {
1130 address = g_strdup_printf( "%s <%s>", name, email->address );
1133 else {
1134 address = g_strdup_printf( "%s", email->address );
1136 return address;
1140 * End of Source.