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