aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/async/nntp/MCNNTPOperation.cpp
blob: 3368ba0e7e3cd5bec411e8142ba7e325c814ef1b (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
//
//  MCNNTPOperation.cpp
//  mailcore2
//
//  Created by Robert Widmann on 8/13/14.
//  Copyright (c) 2014 MailCore. All rights reserved.
//

#include "MCNNTPOperation.h"

#include <stdlib.h>

#include "MCNNTPSession.h"
#include "MCNNTPAsyncSession.h"
#include "MCNNTPOperationCallback.h"

using namespace mailcore;

NNTPOperation::NNTPOperation()
{
    mSession = NULL;
    mPopCallback = NULL;
    mError = ErrorNone;
}

NNTPOperation::~NNTPOperation()
{
    MC_SAFE_RELEASE(mSession);
}

void NNTPOperation::setSession(NNTPAsyncSession * session)
{
    MC_SAFE_REPLACE_RETAIN(NNTPAsyncSession, mSession, session);
#if __APPLE__
    dispatch_queue_t queue;
    if (session != NULL) {
        queue = session->dispatchQueue();
    }
    else {
        queue = dispatch_get_main_queue();
    }
    setCallbackDispatchQueue(queue);
#endif
}

NNTPAsyncSession * NNTPOperation::session()
{
    return mSession;
}

void NNTPOperation::setNNTPCallback(NNTPOperationCallback * callback)
{
    mPopCallback = callback;
}

NNTPOperationCallback * NNTPOperation::nntpCallback()
{
    return mPopCallback;
}

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

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

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

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

void NNTPOperation::bodyProgress(NNTPSession * session, unsigned int current, unsigned int maximum)
{
    struct progressContext * context = (struct progressContext *) calloc(sizeof(* context), 1);
    context->current = current;
    context->maximum = maximum;
    
    retain();
    performMethodOnCallbackThread((Object::Method) &NNTPOperation::bodyProgressOnMainThread, context);
}

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