aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/java/native/com_libmailcore_Operation.cpp
blob: 08d518b6ef5fb17e432911ead282d5c3cc8d7b0c (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
#include "com_libmailcore_Operation.h"

#include "TypesUtils.h"
#include "JavaHandle.h"
#include "MCOperation.h"
#include "MCDefines.h"

using namespace mailcore;

#define nativeType Operation
#define javaType nativeType

class JavaOperationCallback : public Object, public OperationCallback {
public:
    JavaOperationCallback(JNIEnv * env, jobject obj) {
        mEnv = env;
        mGlobalRef = env->NewGlobalRef(obj);
    }
    
    virtual ~JavaOperationCallback() {
        if (mGlobalRef != NULL) {
            mEnv->DeleteGlobalRef(mGlobalRef);
            mGlobalRef = NULL;
        }
    }
    
    virtual void operationFinished(Operation * op)
    {
        jclass cls = mEnv->GetObjectClass(mGlobalRef);
        jmethodID mid = mEnv->GetMethodID(cls, "callCallback", "()V");
        mEnv->CallVoidMethod(mGlobalRef, mid);
        
        mEnv->DeleteGlobalRef(mGlobalRef);
        mGlobalRef = NULL;
        
        this->release();
    }
    
private:
    JNIEnv * mEnv;
    jobject mGlobalRef;
};

JNIEXPORT void JNICALL Java_com_libmailcore_Operation_cancel
  (JNIEnv * env, jobject obj)
{
    MC_POOL_BEGIN;
    MC_JAVA_NATIVE_INSTANCE->cancel();
    MC_POOL_END;
}

JNIEXPORT jboolean JNICALL Java_com_libmailcore_Operation_isCancelled
  (JNIEnv * env, jobject obj)
{
    MC_POOL_BEGIN;
    jboolean result = MC_JAVA_BRIDGE_GET_SCALAR(jboolean, isCancelled);
    MC_POOL_END;
    return result;
}

JNIEXPORT void JNICALL Java_com_libmailcore_Operation_nativeStart
  (JNIEnv * env, jobject obj)
{
    MC_POOL_BEGIN;
    JavaOperationCallback * callback = new JavaOperationCallback(env, obj);
    MC_JAVA_NATIVE_INSTANCE->setCallback(callback);
    MC_JAVA_NATIVE_INSTANCE->start();
    MC_POOL_END;
}

MC_JAVA_BRIDGE