2006-08-05 00:46:19 +02:00
|
|
|
/* SOGoContactFolders.m - this file is part of SOGo
|
|
|
|
*
|
2011-03-04 21:30:28 +01:00
|
|
|
* Copyright (C) 2006-2011 Inverse inc.
|
2006-08-05 00:46:19 +02:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2006-08-09 23:31:28 +02:00
|
|
|
/* MailItems IPF.Note
|
|
|
|
ContactItems IPF.Contact
|
|
|
|
AppointmentItems IPF.Appointment
|
|
|
|
NoteItems IPF.StickyNote
|
|
|
|
TaskItems IPF.Task
|
|
|
|
JournalItems IPF.Journal */
|
|
|
|
|
2007-09-11 22:17:39 +02:00
|
|
|
#import <Foundation/NSArray.h>
|
2008-07-10 17:11:33 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
2006-08-05 00:46:19 +02:00
|
|
|
#import <Foundation/NSString.h>
|
2007-09-11 22:17:39 +02:00
|
|
|
#import <Foundation/NSEnumerator.h>
|
2006-08-05 00:46:19 +02:00
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
#import <NGObjWeb/WOContext+SoObjects.h>
|
2012-01-04 16:26:30 +01:00
|
|
|
#import <NGObjWeb/NSException+HTTP.h>
|
2010-10-25 20:46:32 +02:00
|
|
|
#import <DOM/DOMElement.h>
|
|
|
|
#import <DOM/DOMProtocols.h>
|
|
|
|
|
|
|
|
#import <SOGo/NSObject+DAV.h>
|
2009-11-29 05:19:32 +01:00
|
|
|
#import <SOGo/SOGoUser.h>
|
2010-10-25 20:46:32 +02:00
|
|
|
#import <SOGo/SOGoUserDefaults.h>
|
2009-11-29 05:19:32 +01:00
|
|
|
#import <SOGo/SOGoUserManager.h>
|
2011-06-24 21:19:50 +02:00
|
|
|
#import <SOGo/SOGoSystemDefaults.h>
|
2010-08-12 15:18:35 +02:00
|
|
|
#import <SOGo/WORequest+SOGo.h>
|
2006-12-15 23:18:55 +01:00
|
|
|
|
2006-08-09 23:31:28 +02:00
|
|
|
#import "SOGoContactGCSFolder.h"
|
2009-09-25 16:42:33 +02:00
|
|
|
#import "SOGoContactSourceFolder.h"
|
2012-01-04 16:26:30 +01:00
|
|
|
|
2006-08-05 00:46:19 +02:00
|
|
|
#import "SOGoContactFolders.h"
|
|
|
|
|
2010-10-27 16:20:09 +02:00
|
|
|
#define XMLNS_INVERSEDAV @"urn:inverse:params:xml:ns:inverse-dav"
|
2006-08-05 00:46:19 +02:00
|
|
|
@implementation SOGoContactFolders
|
|
|
|
|
2007-09-11 22:17:39 +02:00
|
|
|
+ (NSString *) gcsFolderType
|
2006-08-05 00:46:19 +02:00
|
|
|
{
|
2007-09-11 22:17:39 +02:00
|
|
|
return @"Contact";
|
2006-08-05 00:46:19 +02:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:17:39 +02:00
|
|
|
+ (Class) subFolderClass
|
2006-08-05 00:46:19 +02:00
|
|
|
{
|
2007-09-11 22:17:39 +02:00
|
|
|
return [SOGoContactGCSFolder class];
|
2007-02-09 22:18:28 +01:00
|
|
|
}
|
|
|
|
|
2012-01-04 16:26:30 +01:00
|
|
|
- (void) _fetchLDAPAddressBooks: (id <SOGoSource>) source
|
|
|
|
{
|
|
|
|
id <SOGoSource> abSource;
|
|
|
|
SOGoContactSourceFolder *folder;
|
|
|
|
NSArray *abSources;
|
|
|
|
NSUInteger count, max;
|
|
|
|
NSString *name;
|
|
|
|
|
|
|
|
abSources = [source addressBookSourcesForUser: owner];
|
|
|
|
max = [abSources count];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
abSource = [abSources objectAtIndex: count];
|
|
|
|
name = [abSource sourceID];
|
|
|
|
folder = [SOGoContactSourceFolder folderWithName: name
|
|
|
|
andDisplayName: [abSource displayName]
|
|
|
|
inContainer: self];
|
|
|
|
[folder setSource: abSource];
|
|
|
|
[folder setIsPersonalSource: YES];
|
|
|
|
[subFolders setObject: folder forKey: name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) appendPersonalSources
|
|
|
|
{
|
|
|
|
SOGoUser *currentUser;
|
|
|
|
NSException *result;
|
|
|
|
id <SOGoSource> source;
|
|
|
|
|
|
|
|
currentUser = [context activeUser];
|
|
|
|
source = [currentUser authenticationSource];
|
|
|
|
if ([source hasUserAddressBooks])
|
|
|
|
{
|
|
|
|
result = nil;
|
|
|
|
/* We don't handle ACLs for user LDAP addressbooks yet, therefore only
|
|
|
|
the owner has access to his addressbooks. */
|
|
|
|
if (activeUserIsOwner
|
|
|
|
|| [[currentUser login] isEqualToString: owner])
|
|
|
|
{
|
|
|
|
[self _fetchLDAPAddressBooks: source];
|
|
|
|
if (![subFolders objectForKey: @"personal"])
|
|
|
|
{
|
|
|
|
result = [source addAddressBookSource: @"personal"
|
|
|
|
withDisplayName: [self defaultFolderName]
|
|
|
|
forUser: owner];
|
|
|
|
if (!result)
|
|
|
|
[self _fetchLDAPAddressBooks: source];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = [super appendPersonalSources];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-03-23 14:10:33 +01:00
|
|
|
- (NSException *) appendSystemSources
|
2007-05-09 21:18:06 +02:00
|
|
|
{
|
2009-09-25 16:42:33 +02:00
|
|
|
SOGoUserManager *um;
|
2011-06-24 21:19:50 +02:00
|
|
|
SOGoSystemDefaults *sd;
|
|
|
|
NSEnumerator *sourceIDs, *domains;
|
2009-11-29 05:19:32 +01:00
|
|
|
NSString *currentSourceID, *srcDisplayName, *domain;
|
2009-09-25 16:42:33 +02:00
|
|
|
SOGoContactSourceFolder *currentFolder;
|
2010-06-02 18:37:43 +02:00
|
|
|
SOGoUser *currentUser;
|
2007-05-09 21:18:06 +02:00
|
|
|
|
2010-08-20 14:50:47 +02:00
|
|
|
if (![[context request] isIPhoneAddressBookApp])
|
2007-05-09 21:18:06 +02:00
|
|
|
{
|
2010-08-20 14:50:47 +02:00
|
|
|
currentUser = [context activeUser];
|
|
|
|
if (activeUserIsOwner
|
|
|
|
|| [[currentUser login] isEqualToString: owner])
|
2010-06-02 18:37:43 +02:00
|
|
|
{
|
2010-08-20 14:50:47 +02:00
|
|
|
domain = [currentUser domain];
|
|
|
|
um = [SOGoUserManager sharedUserManager];
|
2011-06-24 21:19:50 +02:00
|
|
|
sd = [SOGoSystemDefaults sharedSystemDefaults];
|
|
|
|
domains = [[sd visibleDomainsForDomain: domain] objectEnumerator];
|
|
|
|
while (domain)
|
2010-08-20 14:50:47 +02:00
|
|
|
{
|
2011-06-24 21:19:50 +02:00
|
|
|
sourceIDs = [[um addressBookSourceIDsInDomain: domain]
|
|
|
|
objectEnumerator];
|
|
|
|
while ((currentSourceID = [sourceIDs nextObject]))
|
|
|
|
{
|
|
|
|
srcDisplayName
|
|
|
|
= [um displayNameForSourceWithID: currentSourceID];
|
|
|
|
currentFolder = [SOGoContactSourceFolder
|
|
|
|
folderWithName: currentSourceID
|
|
|
|
andDisplayName: srcDisplayName
|
|
|
|
inContainer: self];
|
|
|
|
[currentFolder setSource: [um sourceWithID: currentSourceID]];
|
|
|
|
[subFolders setObject: currentFolder forKey: currentSourceID];
|
|
|
|
}
|
|
|
|
domain = [domains nextObject];
|
2010-08-20 14:50:47 +02:00
|
|
|
}
|
2010-06-02 18:37:43 +02:00
|
|
|
}
|
2007-05-09 21:18:06 +02:00
|
|
|
}
|
2009-03-23 14:10:33 +01:00
|
|
|
|
|
|
|
return nil;
|
2007-05-09 21:18:06 +02:00
|
|
|
}
|
2012-01-04 16:26:30 +01:00
|
|
|
|
|
|
|
- (NSException *) newFolderWithName: (NSString *) name
|
|
|
|
andNameInContainer: (NSString *) newNameInContainer
|
|
|
|
{
|
|
|
|
SOGoUser *currentUser;
|
|
|
|
NSException *result;
|
|
|
|
id <SOGoSource> source;
|
|
|
|
|
|
|
|
currentUser = [context activeUser];
|
|
|
|
source = [currentUser authenticationSource];
|
|
|
|
if ([source hasUserAddressBooks])
|
|
|
|
{
|
|
|
|
result = nil;
|
|
|
|
/* We don't handle ACLs for user LDAP addressbooks yet, therefore only
|
|
|
|
the owner has access to his addressbooks. */
|
|
|
|
if (activeUserIsOwner
|
|
|
|
|| [[currentUser login] isEqualToString: owner])
|
|
|
|
{
|
|
|
|
result = [source addAddressBookSource: newNameInContainer
|
|
|
|
withDisplayName: name
|
|
|
|
forUser: owner];
|
|
|
|
if (!result)
|
|
|
|
[self _fetchLDAPAddressBooks: source];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = [super newFolderWithName: name
|
|
|
|
andNameInContainer: newNameInContainer];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) renameLDAPAddressBook: (NSString *) sourceID
|
|
|
|
withDisplayName: (NSString *) newDisplayName
|
|
|
|
{
|
|
|
|
NSException *result;
|
|
|
|
SOGoUser *currentUser;
|
|
|
|
id <SOGoSource> source;
|
|
|
|
|
|
|
|
currentUser = [context activeUser];
|
|
|
|
source = [currentUser authenticationSource];
|
|
|
|
/* We don't handle ACLs for user LDAP addressbooks yet, therefore only
|
|
|
|
the owner has access to his addressbooks. */
|
|
|
|
if (activeUserIsOwner
|
|
|
|
|| [[currentUser login] isEqualToString: owner])
|
|
|
|
result = [source renameAddressBookSource: sourceID
|
|
|
|
withDisplayName: newDisplayName
|
|
|
|
forUser: owner];
|
|
|
|
else
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 403
|
|
|
|
reason: @"operation denied"];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) removeLDAPAddressBook: (NSString *) sourceID
|
|
|
|
{
|
|
|
|
NSException *result;
|
|
|
|
SOGoUser *currentUser;
|
|
|
|
id <SOGoSource> source;
|
|
|
|
|
|
|
|
if ([sourceID isEqualToString: @"personal"])
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 403
|
|
|
|
reason: @"folder 'personal' cannot be deleted"];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = nil;
|
|
|
|
currentUser = [context activeUser];
|
|
|
|
source = [currentUser authenticationSource];
|
|
|
|
/* We don't handle ACLs for user LDAP addressbooks yet, therefore only
|
|
|
|
the owner has access to his addressbooks. */
|
|
|
|
if (activeUserIsOwner
|
|
|
|
|| [[currentUser login] isEqualToString: owner])
|
|
|
|
result = [source removeAddressBookSource: sourceID
|
|
|
|
forUser: owner];
|
|
|
|
else
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 403
|
|
|
|
reason: @"operation denied"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2010-08-20 14:50:47 +02:00
|
|
|
|
2007-09-15 00:01:02 +02:00
|
|
|
- (NSString *) defaultFolderName
|
|
|
|
{
|
2008-06-23 16:23:18 +02:00
|
|
|
return [self labelForKey: @"Personal Address Book"];
|
2007-09-15 00:01:02 +02:00
|
|
|
}
|
|
|
|
|
2010-08-12 15:18:35 +02:00
|
|
|
- (NSArray *) toManyRelationshipKeys
|
|
|
|
{
|
|
|
|
NSMutableArray *keys;
|
|
|
|
|
2010-08-20 14:50:47 +02:00
|
|
|
if ([[context request] isMacOSXAddressBookApp])
|
2010-08-12 15:18:35 +02:00
|
|
|
keys = [NSMutableArray arrayWithObject: @"personal"];
|
|
|
|
else
|
|
|
|
keys = (NSMutableArray *) [super toManyRelationshipKeys];
|
|
|
|
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
2010-10-26 20:25:32 +02:00
|
|
|
- (NSException *) setDavContactsCategories: (NSString *) newCategories
|
2010-10-25 20:46:32 +02:00
|
|
|
{
|
|
|
|
SOGoUser *ownerUser;
|
|
|
|
NSMutableArray *categories;
|
2011-03-11 20:31:06 +01:00
|
|
|
DOMElement *documentElement, *catNode;
|
2010-10-25 20:46:32 +02:00
|
|
|
id <DOMDocument> document;
|
|
|
|
id <DOMNodeList> catNodes;
|
|
|
|
NSUInteger count, max;
|
|
|
|
SOGoUserDefaults *ud;
|
|
|
|
|
|
|
|
categories = [NSMutableArray array];
|
|
|
|
|
|
|
|
if ([newCategories length] > 0)
|
|
|
|
{
|
|
|
|
document = [[context request] contentAsDOMDocument];
|
|
|
|
documentElement = (DOMElement *) [document documentElement];
|
|
|
|
catNodes = [documentElement getElementsByTagName: @"category"];
|
|
|
|
max = [catNodes length];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
catNode = [catNodes objectAtIndex: count];
|
|
|
|
if ([catNode hasChildNodes])
|
2011-03-11 20:31:06 +01:00
|
|
|
[categories addObject: [catNode textValue]];
|
2010-10-25 20:46:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-11 20:31:06 +01:00
|
|
|
// NSLog (@"setting categories to : %@", categories);
|
2010-10-25 20:46:32 +02:00
|
|
|
ownerUser = [SOGoUser userWithLogin: owner];
|
|
|
|
ud = [ownerUser userDefaults];
|
2010-10-26 20:25:32 +02:00
|
|
|
[ud setContactsCategories: categories];
|
2010-10-25 20:46:32 +02:00
|
|
|
[ud synchronize];
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2010-10-26 20:25:32 +02:00
|
|
|
- (SOGoWebDAVValue *) davContactsCategories
|
2010-10-25 20:46:32 +02:00
|
|
|
{
|
|
|
|
NSMutableArray *davCategories;
|
|
|
|
NSArray *categories;
|
|
|
|
NSUInteger count, max;
|
|
|
|
SOGoUser *ownerUser;
|
|
|
|
NSDictionary *catElement;
|
|
|
|
|
|
|
|
ownerUser = [SOGoUser userWithLogin: owner];
|
2010-10-26 20:25:32 +02:00
|
|
|
categories = [[ownerUser userDefaults] contactsCategories];
|
2010-10-25 20:46:32 +02:00
|
|
|
|
|
|
|
max = [categories count];
|
|
|
|
davCategories = [NSMutableArray arrayWithCapacity: max];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
catElement = davElementWithContent (@"category",
|
|
|
|
XMLNS_INVERSEDAV,
|
|
|
|
[categories objectAtIndex: count]);
|
|
|
|
[davCategories addObject: catElement];
|
|
|
|
}
|
|
|
|
|
2010-10-26 20:25:32 +02:00
|
|
|
return [davElementWithContent (@"contacts-categories",
|
2010-10-25 20:46:32 +02:00
|
|
|
XMLNS_INVERSEDAV,
|
|
|
|
davCategories)
|
|
|
|
asWebDAVValue];
|
|
|
|
}
|
|
|
|
|
2006-08-05 00:46:19 +02:00
|
|
|
@end
|