diff options
author | Hoa V. DINH <dinh.viet.hoa@gmail.com> | 2016-03-07 22:14:47 -0800 |
---|---|---|
committer | Hoa V. DINH <dinh.viet.hoa@gmail.com> | 2016-03-07 22:14:47 -0800 |
commit | f3be74e24f4b460bf309e4b6289973c34b137382 (patch) | |
tree | da08b5c577685b6fc395d87a287b8d8717cbb149 /src | |
parent | a55c8f370bd663ea1d31fed230987711aa1cf176 (diff) |
Improved error management
Diffstat (limited to 'src')
-rwxr-xr-x | src/async/imap/MCAsyncIMAP.h | 1 | ||||
-rwxr-xr-x | src/async/imap/MCIMAPAsyncSession.cpp | 2 | ||||
-rwxr-xr-x | src/async/imap/MCIMAPAsyncSession.h | 3 | ||||
-rw-r--r-- | src/async/imap/MCIMAPCheckAccountOperation.cpp | 21 | ||||
-rw-r--r-- | src/async/imap/MCIMAPCheckAccountOperation.h | 9 | ||||
-rw-r--r-- | src/cmake/objc.cmake | 1 | ||||
-rw-r--r-- | src/cmake/public-headers.cmake | 1 | ||||
-rwxr-xr-x | src/core/imap/MCIMAPSession.cpp | 6 | ||||
-rwxr-xr-x | src/core/imap/MCIMAPSession.h | 2 | ||||
-rw-r--r-- | src/core/provider/MCAccountValidator.cpp | 11 | ||||
-rw-r--r-- | src/core/provider/MCAccountValidator.h | 4 | ||||
-rw-r--r-- | src/objc/imap/MCOIMAPCheckAccountOperation.h | 22 | ||||
-rw-r--r-- | src/objc/imap/MCOIMAPCheckAccountOperation.mm | 74 | ||||
-rwxr-xr-x | src/objc/imap/MCOIMAPSession.mm | 2 |
14 files changed, 153 insertions, 6 deletions
diff --git a/src/async/imap/MCAsyncIMAP.h b/src/async/imap/MCAsyncIMAP.h index 1fb51de0..e6d6d367 100755 --- a/src/async/imap/MCAsyncIMAP.h +++ b/src/async/imap/MCAsyncIMAP.h @@ -35,5 +35,6 @@ #include <MailCore/MCIMAPOperationCallback.h> #include <MailCore/MCIMAPMessageRenderingOperation.h> #include <MailCore/MCIMAPCustomCommandOperation.h> +#include <MailCore/MCIMAPCheckAccountOperation.h> #endif diff --git a/src/async/imap/MCIMAPAsyncSession.cpp b/src/async/imap/MCIMAPAsyncSession.cpp index 976dce95..aa7be032 100755 --- a/src/async/imap/MCIMAPAsyncSession.cpp +++ b/src/async/imap/MCIMAPAsyncSession.cpp @@ -719,7 +719,7 @@ IMAPOperation * IMAPAsyncSession::connectOperation() return op; } -IMAPOperation * IMAPAsyncSession::checkAccountOperation() +IMAPCheckAccountOperation * IMAPAsyncSession::checkAccountOperation() { IMAPCheckAccountOperation * op = new IMAPCheckAccountOperation(); op->setMainSession(this); diff --git a/src/async/imap/MCIMAPAsyncSession.h b/src/async/imap/MCIMAPAsyncSession.h index f5129a26..873c3dde 100755 --- a/src/async/imap/MCIMAPAsyncSession.h +++ b/src/async/imap/MCIMAPAsyncSession.h @@ -42,6 +42,7 @@ namespace mailcore { class IMAPIdentity; class OperationQueueCallback; class IMAPCustomCommandOperation; + class IMAPCheckAccountOperation; class MAILCORE_EXPORT IMAPAsyncSession : public Object { public: @@ -163,7 +164,7 @@ namespace mailcore { virtual IMAPIdentityOperation * identityOperation(IMAPIdentity * identity); virtual IMAPOperation * connectOperation(); - virtual IMAPOperation * checkAccountOperation(); + virtual IMAPCheckAccountOperation * checkAccountOperation(); virtual IMAPOperation * disconnectOperation(); virtual IMAPCapabilityOperation * capabilityOperation(); diff --git a/src/async/imap/MCIMAPCheckAccountOperation.cpp b/src/async/imap/MCIMAPCheckAccountOperation.cpp index 5baa89a9..4a833711 100644 --- a/src/async/imap/MCIMAPCheckAccountOperation.cpp +++ b/src/async/imap/MCIMAPCheckAccountOperation.cpp @@ -13,11 +13,30 @@ using namespace mailcore; +IMAPCheckAccountOperation::IMAPCheckAccountOperation() +{ + mLoginResponse = NULL; +} + +IMAPCheckAccountOperation::~IMAPCheckAccountOperation() +{ + MC_SAFE_RELEASE(mLoginResponse); +} + void IMAPCheckAccountOperation::main() { ErrorCode error; session()->session()->connectIfNeeded(&error); - if (error == ErrorNone) + if (error == ErrorNone) { session()->session()->login(&error); + if (error != ErrorNone) { + MC_SAFE_REPLACE_COPY(String, mLoginResponse, session()->session()->loginResponse()); + } + } setError(error); } + +String * IMAPCheckAccountOperation::loginResponse() +{ + return mLoginResponse; +} diff --git a/src/async/imap/MCIMAPCheckAccountOperation.h b/src/async/imap/MCIMAPCheckAccountOperation.h index 9c81f00e..b6ce9dbf 100644 --- a/src/async/imap/MCIMAPCheckAccountOperation.h +++ b/src/async/imap/MCIMAPCheckAccountOperation.h @@ -17,8 +17,17 @@ namespace mailcore { class MAILCORE_EXPORT IMAPCheckAccountOperation : public IMAPOperation { + public: + IMAPCheckAccountOperation(); + virtual ~IMAPCheckAccountOperation(); + + virtual String * loginResponse(); + public: // subclass behavior virtual void main(); + + private: + String * mLoginResponse; }; } diff --git a/src/cmake/objc.cmake b/src/cmake/objc.cmake index aeb01d3b..f4e10b28 100644 --- a/src/cmake/objc.cmake +++ b/src/cmake/objc.cmake @@ -43,6 +43,7 @@ set(objc_imap_files objc/imap/MCOIMAPSearchOperation.mm objc/imap/MCOIMAPNoopOperation.mm objc/imap/MCOIMAPCustomCommandOperation.mm + objc/imap/MCOIMAPCheckAccountOperation.mm objc/imap/MCOIMAPSession.mm ) diff --git a/src/cmake/public-headers.cmake b/src/cmake/public-headers.cmake index bcbdb0b6..a6bd7f93 100644 --- a/src/cmake/public-headers.cmake +++ b/src/cmake/public-headers.cmake @@ -102,6 +102,7 @@ async/imap/MCIMAPQuotaOperation.h async/imap/MCIMAPOperationCallback.h async/imap/MCIMAPMessageRenderingOperation.h async/imap/MCIMAPCustomCommandOperation.h +async/imap/MCIMAPCheckAccountOperation.h async/pop/MCAsyncPOP.h async/pop/MCPOPAsyncSession.h async/pop/MCPOPOperation.h diff --git a/src/core/imap/MCIMAPSession.cpp b/src/core/imap/MCIMAPSession.cpp index 8b8565d9..8f404cc9 100755 --- a/src/core/imap/MCIMAPSession.cpp +++ b/src/core/imap/MCIMAPSession.cpp @@ -480,6 +480,11 @@ bool IMAPSession::isVoIPEnabled() return mVoIPEnabled; } +String * IMAPSession::loginResponse() +{ + return mLoginResponse; +} + static bool hasError(int errorCode) { return ((errorCode != MAILIMAP_NO_ERROR) && (errorCode != MAILIMAP_NO_ERROR_AUTHENTICATED) && @@ -829,6 +834,7 @@ void IMAPSession::login(ErrorCode * pError) if (mImap->imap_response != NULL) { response = String::stringWithUTF8Characters(mImap->imap_response); } + MC_SAFE_REPLACE_COPY(String, mLoginResponse, response); if (response->locationOfString(MCSTR("not enabled for IMAP use")) != -1) { * pError = ErrorGmailIMAPNotEnabled; } diff --git a/src/core/imap/MCIMAPSession.h b/src/core/imap/MCIMAPSession.h index 9c6ba95b..fa230d40 100755 --- a/src/core/imap/MCIMAPSession.h +++ b/src/core/imap/MCIMAPSession.h @@ -207,6 +207,8 @@ namespace mailcore { /** Check if the automatic query of the capabilities of the IMAP server is enabled. */ virtual bool isAutomaticConfigurationEnabled(); + + virtual String * loginResponse(); public: // private virtual void loginIfNeeded(ErrorCode * pError); diff --git a/src/core/provider/MCAccountValidator.cpp b/src/core/provider/MCAccountValidator.cpp index 1e6ebdd9..d35bd000 100644 --- a/src/core/provider/MCAccountValidator.cpp +++ b/src/core/provider/MCAccountValidator.cpp @@ -18,6 +18,7 @@ #include "MCPOPOperation.h" #include "MCSMTPOperation.h" #include "MCMXRecordResolverOperation.h" +#include "MCIMAPCheckAccountOperation.h" using namespace mailcore; @@ -47,6 +48,7 @@ void AccountValidator::init() mImapError = ErrorNone; mPopError = ErrorNone; mSmtpError = ErrorNone; + mImapLoginResponse = NULL; mCurrentServiceIndex = 0; mCurrentServiceTested = 0; @@ -77,6 +79,7 @@ AccountValidator::AccountValidator() AccountValidator::~AccountValidator() { pthread_mutex_destroy(&mConnectionLoggerLock); + MC_SAFE_RELEASE(mImapLoginResponse); MC_SAFE_RELEASE(mEmail); MC_SAFE_RELEASE(mUsername); MC_SAFE_RELEASE(mPassword); @@ -256,7 +259,7 @@ void AccountValidator::checkNextHost() mImapSession->setConnectionType(mImapServer->connectionType()); mImapSession->setConnectionLogger(this); - mOperation = (IMAPOperation *)mImapSession->checkAccountOperation(); + mOperation = mImapSession->checkAccountOperation(); mOperation->retain(); mOperation->setCallback(this); mOperation->start(); @@ -340,6 +343,7 @@ void AccountValidator::checkNextHostDone() if (mCurrentServiceTested == SERVICE_IMAP) { mImapError = ((IMAPOperation *)mOperation)->error(); + MC_SAFE_REPLACE_COPY(String, mImapLoginResponse, ((IMAPCheckAccountOperation *)mOperation)->loginResponse()); error = mImapError; mImapSession->setConnectionLogger(NULL); MC_SAFE_RELEASE(mImapSession); @@ -506,6 +510,11 @@ ErrorCode AccountValidator::smtpError() return mSmtpError; } +String * AccountValidator::imapLoginResponse() +{ + return mImapLoginResponse; +} + void AccountValidator::setConnectionLogger(ConnectionLogger * logger) { pthread_mutex_lock(&mConnectionLoggerLock); diff --git a/src/core/provider/MCAccountValidator.h b/src/core/provider/MCAccountValidator.h index 788458b6..7029870f 100644 --- a/src/core/provider/MCAccountValidator.h +++ b/src/core/provider/MCAccountValidator.h @@ -64,7 +64,8 @@ namespace mailcore { virtual ErrorCode imapError(); virtual ErrorCode popError(); virtual ErrorCode smtpError(); - + virtual String * imapLoginResponse(); + virtual void start(); virtual void cancel(); @@ -89,6 +90,7 @@ namespace mailcore { ErrorCode mImapError; ErrorCode mPopError; ErrorCode mSmtpError; + String * mImapLoginResponse; MailProvider * mProvider; diff --git a/src/objc/imap/MCOIMAPCheckAccountOperation.h b/src/objc/imap/MCOIMAPCheckAccountOperation.h new file mode 100644 index 00000000..f2ad3340 --- /dev/null +++ b/src/objc/imap/MCOIMAPCheckAccountOperation.h @@ -0,0 +1,22 @@ +// +// MCOIMAPCheckAccountOperation.h +// mailcore2 +// +// Created by Hoa V. DINH on 3/7/16. +// Copyright © 2016 MailCore. All rights reserved. +// + +#ifndef MAILCORE_MCOIMAPCHECKACCOUNTOPERATION_H + +#define MAILCORE_MCOIMAPCHECKACCOUNTOPERATION_H + +#import <MailCore/MCOIMAPOperation.h> + +NS_ASSUME_NONNULL_BEGIN +/* The class is used to perform a No-Op operation. */ +@interface MCOIMAPCheckAccountOperation : MCOIMAPOperation + +@end +NS_ASSUME_NONNULL_END + +#endif diff --git a/src/objc/imap/MCOIMAPCheckAccountOperation.mm b/src/objc/imap/MCOIMAPCheckAccountOperation.mm new file mode 100644 index 00000000..f1aa59b4 --- /dev/null +++ b/src/objc/imap/MCOIMAPCheckAccountOperation.mm @@ -0,0 +1,74 @@ +// +// MCOIMAPCheckAccountOperation.m +// mailcore2 +// +// Created by Hoa V. DINH on 3/7/16. +// Copyright © 2016 MailCore. All rights reserved. +// + +#import "MCOIMAPCheckAccountOperation.h" + +#include "MCIMAPCheckAccountOperation.h" + +#import "MCOOperation+Private.h" +#import "MCOUtils.h" + +typedef void (^CompletionType)(NSError *error); + +@implementation MCOIMAPCheckAccountOperation { + CompletionType _completionBlock; +} + +#define nativeType mailcore::IMAPCheckAccountOperation + ++ (void) load +{ + MCORegisterClass(self, &typeid(nativeType)); +} + ++ (NSObject *) mco_objectWithMCObject:(mailcore::Object *)object +{ + nativeType * op = (nativeType *) object; + return [[[self alloc] initWithMCOperation:op] autorelease]; +} + +- (void) dealloc +{ + [_completionBlock release]; + [super dealloc]; +} + +- (void) start:(void (^)(NSError *error))completionBlock +{ + _completionBlock = [completionBlock copy]; + [self start]; +} + +- (void) cancel +{ + [_completionBlock release]; + _completionBlock = nil; + [super cancel]; +} + +- (void) operationCompleted +{ + if (_completionBlock == NULL) + return; + + nativeType *op = MCO_NATIVE_INSTANCE; + if (op->error() == mailcore::ErrorNone) { + _completionBlock(nil); + } else { + NSError * error = [NSError mco_errorWithErrorCode:op->error()]; + if (op->loginResponse() != NULL) { + NSDictionary * userInfo = @{@"IMAPServerError": MCO_TO_OBJC(op->loginResponse())}; + error = [NSError errorWithDomain:[error domain] code:[error code] userInfo:userInfo]; + } + _completionBlock(error); + } + [_completionBlock release]; + _completionBlock = nil; +} + +@end diff --git a/src/objc/imap/MCOIMAPSession.mm b/src/objc/imap/MCOIMAPSession.mm index fdd57886..bec998c8 100755 --- a/src/objc/imap/MCOIMAPSession.mm +++ b/src/objc/imap/MCOIMAPSession.mm @@ -616,7 +616,7 @@ MCO_OBJC_SYNTHESIZE_SCALAR(dispatch_queue_t, dispatch_queue_t, setDispatchQueue, - (MCOIMAPOperation *)checkAccountOperation { IMAPOperation *coreOp = MCO_NATIVE_INSTANCE->checkAccountOperation(); - return OPAQUE_OPERATION(coreOp); + return MCO_TO_OBJC_OP(coreOp); } - (MCOIMAPCapabilityOperation *) capabilityOperation |