aboutsummaryrefslogblamecommitdiffstats
path: root/libical/src/test/storage.c
blob: f6d0dd69e5a51fe4495ef911b553f8903fb12299 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460











































































































































































































































































































































































































































































                                                                                        
/* -*- Mode: C -*-
  ======================================================================
  FILE: usecases.c
  CREATOR: eric 03 April 1999
  
  DESCRIPTION:
  
  $Id$
  $Locker$

  (C) COPYRIGHT 1999 Eric Busboom 
  http://www.softwarestudio.org

  The contents of this file are subject to the Mozilla Public License
  Version 1.0 (the "License"); you may not use this file except in
  compliance with the License. You may obtain a copy of the License at
  http://www.mozilla.org/MPL/
 
  Software distributed under the License is distributed on an "AS IS"
  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  the License for the specific language governing rights and
  limitations under the License.

  The original author is Eric Busboom
  The original code is usecases.c

    
  ======================================================================*/

#include "ical.h"
#include <assert.h>
#include <string.h> /* for strdup */
#include <stdlib.h> /* for malloc */
#include <stdio.h> /* for printf */
#include <time.h> /* for time() */
#include "icalmemory.h"
#include "icalstore.h"
#include "icalcluster.h"
#include "icalerror.h"
#include "icalrestriction.h"
#include "icalcalendar.h"

/* This example creates and minipulates the ical object that appears
 * in rfc 2445, page 137 */

char str[] = "BEGIN:VCALENDAR\n\
PRODID:\"-//RDU Software//NONSGML HandCal//EN\"\n\
VERSION:2.0\n\
BEGIN:VTIMEZONE\n\
TZID:US-Eastern\n\
BEGIN:STANDARD\n\
DTSTART:19981025T020000\n\
RDATE:19981025T020000\n\
TZOFFSETFROM:-0400\n\
TZOFFSETTO:-0500\n\
TZNAME:EST\n\
END:STANDARD\n\
BEGIN:DAYLIGHT\n\
DTSTART:19990404T020000\n\
RDATE:19990404T020000\n\
TZOFFSETFROM:-0500\n\
TZOFFSETTO:-0400\n\
TZNAME:EDT\n\
END:DAYLIGHT\n\
END:VTIMEZONE\n\
BEGIN:VEVENT\n\
DTSTAMP:19980309T231000Z\n\
UID:guid-1.host1.com\n\
ORGANIZER;ROLE=CHAIR:MAILTO:mrbig@host.com\n\
ATTENDEE;RSVP=TRUE;ROLE=REQ-PARTICIPANT;CUTYPE=GROUP:MAILTO:employee-A@host.com\n\
DESCRIPTION:Project XYZ Review Meeting\n\
CATEGORIES:MEETING\n\
CLASS:PUBLIC\n\
CREATED:19980309T130000Z\n\
SUMMARY:XYZ Project Review\n\
DTSTART;TZID=US-Eastern:19980312T083000\n\
DTEND;TZID=US-Eastern:19980312T093000\n\
LOCATION:1CP Conference Room 4350\n\
END:VEVENT\n\
BEGIN:BOOGA\n\
DTSTAMP:19980309T231000Z\n\
X-LIC-FOO:Booga\n\
DTSTOMP:19980309T231000Z\n\
UID:guid-1.host1.com\n\
END:BOOGA\n\
END:VCALENDAR";

char str2[] = "BEGIN:VCALENDAR\n\
PRODID:\"-//RDU Software//NONSGML HandCal//EN\"\n\
VERSION:2.0\n\
BEGIN:VEVENT\n\
DTSTAMP:19980309T231000Z\n\
UID:guid-1.host1.com\n\
ORGANIZER;ROLE=CHAIR:MAILTO:mrbig@host.com\n\
ATTENDEE;RSVP=TRUE;ROLE=REQ-PARTICIPANT;CUTYPE=GROUP:MAILTO:employee-A@host.com\n\
DESCRIPTION:Project XYZ Review Meeting\n\
CATEGORIES:MEETING\n\
CLASS:PUBLIC\n\
CREATED:19980309T130000Z\n\
SUMMARY:XYZ Project Review\n\
DTSTART;TZID=US-Eastern:19980312T083000\n\
DTEND;TZID=US-Eastern:19980312T093000\n\
LOCATION:1CP Conference Room 4350\n\
END:VEVENT\n\
END:VCALENDAR\n\
";


