]> err.no Git - scalable-opengroupware.org/blob - UI/Contacts/UIxContactView.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1026 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / Contacts / UIxContactView.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$
22
23 #import <Contacts/SOGoContactObject.h>
24
25 #import <NGCards/NGVCard.h>
26 #import <NGCards/CardElement.h>
27 #import <NGCards/NSArray+NGCards.h>
28 #import <NGExtensions/NSString+Ext.h>
29
30 #import "common.h"
31
32 #import "UIxContactView.h"
33
34 @implementation UIxContactView
35
36 /* accessors */
37
38 - (NSString *)tabSelection {
39   NSString *selection;
40     
41   selection = [self queryParameterForKey:@"tab"];
42   if (selection == nil)
43     selection = @"attributes";
44   return selection;
45 }
46
47 - (NSString *) _cardStringWithLabel: (NSString *) label
48                               value: (NSString *) value
49 {
50   NSMutableString *cardString;
51
52   cardString = [NSMutableString new];
53   [cardString autorelease];
54
55   if (value && [value length] > 0)
56     {
57       if (label)
58         [cardString appendFormat: @"%@%@<br />\n",
59                     [self labelForKey: label], value];
60       else
61         [cardString appendFormat: @"%@<br />\n", value];
62     }
63
64   return cardString;
65 }
66
67 - (NSString *) contactCardTitle
68 {
69   return [NSString stringWithFormat:
70                      [self labelForKey: @"Card for %@"],
71                    [card fn]];
72 }
73
74 - (NSString *) displayName
75 {
76   return [self _cardStringWithLabel: @"Display Name: "
77                value: [card fn]];
78 }
79
80 - (NSString *) nickName
81 {
82   return [self _cardStringWithLabel: @"Nickname: "
83                value: [card nickname]];
84 }
85
86 - (NSString *) preferredEmail
87 {
88   NSString *email, *mailTo;
89
90   email = [card preferredEMail];
91   if (email && [email length] > 0)
92     mailTo = [NSString stringWithFormat: @"<a href=\"mailto:%@\""
93                        @" onclick=\"return onContactMailTo(this);\">"
94                        @"%@</a>", email, email];
95   else
96     mailTo = nil;
97
98   return [self _cardStringWithLabel: @"Email Address: "
99                value: mailTo];
100 }
101
102 - (NSString *) preferredTel
103 {
104   return [self _cardStringWithLabel: @"Phone Number: "
105                value: [card preferredTel]];
106 }
107
108 - (NSString *) preferredAddress
109 {
110   return @"";
111 }
112
113 - (BOOL) hasTelephones
114 {
115   if (!phones)
116     phones = [card childrenWithTag: @"tel"];
117
118   return ([phones count] > 0);
119 }
120
121 - (NSString *) _phoneOfType: (NSString *) aType
122                   withLabel: (NSString *) aLabel
123 {
124   NSArray *elements;
125   NSString *phone;
126
127   elements = [phones cardElementsWithAttribute: @"type"
128                      havingValue: aType];
129
130   if ([elements count] > 0)
131     phone = [[elements objectAtIndex: 0] value: 0];
132   else
133     phone = nil;
134
135   return [self _cardStringWithLabel: aLabel value: phone];
136 }
137
138 - (NSString *) workPhone
139 {
140   return [self _phoneOfType: @"work" withLabel: @"Work: "];
141 }
142
143 - (NSString *) homePhone
144 {
145   return [self _phoneOfType: @"home" withLabel: @"Home: "];
146 }
147
148 - (NSString *) fax
149 {
150   return [self _phoneOfType: @"fax" withLabel: @"Fax: "];
151 }
152
153 - (NSString *) mobile
154 {
155   return [self _phoneOfType: @"cell" withLabel: @"Mobile: "];
156 }
157
158 - (NSString *) pager
159 {
160   return [self _phoneOfType: @"pager" withLabel: @"Pager: "];
161 }
162
163 - (BOOL) hasHomeInfos
164 {
165   BOOL result;
166   NSArray *elements;
167
168   elements = [card childrenWithTag: @"adr"
169                    andAttribute: @"type"
170                    havingValue: @"home"];
171   if ([elements count] > 0)
172     {
173       result = YES;
174       homeAdr = [elements objectAtIndex: 0];
175     }
176   else
177     result = ([[card childrenWithTag: @"url"
178                      andAttribute: @"type"
179                      havingValue: @"home"] count] > 0);
180
181   return result;
182 }
183
184 - (NSString *) homePobox
185 {
186   return [self _cardStringWithLabel: nil value: [homeAdr value: 0]];
187 }
188
189 - (NSString *) homeExtendedAddress
190 {
191   return [self _cardStringWithLabel: nil value: [homeAdr value: 1]];
192 }
193
194 - (NSString *) homeStreetAddress
195 {
196   return [self _cardStringWithLabel: nil value: [homeAdr value: 2]];
197 }
198
199 - (NSString *) homeCityAndProv
200 {
201   NSString *city, *prov;
202   NSMutableString *data;
203
204   city = [homeAdr value: 3];
205   prov = [homeAdr value: 4];
206
207   data = [NSMutableString new];
208   [data autorelease];
209   [data appendString: city];
210   if ([city length] > 0 && [prov length] > 0)
211     [data appendString: @", "];
212   [data appendString: prov];
213
214   return [self _cardStringWithLabel: nil value: data];
215 }
216
217 - (NSString *) homePostalCodeAndCountry
218 {
219   NSString *postalCode, *country;
220   NSMutableString *data;
221
222   postalCode = [homeAdr value: 5];
223   country = [homeAdr value: 6];
224
225   data = [NSMutableString new];
226   [data autorelease];
227   [data appendString: postalCode];
228   if ([postalCode length] > 0 && [country length] > 0)
229     [data appendFormat: @", ", country];
230   [data appendString: country];
231
232   return [self _cardStringWithLabel: nil value: data];
233 }
234
235 - (NSString *) _urlOfType: (NSString *) aType
236 {
237   NSArray *elements;
238   NSString *data, *url;
239
240   elements = [card childrenWithTag: @"url"
241                    andAttribute: @"type"
242                    havingValue: aType];
243   if ([elements count] > 0)
244     {
245       url = [[elements objectAtIndex: 0] value: 0];
246       data = [NSString stringWithFormat:
247                          @"<a href=\"%@\">%@</a>",
248                        url, url];
249     }
250   else
251     data = nil;
252
253   return [self _cardStringWithLabel: nil value: data];
254 }
255
256 - (NSString *) homeUrl
257 {
258   return [self _urlOfType: @"home"];
259 }
260
261 - (BOOL) hasWorkInfos
262 {
263   BOOL result;
264   NSArray *elements;
265
266   elements = [card childrenWithTag: @"adr"
267                    andAttribute: @"type"
268                    havingValue: @"work"];
269   if ([elements count] > 0)
270     {
271       result = YES;
272       workAdr = [elements objectAtIndex: 0];
273     }
274   else
275     result = (([[card childrenWithTag: @"url"
276                       andAttribute: @"type"
277                      havingValue: @"work"] count] > 0)
278               || [[card childrenWithTag: @"org"] count] > 0);
279
280   return result;
281 }
282
283 - (NSString *) workTitle
284 {
285   return [self _cardStringWithLabel: nil value: [card title]];
286 }
287
288 - (NSString *) workService
289 {
290   NSArray *org, *orgServices;
291   NSRange aRange;
292   NSString *services;
293
294   org = [card org];
295   if (org && [org count] > 1)
296     {
297       aRange = NSMakeRange (1, [org count] - 1);
298       orgServices = [org subarrayWithRange: aRange];
299       services = [orgServices componentsJoinedByString: @", "];
300     }
301   else
302     services = nil;
303
304   return [self _cardStringWithLabel: nil value: services];
305 }
306
307 - (NSString *) workCompany
308 {
309   NSArray *org;
310   NSString *company;
311
312   org = [card org];
313   if (org && [org count] > 0)
314     company = [org objectAtIndex: 0];
315   else
316     company = nil;
317
318   return [self _cardStringWithLabel: nil value: company];
319 }
320
321 - (NSString *) workPobox
322 {
323   return [self _cardStringWithLabel: nil value: [workAdr value: 0]];
324 }
325
326 - (NSString *) workExtendedAddress
327 {
328   return [self _cardStringWithLabel: nil value: [workAdr value: 1]];
329 }
330
331 - (NSString *) workStreetAddress
332 {
333   return [self _cardStringWithLabel: nil value: [workAdr value: 2]];
334 }
335
336 - (NSString *) workCityAndProv
337 {
338   NSString *city, *prov;
339   NSMutableString *data;
340
341   city = [workAdr value: 3];
342   prov = [workAdr value: 4];
343
344   data = [NSMutableString new];
345   [data autorelease];
346   [data appendString: city];
347   if ([city length] > 0 && [prov length] > 0)
348     [data appendString: @", "];
349   [data appendString: prov];
350
351   return [self _cardStringWithLabel: nil value: data];
352 }
353
354 - (NSString *) workPostalCodeAndCountry
355 {
356   NSString *postalCode, *country;
357   NSMutableString *data;
358
359   postalCode = [workAdr value: 5];
360   country = [workAdr value: 6];
361
362   data = [NSMutableString new];
363   [data autorelease];
364   [data appendString: postalCode];
365   if ([postalCode length] > 0 && [country length] > 0)
366     [data appendFormat: @", ", country];
367   [data appendString: country];
368
369   return [self _cardStringWithLabel: nil value: data];
370 }
371
372 - (NSString *) workUrl
373 {
374   return [self _urlOfType: @"home"];
375 }
376
377 - (BOOL) hasOtherInfos
378 {
379   return ([[card note] length] > 0
380           || [[card bday] length] > 0
381           || [[card tz] length] > 0);
382 }
383
384 - (NSString *) bday
385 {
386   return [self _cardStringWithLabel: @"Birthday: " value: [card bday]];
387 }
388
389 - (NSString *) tz
390 {
391   return [self _cardStringWithLabel: @"Timezone: " value: [card tz]];
392 }
393
394 - (NSString *) note
395 {
396   NSString *note;
397
398   note = [card note];
399   if (note)
400     {
401       note = [note stringByReplacingString: @"\r\n"
402                    withString: @"<br />"];
403       note = [note stringByReplacingString: @"\n"
404                    withString: @"<br />"];
405     }
406
407   return [self _cardStringWithLabel: @"Note: " value: note];
408 }
409
410 /* hrefs */
411
412 - (NSString *) completeHrefForMethod: (NSString *) _method
413                        withParameter: (NSString *) _param
414                               forKey: (NSString *) _key
415 {
416   NSString *href;
417
418   [self setQueryParameter:_param forKey:_key];
419   href = [self completeHrefForMethod:[self ownMethodName]];
420   [self setQueryParameter:nil forKey:_key];
421
422   return href;
423 }
424
425 - (NSString *)attributesTabLink {
426   return [self completeHrefForMethod:[self ownMethodName]
427                withParameter:@"attributes"
428                forKey:@"tab"];
429 }
430 - (NSString *)debugTabLink {
431   return [self completeHrefForMethod:[self ownMethodName]
432                withParameter:@"debug"
433                forKey:@"tab"];
434 }
435
436 /* action */
437
438 - (id <WOActionResults>) vcardAction
439 {
440   WOResponse *response;
441
442   card = [[self clientObject] vCard];
443   if (card)
444     {
445       response = [WOResponse new];
446       [response autorelease];
447       [response setHeader: @"text/vcard" forKey: @"Content-type"];
448       [response appendContentString: [card versitString]];
449     }
450   else
451     return [NSException exceptionWithHTTPStatus: 404 /* Not Found */
452                         reason:@"could not locate contact"];
453
454   return response;
455 }
456
457 - (id <WOActionResults>) defaultAction
458 {
459   card = [[self clientObject] vCard];
460   if (card)
461     {
462       phones = nil;
463       homeAdr = nil;
464       workAdr = nil;
465     }
466   else
467     return [NSException exceptionWithHTTPStatus: 404 /* Not Found */
468                         reason: @"could not locate contact"];
469
470   return self;
471 }
472
473 - (BOOL) isDeletableClientObject
474 {
475   return [[self clientObject] respondsToSelector: @selector(delete)];
476 }
477
478 - (id) deleteAction
479 {
480   NSException *ex;
481   id url;
482
483   if (![self isDeletableClientObject])
484     /* return 400 == Bad Request */
485     return [NSException exceptionWithHTTPStatus:400
486                         reason:@"method cannot be invoked on "
487                         @"the specified object"];
488
489   ex = [[self clientObject] delete];
490   if (ex)
491     {
492     // TODO: improve error handling
493       [self debugWithFormat:@"failed to delete: %@", ex];
494
495       return ex;
496     }
497
498   url = [[[self clientObject] container] baseURLInContext:[self context]];
499
500   return [self redirectToLocation:url];
501 }
502
503 @end /* UIxContactView */