2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OGo
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
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.
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
25 #include <Common/UIxComponent.h>
26 #include <SOGoLogic/SOGoAppointment.h>
27 #include <NGiCal/NGiCal.h>
30 /* TODO: CLEAN THIS MESS UP */
33 @interface NSObject (AppointmentHack)
34 - (BOOL)isAppointment;
37 @implementation NSObject (AppointmentHack)
38 - (BOOL)isAppointment {
43 @interface SxAppointment : NSObject
46 @implementation SxAppointment (AppointmentHack)
47 - (BOOL)isAppointment {
52 @interface iCalPerson (Convenience)
53 - (NSString *)rfc822Email;
56 @implementation iCalPerson (Convenience)
57 - (NSString *)rfc822Email {
58 NSString *_email = [self email];
59 NSRange colon = [_email rangeOfString:@":"];
60 if(colon.location != NSNotFound) {
61 return [_email substringFromIndex:colon.location + 1];
67 @interface UIxAppointmentEditor : UIxComponent
73 - (SOGoAppointment *)appointment;
74 - (NSString *)iCalStringTemplate;
75 - (BOOL)isNewAppointment;
79 @implementation UIxAppointmentEditor
82 [self->appointment release];
83 [self->participants release];
91 - (NSString *)formattedAptStartTime {
94 date = [[self appointment] startDate];
95 /* TODO: convert this into display timeZone! */
96 return [date descriptionWithCalendarFormat:@"%A, %Y-%m-%d %H:%M %Z"];
99 - (BOOL)isNewAppointment {
100 return ! [[self clientObject] isAppointment];
106 - (SOGoAppointment *)appointment {
107 if(self->appointment == nil) {
108 NSString *iCalString;
110 if([[self clientObject] isAppointment]) {
111 iCalString = [[self clientObject] valueForKey:@"iCalString"];
114 iCalString = [self iCalStringTemplate];
116 self->appointment = [[SOGoAppointment alloc]
117 initWithICalString:iCalString];
119 return self->appointment;
123 if(self->participants == nil) {
125 NSMutableArray *emailAddresses;
128 attendees = [self->appointment attendees];
129 count = [attendees count];
130 emailAddresses = [[NSMutableArray alloc] initWithCapacity:count];
131 for(i = 0; i < count; i++) {
134 email = [[attendees objectAtIndex:i] rfc822Email];
136 [emailAddresses addObject:email];
138 self->participants = [[emailAddresses componentsJoinedByString:@"\n"]
140 [emailAddresses release];
142 return self->participants;
145 - (NSString *)iCalStringTemplate {
146 static NSString *iCalStringTemplate = \
147 @"BEGIN:VCALENDAR\nMETHOD:REQUEST\nPRODID:OpenGroupware.org ZideStore 1.2\n" \
148 @"VERSION:2.0\nBEGIN:VEVENT\nCLASS:PRIVATE\nSTATUS:CONFIRMED\n" \
149 @"DTSTART:%@\nDTEND:%@\n" \
151 @"END:VEVENT\nEND:VCALENDAR";
152 NSCalendarDate *startDate, *endDate;
155 startDate = [self selectedDate];
156 endDate = [startDate dateByAddingYears:0 months:0 days:0
157 hours:1 minutes:0 seconds:0];
160 template = [NSString stringWithFormat:iCalStringTemplate,
161 [startDate icalString],
162 [endDate icalString]];
170 - (NSString *)uriAsFormat {
174 uri = [[[self context] request] uri];
176 /* first: identify query parameters */
177 r = [uri rangeOfString:@"?" options:NSBackwardsSearch];
179 uri = [uri substringToIndex:r.location];
180 qp = [uri substringFromIndex:r.location];
186 /* next: strip trailing slash */
187 if([uri hasSuffix:@"/"])
188 uri = [uri substringToIndex:([uri length] - 1)];
189 r = [uri rangeOfString:@"/" options:NSBackwardsSearch];
191 /* then: cut of last path component */
192 if(r.location == NSNotFound) { // no slash? are we at root?
196 uri = [uri substringToIndex:(r.location + 1)];
198 /* next: append format token */
199 uri = [uri stringByAppendingString:@"%@"];
201 uri = [uri stringByAppendingString:qp];
210 NSString *foo, *nextMethod, *uri, *uriFormat;
211 NSMutableArray *attendees;
215 req = [[self context] request];
216 foo = [req formValueForKey:@"ical"];
218 if([self isNewAppointment])
219 nextMethod = @"duhduh";
221 nextMethod = @"view";
223 uriFormat = [self uriAsFormat];
224 uri = [NSString stringWithFormat:uriFormat, nextMethod];
225 NSLog(@"%s got foo:%@, redirecting to '%@'",
230 r = [WOResponse responseWithRequest:req];
231 [r setStatus:302 /* moved */];
232 [r setHeader:uri forKey:@"location"];