diff --git a/OpenChange/MAPIApplication.h b/OpenChange/MAPIApplication.h index e96d2d545..44fdaa15a 100644 --- a/OpenChange/MAPIApplication.h +++ b/OpenChange/MAPIApplication.h @@ -33,8 +33,9 @@ } - (id) authenticatorInContext: (id) context; - +- (MAPIStoreUserContext *) userContext; - (void) setUserContext: (MAPIStoreUserContext *) newContext; +- (void) cleanup; @end diff --git a/OpenChange/MAPIApplication.m b/OpenChange/MAPIApplication.m index 47438c2b1..818ec1199 100644 --- a/OpenChange/MAPIApplication.m +++ b/OpenChange/MAPIApplication.m @@ -76,6 +76,11 @@ MAPIApplication *MAPIApp = nil; return NO; } +- (MAPIStoreUserContext *) userContext +{ + return userContext; +} + - (void) setUserContext: (MAPIStoreUserContext *) newContext { /* user contexts must not be retained here ad their holder (mapistore) @@ -88,4 +93,9 @@ MAPIApplication *MAPIApp = nil; return [userContext authenticator]; } +- (void) cleanup +{ + [userContext deactivate]; +} + @end diff --git a/OpenChange/MAPIStoreSOGo.m b/OpenChange/MAPIStoreSOGo.m index f82cd3814..c11967a0e 100644 --- a/OpenChange/MAPIStoreSOGo.m +++ b/OpenChange/MAPIStoreSOGo.m @@ -74,10 +74,12 @@ static BOOL initialization_done = NO; #define TRYCATCH_END(pool) \ } @catch (NSException * e) { \ enum mapistore_error ret = sogo_backend_handle_objc_exception(e, __PRETTY_FUNCTION__, __LINE__); \ + mapiapp_cleanup(); \ [pool release]; \ NS_CURRENT_THREAD_TRY_UNREGISTER(); \ return ret; \ - } + } \ + mapiapp_cleanup(); static enum mapistore_error @@ -199,12 +201,28 @@ sogo_backend_init (void) return MAPISTORE_SUCCESS; } +/** + \details Cleanup operation to execute after an action has been performed + so there won't be any conflicts with future calls. + In practice this will deactivate the current user context set on MAPIApp + (which is the current WOApplication), this means two things: (1) set nil + as current user context on MAPIApp and (2) remove woContext from current + thread dictionary (this is used on WOContext.m). +*/ +static void mapiapp_cleanup(void) +{ + Class MAPIApplicationK; + MAPIApplicationK = NSClassFromString (@"MAPIApplication"); + if (MAPIApplicationK) + [[MAPIApplicationK application] cleanup]; +} + /** \details Create a connection context to the sogo backend \param mem_ctx pointer to the memory context \param uri pointer to the sogo path - \param private_data pointer to the private backend context + \param private_data pointer to the private backend context */ static enum mapistore_error diff --git a/OpenChange/MAPIStoreUserContext.h b/OpenChange/MAPIStoreUserContext.h index f9867d15a..b8defd0cd 100644 --- a/OpenChange/MAPIStoreUserContext.h +++ b/OpenChange/MAPIStoreUserContext.h @@ -86,6 +86,8 @@ /* SOGo hacky magic */ - (void) activateWithUser: (SOGoUser *) activeUser; +- (void) activate; +- (void) deactivate; - (MAPIStoreAuthenticator *) authenticator; - (WOContext *) woContext; diff --git a/OpenChange/MAPIStoreUserContext.m b/OpenChange/MAPIStoreUserContext.m index 159748519..cf3fe46fe 100644 --- a/OpenChange/MAPIStoreUserContext.m +++ b/OpenChange/MAPIStoreUserContext.m @@ -39,6 +39,7 @@ #import #import #import +#import #import #import @@ -369,6 +370,11 @@ static NSMapTable *contextsTable = nil; return authenticator; } +- (void) activate +{ + [self activateWithUser: [self sogoUser]]; +} + - (void) activateWithUser: (SOGoUser *) activeUser; { NSMutableDictionary *info; @@ -379,4 +385,24 @@ static NSMapTable *contextsTable = nil; [info setObject: woContext forKey: @"WOContext"]; } +- (void) deactivate +{ + NSMutableDictionary *info; + + if (self == [MAPIApp userContext]) + [MAPIApp setUserContext: nil]; + else + [self errorWithFormat: @"Error: Tried to deactivate an user context " + @"not enabled (%@ vs %@)", + [self username], [[MAPIApp userContext] username]]; + + info = [[NSThread currentThread] threadDictionary]; + if (woContext == [info objectForKey: @"WOContext"]) + [info removeObjectForKey: @"WOContext"]; + else + [self errorWithFormat: @"Error: Tried to deactivate a WOContext " + @"not enabled (%@ vs %@)", + woContext, [info objectForKey: @"WOContext"]]; +} + @end