Unit test stuff on new contact fields.
[libgcal.git] / src / gcontact.c
blobfabe88c8cfd40253cf785034ef6bb0f8f3b6f7bf
1 /*
2 Copyright (c) 2008 Instituto Nokia de Tecnologia
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13 * Neither the name of the INdT nor the names of its contributors
14 may be used to endorse or promote products derived from this software
15 without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
29 /**
30 * @file gcontact.c
31 * @author Adenilson Cavalcanti da Silva <adenilson.silva@indt.org.br>
32 * @date Thu Jun 26 07:37:03 2008
34 * @brief libgcal google contacts user public API.
36 * Use this functions to handle common tasks when dealing with google contacts.
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #else
42 #define _GNU_SOURCE
43 #endif
45 #include <string.h>
46 #include <stdio.h>
47 #include "gcontact.h"
48 #include "gcal_parser.h"
49 #include "internal_gcal.h"
52 /** Strings associated with phone number types */
53 const char* gcal_phone_type_str[] = {
54 "assistant", // P_ASSISTANT
55 "callback", // P_CALLBACK
56 "car", // P_CAR
57 "company_main", // P_COMPANY_MAIN
58 "fax", // P_FAX
59 "home", // P_HOME
60 "home_fax", // P_HOME_FAX
61 "isdn", // P_ISDN
62 "main", // P_MAIN
63 "mobile", // P_MOBILE
64 "other", // P_OTHER
65 "other_fax", // P_OTHER_FAX
66 "pager", // P_PAGER
67 "radio", // P_RADIO
68 "telex", // P_TELEX
69 "tty_tdd", // P_TTY_TDD
70 "work", // P_WORK
71 "work_fax", // P_WORK_FAX
72 "work_mobile", // P_WORK_MOBILE
73 "work_pager" // P_WORK_PAGER
76 /** Strings associated with email types */
77 const char* gcal_email_type_str[] = {
78 "home", // E_HOME
79 "other", // E_OTHER
80 "work" // E_WORK
83 /** Strings associated with address types */
84 const char* gcal_address_type_str[] = {
85 "home", // A_HOME
86 "work", // A_WORK
87 "other" // A_OTHER
90 /** Strings associated with im types */
91 const char* gcal_im_type_str[] = {
92 "home", // I_HOME
93 "work", // I_WORK
94 "netmeeting", // I_NETMEETING
95 "other" // I_OTHER
98 gcal_contact_t gcal_contact_new(char *raw_xml)
100 gcal_contact_t contact = NULL;
101 dom_document *doc;
102 int result = -1;
104 contact = (gcal_contact_t) malloc(sizeof(struct gcal_contact));
105 if (!contact)
106 goto exit;
108 gcal_init_contact(contact);
109 if (!raw_xml)
110 goto exit;
112 /* Builds a doc, parse and init object */
113 doc = build_dom_document(raw_xml);
114 if (!doc)
115 goto cleanup;
117 result = extract_all_contacts(doc, contact, 1);
118 clean_dom_document(doc);
120 cleanup:
121 if (result) {
122 free(contact);
123 contact = NULL;
125 exit:
126 return contact;
129 void gcal_contact_delete(gcal_contact_t contact)
131 if (!contact)
132 return;
134 gcal_destroy_contact(contact);
135 free(contact);
139 int gcal_get_contacts(gcal_t gcalobj, struct gcal_contact_array *contact_array)
141 int result = -1;
142 if (contact_array)
143 contact_array->length = 0;
145 if ((!gcalobj) || (!contact_array))
146 return result;
148 result = gcal_dump(gcalobj, "GData-Version: 3.0");
149 if (result == -1) {
150 contact_array->entries = NULL;
151 contact_array->length = 0;
152 return result;
155 contact_array->entries = gcal_get_all_contacts(gcalobj,
156 &contact_array->length);
157 if (!contact_array->entries)
158 return result;
160 result = 0;
162 return result;
166 void gcal_cleanup_contacts(struct gcal_contact_array *contacts)
168 if (!contacts)
169 return;
171 gcal_destroy_contacts(contacts->entries, contacts->length);
172 contacts->length = 0;
173 contacts->entries = NULL;
178 int gcal_add_contact(gcal_t gcalobj, gcal_contact_t contact)
180 int result = -1;
181 struct gcal_contact updated;
182 gcal_init_contact(&updated);
184 if ((!gcalobj) || (!contact))
185 goto exit;
188 result = gcal_create_contact(gcalobj, contact, &updated);
189 if (result)
190 goto exit;
192 /* Swap updated fields: id, updated, edit_uri, etag, photo url */
193 if (contact->common.id)
194 free(contact->common.id);
195 contact->common.id = updated.common.id;
196 updated.common.id = NULL;
198 if (contact->common.updated)
199 free(contact->common.updated);
200 contact->common.updated = updated.common.updated;
201 updated.common.updated = NULL;
203 if (contact->common.edit_uri)
204 free(contact->common.edit_uri);
205 contact->common.edit_uri = updated.common.edit_uri;
206 updated.common.edit_uri = NULL;
208 if (contact->common.etag)
209 free(contact->common.etag);
210 contact->common.etag = updated.common.etag;
211 updated.common.etag = NULL;
213 if (contact->photo)
214 free(contact->photo);
215 contact->photo = updated.photo;
216 updated.photo = NULL;
218 /* Cleanup updated contact */
219 gcal_destroy_contact(&updated);
221 exit:
222 return result;
225 int gcal_update_contact(gcal_t gcalobj, gcal_contact_t contact)
227 int result = -1;
228 struct gcal_contact updated;
229 gcal_init_contact(&updated);
231 if ((!gcalobj) || (!contact))
232 goto exit;
235 result = gcal_edit_contact(gcalobj, contact, &updated);
236 if (result)
237 goto exit;
239 /* Swap updated fields: updated, edit_uri, etag */
240 if (contact->common.updated)
241 free(contact->common.updated);
242 contact->common.updated = updated.common.updated;
243 updated.common.updated = NULL;
245 if (contact->common.edit_uri)
246 free(contact->common.edit_uri);
247 contact->common.edit_uri = updated.common.edit_uri;
248 updated.common.edit_uri = NULL;
250 if (contact->common.etag)
251 free(contact->common.etag);
252 contact->common.etag = updated.common.etag;
253 updated.common.etag = NULL;
255 if (contact->photo)
256 free(contact->photo);
257 contact->photo = updated.photo;
258 updated.photo = NULL;
260 /* Cleanup updated contact */
261 gcal_destroy_contact(&updated);
263 exit:
264 return result;
267 int gcal_erase_contact(gcal_t gcalobj, gcal_contact_t contact)
269 int result = -1;
270 if ((!gcalobj) || (!contact))
271 goto exit;
273 result = gcal_delete_contact(gcalobj, contact);
274 exit:
275 return result;
278 int gcal_get_updated_contacts(gcal_t gcal_obj,
279 struct gcal_contact_array *contacts,
280 char *timestamp)
282 int result = -1;
283 if (contacts)
284 contacts->length = 0;
286 if ((!gcal_obj) || (!contacts))
287 return result;
289 result = gcal_query_updated(gcal_obj, timestamp, "GData-Version: 3.0");
290 if (result) {
291 contacts->entries = NULL;
292 contacts->length = 0;
293 return result;
296 contacts->entries = gcal_get_all_contacts(gcal_obj, &contacts->length);
297 if (contacts->entries)
298 result = 0;
300 return result;
303 gcal_contact_t gcal_contact_element(struct gcal_contact_array *contacts,
304 size_t _index)
307 struct gcal_contact *contact = NULL;
308 if ((!contacts) || (_index > (contacts->length - 1)) ||
309 (contacts->length == 0))
310 return contact;
312 contact = &contacts->entries[_index];
313 return contact;
316 char *gcal_contact_get_xml(gcal_contact_t contact)
318 if ((!contact))
319 return NULL;
320 return gcal_get_xml(&(contact->common));
323 char *gcal_contact_get_id(gcal_contact_t contact)
325 if ((!contact))
326 return NULL;
327 return gcal_get_id(&(contact->common));
330 char *gcal_contact_get_updated(gcal_contact_t contact)
332 if ((!contact))
333 return NULL;
334 return gcal_get_updated(&(contact->common));
337 char *gcal_contact_get_title(gcal_contact_t contact)
339 if ((!contact))
340 return NULL;
341 return gcal_get_title(&(contact->common));
344 char *gcal_contact_get_url(gcal_contact_t contact)
346 if ((!contact))
347 return NULL;
348 return gcal_get_url(&(contact->common));
351 char *gcal_contact_get_etag(gcal_contact_t contact)
353 if ((!contact))
354 return NULL;
355 return gcal_get_etag(&(contact->common));
358 char gcal_contact_is_deleted(gcal_contact_t contact)
360 if ((!contact))
361 return -1;
362 return gcal_get_deleted(&(contact->common));
366 /* This are the fields unique to contacts */
367 int gcal_contact_get_emails_count(gcal_contact_t contact)
369 if ((!contact))
370 return -1;
371 return contact->emails_nr;
374 int gcal_contact_get_pref_email(gcal_contact_t contact)
376 if ((!contact))
377 return -1;
378 return contact->pref_email;
381 char *gcal_contact_get_email_address(gcal_contact_t contact, int i)
383 if ((!contact))
384 return NULL;
385 if (!(contact->emails_field) || (i >= contact->emails_nr))
386 return NULL;
387 return contact->emails_field[i];
390 char *gcal_contact_get_email(gcal_contact_t contact)
392 int tmp;
393 if ((!contact))
394 return NULL;
396 tmp = gcal_contact_get_pref_email(contact);
397 return gcal_contact_get_email_address(contact, tmp);
400 gcal_email_type gcal_contact_get_email_address_type(gcal_contact_t contact, int i)
402 gcal_email_type result = E_INVALID;
403 int j;
405 if ((!contact))
406 return result;
407 if (!(contact->emails_type) || (i >= contact->emails_nr))
408 return result;
409 for (j = 0; j < E_ITEMS_COUNT; j++)
410 if (!strcmp(contact->emails_type[i], gcal_email_type_str[j]))
411 result = j;
412 return result;
415 char *gcal_contact_get_content(gcal_contact_t contact)
417 if ((!contact))
418 return NULL;
419 return contact->content;
422 char *gcal_contact_get_nickname(gcal_contact_t contact)
424 if ((!contact))
425 return NULL;
426 return contact->nickname;
429 char *gcal_contact_get_organization(gcal_contact_t contact)
431 if ((!contact))
432 return NULL;
433 return contact->org_name;
436 char *gcal_contact_get_profission(gcal_contact_t contact)
438 if ((!contact))
439 return NULL;
440 return contact->org_title;
443 char *gcal_contact_get_occupation(gcal_contact_t contact)
445 if ((!contact))
446 return NULL;
447 return contact->occupation;
450 char *gcal_contact_get_homepage(gcal_contact_t contact)
452 if ((!contact))
453 return NULL;
454 return contact->homepage;
457 char *gcal_contact_get_blog(gcal_contact_t contact)
459 if ((!contact))
460 return NULL;
461 return contact->blog;
464 int gcal_contact_get_phone_numbers_count(gcal_contact_t contact)
466 if ((!contact))
467 return -1;
468 return contact->phone_numbers_nr;
471 char *gcal_contact_get_phone_number(gcal_contact_t contact, int i)
473 if ((!contact))
474 return NULL;
475 if (!(contact->phone_numbers_field) || (i >= contact->phone_numbers_nr))
476 return NULL;
477 return contact->phone_numbers_field[i];
480 char *gcal_contact_get_phone(gcal_contact_t contact)
482 if ((!contact))
483 return NULL;
485 char *res;
486 /* The prefered phone is *always* the first */
487 res = gcal_contact_get_phone_number(contact, 0);
488 return res;
491 gcal_phone_type gcal_contact_get_phone_number_type(gcal_contact_t contact, int i)
493 gcal_phone_type result = P_INVALID;
494 int j;
496 if ((!contact))
497 return result;
498 if (!(contact->phone_numbers_type) || (i >= contact->phone_numbers_nr))
499 return result;
500 for (j = 0; j < P_ITEMS_COUNT; j++)
501 if (!strcmp(contact->phone_numbers_type[i], gcal_phone_type_str[j]))
502 result = j;
503 return result;
506 int gcal_contact_get_im_count(gcal_contact_t contact)
508 if ((!contact))
509 return -1;
510 return contact->im_nr;
513 int gcal_contact_get_pref_im(gcal_contact_t contact)
515 if ((!contact))
516 return -1;
517 return contact->im_pref;
520 char *gcal_contact_get_im_protocol(gcal_contact_t contact, int i)
522 if ((!contact))
523 return NULL;
524 if (!(contact->im_protocol) || (i >= contact->im_nr))
525 return NULL;
526 return contact->im_protocol[i];
529 char *gcal_contact_get_im_address(gcal_contact_t contact, int i)
531 if ((!contact))
532 return NULL;
533 if (!(contact->im_address) || (i >= contact->im_nr))
534 return NULL;
535 return contact->im_address[i];
538 gcal_phone_type gcal_contact_get_im_type(gcal_contact_t contact, int i)
540 gcal_im_type result = P_INVALID;
541 int j;
543 if ((!contact))
544 return result;
545 if (!(contact->im_type) || (i >= contact->im_nr))
546 return result;
547 for (j = 0; j < I_ITEMS_COUNT; j++)
548 if (!strcmp(contact->im_type[i], gcal_im_type_str[j]))
549 result = j;
550 return result;
553 gcal_structured_subvalues_t gcal_contact_get_structured_name(gcal_contact_t contact)
555 if ((!contact) || (!contact->structured_name))
556 return NULL;
557 return contact->structured_name;
560 char *gcal_contact_get_address(gcal_contact_t contact)
562 if ((!contact))
563 return NULL;
564 return contact->post_address;
567 gcal_structured_subvalues_t gcal_contact_get_structured_address(gcal_contact_t contact)
569 if ((!contact) || (!contact->structured_address))
570 return NULL;
571 return contact->structured_address;
574 int gcal_contact_get_structured_address_count(gcal_contact_t contact)
576 if ((!contact))
577 return -1;
578 return contact->structured_address_nr;
581 int *gcal_contact_get_structured_address_count_obj(gcal_contact_t contact)
583 if ((!contact))
584 return NULL;
585 return &contact->structured_address_nr;
588 char *gcal_contact_get_structured_entry(gcal_structured_subvalues_t structured_entry,
589 int structured_entry_nr,
590 int structured_entry_count,
591 const char *field_key)
593 struct gcal_structured_subvalues *temp_structured_entry;
595 if(field_key == NULL)
596 field_key = "";
598 if (!structured_entry || (structured_entry_nr >= structured_entry_count))
599 return NULL;
601 for (temp_structured_entry = structured_entry;
602 temp_structured_entry != NULL;
603 temp_structured_entry = temp_structured_entry->next_field) {
605 if (temp_structured_entry->next_field != NULL) {
606 if (!strcmp(temp_structured_entry->field_key, field_key)
607 && (temp_structured_entry->field_typenr == structured_entry_nr)) {
608 return temp_structured_entry->field_value;
613 return NULL;
616 char ***gcal_contact_get_structured_address_type_obj(gcal_contact_t contact)
618 if ((!contact))
619 return NULL;
620 return &contact->structured_address_type;
623 gcal_address_type gcal_contact_get_structured_address_type(gcal_contact_t contact,
624 int structured_entry_nr,
625 int structured_entry_count)
627 gcal_address_type result = A_INVALID;
628 int j;
630 if ((!contact))
631 return result;
633 if (!(contact->structured_address_type) ||
634 (structured_entry_nr >= structured_entry_count))
635 return result;
637 for (j = 0; j < A_ITEMS_COUNT; j++)
638 if (!strcmp(contact->structured_address_type[structured_entry_nr], gcal_address_type_str[j]))
639 result = j;
641 return result;
644 int gcal_contact_get_pref_structured_address(gcal_contact_t contact)
646 if ((!contact))
647 return -1;
648 return contact->structured_address_pref;
651 int gcal_contact_get_groupMembership_count(gcal_contact_t contact)
653 if ((!contact))
654 return -1;
655 return contact->groupMembership_nr;
658 char *gcal_contact_get_groupMembership(gcal_contact_t contact, int i)
660 if ((!contact))
661 return NULL;
662 if (!(contact->groupMembership) || (i >= contact->groupMembership_nr))
663 return NULL;
664 return contact->groupMembership[i];
667 char *gcal_contact_get_photo(gcal_contact_t contact)
669 if ((!contact))
670 return NULL;
672 return contact->photo_data;
675 unsigned int gcal_contact_get_photolength(gcal_contact_t contact)
677 if ((!contact))
678 return -1;
680 return contact->photo_length;
683 char *gcal_contact_get_birthday(gcal_contact_t contact)
685 if ((!contact))
686 return NULL;
687 return contact->birthday;
690 /* Here starts the gcal_contact setters */
691 int gcal_contact_set_title(gcal_contact_t contact, const char *field)
693 int result = -1;
695 if ((!contact) || (!field))
696 return result;
698 if (contact->common.title)
699 free(contact->common.title);
701 contact->common.title = strdup(field);
702 if (contact->common.title)
703 result = 0;
705 return result;
708 int gcal_contact_delete_email_addresses(gcal_contact_t contact)
710 int result = -1;
711 int temp;
713 if (!contact)
714 return result;
716 if (contact->emails_nr > 0) {
717 for (temp = 0; temp < contact->emails_nr; temp++) {
718 if (contact->emails_field[temp])
719 free(contact->emails_field[temp]);
720 if (contact->emails_type[temp])
721 free(contact->emails_type[temp]);
724 free(contact->emails_field);
725 free(contact->emails_type);
728 contact->emails_nr = contact->pref_email = 0;
730 /* XXX: Think, this is obsolete??? */
731 contact->emails_field = contact->emails_type = 0;
733 result = 0;
735 return result;
738 int gcal_contact_add_email_address(gcal_contact_t contact, const char *field,
739 gcal_email_type type, int pref)
741 int result = -1;
743 if ((!contact) || (!field) || (type<0) || (type>=E_ITEMS_COUNT))
744 return result;
746 contact->emails_field = (char**) realloc(contact->emails_field,
747 (contact->emails_nr+1) *
748 sizeof(char*));
750 contact->emails_field[contact->emails_nr] = strdup(field);
752 contact->emails_type = (char**) realloc(contact->emails_type,
753 (contact->emails_nr+1) *
754 sizeof(char*));
756 contact->emails_type[contact->emails_nr] = strdup(gcal_email_type_str[type]);
758 if (pref)
759 contact->pref_email = contact->emails_nr;
761 contact->emails_nr++;
763 result = 0;
765 return result;
768 int gcal_contact_set_email(gcal_contact_t contact, const char *pref_email)
770 int res;
771 res = gcal_contact_add_email_address(contact, pref_email, E_HOME, 1);
772 return res;
775 int gcal_contact_set_url(gcal_contact_t contact, const char *field)
777 int result = -1;
779 if ((!contact) || (!field))
780 return result;
782 if (contact->common.edit_uri)
783 free(contact->common.edit_uri);
785 contact->common.edit_uri = strdup(field);
786 if (contact->common.edit_uri)
787 result = 0;
789 return result;
793 int gcal_contact_set_id(gcal_contact_t contact, const char *field)
795 int result = -1;
797 if ((!contact) || (!field))
798 return result;
800 if (contact->common.id)
801 free(contact->common.id);
803 contact->common.id = strdup(field);
804 if (contact->common.id)
805 result = 0;
807 return result;
811 int gcal_contact_set_etag(gcal_contact_t contact, const char *field)
813 int result = -1;
815 if ((!contact) || (!field))
816 return result;
818 if (contact->common.etag)
819 free(contact->common.etag);
821 contact->common.etag = strdup(field);
822 if (contact->common.etag)
823 result = 0;
825 return result;
828 int gcal_contact_delete_phone_numbers(gcal_contact_t contact)
830 int result = -1;
831 int temp;
833 if (!contact)
834 return result;
836 if (contact->phone_numbers_nr > 0) {
837 for (temp = 0; temp < contact->phone_numbers_nr; temp++) {
838 if (contact->phone_numbers_field[temp])
839 free(contact->phone_numbers_field[temp]);
840 if (contact->phone_numbers_type[temp])
841 free(contact->phone_numbers_type[temp]);
844 free(contact->phone_numbers_field);
845 free(contact->phone_numbers_type);
848 contact->phone_numbers_nr = 0;
850 result = 0;
852 return result;
855 int gcal_contact_add_phone_number(gcal_contact_t contact, const char *field,
856 gcal_phone_type type)
858 int result = -1;
860 if ((!contact) || (!field) || (type<0) || (type>=P_ITEMS_COUNT))
861 return result;
863 contact->phone_numbers_field = (char**) realloc(contact->phone_numbers_field, (contact->phone_numbers_nr+1) * sizeof(char*));
864 contact->phone_numbers_field[contact->phone_numbers_nr] = strdup(field);
866 contact->phone_numbers_type = (char**) realloc(contact->phone_numbers_type, (contact->phone_numbers_nr+1) * sizeof(char*));
867 contact->phone_numbers_type[contact->phone_numbers_nr] = strdup(gcal_phone_type_str[type]);
869 contact->phone_numbers_nr++;
871 result = 0;
873 return result;
876 int gcal_contact_set_phone(gcal_contact_t contact, const char *phone)
878 int res;
879 res = gcal_contact_delete_phone_numbers(contact);
880 if (res)
881 return res;
883 res = gcal_contact_add_phone_number(contact, phone, P_MOBILE);
884 return res;
887 int gcal_contact_delete_im(gcal_contact_t contact)
889 int result = -1;
890 int temp;
892 if (!contact)
893 return result;
895 if (contact->im_nr > 0) {
896 for (temp = 0; temp < contact->im_nr; temp++) {
897 if (contact->im_protocol[temp])
898 free(contact->im_protocol[temp]);
899 if (contact->im_address[temp])
900 free(contact->im_address[temp]);
901 if (contact->im_type[temp])
902 free(contact->im_type[temp]);
904 free(contact->im_protocol);
905 free(contact->im_address);
906 free(contact->im_type);
909 contact->im_nr = contact->im_pref = 0;
911 result = 0;
913 return result;
916 int gcal_contact_add_im(gcal_contact_t contact, const char *protcol,
917 const char *address, gcal_im_type type, int pref)
919 int result = -1;
921 if ((!contact) || (!protcol) || (!address) || (type<0) || (type>=I_ITEMS_COUNT))
922 return result;
924 contact->im_protocol = (char**) realloc(contact->im_protocol, (contact->im_nr+1) * sizeof(char*));
925 contact->im_protocol[contact->im_nr] = strdup(protcol);
927 contact->im_address = (char**) realloc(contact->im_address, (contact->im_nr+1) * sizeof(char*));
928 contact->im_address[contact->im_nr] = strdup(address);
930 contact->im_type = (char**) realloc(contact->im_type, (contact->im_nr+1) * sizeof(char*));
931 contact->im_type[contact->im_nr] = strdup(gcal_im_type_str[type]);
933 if (pref)
934 contact->im_pref = contact->im_nr;
936 contact->im_nr++;
938 result = 0;
940 return result;
943 int gcal_contact_set_address(gcal_contact_t contact, const char *field)
945 int result = -1;
947 if ((!contact) || (!field))
948 return result;
950 if (contact->post_address)
951 free(contact->post_address);
953 contact->post_address = strdup(field);
954 if (contact->post_address)
955 result = 0;
957 return result;
960 int gcal_contact_set_structured_address_nr(gcal_contact_t contact,
961 gcal_address_type type)
963 int entry_nr, result = -1;
965 if (!contact || (type < 0) || (type >= A_ITEMS_COUNT))
966 return result;
968 entry_nr = contact->structured_address_nr;
969 contact->structured_address_type = (char**) realloc(contact->structured_address_type, (entry_nr + 1) * sizeof(char*));
970 contact->structured_address_type[entry_nr] = strdup(gcal_address_type_str[type]);
971 contact->structured_address_nr++;
973 result = entry_nr;
975 return result;
978 int gcal_contact_set_pref_structured_address(gcal_contact_t contact, int pref_address)
980 int result = -1;
982 if ((!contact) || (pref_address < 0))
983 return result;
985 contact->structured_address_pref = pref_address;
987 result = 0;
989 return result;
992 int gcal_contact_set_structured_entry(gcal_structured_subvalues_t structured_entry,
993 int structured_entry_nr,
994 int structured_entry_count,
995 const char *field_key,
996 const char *field_value )
998 struct gcal_structured_subvalues *temp_structured_entry;
1000 if (!structured_entry || (!field_value) || (!field_key) ||
1001 (structured_entry_nr < 0) ||
1002 (structured_entry_nr >= structured_entry_count))
1003 return -1;
1005 if (field_value == NULL)
1006 field_value = "";
1008 if (structured_entry->field_key == NULL) {
1009 structured_entry->field_typenr = structured_entry_nr;
1010 structured_entry->field_key = strdup(field_key);
1011 structured_entry->field_value = strdup(field_value);
1012 structured_entry->next_field = NULL;
1013 return 0;
1016 for (temp_structured_entry = structured_entry; temp_structured_entry; temp_structured_entry = temp_structured_entry->next_field) {
1017 if (!strcmp(temp_structured_entry->field_key,field_key) &&
1018 (temp_structured_entry->field_typenr == structured_entry_nr)) {
1019 if (temp_structured_entry->field_value != NULL) {
1020 free(temp_structured_entry->field_value);
1021 temp_structured_entry->field_value = strdup(field_value);
1022 return 0;
1026 if (temp_structured_entry->next_field == NULL) {
1027 temp_structured_entry->next_field = (struct gcal_structured_subvalues *)malloc(sizeof(struct gcal_structured_subvalues));
1028 temp_structured_entry = temp_structured_entry->next_field;
1030 temp_structured_entry->field_typenr = structured_entry_nr;
1031 temp_structured_entry->field_key = strdup(field_key);
1032 temp_structured_entry->field_value = strdup(field_value);
1033 temp_structured_entry->next_field = NULL;
1035 return 0;
1038 return -1;
1041 int gcal_contact_delete_structured_entry(gcal_structured_subvalues_t structured_entry,
1042 int *structured_entry_count,
1043 char ***structured_entry_type)
1045 int i, result = -1;
1046 struct gcal_structured_subvalues *temp_structured_entry;
1048 if (!structured_entry)
1049 return result;
1051 for (temp_structured_entry = structured_entry;
1052 temp_structured_entry != NULL;
1053 temp_structured_entry = temp_structured_entry->next_field) {
1054 if (temp_structured_entry->field_typenr)
1055 temp_structured_entry->field_typenr = 0;
1056 if (temp_structured_entry->field_key)
1057 free(temp_structured_entry->field_key);
1058 if (temp_structured_entry->field_value)
1059 free(temp_structured_entry->field_value);
1062 if (structured_entry_count && structured_entry_type) {
1063 if ((*structured_entry_count) > 0) {
1064 for (i = 0; i < (*structured_entry_count); i++)
1065 if ((*structured_entry_type)[i])
1066 free((*structured_entry_type)[i]);
1067 free((*structured_entry_type));
1070 (*structured_entry_count) = 0;
1073 result = 0;
1074 return result;
1077 int gcal_contact_delete_groupMembership(gcal_contact_t contact)
1079 int result = -1;
1080 int temp;
1082 if (!contact)
1083 return result;
1085 if (contact->groupMembership_nr > 0) {
1086 for (temp = 0; temp < contact->groupMembership_nr; temp++) {
1087 if (contact->groupMembership[temp])
1088 free(contact->groupMembership[temp]);
1091 free(contact->groupMembership);
1094 contact->groupMembership_nr = 0;
1095 result = 0;
1096 return result;
1099 int gcal_contact_add_groupMembership(gcal_contact_t contact, char *field)
1101 int result = -1;
1103 if ((!contact) || (!field))
1104 return result;
1106 contact->groupMembership = (char**) realloc(contact->groupMembership, (contact->groupMembership_nr+1) * sizeof(char*));
1107 contact->groupMembership[contact->groupMembership_nr] = strdup(field);
1109 contact->groupMembership_nr++;
1111 result = 0;
1113 return result;
1116 int gcal_contact_set_profission(gcal_contact_t contact, const char *field)
1118 int result = -1;
1120 if ((!contact) || (!field))
1121 return result;
1123 if (contact->org_title)
1124 free(contact->org_title);
1126 contact->org_title = strdup(field);
1127 if (contact->org_title)
1128 result = 0;
1130 return result;
1134 int gcal_contact_set_organization(gcal_contact_t contact, const char *field)
1136 int result = -1;
1138 if ((!contact) || (!field))
1139 return result;
1141 if (contact->org_name)
1142 free(contact->org_name);
1144 contact->org_name = strdup(field);
1145 if (contact->org_name)
1146 result = 0;
1148 return result;
1151 int gcal_contact_set_occupation(gcal_contact_t contact, const char *field)
1153 int result = -1;
1155 if ((!contact) || (!field))
1156 return result;
1158 if (contact->occupation)
1159 free(contact->occupation);
1161 contact->occupation = strdup(field);
1162 if (contact->occupation)
1163 result = 0;
1165 return result;
1168 int gcal_contact_set_content(gcal_contact_t contact, const char *field)
1170 int result = -1;
1172 if ((!contact) || (!field))
1173 return result;
1175 if (contact->content)
1176 free(contact->content);
1178 contact->content = strdup(field);
1179 if (contact->content)
1180 result = 0;
1182 return result;
1185 int gcal_contact_set_nickname(gcal_contact_t contact, const char *field)
1187 int result = -1;
1189 if ((!contact) || (!field))
1190 return result;
1192 if (contact->nickname)
1193 free(contact->nickname);
1195 contact->nickname = strdup(field);
1196 if (contact->nickname)
1197 result = 0;
1199 return result;
1202 int gcal_contact_set_photo(gcal_contact_t contact, const char *field,
1203 int length)
1205 int result = -1;
1207 if ((!contact) || (!field))
1208 return result;
1210 if (contact->photo_data)
1211 if (contact->photo_length > 1)
1212 free(contact->photo_data);
1214 if (!(contact->photo_data = malloc(length * sizeof(unsigned char))))
1215 return result;
1217 memcpy(contact->photo_data, field, length);
1218 contact->photo_length = length;
1219 result = 0;
1221 return result;
1224 int gcal_contact_set_birthday(gcal_contact_t contact, const char *field)
1226 int result = -1;
1228 if ((!contact) || (!field))
1229 return result;
1231 if (contact->birthday)
1232 free(contact->birthday);
1234 contact->birthday = strdup(field);
1235 if (contact->birthday)
1236 result = 0;
1238 return result;
1241 int gcal_contact_set_homepage(gcal_contact_t contact, const char *field)
1243 int result = -1;
1245 if ((!contact) || (!field))
1246 return result;
1248 if (contact->homepage)
1249 free(contact->homepage);
1251 contact->homepage = strdup(field);
1252 if (contact->homepage)
1253 result = 0;
1255 return result;
1258 int gcal_contact_set_blog(gcal_contact_t contact, const char *field)
1260 int result = -1;
1262 if ((!contact) || (!field))
1263 return result;
1265 if (contact->blog)
1266 free(contact->blog);
1268 contact->blog = strdup(field);
1269 if (contact->blog)
1270 result = 0;
1272 return result;