void test_cluster()
{
    icalcluster *cin, *cout;
    int month = 0;
    int count=0;
    struct icaltimetype start, end;
    icalcomponent *c,*clone, *itr;

    start = icaltimetype_from_timet( time(0),0);
    end = start;
    end.hour++;

    cout = icalcluster_new("clusterout.ics");
    assert(cout != 0);

    c = icalparser_parse_string(str2);
    assert(c != 0);

    for(month = 1; month < 2; month++){
    icalcomponent *event;
    icalproperty *dtstart, *dtend;

        cout = icalcluster_new("clusterout.ics");
        assert(cout != 0);

    start.month = month; 
    end.month = month;
    
    clone = icalcomponent_new_clone(c);
    assert(clone !=0);
    event = icalcomponent_get_first_component(clone,ICAL_VEVENT_COMPONENT);
    assert(event != 0);

    dtstart = icalcomponent_get_first_property(event,ICAL_DTSTART_PROPERTY);
    assert(dtstart!=0);
    icalproperty_set_dtstart(dtstart,start);
        
    dtend = icalcomponent_get_first_property(event,ICAL_DTEND_PROPERTY);
    assert(dtend!=0);
    icalproperty_set_dtend(dtend,end);
    
    icalcluster_add_component(cout,clone);
    icalcluster_commit(cout);

        icalcluster_free(cout);

    }


    /* Print them out */


    cout = icalcluster_new("clusterout.ics");
    assert(cout != 0);
    
    for (itr = icalcluster_get_first_component(cout,
                                               ICAL_ANY_COMPONENT);
         itr != 0;
         itr = icalcluster_get_next_component(cout,
                                              ICAL_ANY_COMPONENT)){ 

      icalcomponent *event;
      icalproperty *dtstart, *dtend;

      count++;

      event = icalcomponent_get_first_component(itr,ICAL_VEVENT_COMPONENT);

      dtstart = icalcomponent_get_first_property(event,ICAL_DTSTART_PROPERTY);
      dtend = icalcomponent_get_first_property(event,ICAL_DTEND_PROPERTY);
      
      printf("%d %s %s\n",count, icalproperty_as_ical_string(dtstart),
             icalproperty_as_ical_string(dtend));

    }

    /* Remove all of them */

    icalcluster_free(cout);

    cout = icalcluster_new("clusterout.ics");
    assert(cout != 0);
    
    for (itr = icalcluster_get_first_component(cout,
                                               ICAL_ANY_COMPONENT);
         itr != 0;
         itr = icalcluster_get_next_component(cout,
                                              ICAL_ANY_COMPONENT)){ 


      icalcluster_remove_component(cout, itr);
    }

    icalcluster_free(cout);


    /* Print them out again */

    cout = icalcluster_new("clusterout.ics");
    assert(cout != 0);
    count =0;
    
    for (itr = icalcluster_get_first_component(cout,
                                               ICAL_ANY_COMPONENT);
         itr != 0;
         itr = icalcluster_get_next_component(cout,
                                              ICAL_ANY_COMPONENT)){ 

      icalcomponent *event;
      icalproperty *dtstart, *dtend;

      count++;

      event = icalcomponent_get_first_component(itr,ICAL_VEVENT_COMPONENT);

      dtstart = icalcomponent_get_first_property(event,ICAL_DTSTART_PROPERTY);
      dtend = icalcomponent_get_first_property(event,ICAL_DTEND_PROPERTY);
      
      printf("%d %s %s\n",count, icalproperty_as_ical_string(dtstart),
             icalproperty_as_ical_string(dtend));

    }

    icalcluster_free(cout);


}



