]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/Anais/AnaisAttendeeSelector.m
presumably bugfixing, but instead disabled a questionable feature
[scalable-opengroupware.org] / SOGo / UI / Anais / AnaisAttendeeSelector.m
1 /*
2   Copyright (C) 2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 // $Id: AnaisSelector.m 184 2004-08-11 17:46:10Z znek $
22
23 #include <SOGoUI/UIxComponent.h>
24
25 /*
26   AnaisAttendeeSelector
27   
28   Select a set of attendees using Anais.
29   
30   Bindings:
31     attendees       - array of iCalPerson objects
32     selectorID      - string to be used as the identifier for form/JS elements
33     withCN          - show CN of person (eg disabled for resources)
34     withAddressBook - allow selection from private address book (default:NO)
35     division        - Anais division
36     emailForUser    - default EMail
37     cnForUser       - default CN
38     role            - role for submitted attendees
39
40   Sample:
41     <var:component className="AnaisAttendeeSelector"
42                    selectorID="participant"
43                    attendees="attendees"
44                    const:division="CC"
45                   />
46 */
47
48 @class iCalPerson;
49
50 @interface AnaisAttendeeSelector : UIxComponent
51 {
52   NSString   *selectorID;
53   NSString   *division;
54   NSArray    *attendees;
55   iCalPerson *attendee;
56   NSString   *emailForUser;
57   NSString   *cnForUser;
58   NSString   *role;
59   struct {
60     int withCN:1;
61     int withAddressBook:1;
62     int reserved:30;
63   } flags;
64 }
65
66 @end
67
68 #include <NGiCal/NGiCal.h>
69 #include <SOGoLogic/AgenorUserManager.h>
70 #include <Scheduler/iCalPerson+UIx.h>
71 #include <NGObjWeb/WOContext.h>
72 #include <NGObjWeb/WORequest.h>
73 #include "common.h"
74
75 @implementation AnaisAttendeeSelector
76
77 static BOOL debugOn = NO;
78
79 + (void)initialize {
80   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
81   
82   debugOn = [ud boolForKey:@"AnaisDebugEnabled"];
83 }
84
85 - (void)dealloc {
86   [self->role         release];
87   [self->emailForUser release];
88   [self->cnForUser    release];
89   [self->division     release];
90   [self->selectorID   release];
91   [self->attendee     release];
92   [self->attendees    release];
93   [super dealloc];
94 }
95
96 /* notifications */
97
98 - (void)sleep {
99   [self->attendee release]; self->attendee = nil;
100   [super sleep];
101 }
102
103 /* accessors */
104
105 - (void)setAttendees:(NSArray *)_attendees {
106   ASSIGN(self->attendees, _attendees);
107 }
108 - (NSArray *)attendees {
109   return self->attendees;
110 }
111 - (BOOL)hasAttendees {
112   return [[self attendees] count] > 0 ? YES : NO;
113 }
114 - (BOOL)hasNoAttendees {
115   return [[self attendees] count] == 0 ? YES : NO;
116 }
117
118 - (void)setWithCN:(BOOL)_flag {
119   self->flags.withCN = _flag ? 1 : 0;
120 }
121 - (BOOL)withCN {
122   return self->flags.withCN ? YES : NO;
123 }
124
125 - (void)setWithAddressBook:(BOOL)_flag {
126   self->flags.withAddressBook = _flag ? 1 : 0;
127 }
128 - (BOOL)withAddressBook {
129   return self->flags.withAddressBook ? YES : NO;
130 }
131
132 - (void)setAttendee:(iCalPerson *)_attendee {
133   ASSIGN(self->attendee, _attendee);
134 }
135 - (iCalPerson *)attendee {
136   return self->attendee;
137 }
138
139 - (void)setDivision:(NSString *)_value {
140   ASSIGNCOPY(self->division, _value);
141 }
142 - (NSString *)division {
143   return self->division;
144 }
145
146 - (void)setRole:(NSString *)_value {
147   ASSIGNCOPY(self->role, _value);
148 }
149 - (NSString *)role {
150   return self->role;
151 }
152
153 - (void)setEmailForUser:(NSString *)_value {
154   ASSIGNCOPY(self->emailForUser, _value);
155 }
156 - (NSString *)emailForUser {
157   return self->emailForUser;
158 }
159
160 - (void)setCnForUser:(NSString *)_value {
161   ASSIGNCOPY(self->cnForUser, _value);
162 }
163 - (NSString *)cnForUser {
164   return self->cnForUser;
165 }
166
167 - (BOOL)showDefaultAttendees {
168   return [self hasNoAttendees] && [[self emailForUser] length] > 0 ? YES : NO;
169 }
170
171 /* email, cn */
172
173 - (NSString *)combinedCNAndEmailForUser {
174   NSString *e, *c;
175
176   e = [self emailForUser];
177   c = [self cnForUser];
178   return [[e stringByAppendingString:@";"] stringByAppendingString:c];
179 }
180
181 - (NSString *)combinedCNAndEmail {
182   NSString *e, *c;
183   
184   e = [[self attendee] rfc822Email];
185   c = [[self attendee] cnForDisplay];
186   return [[e stringByAppendingString:@";"] stringByAppendingString:c];
187 }
188
189 /* id accessors */
190
191 - (void)setSelectorID:(NSString *)_value {
192   ASSIGNCOPY(self->selectorID, _value);
193 }
194 - (NSString *)selectorID {
195   return self->selectorID;
196 }
197 - (NSString *)capitalizedSelectorID {
198   return [[self selectorID] capitalizedString];
199 }
200
201 - (NSString *)windowId {
202   /* eg: 'Resources' */
203   return [[self capitalizedSelectorID] stringByAppendingString:@"s"];
204 }
205 - (NSString *)callbackName {
206   /* eg: 'addResource' */
207   return [@"add" stringByAppendingString:[self capitalizedSelectorID]];
208 }
209 - (NSString *)tableId {
210   /* eg: 'resources' */
211   return [[self selectorID] stringByAppendingString:@"s"];
212 }
213 - (NSString *)checkboxId {
214   /* eg: 'resources' */
215   return [self tableId]; /* TODO: znek, is this ok? */
216 }
217
218 /* handling requests */
219
220 - (NSArray *)getICalPersonsFromFormValues:(NSArray *)_values {
221   unsigned i, count;
222   NSMutableArray *result;
223
224   count = [_values count];
225   result = [NSMutableArray arrayWithCapacity:count];
226   for (i = 0; i < count; i++) {
227     NSString   *pString, *email, *cn;
228     NSRange    r;
229     iCalPerson *p;
230     
231     pString = [_values objectAtIndex:i];
232     if ([pString length] == 0)
233       continue;
234     
235     /* delimiter between email and cn */
236     r = [pString rangeOfString:@";"];
237     if (r.length > 0) {
238       /* sample: 'test.et.di.cete-lyon@equipement.gouv.fr;test' */
239       email = [pString substringToIndex:r.location];
240       cn    = [pString substringFromIndex:NSMaxRange(r)];
241       if ([cn length] == 0) cn = nil;
242     }
243     else {
244       email = pString;
245       cn    = nil;
246     }
247     if (cn == nil) {
248       /* fallback */
249       AgenorUserManager *um = [AgenorUserManager sharedUserManager];
250       cn = [um getCNForUID:[um getUIDForEmail:email]];
251     }
252     
253     p = [[iCalPerson alloc] init];
254     [p setEmail:[@"mailto:" stringByAppendingString:email]];
255     if ([cn isNotNull]) [p setCn:cn];
256     
257     /* see RFC2445, sect. 4.2.16 for details */
258     [p setRole:[self role]];
259     [result addObject:p];
260     [p release];
261   }
262   return result;
263 }
264 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
265   /* OK, we have a special form value processor */
266   NSArray *tmp;
267   
268   [self debugWithFormat:@"Note: will take values ..."];
269   
270   tmp = [self getICalPersonsFromFormValues:
271                 [_rq formValuesForKey:[self checkboxId]]];
272   [self debugWithFormat:@"  got %i attendees: %@", [tmp count], tmp];
273   [self setAttendees:tmp];
274 }
275
276 /* response generation */
277
278 - (NSString *)jsCode {
279   NSMutableString *ms;
280   WOContext *ctx;
281   NSString *s;
282
283   ms = [NSMutableString stringWithCapacity:128];
284   
285   ctx = [self context];
286   if (![[ctx valueForKey:@"HasAddTableAnaisAttendeeSelector"] boolValue]) {
287     static NSString *script = \
288       @"function addToTable(tableId, type, cn, dn, email, uid, sn) {\n"
289       @"  var test = document.getElementById(email);"
290       @"  if(test)"
291       @"    return;"
292       @""
293       @"  var table = document.getElementById(tableId);"
294       @"  var tr = document.createElement('tr');"
295       @"  var td, checkbox, text;"
296       @""
297       @"  td = document.createElement('td');"
298       @"  checkbox = document.createElement('input');"
299       @"  checkbox.setAttribute('type', 'checkbox');"
300       @"  checkbox.setAttribute('checked', 'checked');"
301       @"  checkbox.setAttribute('value', email + ';' + cn);"
302       @"  checkbox.setAttribute('id', email);"
303       @"  checkbox.setAttribute('name', tableId);"
304       @"  td.appendChild(checkbox);"
305       @"  tr.appendChild(td);"
306       @"  td = document.createElement('td');"
307       @"  text = document.createTextNode(cn);"
308       @"  td.appendChild(text);"
309       @"  tr.appendChild(td);"
310       @"  table.appendChild(tr);"
311       @"}\n";
312     [ms appendString:script];
313     
314     [ctx takeValue:[NSNumber numberWithBool:YES]
315          forKey:@"HasAddTableAnaisAttendeeSelector"];
316   }
317   
318   s = 
319     @"function %@(type, cn, dn, email, uid, sn) {\n"
320     @"  addToTable('%@', type, cn, dn, email, uid, sn);\n"
321     @"}\n";
322   [ms appendFormat:s, [self callbackName], [self tableId]];
323   return ms;
324 }
325
326 /* debugging */
327
328 - (BOOL)isDebuggingEnabled {
329   return debugOn;
330 }
331
332 @end /* AnaisAttendeeSelector */