2 Copyright (c) 2008 Instituto Nokia de Tecnologia
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.
31 * @author Adenilson Cavalcanti da Silva <adenilson.silva@indt.org.br>
32 * @date Mon Mar 3 20:15:09 2008
34 * @brief libgcal header file.
36 * This defines the public functions.
45 /** Set gcal Google service mode */
46 typedef enum { GCALENDAR
, GCONTACT
} gservice
;
48 /** Flag to control if deleted entries will be retrieved or not
49 * (for while, google only implements that for 'contacts').
50 * Calendar deleted entries will *always* be returned with
51 * 'eventStatus' set to 'cancelled'.
54 typedef enum { HIDE
, SHOW
} display_deleted_entries
;
56 /** Upload (being POST or PUT) definition option.
58 * Its used to set behavior in \ref up_entry function.
61 /** Code for HTTP POST. */
63 /** Code for HTTP PUT */
66 /** Really weird timestamp from RFC 3339 is
67 * 1937-01-01T12:00:27.87+00:20
68 * so 30 bytes is enough to have milisecond precision
71 static const size_t TIMESTAMP_MAX_SIZE
= 30;
73 /** Minimal timestamp size is
74 * 1937-01-01T12:00:27.87Z
76 static const size_t TIMESTAMP_SIZE
= 23;
79 /** Library structure. It holds resources (curl, buffer, etc).
83 /** Library structure, represents each calendar event entry.
87 /** Library structure, represents common data for a google entry (being
88 * calendar or contact). It has: id, updated, title, edit_uri.
92 /** An opaque type for gcal object.
94 typedef struct gcal_resource
* gcal_t
;
97 /** Library structure destructor (use it free its internal resources properly).
99 void gcal_destroy(struct gcal_resource
*gcal_obj
);
101 /** Internal use function, cleans up the internal buffer.
103 * \todo move it to a distinct internal module.
105 * @param gcal_obj Library resource structure pointer.
107 void clean_buffer(struct gcal_resource
*gcal_obj
);
110 /** Library structure constructor, the user can only have pointers to the
111 * library \ref gcal_resource structure.
113 * Concerning google service type, it defaults to google calendar. You can
114 * change it using \ref gcal_set_service.
116 * @param mode Service that libgcal will handle (for while, GCALENDAR
119 * @return A pointer to a newly created object or NULL.
121 struct gcal_resource
*gcal_construct(gservice mode
);
123 /** Sets the google service that user wants to authenticate.
125 * For while, only calendar (cl) and contacts (cp) are supported.
127 * @param gcalobj Pointer to a library resource structure \ref gcal_resource.
129 * @param mode Service type, see \ref gservice.
131 * @return 0 for success, -1 otherwise.
133 int gcal_set_service(struct gcal_resource
*gcalobj
, gservice mode
);
136 /** Gets from google an authentication token, using the 'ClientLogin' service.
138 * Use it before getting/setting google calendar events. If you are behind of
139 * a network proxy, its import to use \ref gcal_set_proxy *before* to set
140 * the proxy (or all network connections will hang and timeout).
142 * @param gcalobj Pointer to a library resource structure \ref gcal_resource
144 * @param user The user google login account.
146 * @param password User password in plain text
147 * \todo think in a way to encrypt password
149 * @return Returns 0 on success, -1 otherwise.
151 int gcal_get_authentication(struct gcal_resource
*gcalobj
,
152 char *user
, char *password
);
155 /** Dumps events from default calendar to internal buffer.
156 * \todo Let the library user select which calendar he/she wants
157 * to get the events. See \ref gcal_calendar_list.
159 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
160 * previously got the authentication using
161 * \ref gcal_get_authentication.
163 * @param gdata_version Version of Data API.
165 * @return Returns 0 on success, -1 otherwise.
167 int gcal_dump(struct gcal_resource
*gcalobj
, const char *gdata_version
);
169 /** Get a list of users calendars (gcalendar supports multiple calendars
170 * besides the default calendar).
172 * I think it would be a good idea to let the library user decide which
173 * calendar to get the events. See too \ref gcal_dump.
175 * \todo Parse the Atom feed and provide easy access to the calendar lists.
177 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
178 * previously got the authentication using
179 * \ref gcal_get_authentication.
181 * @return Returns 0 on success, -1 otherwise.
183 int gcal_calendar_list(struct gcal_resource
*gcalobj
);
186 /** Return the number of event entries a calendar has (you should
187 * had got the atom stream before, using \ref gcal_dump).
189 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
190 * previously got the authentication using
191 * \ref gcal_get_authentication.
193 * @return -1 on error, any number >= 0 otherwise.
195 int gcal_entry_number(struct gcal_resource
*gcalobj
);
198 /** Extracts from the atom stream the calendar event entries (you should
199 * had got the atom stream before, using \ref gcal_dump).
201 * Pay attention that it returns a vector of structures that must be destroyed
202 * using \ref gcal_destroy_entries.
204 * Since atom XML feeds can get huge, as soon the function creates entries
205 * vector and copies the data from the internal \ref gcal_resource buffer,
206 * it will free its internal buffer to save memory.
209 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
210 * previously got the authentication using
211 * \ref gcal_get_authentication.
213 * @param length Pointer to an unsigned int, it will have the vector length.
215 * @return A pointer on sucess, NULL otherwise.
217 struct gcal_event
*gcal_get_entries(struct gcal_resource
*gcalobj
,
221 /** Always use this to set calendar event structure to a sane state.
225 * @param entry A pointer to a \ref gcal_event.
227 void gcal_init_event(struct gcal_event
*entry
);
230 /** Cleanup memory of 1 entry structure pointer.
233 * @param entry A pointer to a \ref gcal_event.
235 void gcal_destroy_entry(struct gcal_event
*entry
);
238 /** Generic HTTP GET function.
240 * Used when getting entries in a feed (or other data that requires
241 * an authenticated GET).
243 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
244 * previously got the authentication using
245 * \ref gcal_get_authentication.
247 * @param url URL string.
249 * @param cb_download Callback used for writing downloaded data. Must
250 * be of type: size_t()(void *, size_t, size_t, void*)
252 * @param gdata_version Version of Data API.
254 * @return 0 for success, -1 for error.
256 int get_follow_redirection(struct gcal_resource
*gcalobj
, const char *url
,
257 void *cb_download
, const char *gdata_version
);
260 /** Cleanup the memory of a vector of calendar entries created using
261 * \ref gcal_get_entries.
263 * @param entries A pointer to a vector of \ref gcal_event structure.
265 * @param length The vector length.
267 void gcal_destroy_entries(struct gcal_event
*entries
, size_t length
);
269 /** Posts data to a server URL and checks its result.
271 * I'm not sure if this one should be here, since it is a more an internal
272 * function (but I need to share it with 'contacts' too).
274 * \todo move it to a distinct internal module.
275 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
276 * previously got the authentication using
277 * \ref gcal_get_authentication.
279 * @param url The server URL.
280 * @param header First header part.
281 * @param header2 Second header part (can be NULL).
282 * @param header3 Third header part (can be NULL).
283 * @param header4 Third header part (can be NULL). Used for ETag (Google Data
285 * @param post_data The data to post (can be NULL).
286 * @param length The data length (can be zero).
287 * @param expected_answer The expected answer code, see GCAL_DEFAULT_ANSWER and
290 * @return -1 on error, 0 on success.
292 int http_post(struct gcal_resource
*gcalobj
, const char *url
,
293 char *header
, char *header2
, char *header3
,
295 char *post_data
, unsigned int length
,
296 const int expected_answer
,
297 const char *gdata_version
);
300 /** Uploads an entry (calendar or contact) to a server URL
302 * Used by \ref gcal_create_event, \ref gcal_create_contact,
303 * \ref gcal_edit_event.
306 * @param data2post A pointer to string, it will be the body to be posted.
308 * @param length The data length (can be zero).
310 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
311 * previously got the authentication using
312 * \ref gcal_get_authentication.
314 * @param url_server The URL of server (one for calendar and other for
317 * @param etag Google Data API 2.0 requires an Etag in HTTP header for some
320 * @param up_mode If the upload of data will be using PUT or POST (internally
321 * it uses 'http_put' and \ref 'http_post').
323 * @param content_type The content type, to use default (application/atom+xml)
326 * @param expected_code The expected return code from server (200, 201, etc.)
327 * See GCAL_DEFAULT_ANSWER and friends.
329 * @return -1 on error, 0 on success.
331 int up_entry(char *data2post
, unsigned int m_length
,
332 struct gcal_resource
*gcalobj
,
333 const char *url_server
, char *etag
,
334 HTTP_CMD up_mode
, char *content_type
,
338 /** Creates an new calendar event.
340 * You need to first succeed to get an authorization token using
341 * \ref gcal_get_authentication.
343 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
344 * previously got the authentication using
345 * \ref gcal_get_authentication.
347 * @param entries A pointer to an calendar entry event (see \ref gcal_event).
349 * @param updated Pass a pointer to a \ref gcal_event structure if you
350 * wish to access the newly created contact (i.e. access fields like
351 * edit_uri and id). If you don't need it, just pass NULL.
353 * @return -1 on error, 0 on success, -2 if operation went correctly but
354 * cannot return 'updated' entry.
356 int gcal_create_event(struct gcal_resource
*gcalobj
,
357 struct gcal_event
*entries
,
358 struct gcal_event
*updated
);
360 /** Deletes a calendar event.
362 * You need to first succeed to get an authorization token using
363 * \ref gcal_get_authentication.
365 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
366 * previously got the authentication using
367 * \ref gcal_get_authentication.
369 * @param entry A pointer to one calendar entry event (see \ref gcal_event).
371 * @return -1 on error, 0 on success.
373 int gcal_delete_event(struct gcal_resource
*gcalobj
,
374 struct gcal_event
*entry
);
377 /** Edits a calendar event.
379 * It requires the presence of field 'edit_uri' in entry pointer structure
380 * (see \ref gcal_event).
383 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
384 * previously got the authentication using
385 * \ref gcal_get_authentication.
387 * @param entry A pointer to an calendar entry event.
389 * @param updated Pass a pointer to a \ref gcal_event structure if you
390 * wish to access the newly created contact (i.e. access fields like
391 * edit_uri and id). If you don't need it, just pass NULL.
393 * @return -1 on error, 0 on success, -2 if operation went correctly but
394 * cannot return 'updated' entry.
396 int gcal_edit_event(struct gcal_resource
*gcalobj
,
397 struct gcal_event
*entry
,
398 struct gcal_event
*updated
);
402 /** Provides access to internal buffer (e.g. to access the raw Atom stream).
404 * Pay attention that you *must not* mess with its memory, since when deleting
405 * the gcal_resource pointer with \ref gcal_destroy the memory pointed will
408 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
409 * previously got the authentication using
410 * \ref gcal_get_authentication.
412 * @return A pointer to internal gcal_resource buffer.
414 char *gcal_access_buffer(struct gcal_resource
*gcalobj
);
416 /** Function to get the current timestamp (RFC3339) with milisecond
419 * @param timestamp A pointer to a buffer to where to write the timestamp.
421 * @param length The buffer length.
423 * @param atimezone A timezone string in format: +/-hh:mm:ss. It will be
424 * appended to the output timestamp.
426 * @return 0 for success, -1 for failure.
428 int get_mili_timestamp(char *timestamp
, size_t length
, char *atimezone
);
431 /** Returns all entries (being calendar or contacts) that are newer
434 * Use it to get updated entries.
436 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
437 * previously got the authentication using
438 * \ref gcal_get_authentication.
440 * @param timestamp A timestamp in RFC 3339 format yyyy-mm-ddThh:mm:ss
441 *(if NULL, this function will use current day, with time set to 06:00AM).
443 * @param gdata_version Version of Data API.
445 * @return -1 on error, 0 on success.
447 int gcal_query_updated(struct gcal_resource
*gcalobj
, char *timestamp
,
448 const char *gdata_version
);
450 /** Set a timezone, following the RFC 3339 format +/-hh:mm.
452 * The structure copy the string with the timezone.
454 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
455 * previously got the authentication using
456 * \ref gcal_get_authentication.
458 * @param atimezone A pointer to string with the timezone (e.g. for Helsinki
459 * which is +2GMT it would be "+02:00").
461 * @return -1 on error, 0 on success.
463 int gcal_set_timezone(struct gcal_resource
*gcalobj
, char *atimezone
);
465 /** Sets gcal XML store mode.
467 * Use it if you wish to store the RAW google XML entry data inside
470 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
471 * previously got the authentication using
472 * \ref gcal_get_authentication.
474 * @param flag 0 for not store, 1 to activate store mode.
476 void gcal_set_store_xml(struct gcal_resource
*gcalobj
, char flag
);
478 /** Sets network proxy.
480 * Use it if you are behind a network proxy and can't directly access
483 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
484 * previously got the authentication using
485 * \ref gcal_get_authentication.
487 * @param proxy A null terminated string, must have the hostname (or IP)
488 * and can have the user+password too (e.g. "user+password@10.10.1.1:8080" or
489 * simply "http://hostname:port". Remark: I only tested it with
490 * a proxy that doesn't require authentication.
493 void gcal_set_proxy(struct gcal_resource
*gcalobj
, char *proxy
);
495 /** Define the location that results should be returned for queries.
497 * Use it to set your current location, otherwise the configured city for
498 * the user account is used.
499 * The structure copy the string with the timezone.
501 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
502 * previously got the authentication using
503 * \ref gcal_get_authentication.
505 * @param location A pointer to string with the location (e.g. "America/Manaus",
506 * "Europe/Helsinki", "America/Los_Angeles'). It must not has empty spaces on
509 * @return -1 on error, 0 on success.
511 int gcal_set_location(struct gcal_resource
*gcalobj
, char *location
);
513 /** Use this to set if deleted entries should be returned or not. Pay attention
514 * that this is implemented only for google contacts (google calendar entries
515 * doesn't have this query parameter).
517 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
518 * previously got the authentication using
519 * \ref gcal_get_authentication.
521 * @param opt Option parameter, enable (SHOW) or not (HIDE) retrieving of
522 * deleted entries (see \ref display_deleted_entries).
524 void gcal_deleted(struct gcal_resource
*gcalobj
, display_deleted_entries opt
);
527 /** Generic query function, use it to do a query to google services.
529 * You can pass a query with multiple parameters, but in just one string.
531 * ATTENTION: querying by name will not work, since its not implemented in
532 * google contacts. It will give HTTP 403 error.
534 * @param gcalobj Pointer to a \ref gcal_resource structure, which has
535 * previously got the authentication using
536 * \ref gcal_get_authentication.
538 * @param parameters A string with the query e.g. for calendar
539 * gcal_query(gcal, "ctz=America/Los_Angeles&author=Joe");
542 * gcal_query(gcal, "updated-min=2008-06-20T06:00:00Z&"
543 * "alt=atom&max-results=500&showdeleted=true");
545 * @param gdata_version Version of Data API.
547 * @return -1 on error, 0 on success.
549 int gcal_query(struct gcal_resource
*gcalobj
, const char *parameters
,
550 const char *gdata_version
);
553 /* Common fields between calendar and contacts are
554 * of type 'gcal_entry'
557 /** Access to entry ID.
559 * Each google entry (event/contact) has a unique ID, you can access
560 * it using this function.
562 * @param entry A data entry pointer, see \ref gcal_entry.
564 * @return A pointer to internal field (*dont* try to free it!).
566 char *gcal_get_id(struct gcal_entry
*entry
);
568 /** Access to last updated timestamp of an entry.
570 * When an operation (being add/edit/delete) is done in an entry, the timestamp
571 * of it is recorded). You can access it using this function.
573 * @param entry A data entry pointer, see \ref gcal_entry.
575 * @return A pointer to internal field (*dont* try to free it!).
577 char *gcal_get_updated(struct gcal_entry
*entry
);
579 /** Access entry title.
581 * Each entry has a title, being contacts (the contact name) or event
584 * @param entry A data entry pointer, see \ref gcal_entry.
586 * @return A pointer to internal field (*dont* try to free it!).
588 char *gcal_get_title(struct gcal_entry
*entry
);
592 * Each entry has an edit url (which is the combination of ID + cookie).
593 * This edit url is required to do operations (edit/delete) and changes
594 * each time an operation is done.
596 * @param entry A data entry pointer, see \ref gcal_entry.
598 * @return A pointer to internal field (*dont* try to free it!).
600 char *gcal_get_url(struct gcal_entry
*entry
);
604 * Each entry has an ETag (Google Data API 2.0).
605 * This etag is required to do operations (edit/delete) and changes
606 * each time an operation is done.
608 * @param entry A data entry pointer, see \ref gcal_entry.
610 * @return A pointer to internal field (*dont* try to free it!).
612 char *gcal_get_etag(struct gcal_entry
*entry
);
616 * Even if some fields are parsed from atom stream and directly accessable
617 * from entry objects, its possible to access the RAW representation of the
618 * whole entry using this function.
620 * @param entry A data entry pointer, see \ref gcal_entry.
622 * @return A pointer to internal field (*dont* try to free it!).
624 char *gcal_get_xml(struct gcal_entry
*entry
);
626 /** Entry status (deleted: 1, not: 0).
628 * When an entry is deleted, there is a way to known it, being specific for
629 * the entry type. Events have the gd:eventStatus equal to
630 * "blah#event.cancelled".
632 * Contacts will have a new field (gd:deleted).
634 * @param entry A data entry pointer, see \ref gcal_entry.
636 * @return 1 for deleted, 0 for not deleted, -1 for error case (f the event
637 * object is invalid).
639 char gcal_get_deleted(struct gcal_entry
*entry
);