int test_store()
{

    icalcomponent *c, *gauge;
    icalerrorenum error;
    icalcomponent *next, *itr;
    icalcluster* cluster;
    struct icalperiodtype rtime;
    icalstore *s = icalstore_new("store");
    int i;

    assert(s != 0);

    rtime.start = icaltimetype_from_timet( time(0),0);

    cluster = icalcluster_new("clusterout.ics");

    assert(cluster != 0);

#define NUMCOMP 4

    /* Duplicate every component in the cluster NUMCOMP times */

    icalerror_clear_errno();

    for (i = 1; i<NUMCOMP+1; i++){

    /*rtime.start.month = i%12;*/
    rtime.start.month = i;
    rtime.end = rtime.start;
    rtime.end.hour++;
    
    for (itr = icalcluster_get_first_component(cluster,
                           ICAL_ANY_COMPONENT);
         itr != 0;
         itr = icalcluster_get_next_component(cluster,
                          ICAL_ANY_COMPONENT)){
        icalcomponent *clone, *inner;
        icalproperty *p;

        inner = icalcomponent_get_first_component(itr,ICAL_VEVENT_COMPONENT);
            if (inner == 0){
              continue;
            }

        /* Change the dtstart and dtend times in the component
               pointed to by Itr*/

        clone = icalcomponent_new_clone(itr);
        inner = icalcomponent_get_first_component(itr,ICAL_VEVENT_COMPONENT);

        assert(icalerrno == ICAL_NO_ERROR);
        assert(inner !=0);

        /* DTSTART*/
        p = icalcomponent_get_first_property(inner,ICAL_DTSTART_PROPERTY);
        assert(icalerrno  == ICAL_NO_ERROR);

        if (p == 0){
        p = icalproperty_new_dtstart(rtime.start);
        icalcomponent_add_property(inner,p);
        } else {
        icalproperty_set_dtstart(p,rtime.start);
        }
        assert(icalerrno  == ICAL_NO_ERROR);

        /* DTEND*/
        p = icalcomponent_get_first_property(inner,ICAL_DTEND_PROPERTY);
        assert(icalerrno  == ICAL_NO_ERROR);

        if (p == 0){
        p = icalproperty_new_dtstart(rtime.end);
        icalcomponent_add_property(inner,p);
        } else {
        icalproperty_set_dtstart(p,rtime.end);
        }
        assert(icalerrno  == ICAL_NO_ERROR);
        
        printf("\n----------\n%s\n---------\n",icalcomponent_as_ical_string(inner));

        error = icalstore_add_component(s,inner);
        
        assert(icalerrno  == ICAL_NO_ERROR);

    }

    }
    
    gauge = 
    icalcomponent_vanew(
        ICAL_VCALENDAR_COMPONENT,
        icalcomponent_vanew(
        ICAL_VEVENT_COMPONENT,  
        icalproperty_vanew_summary(
            "Submit Income Taxes",
            icalparameter_new_xliccomparetype(ICAL_XLICCOMPARETYPE_EQUAL),
            0),
        0),
        icalcomponent_vanew(
        ICAL_VEVENT_COMPONENT,  
        icalproperty_vanew_summary(
            "Bastille Day Party",
            icalparameter_new_xliccomparetype(ICAL_XLICCOMPARETYPE_EQUAL),
            0),
        0),
        0);

#if 0


    icalstore_select(s,gauge);

    for(c = icalstore_first(s); c != 0; c = icalstore_next(s)){
    
    printf("Got one! (%d)\n", count++);
    
    if (c != 0){
        printf("%s", icalcomponent_as_ical_string(c));;
        if (icalstore_store(s2,c) == 0){
        printf("Failed to write!\n");
        }
        icalcomponent_free(c);
    } else {
        printf("Failed to get component\n");
    }
    }


    icalstore_free(s2);
#endif


    for(c = icalstore_get_first_component(s); 
    c != 0; 
    c =  next){
    
    next = icalstore_get_next_component(s);

    if (c != 0){
        /*icalstore_remove_component(s,c);*/
        printf("%s", icalcomponent_as_ical_string(c));;
    } else {
        printf("Failed to get component\n");
    }


    }

    icalstore_free(s);
    return 0;
}

void test_calendar()
{
    icalcomponent *comp;
    icalcluster *c;
    icalstore *s;
    icalcalendar* calendar = icalcalendar_new("calendar");
    icalerrorenum error;
    struct icaltimetype atime = icaltimetype_from_timet( time(0),0);

    comp = icalcomponent_vanew(
    ICAL_VEVENT_COMPONENT,
    icalproperty_new_version("2.0"),
    icalproperty_new_description("This is an event"),
    icalproperty_new_dtstart(atime),
    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),
    icalproperty_vanew_xlicerror(
        "This is only a test",
        icalparameter_new_xlicerrortype(ICAL_XLICERRORTYPE_COMPONENTPARSEERROR),
        0),
    
    0);
    
    
    s = icalcalendar_get_booked(calendar);

    error = icalstore_add_component(s,comp);
    
    assert(error == ICAL_NO_ERROR);

    c = icalcalendar_get_properties(calendar);

    error = icalcluster_add_component(c,icalcomponent_new_clone(comp));

    assert(error == ICAL_NO_ERROR);

    icalcalendar_free(calendar);

}


int main(int argc, char *argv[])
{


    printf("\n------------Test Cluster---------------\n");
    test_cluster();

#if 0    

    printf("\n------------Test Store---------------\n");
    test_store();



    printf("\n------------Test Calendar---------------\n");
    test_calendar();

#endif

    return 0;
}