2006-08-05 00:46:19 +02:00
|
|
|
/* SOGoContactFolders.m - this file is part of SOGo
|
|
|
|
*
|
2017-12-23 10:42:56 +01:00
|
|
|
* Copyright (C) 2006-2017 Inverse inc.
|
2006-08-05 00:46:19 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-03-24 18:41:57 +01:00
|
|
|
#import <Foundation/NSSortDescriptor.h>
|
2006-08-05 00:46:19 +02:00
|
|
|
|
2017-12-23 10:42:56 +01:00
|
|
|
#import <NGCards/NGVCard.h>
|
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>
|
|
|
|
|
2014-03-24 18:41:57 +01:00
|
|
|
#import <GDLContentStore/GCSChannelManager.h>
|
|
|
|
#import <GDLContentStore/GCSFolderManager.h>
|
|
|
|
#import <GDLContentStore/NSURL+GCS.h>
|
|
|
|
#import <GDLAccess/EOAdaptorChannel.h>
|
|
|
|
|
2010-10-25 20:46:32 +02:00
|
|
|
#import <SOGo/NSObject+DAV.h>
|
2009-11-29 05:19:32 +01:00
|
|
|
#import <SOGo/SOGoUser.h>
|
|
|
|
#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
|
|
|
|
2017-12-23 10:42:56 +01:00
|
|
|
#import "SOGoContactFolder.h"
|
2006-08-05 00:46:19 +02:00
|
|
|
#import "SOGoContactFolders.h"
|
|
|
|
|
2014-03-24 18:41:57 +01:00
|
|
|
Class SOGoContactSourceFolderK;
|
|
|
|
|
2010-10-27 16:20:09 +02:00
|
|
|
#define XMLNS_INVERSEDAV @"urn:inverse:params:xml:ns:inverse-dav"
|
2013-11-13 21:44:57 +01:00
|
|
|
|
2006-08-05 00:46:19 +02:00
|
|
|
@implementation SOGoContactFolders
|
|
|
|
|
2014-03-24 18:41:57 +01:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
SOGoContactSourceFolderK = [SOGoContactSourceFolder class];
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-24 18:41:57 +01:00
|
|
|
- (NSException *) appendCollectedSources
|
|
|
|
{
|
|
|
|
GCSChannelManager *cm;
|
|
|
|
EOAdaptorChannel *fc;
|
|
|
|
NSURL *folderLocation;
|
|
|
|
NSString *sql, *gcsFolderType;
|
|
|
|
NSException *error;
|
|
|
|
|
|
|
|
cm = [GCSChannelManager defaultChannelManager];
|
|
|
|
folderLocation = [[GCSFolderManager defaultFolderManager] folderInfoLocation];
|
|
|
|
fc = [cm acquireOpenChannelForURL: folderLocation];
|
|
|
|
if ([fc isOpen])
|
|
|
|
{
|
|
|
|
gcsFolderType = [[self class] gcsFolderType];
|
|
|
|
|
|
|
|
sql = [NSString stringWithFormat: (@"SELECT c_path4 FROM %@"
|
|
|
|
@" WHERE c_path2 = '%@'"
|
|
|
|
@" AND c_folder_type = '%@'"),
|
2014-03-25 19:53:42 +01:00
|
|
|
[folderLocation gcsTableName], owner, gcsFolderType];
|
|
|
|
|
|
|
|
error = [super fetchSpecialFolders: sql withChannel: fc andFolderType: SOGoCollectedFolder];
|
|
|
|
|
2014-03-24 18:41:57 +01:00
|
|
|
[cm releaseChannel: fc];
|
|
|
|
}
|
|
|
|
else
|
2014-03-25 19:53:42 +01:00
|
|
|
error = [NSException exceptionWithName: @"SOGoDBException"
|
|
|
|
reason: @"database connection could not be open"
|
|
|
|
userInfo: nil];
|
2014-03-24 18:41:57 +01:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2014-01-10 20:08:12 +01:00
|
|
|
- (NSDictionary *) systemSources
|
2007-05-09 21:18:06 +02:00
|
|
|
{
|
2014-01-10 20:08:12 +01:00
|
|
|
NSMutableDictionary *systemSources;
|
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
|
|
|
|
2014-01-10 20:08:12 +01:00
|
|
|
systemSources = [NSMutableDictionary dictionary];
|
|
|
|
|
2012-06-29 22:17:20 +02:00
|
|
|
if (! ([[context request] isIPhoneAddressBookApp] &&
|
|
|
|
![[context request] isAndroid]))
|
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]];
|
2014-01-10 20:08:12 +01:00
|
|
|
[systemSources setObject: currentFolder forKey: currentSourceID];
|
2011-06-24 21:19:50 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2014-01-10 20:08:12 +01:00
|
|
|
return systemSources;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) appendSystemSources
|
|
|
|
{
|
|
|
|
[subFolders addEntriesFromDictionary: [self systemSources]];
|
|
|
|
|
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;
|
|
|
|
|
2015-10-31 07:10:03 +01:00
|
|
|
if ([sourceID isEqualToString: @"personal"]){
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 403 reason: [NSString stringWithFormat: (@"folder '%@' cannot be deleted"), sourceID]];
|
|
|
|
}
|
2012-01-04 16:26:30 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-03-27 20:27:48 +01:00
|
|
|
- (NSString *) collectedFolderName
|
|
|
|
{
|
|
|
|
return [self labelForKey: @"Collected Address Book"];
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-03-30 16:07:36 +02:00
|
|
|
- (id) lookupName: (NSString *) name
|
|
|
|
inContext: (WOContext *) lookupContext
|
|
|
|
acquire: (BOOL) acquire
|
|
|
|
{
|
|
|
|
id folder = nil;
|
|
|
|
SOGoUser *currentUser;
|
|
|
|
SOGoUserManager *um;
|
|
|
|
SOGoSystemDefaults *sd;
|
|
|
|
NSEnumerator *domains;
|
|
|
|
NSArray *sourceIDs;
|
|
|
|
NSString *domain, *srcDisplayName;
|
|
|
|
|
2012-06-29 22:17:20 +02:00
|
|
|
if ([[context request] isIPhoneAddressBookApp] &&
|
|
|
|
![[context request] isAndroid])
|
2012-03-30 16:07:36 +02:00
|
|
|
{
|
|
|
|
currentUser = [context activeUser];
|
|
|
|
if (activeUserIsOwner
|
|
|
|
|| [[currentUser login] isEqualToString: owner])
|
|
|
|
{
|
|
|
|
domain = [currentUser domain];
|
|
|
|
um = [SOGoUserManager sharedUserManager];
|
|
|
|
sd = [SOGoSystemDefaults sharedSystemDefaults];
|
|
|
|
domains = [[sd visibleDomainsForDomain: domain] objectEnumerator];
|
|
|
|
while (domain)
|
|
|
|
{
|
|
|
|
sourceIDs = [um addressBookSourceIDsInDomain: domain];
|
|
|
|
if ([sourceIDs containsObject: name])
|
|
|
|
{
|
|
|
|
srcDisplayName = [um displayNameForSourceWithID: name];
|
|
|
|
folder = [SOGoContactSourceFolder
|
|
|
|
folderWithName: name
|
|
|
|
andDisplayName: srcDisplayName
|
|
|
|
inContainer: self];
|
|
|
|
[folder setSource: [um sourceWithID: name]];
|
|
|
|
}
|
|
|
|
domain = [domains nextObject];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!folder)
|
|
|
|
folder = [super lookupName: name inContext: lookupContext
|
|
|
|
acquire: acquire];
|
|
|
|
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
|
2010-10-26 20:25:32 +02:00
|
|
|
- (NSException *) setDavContactsCategories: (NSString *) newCategories
|
2010-10-25 20:46:32 +02:00
|
|
|
{
|
|
|
|
SOGoUser *ownerUser;
|
|
|
|
NSMutableArray *categories;
|
2012-10-22 16:09:13 +02:00
|
|
|
id <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];
|
2012-10-22 16:09:13 +02:00
|
|
|
documentElement = [document documentElement];
|
2010-10-25 20:46:32 +02:00
|
|
|
catNodes = [documentElement getElementsByTagName: @"category"];
|
|
|
|
max = [catNodes length];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
catNode = [catNodes objectAtIndex: count];
|
|
|
|
if ([catNode hasChildNodes])
|
2012-10-22 16:09:13 +02:00
|
|
|
[categories addObject: [(NGDOMNode *) 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];
|
|
|
|
}
|
|
|
|
|
2014-03-24 18:41:57 +01:00
|
|
|
- (NSArray *) allContactsFromFilter: (NSString *) theFilter
|
|
|
|
excludeGroups: (BOOL) excludeGroups
|
|
|
|
excludeLists: (BOOL) excludeLists
|
|
|
|
{
|
|
|
|
NSArray *folders, *contacts, *descriptors, *sortedContacts;
|
|
|
|
NSMutableDictionary *contact, *uniqueContacts;
|
2017-12-23 10:42:56 +01:00
|
|
|
NSMutableArray *sortedFolders;
|
|
|
|
SOGoFolder <SOGoContactFolder> *folder;
|
|
|
|
NSString *mail, *domain;
|
2014-03-24 18:41:57 +01:00
|
|
|
|
2017-12-23 10:42:56 +01:00
|
|
|
NSSortDescriptor *commonNameDescriptor;
|
|
|
|
unsigned int i, j, max;
|
2014-03-24 18:41:57 +01:00
|
|
|
|
|
|
|
domain = [[context activeUser] domain];
|
|
|
|
folders = nil;
|
2017-12-23 10:42:56 +01:00
|
|
|
|
2014-03-24 18:41:57 +01:00
|
|
|
NS_DURING
|
2017-12-23 10:42:56 +01:00
|
|
|
{
|
|
|
|
folders = [self subFolders];
|
|
|
|
}
|
2014-03-24 18:41:57 +01:00
|
|
|
NS_HANDLER
|
2017-12-23 10:42:56 +01:00
|
|
|
{
|
|
|
|
/* We need to specifically test for @"SOGoDBException", which is
|
|
|
|
raised explicitly in SOGoParentFolder. Any other exception should
|
|
|
|
be re-raised. */
|
|
|
|
if ([[localException name] isEqualToString: @"SOGoDBException"])
|
|
|
|
folders = nil;
|
|
|
|
else
|
|
|
|
[localException raise];
|
|
|
|
}
|
2014-03-24 18:41:57 +01:00
|
|
|
NS_ENDHANDLER;
|
2017-12-23 10:42:56 +01:00
|
|
|
|
2014-03-24 18:41:57 +01:00
|
|
|
max = [folders count];
|
|
|
|
sortedFolders = [NSMutableArray arrayWithCapacity: max];
|
|
|
|
uniqueContacts = [NSMutableDictionary dictionary];
|
2017-12-23 10:42:56 +01:00
|
|
|
|
2014-03-24 18:41:57 +01:00
|
|
|
for (i = 0; i < max; i++)
|
2017-12-23 10:42:56 +01:00
|
|
|
{
|
|
|
|
folder = [folders objectAtIndex: i];
|
|
|
|
/* We first search in LDAP folders (in case of duplicated entries in GCS folders) */
|
|
|
|
if ([folder isKindOfClass: SOGoContactSourceFolderK])
|
|
|
|
[sortedFolders insertObject: folder atIndex: 0];
|
|
|
|
else
|
|
|
|
[sortedFolders addObject: folder];
|
|
|
|
}
|
2014-03-24 18:41:57 +01:00
|
|
|
for (i = 0; i < max; i++)
|
|
|
|
{
|
2017-12-23 10:42:56 +01:00
|
|
|
folder = [sortedFolders objectAtIndex: i];
|
|
|
|
//NSLog(@" Address book: %@ (%@)", [folder displayName], [folder class]);
|
|
|
|
contacts = [folder lookupContactsWithFilter: theFilter
|
|
|
|
onCriteria: nil
|
|
|
|
sortBy: @"c_cn"
|
|
|
|
ordering: NSOrderedAscending
|
|
|
|
inDomain: domain];
|
|
|
|
for (j = 0; j < [contacts count]; j++)
|
|
|
|
{
|
|
|
|
contact = [contacts objectAtIndex: j];
|
|
|
|
[contact setObject: [folder nameInContainer]
|
|
|
|
forKey: @"container"];
|
|
|
|
mail = [contact objectForKey: @"c_mail"];
|
|
|
|
|
|
|
|
if (!excludeLists && [[contact objectForKey: @"c_component"]
|
|
|
|
isEqualToString: @"vlist"])
|
|
|
|
[uniqueContacts setObject: contact
|
|
|
|
forKey: [contact objectForKey: @"c_name"]];
|
|
|
|
else if ([mail length]
|
|
|
|
&& [uniqueContacts objectForKey: mail] == nil
|
|
|
|
&& !(excludeGroups && [contact objectForKey: @"isGroup"]))
|
|
|
|
[uniqueContacts setObject: contact forKey: mail];
|
|
|
|
}
|
2014-03-24 18:41:57 +01:00
|
|
|
}
|
|
|
|
if ([uniqueContacts count] > 0)
|
2017-12-23 10:42:56 +01:00
|
|
|
{
|
|
|
|
// Sort the contacts by display name
|
|
|
|
commonNameDescriptor = [[NSSortDescriptor alloc] initWithKey: @"c_cn"
|
|
|
|
ascending: YES];
|
|
|
|
descriptors = [NSArray arrayWithObjects: commonNameDescriptor, nil];
|
|
|
|
[commonNameDescriptor release];
|
2014-03-24 18:41:57 +01:00
|
|
|
sortedContacts = [[uniqueContacts allValues]
|
2017-12-23 10:42:56 +01:00
|
|
|
sortedArrayUsingDescriptors: descriptors];
|
|
|
|
}
|
2014-03-24 18:41:57 +01:00
|
|
|
else
|
|
|
|
sortedContacts = [NSArray array];
|
|
|
|
|
|
|
|
return sortedContacts;
|
|
|
|
}
|
|
|
|
|
2017-12-23 10:42:56 +01:00
|
|
|
- (id<SOGoContactObject>) contactForEmail: (NSString *) theEmail
|
|
|
|
{
|
|
|
|
NSDictionary *contact;
|
|
|
|
NSArray *allContacts;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
allContacts = [self allContactsFromFilter: theEmail excludeGroups: YES excludeLists: YES];
|
|
|
|
|
|
|
|
for (i = 0; i < [allContacts count]; i++)
|
|
|
|
{
|
|
|
|
contact = [allContacts objectAtIndex: i];
|
|
|
|
if ([[contact objectForKey: @"c_hascertificate"] boolValue])
|
|
|
|
{
|
|
|
|
SOGoFolder<SOGoContactObject> *contactObject;
|
|
|
|
SOGoFolder<SOGoContactFolder> *contactFolder;
|
|
|
|
|
|
|
|
contactFolder = [self lookupName: [contact objectForKey: @"container"] inContext: context acquire: NO];
|
|
|
|
contactObject = [contactFolder lookupName: [contact objectForKey: @"id"] inContext: context acquire: NO];
|
|
|
|
|
|
|
|
return contactObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSData *) certificateForEmail: (NSString *) theEmail
|
|
|
|
{
|
|
|
|
return [[[self contactForEmail: theEmail] vCard] certificate];
|
|
|
|
}
|
2014-03-24 18:41:57 +01:00
|
|
|
|
2006-08-05 00:46:19 +02:00
|
|
|
@end
|