2009-09-09 15:31:45 +02:00
|
|
|
/*
|
|
|
|
Copyright (C) 2004-2005 SKYRIX Software AG
|
2014-07-30 20:51:00 +02:00
|
|
|
Copyright (C) 2006-2014 Inverse inc.
|
2009-09-09 15:31:45 +02:00
|
|
|
|
2012-11-17 21:33:07 +01:00
|
|
|
This file is part of SOGo
|
2009-09-09 15:31:45 +02:00
|
|
|
|
2012-11-17 21:33:07 +01:00
|
|
|
SOGo is free software; you can redistribute it and/or modify it under
|
2009-09-09 15:31:45 +02:00
|
|
|
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.
|
|
|
|
|
2012-11-17 21:33:07 +01:00
|
|
|
SOGo is distributed in the hope that it will be useful, but WITHOUT ANY
|
2009-09-09 15:31:45 +02:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import <SoObjects/SOGo/NSArray+Utilities.h>
|
|
|
|
#import <SoObjects/SOGo/NSDictionary+Utilities.h>
|
|
|
|
#import <SoObjects/SOGo/NSString+Utilities.h>
|
|
|
|
|
|
|
|
#import <NGObjWeb/NSException+HTTP.h>
|
|
|
|
#import <NGObjWeb/WOContext.h>
|
|
|
|
#import <NGObjWeb/WOResponse.h>
|
2015-11-03 20:59:40 +01:00
|
|
|
#define COMPILING_NGOBJWEB 1 /* we want httpRequest for parsing multi-part
|
|
|
|
form data */
|
2009-09-09 15:31:45 +02:00
|
|
|
#import <NGObjWeb/WORequest.h>
|
2015-11-03 20:59:40 +01:00
|
|
|
#undef COMPILING_NGOBJWEB
|
2009-09-09 15:31:45 +02:00
|
|
|
#import <NGExtensions/NSString+misc.h>
|
|
|
|
#import <NGExtensions/NSNull+misc.h>
|
2011-06-16 19:04:00 +02:00
|
|
|
#import <NGExtensions/NGBase64Coding.h>
|
2015-11-03 20:59:40 +01:00
|
|
|
#import <NGMime/NGMimeMultipartBody.h>
|
2009-09-09 15:31:45 +02:00
|
|
|
|
|
|
|
#import <Contacts/SOGoContactObject.h>
|
|
|
|
#import <Contacts/SOGoContactFolder.h>
|
|
|
|
#import <Contacts/SOGoContactFolders.h>
|
2011-12-30 21:39:07 +01:00
|
|
|
#import <Contacts/NSDictionary+LDIF.h>
|
2009-09-09 15:31:45 +02:00
|
|
|
|
2009-09-09 17:30:22 +02:00
|
|
|
#import <SoObjects/Contacts/NGVCard+SOGo.h>
|
|
|
|
#import <SoObjects/Contacts/NGVList+SOGo.h>
|
2009-09-09 15:31:45 +02:00
|
|
|
#import <SoObjects/Contacts/SOGoContactGCSEntry.h>
|
|
|
|
#import <SoObjects/Contacts/SOGoContactLDIFEntry.h>
|
|
|
|
#import <SoObjects/Contacts/SOGoContactGCSList.h>
|
|
|
|
#import <SoObjects/Contacts/SOGoContactGCSFolder.h>
|
|
|
|
#import <GDLContentStore/GCSFolder.h>
|
|
|
|
|
|
|
|
#import "UIxContactFolderActions.h"
|
|
|
|
|
2016-12-29 20:12:45 +01:00
|
|
|
static NSArray *photoTags = nil;
|
|
|
|
|
2009-09-09 15:31:45 +02:00
|
|
|
@implementation UIxContactFolderActions
|
|
|
|
|
2016-12-29 20:12:45 +01:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (!photoTags)
|
|
|
|
{
|
|
|
|
photoTags = [[NSArray alloc] initWithObjects: @"jpegphoto", @"photo", @"thumbnailphoto", nil];
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 15:31:45 +02:00
|
|
|
|
|
|
|
/* actions */
|
|
|
|
|
|
|
|
- (id <WOActionResults>) exportAction
|
|
|
|
{
|
|
|
|
WORequest *request;
|
2010-01-15 12:41:20 +01:00
|
|
|
WOResponse *response;
|
2009-09-09 15:31:45 +02:00
|
|
|
NSArray *contactsId;
|
|
|
|
NSEnumerator *uids;
|
2010-04-19 18:35:35 +02:00
|
|
|
NSString *uid, *filename, *disposition;
|
2009-09-09 15:31:45 +02:00
|
|
|
id currentChild;
|
|
|
|
SOGoContactGCSFolder *sourceFolder;
|
|
|
|
NSMutableString *content;
|
|
|
|
|
|
|
|
content = [NSMutableString string];
|
|
|
|
request = [context request];
|
2010-04-19 18:35:35 +02:00
|
|
|
sourceFolder = [self clientObject];
|
2009-09-09 15:31:45 +02:00
|
|
|
contactsId = [request formValuesForKey: @"uid"];
|
2010-04-19 18:35:35 +02:00
|
|
|
if (!contactsId)
|
|
|
|
contactsId = [sourceFolder toOneRelationshipKeys];
|
|
|
|
|
|
|
|
uids = [contactsId objectEnumerator];
|
|
|
|
while ((uid = [uids nextObject]))
|
2009-09-09 15:31:45 +02:00
|
|
|
{
|
2010-04-19 18:35:35 +02:00
|
|
|
currentChild = [sourceFolder lookupName: uid
|
|
|
|
inContext: [self context]
|
|
|
|
acquire: NO];
|
|
|
|
if ([currentChild respondsToSelector: @selector (vCard)])
|
2015-11-05 18:29:26 +01:00
|
|
|
[content appendFormat: @"%@", [[currentChild ldifRecord] ldifRecordAsString]];
|
2010-04-19 18:35:35 +02:00
|
|
|
else if ([currentChild respondsToSelector: @selector (vList)])
|
2015-11-05 18:29:26 +01:00
|
|
|
[content appendFormat: @"%@", [[currentChild vList] ldifString]];
|
2011-12-30 21:39:07 +01:00
|
|
|
[content appendString: @"\n"];
|
2009-09-09 15:31:45 +02:00
|
|
|
}
|
2009-09-22 17:43:38 +02:00
|
|
|
|
2009-09-09 15:31:45 +02:00
|
|
|
response = [context response];
|
2009-09-22 19:25:00 +02:00
|
|
|
[response setHeader: @"application/octet-stream; charset=utf-8"
|
2009-09-09 15:31:45 +02:00
|
|
|
forKey: @"content-type"];
|
2010-04-19 18:35:35 +02:00
|
|
|
filename = [NSString stringWithFormat: @"%@.ldif",
|
|
|
|
[sourceFolder displayName]];
|
|
|
|
disposition = [NSString stringWithFormat: @"attachment; filename=\"%@\"",
|
|
|
|
[filename asQPSubjectString: @"utf-8"]];
|
|
|
|
[response setHeader: disposition forKey: @"Content-Disposition"];
|
2009-09-09 15:31:45 +02:00
|
|
|
[response setContent: [content dataUsingEncoding: NSUTF8StringEncoding]];
|
2010-04-19 18:35:35 +02:00
|
|
|
|
2009-09-09 15:31:45 +02:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id <WOActionResults>) importAction
|
|
|
|
{
|
|
|
|
WORequest *request;
|
|
|
|
WOResponse *response;
|
|
|
|
id data;
|
|
|
|
NSMutableDictionary *rc;
|
|
|
|
NSString *fileContent;
|
|
|
|
int imported = 0;
|
|
|
|
|
|
|
|
|
|
|
|
request = [context request];
|
|
|
|
rc = [NSMutableDictionary dictionary];
|
|
|
|
data = [request formValueForKey: @"contactsFile"];
|
|
|
|
if ([data respondsToSelector: @selector(isEqualToString:)])
|
|
|
|
fileContent = (NSString *) data;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fileContent = [[NSString alloc] initWithData: (NSData *) data
|
|
|
|
encoding: NSUTF8StringEncoding];
|
|
|
|
[fileContent autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileContent && [fileContent length])
|
|
|
|
{
|
|
|
|
if ([fileContent hasPrefix: @"dn:"])
|
|
|
|
imported = [self importLdifData: fileContent];
|
|
|
|
else if ([fileContent hasPrefix: @"BEGIN:"])
|
|
|
|
imported = [self importVcardData: fileContent];
|
|
|
|
else
|
|
|
|
imported = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
[rc setObject: [NSNumber numberWithInt: imported]
|
|
|
|
forKey: @"imported"];
|
|
|
|
|
|
|
|
response = [self responseWithStatus: 200];
|
|
|
|
[response setHeader: @"text/html"
|
|
|
|
forKey: @"content-type"];
|
|
|
|
[(WOResponse*)response appendContentString: [rc jsonRepresentation]];
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) importLdifData: (NSString *) ldifData
|
|
|
|
{
|
2016-12-29 22:21:30 +01:00
|
|
|
NSMutableDictionary *entry, *encodedEntry;
|
|
|
|
SOGoContactLDIFEntry *ldifEntry;
|
|
|
|
NSArray *ldifContacts, *lines;
|
2016-12-29 20:12:45 +01:00
|
|
|
SOGoContactGCSFolder *folder;
|
2016-12-29 22:21:30 +01:00
|
|
|
NSEnumerator *keyEnumerator;
|
|
|
|
NSString *key, *uid, *line;
|
2009-09-09 15:31:45 +02:00
|
|
|
NGVCard *vCard;
|
2016-12-29 20:12:45 +01:00
|
|
|
id value;
|
|
|
|
|
2016-12-29 22:21:30 +01:00
|
|
|
NSRange r;
|
2016-12-29 20:12:45 +01:00
|
|
|
int i, j, count, linesCount;
|
|
|
|
int rc;
|
2009-09-09 15:31:45 +02:00
|
|
|
|
|
|
|
folder = [self clientObject];
|
|
|
|
ldifContacts = [ldifData componentsSeparatedByString: @"\ndn"];
|
|
|
|
count = [ldifContacts count];
|
2016-12-29 20:12:45 +01:00
|
|
|
rc = 0;
|
2009-09-09 15:31:45 +02:00
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
2011-06-16 19:04:00 +02:00
|
|
|
encodedEntry = [NSMutableDictionary dictionary];
|
2009-09-09 15:31:45 +02:00
|
|
|
lines = [[ldifContacts objectAtIndex: i]
|
|
|
|
componentsSeparatedByString: @"\n"];
|
|
|
|
|
2011-06-16 19:04:00 +02:00
|
|
|
key = NULL;
|
2009-09-09 15:31:45 +02:00
|
|
|
linesCount = [lines count];
|
|
|
|
for (j = 0; j < linesCount; j++)
|
|
|
|
{
|
2011-06-16 19:04:00 +02:00
|
|
|
line = [lines objectAtIndex: j];
|
|
|
|
|
|
|
|
/* skip embedded comment lines */
|
|
|
|
if ([line hasPrefix: @"#"])
|
|
|
|
{
|
|
|
|
key = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle continuation lines */
|
|
|
|
if ([line hasPrefix: @" "])
|
|
|
|
{
|
|
|
|
if (key != NULL)
|
|
|
|
{
|
|
|
|
value = [[encodedEntry valueForKey: key]
|
2016-12-29 22:21:30 +01:00
|
|
|
stringByAppendingString: [line substringFromIndex: 1]];
|
2011-06-16 19:04:00 +02:00
|
|
|
[encodedEntry setValue: value forKey: key];
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-12-29 22:21:30 +01:00
|
|
|
r = [line rangeOfString: @": "];
|
|
|
|
if (r.location != NSNotFound)
|
2009-09-09 15:31:45 +02:00
|
|
|
{
|
2016-12-29 22:21:30 +01:00
|
|
|
key = [[line substringToIndex: r.location] lowercaseString];
|
|
|
|
value = [line substringFromIndex: NSMaxRange(r)];
|
2009-09-09 15:31:45 +02:00
|
|
|
|
|
|
|
if ([key length] == 0)
|
|
|
|
key = @"dn";
|
|
|
|
|
2011-06-16 19:04:00 +02:00
|
|
|
[encodedEntry setValue: value forKey: key];
|
2009-09-09 15:31:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-06-16 19:04:00 +02:00
|
|
|
|
|
|
|
/* decode Base64-encoded attributes */
|
|
|
|
entry = [NSMutableDictionary dictionary];
|
|
|
|
keyEnumerator = [encodedEntry keyEnumerator];
|
|
|
|
while ((key = [keyEnumerator nextObject]))
|
|
|
|
{
|
|
|
|
value = [encodedEntry valueForKey: key];
|
|
|
|
if ([key hasSuffix: @":"])
|
|
|
|
{
|
|
|
|
key = [key substringToIndex: [key length] - 1];
|
2016-12-29 20:12:45 +01:00
|
|
|
if ([photoTags containsObject: key])
|
|
|
|
value = [value dataByDecodingBase64];
|
|
|
|
else
|
|
|
|
value = [value stringByDecodingBase64];
|
2011-06-16 19:04:00 +02:00
|
|
|
}
|
2016-12-29 20:12:45 +01:00
|
|
|
|
|
|
|
// Standard key recognized in NGCards
|
|
|
|
if ([photoTags containsObject: key])
|
|
|
|
key = @"photo";
|
|
|
|
|
2011-06-16 19:04:00 +02:00
|
|
|
[entry setValue: value forKey: key];
|
|
|
|
}
|
|
|
|
|
2009-09-09 15:31:45 +02:00
|
|
|
uid = [folder globallyUniqueObjectId];
|
|
|
|
ldifEntry = [SOGoContactLDIFEntry contactEntryWithName: uid
|
|
|
|
withLDIFEntry: entry
|
|
|
|
inContainer: folder];
|
|
|
|
if (ldifEntry)
|
|
|
|
{
|
|
|
|
vCard = [ldifEntry vCard];
|
|
|
|
if ([self importVcard: vCard])
|
|
|
|
rc++;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) importVcardData: (NSString *) vcardData
|
|
|
|
{
|
2010-01-11 19:38:44 +01:00
|
|
|
NSArray *allCards;
|
2009-09-09 15:31:45 +02:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = 0;
|
2010-01-11 19:38:44 +01:00
|
|
|
allCards = [NGVCard parseFromSource: vcardData];
|
|
|
|
|
|
|
|
if (allCards && [allCards count])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < [allCards count]; i++)
|
|
|
|
{
|
|
|
|
if (![self importVcard: [allCards objectAtIndex: i]])
|
|
|
|
{
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rc++;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 15:31:45 +02:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) importVcard: (NGVCard *) card
|
|
|
|
{
|
2009-09-28 15:25:14 +02:00
|
|
|
NSString *uid;
|
2009-09-09 15:31:45 +02:00
|
|
|
SOGoContactGCSFolder *folder;
|
2009-09-28 15:25:14 +02:00
|
|
|
SOGoContactGCSEntry *contact;
|
2009-09-09 15:31:45 +02:00
|
|
|
BOOL rc = NO;
|
|
|
|
|
|
|
|
if (card)
|
|
|
|
{
|
|
|
|
folder = [self clientObject];
|
|
|
|
uid = [folder globallyUniqueObjectId];
|
2009-09-28 15:25:14 +02:00
|
|
|
|
2009-09-09 15:31:45 +02:00
|
|
|
[card setUid: uid];
|
2009-09-28 15:25:14 +02:00
|
|
|
contact = [SOGoContactGCSEntry objectWithName: uid
|
|
|
|
inContainer: folder];
|
|
|
|
[contact setIsNew: YES];
|
|
|
|
|
2014-07-30 20:51:00 +02:00
|
|
|
[contact saveComponent: card];
|
2009-09-28 15:25:14 +02:00
|
|
|
|
|
|
|
rc = YES;
|
2009-09-09 15:31:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-03-15 16:23:27 +01:00
|
|
|
@end /* UIxContactFolderActions */
|