2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2017 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 primitive address item objects.
30 #include "file-utils.h"
37 * Create new email address item.
38 * \return Initialized email item.
40 ItemEMail
*addritem_create_item_email( void ) {
42 item
= g_new0( ItemEMail
, 1 );
43 ADDRITEM_TYPE(item
) = ITEMTYPE_EMAIL
;
44 ADDRITEM_ID(item
) = NULL
;
45 ADDRITEM_NAME(item
) = NULL
;
46 ADDRITEM_PARENT(item
) = NULL
;
47 ADDRITEM_SUBTYPE(item
) = 0;
54 * Create a shallow copy of specified email address item.
55 * \param item E-Mail to copy.
56 * \return Copy of email, or <i>NULL</i> if null argument supplied.
58 ItemEMail
*addritem_copy_item_email( ItemEMail
*item
) {
59 ItemEMail
*itemNew
= NULL
;
61 itemNew
= addritem_create_item_email();
62 ADDRITEM_NAME(itemNew
) = g_strdup( ADDRITEM_NAME(item
) );
63 itemNew
->address
= g_strdup( item
->address
);
64 itemNew
->remarks
= g_strdup( item
->remarks
);
70 * Create a full copy (deep copy) of specified email address item.
71 * \param item E-Mail to copy.
72 * \return Copy of email.
74 ItemEMail
*addritem_copyfull_item_email( ItemEMail
*item
) {
75 ItemEMail
*itemNew
= NULL
;
77 itemNew
= addritem_create_item_email();
78 ADDRITEM_ID(itemNew
) = g_strdup( ADDRITEM_ID(item
) );
79 ADDRITEM_NAME(itemNew
) = g_strdup( ADDRITEM_NAME(item
) );
80 ADDRITEM_PARENT(itemNew
) = ADDRITEM_PARENT(item
);
81 itemNew
->address
= g_strdup( item
->address
);
82 itemNew
->remarks
= g_strdup( item
->remarks
);
88 * Specify alias for email.
89 * \param email E-Mail item.
92 void addritem_email_set_alias( ItemEMail
*email
, const gchar
*value
) {
93 ADDRITEM_NAME(email
) = mgu_replace_string( ADDRITEM_NAME(email
), value
);
97 * Specify address for email.
98 * \param email E-Mail item.
99 * \param value Address.
101 void addritem_email_set_address( ItemEMail
*email
, const gchar
*value
) {
102 email
->address
= mgu_replace_string( email
->address
, value
);
106 * Specify remarks for email.
107 * \param email E-Mail item.
108 * \param value Remarks.
110 void addritem_email_set_remarks( ItemEMail
*email
, const gchar
*value
) {
111 email
->remarks
= mgu_replace_string( email
->remarks
, value
);
115 * Free address item email object.
116 * \param item E-Mail item to free.
118 void addritem_free_item_email( ItemEMail
*item
) {
119 cm_return_if_fail( item
!= NULL
);
121 /* Free internal stuff */
122 g_free( ADDRITEM_ID(item
) );
123 g_free( ADDRITEM_NAME(item
) );
124 g_free( item
->address
);
125 g_free( item
->remarks
);
127 ADDRITEM_OBJECT(item
)->type
= ITEMTYPE_NONE
;
128 ADDRITEM_ID(item
) = NULL
;
129 ADDRITEM_NAME(item
) = NULL
;
130 ADDRITEM_PARENT(item
) = NULL
;
131 ADDRITEM_SUBTYPE(item
) = 0;
132 item
->address
= NULL
;
133 item
->remarks
= NULL
;
138 * Create new attribute object.
139 * \return Initialized attribute object.
141 UserAttribute
*addritem_create_attribute( void ) {
143 item
= g_new0( UserAttribute
, 1 );
151 * Create copy (deep copy) of specified attribute.
152 * \param item Attribute to copy.
153 * \return Copy of attribute, or <i>NULL</i> if null argument supplied.
155 UserAttribute
*addritem_copy_attribute( UserAttribute
*item
) {
156 UserAttribute
*itemNew
= NULL
;
158 itemNew
= addritem_create_attribute();
159 itemNew
->uid
= g_strdup( item
->uid
);
160 itemNew
->name
= g_strdup( item
->name
);
161 itemNew
->value
= g_strdup( item
->value
);
167 * Specify ID for attribute.
168 * \param item Attribute object.
171 void addritem_attrib_set_id( UserAttribute
*item
, const gchar
*value
) {
172 cm_return_if_fail( item
!= NULL
);
173 item
->uid
= mgu_replace_string( item
->uid
, value
);
177 * Specify name for attribute.
178 * \param item Attribute object.
181 void addritem_attrib_set_name( UserAttribute
*item
, const gchar
*value
) {
182 cm_return_if_fail( item
!= NULL
);
183 item
->name
= mgu_replace_string( item
->name
, value
);
187 * Specify value for attribute.
188 * \param item Attribute object.
189 * \param value Value.
191 void addritem_attrib_set_value( UserAttribute
*item
, const gchar
*value
) {
192 cm_return_if_fail( item
!= NULL
);
193 item
->value
= mgu_replace_string( item
->value
, value
);
197 * Free user attribute.
198 * \param item Attribute object to free.
200 void addritem_free_attribute( UserAttribute
*item
) {
201 cm_return_if_fail( item
!= NULL
);
203 g_free( item
->name
);
204 g_free( item
->value
);
212 * Create new address book person.
213 * \return Initialized person object.
215 ItemPerson
*addritem_create_item_person( void ) {
217 person
= g_new0( ItemPerson
, 1 );
218 ADDRITEM_TYPE(person
) = ITEMTYPE_PERSON
;
219 ADDRITEM_ID(person
) = NULL
;
220 ADDRITEM_NAME(person
) = NULL
;
221 ADDRITEM_PARENT(person
) = NULL
;
222 ADDRITEM_SUBTYPE(person
) = 0;
223 person
->picture
= NULL
;
224 person
->firstName
= NULL
;
225 person
->lastName
= NULL
;
226 person
->nickName
= NULL
;
227 person
->listEMail
= NULL
;
228 person
->listAttrib
= NULL
;
229 person
->externalID
= NULL
;
230 person
->isOpened
= FALSE
;
231 person
->status
= NONE
;
236 * Create a shallow copy of address book person object.
237 * \param item Person to copy.
238 * \return Copy of person, or <i>NULL</i> if null argument supplied.
240 ItemPerson
*addritem_copy_item_person( ItemPerson
*item
) {
245 itemNew
= addritem_create_item_person();
246 ADDRITEM_NAME(itemNew
) = g_strdup( ADDRITEM_NAME(item
) );
247 itemNew
->picture
= g_strdup( ADDRITEM_ID(itemNew
) );
248 itemNew
->firstName
= g_strdup( item
->firstName
);
249 itemNew
->lastName
= g_strdup( item
->lastName
);
250 itemNew
->nickName
= g_strdup( item
->nickName
);
251 itemNew
->externalID
= g_strdup( item
->externalID
);
252 itemNew
->status
= item
->status
;
258 * Specify picture for person object.
259 * \param person Person object.
260 * \param value Picture.
262 void addritem_person_set_picture( ItemPerson
*person
, const gchar
*value
) {
263 if (!value
|| g_utf8_validate(value
, -1, NULL
))
264 person
->picture
= mgu_replace_string( person
->picture
, value
);
266 gchar
*out
= conv_codeset_strdup(value
,
267 conv_get_locale_charset_str_no_utf8(),
270 person
->picture
= mgu_replace_string( person
->picture
, out
);
276 * Get picture filename for person object.
277 * \param person Person object.
278 * \return copy of picture file path string (to be freed by caller - and there is
279 * no guarantee that path does exist, or NULL.
281 gchar
*addritem_person_get_picture( ItemPerson
*person
) {
283 return g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S
,
284 ADDRBOOK_DIR
, G_DIR_SEPARATOR_S
, person
->picture
,
290 * Delete picture for person object.
291 * \param person Person object.
293 void addritem_person_remove_picture( ItemPerson
*person
) {
294 if (person
->picture
) {
295 gchar
*filename
= g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S
,
296 ADDRBOOK_DIR
, G_DIR_SEPARATOR_S
, person
->picture
,
298 if (is_file_exist(filename
)) {
299 debug_print("removing addressbook picture %s\n",
301 if (claws_unlink(filename
) < 0) {
302 FILE_OP_ERROR(filename
, "remove");
307 g_free(person
->picture
);
308 person
->picture
= NULL
;
314 * Specify first name for person object.
315 * \param person Person object.
318 void addritem_person_set_first_name( ItemPerson
*person
, const gchar
*value
) {
319 if (!value
|| g_utf8_validate(value
, -1, NULL
))
320 person
->firstName
= mgu_replace_string( person
->firstName
, value
);
322 gchar
*out
= conv_codeset_strdup(value
,
323 conv_get_locale_charset_str_no_utf8(),
326 person
->firstName
= mgu_replace_string( person
->firstName
, out
);
332 * Specify last name for person object.
333 * \param person Person object.
336 void addritem_person_set_last_name( ItemPerson
*person
, const gchar
*value
) {
337 if (!value
|| g_utf8_validate(value
, -1, NULL
))
338 person
->lastName
= mgu_replace_string( person
->lastName
, value
);
340 gchar
*out
= conv_codeset_strdup(value
,
341 conv_get_locale_charset_str_no_utf8(),
344 person
->lastName
= mgu_replace_string( person
->lastName
, out
);
350 * Specify nick name for person object.
351 * \param person Person object.
354 void addritem_person_set_nick_name( ItemPerson
*person
, const gchar
*value
) {
355 if (!value
|| g_utf8_validate(value
, -1, NULL
))
356 person
->nickName
= mgu_replace_string( person
->nickName
, value
);
358 gchar
*out
= conv_codeset_strdup(value
,
359 conv_get_locale_charset_str_no_utf8(),
362 person
->nickName
= mgu_replace_string( person
->nickName
, out
);
368 * Specify common name for person object.
369 * \param person Person object.
372 void addritem_person_set_common_name( ItemPerson
*person
, const gchar
*value
) {
373 if (!value
|| g_utf8_validate(value
, -1, NULL
))
374 ADDRITEM_NAME(person
) = mgu_replace_string( ADDRITEM_NAME(person
), value
);
376 gchar
*out
= conv_codeset_strdup(value
,
377 conv_get_locale_charset_str_no_utf8(),
380 ADDRITEM_NAME(person
) = mgu_replace_string( ADDRITEM_NAME(person
), out
);
386 * Specify external ID for person object.
387 * \param person Person object.
390 void addritem_person_set_external_id( ItemPerson
*person
, const gchar
*value
) {
391 person
->externalID
= mgu_replace_string( person
->externalID
, value
);
395 * Specify value of open indicator for person object. This is typically used to
396 * simplify open/close folders in the address book GUI.
397 * \param person Person object.
398 * \param value Value for indicator. Set to <i>TRUE</i> if opened.
400 void addritem_person_set_opened( ItemPerson
*person
, const gboolean value
) {
401 person
->isOpened
= value
;
405 * Free linked list of item addresses; both addresses and the list are freed.
406 * It is assumed that addresses are *NOT* contained within some other
408 * \param list List of addresses to be freed.
410 void addritem_free_list_email( GList
*list
) {
413 ItemEMail
*email
= node
->data
;
415 addritem_free_item_email( email
);
417 node
= g_list_next( node
);
424 * Free linked list of attributes; both attributes and the list are freed.
425 * It is assumed that attributes are *NOT* contained within some other
427 * \param list List of attributes to be freed.
429 void addritem_free_list_attribute( GList
*list
) {
432 addritem_free_attribute( node
->data
);
434 node
= g_list_next( node
);
440 * Free address person object.
441 * \param person Person object to free.
443 void addritem_free_item_person( ItemPerson
*person
) {
444 cm_return_if_fail( person
!= NULL
);
446 /* Free internal stuff */
447 g_free( ADDRITEM_ID(person
) );
448 g_free( ADDRITEM_NAME(person
) );
449 g_free( person
->picture
);
450 g_free( person
->firstName
);
451 g_free( person
->lastName
);
452 g_free( person
->nickName
);
453 g_free( person
->externalID
);
454 g_list_free( person
->listEMail
);
455 addritem_free_list_attribute( person
->listAttrib
);
457 ADDRITEM_OBJECT(person
)->type
= ITEMTYPE_NONE
;
458 ADDRITEM_ID(person
) = NULL
;
459 ADDRITEM_NAME(person
) = NULL
;
460 ADDRITEM_PARENT(person
) = NULL
;
461 ADDRITEM_SUBTYPE(person
) = 0;
462 person
->picture
= NULL
;
463 person
->firstName
= NULL
;
464 person
->lastName
= NULL
;
465 person
->nickName
= NULL
;
466 person
->externalID
= NULL
;
467 person
->listEMail
= NULL
;
468 person
->listAttrib
= NULL
;
474 * Print E-Mail address object for debug.
475 * \param item Item to print.
476 * \param stream Output stream.
478 void addritem_print_item_email( ItemEMail
*item
, FILE *stream
) {
479 cm_return_if_fail( item
!= NULL
);
480 fprintf( stream
, "\t\tt/id: %d : '%s'\n", ADDRITEM_TYPE(item
), ADDRITEM_ID(item
) );
481 fprintf( stream
, "\t\tsubty: %d\n", ADDRITEM_SUBTYPE(item
) );
482 fprintf( stream
, "\t\talis: '%s'\n", ADDRITEM_NAME(item
) );
483 fprintf( stream
, "\t\taddr: '%s'\n", item
->address
);
484 fprintf( stream
, "\t\trems: '%s'\n", item
->remarks
);
485 fprintf( stream
, "\t\t---\n" );
489 * Print user attribute object for debug.
490 * \param item Attribute to print.
491 * \param stream Output stream.
493 static void addritem_print_attribute( UserAttribute
*item
, FILE *stream
) {
494 cm_return_if_fail( item
!= NULL
);
495 fprintf( stream
, "\t\tuid : '%s'\n", item
->uid
);
496 fprintf( stream
, "\t\tname : '%s'\n", item
->name
);
497 fprintf( stream
, "\t\tvalue: '%s'\n", item
->value
);
498 fprintf( stream
, "\t\t---\n" );
502 * Print person item for debug.
503 * \param person Person to print.
504 * \param stream Output stream.
506 void addritem_print_item_person( ItemPerson
*person
, FILE *stream
) {
508 cm_return_if_fail( person
!= NULL
);
509 fprintf( stream
, "Person:\n" );
510 fprintf( stream
, "\tt/uid: %d : '%s'\n", ADDRITEM_TYPE(person
), ADDRITEM_ID(person
) );
511 fprintf( stream
, "\tsubty: %d\n", ADDRITEM_SUBTYPE(person
) );
512 fprintf( stream
, "\tcommn: '%s'\n", ADDRITEM_NAME(person
) );
513 fprintf( stream
, "\tphoto: '%s'\n", person
->picture
);
514 fprintf( stream
, "\tfirst: '%s'\n", person
->firstName
);
515 fprintf( stream
, "\tlast : '%s'\n", person
->lastName
);
516 fprintf( stream
, "\tnick : '%s'\n", person
->nickName
);
517 fprintf( stream
, "\textID: '%s'\n", person
->externalID
);
518 fprintf( stream
, "\teMail:\n" );
519 fprintf( stream
, "\t---\n" );
520 node
= person
->listEMail
;
522 addritem_print_item_email( node
->data
, stream
);
523 node
= g_list_next( node
);
525 fprintf( stream
, "\tuAttr:\n" );
526 fprintf( stream
, "\t---\n" );
527 node
= person
->listAttrib
;
529 addritem_print_attribute( node
->data
, stream
);
530 node
= g_list_next( node
);
532 gchar
*current_status
;
533 switch (person
->status
) {
534 case NONE
: current_status
= g_strdup("Unchanged"); break;
535 case ADD_ENTRY
: current_status
= g_strdup("New"); break;
536 case UPDATE_ENTRY
: current_status
= g_strdup("Updated"); break;
537 case DELETE_ENTRY
: current_status
= g_strdup("Deleted"); break;
538 default: current_status
= g_strdup("Unknown");
540 fprintf( stream
, "\t\tStatus: %s\n", current_status
);
541 if ( current_status
)
542 g_free(current_status
);
543 fprintf( stream
, "\t===\n" );
547 * Add E-Mail address object to person.
548 * \param person Person.
549 * \param email E-Mail object to add.
550 * \return <i>TRUE</i> if E-Mail added.
552 gboolean
addritem_person_add_email( ItemPerson
*person
, ItemEMail
*email
) {
555 cm_return_val_if_fail( person
!= NULL
, FALSE
);
556 cm_return_val_if_fail( email
!= NULL
, FALSE
);
558 node
= person
->listEMail
;
560 if( node
->data
== email
) return FALSE
;
561 node
= g_list_next( node
);
563 person
->listEMail
= g_list_append( person
->listEMail
, email
);
564 ADDRITEM_PARENT(email
) = ADDRITEM_OBJECT(person
);
569 * Remove email address for specified person.
570 * \param person Person.
571 * \param email EMail to remove.
572 * \return EMail object, or <i>NULL</i> if not found. Note that object should
573 * still be freed after calling this method.
575 ItemEMail
*addritem_person_remove_email( ItemPerson
*person
, ItemEMail
*email
) {
576 gboolean found
= FALSE
;
579 cm_return_val_if_fail( person
!= NULL
, NULL
);
580 if( email
== NULL
) return NULL
;
583 node
= person
->listEMail
;
585 if( node
-> data
== email
) {
589 node
= g_list_next( node
);
593 /* Remove email from person's address list */
594 if( person
->listEMail
) {
595 person
->listEMail
= g_list_remove( person
->listEMail
, email
);
597 /* Unlink reference to person. */
598 ADDRITEM_PARENT(email
) = NULL
;
605 * Add user attribute to specified person.
606 * \param person Person.
607 * \param attrib Attribute to add.
608 * \return <i>TRUE</i> if item added.
610 void addritem_person_add_attribute(
611 ItemPerson
*person
, UserAttribute
*attrib
)
613 cm_return_if_fail( person
!= NULL
);
614 person
->listAttrib
= g_list_append( person
->listAttrib
, attrib
);
618 * Remove user attribute from specified person.
619 * \param person Person.
620 * \param attrib Attribute to remove.
622 void addritem_person_remove_attribute( ItemPerson
*person
, const gchar
*attrib
) {
623 cm_return_if_fail( person
!= NULL
&& attrib
!= NULL
);
626 attrib_list
= person
->listAttrib
;
627 while (attrib_list
) {
628 UserAttribute
*user_attrib
= attrib_list
->data
;
629 if (strcmp(user_attrib
->name
, attrib
) == 0) {
630 person
->listAttrib
= g_list_remove(person
->listAttrib
, (gconstpointer
) user_attrib
);
633 attrib_list
= g_list_next(attrib_list
);
639 * Create new address book group object.
640 * \return Initialized group object.
642 ItemGroup
*addritem_create_item_group( void ) {
645 group
= g_new0( ItemGroup
, 1 );
646 ADDRITEM_TYPE(group
) = ITEMTYPE_GROUP
;
647 ADDRITEM_ID(group
) = NULL
;
648 ADDRITEM_NAME(group
) = NULL
;
649 ADDRITEM_PARENT(group
) = NULL
;
650 ADDRITEM_SUBTYPE(group
) = 0;
651 group
->remarks
= NULL
;
652 group
->listEMail
= NULL
;
657 * Copy (deep copy) address book group.
658 * \param item Group to copy.
659 * \return Copy of the group object, or <i>NULL</i> if null argument supplied.
661 ItemGroup
*addritem_copy_item_group( ItemGroup
*item
) {
666 itemNew
= addritem_create_item_group();
667 ADDRITEM_NAME(itemNew
) = g_strdup( ADDRITEM_NAME(item
) );
668 itemNew
->remarks
= g_strdup( item
->remarks
);
674 * Specify name to be used for group.
675 * \param group Group object.
676 * \param value Name of group.
678 void addritem_group_set_name( ItemGroup
*group
, const gchar
*value
) {
679 ADDRITEM_NAME(group
) = mgu_replace_string( ADDRITEM_NAME(group
), value
);
683 * Free address group object.
684 * \param group Group to free.
686 void addritem_free_item_group( ItemGroup
*group
) {
687 cm_return_if_fail( group
!= NULL
);
689 /* Free internal stuff */
690 g_free( ADDRITEM_ID(group
) );
691 g_free( ADDRITEM_NAME(group
) );
692 g_free( group
->remarks
);
693 g_list_free( group
->listEMail
);
695 ADDRITEM_TYPE(group
) = ITEMTYPE_NONE
;
696 ADDRITEM_ID(group
) = NULL
;
697 ADDRITEM_NAME(group
) = NULL
;
698 ADDRITEM_PARENT(group
) = NULL
;
699 ADDRITEM_SUBTYPE(group
) = 0;
700 group
->remarks
= NULL
;
701 group
->listEMail
= NULL
;
707 * Add EMail address to group. Note that a reference to an E-Mail item is
708 * added to a group. A person object is the only container that for an
710 * \param group Group.
711 * \param email E-Mail object.
712 * \return <i>TRUE</i> if email item added.
714 gboolean
addritem_group_add_email( ItemGroup
*group
, ItemEMail
*email
) {
717 cm_return_val_if_fail( group
!= NULL
, FALSE
);
718 cm_return_val_if_fail( email
!= NULL
, FALSE
);
720 node
= group
->listEMail
;
722 if( node
->data
== email
) return FALSE
;
723 node
= g_list_next( node
);
725 group
->listEMail
= g_list_append( group
->listEMail
, email
);
730 * Remove person object for specified group.
731 * \param group Group from which to remove address.
732 * \param email EMail to remove
733 * \return EMail object, or <i>NULL if email not found in group. Note that
734 * this object is referenced (linked) to a group and should *NOT*
735 * be freed. An E-Mail object object should only be freed after
736 * removing from a person.
738 ItemPerson
*addritem_folder_remove_person( ItemFolder
*group
, ItemPerson
*person
) {
739 if( group
&& person
) {
740 GList
*node
= group
->listPerson
;
742 if( node
->data
== person
) {
743 group
->listPerson
= g_list_remove( group
->listPerson
, person
);
746 node
= g_list_next( node
);
753 * Print address group item for debug.
754 * \param group Group to print.
755 * \param stream Output stream.
757 void addritem_print_item_group( ItemGroup
*group
, FILE *stream
) {
761 cm_return_if_fail( group
!= NULL
);
762 fprintf( stream
, "Group:\n" );
763 fprintf( stream
, "\tt/u: %d : '%s'\n", ADDRITEM_TYPE(group
), ADDRITEM_ID(group
) );
764 fprintf( stream
, "\tsub: %d\n", ADDRITEM_SUBTYPE(group
) );
765 fprintf( stream
, "\tgrp: '%s'\n", ADDRITEM_NAME(group
) );
766 fprintf( stream
, "\trem: '%s'\n", group
->remarks
);
767 fprintf( stream
, "\t---\n" );
768 node
= group
->listEMail
;
771 person
= ( ItemPerson
* ) ADDRITEM_PARENT(item
);
773 fprintf( stream
, "\t\tpid : '%s'\n", ADDRITEM_ID(person
) );
774 fprintf( stream
, "\t\tcomn: '%s'\n", ADDRITEM_NAME(person
) );
777 fprintf( stream
, "\t\tpid : ???\n" );
778 fprintf( stream
, "\t\tcomn: ???\n" );
780 addritem_print_item_email( item
, stream
);
781 node
= g_list_next( node
);
783 fprintf( stream
, "\t***\n" );
787 * Create new address folder.
788 * \return Initialized address folder object.
790 ItemFolder
*addritem_create_item_folder( void ) {
792 folder
= g_new0( ItemFolder
, 1 );
794 g_error("could not create new itemFolder");
795 ADDRITEM_TYPE(folder
) = ITEMTYPE_FOLDER
;
796 ADDRITEM_ID(folder
) = NULL
;
797 ADDRITEM_NAME(folder
) = NULL
;
798 ADDRITEM_PARENT(folder
) = NULL
;
799 ADDRITEM_SUBTYPE(folder
) = 0;
800 folder
->remarks
= NULL
;
801 folder
->isRoot
= FALSE
;
802 folder
->listItems
= NULL
;
803 folder
->listFolder
= NULL
;
804 folder
->listPerson
= NULL
;
805 folder
->listGroup
= NULL
;
806 folder
->folderType
= ADDRFOLDER_NONE
;
807 folder
->folderData
= NULL
;
808 folder
->isHidden
= FALSE
;
813 * Copy address book folder. Note that only the folder and not its contents are
815 * \param item Folder to copy.
816 * \return A copy of the folder, or <i>NULL</i> if null argument supplied.
818 ItemFolder
*addritem_copy_item_folder( ItemFolder
*item
) {
819 ItemFolder
*itemNew
= NULL
;
822 itemNew
= addritem_create_item_folder();
823 ADDRITEM_NAME(itemNew
) = g_strdup( ADDRITEM_NAME(item
) );
824 itemNew
->folderType
= item
->folderType
;
826 g_error("could not create a copy of a null itemFolder");
831 * Specify name to be used for folder.
832 * \param folder Folder.
835 void addritem_folder_set_name( ItemFolder
*folder
, const gchar
*value
) {
836 ADDRITEM_NAME(folder
) = mgu_replace_string( ADDRITEM_NAME(folder
), value
);
840 * Specify remarks to be used for folder.
841 * \param folder Folder.
842 * \param value Remarks.
844 void addritem_folder_set_remarks( ItemFolder
*folder
, const gchar
*value
) {
845 folder
->remarks
= mgu_replace_string( folder
->remarks
, value
);
849 * Specify visibility of folder.
850 * \param folder Folder.
851 * \param value Set to <code>TRUE</code> to hide folder.
853 void addritem_folder_set_hidden( ItemFolder
*folder
, const gboolean value
) {
854 folder
->isHidden
= value
;
858 * Free address folder. Note: this does not free up the lists of children
859 * (folders, groups and person). This should be done prior to calling this
861 * \param folder Folder to free.
863 void addritem_free_item_folder( ItemFolder
*folder
) {
864 cm_return_if_fail( folder
!= NULL
);
866 /* Free internal stuff */
867 g_free( ADDRITEM_ID(folder
) );
868 g_free( ADDRITEM_NAME(folder
) );
869 g_free( folder
->remarks
);
870 g_list_free( folder
->listItems
);
872 ADDRITEM_TYPE(folder
) = ITEMTYPE_NONE
;
873 ADDRITEM_ID(folder
) = NULL
;
874 ADDRITEM_NAME(folder
) = NULL
;
875 ADDRITEM_PARENT(folder
) = NULL
;
876 ADDRITEM_SUBTYPE(folder
) = 0;
877 folder
->isRoot
= FALSE
;
878 folder
->remarks
= NULL
;
879 folder
->listItems
= NULL
;
880 folder
->listFolder
= NULL
;
881 folder
->listGroup
= NULL
;
882 folder
->listPerson
= NULL
;
883 folder
->folderType
= ADDRFOLDER_NONE
;
884 folder
->folderData
= NULL
;
885 folder
->isHidden
= FALSE
;
891 * Add person into folder.
892 * \param folder Folder.
893 * \param item Person to add.
894 * \return <i>TRUE</i> if person added.
896 gboolean
addritem_folder_add_person( ItemFolder
*folder
, ItemPerson
*item
) {
897 cm_return_val_if_fail( folder
!= NULL
, FALSE
);
898 cm_return_val_if_fail( item
!= NULL
, FALSE
);
900 folder
->listPerson
= g_list_append( folder
->listPerson
, item
);
901 ADDRITEM_PARENT(item
) = ADDRITEM_OBJECT(folder
);
906 * Add folder into folder.
907 * \param folder Folder.
908 * \param item Folder to add.
909 * \return <i>TRUE</i> if folder added.
911 gboolean
addritem_folder_add_folder( ItemFolder
*folder
, ItemFolder
*item
) {
912 cm_return_val_if_fail( folder
!= NULL
, FALSE
);
913 cm_return_val_if_fail( item
!= NULL
, FALSE
);
915 folder
->listFolder
= g_list_append( folder
->listFolder
, item
);
916 ADDRITEM_PARENT(item
) = ADDRITEM_OBJECT(folder
);
921 * Add group into folder.
922 * \param folder Folder.
923 * \param item Group to add.
924 * \return <i>TRUE</i> if group added.
926 gboolean
addritem_folder_add_group( ItemFolder
*folder
, ItemGroup
*item
) {
927 cm_return_val_if_fail( folder
!= NULL
, FALSE
);
928 cm_return_val_if_fail( item
!= NULL
, FALSE
);
930 folder
->listGroup
= g_list_append( folder
->listGroup
, item
);
931 ADDRITEM_PARENT(item
) = ADDRITEM_OBJECT(folder
);
936 * Print address folder item contents for debug.
937 * \param folder Folder to process.
938 * \param stream Output stream.
940 void addritem_print_item_folder( ItemFolder
*folder
, FILE *stream
) {
942 /* ItemPerson *person; */
945 cm_return_if_fail( folder
!= NULL
);
947 fprintf( stream
, "Folder:\n" );
948 fprintf( stream
, "\tt/u: %d : '%s'\n", ADDRITEM_TYPE(folder
), ADDRITEM_ID(folder
) );
949 fprintf( stream
, "\tsub: %d\n", ADDRITEM_SUBTYPE(folder
) );
950 fprintf( stream
, "\tnam: '%s'\n", ADDRITEM_NAME(folder
) );
951 fprintf( stream
, "\trem: '%s'\n", folder
->remarks
);
952 fprintf( stream
, "\ttyp: %d\n", folder
->folderType
);
953 fprintf( stream
, "\thid: %s\n", folder
->isHidden
? "hidden" : "visible" );
954 fprintf( stream
, "\t---\n" );
955 parent
= ( ItemFolder
* ) ADDRITEM_PARENT(folder
);
957 fprintf( stream
, "\tpar: %s : %s\n", ADDRITEM_ID(parent
), ADDRITEM_NAME(parent
) );
960 fprintf( stream
, "\tpar: NULL\n" );
962 node
= folder
->listFolder
;
964 AddrItemObject
*aio
= node
->data
;
966 if( aio
->type
== ITEMTYPE_FOLDER
) {
967 ItemFolder
*item
= ( ItemFolder
* ) aio
;
968 addritem_print_item_folder( item
, stream
);
972 fprintf( stream
, "\t\tpid : ???\n" );
975 node
= g_list_next( node
);
978 node
= folder
->listPerson
;
980 AddrItemObject
*aio
= node
->data
;
982 if( aio
->type
== ITEMTYPE_PERSON
) {
983 ItemPerson
*item
= ( ItemPerson
* ) aio
;
984 addritem_print_item_person( item
, stream
);
988 fprintf( stream
, "\t\tpid : ???\n" );
991 node
= g_list_next( node
);
994 node
= folder
->listGroup
;
996 AddrItemObject
*aio
= node
->data
;
998 if( aio
->type
== ITEMTYPE_GROUP
) {
999 ItemGroup
*item
= ( ItemGroup
* ) aio
;
1000 addritem_print_item_group( item
, stream
);
1004 fprintf( stream
, "\t\tpid : ???\n" );
1006 node
= g_list_next( node
);
1008 fprintf( stream
, "\t###\n" );
1012 * Return link list of persons for specified folder. Note that the list contains
1013 * references to items and should be g_free() when done. Do *NOT* attempt to use the
1014 * addritem_free_xxx() functions... this will destroy the addressbook data!
1016 * \param folder Folder to process.
1017 * \return List of items, or <i>NULL</i> if none.
1019 GList
*addritem_folder_get_person_list( ItemFolder
*folder
) {
1023 cm_return_val_if_fail( folder
!= NULL
, NULL
);
1025 node
= folder
->listPerson
;
1027 ItemPerson
*person
= node
->data
;
1028 list
= g_list_prepend( list
, person
);
1029 node
= g_list_next( node
);
1031 return g_list_reverse(list
);
1035 * Return link list of groups for specified folder. Note that the list contains
1036 * references to items and should be g_free() when done. Do *NOT* attempt to use the
1037 * addritem_free_xxx() functions... this will destroy the addressbook data!
1039 * \param folder Folder to process.
1040 * \return List of items, or <i>NULL</i> if none.
1042 GList
*addritem_folder_get_group_list( ItemFolder
*folder
) {
1046 cm_return_val_if_fail( folder
!= NULL
, NULL
);
1048 node
= folder
->listGroup
;
1050 ItemGroup
*group
= node
->data
;
1051 list
= g_list_prepend( list
, group
);
1052 node
= g_list_next( node
);
1054 return g_list_reverse(list
);
1058 * Parse first and last names for person from common name.
1059 * \param person Person to process.
1061 void addritem_parse_first_last( ItemPerson
*person
) {
1063 gchar
*fName
, *lName
;
1067 cm_return_if_fail( person
!= NULL
);
1069 name
= ADDRITEM_NAME(person
);
1070 if( name
== NULL
) return;
1074 p
= strchr( name
, ',' );
1076 len
= ( size_t ) ( p
- name
);
1077 lName
= g_strndup( name
, len
);
1078 fName
= g_strdup( p
+ 1 );
1081 /* Other way around */
1084 if( name
[i
] == ' ' ) {
1085 fName
= g_strndup( name
, i
);
1086 lName
= g_strdup( &name
[i
] );
1091 if( fName
== NULL
) {
1092 fName
= g_strdup( name
);
1096 g_free( person
->firstName
);
1097 person
->firstName
= fName
;
1098 if( person
->firstName
)
1099 g_strstrip( person
->firstName
);
1101 g_free( person
->lastName
);
1102 person
->lastName
= lName
;
1103 if( person
->lastName
)
1104 g_strstrip( person
->lastName
);
1108 * Build a path of all ancestor folders for specified folder.
1109 * \param folder Folder.
1110 * \param seq Path sequence, FALSE top down, TRUE bottom up.
1111 * \return List of folders from the top down.
1113 GList
*addritem_folder_path( const ItemFolder
*folder
, const gboolean seq
) {
1115 AddrItemObject
*item
;
1118 item
= ( AddrItemObject
* ) folder
;
1121 list
= g_list_prepend( list
, item
);
1122 item
= ADDRITEM_PARENT( item
);
1127 list
= g_list_append( list
, item
);
1128 item
= ADDRITEM_PARENT( item
);
1135 * Format E-Mail address.
1136 * \param email EMail item to format.
1137 * \return Formatted string. Should be freed after use.
1139 gchar
*addritem_format_email( ItemEMail
*email
) {
1146 if( ADDRITEM_NAME( email
) ) {
1147 if( strlen( ADDRITEM_NAME( email
) ) ) {
1148 name
= ADDRITEM_NAME( email
);
1152 person
= ( ItemPerson
* ) ADDRITEM_PARENT( email
);
1153 name
= ADDRITEM_NAME( person
);
1157 if( strchr_with_skip_quote( name
, '"', ',' ) ) {
1158 address
= g_strdup_printf( "\"%s\" <%s>", name
, email
->address
);
1161 address
= g_strdup_printf( "%s <%s>", name
, email
->address
);
1165 address
= g_strdup_printf( "%s", email
->address
);