]> err.no Git - sope/blob - sope-ical/samples/ical3.m
bumbed versions to 4.5
[sope] / sope-ical / samples / ical3.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #import <Foundation/Foundation.h>
23
24 @class EOQualifier, NSString, EOSortOrdering;
25
26 @interface iCal3Tool : NSObject
27 {
28   EOQualifier *qualifier;
29   NSString    *entityName;
30   NSArray     *sortOrderings;
31 }
32
33 - (int)runWithArguments:(NSArray *)_args;
34
35 @end
36
37 #include <NGiCal/iCalDataSource.h>
38 #include <NGiCal/iCalObject.h>
39 #include <EOControl/EOQualifier.h>
40 #include <EOControl/EOSortOrdering.h>
41 #include "common.h"
42
43 @implementation iCal3Tool
44
45 - (id)init {
46   if ((self = [super init])) {
47     NSUserDefaults *ud;
48     id tmp;
49     
50     /* collect options */
51     ud = [NSUserDefaults standardUserDefaults];
52
53     self->entityName = [[ud stringForKey:@"entity"] copy];
54     
55     if ((tmp = [ud objectForKey:@"qualifier"]))
56       self->qualifier = [[EOQualifier alloc] initWithPropertyList:tmp];
57
58     if ((tmp = [ud objectForKey:@"sort"])) {
59       if ((tmp = [[EOSortOrdering alloc] initWithPropertyList:tmp])) {
60         self->sortOrderings = [[NSArray alloc] initWithObjects:tmp,nil];
61         [tmp release];
62       }
63     }
64   }
65   return self;
66 }
67 - (void)dealloc {
68   [self->sortOrderings release];
69   [self->qualifier     release];
70   [self->entityName    release];
71   [super dealloc];
72 }
73
74 /* run */
75
76 - (void)printObject:(id)_object {
77   printf("object: %s\n", [[_object description] cString]);
78 }
79
80 - (int)runWithArguments:(NSArray *)_args {
81   NSEnumerator *args;
82   NSString     *arg;
83   
84   /* begin processing */
85   
86   args = [_args objectEnumerator];
87   [args nextObject]; // process name ...
88   
89   while ((arg = [args nextObject])) {
90     NSAutoreleasePool *pool2;
91     
92     if ([arg hasPrefix:@"-"]) { /* consume defaults */
93       [args nextObject];
94       continue;
95     }
96     
97     pool2 = [[NSAutoreleasePool alloc] init];
98     {    
99       iCalDataSource       *ds;
100       EOFetchSpecification *fspec;
101       NSArray    *objs;
102       iCalObject *obj;
103       
104       /* setup fetch specification */
105       
106       fspec = [[[EOFetchSpecification alloc] init] autorelease];
107       [fspec setEntityName:self->entityName];
108       [fspec setQualifier:self->qualifier];
109       
110       /* setup datasource */
111       
112       ds = [[[iCalDataSource alloc] initWithPath:arg] autorelease];
113       [ds setFetchSpecification:fspec];
114
115       /* perform fetch */
116       
117       if ((objs = [ds fetchObjects]) == nil) {
118         /* fetch failed */
119         
120         NSLog(@"fetch on ical file failed: %@", arg);
121       }
122       else {
123         /* process results */
124         NSEnumerator *e;
125         
126         e = [objs objectEnumerator];
127         while ((obj = [e nextObject])) {
128           [self printObject:obj];
129         }
130       }
131     }
132     [pool2 release];
133   }
134   return 0;
135 }
136
137 @end /* iCal3Tool */
138
139 int main(int argc, char **argv, char **env)  {
140   NSAutoreleasePool *pool;
141   iCal3Tool *tool;
142   int rc;
143
144 #if LIB_FOUNDATION_LIBRARY  
145   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
146 #endif
147   
148   pool = [[NSAutoreleasePool alloc] init];
149   
150   if ((tool = [[iCal3Tool alloc] init])) {
151     NS_DURING
152       rc = [tool runWithArguments:[[NSProcessInfo processInfo] arguments]];
153     NS_HANDLER
154       abort();
155     NS_ENDHANDLER;
156     
157     [tool release];
158   }
159   else
160     rc = 1;
161   
162   [pool release];
163   return rc;
164 }