]> err.no Git - sope/blob - sope-ical/samples/ievalrrule.m
bumped Xcode projects to version 4.7. Added a new Xcode project for the STXSaxDriver.
[sope] / sope-ical / samples / ievalrrule.m
1 /*
2   Copyright (C) 2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE 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   SOPE 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 SOPE; 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 #include <NGiCal/iCalRecurrenceRule.h>
23 #include <NGiCal/iCalRecurrenceCalculator.h>
24 #include <NGExtensions/NGCalendarDateRange.h>
25 #include "common.h"
26
27 static NSCalendarDate *dateForString(NSString *_s) {
28   // copied from ogo-chkaptconflicts, maybe move to NGExtensions?
29   static NSCalendarDate *now = nil;
30   static NSCalendarDate *mon = nil;
31   
32   if (now == nil) now = [[NSCalendarDate date] retain];
33   if (mon == nil) mon = [[now mondayOfWeek] retain];
34   _s = [_s lowercaseString];
35   
36   if ([_s isEqualToString:@"now"])       return now;
37   if ([_s isEqualToString:@"tomorrow"])  return [now tomorrow];
38   if ([_s isEqualToString:@"yesterday"]) return [now yesterday];
39   
40   if ([_s hasPrefix:@"mon"]) return mon;
41   if ([_s hasPrefix:@"tue"]) return [mon dateByAddingYears:0 months:0 days:1];
42   if ([_s hasPrefix:@"wed"]) return [mon dateByAddingYears:0 months:0 days:2];
43   if ([_s hasPrefix:@"thu"]) return [mon dateByAddingYears:0 months:0 days:3];
44   if ([_s hasPrefix:@"fri"]) return [mon dateByAddingYears:0 months:0 days:4];
45   if ([_s hasPrefix:@"sat"]) return [mon dateByAddingYears:0 months:0 days:5];
46   if ([_s hasPrefix:@"sun"]) return [mon dateByAddingYears:0 months:0 days:6];
47   
48   switch ([_s length]) {
49   case 6:
50     return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y%m"];
51   case 8:
52     return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y%m%d"];
53   case 10:
54     return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y%-m-%d"];
55   case 13:
56     return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y%m%d %H%M"];
57   case 14:
58     return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y%m%d %H:%M"];
59   case 16:
60     return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y-%m-%d %H:%M"];
61   default:
62     return nil;
63   }
64 }
65
66 static int usage(NSArray *args) {
67   fprintf(stderr,
68           "usage: %s <rrule> <startdate> <enddate> <cycleend>\n"
69           "\n"
70           "sample:\n"
71           "  %s 'FREQ=MONTHLY;BYDAY=2TU' '20050901 14:00' '20050901 15:00' "
72           "20060921\n",
73           [[args objectAtIndex:0] cString],
74           [[args objectAtIndex:0] cString]);
75   return 1;
76 }
77
78 static void printInstances(NSArray *instances) {
79   unsigned i, count;
80   
81   if ((count = [instances count]) == 0) {
82     printf("no reccurrences in given range\n");
83     return;
84   }
85   
86   for (i = 0; i < count; i++) {
87     NGCalendarDateRange *instance;
88     NSString *s;
89     
90     instance = [instances objectAtIndex:i];
91
92     s = [[instance startDate] descriptionWithCalendarFormat:
93                                 @"%a, %Y-%m-%d at %H:%M"];
94     printf("%s - ", [s cString]);
95
96     s = [[instance endDate] descriptionWithCalendarFormat:
97                               [[instance startDate] isDateOnSameDay:
98                                                       [instance endDate]]
99                             ? @"%H:%M"
100                             : @"%a, %Y-%m-%d at %H:%M"];
101     printf("%s\n", [s cString]);
102   }
103 }
104
105 static int runIt(NSArray *args) {
106   iCalRecurrenceCalculator *cpu;
107   iCalRecurrenceRule  *rrule;
108   NGCalendarDateRange *startRange, *calcRange;
109   NSCalendarDate      *from, *to, *cycleTo;
110   NSString            *pattern;
111   NSArray             *instances;
112   
113   if ([args count] < 5)
114     return usage(args);
115   
116   pattern = [args objectAtIndex:1];
117   from    = dateForString([args objectAtIndex:2]);
118   to      = dateForString([args objectAtIndex:3]);
119   cycleTo = dateForString([args objectAtIndex:4]);
120   
121   if (from == nil || to == nil || cycleTo == nil || ![pattern isNotEmpty])
122     return usage(args);
123   
124   startRange =
125     [NGCalendarDateRange calendarDateRangeWithStartDate:from endDate:to];
126   
127   calcRange =
128     [NGCalendarDateRange calendarDateRangeWithStartDate:from endDate:cycleTo];
129   
130   /* parse rrule */
131
132   if ((rrule = [[iCalRecurrenceRule alloc] initWithString:pattern]) == nil) {
133     usage(args);
134     fprintf(stderr, "error: could not parse reccurence rule: '%s'\n",
135             [pattern cString]);
136     return 2;
137   }
138   
139   NSLog(@"from: %@ to: %@, cycle %@", from, to, cycleTo);
140   NSLog(@"rrule: %@", rrule);
141   
142   /* calculate */
143   
144   cpu = [iCalRecurrenceCalculator 
145           recurrenceCalculatorForRecurrenceRule:rrule
146           withFirstInstanceCalendarDateRange:startRange];
147   
148   instances = [cpu recurrenceRangesWithinCalendarDateRange:calcRange];
149   printInstances(instances);
150   
151   return 0;
152 }
153
154 int main(int argc, char **argv, char **env)  {
155   NSAutoreleasePool *pool;
156   int rc;
157   
158   pool = [[NSAutoreleasePool alloc] init];
159 #if LIB_FOUNDATION_LIBRARY  
160   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
161 #endif
162   
163   rc = runIt([[NSProcessInfo processInfo] argumentsWithoutDefaults]);
164   [pool release];
165   return rc;
166 }