]> err.no Git - sope/blob - sope-ical/NGiCal/NGVCard.m
f1de3bcfa24f92a2750259d2d0471ebefda7260c
[sope] / sope-ical / NGiCal / NGVCard.m
1 /*
2   Copyright (C) 2005 Helge Hess
3
4   This file is part of SOPE.
5
6   SOPE 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   SOPE 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 SOPE; 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
22 #include "NGVCard.h"
23 #include "NGVCardSaxHandler.h"
24 #include <SaxObjC/SaxXMLReader.h>
25 #include <SaxObjC/SaxXMLReaderFactory.h>
26 #include "common.h"
27
28 @implementation NGVCard
29
30 static id<NSObject,SaxXMLReader> parser = nil; // THREAD
31 static NGVCardSaxHandler         *sax   = nil; // THREAD
32
33 + (id<NSObject,SaxXMLReader>)vCardParser {
34   if (sax == nil)
35     sax = [[NGVCardSaxHandler alloc] init];
36   
37   if (parser == nil) {
38     parser =
39       [[[SaxXMLReaderFactory standardXMLReaderFactory] 
40                              createXMLReaderForMimeType:@"text/x-vcard"]
41                              retain];
42     if (parser == nil) {
43       NSLog(@"%s: did not find a parser for text/x-vcard !",
44             __PRETTY_FUNCTION__);
45       return nil;
46     }
47     
48     [parser setContentHandler:sax];
49     [parser setErrorHandler:sax];
50   }
51   
52   return parser;
53 }
54
55 + (NSArray *)parseVCardsFromSource:(id)_src {
56   static id<NSObject,SaxXMLReader> parser;
57   
58   if ((parser = [self vCardParser]) == nil)
59     return nil;
60   
61   [parser parseFromSource:_src];
62   return [sax vCards];
63 }
64
65 - (id)initWithUid:(NSString *)_uid version:(NSString *)_version {
66   if ((self = [super init]) != nil) {
67     self->uid     = [_uid     copy];
68     self->version = [_version copy];
69   }
70   return self;
71 }
72 - (id)init {
73   return [self initWithUid:nil version:@"3.0"];
74 }
75
76 - (void)dealloc {
77   [self->vClass  release];
78   [self->prodID  release];
79   [self->x       release];
80   [self->tel     release];
81   [self->adr     release];
82   [self->email   release];
83   [self->label   release];
84   [self->version release];
85   [self->uid     release];
86   [super dealloc];
87 }
88
89 /* accessors */
90
91 - (NSString *)version {
92   return self->version;
93 }
94
95 - (void)setUid:(NSString *)_uid {
96   ASSIGNCOPY(self->uid, _uid);
97 }
98 - (NSString *)uid {
99   return self->uid;
100 }
101
102 - (void)setVClass:(NSString *)_vClass {
103   ASSIGNCOPY(self->vClass, _vClass);
104 }
105 - (NSString *)vClass {
106   return self->vClass;
107 }
108
109 - (void)setProdID:(NSString *)_prodID {
110   ASSIGNCOPY(self->prodID, _prodID);
111 }
112 - (NSString *)prodID {
113   return self->prodID;
114 }
115
116 - (void)setTel:(NSArray *)_tel {
117   ASSIGNCOPY(self->tel, _tel);
118 }
119 - (NSArray *)tel {
120   return self->tel;
121 }
122
123 - (void)setAdr:(NSArray *)_adr {
124   ASSIGNCOPY(self->adr, _adr);
125 }
126 - (NSArray *)adr {
127   return self->adr;
128 }
129
130 - (void)setEmail:(NSArray *)_email {
131   ASSIGNCOPY(self->email, _email);
132 }
133 - (NSArray *)email {
134   return self->email;
135 }
136
137 - (void)setLabel:(NSArray *)_label {
138   ASSIGNCOPY(self->label, _label);
139 }
140 - (NSArray *)label {
141   return self->label;
142 }
143
144 - (void)setX:(NSDictionary *)_dict {
145   ASSIGNCOPY(self->x, _dict);
146 }
147 - (NSDictionary *)x {
148   return self->x;
149 }
150
151 /* description */
152
153 - (void)appendAttributesToDescription:(NSMutableString *)_ms {
154   if (self->uid   != nil) [_ms appendFormat:@" uid='%@'", self->uid];
155   
156   if ([self->tel   count] > 0) [_ms appendFormat:@" tel=%@",   self->tel];
157   if ([self->adr   count] > 0) [_ms appendFormat:@" adr=%@",   self->adr];
158   if ([self->email count] > 0) [_ms appendFormat:@" email=%@", self->email];
159   if ([self->label count] > 0) [_ms appendFormat:@" label=%@", self->label];
160   if ([self->x     count] > 0) [_ms appendFormat:@" x=%@",     self->x];
161 }
162
163 - (NSString *)description {
164   NSMutableString *str = nil;
165   
166   str = [NSMutableString stringWithCapacity:64];
167   [str appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
168   [self appendAttributesToDescription:str];
169   [str appendString:@">"];
170   return str;
171 }
172
173 @end /* NGVCard */