]> err.no Git - scalable-opengroupware.org/blob - UI/MainUI/SOGoUserHomePage.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1163 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           else
68             defaultModule = @"Calendar";
69         }
70       else
71         defaultModule = @"Calendar";
72       [self logWithFormat: @"default module set to '%@'", defaultModule];
73       [defaultModule retain];
74     }
75 }
76
77 - (id <WOActionResults>) defaultAction
78 {
79   SOGoUserFolder *co;
80   NSURL *moduleURL;
81
82   co = [self clientObject];
83   moduleURL = [NSURL URLWithString: defaultModule
84                      relativeToURL: [co soURL]];
85
86   return [self redirectToLocation: [moduleURL absoluteString]];
87 }
88
89 - (void) _fillFreeBusyItems: (NSMutableArray *) items
90                 withRecords: (NSEnumerator *) records
91               fromStartDate: (NSCalendarDate *) startDate
92                   toEndDate: (NSCalendarDate *) endDate
93 {
94   NSDictionary *record;
95   int count, startInterval, endInterval, value;
96   NSNumber *status;
97   NSCalendarDate *currentDate;
98   
99   record = [records nextObject];
100   while (record)
101     {
102       status = [record objectForKey: @"c_status"];
103  
104       value = [[record objectForKey: @"c_startdate"] intValue];
105       currentDate = [NSCalendarDate dateWithTimeIntervalSince1970: value];
106       if ([currentDate earlierDate: startDate] == currentDate)
107         startInterval = 0;
108       else
109         startInterval
110           = ([currentDate timeIntervalSinceDate: startDate] / 900);
111
112       value = [[record objectForKey: @"c_enddate"] intValue];
113       currentDate = [NSCalendarDate dateWithTimeIntervalSince1970: value];
114       if ([currentDate earlierDate: endDate] == endDate)
115         endInterval = [items count] - 1;
116       else
117         endInterval = ([currentDate timeIntervalSinceDate: startDate] / 900);
118
119       for (count = startInterval; count < endInterval; count++)
120         [items replaceObjectAtIndex: count withObject: status];
121
122       record = [records nextObject];
123     }
124 }
125  
126 - (NSString *) _freeBusyAsTextFromStartDate: (NSCalendarDate *) startDate
127                                   toEndDate: (NSCalendarDate *) endDate
128                                 forFreeBusy: (SOGoFreeBusyObject *) fb
129 {
130   NSEnumerator *records;
131   NSMutableArray *freeBusyItems;
132   NSTimeInterval interval;
133   int count, intervals;
134
135   interval = [endDate timeIntervalSinceDate: startDate] + 60;
136   intervals = interval / 900; /* slices of 15 minutes */
137   freeBusyItems = [NSMutableArray arrayWithCapacity: intervals];
138   for (count = 1; count < intervals; count++)
139     [freeBusyItems addObject: @"0"];
140
141   records = [[fb fetchFreeBusyInfosFrom: startDate to: endDate] objectEnumerator];
142   [self _fillFreeBusyItems: freeBusyItems withRecords: records
143         fromStartDate: startDate toEndDate: endDate];
144
145   return [freeBusyItems componentsJoinedByString: @","];
146 }
147
148 - (NSString *) _freeBusyAsText
149 {
150   SOGoFreeBusyObject *co;
151   NSCalendarDate *startDate, *endDate;
152   NSString *queryDay, *additionalDays;
153   NSTimeZone *uTZ;
154   SOGoUser *user;
155
156   co = [self clientObject];
157   user = [context activeUser];
158   uTZ = [user timeZone];
159
160   queryDay = [self queryParameterForKey: @"sday"];
161   if ([queryDay length])
162     startDate = [NSCalendarDate dateFromShortDateString: queryDay
163                                 andShortTimeString: @"0000"
164                                 inTimeZone: uTZ];
165   else
166     {
167       startDate = [NSCalendarDate calendarDate];
168       [startDate setTimeZone: uTZ];
169       startDate = [startDate hour: 0 minute: 0];
170     }
171
172   queryDay = [self queryParameterForKey: @"eday"];
173   if ([queryDay length])
174     endDate = [NSCalendarDate dateFromShortDateString: queryDay
175                               andShortTimeString: @"2359"
176                               inTimeZone: uTZ];
177   else
178     endDate = [startDate hour: 23 minute: 59];
179
180   additionalDays = [self queryParameterForKey: @"additional"];
181   if ([additionalDays length] > 0)
182     endDate = [endDate dateByAddingYears: 0 months: 0
183                        days: [additionalDays intValue]
184                        hours: 0 minutes: 0 seconds: 0];
185
186   return [self _freeBusyAsTextFromStartDate: startDate toEndDate: endDate
187                forFreeBusy: co];
188 }
189
190 - (id <WOActionResults>) readFreeBusyAction
191 {
192   WOResponse *response;
193
194   response = [context response];
195   [response setStatus: 200];
196 //   [response setHeader: @"text/plain; charset=iso-8859-1"
197 //             forKey: @"Content-Type"];
198   [response appendContentString: [self _freeBusyAsText]];
199
200   return response;
201 }
202
203 - (id <WOActionResults>) logoffAction
204 {
205   WOResponse *response;
206   NSEnumerator *cookies;
207   WOCookie *cookie;
208   SOGoWebAuthenticator *auth;
209   id container;
210
211   container = [[self clientObject] container];
212
213   response = [context response];
214   [response setStatus: 302];
215   [response setHeader: [container baseURLInContext: context]
216             forKey: @"location"];
217   cookies = [[response cookies] objectEnumerator];
218   auth = [[self clientObject] authenticatorInContext: context];
219   cookie = [WOCookie cookieWithName: [auth cookieNameInContext: context]
220                      value: @"logoff"];
221   [response addCookie: cookie];
222
223   return response;
224 }
225
226 @end