aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/async/imap/MCIMAPOperation.cc
blob: 35eedd6caa9b39ebc30b24c2bffc4702de16cc6f (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
//  MCIMAPOperation.cc
//  mailcore2
//
//  Created by DINH Viêt Hoà on 1/12/13.
//  Copyright (c) 2013 MailCore. All rights reserved.
//

#include "MCIMAPOperation.h"

#include <stdlib.h>

#include "MCIMAPSession.h"
#include "MCIMAPAsyncConnection.h"
#include "MCIMAPOperationCallback.h"

using namespace mailcore;

IMAPOperation::IMAPOperation()
{
    mSession = NULL;
    mImapCallback = NULL;
    mError = ErrorNone;
    mFolder = NULL;
}

IMAPOperation::~IMAPOperation()
{
    MC_SAFE_RELEASE(mFolder);
    MC_SAFE_RELEASE(mSession);
}

void IMAPOperation::setSession(IMAPAsyncConnection * session)
{
    MC_SAFE_REPLACE_RETAIN(IMAPAsyncConnection, mSession, session);
}

IMAPAsyncConnection * IMAPOperation::session()
{
    return mSession;
}

void IMAPOperation::setFolder(String * folder)
{
    MC_SAFE_REPLACE_COPY(String, mFolder, folder);
}

String * IMAPOperation::folder()
{
    return mFolder;
}

void IMAPOperation::setImapCallback(IMAPOperationCallback * callback)
{
    mImapCallback = callback;
}

IMAPOperationCallback * IMAPOperation::imapCallback()
{
    return mImapCallback;
}

void IMAPOperation::setError(ErrorCode error)
{
    mError = error;
}

ErrorCode IMAPOperation::error()
{
    return mError;
}

void IMAPOperation::start()
{
    mSession->runOperation(this);
}

struct progressContext {
    unsigned int current;
    unsigned int maximum;
};

void IMAPOperation::bodyProgress(IMAPSession * session, unsigned int current, unsigned int maximum)
{
    if (isCancelled())
        return;
    
    struct progressContext * context = (struct progressContext *) calloc(sizeof(* context), 1);
    context->current = current;
    context->maximum = maximum;
    retain();
    performMethodOnMainThread((Object::Method) &IMAPOperation::bodyProgressOnMainThread, context);
}

void IMAPOperation::bodyProgressOnMainThread(void * ctx)
{
    if (isCancelled()) {
        release();
        return;
    }
    
    struct progressContext * context = (struct progressContext *) ctx;
    if (mImapCallback != NULL) {
        mImapCallback->bodyProgress(this, context->current, context->maximum);
    }
    free(context);
    release();
}

void IMAPOperation::itemsProgress(IMAPSession * session, unsigned int current, unsigned int maximum)
{
    if (isCancelled())
        return;
    
    struct progressContext * context = (struct progressContext *) calloc(sizeof(* context), 1);
    context->current = current;
    context->maximum = maximum;
    retain();
    performMethodOnMainThread((Object::Method) &IMAPOperation::itemsProgressOnMainThread, context);
}

void IMAPOperation::itemsProgressOnMainThread(void * ctx)
{
    if (isCancelled()) {
        release();
        return;
    }
    
    struct progressContext * context = (struct progressContext *) ctx;
    if (mImapCallback != NULL) {
        mImapCallback->itemProgress(this, context->current, context->maximum);
    }
    free(context);
    release();
}