]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxCalWeekChartview.m
some more work on the Kolab viewers
[scalable-opengroupware.org] / UI / Scheduler / UIxCalWeekChartview.m
1 /*
2   Copyright (C) 2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 // $Id$
22
23
24 #include "UIxCalWeekView.h"
25
26 @interface UIxCalWeekChartview : UIxCalWeekView
27 {
28   int day;
29   int hour;
30   NSArray *hours;
31 }
32
33 - (NSCalendarDate *)currentDate;
34 @end
35
36 #include "common.h"
37 #include <NGExtensions/NGCalendarDateRange.h>
38 #include <sys/param.h> // MIN, MAX
39 #include <SOGoUI/SOGoAptFormatter.h>
40
41 @implementation UIxCalWeekChartview
42
43 - (void)dealloc {
44   [self->hours release];
45   [super dealloc];
46 }
47
48 - (void)setDay:(int)_day {
49   NSCalendarDate *date;
50
51   self->day = _day;
52
53   date = [[self startDate] dateByAddingYears:0 months:0 days:_day];
54   [self setCurrentDay:date];
55 }
56 - (int)day {
57   return self->day;
58 }
59
60 - (void)setHour:(int)_hour {
61   self->hour = _hour;
62 }
63 - (int)hour {
64   return self->hour;
65 }
66
67 - (NSCalendarDate *)currentDate {
68   NSCalendarDate *date;
69   
70   date = [[self startDate] beginOfDay];
71   date = [date dateByAddingYears:0 months:0 days:[self day]
72                                     hours:[self hour] minutes:0 seconds:0];
73   return date;
74 }
75
76 /* columns */
77
78 - (NSArray *)columns {
79   static NSMutableArray *columns = nil;
80
81   if(!columns) {
82     unsigned i, count;
83     
84     count = [self shouldDisplayWeekend] ? 7 : 5;
85     columns = [[NSMutableArray alloc] initWithCapacity:count];
86     for(i = 0; i < count; i++) {
87       [columns addObject:[NSNumber numberWithInt:i]];
88     }
89   }
90   return columns;
91 }
92
93 /* tests */
94
95 /* row is active, if apt intersects hour range */
96 - (BOOL)isRowActive {
97   NSCalendarDate *aptStart, *aptEnd, *date;
98   int            aptStartHour, aptEndHour;
99   BOOL           isStartOnSameDay, isEndOnSameDay;
100
101   aptStart         = [self->appointment valueForKey:@"startDate"];
102   aptEnd           = [self->appointment valueForKey:@"endDate"];
103   date             = [self currentDay];
104   isStartOnSameDay = [aptStart isDateOnSameDay:date];
105   isEndOnSameDay   = [aptEnd   isDateOnSameDay:date];
106
107   if (!isStartOnSameDay && !isEndOnSameDay)
108     return YES;
109   aptStartHour  = [aptStart hourOfDay];
110   aptEndHour    = [aptEnd   hourOfDay];
111   if (isStartOnSameDay && isEndOnSameDay)
112     return (([self hour] >= aptStartHour) &&
113             ([self hour] <= aptEndHour));
114   if (!isStartOnSameDay)
115     return [self hour] <= aptEndHour;
116   return [self hour] >= aptStartHour;
117 }
118
119 /* item is active, if apt's dateRange intersects the range
120 of the current column (currentDay is set to be this date) */
121 - (BOOL)isItemActive {
122   NSCalendarDate *dateStart, *dateEnd, *aptStart, *aptEnd;
123   NGCalendarDateRange *dateRange, *aptRange;
124   
125   dateStart = [self currentDate];
126   dateEnd   = [dateStart dateByAddingYears:0 months:0 days:0
127                                      hours:1 minutes:0 seconds:0];
128   dateRange = [NGCalendarDateRange calendarDateRangeWithStartDate:dateStart
129                                                           endDate:dateEnd];
130   aptStart = [self->appointment valueForKey:@"startDate"];
131   aptEnd   = [self->appointment valueForKey:@"endDate"];
132   aptRange = [NGCalendarDateRange calendarDateRangeWithStartDate:aptStart
133                                                          endDate:aptEnd];
134   return [dateRange doesIntersectWithDateRange:aptRange];
135 }
136
137 /* hours */
138
139 - (NSArray *)hours {
140   if(!self->hours) {
141     NSMutableArray *result;
142     NSArray *apts;
143     unsigned i, count;
144     unsigned min, max;
145     
146     min = [self dayStartHour];
147     max = [self dayEndHour];
148     
149     apts = [self appointments];
150     count = [apts count];
151     for(i = 0; i < count; i++) {
152       id apt;
153       NSCalendarDate *aptStart, *aptEnd;
154       apt = [apts objectAtIndex:i];
155       aptStart = [apt valueForKey:@"startDate"];
156       if(aptStart) {
157         min = MIN(min, [aptStart hourOfDay]);
158       }
159       aptEnd = [apt valueForKey:@"endDate"];
160       if(aptEnd) {
161         max = MAX(max, [aptEnd hourOfDay]);
162       }
163     }
164     result = [[NSMutableArray alloc] initWithCapacity:max - min];
165     for(i = min; i <= max; i++) {
166       [result addObject:[NSNumber numberWithInt:i]];
167     }
168     self->hours = result;
169   }
170   return self->hours;
171 }
172
173 /* descriptions */
174
175 - (void)configureFormatters {
176   [super configureFormatters];
177   [self->aptFormatter setTitleOnly];
178   [self->privateAptFormatter setPrivateTitleOnly];
179 }
180
181
182 /* style sheet */
183
184 - (NSString *)titleStyle {
185   if([self->currentDay isToday])
186     return @"weekoverview_title_hilite";
187   return @"weekoverview_title";
188 }
189
190 @end /* UIxCalWeekChartview */
191