Add some methods to get information from the library.

Else, the library is useless.
This commit is contained in:
Matthieu Gautier 2020-01-23 10:23:54 +01:00
parent c2c89c6c86
commit 54f671b2f1
4 changed files with 100 additions and 22 deletions

View File

@ -39,7 +39,7 @@ Java_org_kiwix_kiwixlib_Book_dispose(JNIEnv* env, jobject thisObj)
#define BOOK (getPtr<kiwix::Book>(env, thisObj)) #define BOOK (getPtr<kiwix::Book>(env, thisObj))
#define METHOD(retType, name) JNIEXPORT retType JNICALL \ #define GETTER(retType, name) JNIEXPORT retType JNICALL \
Java_org_kiwix_kiwixlib_Book_##name (JNIEnv* env, jobject thisObj) \ Java_org_kiwix_kiwixlib_Book_##name (JNIEnv* env, jobject thisObj) \
{ \ { \
auto cRet = BOOK->name(); \ auto cRet = BOOK->name(); \
@ -47,21 +47,23 @@ Java_org_kiwix_kiwixlib_Book_##name (JNIEnv* env, jobject thisObj) \
return ret; \ return ret; \
} }
METHOD(jstring, getId) GETTER(jstring, getId)
METHOD(jstring, getPath) GETTER(jstring, getPath)
METHOD(jboolean, isPathValid) GETTER(jboolean, isPathValid)
METHOD(jstring, getTitle) GETTER(jstring, getTitle)
METHOD(jstring, getDescription) GETTER(jstring, getDescription)
METHOD(jstring, getLanguage) GETTER(jstring, getLanguage)
METHOD(jstring, getCreator) GETTER(jstring, getCreator)
METHOD(jstring, getPublisher) GETTER(jstring, getPublisher)
METHOD(jstring, getDate) GETTER(jstring, getDate)
METHOD(jstring, getUrl) GETTER(jstring, getUrl)
METHOD(jstring, getName) GETTER(jstring, getName)
METHOD(jstring, getTags) GETTER(jstring, getTags)
METHOD(jlong, getArticleCount) GETTER(jlong, getArticleCount)
METHOD(jlong, getMediaCount) GETTER(jlong, getMediaCount)
METHOD(jlong, getSize) GETTER(jlong, getSize)
METHOD(jstring, getFavicon) GETTER(jstring, getFavicon)
METHOD(jstring, getFaviconUrl) GETTER(jstring, getFaviconUrl)
METHOD(jstring, getFaviconMimeType) GETTER(jstring, getFaviconMimeType)
#undef GETTER

View File

@ -60,3 +60,34 @@ Java_org_kiwix_kiwixlib_Library_addBook(
} }
return false; return false;
} }
METHOD(jobject, Library, getBookById, jstring id) {
auto cId = jni2c(id, env);
auto cBook = new kiwix::Book(LIBRARY->getBookById(cId));
jclass cls = env->FindClass("org/kiwix/kiwixlib/Book");
jmethodID constructorId = env->GetMethodID(cls, "<init>", "()V");
jobject book = env->NewObject(cls, constructorId);
setPtr(env, book, cBook);
return book;
}
METHOD(jint, Library, getBookCount, jboolean localBooks, jboolean remoteBooks) {
return LIBRARY->getBookCount(localBooks, remoteBooks);
}
METHOD0(jobjectArray, Library, getBooksIds) {
return c2jni(LIBRARY->getBooksIds(), env);
}
METHOD0(jobjectArray, Library, getBooksLanguages) {
return c2jni(LIBRARY->getBooksLanguages(), env);
}
METHOD0(jobjectArray, Library, getBooksCreators) {
return c2jni(LIBRARY->getBooksCreators(), env);
}
METHOD0(jobjectArray, Library, getBooksPublisher) {
return c2jni(LIBRARY->getBooksPublishers(), env);
}

View File

