diff --git a/OpenChange/GNUmakefile b/OpenChange/GNUmakefile index 822c137e9..0f3fb1a39 100644 --- a/OpenChange/GNUmakefile +++ b/OpenChange/GNUmakefile @@ -39,6 +39,7 @@ $(SOGOBACKEND)_OBJC_FILES += \ MAPIStoreTypes.m \ MAPIStorePropertySelectors.m \ MAPIStoreSamDBUtils.m \ + MAPIStoreUserContext.m \ \ SOGoMAPIVolatileMessage.m \ SOGoMAPIFSFolder.m \ diff --git a/OpenChange/MAPIStoreUserContext.h b/OpenChange/MAPIStoreUserContext.h new file mode 100644 index 000000000..3b986988f --- /dev/null +++ b/OpenChange/MAPIStoreUserContext.h @@ -0,0 +1,82 @@ +/* MAPIStoreUserContext.h - this file is part of $PROJECT_NAME_HERE$ + * + * Copyright (C) 2012 Inverse inc + * + * Author: Wolfgang Sourdeau + * + * 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. + */ + +#ifndef MAPISTOREUSERCONTEXT_H +#define MAPISTOREUSERCONTEXT_H + +#import + +@class NSString; + +@class WOContext; + +@class SOGoAppointmentFolders; +@class SOGoContactFolders; +@class SOGoMailAccount; +@class SOGoUser; +@class SOGoUserFolder; + +@class MAPIStoreAuthenticator; +@class MAPIStoreMapping; + +@interface MAPIStoreUserContext : NSObject +{ + NSString *username; + SOGoUser *sogoUser; + + SOGoUserFolder *userFolder; + SOGoAppointmentFolders *calendarRoot; + SOGoContactFolders *contactsRoot; + SOGoMailAccount *mailRoot; /* only one account supported */ + NSMutableArray *containersBag; + + MAPIStoreMapping *mapping; + + WOContext *woContext; + MAPIStoreAuthenticator *authenticator; +} + ++ (id) userContextWithUsername: (NSString *) username + andTDBIndexing: (struct tdb_wrap *) indexingTdb; + +- (id) initWithUsername: (NSString *) newUsername + andTDBIndexing: (struct tdb_wrap *) indexingTdb; + +- (NSString *) username; +- (SOGoUser *) sogoUser; + +- (SOGoUserFolder *) userFolder; + +- (SOGoAppointmentFolders *) calendarRoot; +- (SOGoContactFolders *) contactsRoot; +- (SOGoMailAccount *) mailRoot; + +- (MAPIStoreMapping *) mapping; + +/* SOGo hacky magic */ +- (void) activateWithUser: (SOGoUser *) activeUser; +- (MAPIStoreAuthenticator *) authenticator; +- (WOContext *) woContext; + +@end + +#endif /* MAPISTOREUSERCONTEXT_H */ diff --git a/OpenChange/MAPIStoreUserContext.m b/OpenChange/MAPIStoreUserContext.m new file mode 100644 index 000000000..c31777133 --- /dev/null +++ b/OpenChange/MAPIStoreUserContext.m @@ -0,0 +1,241 @@ +/* MAPIStoreUserContext.m - this file is part of $PROJECT_NAME_HERE$ + * + * Copyright (C) 2012 Inverse inc + * + * Author: Wolfgang Sourdeau + * + * 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. + */ + +#import +#import + +#import +#import + +#import + +#import +#import +#import +#import + +#import "MAPIApplication.h" +#import "MAPIStoreAuthenticator.h" +#import "MAPIStoreMapping.h" + +#import "MAPIStoreUserContext.h" + +static NSMapTable *contextsTable = nil; + +@implementation MAPIStoreUserContext + ++ (void) initialize +{ + contextsTable = [NSMapTable mapTableWithStrongToWeakObjects]; + [contextsTable retain]; +} + ++ (id) userContextWithUsername: (NSString *) username + andTDBIndexing: (struct tdb_wrap *) indexingTdb; +{ + id userContext; + + userContext = [contextsTable objectForKey: username]; + if (!userContext) + { + userContext = [[self alloc] initWithUsername: username + andTDBIndexing: indexingTdb]; + [userContext autorelease]; + } + + return userContext; +} + +- (id) init +{ + if ((self = [super init])) + { + username = nil; + sogoUser = nil; + + userFolder = nil; + calendarRoot = nil; + contactsRoot = nil; + mailRoot = nil; + + containersBag = [NSMutableArray new]; + + mapping = nil; + + authenticator = nil; + woContext = [WOContext contextWithRequest: nil]; + [woContext retain]; + } + + return self; +} + +- (id) initWithUsername: (NSString *) newUsername + andTDBIndexing: (struct tdb_wrap *) indexingTdb +{ + if ((self = [self init])) + { + /* "username" will be retained by table */ + username = newUsername; + if (indexingTdb) + ASSIGN (mapping, [MAPIStoreMapping mappingForUsername: username + withIndexing: indexingTdb]); + [contextsTable setObject: self forKey: username]; + + authenticator = [MAPIStoreAuthenticator new]; + [authenticator setUsername: username]; + + /* TODO: very hackish (IMAP access) */ + [authenticator setPassword: username]; + } + + return self; +} + +- (void) dealloc +{ + [contextsTable removeObjectForKey: username]; + + [userFolder release]; + [calendarRoot release]; + [contactsRoot release]; + [mailRoot release]; + [containersBag release]; + + [authenticator release]; + [mapping release]; + + [username release]; + [sogoUser release]; + + [super dealloc]; +} + +- (NSString *) username +{ + return username; +} + +- (SOGoUser *) sogoUser +{ + if (!sogoUser) + ASSIGN (sogoUser, [SOGoUser userWithLogin: username]); + + return sogoUser; +} + +- (SOGoUserFolder *) userFolder +{ + if (!userFolder) + { + userFolder = [SOGoUserFolder objectWithName: username + inContainer: MAPIApp]; + [userFolder retain]; + } + + return userFolder; +} + +- (SOGoAppointmentFolders *) calendarRoot +{ + if (!calendarRoot) + { + [self userFolder]; + [woContext setClientObject: userFolder]; + + calendarRoot = [userFolder lookupName: @"Calendar" + inContext: woContext + acquire: NO]; + [calendarRoot retain]; + } + + return calendarRoot; +} + +- (SOGoContactFolders *) contactsRoot +{ + if (!contactsRoot) + { + [self userFolder]; + [woContext setClientObject: userFolder]; + + contactsRoot = [userFolder lookupName: @"Contacts" + inContext: woContext + acquire: NO]; + [contactsRoot retain]; + } + + return contactsRoot; +} + +- (SOGoMailAccount *) mailRoot +{ + SOGoMailAccounts *accountsFolder; + + if (!mailRoot) + { + [self userFolder]; + [woContext setClientObject: userFolder]; + + accountsFolder = [userFolder lookupName: @"Mail" + inContext: woContext + acquire: NO]; + [containersBag addObject: accountsFolder]; + [woContext setClientObject: accountsFolder]; + + mailRoot = [accountsFolder lookupName: @"0" + inContext: woContext + acquire: NO]; + [[mailRoot imap4Connection] + enableExtension: @"QRESYNC"]; + [mailRoot retain]; + } + + return mailRoot; +} + +- (MAPIStoreMapping *) mapping +{ + return mapping; +} + +- (WOContext *) woContext +{ + return woContext; +} + +- (MAPIStoreAuthenticator *) authenticator +{ + return authenticator; +} + +- (void) activateWithUser: (SOGoUser *) activeUser; +{ + NSMutableDictionary *info; + + [MAPIApp setUserContext: self]; + [woContext setActiveUser: activeUser]; + info = [[NSThread currentThread] threadDictionary]; + [info setObject: woContext forKey: @"WOContext"]; +} + +@end