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