]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxAppointmentEditor.m
Install libs to /usr/lib
[scalable-opengroupware.org] / UI / Scheduler / UIxAppointmentEditor.m
1 /* UIxAppointmentEditor.m - this file is part of SOGo
2  *
3  * Copyright (C) 2007 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
7  * This file is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <math.h>
24
25 #import <NGObjWeb/SoObject.h>
26 #import <NGObjWeb/WORequest.h>
27 #import <NGObjWeb/WOResponse.h>
28 #import <NGObjWeb/NSException+HTTP.h>
29 #import <NGExtensions/NSCalendarDate+misc.h>
30
31 #import <NGCards/iCalCalendar.h>
32 #import <NGCards/iCalEvent.h>
33 #import <NGCards/iCalPerson.h>
34 #import <NGCards/iCalRecurrenceRule.h>
35
36 #import <SoObjects/SOGo/SOGoUser.h>
37 #import <SoObjects/SOGo/SOGoContentObject.h>
38 #import <SoObjects/Appointments/SOGoAppointmentFolder.h>
39 #import <SoObjects/Appointments/SOGoAppointmentObject.h>
40
41 #import "UIxComponentEditor.h"
42 #import "UIxAppointmentEditor.h"
43
44 @implementation UIxAppointmentEditor
45
46 - (id) init
47 {
48   if ((self = [super init]))
49     {
50       aptStartDate = nil;
51       aptEndDate = nil;
52       item = nil;
53       event = nil;
54       isAllDay = NO;
55     }
56
57   return self;
58 }
59
60 - (void) dealloc
61 {
62   [item release];
63   [aptStartDate release];
64   [aptEndDate release];
65   [super dealloc];
66 }
67
68 /* template values */
69 - (iCalEvent *) event
70 {
71   if (!event)
72     {
73       event = (iCalEvent *) [[self clientObject] component: YES secure: YES];
74       [event retain];
75     }
76
77   return event;
78 }
79
80 - (NSString *) saveURL
81 {
82   return [NSString stringWithFormat: @"%@/saveAsAppointment",
83                    [[self clientObject] baseURL]];
84 }
85
86 /* icalendar values */
87 - (BOOL) isAllDay
88 {
89   NSString *hm;
90
91   hm = [self queryParameterForKey: @"hm"];
92
93   return (isAllDay
94           || (hm && [hm isEqualToString: @"allday"]));
95 }
96
97 - (void) setIsAllDay: (BOOL) newIsAllDay
98 {
99   isAllDay = newIsAllDay;
100 }
101
102 - (void) setAptStartDate: (NSCalendarDate *) newAptStartDate
103 {
104   ASSIGN (aptStartDate, newAptStartDate);
105 }
106
107 - (NSCalendarDate *) aptStartDate
108 {
109   return aptStartDate;
110 }
111
112 - (void) setAptEndDate: (NSCalendarDate *) newAptEndDate
113 {
114   ASSIGN (aptEndDate, newAptEndDate);
115 }
116
117 - (NSCalendarDate *) aptEndDate
118 {
119   return aptEndDate;
120 }
121
122 - (void) setItem: (NSString *) newItem
123 {
124   ASSIGN (item, newItem);
125 }
126
127 - (NSString *) item
128 {
129   return item;
130 }
131
132 /* actions */
133 - (NSCalendarDate *) newStartDate
134 {
135   NSCalendarDate *newStartDate, *now;
136   NSTimeZone *timeZone;
137   int hour;
138
139   newStartDate = [self selectedDate];
140   if ([[self queryParameterForKey: @"hm"] length] == 0)
141     {
142       now = [NSCalendarDate calendarDate];
143       timeZone = [[context activeUser] timeZone];
144       [now setTimeZone: timeZone];
145       if ([now isDateOnSameDay: newStartDate])
146         {
147           hour = [now hourOfDay];
148           if (hour < 8)
149             newStartDate = [now hour: 8 minute: 0];
150           else if (hour > 18)
151             newStartDate = [[now tomorrow] hour: 8 minute: 0];
152           else
153             newStartDate = now;
154         }
155       else
156         newStartDate = [newStartDate hour: 8 minute: 0];
157     }
158
159   return newStartDate;
160 }
161
162 - (id <WOActionResults>) defaultAction
163 {
164   NSCalendarDate *startDate, *endDate;
165   NSString *duration;
166   unsigned int minutes;
167
168   [self event];
169   if ([[self clientObject] isNew])
170     {
171       startDate = [self newStartDate];
172       duration = [self queryParameterForKey:@"dur"];
173       if ([duration length] > 0)
174         minutes = [duration intValue];
175       else
176         minutes = 60;
177       endDate
178         = [startDate dateByAddingYears: 0 months: 0 days: 0
179                      hours: 0 minutes: minutes seconds: 0];
180     }
181   else
182     {
183       startDate = [event startDate];
184       isAllDay = [event isAllDay];
185       if (isAllDay)
186         endDate = [[event endDate] dateByAddingYears: 0 months: 0 days: -1];
187       else
188         endDate = [event endDate];
189     }
190
191   ASSIGN (aptStartDate, startDate);
192   ASSIGN (aptEndDate, endDate);
193
194   return self;
195 }
196
197 - (id <WOActionResults>) newAction
198 {
199   NSString *objectId, *method, *uri;
200   id <WOActionResults> result;
201   SOGoAppointmentFolder *co;
202
203   co = [self clientObject];
204   objectId = [co globallyUniqueObjectId];
205   if ([objectId length] > 0)
206     {
207       method = [NSString stringWithFormat:@"%@/%@.ics/editAsAppointment",
208                          [co soURL], objectId];
209       uri = [self completeHrefForMethod: method];
210       result = [self redirectToLocation: uri];
211     }
212   else
213     result = [NSException exceptionWithHTTPStatus: 500 /* Internal Error */
214                           reason: @"could not create a unique ID"];
215
216   return result;
217 }
218
219 - (id <WOActionResults>) saveAction
220 {
221   [[self clientObject] saveComponent: event];
222
223   return [self jsCloseWithRefreshMethod: @"refreshEventsAndDisplay()"];
224 }
225
226 - (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
227                            inContext: (WOContext*) context
228 {
229   NSString *actionName;
230
231   actionName = [[request requestHandlerPath] lastPathComponent];
232
233   return ([[self clientObject] isKindOfClass: [SOGoAppointmentObject class]]
234           && [actionName hasPrefix: @"save"]);
235 }
236
237 - (void) takeValuesFromRequest: (WORequest *) _rq
238                      inContext: (WOContext *) _ctx
239 {
240   SOGoAppointmentObject *clientObject;
241   int nbrDays;
242
243   clientObject = [self clientObject];
244   [self event];
245
246   [super takeValuesFromRequest: _rq inContext: _ctx];
247
248   if (isAllDay)
249     {
250       nbrDays = ((float) abs ([aptEndDate timeIntervalSinceDate: aptStartDate])
251                  / 86400) + 1;
252       [event setAllDayWithStartDate: aptStartDate
253              duration: nbrDays];
254     }
255   else
256     {
257       [event setStartDate: aptStartDate];
258       [event setEndDate: aptEndDate];
259     }
260   if ([clientObject isNew])
261     [event setTransparency: @"OPAQUE"];
262 }
263
264 // TODO: add tentatively
265
266 - (id) acceptAction
267 {
268   [[self clientObject] changeParticipationStatus: @"ACCEPTED"];
269
270   return self;
271 }
272
273 - (id) declineAction
274 {
275   [[self clientObject] changeParticipationStatus: @"DECLINED"];
276
277   return self;
278 }
279
280 @end