2006-06-15 21:34:10 +02:00
|
|
|
/*
|
|
|
|
Copyright (C) 2004-2005 SKYRIX Software AG
|
|
|
|
|
|
|
|
This file is part of OpenGroupware.org.
|
|
|
|
|
|
|
|
OGo is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License as published by the
|
|
|
|
Free Software Foundation; either version 2, or (at your option) any
|
|
|
|
later version.
|
|
|
|
|
|
|
|
OGo is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with OGo; see the file COPYING. If not, write to the
|
|
|
|
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2007-07-10 21:25:20 +02:00
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
#import <Foundation/NSDictionary.h>
|
2008-10-15 22:22:14 +02:00
|
|
|
#import <NGExtensions/NSNull+misc.h>
|
2007-07-10 21:25:20 +02:00
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
|
|
|
|
2007-04-10 22:39:00 +02:00
|
|
|
#import <GDLContentStore/GCSFieldExtractor.h>
|
|
|
|
#import <NGCards/NGVCard.h>
|
2008-03-07 23:17:16 +01:00
|
|
|
#import <NGCards/NGVList.h>
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2010-10-26 22:15:01 +02:00
|
|
|
#import <SOGo/NSArray+Utilities.h>
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
@interface OCSContactFieldExtractor : GCSFieldExtractor
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation OCSContactFieldExtractor
|
|
|
|
|
2007-04-02 22:06:53 +02:00
|
|
|
- (NSMutableDictionary *) extractQuickFieldsFromVCard: (NGVCard *) vCard
|
2006-09-18 17:22:21 +02:00
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSMutableDictionary *fields;
|
2006-09-18 17:22:21 +02:00
|
|
|
NSArray *values;
|
|
|
|
CardElement *adr;
|
|
|
|
NSString *value;
|
|
|
|
unsigned int max;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-04-10 22:39:00 +02:00
|
|
|
fields = [NSMutableDictionary dictionaryWithCapacity: 16];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-04-02 22:06:53 +02:00
|
|
|
value = [vCard fn];
|
2006-09-18 17:22:21 +02:00
|
|
|
if (value)
|
2007-07-24 20:45:52 +02:00
|
|
|
[fields setObject: value forKey: @"c_cn"];
|
2007-04-02 22:06:53 +02:00
|
|
|
values = [vCard n];
|
2006-09-18 17:22:21 +02:00
|
|
|
if (values)
|
|
|
|
{
|
|
|
|
max = [values count];
|
|
|
|
if (max > 0)
|
|
|
|
{
|
2007-07-24 20:45:52 +02:00
|
|
|
[fields setObject: [values objectAtIndex: 0] forKey: @"c_sn"];
|
2006-09-18 17:22:21 +02:00
|
|
|
if (max > 1)
|
|
|
|
[fields setObject: [values objectAtIndex: 1]
|
2007-07-24 20:45:52 +02:00
|
|
|
forKey: @"c_givenName"];
|
2006-09-18 17:22:21 +02:00
|
|
|
}
|
|
|
|
}
|
2007-04-02 22:06:53 +02:00
|
|
|
value = [vCard preferredTel];
|
2006-09-18 17:22:21 +02:00
|
|
|
if (value)
|
2007-07-24 20:45:52 +02:00
|
|
|
[fields setObject: value forKey: @"c_telephoneNumber"];
|
2007-04-02 22:06:53 +02:00
|
|
|
value = [vCard preferredEMail];
|
2008-10-15 21:57:17 +02:00
|
|
|
if (![value isNotNull])
|
|
|
|
value = @"";
|
|
|
|
[fields setObject: value forKey: @"c_mail"];
|
2007-04-02 22:06:53 +02:00
|
|
|
values = [vCard org];
|
|
|
|
max = [values count];
|
|
|
|
if (max > 0)
|
2006-09-18 17:22:21 +02:00
|
|
|
{
|
2007-07-24 20:45:52 +02:00
|
|
|
[fields setObject: [values objectAtIndex: 0] forKey: @"c_o"];
|
2007-04-02 22:06:53 +02:00
|
|
|
if (max > 1)
|
2007-07-24 20:45:52 +02:00
|
|
|
[fields setObject: [values objectAtIndex: 1] forKey: @"c_ou"];
|
2006-09-18 17:22:21 +02:00
|
|
|
}
|
2007-04-02 22:06:53 +02:00
|
|
|
adr = [vCard preferredAdr];
|
2006-09-18 17:22:21 +02:00
|
|
|
if (adr)
|
2007-07-24 20:45:52 +02:00
|
|
|
[fields setObject: [adr value: 3] forKey: @"c_l"];
|
2007-04-02 22:06:53 +02:00
|
|
|
value = [[vCard uniqueChildWithTag: @"X-AIM"] value: 0];
|
2007-07-24 20:45:52 +02:00
|
|
|
[fields setObject: value forKey: @"c_screenname"];
|
2010-10-26 22:15:01 +02:00
|
|
|
values = [[vCard categories] trimmedComponents];
|
|
|
|
if ([values count] > 0)
|
|
|
|
[fields setObject: [values componentsJoinedByString: @","]
|
|
|
|
forKey: @"c_categories"];
|
2008-03-07 23:17:16 +01:00
|
|
|
[fields setObject: @"vcard" forKey: @"c_component"];
|
|
|
|
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSMutableDictionary *) extractQuickFieldsFromVList: (NGVList *) vList
|
|
|
|
{
|
|
|
|
NSMutableDictionary *fields;
|
|
|
|
NSString *value;
|
|
|
|
|
|
|
|
fields = [NSMutableDictionary dictionaryWithCapacity: 1];
|
|
|
|
|
|
|
|
value = [vList fn];
|
|
|
|
if (value)
|
|
|
|
[fields setObject: value forKey: @"c_cn"];
|
|
|
|
[fields setObject: @"vlist" forKey: @"c_component"];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
2007-04-10 22:39:00 +02:00
|
|
|
- (NSMutableDictionary *) extractQuickFieldsFromContent: (NSString *) content
|
|
|
|
{
|
|
|
|
NSMutableDictionary *fields;
|
2006-09-29 20:31:20 +02:00
|
|
|
NGVCard *vCard;
|
2008-03-07 23:17:16 +01:00
|
|
|
NGVList *vList;
|
|
|
|
NSString *upperContent;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-04-10 22:39:00 +02:00
|
|
|
fields = nil;
|
2008-03-07 23:17:16 +01:00
|
|
|
if ([content length] > 0)
|
2007-04-10 22:39:00 +02:00
|
|
|
{
|
2008-03-07 23:17:16 +01:00
|
|
|
upperContent = [content uppercaseString];
|
|
|
|
if ([upperContent hasPrefix: @"BEGIN:VCARD"])
|
|
|
|
{
|
|
|
|
vCard = [NGVCard parseSingleFromSource: content];
|
|
|
|
if (vCard)
|
|
|
|
fields = [self extractQuickFieldsFromVCard: vCard];
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"Could not parse VCARD content."];
|
|
|
|
}
|
|
|
|
else if ([upperContent hasPrefix: @"BEGIN:VLIST"])
|
|
|
|
{
|
|
|
|
vList = [NGVList parseSingleFromSource: content];
|
|
|
|
if (vList)
|
|
|
|
fields = [self extractQuickFieldsFromVList: vList];
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"Could not parse VLIST content."];
|
|
|
|
}
|
2007-04-10 22:39:00 +02:00
|
|
|
else
|
2008-03-07 23:17:16 +01:00
|
|
|
[self errorWithFormat: @"Content is unknown."];
|
2007-04-10 22:39:00 +02:00
|
|
|
}
|
|
|
|
else
|
2008-03-07 23:17:16 +01:00
|
|
|
[self errorWithFormat: @"Content is empty."];
|
2007-04-10 22:39:00 +02:00
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end /* OCSContactFieldExtractor */
|