]> err.no Git - scalable-opengroupware.org/blob - UI/MainUI/SOGoUserHomePage.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1196 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / MainUI / SOGoUserHomePage.m
1 /* SOGoUserHomePage.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 #import <Foundation/NSArray.h>
24 #import <Foundation/NSCalendarDate.h>
25 #import <Foundation/NSDictionary.h>
26 #import <Foundation/NSURL.h>
27 #import <Foundation/NSUserDefaults.h>
28 #import <Foundation/NSValue.h>
29 #import <NGObjWeb/WOCookie.h>
30 #import <NGObjWeb/WORequest.h>
31 #import <NGObjWeb/WOResponse.h>
32 #import <NGExtensions/NSCalendarDate+misc.h>
33 #import <NGExtensions/NSObject+Logs.h>
34
35 #import <Appointments/SOGoFreeBusyObject.h>
36 #import <SoObjects/SOGo/SOGoWebAuthenticator.h>
37 #import <SoObjects/SOGo/SOGoUser.h>
38 #import <SoObjects/SOGo/NSCalendarDate+SOGo.h>
39 #import <SOGoUI/UIxComponent.h>
40
41 static NSString *defaultModule = nil;
42
43 @interface SOGoUserHomePage : UIxComponent
44
45 @end
46
47 @implementation SOGoUserHomePage
48
49 + (void) initialize
50 {
51   NSUserDefaults *ud;
52
53   if (!defaultModule)
54     {
55       ud = [NSUserDefaults standardUserDefaults];
56       defaultModule = [ud stringForKey: @"SOGoUIxDefaultModule"];
57       if (defaultModule)
58         {
59           if (!([defaultModule isEqualToString: @"Calendar"]
60                 || [defaultModule isEqualToString: @"Contacts"]
61                 || [defaultModule isEqualToString: @"Mail"]))
62             {
63               [self logWithFormat: @"default module '%@' not accepted (must be"
64                     @"'Calendar', 'Contacts' or Mail)", defaultModule];
65               defaultModule = @"Calendar";
66             }
67         }
68       else
69         defaultModule = @"Calendar";
70       [self logWithFormat: @"default module set to '%@'", defaultModule];
71       [defaultModule retain];
72     }
73 }
74
75 - (id <WOActionResults>) defaultAction
76 {
77   SOGoUserFolder *co;
78   NSURL *moduleURL;
79
80   co = [self clientObject];
81   moduleURL = [NSURL URLWithString: defaultModule
82                      relativeToURL: [co soURL]];
83
84   return [self redirectToLocation: [moduleURL absoluteString]];
85 }
86
87 - (void) _fillFreeBusyItems: (NSMutableArray *) items
88                 withRecords: (NSEnumerator *) records
89               fromStartDate: (NSCalendarDate *) startDate
90                   toEndDate: (NSCalendarDate *) endDate
91 {
92   NSDictionary *record;
93   int count, startInterval, endInterval, value;
94   NSNumber *status;
95   NSCalendarDate *currentDate;
96   
97   record = [records nextObject];
98   while (record)
99     {
100       status = [record objectForKey: @"c_status"];
101  
102       value = [[record objectForKey: @"c_startdate"] intValue];
103       currentDate = [NSCalendarDate dateWithTimeIntervalSince1970: value];
104       if ([currentDate earlierDate: startDate] == currentDate)
105         startInterval = 0;
106       else
107         startInterval
108           = ([currentDate timeIntervalSinceDate: startDate] / 900);
109
110       value = [[record objectForKey: @"c_enddate"] intValue];
111       currentDate = [NSCalendarDate dateWithTimeIntervalSince1970: value];
112       if ([currentDate earlierDate: endDate] == endDate)
113         endInterval = [items count] - 1;
114       else
115         endInterval = ([currentDate timeIntervalSinceDate: startDate] / 900);
116
117       for (count = startInterval; count < endInterval; count++)
118         [items replaceObjectAtIndex: count withObject: status];
119
120       record = [records nextObject];
121     }
122 }
123  
124 - (NSString *) _freeBusyAsTextFromStartDate: (NSCalendarDate *) startDate
125                                   toEndDate: (NSCalendarDate *) endDate
126                                 forFreeBusy: (SOGoFreeBusyObject *) fb
127 {
128   NSEnumerator *records;
129   NSMutableArray *freeBusyItems;
130   NSTimeInterval interval;
131   int count, intervals;
132
133   interval = [endDate timeIntervalSinceDate: startDate] + 60;
134   intervals = interval / 900; /* slices of 15 minutes */
135   freeBusyItems = [NSMutableArray arrayWithCapacity: intervals];
136   for (count = 1; count < intervals; count++)
137     [freeBusyItems addObject: @"0"];
138
139   records = [[fb fetchFreeBusyInfosFrom: startDate to: endDate] objectEnumerator];
140   [self _fillFreeBusyItems: freeBusyItems withRecords: records
141         fromStartDate: startDate toEndDate: endDate];
142
143   return [freeBusyItems componentsJoinedByString: @","];
144 }
145
146 - (NSString *) _freeBusyAsText
147 {
148   SOGoFreeBusyObject *co;
149   NSCalendarDate *startDate, *endDate;
150   NSString *queryDay, *additionalDays;
151   NSTimeZone *uTZ;
152   SOGoUser *user;
153
154   co = [self clientObject];
155   user = [context activeUser];
156   uTZ = [user timeZone];
157
158   queryDay = [self queryParameterForKey: @"sday"];
159   if ([queryDay length])
160     startDate = [NSCalendarDate dateFromShortDateString: queryDay
161                                 andShortTimeString: @"0000"
162                                 inTimeZone: uTZ];
163   else
164     {
165       startDate = [NSCalendarDate calendarDate];
166       [startDate setTimeZone: uTZ];
167       startDate = [startDate hour: 0 minute: 0];
168     }
169
170   queryDay = [self queryParameterForKey: @"eday"];
171   if ([queryDay length])
172     endDate = [NSCalendarDate dateFromShortDateString: queryDay
173                               andShortTimeString: @"2359"
174                               inTimeZone: uTZ];
175   else
176     endDate = [startDate hour: 23 minute: 59];
177
178   additionalDays = [self queryParameterForKey: @"additional"];
179   if ([additionalDays length] > 0)
180     endDate = [endDate dateByAddingYears: 0 months: 0
181                        days: [additionalDays intValue]
182                        hours: 0 minutes: 0 seconds: 0];
183
184   return [self _freeBusyAsTextFromStartDate: startDate toEndDate: endDate
185                forFreeBusy: co];
186 }
187
188 - (id <WOActionResults>) readFreeBusyAction
189 {
190   WOResponse *response;
191
192   response = [context response];
193   [response setStatus: 200];
194 //   [response setHeader: @"text/plain; charset=iso-8859-1"
195 //             forKey: @"Content-Type"];
196   [response appendContentString: [self _freeBusyAsText]];
197
198   return response;
199 }
200
201 - (id <WOActionResults>) logoffAction
202 {
203   WOResponse *response;
204   WOCookie *cookie;
205   SOGoWebAuthenticator *auth;
206   id container;
207   NSCalendarDate *date;
208
209   container = [[self clientObject] container];
210
211   response = [context response];
212   [response setStatus: 302];
213   [response setHeader: [container baseURLInContext: context]
214             forKey: @"location"];
215   auth = [[self clientObject] authenticatorInContext: context];
216   cookie = [WOCookie cookieWithName: [auth cookieNameInContext: context]
217                      value: @"discard"];
218   [cookie setPath: @"/"];
219   date = [NSCalendarDate calendarDate];
220   [cookie setExpires: [date yesterday]];
221   [response addCookie: cookie];
222   
223   [response setHeader: date forKey: @"Last-Modified"];
224   [response setHeader: @"no-store, no-cache, must-revalidate, max-age=0" forKey: @"Cache-Control"];
225   [response setHeader: @"post-check=0, pre-check=0" forKey: @"Cache-Control"];
226   [response setHeader: @"no-cache" forKey: @"Pragma"];
227
228   return response;
229 }
230
231 @end