From 460c52653ab0dcca6f19a4f492ed2c5e4e963ab0 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdepim@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- libkcal/libical/examples/Makefile.am | 16 ++ libkcal/libical/examples/access_components.c | 318 +++++++++++++++++++++ .../examples/access_properties_and_parameters.c | 144 ++++++++++ libkcal/libical/examples/errors.c | 70 +++++ libkcal/libical/examples/main.c | 12 + libkcal/libical/examples/parse_text.c | 68 +++++ 6 files changed, 628 insertions(+) create mode 100644 libkcal/libical/examples/Makefile.am create mode 100644 libkcal/libical/examples/access_components.c create mode 100644 libkcal/libical/examples/access_properties_and_parameters.c create mode 100644 libkcal/libical/examples/errors.c create mode 100644 libkcal/libical/examples/main.c create mode 100644 libkcal/libical/examples/parse_text.c (limited to 'libkcal/libical/examples') diff --git a/libkcal/libical/examples/Makefile.am b/libkcal/libical/examples/Makefile.am new file mode 100644 index 000000000..95076dbaf --- /dev/null +++ b/libkcal/libical/examples/Makefile.am @@ -0,0 +1,16 @@ + +noinst_PROGRAMS = doesnothing + +doesnothing_LDADD = ../src/libical/libical.la ../src/libicalss/libicalss.la ../src/libicalvcal/libicalvcal.la + +#LIBS = @PTHREAD_LIBS@ + +INCLUDES = -I. -I../src/libical -I../src/libicalss -I../src/libicalvcal + +doesnothing_SOURCES = \ + access_components.c \ + access_properties_and_parameters.c \ + errors.c \ + main.c \ + parse_text.c + diff --git a/libkcal/libical/examples/access_components.c b/libkcal/libical/examples/access_components.c new file mode 100644 index 000000000..dc92b64a7 --- /dev/null +++ b/libkcal/libical/examples/access_components.c @@ -0,0 +1,318 @@ +/* Access_component.c */ + +#include "ical.h" + +#include +#include /* for strdup */ +#include /* for malloc */ +#include /* for printf */ +#include /* for time() */ + +void do_something(icalcomponent *c); + +/* Creating iCal Components + + There are two ways to create new component in libical. You can + build the component from primitive parts, or you can create it + from a string. + + There are two variations of the API for building the component from + primitive parts. In the first variation, you add each parameter and + value to a property, and then add each property to a + component. This results in a long series of function calls. This + style is show in create_new_component() + + The second variation uses vargs lists to nest many primitive part + constructors, resulting in a compact, neatly formated way to create + components. This style is shown in create_new_component_with_va_args() + + + +*/ + +icalcomponent* create_new_component() +{ + + /* variable definitions */ + icalcomponent* calendar; + icalcomponent* event; + struct icaltimetype atime = icaltime_from_timet( time(0),0); + struct icalperiodtype rtime; + icalproperty* property; + + /* Define a time type that will use as data later. */ + rtime.start = icaltime_from_timet( time(0),0); + rtime.end = icaltime_from_timet( time(0),0); + rtime.end.hour++; + + /* Create calendar and add properties */ + + calendar = icalcomponent_new(ICAL_VCALENDAR_COMPONENT); + + /* Nearly every libical function call has the same general + form. The first part of the name defines the 'class' for the + function, and the first argument will be a pointer to a struct + of that class. So, icalcomponent_ functions will all take + icalcomponent* as their first argument. */ + + /* The next call creates a new proeprty and immediately adds it to the + 'calendar' component. */ + + icalcomponent_add_property( + calendar, + icalproperty_new_version("2.0") + ); + + + /* Here is the short version of the memory rules: + + If the routine name has "new" in it: + Caller owns the returned memory. + If you pass in a string, the routine takes the memory. + + If the routine name has "add" in it: + The routine takes control of the component, property, + parameter or value memory. + + If the routine returns a string ( "get" and "as_ical_string" ) + The library owns the returned memory. + + There are more rules, so refer to the documentation for more + details. + + */ + + icalcomponent_add_property( + calendar, + icalproperty_new_prodid("-//RDU Software//NONSGML HandCal//EN") + ); + + /* Add an event */ + + event = icalcomponent_new(ICAL_VEVENT_COMPONENT); + + icalcomponent_add_property( + event, + icalproperty_new_dtstamp(atime) + ); + + /* In the previous call, atime is a struct, and it is passed in by value. + This is how all compound types of values are handled. */ + + icalcomponent_add_property( + event, + icalproperty_new_uid("guid-1.host1.com") + ); + + /* add a property that has parameters */ + property = icalproperty_new_organizer("mailto:mrbig@host.com"); + + icalproperty_add_parameter( + property, + icalparameter_new_role(ICAL_ROLE_CHAIR) + ); + + icalcomponent_add_property(event,property); + + /* In this style of component creation, you need to use an extra + call to add parameters to properties, but the form of this + operation is the same as adding a property to a component */ + + /* add another property that has parameters */ + property = icalproperty_new_attendee("mailto:employee-A@host.com"); + + icalproperty_add_parameter( + property, + icalparameter_new_role(ICAL_ROLE_REQPARTICIPANT) + ); + + icalproperty_add_parameter( + property, + icalparameter_new_rsvp(1) + ); + + icalproperty_add_parameter( + property, + icalparameter_new_cutype(ICAL_CUTYPE_GROUP) + ); + + icalcomponent_add_property(event,property); + + + /* more properties */ + + icalcomponent_add_property( + event, + icalproperty_new_description("Project XYZ Review Meeting") + ); + + icalcomponent_add_property( + event, + icalproperty_new_categories("MEETING") + ); + + icalcomponent_add_property( + event, + icalproperty_new_class(ICAL_CLASS_PUBLIC) + ); + + icalcomponent_add_property( + event, + icalproperty_new_created(atime) + ); + + icalcomponent_add_property( + event, + icalproperty_new_summary("XYZ Project Review") + ); + + property = icalproperty_new_dtstart(atime); + + icalproperty_add_parameter( + property, + icalparameter_new_tzid("US-Eastern") + ); + + icalcomponent_add_property(event,property); + + + property = icalproperty_new_dtend(atime); + + icalproperty_add_parameter( + property, + icalparameter_new_tzid("US-Eastern") + ); + + icalcomponent_add_property(event,property); + + icalcomponent_add_property( + event, + icalproperty_new_location("1CP Conference Room 4350") + ); + + icalcomponent_add_component(calendar,event); + + return calendar; +} + + +/* Now, create the same component as in the previous routine, but use +the constructor style. */ + +icalcomponent* create_new_component_with_va_args() +{ + + /* This is a similar set up to the last routine */ + icalcomponent* calendar; + struct icaltimetype atime = icaltime_from_timet( time(0),0); + struct icalperiodtype rtime; + + rtime.start = icaltime_from_timet( time(0),0); + rtime.end = icaltime_from_timet( time(0),0); + rtime.end.hour++; + + /* Some of these routines are the same as those in the previous + routine, but we've also added several 'vanew' routines. These + 'vanew' routines take a list of properties, parameters or + values and add each of them to the parent property or + component. */ + + calendar = + icalcomponent_vanew( + ICAL_VCALENDAR_COMPONENT, + icalproperty_new_version("2.0"), + icalproperty_new_prodid("-//RDU Software//NONSGML HandCal//EN"), + icalcomponent_vanew( + ICAL_VEVENT_COMPONENT, + icalproperty_new_dtstamp(atime), + icalproperty_new_uid("guid-1.host1.com"), + icalproperty_vanew_organizer( + "mailto:mrbig@host.com", + icalparameter_new_role(ICAL_ROLE_CHAIR), + 0 + ), + icalproperty_vanew_attendee( + "mailto:employee-A@host.com", + icalparameter_new_role(ICAL_ROLE_REQPARTICIPANT), + icalparameter_new_rsvp(1), + icalparameter_new_cutype(ICAL_CUTYPE_GROUP), + 0 + ), + icalproperty_new_description("Project XYZ Review Meeting"), + + icalproperty_new_categories("MEETING"), + icalproperty_new_class(ICAL_CLASS_PUBLIC), + icalproperty_new_created(atime), + icalproperty_new_summary("XYZ Project Review"), + icalproperty_vanew_dtstart( + atime, + icalparameter_new_tzid("US-Eastern"), + 0 + ), + icalproperty_vanew_dtend( + atime, + icalparameter_new_tzid("US-Eastern"), + 0 + ), + icalproperty_new_location("1CP Conference Room 4350"), + 0 + ), + 0 + ); + + + /* Note that properties with no parameters can use the regular + 'new' constructor, while those with parameters use the 'vanew' + constructor. And, be sure that the last argument in the 'vanew' + call is a zero. Without, your program will probably crash. */ + + return calendar; +} + + +void find_sub_components(icalcomponent* comp) +{ + icalcomponent *c; + + /* The second parameter to icalcomponent_get_first_component + indicates the type of component to search for. This will + iterate through all sub-components */ + for(c = icalcomponent_get_first_component(comp,ICAL_ANY_COMPONENT); + c != 0; + c = icalcomponent_get_next_component(comp,ICAL_ANY_COMPONENT)){ + + do_something(c); + } + + /* This will iterate only though VEVENT sub-components */ + + for(c = icalcomponent_get_first_component(comp,ICAL_VEVENT_COMPONENT); + c != 0; + c = icalcomponent_get_next_component(comp,ICAL_VEVENT_COMPONENT)){ + + do_something(c); + } + +} + +/* Ical components only have one internal iterator, so removing the + object that the iterator points to can cause problems. Here is the + right way to remove components */ + +void remove_vevent_sub_components(icalcomponent* comp){ + + icalcomponent *c, *next; + + for( c = icalcomponent_get_first_component(comp,ICAL_VEVENT_COMPONENT); + c != 0; + c = next) + { + next = icalcomponent_get_next_component(comp,ICAL_VEVENT_COMPONENT); + + icalcomponent_remove_component(comp,c); + + do_something(c); + } + +} + diff --git a/libkcal/libical/examples/access_properties_and_parameters.c b/libkcal/libical/examples/access_properties_and_parameters.c new file mode 100644 index 000000000..ba3d7fcc0 --- /dev/null +++ b/libkcal/libical/examples/access_properties_and_parameters.c @@ -0,0 +1,144 @@ +/* access_properties_and_parameters.c */ + +#include "ical.h" +#include + +/* Get a particular parameter out of a component. This routine will + return a list of strings of all attendees who are required. Note + that this routine assumes that the component that we pass in is a + VEVENT. */ + +void get_required_attendees(icalcomponent* event) +{ + icalproperty* p; + icalparameter* parameter; + + assert(event != 0); + assert(icalcomponent_isa(event) == ICAL_VEVENT_COMPONENT); + + /* This loop iterates over all of the ATTENDEE properties in the + event */ + + /* The iteration routines save their state in the event + struct, so the are not thread safe unless you lock the whole + component. */ + + for( + p = icalcomponent_get_first_property(event,ICAL_ATTENDEE_PROPERTY); + p != 0; + p = icalcomponent_get_next_property(event,ICAL_ATTENDEE_PROPERTY) + ) { + + /* Get the first ROLE parameter in the property. There should + only be one, so we won't bother to iterate over them. But, + you can iterate over parameters just like with properties */ + + parameter = icalproperty_get_first_parameter(p,ICAL_ROLE_PARAMETER); + + /* If the parameter indicates the participant is required, get + the attendees name and stick a copy of it into the output + array */ + + if ( icalparameter_get_role(parameter) == ICAL_ROLE_REQPARTICIPANT) + { + /* Remember, the caller does not own this string, so you + should strdup it if you want to change it. */ + const char *attendee = icalproperty_get_attendee(p); + } + } + +} + +/* Here is a similar example. If an attendee has a PARTSTAT of + NEEDSACTION or has no PARTSTAT parameter, change it to + TENTATIVE. */ + +void update_attendees(icalcomponent* event) +{ + icalproperty* p; + icalparameter* parameter; + + assert(event != 0); + assert(icalcomponent_isa(event) == ICAL_VEVENT_COMPONENT); + + for( + p = icalcomponent_get_first_property(event,ICAL_ATTENDEE_PROPERTY); + p != 0; + p = icalcomponent_get_next_property(event,ICAL_ATTENDEE_PROPERTY) + ) { + + parameter = icalproperty_get_first_parameter(p,ICAL_PARTSTAT_PARAMETER); + + if (parameter == 0) { + + /* There was no PARTSTAT parameter, so add one. */ + icalproperty_add_parameter( + p, + icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE) + ); + + } else if (icalparameter_get_partstat(parameter) == ICAL_PARTSTAT_NEEDSACTION) { + /* Remove the NEEDSACTION parameter and replace it with + TENTATIVE */ + + icalproperty_remove_parameter(p,ICAL_PARTSTAT_PARAMETER); + + /* Don't forget to free it */ + icalparameter_free(parameter); + + /* Add a new one */ + icalproperty_add_parameter( + p, + icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE) + ); + } + + } +} + +/* Here are some examples of manipulating properties */ + +void test_properties() +{ + icalproperty *prop; + icalparameter *param; + icalvalue *value; + + icalproperty *clone; + + /* Create a new property */ + prop = icalproperty_vanew_comment( + "Another Comment", + icalparameter_new_cn("A Common Name 1"), + icalparameter_new_cn("A Common Name 2"), + icalparameter_new_cn("A Common Name 3"), + icalparameter_new_cn("A Common Name 4"), + 0); + + /* Iterate through all of the parameters in the property */ + for(param = icalproperty_get_first_parameter(prop,ICAL_ANY_PARAMETER); + param != 0; + param = icalproperty_get_next_parameter(prop,ICAL_ANY_PARAMETER)) { + + printf("Prop parameter: %s\n",icalparameter_get_cn(param)); + } + + /* Get a string representation of the property's value */ + printf("Prop value: %s\n",icalproperty_get_comment(prop)); + + /* Spit out the property in its RFC 2445 representation */ + printf("As iCAL string:\n %s\n",icalproperty_as_ical_string(prop)); + + /* Make a copy of the property. Caller owns the memory */ + clone = icalproperty_new_clone(prop); + + /* Get a reference to the value within the clone property */ + value = icalproperty_get_value(clone); + + printf("Value: %s",icalvalue_as_ical_string(value)); + + /* Free the original and the clone */ + icalproperty_free(clone); + icalproperty_free(prop); + +} diff --git a/libkcal/libical/examples/errors.c b/libkcal/libical/examples/errors.c new file mode 100644 index 000000000..86d963bd7 --- /dev/null +++ b/libkcal/libical/examples/errors.c @@ -0,0 +1,70 @@ +/* errors.c */ + +#include "ical.h" +#include + +void program_errors() +{ + /*Most routines will set icalerrno on errors. This is an + enumeration defined in icalerror.h */ + + icalcomponent *c; + + icalerror_clear_errno(); + + c = icalcomponent_new(ICAL_VEVENT_COMPONENT); + + if (icalerrno != ICAL_NO_ERROR){ + + fprintf(stderr,"Horrible libical error: %s\n", + icalerror_strerror(icalerrno)); + + } + +} + +void component_errors(icalcomponent *comp) +{ + int errors; + icalproperty *p; + + /* presume that we just got this component from the parser */ + + errors = icalcomponent_count_errors(comp); + + printf("This component has %d parsing errors\n", errors); + + /* Print out all of the parsing errors. This is not strictly + correct, because it does not descend into any sub-components, + as icalcomponent_count_errors() does. */ + + for(p = icalcomponent_get_first_property(comp,ICAL_XLICERROR_PROPERTY); + p != 0; + p = icalcomponent_get_next_property(comp,ICAL_XLICERROR_PROPERTY)) + { + + printf("-- The error is %s:\n",icalproperty_get_xlicerror(p)); + } + + + + /* Check the component for iTIP compilance, and add more + X-LIC-ERROR properties if it is non-compilant. */ + icalrestriction_check(comp); + + + /* Count the new errors. */ + if(errors != icalcomponent_count_errors(comp)){ + printf(" -- The component also has iTIP restriction errors \n"); + } + + /* Since there are iTIP restriction errors, it may be impossible + to process this component as an iTIP request. In this case, the + X-LIC-ERROR proeprties should be expressed as REQUEST-STATUS + properties in the reply. This following routine makes this + conversion */ + + + icalcomponent_convert_errors(comp); + +} diff --git a/libkcal/libical/examples/main.c b/libkcal/libical/examples/main.c new file mode 100644 index 000000000..1be2de5c9 --- /dev/null +++ b/libkcal/libical/examples/main.c @@ -0,0 +1,12 @@ +/* This is just to make the code in the example directory link properly. */ +#include "ical.h" + +int main() +{ + + return 1; +} + + +void do_something(icalcomponent* comp){ +} diff --git a/libkcal/libical/examples/parse_text.c b/libkcal/libical/examples/parse_text.c new file mode 100644 index 000000000..b7eba43b8 --- /dev/null +++ b/libkcal/libical/examples/parse_text.c @@ -0,0 +1,68 @@ +/* parse_text.c + + */ +#include +#include +#include +#include +#include "ical.h" + +#include + +/* The icalparser_get_line routine will create a single *content* line +out of one or more input lines. The content line is all of the +properties and values for a single property, and it can span several +input lines. So, icalparser_get_line will need to be able to get more +data on its own. Read_string is a routine that does this. You can +write your own version of read stream to get data from other types of +files, sockets, etc. */ + +char* read_stream(char *s, size_t size, void *d) +{ + char *c = fgets(s,size, (FILE*)d); + + return c; + +} + +void parse_text(int argc, char* argv[]) +{ + + char* line; + FILE* stream; + icalcomponent *c; + + /* Create a new parser object */ + icalparser *parser = icalparser_new(); + + stream = fopen(argv[1],"r"); + + assert(stream != 0); + + /* Tell the parser what input routie it should use. */ + icalparser_set_gen_data(parser,stream); + + do{ + + /* Get a single content line by making one or more calls to + read_stream()*/ + line = icalparser_get_line(parser,read_stream); + + /* Now, add that line into the parser object. If that line + completes a component, c will be non-zero */ + c = icalparser_add_line(parser,line); + + + if (c != 0){ + printf("%s",icalcomponent_as_ical_string(c)); + + printf("\n---------------\n"); + + icalcomponent_free(c); + } + + } while ( line != 0); + + + icalparser_free(parser); +} -- cgit v1.2.1