From fdb0bc090dcde12cc960305354f2e978f1746d7e Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Fri, 8 Jun 2018 16:55:35 +0430 Subject: [PATCH 1/3] feat(IMAPSession): add support for moveMessagesOpeation to java library --- .../IMAPMoveMessagesOperation.java | 12 ++++++++++ src/java/com/libmailcore/IMAPSession.java | 3 +++ ..._libmailcore_IMAPMoveMessagesOperation.cpp | 22 ++++++++++++++++++ ...om_libmailcore_IMAPMoveMessagesOperation.h | 23 +++++++++++++++++++ .../native/com_libmailcore_IMAPSession.cpp | 10 ++++++++ src/java/native/com_libmailcore_IMAPSession.h | 8 +++++++ 6 files changed, 78 insertions(+) create mode 100644 src/java/com/libmailcore/IMAPMoveMessagesOperation.java create mode 100644 src/java/native/com_libmailcore_IMAPMoveMessagesOperation.cpp create mode 100644 src/java/native/com_libmailcore_IMAPMoveMessagesOperation.h diff --git a/src/java/com/libmailcore/IMAPMoveMessagesOperation.java b/src/java/com/libmailcore/IMAPMoveMessagesOperation.java new file mode 100644 index 000000000..eab74818e --- /dev/null +++ b/src/java/com/libmailcore/IMAPMoveMessagesOperation.java @@ -0,0 +1,12 @@ +package com.libmailcore; + +import java.util.Map; + +/** Operation to move IMAP messages. */ +public class IMAPMoveMessagesOperation extends IMAPOperation { + /** + Returns a map of the UIDs of the messages in the source folder to the UIDs of + the messages in the destination folder. + */ + public native Map uidMapping(); +} diff --git a/src/java/com/libmailcore/IMAPSession.java b/src/java/com/libmailcore/IMAPSession.java index 27ab84610..b59453081 100644 --- a/src/java/com/libmailcore/IMAPSession.java +++ b/src/java/com/libmailcore/IMAPSession.java @@ -160,6 +160,9 @@ public IMAPAppendMessageOperation appendMessageOperation(String folder, byte[] m /** Returns an operation to copy messages to a folder. */ public native IMAPCopyMessagesOperation copyMessagesOperation(String folder, IndexSet uids, String destFolder); + /** Returns an operation to move messages to a folder. */ + public native IMAPMoveMessagesOperation moveMessagesOperation(String folder, IndexSet uids, String destFolder); + /** Returns an operation to expunge messages after they've been marked as deleted. */ public native IMAPOperation expungeOperation(String folder); diff --git a/src/java/native/com_libmailcore_IMAPMoveMessagesOperation.cpp b/src/java/native/com_libmailcore_IMAPMoveMessagesOperation.cpp new file mode 100644 index 000000000..be6f974b4 --- /dev/null +++ b/src/java/native/com_libmailcore_IMAPMoveMessagesOperation.cpp @@ -0,0 +1,22 @@ +#include "com_libmailcore_IMAPMoveMessagesOperation.h" + +#include "MCBaseTypes.h" +#include "JavaHandle.h" +#include "TypesUtils.h" +#include "MCIMAPMoveMessagesOperation.h" + +using namespace mailcore; + +#define nativeType IMAPMoveMessagesOperation +#define javaType nativeType + +JNIEXPORT jobject JNICALL Java_com_libmailcore_IMAPMoveMessagesOperation_uidMapping + (JNIEnv * env, jobject obj) +{ + MC_POOL_BEGIN; + jobject result = MC_JAVA_BRIDGE_GET(uidMapping); + MC_POOL_END; + return result; +} + +MC_JAVA_BRIDGE diff --git a/src/java/native/com_libmailcore_IMAPMoveMessagesOperation.h b/src/java/native/com_libmailcore_IMAPMoveMessagesOperation.h new file mode 100644 index 000000000..d4dede646 --- /dev/null +++ b/src/java/native/com_libmailcore_IMAPMoveMessagesOperation.h @@ -0,0 +1,23 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class com_libmailcore_IMAPMoveMessagesOperation */ + +#ifndef _Included_com_libmailcore_IMAPMoveMessagesOperation +#define _Included_com_libmailcore_IMAPMoveMessagesOperation +#ifdef __cplusplus +extern "C" { +#endif +#undef com_libmailcore_IMAPMoveMessagesOperation_serialVersionUID +#define com_libmailcore_IMAPMoveMessagesOperation_serialVersionUID 1LL +/* + * Class: com_libmailcore_IMAPMoveMessagesOperation + * Method: uidMapping + * Signature: ()Ljava/util/Map; + */ +JNIEXPORT jobject JNICALL Java_com_libmailcore_IMAPMoveMessagesOperation_uidMapping + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/java/native/com_libmailcore_IMAPSession.cpp b/src/java/native/com_libmailcore_IMAPSession.cpp index 59422f35f..8be3a37f4 100644 --- a/src/java/native/com_libmailcore_IMAPSession.cpp +++ b/src/java/native/com_libmailcore_IMAPSession.cpp @@ -175,6 +175,16 @@ JNIEXPORT jobject JNICALL Java_com_libmailcore_IMAPSession_copyMessagesOperation return result; } +JNIEXPORT jobject JNICALL Java_com_libmailcore_IMAPSession_moveMessagesOperation + (JNIEnv * env, jobject obj, jstring sourcePath, jobject uids, jstring destPath) +{ + MC_POOL_BEGIN; + jobject result = MC_TO_JAVA(MC_JAVA_NATIVE_INSTANCE->moveMessagesOperation(MC_FROM_JAVA(String, sourcePath), + MC_FROM_JAVA(IndexSet, uids), MC_FROM_JAVA(String, destPath))); + MC_POOL_END; + return result; +} + JNIEXPORT jobject JNICALL Java_com_libmailcore_IMAPSession_expungeOperation (JNIEnv * env, jobject obj, jstring path) { diff --git a/src/java/native/com_libmailcore_IMAPSession.h b/src/java/native/com_libmailcore_IMAPSession.h index 8d6ee73cb..2ed1f374e 100644 --- a/src/java/native/com_libmailcore_IMAPSession.h +++ b/src/java/native/com_libmailcore_IMAPSession.h @@ -329,6 +329,14 @@ JNIEXPORT jobject JNICALL Java_com_libmailcore_IMAPSession_appendMessageOperatio JNIEXPORT jobject JNICALL Java_com_libmailcore_IMAPSession_copyMessagesOperation (JNIEnv *, jobject, jstring, jobject, jstring); +/* + * Class: com_libmailcore_IMAPSession + * Method: moveMessagesOperation + * Signature: (Ljava/lang/String;Lcom/libmailcore/IndexSet;Ljava/lang/String;)Lcom/libmailcore/IMAPMoveMessagesOperation; + */ +JNIEXPORT jobject JNICALL Java_com_libmailcore_IMAPSession_moveMessagesOperation + (JNIEnv *, jobject, jstring, jobject, jstring); + /* * Class: com_libmailcore_IMAPSession * Method: expungeOperation From 138851c0e009e533577c749ea2c232b4e6cfa9ca Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Fri, 5 Oct 2018 02:33:05 +0330 Subject: [PATCH 2/3] feat: support fieldClass in getObjectField --- src/java/JavaHandle.cpp | 14 +++++++------- src/java/JavaHandle.h | 2 +- .../com_libmailcore_IMAPAppendMessageOperation.cpp | 2 +- .../com_libmailcore_IMAPFetchContentOperation.cpp | 2 +- .../com_libmailcore_IMAPFetchMessagesOperation.cpp | 2 +- ...libmailcore_IMAPFetchParsedContentOperation.cpp | 2 +- src/java/native/com_libmailcore_IMAPSession.cpp | 4 ++-- src/java/native/com_libmailcore_NNTPSession.cpp | 4 ++-- src/java/native/com_libmailcore_POPSession.cpp | 4 ++-- src/java/native/com_libmailcore_SMTPSession.cpp | 4 ++-- 10 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/java/JavaHandle.cpp b/src/java/JavaHandle.cpp index 1bf799e3a..fdefc49a7 100644 --- a/src/java/JavaHandle.cpp +++ b/src/java/JavaHandle.cpp @@ -2,15 +2,15 @@ using namespace mailcore; -static jfieldID getField(JNIEnv * env, jobject obj, const char * fieldName) +static jfieldID getField(JNIEnv * env, jobject obj, const char * fieldName, const char * fieldClass) { jclass c = env->GetObjectClass(obj); - return env->GetFieldID(c, fieldName, "J"); + return env->GetFieldID(c, fieldName, fieldClass); } static jfieldID getHandleField(JNIEnv * env, jobject obj) { - return getField(env, obj, "nativeHandle"); + return getField(env, obj, "nativeHandle", "J"); } void * mailcore::getHandle(JNIEnv * env, jobject obj) @@ -27,17 +27,17 @@ void mailcore::setHandle(JNIEnv *env, jobject obj, void *t) void * mailcore::getCustomHandle(JNIEnv * env, jobject obj, const char * fieldName) { - jlong handle = env->GetLongField(obj, getField(env, obj, fieldName)); + jlong handle = env->GetLongField(obj, getField(env, obj, fieldName, "J")); return (void *) handle; } void mailcore::setCustomHandle(JNIEnv *env, jobject obj, const char * fieldName, void *t) { jlong handle = reinterpret_cast(t); - env->SetLongField(obj, getField(env, obj, fieldName), handle); + env->SetLongField(obj, getField(env, obj, fieldName, "J"), handle); } -jobject mailcore::getObjectField(JNIEnv *env, jobject obj, const char * fieldName) +jobject mailcore::getObjectField(JNIEnv *env, jobject obj, const char * fieldName, const char * fieldClass) { - return env->GetObjectField(obj, getField(env, obj, fieldName)); + return env->GetObjectField(obj, getField(env, obj, fieldName, fieldClass)); } \ No newline at end of file diff --git a/src/java/JavaHandle.h b/src/java/JavaHandle.h index 19e8aee8e..6c2b94135 100644 --- a/src/java/JavaHandle.h +++ b/src/java/JavaHandle.h @@ -9,7 +9,7 @@ namespace mailcore { void setHandle(JNIEnv * env, jobject obj, void * t); void * getCustomHandle(JNIEnv * env, jobject obj, const char * fieldName); void setCustomHandle(JNIEnv *env, jobject obj, const char * fieldName, void *t); - jobject getObjectField(JNIEnv *env, jobject obj, const char * fieldName); + jobject getObjectField(JNIEnv *env, jobject obj, const char * fieldName, const char * fieldClass); } #endif diff --git a/src/java/native/com_libmailcore_IMAPAppendMessageOperation.cpp b/src/java/native/com_libmailcore_IMAPAppendMessageOperation.cpp index 16da4df26..69b17f088 100644 --- a/src/java/native/com_libmailcore_IMAPAppendMessageOperation.cpp +++ b/src/java/native/com_libmailcore_IMAPAppendMessageOperation.cpp @@ -55,7 +55,7 @@ JNIEXPORT void JNICALL Java_com_libmailcore_IMAPAppendMessageOperation_setupNati MC_SAFE_RELEASE(callback); MC_JAVA_NATIVE_INSTANCE->setImapCallback(NULL); - jobject javaListener = getObjectField(env, obj, "listener"); + jobject javaListener = getObjectField(env, obj, "listener", "J"); if (javaListener != NULL) { callback = new JavaIMAPOperationCallback(env, javaListener); MC_JAVA_NATIVE_INSTANCE->setImapCallback(callback); diff --git a/src/java/native/com_libmailcore_IMAPFetchContentOperation.cpp b/src/java/native/com_libmailcore_IMAPFetchContentOperation.cpp index a8be97b86..9dfff9b04 100644 --- a/src/java/native/com_libmailcore_IMAPFetchContentOperation.cpp +++ b/src/java/native/com_libmailcore_IMAPFetchContentOperation.cpp @@ -38,7 +38,7 @@ JNIEXPORT void JNICALL Java_com_libmailcore_IMAPFetchContentOperation_setupNativ MC_SAFE_RELEASE(callback); MC_JAVA_NATIVE_INSTANCE->setImapCallback(NULL); - jobject javaListener = getObjectField(env, obj, "listener"); + jobject javaListener = getObjectField(env, obj, "listener", "Lcom/libmailcore/IMAPOperationProgressListener;"); if (javaListener != NULL) { callback = new JavaIMAPOperationCallback(env, javaListener); MC_JAVA_NATIVE_INSTANCE->setImapCallback(callback); diff --git a/src/java/native/com_libmailcore_IMAPFetchMessagesOperation.cpp b/src/java/native/com_libmailcore_IMAPFetchMessagesOperation.cpp index ed0fae093..c1d07d9fd 100644 --- a/src/java/native/com_libmailcore_IMAPFetchMessagesOperation.cpp +++ b/src/java/native/com_libmailcore_IMAPFetchMessagesOperation.cpp @@ -49,7 +49,7 @@ JNIEXPORT void JNICALL Java_com_libmailcore_IMAPFetchMessagesOperation_setupNati MC_SAFE_RELEASE(callback); MC_JAVA_NATIVE_INSTANCE->setImapCallback(NULL); - jobject javaListener = getObjectField(env, obj, "listener"); + jobject javaListener = getObjectField(env, obj, "listener", "J"); if (javaListener != NULL) { callback = new JavaIMAPOperationCallback(env, javaListener); MC_JAVA_NATIVE_INSTANCE->setImapCallback(callback); diff --git a/src/java/native/com_libmailcore_IMAPFetchParsedContentOperation.cpp b/src/java/native/com_libmailcore_IMAPFetchParsedContentOperation.cpp index da639f2c0..b058b9df2 100644 --- a/src/java/native/com_libmailcore_IMAPFetchParsedContentOperation.cpp +++ b/src/java/native/com_libmailcore_IMAPFetchParsedContentOperation.cpp @@ -38,7 +38,7 @@ JNIEXPORT void JNICALL Java_com_libmailcore_IMAPFetchParsedContentOperation_setu MC_SAFE_RELEASE(callback); MC_JAVA_NATIVE_INSTANCE->setImapCallback(NULL); - jobject javaListener = getObjectField(env, obj, "listener"); + jobject javaListener = getObjectField(env, obj, "listener", "J"); if (javaListener != NULL) { callback = new JavaIMAPOperationCallback(env, javaListener); MC_JAVA_NATIVE_INSTANCE->setImapCallback(callback); diff --git a/src/java/native/com_libmailcore_IMAPSession.cpp b/src/java/native/com_libmailcore_IMAPSession.cpp index 8be3a37f4..2c2ce21cb 100644 --- a/src/java/native/com_libmailcore_IMAPSession.cpp +++ b/src/java/native/com_libmailcore_IMAPSession.cpp @@ -479,7 +479,7 @@ JNIEXPORT void JNICALL Java_com_libmailcore_IMAPSession_setupNativeOperationQueu MC_SAFE_RELEASE(callback); MC_JAVA_NATIVE_INSTANCE->setOperationQueueCallback(NULL); - jobject javaListener = getObjectField(env, obj, "operationQueueListener"); + jobject javaListener = getObjectField(env, obj, "operationQueueListener", "J"); if (javaListener != NULL) { callback = new JavaOperationQueueCallback(env, javaListener); MC_JAVA_NATIVE_INSTANCE->setOperationQueueCallback(callback); @@ -495,7 +495,7 @@ JNIEXPORT void JNICALL Java_com_libmailcore_IMAPSession_setupNativeConnectionLog MC_SAFE_RELEASE(logger); MC_JAVA_NATIVE_INSTANCE->setConnectionLogger(NULL); - jobject javaLogger = getObjectField(env, obj, "connectionLogger"); + jobject javaLogger = getObjectField(env, obj, "connectionLogger", "J"); if (javaLogger != NULL) { logger = new JavaConnectionLogger(env, javaLogger); MC_JAVA_NATIVE_INSTANCE->setConnectionLogger(logger); diff --git a/src/java/native/com_libmailcore_NNTPSession.cpp b/src/java/native/com_libmailcore_NNTPSession.cpp index bbae318cf..c17d4d9c4 100644 --- a/src/java/native/com_libmailcore_NNTPSession.cpp +++ b/src/java/native/com_libmailcore_NNTPSession.cpp @@ -149,7 +149,7 @@ JNIEXPORT void JNICALL Java_com_libmailcore_NNTPSession_setupNativeOperationQueu MC_SAFE_RELEASE(callback); MC_JAVA_NATIVE_INSTANCE->setOperationQueueCallback(NULL); - jobject javaListener = getObjectField(env, obj, "operationQueueListener"); + jobject javaListener = getObjectField(env, obj, "operationQueueListener", "J"); if (javaListener != NULL) { callback = new JavaOperationQueueCallback(env, javaListener); MC_JAVA_NATIVE_INSTANCE->setOperationQueueCallback(callback); @@ -165,7 +165,7 @@ JNIEXPORT void JNICALL Java_com_libmailcore_NNTPSession_setupNativeConnectionLog MC_SAFE_RELEASE(logger); MC_JAVA_NATIVE_INSTANCE->setConnectionLogger(NULL); - jobject javaLogger = getObjectField(env, obj, "connectionLogger"); + jobject javaLogger = getObjectField(env, obj, "connectionLogger", "J"); if (javaLogger != NULL) { logger = new JavaConnectionLogger(env, javaLogger); MC_JAVA_NATIVE_INSTANCE->setConnectionLogger(logger); diff --git a/src/java/native/com_libmailcore_POPSession.cpp b/src/java/native/com_libmailcore_POPSession.cpp index f0dbfd400..78e8d794c 100644 --- a/src/java/native/com_libmailcore_POPSession.cpp +++ b/src/java/native/com_libmailcore_POPSession.cpp @@ -123,7 +123,7 @@ JNIEXPORT void JNICALL Java_com_libmailcore_POPSession_setupNativeOperationQueue MC_SAFE_RELEASE(callback); MC_JAVA_NATIVE_INSTANCE->setOperationQueueCallback(NULL); - jobject javaListener = getObjectField(env, obj, "operationQueueListener"); + jobject javaListener = getObjectField(env, obj, "operationQueueListener", "J"); if (javaListener != NULL) { callback = new JavaOperationQueueCallback(env, javaListener); MC_JAVA_NATIVE_INSTANCE->setOperationQueueCallback(callback); @@ -139,7 +139,7 @@ JNIEXPORT void JNICALL Java_com_libmailcore_POPSession_setupNativeConnectionLogg MC_SAFE_RELEASE(logger); MC_JAVA_NATIVE_INSTANCE->setConnectionLogger(NULL); - jobject javaLogger = getObjectField(env, obj, "connectionLogger"); + jobject javaLogger = getObjectField(env, obj, "connectionLogger", "J"); if (javaLogger != NULL) { logger = new JavaConnectionLogger(env, javaLogger); MC_JAVA_NATIVE_INSTANCE->setConnectionLogger(logger); diff --git a/src/java/native/com_libmailcore_SMTPSession.cpp b/src/java/native/com_libmailcore_SMTPSession.cpp index 0d43014c2..c831b1f62 100644 --- a/src/java/native/com_libmailcore_SMTPSession.cpp +++ b/src/java/native/com_libmailcore_SMTPSession.cpp @@ -107,7 +107,7 @@ JNIEXPORT void JNICALL Java_com_libmailcore_SMTPSession_setupNativeOperationQueu MC_SAFE_RELEASE(callback); MC_JAVA_NATIVE_INSTANCE->setOperationQueueCallback(NULL); - jobject javaListener = getObjectField(env, obj, "operationQueueListener"); + jobject javaListener = getObjectField(env, obj, "operationQueueListener", "J"); if (javaListener != NULL) { callback = new JavaOperationQueueCallback(env, javaListener); MC_JAVA_NATIVE_INSTANCE->setOperationQueueCallback(callback); @@ -123,7 +123,7 @@ JNIEXPORT void JNICALL Java_com_libmailcore_SMTPSession_setupNativeConnectionLog MC_SAFE_RELEASE(logger); MC_JAVA_NATIVE_INSTANCE->setConnectionLogger(NULL); - jobject javaLogger = getObjectField(env, obj, "connectionLogger"); + jobject javaLogger = getObjectField(env, obj, "connectionLogger", "J"); if (javaLogger != NULL) { logger = new JavaConnectionLogger(env, javaLogger); MC_JAVA_NATIVE_INSTANCE->setConnectionLogger(logger); From ae24b2db4ab30db53b1faf6f66d655707af4e77e Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Fri, 5 Oct 2018 02:34:51 +0330 Subject: [PATCH 3/3] fix: use global reference of progress listener pass progress values to callback function --- src/java/JavaIMAPOperationCallback.cpp | 4 ++-- src/java/native/com_libmailcore_IMAPFetchContentOperation.cpp | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/java/JavaIMAPOperationCallback.cpp b/src/java/JavaIMAPOperationCallback.cpp index fba857b80..c37e0c585 100644 --- a/src/java/JavaIMAPOperationCallback.cpp +++ b/src/java/JavaIMAPOperationCallback.cpp @@ -29,7 +29,7 @@ void JavaIMAPOperationCallback::bodyProgress(IMAPOperation * session, unsigned i JNIEnv * env = mEnv; jclass cls = mEnv->GetObjectClass(mListener); jmethodID mid = mEnv->GetMethodID(cls, "bodyProgress", "(JJ)V"); - mEnv->CallVoidMethod(mListener, mid); + mEnv->CallVoidMethod(mListener, mid, (jlong) current, (jlong) maximum); } void JavaIMAPOperationCallback::itemProgress(IMAPOperation * session, unsigned int current, unsigned int maximum) @@ -41,5 +41,5 @@ void JavaIMAPOperationCallback::itemProgress(IMAPOperation * session, unsigned i JNIEnv * env = mEnv; jclass cls = mEnv->GetObjectClass(mListener); jmethodID mid = mEnv->GetMethodID(cls, "itemProgress", "(JJ)V"); - mEnv->CallVoidMethod(mListener, mid); + mEnv->CallVoidMethod(mListener, mid, (jlong) current, (jlong) maximum); } diff --git a/src/java/native/com_libmailcore_IMAPFetchContentOperation.cpp b/src/java/native/com_libmailcore_IMAPFetchContentOperation.cpp index 9dfff9b04..24d3a0900 100644 --- a/src/java/native/com_libmailcore_IMAPFetchContentOperation.cpp +++ b/src/java/native/com_libmailcore_IMAPFetchContentOperation.cpp @@ -40,7 +40,8 @@ JNIEXPORT void JNICALL Java_com_libmailcore_IMAPFetchContentOperation_setupNativ jobject javaListener = getObjectField(env, obj, "listener", "Lcom/libmailcore/IMAPOperationProgressListener;"); if (javaListener != NULL) { - callback = new JavaIMAPOperationCallback(env, javaListener); + jobject c = reinterpret_cast(env->NewGlobalRef(javaListener)); + callback = new JavaIMAPOperationCallback(env, c); MC_JAVA_NATIVE_INSTANCE->setImapCallback(callback); } MC_POOL_END;