]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxCalendarSelector.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1036 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/AgenorUserManager.h>
32 #import <SOGo/SOGoUser.h>
33 #import <SOGoUI/UIxComponent.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 = [NSMutableDictionary new];
94       calendarFolders = nil;
95       currentCalendarFolder = nil;
96     }
97
98   return self;
99 }
100
101 - (void) dealloc
102 {
103   [calendarFolders release];
104   [currentCalendarFolder release];
105   [colors release];
106   [super dealloc];
107 }
108
109 - (void) setCalendarFolders: (NSArray *) newCalendarFolders
110 {
111   NSEnumerator *newFolders;
112   NSDictionary *currentFolder;
113   unsigned int count;
114
115   ASSIGN (calendarFolders, newCalendarFolders);
116
117   newFolders = [calendarFolders objectEnumerator];
118   currentFolder = [newFolders nextObject];
119   count = 0;
120   while (currentFolder)
121     {
122       [colors setObject: colorForNumber (count)
123               forKey: [currentFolder objectForKey: @"folder"]];
124       count++;
125       currentFolder = [newFolders nextObject];
126     }
127 }
128
129 - (NSArray *) calendarFolders
130 {
131   return calendarFolders;
132 }
133
134 - (void) setCurrentCalendarFolder: (NSDictionary *) newCurrentCalendarFolder
135 {
136   ASSIGN (currentCalendarFolder, newCurrentCalendarFolder);
137 }
138
139 - (NSDictionary *) currentCalendarFolder
140 {
141   return currentCalendarFolder;
142 }
143
144 - (NSString *) currentCalendarSpanBG
145 {
146   NSString *colorKey;
147
148   colorKey = [currentCalendarFolder objectForKey: @"folder"];
149
150   return [colors objectForKey: colorKey];
151 }
152
153 - (NSString *) currentCalendarLogin
154 {
155   NSArray *parts;
156
157   parts = [[currentCalendarFolder objectForKey: @"folder"]
158             componentsSeparatedByString: @":"];
159
160   return (([parts count] > 1)
161           ? [parts objectAtIndex: 0]
162           : [[context activeUser] login]);
163 }
164
165 - (NSString *) currentCalendarStyle
166 {
167   NSString *color;
168
169   color = [self currentCalendarSpanBG];
170
171   return [NSString stringWithFormat: @"color: %@; background-color: %@;",
172                    color, color];
173 }
174
175 @end /* UIxCalendarSelector */