]> err.no Git - scalable-opengroupware.org/blob - UI/MainUI/SOGoUserHomePage.m
7731f9eaae9e2ca51d91b8d0873ef2af623dad53
[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/NSValue.h>
26 #import <NGObjWeb/WORequest.h>
27 #import <NGObjWeb/WOResponse.h>
28 #import <NGExtensions/NSCalendarDate+misc.h>
29
30 #import <Appointments/SOGoFreeBusyObject.h>
31 #import <SOGo/NSCalendarDate+SOGo.h>
32 #import <SOGoUI/UIxComponent.h>
33
34 @interface SOGoUserHomePage : UIxComponent
35
36 @end
37
38 @implementation SOGoUserHomePage
39
40 - (id <WOActionResults>) defaultAction
41 {
42   NSString *baseURL, *url;
43
44   baseURL = [[context request] uri];
45   url = [baseURL stringByAppendingString:@"/../Calendar"];
46
47   return [self redirectToLocation: url];
48 }
49
50 - (void) _fillFreeBusyItems: (NSMutableArray *) items
51                 withRecords: (NSEnumerator *) records
52               fromStartDate: (NSCalendarDate *) startDate
53                   toEndDate: (NSCalendarDate *) endDate
54 {
55   NSDictionary *record;
56   int count, startInterval, endInterval, value;
57   NSNumber *status;
58   NSCalendarDate *currentDate;
59   
60   record = [records nextObject];
61   while (record)
62     {
63       status = [record objectForKey: @"status"];
64  
65       value = [[record objectForKey: @"startdate"] intValue];
66       currentDate = [NSCalendarDate dateWithTimeIntervalSince1970: value];
67       if ([currentDate earlierDate: startDate] == currentDate)
68         startInterval = 0;
69       else
70         startInterval
71           = ([currentDate timeIntervalSinceDate: startDate] / 900);
72
73       value = [[record objectForKey: @"enddate"] intValue];
74       currentDate = [NSCalendarDate dateWithTimeIntervalSince1970: value];
75       if ([currentDate earlierDate: endDate] == endDate)
76         endInterval = [items count] - 1;
77       else
78         endInterval = ([currentDate timeIntervalSinceDate: startDate] / 900);
79
80       for (count = startInterval; count < endInterval; count++)
81         [items replaceObjectAtIndex: count withObject: status];
82
83       record = [records nextObject];
84     }
85 }
86  
87 - (NSString *) _freeBusyAsTextFromStartDate: (NSCalendarDate *) startDate
88                                   toEndDate: (NSCalendarDate *) endDate
89                                 forFreeBusy: (SOGoFreeBusyObject *) fb
90 {
91   NSEnumerator *records;
92   NSMutableArray *freeBusyItems;
93   NSTimeInterval interval;
94   int count, intervals;
95
96   interval = [endDate timeIntervalSinceDate: startDate] + 60;
97   intervals = interval / 900; /* slices of 15 minutes */
98   freeBusyItems = [NSMutableArray arrayWithCapacity: intervals];
99   for (count = 1; count < intervals; count++)
100     [freeBusyItems addObject: @"0"];
101
102   records = [[fb fetchFreeBusyInfosFrom: startDate to: endDate] objectEnumerator];
103   [self _fillFreeBusyItems: freeBusyItems withRecords: records
104         fromStartDate: startDate toEndDate: endDate];
105
106   return [freeBusyItems componentsJoinedByString: @","];
107 }
108
109 - (NSString *) _freeBusyAsText
110 {
111   SOGoFreeBusyObject *co;
112   NSCalendarDate *startDate, *endDate;
113   NSString *queryDay, *additionalDays;
114   NSTimeZone *uTZ;
115
116   co = [self clientObject];
117   uTZ = [co userTimeZone];
118
119   queryDay = [self queryParameterForKey: @"sday"];
120   if ([queryDay length])
121     startDate = [NSCalendarDate dateFromShortDateString: queryDay
122                                 andShortTimeString: @"0000"
123                                 inTimeZone: uTZ];
124   else
125     {
126       startDate = [NSCalendarDate calendarDate];
127       [startDate setTimeZone: uTZ];
128       startDate = [startDate hour: 0 minute: 0];
129     }
130
131   queryDay = [self queryParameterForKey: @"eday"];
132   if ([queryDay length])
133     endDate = [NSCalendarDate dateFromShortDateString: queryDay
134                               andShortTimeString: @"2359"
135                               inTimeZone: uTZ];
136   else
137     endDate = [startDate hour: 23 minute: 59];
138
139   additionalDays = [self queryParameterForKey: @"additional"];
140   if ([additionalDays length] > 0)
141     endDate = [endDate dateByAddingYears: 0 months: 0
142                        days: [additionalDays intValue]
143                        hours: 0 minutes: 0 seconds: 0];
144
145   return [self _freeBusyAsTextFromStartDate: startDate toEndDate: endDate
146                forFreeBusy: co];
147 }
148
149 - (id <WOActionResults>) readFreeBusyAction
150 {
151   WOResponse *response;
152
153   response = [context response];
154   [response setStatus: 200];
155   [response setHeader: @"text/plain; charset=iso-8859-1"
156             forKey: @"Content-Type"];
157   [response appendContentString: [self _freeBusyAsText]];
158
159   return response;
160 }
161
162 @end