]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxCalendarSelector.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1127 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/NSString.h>
26 #import <Foundation/NSUserDefaults.h>
27
28 #import <NGExtensions/NGExtensions.h>
29 #import <NGCards/iCalPerson.h>
30
31 #import <SOGo/SOGoUser.h>
32 #import <SOGoUI/UIxComponent.h>
33 #import <Appointments/SOGoAppointmentFolder.h>
34
35 #import "UIxCalendarSelector.h"
36
37 static inline char
38 darkenedColor (const char value)
39 {
40   char newValue;
41
42   if (value >= '0' && value <= '9')
43     newValue = ((value - '0') / 2) + '0';
44   else if (value >= 'a' && value <= 'f')
45     newValue = ((value + 10 - 'a') / 2) + '0';
46   else if (value >= 'A' && value <= 'F')
47     newValue = ((value + 10 - 'A') / 2) + '0';
48   else
49     newValue = value;
50
51   return newValue;
52 }
53
54 static inline NSString *
55 colorForNumber (unsigned int number)
56 {
57   unsigned int index, currentValue;
58   unsigned char colorTable[] = { 1, 1, 1 };
59   NSString *color;
60
61   if (number == 0)
62     color = @"#ccf";
63   else if (number == NSNotFound)
64     color = @"#f00";
65   else
66     {
67       currentValue = number;
68       index = 0;
69       while (currentValue)
70         {
71           if (currentValue & 1)
72             colorTable[index]++;
73           if (index == 3)
74             index = 0;
75           currentValue >>= 1;
76           index++;
77         }
78       color = [NSString stringWithFormat: @"#%2x%2x%2x",
79                         (255 / colorTable[2]) - 1,
80                         (255 / colorTable[1]) - 1,
81                         (255 / colorTable[0]) - 1];
82     }
83
84   return color;
85 }
86
87 @implementation UIxCalendarSelector
88
89 - (id) init
90 {
91   if ((self = [super init]))
92     {
93       colors = nil;
94       currentCalendarFolder = nil;
95     }
96
97   return self;
98 }
99
100 - (void) dealloc
101 {
102   [currentCalendarFolder release];
103   [colors release];
104   [super dealloc];
105 }
106
107 - (NSArray *) calendarFolders
108 {
109   NSArray *calendarFolders;
110   NSEnumerator *newFolders;
111   NSDictionary *currentFolder;
112   unsigned int count;
113
114   calendarFolders = [[self clientObject] calendarFolders];
115   if (!colors)
116     {
117       colors = [NSMutableDictionary new];
118       count = 0;
119       newFolders = [calendarFolders objectEnumerator];
120       currentFolder = [newFolders nextObject];
121       while (currentFolder)
122         {
123           [colors setObject: colorForNumber (count)
124                   forKey: [currentFolder objectForKey: @"folder"]];
125           count++;
126           currentFolder = [newFolders nextObject];
127         }
128     }
129
130   return calendarFolders;
131 }
132
133 - (void) setCurrentCalendarFolder: (NSDictionary *) newCurrentCalendarFolder
134 {
135   ASSIGN (currentCalendarFolder, newCurrentCalendarFolder);
136 }
137
138 - (NSDictionary *) currentCalendarFolder
139 {
140   return currentCalendarFolder;
141 }
142
143 - (NSString *) currentCalendarSpanBG
144 {
145   NSString *colorKey;
146
147   colorKey = [currentCalendarFolder objectForKey: @"folder"];
148
149   return [colors objectForKey: colorKey];
150 }
151
152 - (NSString *) currentCalendarLogin
153 {
154   NSArray *parts;
155
156   parts = [[currentCalendarFolder objectForKey: @"folder"]
157             componentsSeparatedByString: @":"];
158
159   return (([parts count] > 1)
160           ? [parts objectAtIndex: 0]
161           : [[context activeUser] login]);
162 }
163
164 - (NSString *) currentCalendarStyle
165 {
166   NSString *color;
167
168   color = [self currentCalendarSpanBG];
169
170   return [NSString stringWithFormat: @"color: %@; background-color: %@;",
171                    color, color];
172 }
173
174 @end /* UIxCalendarSelector */