@ -19,12 +19,23 @@
package org.kiwix.kiwixlib; package org.kiwix.kiwixlib;
import org.kiwix.kiwixlib.Book;
import org.kiwix.kiwixlib.JNIKiwixException; import org.kiwix.kiwixlib.JNIKiwixException;
public class Library public class Library
{ {
public native boolean addBook(String path) throws JNIKiwixException; public native boolean addBook(String path) throws JNIKiwixException;
public native Book getBookById(String id);
public native int getBookCount(boolean localBooks, boolean remoteBooks);
public native String[] getBooksIds();
// public native String[] filter(Filter filter);
public native String[] getBooksLanguages();
public native String[] getBooksCreators();
public native String[] getBooksPublishers();
public Library() public Library()
{ {
allocate(); allocate();

View File

@ -26,16 +26,23 @@
#include <pthread.h> #include <pthread.h>
#include <string> #include <string>
#include <vector>
extern pthread_mutex_t globalLock; extern pthread_mutex_t globalLock;
template<typename T>
void setPtr(JNIEnv* env, jobject thisObj, T* ptr)
{
jclass thisClass = env->GetObjectClass(thisObj);
jfieldID fieldId = env->GetFieldID(thisClass, "nativeHandle", "J");
env->SetLongField(thisObj, fieldId, reinterpret_cast<jlong>(ptr));
}
template<typename T, typename ...Args> template<typename T, typename ...Args>
void allocate(JNIEnv* env, jobject thisObj, Args && ...args) void allocate(JNIEnv* env, jobject thisObj, Args && ...args)
{ {
jclass thisClass = env->GetObjectClass(thisObj);
jfieldID fidNumber = env->GetFieldID(thisClass, "nativeHandle", "J");
T* ptr = new T(std::forward<Args>(args)...); T* ptr = new T(std::forward<Args>(args)...);
env->SetLongField(thisObj, fidNumber, reinterpret_cast<jlong>(ptr)); setPtr(env, thisObj, ptr);
} }
template<typename T> template<typename T>
@ -52,6 +59,14 @@ void dispose(JNIEnv* env, jobject thisObj)
delete getPtr<T>(env, thisObj); delete getPtr<T>(env, thisObj);
} }
#define METHOD0(retType, class, name) \
JNIEXPORT retType JNICALL Java_org_kiwix_kiwixlib_##class##_##name( \
JNIEnv* env, jobject thisObj)
#define METHOD(retType, class, name, ...) \
JNIEXPORT retType JNICALL Java_org_kiwix_kiwixlib_##class##_##name( \
JNIEnv* env, jobject thisObj, __VA_ARGS__)
inline jfieldID getHandleField(JNIEnv* env, jobject obj) inline jfieldID getHandleField(JNIEnv* env, jobject obj)
{ {
jclass c = env->GetObjectClass(obj); jclass c = env->GetObjectClass(obj);
@ -59,6 +74,12 @@ inline jfieldID getHandleField(JNIEnv* env, jobject obj)
return env->GetFieldID(c, "nativeHandle", "J"); return env->GetFieldID(c, "nativeHandle", "J");
} }
inline jobjectArray createArray(JNIEnv* env, size_t length, const std::string& type_sig)
{
jclass c = env->FindClass(type_sig.c_str());
return env->NewObjectArray(length, c, NULL);
}
class Lock class Lock
{ {
protected: protected:
@ -126,6 +147,7 @@ template<> struct JType<long>{ typedef jlong type_t; };
template<> struct JType<uint64_t> { typedef jlong type_t; }; template<> struct JType<uint64_t> { typedef jlong type_t; };
template<> struct JType<uint32_t> { typedef jlong type_t; }; template<> struct JType<uint32_t> { typedef jlong type_t; };
template<> struct JType<std::string>{ typedef jstring type_t; }; template<> struct JType<std::string>{ typedef jstring type_t; };
template<> struct JType<std::vector<std::string>>{ typedef jobjectArray type_t; };
template<typename T> template<typename T>
inline typename JType<T>::type_t c2jni(const T& val, JNIEnv* env) { inline typename JType<T>::type_t c2jni(const T& val, JNIEnv* env) {
@ -141,6 +163,18 @@ inline jstring c2jni(const std::string& val, JNIEnv* env)
return env->NewStringUTF(val.c_str()); return env->NewStringUTF(val.c_str());
} }
template<>
inline jobjectArray c2jni(const std::vector<std::string>& val, JNIEnv* env)
{
auto array = createArray(env, val.size(), "java/lang/String");
size_t index = 0;
for (auto& elem: val) {
auto jElem = c2jni(elem, env);
env->SetObjectArrayElement(array, index++, jElem);
}
return array;
}
/* jni2c type conversion functions */ /* jni2c type conversion functions */
inline bool jni2c(const jboolean& val) { return val == JNI_TRUE; } inline bool jni2c(const jboolean& val) { return val == JNI_TRUE; }