From: helge Date: Wed, 13 Jul 2005 12:41:40 +0000 (+0000) Subject: enhanced contact extractor to be able to deal with vCards X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6aa2021f7cd73eeab5f163bf670eeb93d861c196;p=scalable-opengroupware.org enhanced contact extractor to be able to deal with vCards git-svn-id: http://svn.opengroupware.org/SOGo/trunk@735 d1b88da0-ebda-0310-925b-ed51d893ca5b --- diff --git a/SOGo/OGoContentStore/ChangeLog b/SOGo/OGoContentStore/ChangeLog index dc8f3308..bbc10df0 100644 --- a/SOGo/OGoContentStore/ChangeLog +++ b/SOGo/OGoContentStore/ChangeLog @@ -1,3 +1,9 @@ +2005-07-13 Helge Hess + + * OCSContactFieldExtractor.m: do not crash if the content is not a + proper property list, added ability to extract fields from a vCard + (v0.9.30) + 2005-07-12 Helge Hess * added sql/profile-create.psql to create the user-profile database diff --git a/SOGo/OGoContentStore/OCSContactFieldExtractor.m b/SOGo/OGoContentStore/OCSContactFieldExtractor.m index 863b15f0..36e8a7c2 100644 --- a/SOGo/OGoContentStore/OCSContactFieldExtractor.m +++ b/SOGo/OGoContentStore/OCSContactFieldExtractor.m @@ -24,22 +24,65 @@ @interface OCSContactFieldExtractor : GCSFieldExtractor @end +#include #include "common.h" @implementation OCSContactFieldExtractor +static NSString *fieldNames[] = { + /* quickfield, vCard KVC path */ + @"givenName", @"n.given", + @"cn", @"fn.stringValue", + @"sn", @"n.family", + @"l", @"preferredAdr.locality", + @"mail", @"preferredEMail.stringValue", + @"o", @"org.orgnam", + @"ou", @"org.orgunit", + @"telephoneNumber", @"preferredTel.stringValue", + nil, nil +}; + +- (NSMutableDictionary *)extractQuickFieldsFromVCard:(NGVCard *)_vCard { + NSMutableDictionary *fields; + unsigned i; + + if (_vCard == nil) + return nil; + + fields = [NSMutableDictionary dictionaryWithCapacity:16]; + + for (i = 0; fieldNames[i] != nil; i += 2) { + id value; + + value = ([fieldNames[i + 1] length] > 0) + ? [_vCard valueForKeyPath:fieldNames[i + 1]] + : nil; + if (![value isNotNull]) value = [NSNull null]; + + [fields setObject:value forKey:[fieldNames[i] lowercaseString]]; + } + return fields; +} + +- (NSMutableDictionary *)extractQuickFieldsFromVCardString:(NSString *)_str { + NSArray *vCards; + + if ((vCards = [NGVCard parseVCardsFromSource:_str]) == nil) { + [self errorWithFormat:@"Could not parse content as a vCard."]; + return nil; + } + if ([vCards count] == 0) { + [self errorWithFormat:@"Could not parse content as a vCard."]; + return nil; + } + + if ([vCards count] > 1) + [self warnWithFormat:@"More than one vCard in content, using first."]; + + return [self extractQuickFieldsFromVCard:[vCards objectAtIndex:0]]; +} + - (NSMutableDictionary *)extractQuickFieldsFromContent:(NSString *)_content { - static NSString *fieldNames[] = { - @"givenName", - @"cn", - @"sn", - @"l", - @"mail", - @"o", - @"ou", - @"telephoneNumber", - nil - }; NSMutableDictionary *fields; NSDictionary *plist; unsigned i; @@ -47,6 +90,9 @@ if ([_content length] == 0) return nil; + if ([_content hasPrefix:@"BEGIN:VCARD"]) + return [self extractQuickFieldsFromVCardString:_content]; + // TODO: we want to support vcard storage in the future?! if ((plist = [_content propertyList]) == nil) { @@ -54,13 +100,18 @@ return nil; } + if (![plist isKindOfClass:[NSDictionary class]]) { + [self logWithFormat:@"ERROR: parsed property list is not a dictionary!"]; + return nil; + } + fields = [NSMutableDictionary dictionaryWithCapacity:16]; /* copy field values to quick record */ - for (i = 0; fieldNames[i] != nil; i++) { + for (i = 0; fieldNames[i] != nil; i += 2) { NSString *fieldName, *sqlName; id value; - + fieldName = fieldNames[i]; sqlName = [fieldName lowercaseString]; /* actually pgsql doesn't care */ diff --git a/SOGo/OGoContentStore/Version b/SOGo/OGoContentStore/Version index c8df7a3e..ae2cbb06 100644 --- a/SOGo/OGoContentStore/Version +++ b/SOGo/OGoContentStore/Version @@ -2,8 +2,9 @@ MAJOR_VERSION=0 MINOR_VERSION=9 -SUBMINOR_VERSION:=29 +SUBMINOR_VERSION:=30 +# v0.9.30 requires libNGiCal v4.5.48 # v0.9.26 requires libGDLContentStore v4.5.26 # v0.9.19 requires libNGiCal v4.5.40 # v0.9.18 requires libNGiCal v4.5.38