aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/objc/smtp/MCOSMTPSendOperation.mm
blob: e02550da577a543eff09f273cd582855336efe6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
//  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 ~MCOSMTPSendOperationCallback()
    {
    }
    
    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];
}

- (instancetype) 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) cancel
{
    [_completionBlock release];
    _completionBlock = nil;
    [super cancel];
}

// This method needs to be duplicated from MCOSMTPOperation since _completionBlock
// references the instance of this subclass and not the one from MCOSMTPOperation.
- (void)operationCompleted
{
    if (_completionBlock == NULL)
        return;
    
    NSError * error = [NSError mco_errorWithErrorCode:MCO_NATIVE_INSTANCE->error()];
    _completionBlock(error);
    [_completionBlock release];
    _completionBlock = NULL;
}

- (void) bodyProgress:(unsigned int)current maximum:(unsigned int)maximum
{
    if (_progress != NULL) {
        _progress(current, maximum);
    }
}

@end