]> err.no Git - scalable-opengroupware.org/blob - ZideStore/UI-X/Scheduler/UIxAppointmentEditor.m
work in progress. some more refactoring and overall improvement of the editor.
[scalable-opengroupware.org] / ZideStore / UI-X / Scheduler / UIxAppointmentEditor.m
1 /*
2   Copyright (C) 2000-2004 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
24 #include "common.h"
25 #include <Common/UIxComponent.h>
26 #include <SOGoLogic/SOGoAppointment.h>
27 #include <NGiCal/NGiCal.h>
28
29
30 /* TODO: CLEAN THIS MESS UP */
31
32
33 @interface NSObject (AppointmentHack)
34 - (BOOL)isAppointment;
35 @end
36
37 @implementation NSObject (AppointmentHack)
38 - (BOOL)isAppointment {
39     return NO;
40 }
41 @end
42
43 @interface SxAppointment : NSObject
44 @end
45
46 @implementation SxAppointment (AppointmentHack)
47 - (BOOL)isAppointment {
48     return YES;
49 }
50 @end
51
52 @interface iCalPerson (Convenience)
53 - (NSString *)rfc822Email;
54 @end
55
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];
62     }
63     return _email;
64 }
65 @end
66
67 @interface UIxAppointmentEditor : UIxComponent
68 {
69     id appointment;
70     id participants;
71 }
72
73 - (SOGoAppointment *)appointment;
74 - (NSString *)iCalStringTemplate;
75 - (BOOL)isNewAppointment;
76
77 @end
78
79 @implementation UIxAppointmentEditor
80
81 - (void)dealloc {
82     [self->appointment release];
83     [self->participants release];
84     [super dealloc];
85 }
86
87
88 /* accessors */
89
90
91 - (NSString *)formattedAptStartTime {
92     NSCalendarDate *date;
93     
94     date = [[self appointment] startDate];
95     /* TODO: convert this into display timeZone! */
96     return [date descriptionWithCalendarFormat:@"%A, %Y-%m-%d %H:%M %Z"];
97 }
98
99 - (BOOL)isNewAppointment {
100     return ! [[self clientObject] isAppointment];
101 }
102
103 /* backend */
104
105
106 - (SOGoAppointment *)appointment {
107     if(self->appointment == nil) {
108         NSString *iCalString;
109
110         if([[self clientObject] isAppointment]) {
111             iCalString = [[self clientObject] valueForKey:@"iCalString"];
112         }
113         else {
114             iCalString = [self iCalStringTemplate];
115         }
116         self->appointment = [[SOGoAppointment alloc]
117                                               initWithICalString:iCalString];
118     }
119     return self->appointment;
120 }
121
122 - (id)participants {
123     if(self->participants == nil) {
124         NSArray *attendees;
125         NSMutableArray *emailAddresses;
126         unsigned i, count;
127
128         attendees = [self->appointment attendees];
129         count = [attendees count];
130         emailAddresses = [[NSMutableArray alloc] initWithCapacity:count];
131         for(i = 0; i < count; i++) {
132             NSString *email;
133             
134             email = [[attendees objectAtIndex:i] rfc822Email];
135             if(email)
136                 [emailAddresses addObject:email];
137         }
138         self->participants = [[emailAddresses componentsJoinedByString:@"\n"]
139             retain];
140         [emailAddresses release];
141     }
142     return self->participants;
143 }
144
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" \
150     @"TRANSP:OPAQUE\n" \
151     @"END:VEVENT\nEND:VCALENDAR";
152     NSCalendarDate *startDate, *endDate;
153     NSString *template;
154
155     startDate = [self selectedDate];
156     endDate = [startDate dateByAddingYears:0 months:0 days:0
157                          hours:1 minutes:0 seconds:0];
158     
159
160     template = [NSString stringWithFormat:iCalStringTemplate,
161                                           [startDate icalString],
162                                           [endDate icalString]];
163
164     return template;
165 }
166
167
168 /* helper */
169
170 - (NSString *)uriAsFormat {
171     NSString *uri, *qp;
172     NSRange r;
173
174     uri = [[[self context] request] uri];
175     
176     /* first: identify query parameters */
177     r = [uri rangeOfString:@"?" options:NSBackwardsSearch];
178     if (r.length > 0) {
179         uri = [uri substringToIndex:r.location];
180         qp = [uri substringFromIndex:r.location];
181     }
182     else {
183         qp = nil;
184     }
185     
186     /* next: strip trailing slash */
187     if([uri hasSuffix:@"/"])
188         uri = [uri substringToIndex:([uri length] - 1)];
189     r = [uri rangeOfString:@"/" options:NSBackwardsSearch];
190     
191     /* then: cut of last path component */
192     if(r.location == NSNotFound) { // no slash? are we at root?
193         uri = @"/";
194     }
195     else {
196         uri = [uri substringToIndex:(r.location + 1)];
197     }
198     /* next: append format token */
199     uri = [uri stringByAppendingString:@"%@"];
200     if(qp != nil)
201         uri = [uri stringByAppendingString:qp];
202     return uri;
203 }
204
205
206 /* save */
207
208
209 - (id)saveAction {
210     NSString *foo, *nextMethod, *uri, *uriFormat;
211     NSMutableArray *attendees;
212     WOResponse *r;
213     WORequest *req;
214
215     req = [[self context] request];
216     foo = [req formValueForKey:@"ical"];
217
218     if([self isNewAppointment])
219         nextMethod = @"duhduh";
220     else
221         nextMethod = @"view";
222
223     uriFormat = [self uriAsFormat];
224     uri = [NSString stringWithFormat:uriFormat, nextMethod];
225     NSLog(@"%s got foo:%@, redirecting to '%@'",
226           __PRETTY_FUNCTION__,
227           foo,
228           uri);
229
230     r = [WOResponse responseWithRequest:req];
231     [r setStatus:302 /* moved */];
232     [r setHeader:uri forKey:@"location"];
233     return r;
234 }
235
236 @end