]> err.no Git - sope/blob - sope-ical/NGiCal/NSString+ICal.m
added iCal renderer
[sope] / sope-ical / NGiCal / NSString+ICal.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
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 #include "NSString+ICal.h"
24 #include "common.h"
25 #include <ical.h>
26
27 @implementation NSString(ICalCString)
28
29 - (const char *)icalCString {
30   NSStringEncoding enc;
31   
32   enc = [[self class] libicalStringEncoding];
33   
34   if (enc == NSUTF8StringEncoding)
35     return [self UTF8String];
36   else if (enc == [NSString defaultCStringEncoding])
37     return [self cString];
38   else
39     return [[self dataUsingEncoding:enc] bytes];
40 }
41
42 - (NSString *)icalString {
43   return self;
44 }
45
46 @end /* NSString(ICalCString) */
47
48 @implementation NSObject(TemporaryStringInit)
49
50 + (NSStringEncoding)libicalStringEncoding {
51   return NSUTF8StringEncoding;
52 }
53
54 - (id)initWithICalCString:(const char *)_cstr {
55   NSStringEncoding enc;
56
57   if (_cstr == NULL) {
58     RELEASE(self);
59     return nil;
60   }
61   
62   enc = [[self class] libicalStringEncoding];
63   
64   if (enc == NSUTF8StringEncoding)
65     return [(NSString *)self initWithUTF8String:_cstr];
66   else if (enc == [[self class] defaultCStringEncoding])
67     return [(NSString *)self initWithCString:_cstr];
68   else {
69     NSData *d;
70     
71     d = [[NSData alloc] initWithBytes:_cstr length:strlen(_cstr)];
72     self = [(NSString *)self initWithData:d encoding:enc];
73     RELEASE(d);
74     
75     return self;
76   }
77   
78   return nil;
79 }
80
81 - (id)initWithICalValueHandle:(icalvalue *)_handle {
82   const char *s;
83   
84   if (_handle == NULL) {
85     RELEASE(self);
86     return nil;
87   }
88   if ((s = icalvalue_as_ical_string(_handle)) == NULL) {
89     RELEASE(self);
90     return nil;
91   }
92   return [self initWithICalCString:s];
93 }
94
95 - (id)initWithICalValueOfProperty:(icalproperty *)_prop {
96   icalvalue *val;
97
98   if (_prop == nil) {
99     RELEASE(self);
100     return nil;
101   }
102   
103   if ((val = icalproperty_get_value(_prop)) == NULL) {
104     NSLog(@"%s: ical property has no value ??", __PRETTY_FUNCTION__);
105     RELEASE(self);
106     return nil;
107   }
108   
109   return [self initWithICalValueHandle:val];
110 }
111
112 @end /* NSObject(TemporaryStringInit) */
113