]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxCalendarSelector.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1263 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / Scheduler / UIxCalendarSelector.m
1 /* UIxCalendarSelector.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/NSDictionary.h>
25 #import <Foundation/NSValue.h>
26
27 #import <SOGo/NSDictionary+Utilities.h>
28
29 #import <SoObjects/SOGo/SOGoUser.h>
30
31 #import <Appointments/SOGoAppointmentFolder.h>
32 #import <Appointments/SOGoAppointmentFolders.h>
33
34 #import "UIxCalendarSelector.h"
35
36 // static inline char
37 // darkenedColor (const char value)
38 // {
39 //   char newValue;
40
41 //   if (value >= '0' && value <= '9')
42 //     newValue = ((value - '0') / 2) + '0';
43 //   else if (value >= 'a' && value <= 'f')
44 //     newValue = ((value + 10 - 'a') / 2) + '0';
45 //   else if (value >= 'A' && value <= 'F')
46 //     newValue = ((value + 10 - 'A') / 2) + '0';
47 //   else
48 //     newValue = value;
49
50 //   return newValue;
51 // }
52
53 static inline NSString *
54 colorForNumber (unsigned int number)
55 {
56   unsigned int index, currentValue;
57   unsigned char colorTable[] = { 1, 1, 1 };
58   NSString *color;
59
60   if (number == 0)
61     color = @"#ccf";
62   else if (number == NSNotFound)
63     color = @"#f00";
64   else
65     {
66       currentValue = number;
67       index = 0;
68       while (currentValue)
69         {
70           if (currentValue & 1)
71             colorTable[index]++;
72           if (index == 3)
73             index = 0;
74           currentValue >>= 1;
75           index++;
76         }
77       color = [NSString stringWithFormat: @"#%2x%2x%2x",
78                         (255 / colorTable[2]) - 1,
79                         (255 / colorTable[1]) - 1,
80                         (255 / colorTable[0]) - 1];
81     }
82
83   return color;
84 }
85
86 @implementation UIxCalendarSelector
87
88 - (id) init
89 {
90   if ((self = [super init]))
91     {
92       calendars = nil;
93       currentCalendar = nil;
94     }
95
96   return self;
97 }
98
99 - (void) dealloc
100 {
101   [calendars release];
102   [currentCalendar release];
103   [super dealloc];
104 }
105
106 - (NSArray *) calendars
107 {
108   NSArray *folders, *roles;
109   SOGoAppointmentFolders *co;
110   SOGoAppointmentFolder *folder;
111   NSMutableDictionary *calendar;
112   unsigned int count, max;
113   NSString *folderName, *fDisplayName;
114   NSNumber *isActive;
115   SOGoUser *user;
116
117   if (!calendars)
118     {
119       co = [self clientObject];
120       user = [[self context] activeUser];
121       folders = [co subFolders];
122       max = [folders count];
123       calendars = [[NSMutableArray alloc] initWithCapacity: max];
124       for (count = 0; count < max; count++)
125         {
126           folder = [folders objectAtIndex: count];
127           roles = [user rolesForObject: folder inContext: [self context]];
128           calendar = [NSMutableDictionary dictionary];
129           folderName = [folder nameInContainer];
130           fDisplayName = [folder displayName];
131           if ([fDisplayName isEqualToString: [co defaultFolderName]])
132             fDisplayName = [self labelForKey: fDisplayName];
133           [calendar setObject: [NSString stringWithFormat: @"/%@", folderName]
134                     forKey: @"id"];
135           [calendar setObject: fDisplayName forKey: @"displayName"];
136           [calendar setObject: folderName forKey: @"folder"];
137           [calendar setObject: colorForNumber (count)
138                     forKey: @"color"];
139           isActive = [NSNumber numberWithBool: [folder isActive]];
140           [calendar setObject: isActive forKey: @"active"];
141           [calendar setObject: [folder ownerInContext: context]
142                     forKey: @"owner"];
143           [calendar setObject: [roles componentsJoinedByString: @","]
144                     forKey: @"roles"];
145           [calendars addObject: calendar];
146         }
147     }
148
149   return calendars;
150 }
151
152 - (void) setCurrentCalendar: (NSDictionary *) newCalendar
153 {
154   ASSIGN (currentCalendar, newCalendar);
155 }
156
157 - (NSDictionary *) currentCalendar
158 {
159   return currentCalendar;
160 }
161
162 - (NSString *) currentCalendarClass
163 {
164   return [currentCalendar
165            keysWithFormat: @"colorBox calendarFolder%{folder}"];
166 }
167
168 - (NSString *) currentCalendarStyle
169 {
170   return [currentCalendar
171            keysWithFormat: @"color: %{color}; background-color: %{color};"];
172 }
173
174 @end /* UIxCalendarSelector */