Imported Upstream version 0.48
[platform/upstream/libical.git] / examples / access_store.c
1
2
3 void acess_cap(void) {
4     
5     /* Note, all routines that are prefixed with "caller_" are
6        implemented by the caller of libical */
7     
8 /* 1 Open a connection to a store.  */
9
10     /* The caller is responsible for getting a socket to the server
11        and negotiating the first stages of the CAP exchange. These can
12        be fairly complex and varied for different operating systems,
13        local vs remote usage, and for different authentication
14        schemes, so the API does not try to simplify them.  */
15
16     int sock = caller_create_socket_to_server();
17     icalcstp *cstp = icalcstp_new(0,sock,sock);
18     
19     caller_authenticate(cstp);
20     
21     icalcsdb *csdb = icalcsdb_new(cstp);
22
23 /* 2 Create a new calendar for which user Bill can read and user Mary can
24 read and write. See CAP draft 7.2.1.1.1. for the text of this example*/
25
26     /* This case requires setting up a TARGET, multiple OWNERs and
27        multiple VCARs, so it creates a component and uses CSTP that
28        than the CSDB interface.
29
30     icalcomponent *create = icalcaputil_new_create();
31
32     icalcomponent_add_property(create,
33                                icalproperty_new_target(
34                                    strdup("cap://cal.example.com/relcal8")
35                                    ));
36
37     icalcomponent *cal = 
38         icalcomponent_vanew_vcalendar(
39             icalproperty_new_relcalid(strdup("relcalid")),
40             icalproperty_new_name(strdup("Bill & Mary's cal")),
41             icalproperty_new_owner(strdup("bill")),
42             icalproperty_new_owner(strdup("mary")),
43             icalproperty_new_calmaster(strdup("mailto:bill@example.com")),
44             icalcomponent_vanew_vcar(
45                 icalproperty_new_grant(strdup("UPN=bill;ACTION=*;OBJECT=*")),
46                 icalproperty_new_grant(strdup("UPN=bill;ACTION=*;OBJECT=*"))
47                 0)
48             0);
49
50     error = icalcomponent_add_component(create,cal);
51
52     /* Send the data */
53     error = icalcstp_senddata(cstp,10,create);
54
55
56     /* Get the response */
57     icalcstp_response response = icalcstp_get_first_response(cstp);
58
59     /* Do something with the response*/
60
61     if(icalenum_reqstat_major(response.code) != 2){
62         /* do something with the error */
63     }
64
65     icalcomponent_free(create);
66     
67
68 /* 3 Create several new calendars */
69
70     /* Same as #2, but insert more TARGET properties and read more responses*/
71
72 /* 4 Delete a calendar */
73
74     error = icalcsdb_delete(csdb,"uid12345-example.com");
75
76 /* 5 Change the calid of a calendar */
77
78     erorr = icalcsdb_move(csdb,"uid12345-old-example.com",
79                           "uid12345-new-example.com");
80
81
82 /* 6 Delete all calendars belonging to user bob */
83
84     icalproperty *p;
85     /* First expand bob's UPN into a set of CALIDs */
86     icalcomponent *calids = icalcsdb_expand_upn("bob@example.com");
87
88     /* Then, create a message to delete all of them */
89     icalcomponent *delete = icalcaputil_new_create();
90
91     
92     for(p = icalcomponent_get_first_property(calids,ICAL_CALID_PROPERTY);
93         p != 0;
94         p = icalcomponent_get_next_property(calids,ICAL_CALID_PROPERTY)){
95
96         char* = icalproperty_get_calid(p);
97
98         icalcomponent_add_target(delete,p);
99
100     }
101
102     /* Send the message */
103
104     error = icalcstp_senddata(cstp,10,delete);
105
106     /* Finally, read the responses */
107
108     for(response = icalcstp_get_first_response(cstp);
109         response.code !=  ICAL_UNKNOWN_STATUS;
110         response = icalcstp_get_next_response(cstp)){
111         
112         if(icalenum_reqstat_major(response.code) != 2){
113             /* do something with the error */
114         }
115     }
116         
117
118 /* 7 Get three new UIDs from the store */
119
120     /* libical owns the returned memory. Copy before using */
121     char* uid1 = icalcsdb_generateuid(csdb);
122     char* uid2 = icalcsdb_generateuid(csdb);
123     char* uid3 = icalcsdb_generateuid(csdb);
124
125 /* 8 Store a new VEVENT in the store.  */
126     
127     /* Very similar to case #2 */
128
129 /* 9 Find all components for which the LOCATION is "West Conference
130 Room" and change them to "East Conference Room"  */
131
132     icalcomponent *modify = icalcaputil_new_modify();
133
134     icalcaputil_modify_add_old_prop(modify,
135                                     icalproperty_new_location(
136                                         strdup("West Conference Room")));
137
138     icalcaputil_modify_add_new_prop(modify,
139                                     icalproperty_new_location(
140                                         strdup("East Conference Room")));
141
142     icalcaputil_add_target(modify,"relcal2");
143
144     /* Send the component */
145     error = icalcstp_senddata(cstp,10,delete);
146
147    /* Get the response */
148     icalcstp_response response = icalcstp_get_first_response(cstp);
149
150     /* Do something with the response*/
151
152     if(icalenum_reqstat_major(response.code) != 2){
153         /* do something with the error */
154     }
155
156     icalcomponent_free(modify);
157     
158 /* 10 Find the component with UID X and add a GEO property to it.  */
159
160
161     icalcomponent *modify = icalcaputil_new_modify();
162
163     icalcaputil_modify_add_query(modify,
164                                  "SELECT UID FROM VEVENT WHERE UID = 'X'");   
165
166     icalcaputil_modify_add_new_prop(modify,
167                                     icalproperty_new_geo(
168                                         strdup("-117;32")));
169
170     icalcaputil_add_target(modify,"relcal2");
171
172     /* Send the component */
173     error = icalcstp_senddata(cstp,10,delete);
174
175    /* Get the response */
176     icalcstp_response response = icalcstp_get_first_response(cstp);
177
178     /* Do something with the response*/
179
180     if(icalenum_reqstat_major(response.code) != 2){
181         /* do something with the error */
182     }
183
184     icalcomponent_free(modify);
185     
186
187 /* 11 Delete all VEVENTS which have a METHOD that is not CREATED */
188
189
190 /* 12 Retrieve all VEVENTS which have a METHOD that is not CREATED */
191
192     /* Nearly the same at #11 */
193
194 /* 13 Retrieve the capabilities of the store */
195
196 /* 14 Retrieve/Modify/Add/Delete properties of a store */
197
198 /* 15 Retrieve/Modify/Add/Delete VCARs of a store */
199
200 /* 16 Retrieve/Modify/Add/Delete VTIMEZONEs of a store */
201
202 /* 17 Retrieve/Modify/Add/Delete properties of a calendar */
203
204 /* 18 Retrieve/Modify/Add/Delete VCARs of a calendar */
205
206 /* 19 Retrieve/Modify/Add/Delete VTIMEZONEs of a calendar */
207
208 /* 20 Translate a CALID into one or more UPNs */
209
210 /* 21 Expand a group UPN into all of the members of the group */