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