This commit was manufactured by cvs2svn to create tag 'LAST_STABLE'.
[claws.git] / src / addritem.c
blobbc0d4a91a1561b59f62adc5958138390207bcc2d
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2002 Match Grun
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 2 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * General primitive address item objects.
24 #include <glib.h>
25 #include <stdio.h>
26 #include <string.h>
28 #include "addritem.h"
29 #include "mgutils.h"
32 * Create new email address item.
34 ItemEMail *addritem_create_item_email( void ) {
35 ItemEMail *item;
36 item = g_new0( ItemEMail, 1 );
37 ADDRITEM_TYPE(item) = ITEMTYPE_EMAIL;
38 ADDRITEM_ID(item) = NULL;
39 ADDRITEM_NAME(item) = NULL;
40 ADDRITEM_PARENT(item) = NULL;
41 ADDRITEM_SUBTYPE(item) = 0;
42 item->address = NULL;
43 item->remarks = NULL;
44 return item;
48 * Create a shallow copy of specified email address item.
49 * Enter: item E-Mail to copy.
51 ItemEMail *addritem_copy_item_email( ItemEMail *item ) {
52 ItemEMail *itemNew = NULL;
53 if( item ) {
54 itemNew = addritem_create_item_email();
55 ADDRITEM_NAME(itemNew) = g_strdup( ADDRITEM_NAME(item) );
56 itemNew->address = g_strdup( item->address );
57 itemNew->remarks = g_strdup( item->remarks );
59 return itemNew;
63 * Create a full copy of specified email address item.
64 * Enter: item E-Mail to copy.
66 ItemEMail *addritem_copyfull_item_email( ItemEMail *item ) {
67 ItemEMail *itemNew = NULL;
68 if( item ) {
69 itemNew = addritem_create_item_email();
70 ADDRITEM_ID(itemNew) = g_strdup( ADDRITEM_ID(item) );
71 ADDRITEM_NAME(itemNew) = g_strdup( ADDRITEM_NAME(item) );
72 ADDRITEM_PARENT(itemNew) = ADDRITEM_PARENT(item);
73 itemNew->address = g_strdup( item->address );
74 itemNew->remarks = g_strdup( item->remarks );
76 return itemNew;
79 void addritem_email_set_id( ItemEMail *email, const gchar *value ) {
80 ADDRITEM_ID(email) = mgu_replace_string( ADDRITEM_ID(email), value );
82 void addritem_email_set_alias( ItemEMail *email, const gchar *value ) {
83 ADDRITEM_NAME(email) = mgu_replace_string( ADDRITEM_NAME(email), value );
85 void addritem_email_set_address( ItemEMail *email, const gchar *value ) {
86 email->address = mgu_replace_string( email->address, value );
88 void addritem_email_set_remarks( ItemEMail *email, const gchar *value ) {
89 email->remarks = mgu_replace_string( email->remarks, value );
93 * Free address item email.
95 void addritem_free_item_email( ItemEMail *item ) {
96 g_return_if_fail( item != NULL );
98 /* Free internal stuff */
99 g_free( ADDRITEM_ID(item) );
100 g_free( ADDRITEM_NAME(item) );
101 g_free( item->address );
102 g_free( item->remarks );
104 ADDRITEM_OBJECT(item)->type = ITEMTYPE_NONE;
105 ADDRITEM_ID(item) = NULL;
106 ADDRITEM_NAME(item) = NULL;
107 ADDRITEM_PARENT(item) = NULL;
108 ADDRITEM_SUBTYPE(item) = 0;
109 item->address = NULL;
110 item->remarks = NULL;
111 g_free( item );
115 * Create new attribute.
117 UserAttribute *addritem_create_attribute( void ) {
118 UserAttribute *item;
119 item = g_new0( UserAttribute, 1 );
120 item->uid = NULL;
121 item->name = NULL;
122 item->value = NULL;
123 return item;
127 * Create copy of specified attribute.
129 UserAttribute *addritem_copy_attribute( UserAttribute *item ) {
130 UserAttribute *itemNew = NULL;
131 if( item ) {
132 itemNew = addritem_create_attribute();
133 itemNew->uid = g_strdup( item->uid );
134 itemNew->name = g_strdup( item->name );
135 itemNew->value = g_strdup( item->value );
137 return itemNew;
140 void addritem_attrib_set_id( UserAttribute *item, const gchar *value ) {
141 g_return_if_fail( item != NULL );
142 item->uid = mgu_replace_string( item->uid, value );
144 void addritem_attrib_set_name( UserAttribute *item, const gchar *value ) {
145 g_return_if_fail( item != NULL );
146 item->name = mgu_replace_string( item->name, value );
148 void addritem_attrib_set_value( UserAttribute *item, const gchar *value ) {
149 g_return_if_fail( item != NULL );
150 item->value = mgu_replace_string( item->value, value );
154 * Free user attribute.
156 void addritem_free_attribute( UserAttribute *item ) {
157 g_return_if_fail( item != NULL );
158 g_free( item->uid );
159 g_free( item->name );
160 g_free( item->value );
161 item->uid = NULL;
162 item->name = NULL;
163 item->value = NULL;
164 g_free( item );
168 * Create new address book person.
170 ItemPerson *addritem_create_item_person( void ) {
171 ItemPerson *person;
172 person = g_new0( ItemPerson, 1 );
173 ADDRITEM_TYPE(person) = ITEMTYPE_PERSON;
174 ADDRITEM_ID(person) = NULL;
175 ADDRITEM_NAME(person) = NULL;
176 ADDRITEM_PARENT(person) = NULL;
177 ADDRITEM_SUBTYPE(person) = 0;
178 person->firstName = NULL;
179 person->lastName = NULL;
180 person->nickName = NULL;
181 person->listEMail = NULL;
182 person->listAttrib = NULL;
183 person->externalID = NULL;
184 person->isOpened = FALSE;
185 return person;
189 * Create a shallow copy of address book person.
190 * Enter: item Person to copy.
192 ItemPerson *addritem_copy_item_person( ItemPerson *item ) {
193 ItemPerson *itemNew;
195 itemNew = NULL;
196 if( item ) {
197 itemNew = addritem_create_item_person();
198 ADDRITEM_NAME(itemNew) = g_strdup( ADDRITEM_NAME(item) );
199 itemNew->firstName = g_strdup( item->firstName );
200 itemNew->lastName = g_strdup( item->lastName );
201 itemNew->nickName = g_strdup( item->nickName );
202 itemNew->externalID = g_strdup( item->externalID );
204 return itemNew;
207 void addritem_person_set_id( ItemPerson *person, const gchar *value ) {
208 ADDRITEM_ID(person) = mgu_replace_string( ADDRITEM_ID(person), value );
210 void addritem_person_set_first_name( ItemPerson *person, const gchar *value ) {
211 person->firstName = mgu_replace_string( person->firstName, value );
213 void addritem_person_set_last_name( ItemPerson *person, const gchar *value ) {
214 person->lastName = mgu_replace_string( person->lastName, value );
216 void addritem_person_set_nick_name( ItemPerson *person, const gchar *value ) {
217 person->nickName = mgu_replace_string( person->nickName, value );
219 void addritem_person_set_common_name( ItemPerson *person, const gchar *value ) {
220 ADDRITEM_NAME(person) = mgu_replace_string( ADDRITEM_NAME(person), value );
222 void addritem_person_set_external_id( ItemPerson *person, const gchar *value ) {
223 person->externalID = mgu_replace_string( person->externalID, value );
225 void addritem_person_set_opened( ItemPerson *person, const gboolean value ) {
226 person->isOpened = value;
230 * Test whether person's data is empty.
231 * Enter: person Person to test.
232 * Return: TRUE if empty.
234 gboolean addritem_person_empty( ItemPerson *person ) {
235 gchar *t;
237 if( person == NULL ) return FALSE;
239 t = ADDRITEM_NAME(person);
240 if( t != NULL && strlen( t ) > 0 ) return FALSE;
242 t = person->firstName;
243 if( t != NULL && strlen( t ) > 0 ) return FALSE;
245 t = person->lastName;
246 if( t != NULL && strlen( t ) > 0 ) return FALSE;
248 t = person->nickName;
249 if( t != NULL && strlen( t ) > 0 ) return FALSE;
251 if( person->listEMail != NULL ) return FALSE;
252 if( person->listAttrib != NULL ) return FALSE;
254 return TRUE;
258 * Free linked list of item addresses; both addresses and the list are freed.
259 * It is assumed that addresses are *NOT* contained within some other
260 * container.
261 * Enter: list List of addresses to be freed.
263 void addritem_free_list_email( GList *list ) {
264 GList *node = list;
265 while( node ) {
266 ItemEMail *email = node->data;
268 addritem_free_item_email( email );
269 node->data = NULL;
270 node = g_list_next( node );
272 g_list_free( list );
273 list = NULL;
277 * Free linked list of attributes.
279 void addritem_free_list_attribute( GList *list ) {
280 GList *node = list;
281 while( node ) {
282 addritem_free_attribute( node->data );
283 node->data = NULL;
284 node = g_list_next( node );
286 g_list_free( list );
290 * Free address person.
292 void addritem_free_item_person( ItemPerson *person ) {
293 g_return_if_fail( person != NULL );
295 /* Free internal stuff */
296 g_free( ADDRITEM_ID(person) );
297 g_free( ADDRITEM_NAME(person) );
298 g_free( person->firstName );
299 g_free( person->lastName );
300 g_free( person->nickName );
301 g_free( person->externalID );
302 g_list_free( person->listEMail );
303 addritem_free_list_attribute( person->listAttrib );
305 ADDRITEM_OBJECT(person)->type = ITEMTYPE_NONE;
306 ADDRITEM_ID(person) = NULL;
307 ADDRITEM_NAME(person) = NULL;
308 ADDRITEM_PARENT(person) = NULL;
309 ADDRITEM_SUBTYPE(person) = 0;
310 person->firstName = NULL;
311 person->lastName = NULL;
312 person->nickName = NULL;
313 person->externalID = NULL;
314 person->listEMail = NULL;
315 person->listAttrib = NULL;
317 g_free( person );
321 * Print address item.
323 void addritem_print_item_email( ItemEMail *item, FILE *stream ) {
324 g_return_if_fail( item != NULL );
325 fprintf( stream, "\t\tt/id: %d : '%s'\n", ADDRITEM_TYPE(item), ADDRITEM_ID(item) );
326 fprintf( stream, "\t\tsubty: %d\n", ADDRITEM_SUBTYPE(item) );
327 fprintf( stream, "\t\talis: '%s'\n", ADDRITEM_NAME(item) );
328 fprintf( stream, "\t\taddr: '%s'\n", item->address );
329 fprintf( stream, "\t\trems: '%s'\n", item->remarks );
330 fprintf( stream, "\t\t---\n" );
334 * Print user attribute.
336 void addritem_print_attribute( UserAttribute *item, FILE *stream ) {
337 g_return_if_fail( item != NULL );
338 fprintf( stream, "\t\tuid : '%s'\n", item->uid );
339 fprintf( stream, "\t\tname : '%s'\n", item->name );
340 fprintf( stream, "\t\tvalue: '%s'\n", item->value );
341 fprintf( stream, "\t\t---\n" );
345 * Print person item.
347 void addritem_print_item_person( ItemPerson *person, FILE *stream ) {
348 GList *node;
349 g_return_if_fail( person != NULL );
350 fprintf( stream, "Person:\n" );
351 fprintf( stream, "\tt/uid: %d : '%s'\n", ADDRITEM_TYPE(person), ADDRITEM_ID(person) );
352 fprintf( stream, "\tsubty: %d\n", ADDRITEM_SUBTYPE(person) );
353 fprintf( stream, "\tcommn: '%s'\n", ADDRITEM_NAME(person) );
354 fprintf( stream, "\tfirst: '%s'\n", person->firstName );
355 fprintf( stream, "\tlast : '%s'\n", person->lastName );
356 fprintf( stream, "\tnick : '%s'\n", person->nickName );
357 fprintf( stream, "\textID: '%s'\n", person->externalID );
358 fprintf( stream, "\teMail:\n" );
359 fprintf( stream, "\t---\n" );
360 node = person->listEMail;
361 while( node ) {
362 addritem_print_item_email( node->data, stream );
363 node = g_list_next( node );
365 fprintf( stream, "\tuAttr:\n" );
366 fprintf( stream, "\t---\n" );
367 node = person->listAttrib;
368 while( node ) {
369 addritem_print_attribute( node->data, stream );
370 node = g_list_next( node );
372 fprintf( stream, "\t===\n" );
376 * Add EMail address to person.
377 * return: TRUE if item added.
379 gboolean addritem_person_add_email( ItemPerson *person, ItemEMail *email ) {
380 GList *node;
382 g_return_val_if_fail( person != NULL, FALSE );
383 g_return_val_if_fail( email != NULL, FALSE );
385 node = person->listEMail;
386 while( node ) {
387 if( node->data == email ) return FALSE;
388 node = g_list_next( node );
390 person->listEMail = g_list_append( person->listEMail, email );
391 ADDRITEM_PARENT(email) = ADDRITEM_OBJECT(person);
392 return TRUE;
396 * Return email object with specified ID.
397 * param: person Person object.
398 * eid EMail ID.
399 * return: EMail object, or NULL if not found.
401 ItemEMail *addritem_person_get_email( ItemPerson *person, const gchar *eid ) {
402 ItemEMail *email = NULL;
403 GList *node;
405 g_return_val_if_fail( person != NULL, NULL );
406 if( eid == NULL || *eid == '\0' ) return NULL;
408 /* Look for email */
409 node = person->listEMail;
410 while( node ) {
411 AddrItemObject *objE = node->data;
412 gchar *ide = ADDRITEM_ID(objE);
413 if( ide ) {
414 if( strcmp( ide, eid ) == 0 ) {
415 email = ( ItemEMail * ) objE;
418 node = g_list_next( node );
420 return email;
424 * Remove email address for specified person.
425 * param: person Person object.
426 * eid EMail ID.
427 * return: EMail object, or NULL if not found. Note that object should still be freed.
429 ItemEMail *addritem_person_remove_email_id( ItemPerson *person, const gchar *eid ) {
430 ItemEMail *email = NULL;
431 GList *node;
433 g_return_val_if_fail( person != NULL, NULL );
434 if( eid == NULL || *eid == '\0' ) return NULL;
436 /* Look for email */
437 node = person->listEMail;
438 while( node ) {
439 AddrItemObject *objE = node->data;
440 gchar *ide = ADDRITEM_ID(objE);
441 if( ide ) {
442 if( strcmp( ide, eid ) == 0 ) {
443 email = ( ItemEMail * ) objE;
446 node = g_list_next( node );
449 if( email ) {
450 /* Remove email from person's address list */
451 if( person->listEMail ) {
452 person->listEMail = g_list_remove( person->listEMail, email );
454 /* Unlink reference to person. */
455 ADDRITEM_PARENT(email) = NULL;
457 return email;
461 * Remove email address for specified.
462 * param: person Person.
463 * email EMail to remove.
464 * return: EMail object, or NULL if not found. Note that object should still be freed.
466 ItemEMail *addritem_person_remove_email( ItemPerson *person, ItemEMail *email ) {
467 gboolean found = FALSE;
468 GList *node;
470 g_return_val_if_fail( person != NULL, NULL );
471 if( email == NULL ) return NULL;
473 /* Look for email */
474 node = person->listEMail;
475 while( node ) {
476 if( node-> data == email ) {
477 found = TRUE;
478 break;
480 node = g_list_next( node );
483 if( found ) {
484 /* Remove email from person's address list */
485 if( person->listEMail ) {
486 person->listEMail = g_list_remove( person->listEMail, email );
488 /* Unlink reference to person. */
489 ADDRITEM_PARENT(email) = NULL;
490 return email;
492 return NULL;
496 * Add user attribute to person.
497 * return: TRUE if item added.
499 void addritem_person_add_attribute( ItemPerson *person, UserAttribute *attrib ) {
500 g_return_if_fail( person != NULL );
501 person->listAttrib = g_list_append( person->listAttrib, attrib );
505 * Return attribute with specified ID.
506 * param: person Person object.
507 * aid Attribute ID.
508 * return: UserAttribute object, or NULL if not found. Note that object should still be freed.
510 UserAttribute *addritem_person_get_attribute( ItemPerson *person, const gchar *aid ) {
511 UserAttribute *attrib = NULL;
512 GList *node;
514 g_return_val_if_fail( person != NULL, NULL );
515 if( aid == NULL || *aid == '\0' ) return NULL;
517 /* Look for attribute */
518 node = person->listAttrib;
519 while( node ) {
520 UserAttribute *attr = node->data;
521 gchar *ida = attr->uid;
522 if( ida ) {
523 if( strcmp( ida, aid ) == 0 ) {
524 attrib = attr;
527 node = g_list_next( node );
529 return attrib;
533 * Remove attribute from person.
534 * param: person Person object.
535 * aid Attribute ID.
536 * return: UserAttribute object, or NULL if not found. Note that object should still be freed.
538 UserAttribute *addritem_person_remove_attrib_id( ItemPerson *person, const gchar *aid ) {
539 UserAttribute *attrib = NULL;
540 GList *node;
542 g_return_val_if_fail( person != NULL, NULL );
543 if( aid == NULL || *aid == '\0' ) return NULL;
545 /* Look for attribute */
546 node = person->listAttrib;
547 while( node ) {
548 UserAttribute *attr = node->data;
549 gchar *ida = attr->uid;
550 if( ida ) {
551 if( strcmp( ida, aid ) == 0 ) {
552 attrib = attr;
555 node = g_list_next( node );
558 /* Remove email from person's address list */
559 if( person->listAttrib ) {
560 person->listAttrib = g_list_remove( person->listAttrib, attrib );
562 return attrib;
566 * Remove attribute from person.
567 * param: person Person.
568 * attrib Attribute to remove.
569 * return: UserAttribute object. Note that object should still be freed.
571 UserAttribute *addritem_person_remove_attribute( ItemPerson *person, UserAttribute *attrib ) {
572 gboolean found = FALSE;
573 GList *node;
575 g_return_val_if_fail( person != NULL, NULL );
576 if( attrib == NULL ) return NULL;
578 /* Look for attribute */
579 node = person->listAttrib;
580 while( node ) {
581 if( node-> data == attrib ) {
582 found = TRUE;
583 break;
585 node = g_list_next( node );
588 if( found ) {
589 /* Remove attribute */
590 if( person->listAttrib ) {
591 person->listAttrib = g_list_remove( person->listAttrib, attrib );
594 return attrib;
598 * Create new address book group.
600 ItemGroup *addritem_create_item_group( void ) {
601 ItemGroup *group;
603 group = g_new0( ItemGroup, 1 );
604 ADDRITEM_TYPE(group) = ITEMTYPE_GROUP;
605 ADDRITEM_ID(group) = NULL;
606 ADDRITEM_NAME(group) = NULL;
607 ADDRITEM_PARENT(group) = NULL;
608 ADDRITEM_SUBTYPE(group) = 0;
609 group->remarks = NULL;
610 group->listEMail = NULL;
611 return group;
615 * Copy address book group.
616 * Enter: item Group to copy.
617 * Return: A copy of the group.
619 ItemGroup *addritem_copy_item_group( ItemGroup *item ) {
620 ItemGroup *itemNew;
622 itemNew = g_new0( ItemGroup, 1 );
623 if( item ) {
624 itemNew = addritem_create_item_group();
625 ADDRITEM_NAME(itemNew) = g_strdup( ADDRITEM_NAME(item) );
626 itemNew->remarks = g_strdup( item->remarks );
628 return itemNew;
632 * Specify name to be used.
634 void addritem_group_set_id( ItemGroup *group, const gchar *value ) {
635 ADDRITEM_ID(group) = mgu_replace_string( ADDRITEM_ID(group), value );
637 void addritem_group_set_name( ItemGroup *group, const gchar *value ) {
638 ADDRITEM_NAME(group) = mgu_replace_string( ADDRITEM_NAME(group), value );
640 void addritem_group_set_remarks( ItemGroup *group, const gchar *value ) {
641 group->remarks = mgu_replace_string( group->remarks, value );
645 * Free address group.
647 void addritem_free_item_group( ItemGroup *group ) {
648 g_return_if_fail( group != NULL );
650 /* Free internal stuff */
651 g_free( ADDRITEM_ID(group) );
652 g_free( ADDRITEM_NAME(group) );
653 g_free( group->remarks );
654 mgu_clear_list( group->listEMail );
655 g_list_free( group->listEMail );
657 ADDRITEM_TYPE(group) = ITEMTYPE_NONE;
658 ADDRITEM_ID(group) = NULL;
659 ADDRITEM_NAME(group) = NULL;
660 ADDRITEM_PARENT(group) = NULL;
661 ADDRITEM_SUBTYPE(group) = 0;
662 group->remarks = NULL;
663 group->listEMail = NULL;
665 g_free( group );
669 * Add EMail address to group.
670 * return: TRUE if item added.
672 gboolean addritem_group_add_email( ItemGroup *group, ItemEMail *email ) {
673 GList *node;
675 g_return_val_if_fail( group != NULL, FALSE );
676 g_return_val_if_fail( email != NULL, FALSE );
678 node = group->listEMail;
679 while( node ) {
680 if( node->data == email ) return FALSE;
681 node = g_list_next( node );
683 group->listEMail = g_list_append( group->listEMail, email );
684 return TRUE;
688 * Remove email address for specified group.
689 * param: group Group from which to remove address.
690 * email EMail to remove
691 * return: EMail object, or NULL if email not found in group. Note that this object is
692 * referenced (linked) to a group and should *NOT* be freed. This object should only be
693 * freed after removing from a person.
695 ItemEMail *addritem_group_remove_email( ItemGroup *group, ItemEMail *email ) {
696 if( group && email ) {
697 GList *node = group->listEMail;
698 while( node ) {
699 if( node->data == email ) {
700 group->listEMail = g_list_remove( group->listEMail, email );
701 return email;
703 node = g_list_next( node );
706 return NULL;
710 * Remove email address for specified group and ID.
711 * param: group Group from which to remove address.
712 * eid EMail ID.
713 * return: EMail object, or NULL if email not found in group. Note that this object is
714 * referenced (linked) to a group and should *NOT* be freed. This object should only be
715 * freed after removing from a person.
717 ItemEMail *addritem_group_remove_email_id( ItemGroup *group, const gchar *eid ) {
718 if( group ) {
719 GList *node = group->listEMail;
720 while( node ) {
721 ItemEMail *email = ( ItemEMail * ) node->data;
722 if( strcmp( ADDRITEM_ID( email ), eid ) == 0 ) {
723 group->listEMail = g_list_remove( group->listEMail, email );
724 return email;
726 node = g_list_next( node );
729 return NULL;
733 * Print address group item.
735 void addritem_print_item_group( ItemGroup *group, FILE *stream ) {
736 GList *node;
737 ItemPerson *person;
738 ItemEMail *item;
739 g_return_if_fail( group != NULL );
740 fprintf( stream, "Group:\n" );
741 fprintf( stream, "\tt/u: %d : '%s'\n", ADDRITEM_TYPE(group), ADDRITEM_ID(group) );
742 fprintf( stream, "\tsub: %d\n", ADDRITEM_SUBTYPE(group) );
743 fprintf( stream, "\tgrp: '%s'\n", ADDRITEM_NAME(group) );
744 fprintf( stream, "\trem: '%s'\n", group->remarks );
745 fprintf( stream, "\t---\n" );
746 node = group->listEMail;
747 while( node ) {
748 item = node->data;
749 person = ( ItemPerson * ) ADDRITEM_PARENT(item);
750 if( person ) {
751 fprintf( stream, "\t\tpid : '%s'\n", ADDRITEM_ID(person) );
752 fprintf( stream, "\t\tcomn: '%s'\n", ADDRITEM_NAME(person) );
754 else {
755 fprintf( stream, "\t\tpid : ???\n" );
756 fprintf( stream, "\t\tcomn: ???\n" );
758 addritem_print_item_email( item, stream );
759 node = g_list_next( node );
761 fprintf( stream, "\t***\n" );
765 * Create new address folder.
767 ItemFolder *addritem_create_item_folder( void ) {
768 ItemFolder *folder;
769 folder = g_new0( ItemFolder, 1 );
770 ADDRITEM_TYPE(folder) = ITEMTYPE_FOLDER;
771 ADDRITEM_ID(folder) = NULL;
772 ADDRITEM_NAME(folder) = NULL;
773 ADDRITEM_PARENT(folder) = NULL;
774 ADDRITEM_SUBTYPE(folder) = 0;
775 folder->remarks = NULL;
776 folder->isRoot = FALSE;
777 folder->listItems = NULL;
778 folder->listFolder = NULL;
779 folder->listPerson = NULL;
780 folder->listGroup = NULL;
781 return folder;
785 * Copy address book folder.
786 * Enter: item Folder to copy.
787 * Return: A copy of the folder.
789 ItemFolder *addritem_copy_item_folder( ItemFolder *item ) {
790 ItemFolder *itemNew;
792 itemNew = g_new0( ItemFolder, 1 );
793 if( item ) {
794 itemNew = addritem_create_item_folder();
795 ADDRITEM_NAME(itemNew) = g_strdup( ADDRITEM_NAME(item) );
797 return itemNew;
801 * Specify name to be used.
803 void addritem_folder_set_id( ItemFolder *folder, const gchar *value ) {
804 ADDRITEM_ID(folder) = mgu_replace_string( ADDRITEM_ID(folder), value );
806 void addritem_folder_set_name( ItemFolder *folder, const gchar *value ) {
807 ADDRITEM_NAME(folder) = mgu_replace_string( ADDRITEM_NAME(folder), value );
809 void addritem_folder_set_remarks( ItemFolder *folder, const gchar *value ) {
810 folder->remarks = mgu_replace_string( folder->remarks, value );
814 * Free address folder. Note: this does not free up the lists of children
815 * (folders, groups and person). This should be done prior to calling this
816 * function.
818 void addritem_free_item_folder( ItemFolder *folder ) {
819 g_return_if_fail( folder != NULL );
821 /* Free internal stuff */
822 g_free( ADDRITEM_ID(folder) );
823 g_free( ADDRITEM_NAME(folder) );
824 g_free( folder->remarks );
825 mgu_clear_list( folder->listItems );
826 g_list_free( folder->listItems );
828 ADDRITEM_TYPE(folder) = ITEMTYPE_NONE;
829 ADDRITEM_ID(folder) = NULL;
830 ADDRITEM_NAME(folder) = NULL;
831 ADDRITEM_PARENT(folder) = NULL;
832 ADDRITEM_SUBTYPE(folder) = 0;
833 folder->isRoot = FALSE;
834 folder->remarks = NULL;
835 folder->listItems = NULL;
836 folder->listFolder = NULL;
837 folder->listGroup = NULL;
838 folder->listPerson = NULL;
840 g_free( folder );
844 * Free up folders recursively. Note: this does not free up the lists of children
845 * (folders, groups and person). This should be done prior to calling this
846 * function.
848 void addritem_free_item_folder_recurse( ItemFolder *parent ) {
849 GList *node = parent->listFolder;
851 while( node ) {
852 ItemFolder *folder = node->data;
853 addritem_free_item_folder_recurse( folder );
854 node = g_list_next( node );
856 g_list_free( parent->listPerson );
857 g_list_free( parent->listGroup );
858 g_list_free( parent->listFolder );
859 parent->listPerson = NULL;
860 parent->listGroup = NULL;
861 parent->listFolder = NULL;
865 * Free up list of persons in specified folder.
867 void addritem_folder_free_person( ItemFolder *folder ) {
868 GList *node;
870 g_return_if_fail( folder != NULL );
872 /* Free up folder of persons. */
873 node = folder->listPerson;
874 while( node ) {
875 ItemPerson *person = node->data;
876 addritem_free_item_person( person );
877 person = NULL;
878 node = g_list_next( node );
883 * Add person into folder.
884 * return: TRUE if person added.
886 gboolean addritem_folder_add_person( ItemFolder *folder, ItemPerson *item ) {
887 g_return_val_if_fail( folder != NULL, FALSE );
888 g_return_val_if_fail( item != NULL, FALSE );
890 folder->listPerson = g_list_append( folder->listPerson, item );
891 ADDRITEM_PARENT(item) = ADDRITEM_OBJECT(folder);
892 return TRUE;
896 * Add folder into folder.
897 * return: TRUE if folder added.
899 gboolean addritem_folder_add_folder( ItemFolder *folder, ItemFolder *item ) {
900 g_return_val_if_fail( folder != NULL, FALSE );
901 g_return_val_if_fail( item != NULL, FALSE );
903 folder->listFolder = g_list_append( folder->listFolder, item );
904 ADDRITEM_PARENT(item) = ADDRITEM_OBJECT(folder);
905 return TRUE;
909 * Add group into folder.
910 * return: TRUE if folder added.
912 gboolean addritem_folder_add_group( ItemFolder *folder, ItemGroup *item ) {
913 g_return_val_if_fail( folder != NULL, FALSE );
914 g_return_val_if_fail( item != NULL, FALSE );
916 folder->listGroup = g_list_append( folder->listGroup, item );
917 ADDRITEM_PARENT(item) = ADDRITEM_OBJECT(folder);
918 return TRUE;
922 * Print address folder item.
924 void addritem_print_item_folder( ItemFolder *folder, FILE *stream ) {
925 GList *node;
926 /* ItemPerson *person; */
927 ItemFolder *parent;
929 g_return_if_fail( folder != NULL );
931 fprintf( stream, "Folder:\n" );
932 fprintf( stream, "\tt/u: %d : '%s'\n", ADDRITEM_TYPE(folder), ADDRITEM_ID(folder) );
933 fprintf( stream, "\tsub: %d\n", ADDRITEM_SUBTYPE(folder) );
934 fprintf( stream, "\tnam: '%s'\n", ADDRITEM_NAME(folder) );
935 fprintf( stream, "\trem: '%s'\n", folder->remarks );
936 fprintf( stream, "\t---\n" );
937 parent = ( ItemFolder * ) ADDRITEM_PARENT(folder);
938 if( parent ) {
939 fprintf( stream, "\tpar: %s : %s\n", ADDRITEM_ID(parent), ADDRITEM_NAME(parent) );
941 else {
942 fprintf( stream, "\tpar: NULL\n" );
944 node = folder->listFolder;
945 while( node ) {
946 AddrItemObject *aio = node->data;
947 if( aio ) {
948 if( aio->type == ITEMTYPE_FOLDER ) {
949 ItemFolder *item = ( ItemFolder * ) aio;
950 addritem_print_item_folder( item, stream );
953 else {
954 fprintf( stream, "\t\tpid : ???\n" );
957 node = g_list_next( node );
960 node = folder->listPerson;
961 while( node ) {
962 AddrItemObject *aio = node->data;
963 if( aio ) {
964 if( aio->type == ITEMTYPE_PERSON ) {
965 ItemPerson *item = ( ItemPerson * ) aio;
966 addritem_print_item_person( item, stream );
969 else {
970 fprintf( stream, "\t\tpid : ???\n" );
973 node = g_list_next( node );
976 node = folder->listGroup;
977 while( node ) {
978 AddrItemObject *aio = node->data;
979 if( aio ) {
980 if( aio->type == ITEMTYPE_GROUP ) {
981 ItemGroup *item = ( ItemGroup * ) aio;
982 addritem_print_item_group( item, stream );
985 else {
986 fprintf( stream, "\t\tpid : ???\n" );
988 node = g_list_next( node );
990 fprintf( stream, "\t###\n" );
994 * Print address item.
996 void addritem_print_item( AddrItemObject *aio, FILE *stream ) {
997 g_return_if_fail( aio != NULL );
999 if( aio->type == ITEMTYPE_PERSON ) {
1000 ItemPerson *item = ( ItemPerson * ) aio;
1001 addritem_print_item_person( item, stream );
1003 else if( aio->type == ITEMTYPE_EMAIL ) {
1004 ItemEMail *item = ( ItemEMail * ) aio;
1005 addritem_print_item_email( item, stream );
1007 else if( aio->type == ITEMTYPE_GROUP ) {
1008 ItemGroup *item = ( ItemGroup * ) aio;
1009 addritem_print_item_group( item, stream );
1011 else if( aio->type == ITEMTYPE_FOLDER ) {
1012 ItemFolder *item = ( ItemFolder * ) aio;
1013 addritem_print_item_folder( item, stream );
1018 * Return link list of persons for specified folder. Note that the list contains
1019 * references to items and should be g_free() when done. Do *NOT* attempt to use the
1020 * addritem_free_xxx() functions... this will destroy the addressbook data!
1021 * Return: List of items, or NULL if none.
1023 GList *addritem_folder_get_person_list( ItemFolder *folder ) {
1024 GList *list = NULL;
1025 GList *node = NULL;
1027 g_return_val_if_fail( folder != NULL, NULL );
1029 node = folder->listPerson;
1030 while( node ) {
1031 ItemPerson *person = node->data;
1032 list = g_list_append( list, person );
1033 node = g_list_next( node );
1035 return list;
1039 * Return link list of groups for specified folder. Note that the list contains
1040 * references to items and should be g_free() when done. Do *NOT* attempt to use the
1041 * addritem_free_xxx() functions... this will destroy the addressbook data!
1042 * Return: List of items, or NULL if none.
1044 GList *addritem_folder_get_group_list( ItemFolder *folder ) {
1045 GList *list = NULL;
1046 GList *node = NULL;
1048 g_return_val_if_fail( folder != NULL, NULL );
1050 node = folder->listGroup;
1051 while( node ) {
1052 ItemGroup *group = node->data;
1053 list = g_list_append( list, group );
1054 node = g_list_next( node );
1056 return list;
1060 * Move person's email item.
1061 * param: person Person.
1062 * itemMove Item to move.
1063 * itemTarget Target item before which to move item.
1066 ItemEMail *addritem_move_email_before( ItemPerson *person, ItemEMail *itemMove, ItemEMail *itemTarget ) {
1067 gint posT, posM;
1069 g_return_val_if_fail( person != NULL, NULL );
1071 if( itemTarget == NULL ) return NULL;
1072 if( itemMove == NULL ) return NULL;
1073 if( itemMove == itemTarget ) return itemMove;
1075 posT = g_list_index( person->listEMail, itemTarget );
1076 if( posT < 0 ) return NULL;
1077 posM = g_list_index( person->listEMail, itemMove );
1078 if( posM < 0 ) return NULL;
1079 person->listEMail = g_list_remove( person->listEMail, itemMove );
1080 person->listEMail = g_list_insert( person->listEMail, itemMove, posT );
1081 return itemMove;
1085 * Move person's email item.
1086 * param: person Person.
1087 * itemMove Item to move.
1088 * itemTarget Target item after which to move item.
1090 ItemEMail *addritem_move_email_after( ItemPerson *person, ItemEMail *itemMove, ItemEMail *itemTarget ) {
1091 gint posT, posM;
1093 g_return_val_if_fail( person != NULL, NULL );
1095 if( itemTarget == NULL ) return NULL;
1096 if( itemMove == NULL ) return NULL;
1097 if( itemMove == itemTarget ) return itemMove;
1099 posT = g_list_index( person->listEMail, itemTarget );
1100 if( posT < 0 ) return NULL;
1101 posM = g_list_index( person->listEMail, itemMove );
1102 if( posM < 0 ) return NULL;
1103 person->listEMail = g_list_remove( person->listEMail, itemMove );
1104 person->listEMail = g_list_insert( person->listEMail, itemMove, 1+posT );
1105 return itemMove;
1109 * Parse first and last names for person from common name.
1110 * Enter: person Person to process.
1112 void addritem_parse_first_last( ItemPerson *person ) {
1113 gchar *name;
1114 gchar *fName, *lName;
1115 gchar *p;
1116 gint len, i;
1118 g_return_if_fail( person != NULL );
1120 name = ADDRITEM_NAME(person);
1121 if( name == NULL ) return;
1123 fName = NULL;
1124 lName = NULL;
1125 p = strchr( name, ',' );
1126 if( p ) {
1127 len = ( size_t ) ( p - name );
1128 lName = g_strndup( name, len );
1129 fName = g_strdup( p + 1 );
1131 else {
1132 /* Other way around */
1133 i = strlen( name );
1134 while( i >= 0 ) {
1135 if( name[i] == ' ' ) {
1136 fName = g_strndup( name, i );
1137 lName = g_strdup( &name[i] );
1138 break;
1140 i--;
1142 if( fName == NULL ) {
1143 fName = g_strdup( name );
1147 if( person->firstName ) {
1148 g_free( person->firstName );
1150 person->firstName = fName;
1151 if( person->firstName )
1152 g_strstrip( person->firstName );
1154 if( person->lastName ) {
1155 g_free( person->lastName );
1157 person->lastName = lName;
1158 if( person->lastName )
1159 g_strstrip( person->lastName );
1163 * Build a path of all ancestor folders for specified folder.
1164 * Enter: folder Folder.
1165 * seq Path sequence, FALSE top down, TRUE bottom up.
1166 * Return: List of folders from the top down.
1168 GList *addritem_folder_path( const ItemFolder *folder, const gboolean seq ) {
1169 GList *list;
1170 AddrItemObject *item;
1172 list = NULL;
1173 item = ( AddrItemObject * ) folder;
1174 if( seq ) {
1175 while( item ) {
1176 list = g_list_prepend( list, item );
1177 item = ADDRITEM_PARENT( item );
1180 else {
1181 while( item ) {
1182 list = g_list_append( list, item );
1183 item = ADDRITEM_PARENT( item );
1186 return list;
1190 * End of Source.