]> err.no Git - sope/blob - sope-core/samples/ical3.m
more code directory reorganizations
[sope] / sope-core / samples / ical3.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
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 // $Id$
22
23 #import <Foundation/Foundation.h>
24
25 @class EOQualifier, NSString, EOSortOrdering;
26
27 @interface iCal3Tool : NSObject
28 {
29   EOQualifier *qualifier;
30   NSString    *entityName;
31   NSArray     *sortOrderings;
32 }
33
34 - (int)runWithArguments:(NSArray *)_args;
35
36 @end
37
38 #include <NGiCal/iCalDataSource.h>
39 #include <NGiCal/iCalObject.h>
40 #include <EOControl/EOQualifier.h>
41 #include <EOControl/EOSortOrdering.h>
42 #include "common.h"
43
44 @implementation iCal3Tool
45
46 - (id)init {
47   if ((self = [super init])) {
48     NSUserDefaults *ud;
49     id tmp;
50     
51     /* collect options */
52     ud = [NSUserDefaults standardUserDefaults];
53
54     self->entityName = [[ud stringForKey:@"entity"] copy];
55     
56     if ((tmp = [ud objectForKey:@"qualifier"]))
57       self->qualifier = [[EOQualifier alloc] initWithPropertyList:tmp];
58
59     if ((tmp = [ud objectForKey:@"sort"])) {
60       if ((tmp = [[EOSortOrdering alloc] initWithPropertyList:tmp])) {
61         self->sortOrderings = [[NSArray alloc] initWithObjects:tmp,nil];
62         [tmp release];
63       }
64     }
65   }
66   return self;
67 }
68 - (void)dealloc {
69   [self->sortOrderings release];
70   [self->qualifier     release];
71   [self->entityName    release];
72   [super dealloc];
73 }
74
75 /* run */
76
77 - (void)printObject:(id)_object {
78   printf("object: %s\n", [[_object description] cString]);
79 }
80
81 - (int)runWithArguments:(NSArray *)_args {
82   NSEnumerator *args;
83   NSString     *arg;
84   
85   /* begin processing */
86   
87   args = [_args objectEnumerator];
88   [args nextObject]; // process name ...
89   
90   while ((arg = [args nextObject])) {
91     NSAutoreleasePool *pool2;
92     
93     if ([arg hasPrefix:@"-"]) { /* consume defaults */
94       [args nextObject];
95       continue;
96     }
97     
98     pool2 = [[NSAutoreleasePool alloc] init];
99     {    
100       iCalDataSource       *ds;
101       EOFetchSpecification *fspec;
102       NSArray    *objs;
103       iCalObject *obj;
104       
105       /* setup fetch specification */
106       
107       fspec = [[[EOFetchSpecification alloc] init] autorelease];
108       [fspec setEntityName:self->entityName];
109       [fspec setQualifier:self->qualifier];
110       
111       /* setup datasource */
112       
113       ds = [[[iCalDataSource alloc] initWithPath:arg] autorelease];
114       [ds setFetchSpecification:fspec];
115
116       /* perform fetch */
117       
118       if ((objs = [ds fetchObjects]) == nil) {
119         /* fetch failed */
120         
121         NSLog(@"fetch on ical file failed: %@", arg);
122       }
123       else {
124         /* process results */
125         NSEnumerator *e;
126         
127         e = [objs objectEnumerator];
128         while ((obj = [e nextObject])) {
129           [self printObject:obj];
130         }
131       }
132     }
133     [pool2 release];
134   }
135   return 0;
136 }
137
138 @end /* iCal3Tool */
139
140 int main(int argc, char **argv, char **env)  {
141   NSAutoreleasePool *pool;
142   iCal3Tool *tool;
143   int rc;
144
145 #if LIB_FOUNDATION_LIBRARY  
146   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
147 #endif
148   
149   pool = [[NSAutoreleasePool alloc] init];
150   
151   if ((tool = [[iCal3Tool alloc] init])) {
152     NS_DURING
153       rc = [tool runWithArguments:[[NSProcessInfo processInfo] arguments]];
154     NS_HANDLER
155       abort();
156     NS_ENDHANDLER;
157     
158     [tool release];
159   }
160   else
161     rc = 1;
162   
163   [pool release];
164   return rc;
165 }