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