1 /* UIxCalendarSelector.m - this file is part of SOGo
3 * Copyright (C) 2007, 2008 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
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)
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.
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.
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSDictionary.h>
25 #import <Foundation/NSValue.h>
27 #import <NGObjWeb/WOResponse.h>
29 #import <SOGo/NSDictionary+Utilities.h>
31 #import <SoObjects/SOGo/NSArray+Utilities.h>
32 #import <SoObjects/SOGo/SOGoUser.h>
34 #import <Appointments/SOGoAppointmentFolder.h>
35 #import <Appointments/SOGoAppointmentFolders.h>
37 #import "UIxCalendarSelector.h"
40 // darkenedColor (const char value)
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';
56 static inline NSString *
57 colorForNumber (unsigned int number)
59 unsigned int index, currentValue;
60 unsigned char colorTable[] = { 1, 1, 1 };
65 else if (number == NSNotFound)
69 currentValue = number;
80 color = [NSString stringWithFormat: @"#%2x%2x%2x",
81 (255 / colorTable[2]) - 1,
82 (255 / colorTable[1]) - 1,
83 (255 / colorTable[0]) - 1];
89 @implementation UIxCalendarSelector
93 if ((self = [super init]))
96 currentCalendar = nil;
105 [currentCalendar release];
109 - (NSArray *) calendars
111 NSArray *folders, *roles;
112 SOGoAppointmentFolders *co;
113 SOGoAppointmentFolder *folder;
114 NSMutableDictionary *calendar;
115 unsigned int count, max;
116 NSString *folderName, *fDisplayName;
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++)
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]
138 [calendar setObject: fDisplayName forKey: @"displayName"];
139 [calendar setObject: folderName forKey: @"folder"];
140 [calendar setObject: colorForNumber (count)
142 isActive = [NSNumber numberWithBool: [folder isActive]];
143 [calendar setObject: isActive forKey: @"active"];
144 [calendar setObject: [folder ownerInContext: context]
146 [calendar setObject: [roles componentsJoinedByString: @","]
148 [calendars addObject: calendar];
155 - (void) setCurrentCalendar: (NSDictionary *) newCalendar
157 ASSIGN (currentCalendar, newCalendar);
160 - (NSDictionary *) currentCalendar
162 return currentCalendar;
165 - (NSString *) currentCalendarClass
167 return [currentCalendar
168 keysWithFormat: @"colorBox calendarFolder%{folder}"];
171 - (NSString *) currentCalendarStyle
173 return [currentCalendar
174 keysWithFormat: @"color: %{color}; background-color: %{color};"];
177 - (WOResponse *) calendarsListAction
179 WOResponse *response;
181 response = [self responseWithStatus: 200];
182 [response setHeader: @"text/plain; charset=utf-8"
183 forKey: @"content-type"];
184 [response appendContentString: [[self calendars] jsonRepresentation]];
189 @end /* UIxCalendarSelector */