Added header guards for C++
[libgcal/libgcal-cdf.git] / src / gcontact.c
blobd6d40f85fec3a798fe87f27d8fdab8639d973192
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(const 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 const 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 const char *gcal_contact_get_xml(gcal_contact_t contact)
318 if ((!contact))
319 return NULL;
320 return gcal_get_xml(&(contact->common));
323 const char *gcal_contact_get_id(gcal_contact_t contact)
325 if ((!contact))
326 return NULL;
327 return gcal_get_id(&(contact->common));
330 const char *gcal_contact_get_updated(gcal_contact_t contact)
332 if ((!contact))
333 return NULL;
334 return gcal_get_updated(&(contact->common));
337 const char *gcal_contact_get_title(gcal_contact_t contact)
339 if ((!contact))
340 return NULL;
341 return gcal_get_title(&(contact->common));
344 const char *gcal_contact_get_url(gcal_contact_t contact)
346 if ((!contact))
347 return NULL;
348 return gcal_get_url(&(contact->common));
351 const 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 const 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 const 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 const char *gcal_contact_get_content(gcal_contact_t contact)
417 if ((!contact))
418 return NULL;
419 return contact->content;
422 const char *gcal_contact_get_nickname(gcal_contact_t contact)
424 if ((!contact))
425 return NULL;
426 return contact->nickname;
429 const char *gcal_contact_get_organization(gcal_contact_t contact)
431 if ((!contact))
432 return NULL;
433 return contact->org_name;
436 const char *gcal_contact_get_profession(gcal_contact_t contact)
438 if ((!contact))
439 return NULL;
440 return contact->org_title;
443 const char *gcal_contact_get_occupation(gcal_contact_t contact)
445 if ((!contact))
446 return NULL;
447 return contact->occupation;
450 const char *gcal_contact_get_homepage(gcal_contact_t contact)
452 if ((!contact))
453 return NULL;
454 return contact->homepage;
457 const 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 const 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 const char *gcal_contact_get_phone(gcal_contact_t contact)
482 if ((!contact))
483 return NULL;
485 const 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 const char *gcal_contact_get_im(gcal_contact_t contact)
508 if ((!contact))
509 return NULL;
510 if (!(contact->im_address))
511 return NULL;
513 const char *res;
514 int pref_im;
515 pref_im = gcal_contact_get_pref_im(contact);
516 if(pref_im == -1)
517 pref_im = 0;
518 res = gcal_contact_get_im_address(contact, pref_im);
519 return res;
522 int gcal_contact_get_im_count(gcal_contact_t contact)
524 if ((!contact))
525 return -1;
526 return contact->im_nr;
529 int gcal_contact_get_pref_im(gcal_contact_t contact)
531 if ((!contact))
532 return -1;
533 return contact->im_pref;
536 const char *gcal_contact_get_im_protocol(gcal_contact_t contact, int i)
538 if ((!contact))
539 return NULL;
540 if (!(contact->im_protocol) || (i >= contact->im_nr))
541 return NULL;
542 return contact->im_protocol[i];
545 const char *gcal_contact_get_im_address(gcal_contact_t contact, int i)
547 if ((!contact))
548 return NULL;
549 if (!(contact->im_address) || (i >= contact->im_nr))
550 return NULL;
551 return contact->im_address[i];
554 gcal_phone_type gcal_contact_get_im_type(gcal_contact_t contact, int i)
556 gcal_im_type result = P_INVALID;
557 int j;
559 if ((!contact))
560 return result;
561 if (!(contact->im_type) || (i >= contact->im_nr))
562 return result;
563 for (j = 0; j < I_ITEMS_COUNT; j++)
564 if (!strcmp(contact->im_type[i], gcal_im_type_str[j]))
565 result = j;
566 return result;
569 gcal_structured_subvalues_t gcal_contact_get_structured_name(gcal_contact_t contact)
571 if ((!contact) || (!contact->structured_name))
572 return NULL;
573 return contact->structured_name;
576 const char *gcal_contact_get_address(gcal_contact_t contact)
578 if ((!contact))
579 return NULL;
580 return contact->post_address;
583 gcal_structured_subvalues_t gcal_contact_get_structured_address(gcal_contact_t contact)
585 if ((!contact) || (!contact->structured_address))
586 return NULL;
587 return contact->structured_address;
590 int gcal_contact_get_structured_address_count(gcal_contact_t contact)
592 if ((!contact))
593 return -1;
594 return contact->structured_address_nr;
597 const int *gcal_contact_get_structured_address_count_obj(gcal_contact_t contact)
599 if ((!contact))
600 return NULL;
601 return &contact->structured_address_nr;
604 const char *gcal_contact_get_structured_entry(gcal_structured_subvalues_t structured_entry,
605 int structured_entry_nr,
606 int structured_entry_count,
607 const char *field_key)
609 struct gcal_structured_subvalues *temp_structured_entry;
611 if(field_key == NULL)
612 field_key = "";
614 if (!structured_entry || (structured_entry_nr >= structured_entry_count))
615 return NULL;
617 for (temp_structured_entry = structured_entry;
618 temp_structured_entry != NULL;
619 temp_structured_entry = temp_structured_entry->next_field) {
621 if (temp_structured_entry->next_field != NULL) {
622 if (!strcmp(temp_structured_entry->field_key, field_key)
623 && (temp_structured_entry->field_typenr == structured_entry_nr)) {
624 return temp_structured_entry->field_value;
629 return NULL;
632 char ***gcal_contact_get_structured_address_type_obj(gcal_contact_t contact)
634 if ((!contact))
635 return NULL;
636 return &contact->structured_address_type;
639 gcal_address_type gcal_contact_get_structured_address_type(gcal_contact_t contact,
640 int structured_entry_nr,
641 int structured_entry_count)
643 gcal_address_type result = A_INVALID;
644 int j;
646 if ((!contact))
647 return result;
649 if (!(contact->structured_address_type) ||
650 (structured_entry_nr >= structured_entry_count))
651 return result;
653 for (j = 0; j < A_ITEMS_COUNT; j++)
654 if (!strcmp(contact->structured_address_type[structured_entry_nr], gcal_address_type_str[j]))
655 result = j;
657 return result;
660 int gcal_contact_get_pref_structured_address(gcal_contact_t contact)
662 if ((!contact))
663 return -1;
664 return contact->structured_address_pref;
667 int gcal_contact_get_groupMembership_count(gcal_contact_t contact)
669 if ((!contact))
670 return -1;
671 return contact->groupMembership_nr;
674 const char *gcal_contact_get_groupMembership(gcal_contact_t contact, int i)
676 if ((!contact))
677 return NULL;
678 if (!(contact->groupMembership) || (i >= contact->groupMembership_nr))
679 return NULL;
680 return contact->groupMembership[i];
683 const char *gcal_contact_get_photo(gcal_contact_t contact)
685 if ((!contact))
686 return NULL;
688 return contact->photo_data;
691 unsigned int gcal_contact_get_photolength(gcal_contact_t contact)
693 if ((!contact))
694 return -1;
696 return contact->photo_length;
699 const char *gcal_contact_get_birthday(gcal_contact_t contact)
701 if ((!contact))
702 return NULL;
703 return contact->birthday;
706 /* Here starts the gcal_contact setters */
707 int gcal_contact_set_title(gcal_contact_t contact, const char *field)
709 int result = -1;
711 if ((!contact) || (!field))
712 return result;
714 if (contact->common.title)
715 free(contact->common.title);
717 contact->common.title = strdup(field);
718 if (contact->common.title)
719 result = 0;
721 return result;
724 int gcal_contact_delete_email_addresses(gcal_contact_t contact)
726 int result = -1;
727 int temp;
729 if (!contact)
730 return result;
732 if (contact->emails_nr > 0) {
733 for (temp = 0; temp < contact->emails_nr; temp++) {
734 if (contact->emails_field[temp])
735 free(contact->emails_field[temp]);
736 if (contact->emails_type[temp])
737 free(contact->emails_type[temp]);
740 free(contact->emails_field);
741 free(contact->emails_type);
744 contact->emails_nr = contact->pref_email = 0;
746 /* XXX: Think, this is obsolete??? */
747 contact->emails_field = contact->emails_type = 0;
749 result = 0;
751 return result;
754 int gcal_contact_add_email_address(gcal_contact_t contact, const char *field,
755 gcal_email_type type, int pref)
757 int result = -1;
759 if ((!contact) || (!field) || (type<0) || (type>=E_ITEMS_COUNT))
760 return result;
762 contact->emails_field = (char**) realloc(contact->emails_field,
763 (contact->emails_nr+1) *
764 sizeof(char*));
766 contact->emails_field[contact->emails_nr] = strdup(field);
768 contact->emails_type = (char**) realloc(contact->emails_type,
769 (contact->emails_nr+1) *
770 sizeof(char*));
772 contact->emails_type[contact->emails_nr] = strdup(gcal_email_type_str[type]);
774 if (pref)
775 contact->pref_email = contact->emails_nr;
777 contact->emails_nr++;
779 result = 0;
781 return result;
784 int gcal_contact_set_email(gcal_contact_t contact, const char *pref_email)
786 int res;
787 res = gcal_contact_add_email_address(contact, pref_email, E_HOME, 1);
788 return res;
791 int gcal_contact_set_url(gcal_contact_t contact, const char *field)
793 int result = -1;
795 if ((!contact) || (!field))
796 return result;
798 if (contact->common.edit_uri)
799 free(contact->common.edit_uri);
801 contact->common.edit_uri = strdup(field);
802 if (contact->common.edit_uri)
803 result = 0;
805 return result;
809 int gcal_contact_set_id(gcal_contact_t contact, const char *field)
811 int result = -1;
813 if ((!contact) || (!field))
814 return result;
816 if (contact->common.id)
817 free(contact->common.id);
819 contact->common.id = strdup(field);
820 if (contact->common.id)
821 result = 0;
823 return result;
827 int gcal_contact_set_etag(gcal_contact_t contact, const char *field)
829 int result = -1;
831 if ((!contact) || (!field))
832 return result;
834 if (contact->common.etag)
835 free(contact->common.etag);
837 contact->common.etag = strdup(field);
838 if (contact->common.etag)
839 result = 0;
841 return result;
844 int gcal_contact_delete_phone_numbers(gcal_contact_t contact)
846 int result = -1;
847 int temp;
849 if (!contact)
850 return result;
852 if (contact->phone_numbers_nr > 0) {
853 for (temp = 0; temp < contact->phone_numbers_nr; temp++) {
854 if (contact->phone_numbers_field[temp])
855 free(contact->phone_numbers_field[temp]);
856 if (contact->phone_numbers_type[temp])
857 free(contact->phone_numbers_type[temp]);
860 free(contact->phone_numbers_field);
861 free(contact->phone_numbers_type);
864 contact->phone_numbers_nr = 0;
866 result = 0;
868 return result;
871 int gcal_contact_add_phone_number(gcal_contact_t contact, const char *field,
872 gcal_phone_type type)
874 int result = -1;
876 if ((!contact) || (!field) || (type<0) || (type>=P_ITEMS_COUNT))
877 return result;
879 contact->phone_numbers_field = (char**) realloc(contact->phone_numbers_field, (contact->phone_numbers_nr+1) * sizeof(char*));
880 contact->phone_numbers_field[contact->phone_numbers_nr] = strdup(field);
882 contact->phone_numbers_type = (char**) realloc(contact->phone_numbers_type, (contact->phone_numbers_nr+1) * sizeof(char*));
883 contact->phone_numbers_type[contact->phone_numbers_nr] = strdup(gcal_phone_type_str[type]);
885 contact->phone_numbers_nr++;
887 result = 0;
889 return result;
892 int gcal_contact_set_phone(gcal_contact_t contact, const char *phone)
894 int res;
895 res = gcal_contact_delete_phone_numbers(contact);
896 if (res)
897 return res;
899 res = gcal_contact_add_phone_number(contact, phone, P_MOBILE);
900 return res;
903 int gcal_contact_delete_im(gcal_contact_t contact)
905 int result = -1;
906 int temp;
908 if (!contact)
909 return result;
911 if (contact->im_nr > 0) {
912 for (temp = 0; temp < contact->im_nr; temp++) {
913 if (contact->im_protocol[temp])
914 free(contact->im_protocol[temp]);
915 if (contact->im_address[temp])
916 free(contact->im_address[temp]);
917 if (contact->im_type[temp])
918 free(contact->im_type[temp]);
920 free(contact->im_protocol);
921 free(contact->im_address);
922 free(contact->im_type);
925 contact->im_nr = contact->im_pref = 0;
927 result = 0;
929 return result;
932 int gcal_contact_add_im(gcal_contact_t contact, const char *protcol,
933 const char *address, gcal_im_type type, int pref)
935 int result = -1;
937 if ((!contact) || (!protcol) || (!address) || (type<0) || (type>=I_ITEMS_COUNT))
938 return result;
940 contact->im_protocol = (char**) realloc(contact->im_protocol, (contact->im_nr+1) * sizeof(char*));
941 contact->im_protocol[contact->im_nr] = strdup(protcol);
943 contact->im_address = (char**) realloc(contact->im_address, (contact->im_nr+1) * sizeof(char*));
944 contact->im_address[contact->im_nr] = strdup(address);
946 contact->im_type = (char**) realloc(contact->im_type, (contact->im_nr+1) * sizeof(char*));
947 contact->im_type[contact->im_nr] = strdup(gcal_im_type_str[type]);
949 if (pref)
950 contact->im_pref = contact->im_nr;
952 contact->im_nr++;
954 result = 0;
956 return result;
959 int gcal_contact_set_address(gcal_contact_t contact, const char *field)
961 int result = -1;
963 if ((!contact) || (!field))
964 return result;
966 if (contact->post_address)
967 free(contact->post_address);
969 contact->post_address = strdup(field);
970 if (contact->post_address)
971 result = 0;
973 return result;
976 int gcal_contact_set_structured_address_nr(gcal_contact_t contact,
977 gcal_address_type type)
979 int entry_nr, result = -1;
981 if (!contact || (type < 0) || (type >= A_ITEMS_COUNT))
982 return result;
984 entry_nr = contact->structured_address_nr;
985 contact->structured_address_type = (char**) realloc(contact->structured_address_type, (entry_nr + 1) * sizeof(char*));
986 contact->structured_address_type[entry_nr] = strdup(gcal_address_type_str[type]);
987 contact->structured_address_nr++;
989 result = entry_nr;
991 return result;
994 int gcal_contact_set_pref_structured_address(gcal_contact_t contact, int pref_address)
996 int result = -1;
998 if ((!contact) || (pref_address < 0))
999 return result;
1001 contact->structured_address_pref = pref_address;
1003 result = 0;
1005 return result;
1008 int gcal_contact_set_structured_entry(gcal_structured_subvalues_t structured_entry,
1009 int structured_entry_nr,
1010 int structured_entry_count,
1011 const char *field_key,
1012 const char *field_value )
1014 struct gcal_structured_subvalues *temp_structured_entry;
1016 if (!structured_entry || (!field_value) || (!field_key) ||
1017 (structured_entry_nr < 0) ||
1018 (structured_entry_nr >= structured_entry_count))
1019 return -1;
1021 if (field_value == NULL)
1022 field_value = "";
1024 if (structured_entry->field_key == NULL) {
1025 structured_entry->field_typenr = structured_entry_nr;
1026 structured_entry->field_key = strdup(field_key);
1027 structured_entry->field_value = strdup(field_value);
1028 structured_entry->next_field = NULL;
1029 return 0;
1032 for (temp_structured_entry = structured_entry; temp_structured_entry; temp_structured_entry = temp_structured_entry->next_field) {
1033 if (!strcmp(temp_structured_entry->field_key,field_key) &&
1034 (temp_structured_entry->field_typenr == structured_entry_nr)) {
1035 if (temp_structured_entry->field_value != NULL) {
1036 free(temp_structured_entry->field_value);
1037 temp_structured_entry->field_value = strdup(field_value);
1038 return 0;
1042 if (temp_structured_entry->next_field == NULL) {
1043 temp_structured_entry->next_field = (struct gcal_structured_subvalues *)malloc(sizeof(struct gcal_structured_subvalues));
1044 temp_structured_entry = temp_structured_entry->next_field;
1046 temp_structured_entry->field_typenr = structured_entry_nr;
1047 temp_structured_entry->field_key = strdup(field_key);
1048 temp_structured_entry->field_value = strdup(field_value);
1049 temp_structured_entry->next_field = NULL;
1051 return 0;
1054 return -1;
1057 int gcal_contact_delete_structured_entry(gcal_structured_subvalues_t structured_entry,
1058 int *structured_entry_count,
1059 char ***structured_entry_type)
1061 int i, result = -1;
1062 struct gcal_structured_subvalues *temp_structured_entry;
1064 if (!structured_entry)
1065 return result;
1067 for (temp_structured_entry = structured_entry;
1068 temp_structured_entry != NULL;
1069 temp_structured_entry = temp_structured_entry->next_field) {
1070 if (temp_structured_entry->field_typenr)
1071 temp_structured_entry->field_typenr = 0;
1072 if (temp_structured_entry->field_key)
1073 free(temp_structured_entry->field_key);
1074 if (temp_structured_entry->field_value)
1075 free(temp_structured_entry->field_value);
1078 if (structured_entry_count && structured_entry_type) {
1079 if ((*structured_entry_count) > 0) {
1080 for (i = 0; i < (*structured_entry_count); i++)
1081 if ((*structured_entry_type)[i])
1082 free((*structured_entry_type)[i]);
1083 free((*structured_entry_type));
1086 (*structured_entry_count) = 0;
1089 result = 0;
1090 return result;
1093 int gcal_contact_delete_groupMembership(gcal_contact_t contact)
1095 int result = -1;
1096 int temp;
1098 if (!contact)
1099 return result;
1101 if (contact->groupMembership_nr > 0) {
1102 for (temp = 0; temp < contact->groupMembership_nr; temp++) {
1103 if (contact->groupMembership[temp])
1104 free(contact->groupMembership[temp]);
1107 free(contact->groupMembership);
1110 contact->groupMembership_nr = 0;
1111 result = 0;
1112 return result;
1115 int gcal_contact_add_groupMembership(gcal_contact_t contact, const char *field)
1117 int result = -1;
1119 if ((!contact) || (!field))
1120 return result;
1122 contact->groupMembership = (char**) realloc(contact->groupMembership, (contact->groupMembership_nr+1) * sizeof(char*));
1123 contact->groupMembership[contact->groupMembership_nr] = strdup(field);
1125 contact->groupMembership_nr++;
1127 result = 0;
1129 return result;
1132 int gcal_contact_set_profession(gcal_contact_t contact, const char *field)
1134 int result = -1;
1136 if ((!contact) || (!field))
1137 return result;
1139 if (contact->org_title)
1140 free(contact->org_title);
1142 contact->org_title = strdup(field);
1143 if (contact->org_title)
1144 result = 0;
1146 return result;
1150 int gcal_contact_set_organization(gcal_contact_t contact, const char *field)
1152 int result = -1;
1154 if ((!contact) || (!field))
1155 return result;
1157 if (contact->org_name)
1158 free(contact->org_name);
1160 contact->org_name = strdup(field);
1161 if (contact->org_name)
1162 result = 0;
1164 return result;
1167 int gcal_contact_set_occupation(gcal_contact_t contact, const char *field)
1169 int result = -1;
1171 if ((!contact) || (!field))
1172 return result;
1174 if (contact->occupation)
1175 free(contact->occupation);
1177 contact->occupation = strdup(field);
1178 if (contact->occupation)
1179 result = 0;
1181 return result;
1184 int gcal_contact_set_content(gcal_contact_t contact, const char *field)
1186 int result = -1;
1188 if ((!contact) || (!field))
1189 return result;
1191 if (contact->content)
1192 free(contact->content);
1194 contact->content = strdup(field);
1195 if (contact->content)
1196 result = 0;
1198 return result;
1201 int gcal_contact_set_nickname(gcal_contact_t contact, const char *field)
1203 int result = -1;
1205 if ((!contact) || (!field))
1206 return result;
1208 if (contact->nickname)
1209 free(contact->nickname);
1211 contact->nickname = strdup(field);
1212 if (contact->nickname)
1213 result = 0;
1215 return result;
1218 int gcal_contact_set_photo(gcal_contact_t contact, const char *field,
1219 int length)
1221 int result = -1;
1223 if ((!contact) || (!field))
1224 return result;
1226 if (contact->photo_data)
1227 if (contact->photo_length > 1)
1228 free(contact->photo_data);
1230 if (!(contact->photo_data = malloc(length * sizeof(unsigned char))))
1231 return result;
1233 memcpy(contact->photo_data, field, length);
1234 contact->photo_length = length;
1235 result = 0;
1237 return result;
1240 int gcal_contact_set_birthday(gcal_contact_t contact, const char *field)
1242 int result = -1;
1244 if ((!contact) || (!field))
1245 return result;
1247 if (contact->birthday)
1248 free(contact->birthday);
1250 contact->birthday = strdup(field);
1251 if (contact->birthday)
1252 result = 0;
1254 return result;
1257 int gcal_contact_set_homepage(gcal_contact_t contact, const char *field)
1259 int result = -1;
1261 if ((!contact) || (!field))
1262 return result;
1264 if (contact->homepage)
1265 free(contact->homepage);
1267 contact->homepage = strdup(field);
1268 if (contact->homepage)
1269 result = 0;
1271 return result;
1274 int gcal_contact_set_blog(gcal_contact_t contact, const char *field)
1276 int result = -1;
1278 if ((!contact) || (!field))
1279 return result;
1281 if (contact->blog)
1282 free(contact->blog);
1284 contact->blog = strdup(field);
1285 if (contact->blog)
1286 result = 0;
1288 return result;