diff options
Diffstat (limited to 'src/objc/smtp/MCOSMTPSendOperation.mm')
-rw-r--r-- | src/objc/smtp/MCOSMTPSendOperation.mm | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/objc/smtp/MCOSMTPSendOperation.mm b/src/objc/smtp/MCOSMTPSendOperation.mm new file mode 100644 index 00000000..fcad264b --- /dev/null +++ b/src/objc/smtp/MCOSMTPSendOperation.mm @@ -0,0 +1,99 @@ +// +// MCOSMTPSendOperation.m +// mailcore2 +// +// Created by DINH Viêt Hoà on 3/29/13. +// Copyright (c) 2013 MailCore. All rights reserved. +// + +#import "MCOSMTPSendOperation.h" + +#include "MCAsyncSMTP.h" + +#import "MCOUtils.h" +#import "MCOOperation+Private.h" + +#define nativeType mailcore::SMTPOperation + +typedef void (^CompletionType)(NSError *error); + +@interface MCOSMTPSendOperation () + +- (void) bodyProgress:(unsigned int)current maximum:(unsigned int)maximum; + +@end + +class MCOSMTPSendOperationCallback : public mailcore::SMTPOperationCallback { +public: + MCOSMTPSendOperationCallback(MCOSMTPSendOperation * op) + { + mOperation = op; + } + + virtual void bodyProgress(mailcore::SMTPOperation * session, unsigned int current, unsigned int maximum) { + [mOperation bodyProgress:current maximum:maximum]; + } + +private: + MCOSMTPSendOperation * mOperation; +}; + +@implementation MCOSMTPSendOperation { + CompletionType _completionBlock; + MCOSMTPSendOperationCallback * _smtpCallback; + MCOSMTPOperationProgressBlock _progress; +} + +@synthesize progress = _progress; + ++ (void) load +{ + MCORegisterClass(self, &typeid(nativeType)); +} + ++ (NSObject *) mco_objectWithMCObject:(mailcore::Object *)object +{ + nativeType * op = (nativeType *) object; + return [[[self alloc] initWithMCOperation:op] autorelease]; +} + +- (id)initWithMCOperation:(mailcore::Operation *)op +{ + self = [super initWithMCOperation:op]; + + _smtpCallback = new MCOSMTPSendOperationCallback(self); + ((mailcore::SMTPOperation *) op)->setSmtpCallback(_smtpCallback); + + return self; +} + +- (void) dealloc +{ + [_progress release]; + [_completionBlock release]; + delete _smtpCallback; + [super dealloc]; +} + +- (void)start:(void (^)(NSError *error))completionBlock { + _completionBlock = [completionBlock copy]; + [self start]; +} + +- (void)operationCompleted { + nativeType *op = MCO_NATIVE_INSTANCE; + if (op->error() == mailcore::ErrorNone) { + _completionBlock(nil); + } else { + _completionBlock([NSError mco_errorWithErrorCode:op->error()]); + } +} + +- (void) bodyProgress:(unsigned int)current maximum:(unsigned int)maximum +{ + if (_progress != NULL) { + _progress(current, maximum); + } +} + +@end |