Some unit test regarding the new fields.
[libgcal.git] / src / gcontact.c
blob759043b41a33b41718e56ef46e1496ecb2582129
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 gcal_contact_t gcal_contact_new(char *raw_xml)
92 gcal_contact_t contact = NULL;
93 dom_document *doc;
94 int result = -1;
96 contact = (gcal_contact_t) malloc(sizeof(struct gcal_contact));
97 if (!contact)
98 goto exit;
100 gcal_init_contact(contact);
101 if (!raw_xml)
102 goto exit;
104 /* Builds a doc, parse and init object */
105 doc = build_dom_document(raw_xml);
106 if (!doc)
107 goto cleanup;
109 result = extract_all_contacts(doc, contact, 1);
110 clean_dom_document(doc);
112 cleanup:
113 if (result) {
114 free(contact);
115 contact = NULL;
117 exit:
118 return contact;
121 void gcal_contact_delete(gcal_contact_t contact)
123 if (!contact)
124 return;
126 gcal_destroy_contact(contact);
127 free(contact);
131 int gcal_get_contacts(gcal_t gcalobj, struct gcal_contact_array *contact_array)
133 int result = -1;
134 if (contact_array)
135 contact_array->length = 0;
137 if ((!gcalobj) || (!contact_array))
138 return result;
140 result = gcal_dump(gcalobj, "GData-Version: 3.0");
141 if (result == -1) {
142 contact_array->entries = NULL;
143 contact_array->length = 0;
144 return result;
147 contact_array->entries = gcal_get_all_contacts(gcalobj,
148 &contact_array->length);
149 if (!contact_array->entries)
150 return result;
152 result = 0;
154 return result;
158 void gcal_cleanup_contacts(struct gcal_contact_array *contacts)
160 if (!contacts)
161 return;
163 gcal_destroy_contacts(contacts->entries, contacts->length);
164 contacts->length = 0;
165 contacts->entries = NULL;
170 int gcal_add_contact(gcal_t gcalobj, gcal_contact_t contact)
172 int result = -1;
173 struct gcal_contact updated;
174 gcal_init_contact(&updated);
176 if ((!gcalobj) || (!contact))
177 goto exit;
180 result = gcal_create_contact(gcalobj, contact, &updated);
181 if (result)
182 goto exit;
184 /* Swap updated fields: id, updated, edit_uri, etag, photo url */
185 if (contact->common.id)
186 free(contact->common.id);
187 contact->common.id = updated.common.id;
188 updated.common.id = NULL;
190 if (contact->common.updated)
191 free(contact->common.updated);
192 contact->common.updated = updated.common.updated;
193 updated.common.updated = NULL;
195 if (contact->common.edit_uri)
196 free(contact->common.edit_uri);
197 contact->common.edit_uri = updated.common.edit_uri;
198 updated.common.edit_uri = NULL;
200 if (contact->common.etag)
201 free(contact->common.etag);
202 contact->common.etag = updated.common.etag;
203 updated.common.etag = NULL;
205 if (contact->photo)
206 free(contact->photo);
207 contact->photo = updated.photo;
208 updated.photo = NULL;
210 /* Cleanup updated contact */
211 gcal_destroy_contact(&updated);
213 exit:
214 return result;
217 int gcal_update_contact(gcal_t gcalobj, gcal_contact_t contact)
219 int result = -1;
220 struct gcal_contact updated;
221 gcal_init_contact(&updated);
223 if ((!gcalobj) || (!contact))
224 goto exit;
227 result = gcal_edit_contact(gcalobj, contact, &updated);
228 if (result)
229 goto exit;
231 /* Swap updated fields: updated, edit_uri, etag */
232 if (contact->common.updated)
233 free(contact->common.updated);
234 contact->common.updated = updated.common.updated;
235 updated.common.updated = NULL;
237 if (contact->common.edit_uri)
238 free(contact->common.edit_uri);
239 contact->common.edit_uri = updated.common.edit_uri;
240 updated.common.edit_uri = NULL;
242 if (contact->common.etag)
243 free(contact->common.etag);
244 contact->common.etag = updated.common.etag;
245 updated.common.etag = NULL;
247 if (contact->photo)
248 free(contact->photo);
249 contact->photo = updated.photo;
250 updated.photo = NULL;
252 /* Cleanup updated contact */
253 gcal_destroy_contact(&updated);
255 exit:
256 return result;
259 int gcal_erase_contact(gcal_t gcalobj, gcal_contact_t contact)
261 int result = -1;
262 if ((!gcalobj) || (!contact))
263 goto exit;
265 result = gcal_delete_contact(gcalobj, contact);
266 exit:
267 return result;
270 int gcal_get_updated_contacts(gcal_t gcal_obj,
271 struct gcal_contact_array *contacts,
272 char *timestamp)
274 int result = -1;
275 if (contacts)
276 contacts->length = 0;
278 if ((!gcal_obj) || (!contacts))
279 return result;
281 result = gcal_query_updated(gcal_obj, timestamp, "GData-Version: 3.0");
282 if (result) {
283 contacts->entries = NULL;
284 contacts->length = 0;
285 return result;
288 contacts->entries = gcal_get_all_contacts(gcal_obj, &contacts->length);
289 if (contacts->entries)
290 result = 0;
292 return result;
295 gcal_contact_t gcal_contact_element(struct gcal_contact_array *contacts,
296 size_t _index)
299 struct gcal_contact *contact = NULL;
300 if ((!contacts) || (_index > (contacts->length - 1)) ||
301 (contacts->length == 0))
302 return contact;
304 contact = &contacts->entries[_index];
305 return contact;
308 char *gcal_contact_get_xml(gcal_contact_t contact)
310 if ((!contact))
311 return NULL;
312 return gcal_get_xml(&(contact->common));
315 char *gcal_contact_get_id(gcal_contact_t contact)
317 if ((!contact))
318 return NULL;
319 return gcal_get_id(&(contact->common));
322 char *gcal_contact_get_updated(gcal_contact_t contact)
324 if ((!contact))
325 return NULL;
326 return gcal_get_updated(&(contact->common));
329 char *gcal_contact_get_title(gcal_contact_t contact)
331 if ((!contact))
332 return NULL;
333 return gcal_get_title(&(contact->common));
336 char *gcal_contact_get_url(gcal_contact_t contact)
338 if ((!contact))
339 return NULL;
340 return gcal_get_url(&(contact->common));
343 char *gcal_contact_get_etag(gcal_contact_t contact)
345 if ((!contact))
346 return NULL;
347 return gcal_get_etag(&(contact->common));
350 char gcal_contact_is_deleted(gcal_contact_t contact)
352 if ((!contact))
353 return -1;
354 return gcal_get_deleted(&(contact->common));
358 /* This are the fields unique to contacts */
359 int gcal_contact_get_emails_count(gcal_contact_t contact)
361 if ((!contact))
362 return -1;
363 return contact->emails_nr;
366 int gcal_contact_get_pref_email(gcal_contact_t contact)
368 if ((!contact))
369 return -1;
370 return contact->pref_email;
373 char *gcal_contact_get_email_address(gcal_contact_t contact, int i)
375 if ((!contact))
376 return NULL;
377 if (!(contact->emails_field) || (i >= contact->emails_nr))
378 return NULL;
379 return contact->emails_field[i];
382 gcal_email_type gcal_contact_get_email_address_type(gcal_contact_t contact, int i)
384 gcal_email_type result = E_INVALID;
385 int j;
387 if ((!contact))
388 return result;
389 if (!(contact->emails_type) || (i >= contact->emails_nr))
390 return result;
391 for (j = 0; j < E_ITEMS_COUNT; j++)
392 if (!strcmp(contact->emails_type[i], gcal_email_type_str[j]))
393 result = j;
394 return result;
397 char *gcal_contact_get_content(gcal_contact_t contact)
399 if ((!contact))
400 return NULL;
401 return contact->content;
404 char *gcal_contact_get_nickname(gcal_contact_t contact)
406 if ((!contact))
407 return NULL;
408 return contact->nickname;
411 char *gcal_contact_get_organization(gcal_contact_t contact)
413 if ((!contact))
414 return NULL;
415 return contact->org_name;
418 char *gcal_contact_get_profission(gcal_contact_t contact)
420 if ((!contact))
421 return NULL;
422 return contact->org_title;
425 char *gcal_contact_get_occupation(gcal_contact_t contact)
427 if ((!contact))
428 return NULL;
429 return contact->occupation;
432 char *gcal_contact_get_im(gcal_contact_t contact)
434 if ((!contact))
435 return NULL;
436 return contact->im;
439 char *gcal_contact_get_homepage(gcal_contact_t contact)
441 if ((!contact))
442 return NULL;
443 return contact->homepage;
446 char *gcal_contact_get_blog(gcal_contact_t contact)
448 if ((!contact))
449 return NULL;
450 return contact->blog;
453 int gcal_contact_get_phone_numbers_count(gcal_contact_t contact)
455 if ((!contact))
456 return -1;
457 return contact->phone_numbers_nr;
460 char *gcal_contact_get_phone_number(gcal_contact_t contact, int i)
462 if ((!contact))
463 return NULL;
464 if (!(contact->phone_numbers_field) || (i >= contact->phone_numbers_nr))
465 return NULL;
466 return contact->phone_numbers_field[i];
469 gcal_phone_type gcal_contact_get_phone_number_type(gcal_contact_t contact, int i)
471 gcal_phone_type result = P_INVALID;
472 int j;
474 if ((!contact))
475 return result;
476 if (!(contact->phone_numbers_type) || (i >= contact->phone_numbers_nr))
477 return result;
478 for (j = 0; j < P_ITEMS_COUNT; j++)
479 if (!strcmp(contact->phone_numbers_type[i], gcal_phone_type_str[j]))
480 result = j;
481 return result;
484 gcal_structured_subvalues_t gcal_contact_get_structured_name(gcal_contact_t contact)
486 if ((!contact) || (!contact->structured_name))
487 return NULL;
488 return contact->structured_name;
491 char *gcal_contact_get_address(gcal_contact_t contact)
493 if ((!contact))
494 return NULL;
495 return contact->post_address;
498 gcal_structured_subvalues_t gcal_contact_get_structured_address(gcal_contact_t contact)
500 if ((!contact) || (!contact->structured_address))
501 return NULL;
502 return contact->structured_address;
505 int gcal_contact_get_structured_address_count(gcal_contact_t contact)
507 if ((!contact))
508 return -1;
509 return contact->structured_address_nr;
512 int *gcal_contact_get_structured_address_count_obj(gcal_contact_t contact)
514 if ((!contact))
515 return NULL;
516 return &contact->structured_address_nr;
519 char *gcal_contact_get_structured_entry(gcal_structured_subvalues_t structured_entry,
520 int structured_entry_nr,
521 int structured_entry_count,
522 const char *field_key)
524 struct gcal_structured_subvalues *temp_structured_entry;
526 if(field_key == NULL)
527 field_key = "";
529 if (!structured_entry || (structured_entry_nr >= structured_entry_count))
530 return NULL;
532 for (temp_structured_entry = structured_entry;
533 temp_structured_entry != NULL;
534 temp_structured_entry = temp_structured_entry->next_field) {
536 if (temp_structured_entry->next_field != NULL) {
537 if (!strcmp(temp_structured_entry->field_key, field_key)
538 && (temp_structured_entry->field_typenr == structured_entry_nr)) {
539 return temp_structured_entry->field_value;
544 return NULL;
547 char ***gcal_contact_get_structured_address_type_obj(gcal_contact_t contact)
549 if ((!contact))
550 return NULL;
551 return &contact->structured_address_type;
554 gcal_address_type gcal_contact_get_structured_address_type(gcal_contact_t contact,
555 int structured_entry_nr,
556 int structured_entry_count)
558 gcal_address_type result = A_INVALID;
559 int j;
561 if ((!contact))
562 return result;
564 if (!(contact->structured_address_type) ||
565 (structured_entry_nr >= structured_entry_count))
566 return result;
568 for (j = 0; j < A_ITEMS_COUNT; j++)
569 if (!strcmp(contact->structured_address_type[structured_entry_nr], gcal_address_type_str[j]))
570 result = j;
572 return result;
575 int gcal_contact_get_groupMembership_count(gcal_contact_t contact)
577 if ((!contact))
578 return -1;
579 return contact->groupMembership_nr;
582 char *gcal_contact_get_groupMembership(gcal_contact_t contact, int i)
584 if ((!contact))
585 return NULL;
586 if (!(contact->groupMembership) || (i >= contact->groupMembership_nr))
587 return NULL;
588 return contact->groupMembership[i];
591 char *gcal_contact_get_photo(gcal_contact_t contact)
593 if ((!contact))
594 return NULL;
596 return contact->photo_data;
599 unsigned int gcal_contact_get_photolength(gcal_contact_t contact)
601 if ((!contact))
602 return -1;
604 return contact->photo_length;
607 char *gcal_contact_get_birthday(gcal_contact_t contact)
609 if ((!contact))
610 return NULL;
611 return contact->birthday;
614 /* Here starts the gcal_contact setters */
615 int gcal_contact_set_title(gcal_contact_t contact, const char *field)
617 int result = -1;
619 if ((!contact) || (!field))
620 return result;
622 if (contact->common.title)
623 free(contact->common.title);
625 contact->common.title = strdup(field);
626 if (contact->common.title)
627 result = 0;
629 return result;
632 int gcal_contact_delete_email_addresses(gcal_contact_t contact)
634 int result = -1;
635 int temp;
637 if (!contact)
638 return result;
640 if (contact->emails_nr > 0) {
641 for (temp = 0; temp < contact->emails_nr; temp++) {
642 if (contact->emails_field[temp])
643 free(contact->emails_field[temp]);
644 if (contact->emails_type[temp])
645 free(contact->emails_type[temp]);
648 free(contact->emails_field);
649 free(contact->emails_type);
652 contact->emails_nr = contact->pref_email = 0;
653 contact->emails_field = contact->emails_type = 0;
655 result = 0;
657 return result;
660 int gcal_contact_add_email_address(gcal_contact_t contact, const char *field,
661 gcal_email_type type, int pref)
663 int result = -1;
665 if ((!contact) || (!field) || (type<0) || (type>=E_ITEMS_COUNT))
666 return result;
668 contact->emails_field = (char**) realloc(contact->emails_field,
669 (contact->emails_nr+1) *
670 sizeof(char*));
672 contact->emails_field[contact->emails_nr] = strdup(field);
674 contact->emails_type = (char**) realloc(contact->emails_type,
675 (contact->emails_nr+1) *
676 sizeof(char*));
678 contact->emails_type[contact->emails_nr] = strdup(gcal_email_type_str[type]);
680 if (pref)
681 contact->pref_email = contact->emails_nr;
683 contact->emails_nr++;
685 result = 0;
687 return result;
690 int gcal_contact_set_url(gcal_contact_t contact, const char *field)
692 int result = -1;
694 if ((!contact) || (!field))
695 return result;
697 if (contact->common.edit_uri)
698 free(contact->common.edit_uri);
700 contact->common.edit_uri = strdup(field);
701 if (contact->common.edit_uri)
702 result = 0;
704 return result;
708 int gcal_contact_set_id(gcal_contact_t contact, const char *field)
710 int result = -1;
712 if ((!contact) || (!field))
713 return result;
715 if (contact->common.id)
716 free(contact->common.id);
718 contact->common.id = strdup(field);
719 if (contact->common.id)
720 result = 0;
722 return result;
726 int gcal_contact_set_etag(gcal_contact_t contact, const char *field)
728 int result = -1;
730 if ((!contact) || (!field))
731 return result;
733 if (contact->common.etag)
734 free(contact->common.etag);
736 contact->common.etag = strdup(field);
737 if (contact->common.etag)
738 result = 0;
740 return result;
743 int gcal_contact_delete_phone_numbers(gcal_contact_t contact)
745 int result = -1;
746 int temp;
748 if (!contact)
749 return result;
751 if (contact->phone_numbers_nr > 0) {
752 for (temp = 0; temp < contact->phone_numbers_nr; temp++) {
753 if (contact->phone_numbers_field[temp])
754 free(contact->phone_numbers_field[temp]);
755 if (contact->phone_numbers_type[temp])
756 free(contact->phone_numbers_type[temp]);
759 free(contact->phone_numbers_field);
760 free(contact->phone_numbers_type);
763 contact->phone_numbers_nr = 0;
765 result = 0;
767 return result;
770 int gcal_contact_add_phone_number(gcal_contact_t contact, const char *field,
771 gcal_phone_type type)
773 int result = -1;
775 if ((!contact) || (!field) || (type<0) || (type>=P_ITEMS_COUNT))
776 return result;
778 contact->phone_numbers_field = (char**) realloc(contact->phone_numbers_field, (contact->phone_numbers_nr+1) * sizeof(char*));
779 contact->phone_numbers_field[contact->phone_numbers_nr] = strdup(field);
781 contact->phone_numbers_type = (char**) realloc(contact->phone_numbers_type, (contact->phone_numbers_nr+1) * sizeof(char*));
782 contact->phone_numbers_type[contact->phone_numbers_nr] = strdup(gcal_phone_type_str[type]);
784 contact->phone_numbers_nr++;
786 result = 0;
788 return result;
791 int gcal_contact_set_address(gcal_contact_t contact, const char *field)
793 int result = -1;
795 if ((!contact) || (!field))
796 return result;
798 if (contact->post_address)
799 free(contact->post_address);
801 contact->post_address = strdup(field);
802 if (contact->post_address)
803 result = 0;
805 return result;
808 int gcal_contact_set_structured_address_nr(gcal_contact_t contact,
809 gcal_address_type type)
811 int entry_nr, result = -1;
813 if (!contact || (type < 0) || (type >= A_ITEMS_COUNT))
814 return result;
816 entry_nr = contact->structured_address_nr;
817 contact->structured_address_type = (char**) realloc(contact->structured_address_type, (entry_nr + 1) * sizeof(char*));
818 contact->structured_address_type[entry_nr] = strdup(gcal_address_type_str[type]);
819 contact->structured_address_nr++;
821 result = entry_nr;
823 return result;
826 int gcal_contact_set_structured_entry(gcal_structured_subvalues_t structured_entry,
827 int structured_entry_nr,
828 int structured_entry_count,
829 const char *field_key,
830 const char *field_value )
832 struct gcal_structured_subvalues *temp_structured_entry;
834 if (!structured_entry || (!field_value) || (!field_key) ||
835 (structured_entry_nr < 0) ||
836 (structured_entry_nr >= structured_entry_count))
837 return -1;
839 if (field_value == NULL)
840 field_value = "";
842 if (structured_entry->field_key == NULL) {
843 structured_entry->field_typenr = structured_entry_nr;
844 structured_entry->field_key = strdup(field_key);
845 structured_entry->field_value = strdup(field_value);
846 structured_entry->next_field = NULL;
847 return 0;
850 for (temp_structured_entry = structured_entry; temp_structured_entry; temp_structured_entry = temp_structured_entry->next_field) {
851 if (!strcmp(temp_structured_entry->field_key,field_key) &&
852 (temp_structured_entry->field_typenr == structured_entry_nr)) {
853 if (temp_structured_entry->field_value != NULL) {
854 free(temp_structured_entry->field_value);
855 temp_structured_entry->field_value = strdup(field_value);
856 return 0;
860 if (temp_structured_entry->next_field == NULL) {
861 temp_structured_entry->next_field = (struct gcal_structured_subvalues *)malloc(sizeof(struct gcal_structured_subvalues));
862 temp_structured_entry = temp_structured_entry->next_field;
864 temp_structured_entry->field_typenr = structured_entry_nr;
865 temp_structured_entry->field_key = strdup(field_key);
866 temp_structured_entry->field_value = strdup(field_value);
867 temp_structured_entry->next_field = NULL;
869 return 0;
872 return -1;
875 int gcal_contact_delete_structured_entry(gcal_structured_subvalues_t structured_entry,
876 int *structured_entry_count,
877 char ***structured_entry_type)
879 int i, result = -1;
880 struct gcal_structured_subvalues *temp_structured_entry;
882 if (!structured_entry)
883 return result;
885 for (temp_structured_entry = structured_entry;
886 temp_structured_entry != NULL;
887 temp_structured_entry = temp_structured_entry->next_field) {
889 if (temp_structured_entry->field_typenr)
890 temp_structured_entry->field_typenr = 0;
891 if (temp_structured_entry->field_key)
892 free(temp_structured_entry->field_key);
893 if (temp_structured_entry->field_value)
894 free(temp_structured_entry->field_value);
897 if (structured_entry_count && structured_entry_type) {
898 if ((*structured_entry_count) > 0) {
899 for (i = 0; i < (*structured_entry_count); i++)
900 if ((*structured_entry_type)[i])
901 free((*structured_entry_type)[i]);
902 free((*structured_entry_type));
905 (*structured_entry_count) = 0;
908 result = 0;
909 return result;
912 int gcal_contact_delete_groupMembership(gcal_contact_t contact)
914 int result = -1;
915 int temp;
917 if (!contact)
918 return result;
920 if (contact->groupMembership_nr > 0) {
921 for (temp = 0; temp < contact->groupMembership_nr; temp++) {
922 if (contact->groupMembership[temp])
923 free(contact->groupMembership[temp]);
926 free(contact->groupMembership);
929 contact->groupMembership_nr = 0;
930 result = 0;
931 return result;
934 int gcal_contact_add_groupMembership(gcal_contact_t contact, char *field)
936 int result = -1;
938 if ((!contact) || (!field))
939 return result;
941 contact->groupMembership = (char**) realloc(contact->groupMembership, (contact->groupMembership_nr+1) * sizeof(char*));
942 contact->groupMembership[contact->groupMembership_nr] = strdup(field);
944 contact->groupMembership_nr++;
946 result = 0;
948 return result;
951 int gcal_contact_set_profission(gcal_contact_t contact, const char *field)
953 int result = -1;
955 if ((!contact) || (!field))
956 return result;
958 if (contact->org_title)
959 free(contact->org_title);
961 contact->org_title = strdup(field);
962 if (contact->org_title)
963 result = 0;
965 return result;
969 int gcal_contact_set_organization(gcal_contact_t contact, const char *field)
971 int result = -1;
973 if ((!contact) || (!field))
974 return result;
976 if (contact->org_name)
977 free(contact->org_name);
979 contact->org_name = strdup(field);
980 if (contact->org_name)
981 result = 0;
983 return result;
986 int gcal_contact_set_occupation(gcal_contact_t contact, const char *field)
988 int result = -1;
990 if ((!contact) || (!field))
991 return result;
993 if (contact->occupation)
994 free(contact->occupation);
996 contact->occupation = strdup(field);
997 if (contact->occupation)
998 result = 0;
1000 return result;
1003 int gcal_contact_set_content(gcal_contact_t contact, const char *field)
1005 int result = -1;
1007 if ((!contact) || (!field))
1008 return result;
1010 if (contact->content)
1011 free(contact->content);
1013 contact->content = strdup(field);
1014 if (contact->content)
1015 result = 0;
1017 return result;
1020 int gcal_contact_set_nickname(gcal_contact_t contact, const char *field)
1022 int result = -1;
1024 if ((!contact) || (!field))
1025 return result;
1027 if (contact->nickname)
1028 free(contact->nickname);
1030 contact->nickname = strdup(field);
1031 if (contact->nickname)
1032 result = 0;
1034 return result;
1037 int gcal_contact_set_photo(gcal_contact_t contact, const char *field,
1038 int length)
1040 int result = -1;
1042 if ((!contact) || (!field))
1043 return result;
1045 if (contact->photo_data)
1046 if (contact->photo_length > 1)
1047 free(contact->photo_data);
1049 if (!(contact->photo_data = malloc(length * sizeof(unsigned char))))
1050 return result;
1052 memcpy(contact->photo_data, field, length);
1053 contact->photo_length = length;
1054 result = 0;
1056 return result;
1059 int gcal_contact_set_birthday(gcal_contact_t contact, const char *field)
1061 int result = -1;
1063 if ((!contact) || (!field))
1064 return result;
1066 if (contact->birthday)
1067 free(contact->birthday);
1069 contact->birthday = strdup(field);
1070 if (contact->birthday)
1071 result = 0;
1073 return result;
1076 int gcal_contact_set_homepage(gcal_contact_t contact, const char *field)
1078 int result = -1;
1080 if ((!contact) || (!field))
1081 return result;
1083 if (contact->homepage)
1084 free(contact->homepage);
1086 contact->homepage = strdup(field);
1087 if (contact->homepage)
1088 result = 0;
1090 return result;
1093 int gcal_contact_set_blog(gcal_contact_t contact, const char *field)
1095 int result = -1;
1097 if ((!contact) || (!field))
1098 return result;
1100 if (contact->blog)
1101 free(contact->blog);
1103 contact->blog = strdup(field);
1104 if (contact->blog)
1105 result = 0;
1107 return result;