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