diff --git a/.gitignore b/.gitignore index 15f02b0..a72e5fd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .gradle local.properties build +lib/.cxx diff --git a/lib/build.gradle b/lib/build.gradle index 9a58b87..f4b5115 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -59,7 +59,7 @@ android { } externalNativeBuild { cmake { - path file('src/cpp/CMakeLists.txt') + path file('src/main/cpp/CMakeLists.txt') version '3.18.1' } } @@ -285,5 +285,35 @@ task checkCurrentJavaVersion() { task generateHeaderFilesFromJavaWrapper(type: Exec) { workingDir "${projectDir}/src/main/java/org/kiwix/" - commandLine 'bash', '-c', "javac -h ${buildDir}/include/javah_generated/ -d ${buildDir}/kiwixlib/ kiwixlib/Book.java kiwixlib/DirectAccessInfo.java kiwixlib/Filter.java kiwixlib/JNIICU.java kiwixlib/JNIKiwixBool.java kiwixlib/JNIKiwixException.java kiwixlib/JNIKiwixInt.java kiwixlib/JNIKiwixReader.java kiwixlib/JNIKiwixSearcher.java kiwixlib/JNIKiwixServer.java kiwixlib/JNIKiwixString.java kiwixlib/Library.java kiwixlib/Manager.java" + commandLine 'bash', '-c', "javac -h ${buildDir}/include/javah_generated/ -d ${buildDir}/libzim/ ${getLibzimFiles()} ${getLibkiwixFiles()}" +} + +String getLibkiwixFiles() { + return "${projectDir}/src/main/java/org/kiwix/libkiwix/Book.java " + + "${projectDir}/src/main/java/org/kiwix/libkiwix/Bookmark.java " + + "${projectDir}/src/main/java/org/kiwix/libkiwix/Filter.java " + + "${projectDir}/src/main/java/org/kiwix/libkiwix/JNIICU.java " + + "${projectDir}/src/main/java/org/kiwix/libkiwix/Illustration.java " + + "${projectDir}/src/main/java/org/kiwix/libkiwix/JNIKiwixException.java " + + "${projectDir}/src/main/java/org/kiwix/libkiwix/Library.java " + + "${projectDir}/src/main/java/org/kiwix/libkiwix/Manager.java " + + "${projectDir}/src/main/java/org/kiwix/libkiwix/Server.java" +} + +String getLibzimFiles() { + return "${projectDir}/src/main/java/org/kiwix/libzim/Archive.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/Blob.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/DirectAccessInfo.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/EntryIterator.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/Entry.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/Item.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/Query.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/Searcher.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/SearchIterator.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/Search.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/SuggestionItem.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/SuggestionIterator.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/SuggestionSearcher.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/SuggestionSearch.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/ZimFileFormatException.java" } diff --git a/lib/src/main/cpp/CMakeLists.txt b/lib/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000..f768ff2 --- /dev/null +++ b/lib/src/main/cpp/CMakeLists.txt @@ -0,0 +1,97 @@ + +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH) +cmake_minimum_required(VERSION 3.18.1) + +set(CMAKE_ANDROID_STL_TYPE llvm-libc++_static) + +project("libkiwix_wrapper") + +add_library( + zim_wrapper + + SHARED + libzim/archive.cpp + libzim/entry.cpp + libzim/entry_iterator.cpp + libzim/item.cpp + libzim/blob.cpp + libzim/searcher.cpp + libzim/query.cpp + libzim/search.cpp + libzim/search_iterator.cpp + libzim/suggestion_searcher.cpp + libzim/suggestion_search.cpp + libzim/suggestion_iterator.cpp + libzim/suggestion_item.cpp +) + +find_library(libzim + zim + PATHS + ${BUILD_DIR}/jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libzim) +if (NOT libzim) + message(FATAL_ERROR "libzim not found!") +endif() +add_library(libzim SHARED IMPORTED) + +set_property(TARGET + libzim + PROPERTY + IMPORTED_LOCATION + ${BUILD_DIR}/jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libzim/libzim.so) + + +add_library( + kiwix_wrapper + + SHARED + libkiwix/book.cpp + libkiwix/filter.cpp + libkiwix/kiwixicu.cpp + libkiwix/kiwixserver.cpp + libkiwix/library.cpp + libkiwix/bookmark.cpp + libkiwix/manager.cpp + libkiwix/illustration.cpp +) + +find_library(libkiwix + kiwix + PATHS + ${BUILD_DIR}/jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libkiwix) +if (NOT libkiwix) + message(FATAL_ERROR "libkiwix not found!") +endif() +add_library(libkiwix SHARED IMPORTED) + +set_property(TARGET + libkiwix + PROPERTY + IMPORTED_LOCATION + ${BUILD_DIR}/jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libkiwix/libkiwix.so) + +include_directories( +${CMAKE_SOURCE_DIR} +${BUILD_DIR}/include/libkiwix +${BUILD_DIR}/include/libzim +${BUILD_DIR}/include/javah_generated +#${CMAKE_SOURCE_DIR}/include/utils +) + +find_library( + log-lib + log) + +target_link_libraries( + zim_wrapper + libzim + ${log-lib} +) + +target_link_libraries( + kiwix_wrapper + libkiwix + libzim + ${log-lib} + ) + diff --git a/lib/src/main/cpp/kiwixreader.cpp b/lib/src/main/cpp/kiwixreader.cpp deleted file mode 100644 index cc3c277..0000000 --- a/lib/src/main/cpp/kiwixreader.cpp +++ /dev/null @@ -1,561 +0,0 @@ -/* - * Copyright (C) 2013 Emmanuel Engelhart - * Copyright (C) 2017 Matthieu Gautier - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - - -#include -#include -#include "org_kiwix_kiwixlib_JNIKiwixReader.h" - -#include "tools/base64.h" -#include "reader.h" -#include "utils.h" - -/* Kiwix Reader JNI functions */ -JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getNativeReader( - JNIEnv* env, jobject obj, jstring filename) -{ - std::string cPath = jni2c(filename, env); - - LOG("Attempting to create reader with: %s", cPath.c_str()); - Lock l; - try { - kiwix::Reader* reader = new kiwix::Reader(cPath); - return reinterpret_cast(new Handle(reader)); - } catch (std::exception& e) { - LOG("Error opening ZIM file"); - LOG(e.what()); - return 0; - } -} - -namespace -{ - -int jni2fd(const jobject& fdObj, JNIEnv* env) -{ - jclass class_fdesc = env->FindClass("java/io/FileDescriptor"); - jfieldID field_fd = env->GetFieldID(class_fdesc, "fd", "I"); - if ( field_fd == NULL ) - { - env->ExceptionClear(); - // Under Android the (private) 'fd' field of java.io.FileDescriptor has been - // renamed to 'descriptor'. See, for example, - // https://android.googlesource.com/platform/libcore/+/refs/tags/android-8.1.0_r1/ojluni/src/main/java/java/io/FileDescriptor.java#55 - field_fd = env->GetFieldID(class_fdesc, "descriptor", "I"); - } - return env->GetIntField(fdObj, field_fd); -} - -} // unnamed namespace - -JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getNativeReaderByFD( - JNIEnv* env, jobject obj, jobject fdObj) -{ -#ifndef _WIN32 - int fd = jni2fd(fdObj, env); - - LOG("Attempting to create reader with fd: %d", fd); - Lock l; - try { - kiwix::Reader* reader = new kiwix::Reader(fd); - return reinterpret_cast(new Handle(reader)); - } catch (std::exception& e) { - LOG("Error opening ZIM file"); - LOG(e.what()); - return 0; - } -#else - jclass exception = env->FindClass("java/lang/UnsupportedOperationException"); - env->ThrowNew(exception, "org.kiwix.kiwixlib.JNIKiwixReader.getNativeReaderByFD() is not supported under Windows"); - return 0; -#endif -} - -JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getNativeReaderEmbedded( - JNIEnv* env, jobject obj, jobject fdObj, jlong offset, jlong size) -{ -#ifndef _WIN32 - int fd = jni2fd(fdObj, env); - - LOG("Attempting to create reader with fd: %d", fd); - Lock l; - try { - kiwix::Reader* reader = new kiwix::Reader(fd, offset, size); - return reinterpret_cast(new Handle(reader)); - } catch (std::exception& e) { - LOG("Error opening ZIM file"); - LOG(e.what()); - return 0; - } -#else - jclass exception = env->FindClass("java/lang/UnsupportedOperationException"); - env->ThrowNew(exception, "org.kiwix.kiwixlib.JNIKiwixReader.getNativeReaderEmbedded() is not supported under Windows"); - return 0; -#endif -} - -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_dispose(JNIEnv* env, jobject obj) -{ - Handle::dispose(env, obj); -} - -#define READER (Handle::getHandle(env, obj)) - -/* Kiwix library functions */ -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getMainPage(JNIEnv* env, jobject obj) -{ - jstring url; - - try { - std::string cUrl = READER->getMainPage().getPath(); - url = c2jni(cUrl, env); - } catch (std::exception& e) { - LOG("Unable to get ZIM main page"); - LOG(e.what()); - url = NULL; - } - return url; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getId(JNIEnv* env, jobject obj) -{ - jstring id; - - try { - std::string cId = READER->getId(); - id = c2jni(cId, env); - } catch (std::exception& e) { - LOG("Unable to get ZIM id"); - LOG(e.what()); - id = NULL; - } - - return id; -} - -JNIEXPORT jint JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getFileSize(JNIEnv* env, jobject obj) -{ - jint size = 0; - - try { - int cSize = READER->getFileSize(); - size = c2jni(cSize, env); - } catch (std::exception& e) { - LOG("Unable to get ZIM file size"); - LOG(e.what()); - } - - return size; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getCreator(JNIEnv* env, jobject obj) -{ - jstring creator; - - try { - std::string cCreator = READER->getCreator(); - creator = c2jni(cCreator, env); - } catch (std::exception& e) { - LOG("Unable to get ZIM creator"); - LOG(e.what()); - creator = NULL; - } - - return creator; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getPublisher(JNIEnv* env, jobject obj) -{ - jstring publisher; - - try { - std::string cPublisher = READER->getPublisher(); - publisher = c2jni(cPublisher, env); - } catch (std::exception& e) { - LOG("Unable to get ZIM publish"); - LOG(e.what()); - publisher = NULL; - } - return publisher; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getName(JNIEnv* env, jobject obj) -{ - jstring name; - - try { - std::string cName = READER->getName(); - name = c2jni(cName, env); - } catch (std::exception& e) { - LOG("Unable to get ZIM name"); - LOG(e.what()); - name = NULL; - } - return name; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getFavicon(JNIEnv* env, jobject obj) -{ - jstring favicon; - - try { - std::string cContent; - std::string cMime; - READER->getFavicon(cContent, cMime); - favicon = c2jni( - base64_encode(cContent), - env); - } catch (std::exception& e) { - LOG("Unable to get ZIM favicon"); - LOG(e.what()); - favicon = NULL; - } - return favicon; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getDate(JNIEnv* env, jobject obj) -{ - jstring date; - - try { - std::string cDate = READER->getDate(); - date = c2jni(cDate, env); - } catch (std::exception& e) { - LOG("Unable to get ZIM date"); - LOG(e.what()); - date = NULL; - } - return date; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getLanguage(JNIEnv* env, jobject obj) -{ - jstring language; - - try { - std::string cLanguage = READER->getLanguage(); - language = c2jni(cLanguage, env); - } catch (std::exception& e) { - LOG("Unable to get ZIM language"); - LOG(e.what()); - language = NULL; - } - - return language; -} - -JNIEXPORT jstring JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getMimeType( - JNIEnv* env, jobject obj, jstring url) -{ - jstring mimeType; - - std::string cUrl = jni2c(url, env); - try { - auto entry = READER->getEntryFromEncodedPath(cUrl); - auto cMimeType = entry.getMimetype(); - mimeType = c2jni(cMimeType, env); - } catch (std::exception& e) { - LOG("Unable to get mime-type for url: %s", cUrl.c_str()); - LOG(e.what()); - mimeType = NULL; - } - return mimeType; -} - -JNIEXPORT jstring JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_checkUrl( - JNIEnv* env, jobject obj, jstring url) -{ - jstring finalUrl; - std::string cUrl = jni2c(url, env); - try { - auto entry = READER->getEntryFromEncodedPath(cUrl); - entry = entry.getFinalEntry(); - finalUrl = c2jni(entry.getPath(), env); - } catch (std::exception& e) { - finalUrl = c2jni(std::string(), env); - } - return finalUrl; -} - -JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getContent( - JNIEnv* env, jobject obj, jobject url, jobject titleObj, jobject mimeTypeObj, jobject sizeObj) -{ - /* Default values */ - setStringObjValue("", titleObj, env); - setStringObjValue("", mimeTypeObj, env); - setIntObjValue(0, sizeObj, env); - jbyteArray data = env->NewByteArray(0); - - /* Retrieve the content */ - std::string cUrl = getStringObjValue(url, env); - unsigned int cSize = 0; - - try { - auto entry = READER->getEntryFromEncodedPath(cUrl); - bool isRedirect = entry.isRedirect(); - entry = entry.getFinalEntry(); - cSize = entry.getSize(); - setIntObjValue(cSize, sizeObj, env); - setStringObjValue(entry.getMimetype(), mimeTypeObj, env); - setStringObjValue(entry.getTitle(), titleObj, env); - if (isRedirect) { - setStringObjValue(entry.getPath(), url, env); - } else { - data = env->NewByteArray(cSize); - env->SetByteArrayRegion( - data, 0, cSize, reinterpret_cast(entry.getBlob().data())); - } - } catch (std::exception& e) { - LOG("Unable to get content for url: %s", cUrl.c_str()); - LOG(e.what()); - } - - return data; -} - -JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getContentPart( - JNIEnv* env, jobject obj, jstring url, jint offset, jint len, jobject sizeObj) -{ - jbyteArray data = env->NewByteArray(0); - setIntObjValue(0, sizeObj, env); - - /* Default values */ - /* Retrieve the content */ - std::string cUrl = jni2c(url, env); - unsigned int cOffset = jni2c(offset, env); - unsigned int cLen = jni2c(len, env); - try { - auto entry = READER->getEntryFromEncodedPath(cUrl); - entry = entry.getFinalEntry(); - - if (cLen == 0) { - setIntObjValue(entry.getSize(), sizeObj, env); - } else if (cOffset+cLen < entry.getSize()) { - auto blob = entry.getBlob(cOffset, cLen); - data = env->NewByteArray(cLen); - env->SetByteArrayRegion( - data, 0, cLen, reinterpret_cast(blob.data())); - setIntObjValue(cLen, sizeObj, env); - } - } catch (std::exception& e) { - LOG("Unable to get partial content for url: %s (%u : %u)", cUrl.c_str(), cOffset, cLen); - LOG(e.what()); - } - return data; -} - -JNIEXPORT jlong JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getArticleSize( - JNIEnv* env, jobject obj, jstring url) -{ - std::string cUrl = jni2c(url, env); - try { - auto entry = READER->getEntryFromEncodedPath(cUrl); - entry = entry.getFinalEntry(); - return c2jni(entry.getSize(), env); - } catch(std::exception& e) { - LOG("Unable to get size for url : %s", cUrl.c_str()); - LOG(e.what()); - } - return c2jni(0, env); -} - -JNIEXPORT jobject JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getDirectAccessInformation( - JNIEnv* env, jobject obj, jstring url) -{ - jclass daiClass = env->FindClass("org/kiwix/kiwixlib/DirectAccessInfo"); - jmethodID daiInitMethod = env->GetMethodID(daiClass, "", "()V"); - jobject dai = env->NewObject(daiClass, daiInitMethod); - setDaiObjValue("", 0, dai, env); - - std::string cUrl = jni2c(url, env); - try { - auto entry = READER->getEntryFromEncodedPath(cUrl); - entry = entry.getFinalEntry(); - auto part_info = entry.getDirectAccessInfo(); - setDaiObjValue(part_info.first, part_info.second, dai, env); - } catch (std::exception& e) { - LOG("Unable to get direct access info for url: %s", cUrl.c_str()); - LOG(e.what()); - } - return dai; -} - -JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_searchSuggestions(JNIEnv* env, - jobject obj, - jstring prefix, - jint count) -{ - jboolean retVal = JNI_FALSE; - std::string cPrefix = jni2c(prefix, env); - unsigned int cCount = jni2c(count, env); - - try { -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" - if (READER->searchSuggestionsSmart(cPrefix, cCount)) { - retVal = JNI_TRUE; - } -#pragma GCC diagnostic pop - } catch (std::exception& e) { - LOG("Unable to get search results for pattern: %s", cPrefix.c_str()); - LOG(e.what()); - } - - return retVal; -} - -JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getNextSuggestion(JNIEnv* env, - jobject obj, - jobject titleObj, - jobject urlObj) -{ - jboolean retVal = JNI_FALSE; - std::string cTitle; - std::string cUrl; - - try { -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" - if (READER->getNextSuggestion(cTitle, cUrl)) { - setStringObjValue(cTitle, titleObj, env); - setStringObjValue(cUrl, urlObj, env); - retVal = JNI_TRUE; - } -#pragma GCC diagnostic pop - } catch (std::exception& e) { - LOG("Unable to get next suggestion"); - LOG(e.what()); - } - - return retVal; -} - -JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getPageUrlFromTitle(JNIEnv* env, - jobject obj, - jstring title, - jobject urlObj) -{ - std::string cTitle = jni2c(title, env); - - try { - auto entry = READER->getEntryFromTitle(cTitle); - entry = entry.getFinalEntry(); - setStringObjValue(entry.getPath(), urlObj, env); - return JNI_TRUE; - } catch (std::exception& e) { - LOG("Unable to get url for title %s: ", cTitle.c_str()); - LOG(e.what()); - } - - return JNI_FALSE; -} - -JNIEXPORT jstring JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getTitle( - JNIEnv* env, jobject obj) -{ - jstring title; - - try { - std::string cTitle = READER->getTitle(); - title = c2jni(cTitle, env); - } catch (std::exception& e) { - LOG("Unable to get zim title"); - LOG(e.what()); - title = NULL; - } - return title; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getDescription(JNIEnv* env, jobject obj) -{ - jstring description; - - try { - std::string cDescription = READER->getDescription(); - description = c2jni(cDescription, env); - } catch (std::exception& e) { - LOG("Unable to get zim description"); - LOG(e.what()); - description = NULL; - } - return description; -} - -JNIEXPORT jint JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getArticleCount(JNIEnv* env, jobject obj) -{ - jint articleCount = 0; - try { - auto cArticleCount = READER->getArticleCount(); - articleCount = c2jni(cArticleCount, env); - } catch (std::exception& e) { - LOG("Unable to get article count."); - LOG(e.what()); - } - return articleCount; -} - -JNIEXPORT jint JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getMediaCount(JNIEnv* env, jobject obj) -{ - jint mediaCount = 0; - try { - auto cMediaCount = READER->getMediaCount(); - mediaCount = c2jni(cMediaCount, env); - } catch (std::exception& e) { - LOG("Unable to get media count."); - LOG(e.what()); - } - return mediaCount; -} - - -JNIEXPORT jboolean JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getRandomPage( - JNIEnv* env, jobject obj, jobject urlObj) -{ - jboolean retVal = JNI_FALSE; - std::string cUrl; - - try { - std::string cUrl = READER->getRandomPage().getPath(); - setStringObjValue(cUrl, urlObj, env); - retVal = JNI_TRUE; - } catch (std::exception& e) { - LOG("Unable to get random page"); - LOG(e.what()); - } - return retVal; -} diff --git a/lib/src/main/cpp/kiwixsearcher.cpp b/lib/src/main/cpp/kiwixsearcher.cpp deleted file mode 100644 index 68241b3..0000000 --- a/lib/src/main/cpp/kiwixsearcher.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (C) 2013 Emmanuel Engelhart - * Copyright (C) 2017 Matthieu Gautier - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - - -#include "org_kiwix_kiwixlib_JNIKiwixSearcher.h" -#include "org_kiwix_kiwixlib_JNIKiwixSearcher_Result.h" - -#include "reader.h" -#include "searcher.h" -#include "utils.h" - -#define SEARCHER (Handle::getHandle(env, obj)) -#define RESULT (Handle::getHandle(env, obj)) - - -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixSearcher_dispose(JNIEnv* env, jobject obj) -{ - Handle::dispose(env, obj); -} - -/* Kiwix Reader JNI functions */ -JNIEXPORT jlong JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixSearcher_getNativeHandle(JNIEnv* env, - jobject obj) -{ - kiwix::Searcher* searcher = new kiwix::Searcher(); - return reinterpret_cast(new Handle(searcher)); -} - -/* Kiwix library functions */ -JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIKiwixSearcher_addReader( - JNIEnv* env, jobject obj, jobject reader) -{ - auto searcher = SEARCHER; - - searcher->add_reader(*(Handle::getHandle(env, reader))); -} - -JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIKiwixSearcher_search( - JNIEnv* env, jobject obj, jstring query, jint count) -{ - std::string cquery = jni2c(query, env); - unsigned int ccount = jni2c(count, env); - - SEARCHER->search(cquery, 0, ccount); -} - -JNIEXPORT jobject JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixSearcher_getNextResult(JNIEnv* env, - jobject obj) -{ - jobject result = nullptr; - - kiwix::Result* cresult = SEARCHER->getNextResult(); - if (cresult != nullptr) { - jclass resultclass - = env->FindClass("org/kiwix/kiwixlib/JNIKiwixSearcher$Result"); - jmethodID ctor = env->GetMethodID( - resultclass, "", "(Lorg/kiwix/kiwixlib/JNIKiwixSearcher;JLorg/kiwix/kiwixlib/JNIKiwixSearcher;)V"); - result = env->NewObject(resultclass, ctor, obj, reinterpret_cast(new Handle(cresult)), obj); - } - return result; -} - -JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIKiwixSearcher_00024Result_dispose( - JNIEnv* env, jobject obj) -{ - Handle::dispose(env, obj); -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixSearcher_00024Result_getUrl(JNIEnv* env, - jobject obj) -{ - try { - return c2jni(RESULT->get_url(), env); - } catch (...) { - return nullptr; - } -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixSearcher_00024Result_getTitle(JNIEnv* env, - jobject obj) -{ - try { - return c2jni(RESULT->get_title(), env); - } catch (...) { - return nullptr; - } -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixSearcher_00024Result_getSnippet(JNIEnv* env, - jobject obj) -{ - return c2jni(RESULT->get_snippet(), env); -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixSearcher_00024Result_getContent(JNIEnv* env, - jobject obj) -{ - return c2jni(RESULT->get_content(), env); -} diff --git a/lib/src/main/cpp/kiwixserver.cpp b/lib/src/main/cpp/kiwixserver.cpp deleted file mode 100644 index 830fdc6..0000000 --- a/lib/src/main/cpp/kiwixserver.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (C) 2013 Emmanuel Engelhart - * Copyright (C) 2017 Matthieu Gautier - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - - -#include -#include "org_kiwix_kiwixlib_JNIKiwixServer.h" - -#include "tools/base64.h" -#include "server.h" -#include "utils.h" - -/* Kiwix Reader JNI functions */ -JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixServer_getNativeServer( - JNIEnv* env, jobject obj, jobject jLibrary) -{ - LOG("Attempting to create server"); - Lock l; - try { - auto library = getPtr(env, jLibrary); - kiwix::Server* server = new kiwix::Server(library); - return reinterpret_cast(new Handle(server)); - } catch (std::exception& e) { - LOG("Error creating the server"); - LOG(e.what()); - return 0; - } -} - -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_dispose(JNIEnv* env, jobject obj) -{ - Handle::dispose(env, obj); -} - -#define SERVER (Handle::getHandle(env, obj)) - -/* Kiwix library functions */ -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setRoot(JNIEnv* env, jobject obj, jstring jRoot) -{ - std::string root = jni2c(jRoot, env); - SERVER->setRoot(root); -} - -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setAddress(JNIEnv* env, jobject obj, jstring jAddress) -{ - std::string address = jni2c(jAddress, env); - SERVER->setAddress(address); -} - -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setPort(JNIEnv* env, jobject obj, int port) -{ - SERVER->setPort(port); -} - -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setNbThreads(JNIEnv* env, jobject obj, int threads) -{ - SERVER->setNbThreads(threads); -} - -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setTaskbar(JNIEnv* env, jobject obj, jboolean withTaskbar, jboolean withLibraryButton) -{ - SERVER->setTaskbar(withTaskbar, withLibraryButton); -} - -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setBlockExternalLinks(JNIEnv* env, jobject obj, jboolean blockExternalLinks) -{ - SERVER->setBlockExternalLinks(blockExternalLinks); -} - -JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_start(JNIEnv* env, jobject obj) -{ - return SERVER->start(); -} - -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_stop(JNIEnv* env, jobject obj) -{ - SERVER->stop(); -} diff --git a/lib/src/main/cpp/book.cpp b/lib/src/main/cpp/libkiwix/book.cpp similarity index 54% rename from lib/src/main/cpp/book.cpp rename to lib/src/main/cpp/libkiwix/book.cpp index 157cc52..81ec03b 100644 --- a/lib/src/main/cpp/book.cpp +++ b/lib/src/main/cpp/libkiwix/book.cpp @@ -19,70 +19,86 @@ #include -#include "org_kiwix_kiwixlib_Book.h" +#include "org_kiwix_libkiwix_Book.h" #include "utils.h" #include "book.h" +#include -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_Book_allocate( - JNIEnv* env, jobject thisObj) -{ - allocate(env, thisObj); -} +#define NATIVE_TYPE kiwix::Book +#define TYPENAME libkiwix_Book +#include -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_Book_dispose(JNIEnv* env, jobject thisObj) +METHOD0(void, allocate) { - dispose(env, thisObj); + SET_PTR(std::make_shared()); } -#define BOOK (getPtr(env, thisObj)) +DISPOSE -METHOD(void, Book, update__Lorg_kiwix_kiwixlib_Book_2, jobject otherBook) +METHOD(void, update__Lorg_kiwix_libkiwix_Book_2, jobject otherBook) { - BOOK->update(*getPtr(env, otherBook)); + THIS->update(*getPtr(env, otherBook)); } -METHOD(void, Book, update__Lorg_kiwix_kiwixlib_JNIKiwixReader_2, jobject reader) +METHOD(void, update__Lorg_kiwix_libzim_Archive_2, jobject archive) { - BOOK->update(**Handle::getHandle(env, reader)); -} - -#define GETTER(retType, name) JNIEXPORT retType JNICALL \ -Java_org_kiwix_kiwixlib_Book_##name (JNIEnv* env, jobject thisObj) \ -{ \ - auto cRet = BOOK->name(); \ - retType ret = c2jni(cRet, env); \ - return ret; \ + THIS->update(*getPtr(env, archive)); } GETTER(jstring, getId) + GETTER(jstring, getPath) + +GETTER(jstring, getHumanReadableIdFromPath) + GETTER(jboolean, isPathValid) + GETTER(jstring, getTitle) + GETTER(jstring, getDescription) + GETTER(jstring, getLanguage) + GETTER(jstring, getCreator) + GETTER(jstring, getPublisher) + GETTER(jstring, getDate) + GETTER(jstring, getUrl) + GETTER(jstring, getName) + GETTER(jstring, getFlavour) + GETTER(jstring, getCategory) + GETTER(jstring, getTags) + +METHOD(jstring, getTagStr, jstring tagName) try { + return TO_JNI(THIS->getTagStr(TO_C(tagName))); +} catch(...) { + return c2jni("", env); +} + GETTER(jlong, getArticleCount) + GETTER(jlong, getMediaCount) + GETTER(jlong, getSize) -GETTER(jstring, getFavicon) -GETTER(jstring, getFaviconUrl) -GETTER(jstring, getFaviconMimeType) -METHOD(jstring, Book, getTagStr, jstring tagName) try { - auto cRet = BOOK->getTagStr(jni2c(tagName, env)); - return c2jni(cRet, env); -} catch(...) { - return c2jni("", env); +METHOD0(jobjectArray, getIllustrations) { + auto illustrations = THIS->getIllustrations(); + jobjectArray retArray = createArray(env, illustrations.size(), "org/kiwix/libkiwix/Illustration"); + size_t index = 0; + for (auto illu: illustrations) { + auto wrapper = BUILD_WRAPPER("org/kiwix/libkiwx/Illustration", illu); + env->SetObjectArrayElement(retArray, index++, wrapper); + } + return retArray; } -#undef GETTER +METHOD(jobject, getIllustration, jint size) { + return BUILD_WRAPPER("org/kiwix/libkiwix/Illustration", THIS->getIllustration(TO_C(size))); +} diff --git a/lib/src/main/cpp/libkiwix/bookmark.cpp b/lib/src/main/cpp/libkiwix/bookmark.cpp new file mode 100644 index 0000000..f14300c --- /dev/null +++ b/lib/src/main/cpp/libkiwix/bookmark.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2020 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + + +#include +#include "org_kiwix_libkiwix_Bookmark.h" + +#include "utils.h" +#include "bookmark.h" + +#define NATIVE_TYPE kiwix::Bookmark +#define TYPENAME libkiwix_Bookmark +#include + +METHOD0(void, setNativeBookmark) +{ + SET_PTR(std::make_shared()); +} + +DISPOSE + +GETTER(jstring, getBookId) + +GETTER(jstring, getBookTitle) + +GETTER(jstring, getUrl) + +GETTER(jstring, getTitle) + +GETTER(jstring, getLanguage) + +GETTER(jstring, getDate) + +METHOD(void, setBookId, jstring bookId) { + THIS->setBookId(TO_C(bookId)); +} + +METHOD(void, setBookTitle, jstring bookTitle) { + THIS->setBookTitle(TO_C(bookTitle)); +} + +METHOD(void, setUrl, jstring url) { + THIS->setUrl(TO_C(url)); +} + +METHOD(void, setTitle, jstring title) { + THIS->setTitle(TO_C(title)); +} + +METHOD(void, setLanguage, jstring lang) { + THIS->setLanguage(TO_C(lang)); +} + +METHOD(void, setDate, jstring date) { + THIS->setDate(TO_C(date)); +} diff --git a/lib/src/main/cpp/filter.cpp b/lib/src/main/cpp/libkiwix/filter.cpp similarity index 74% rename from lib/src/main/cpp/filter.cpp rename to lib/src/main/cpp/libkiwix/filter.cpp index 09c0734..4e4a5f3 100644 --- a/lib/src/main/cpp/filter.cpp +++ b/lib/src/main/cpp/libkiwix/filter.cpp @@ -19,31 +19,33 @@ #include -#include "org_kiwix_kiwixlib_Filter.h" +#include "org_kiwix_libkiwix_Filter.h" #include "library.h" #include "utils.h" -/* Kiwix Reader JNI functions */ -METHOD0(void, Filter, allocate) { - allocate(env, thisObj); -} +#define NATIVE_TYPE kiwix::Filter +#define TYPENAME libkiwix_Filter +#include "macros.h" + -METHOD0(void, Filter, dispose) { - dispose(env, thisObj); + +/* Kiwix Reader JNI functions */ +METHOD0(void, allocate) { + SET_PTR(std::make_shared()); } -#define FILTER (getPtr(env, thisObj)) +DISPOSE #define FORWARD(name, args_type) \ -METHOD(jobject, Filter, name, args_type value) { \ - FILTER->name(jni2c(value, env)); \ +METHOD(jobject, name, args_type value) { \ + THIS->name(jni2c(value, env)); \ return thisObj; \ } #define FORWARDA(name, args_type) \ -METHOD(jobject, Filter, name, jobjectArray value) { \ - FILTER->name(jni2c(value, env)); \ +METHOD(jobject, name, jobjectArray value) { \ + THIS->name(jni2c(value, env)); \ return thisObj; \ } diff --git a/lib/src/main/cpp/libkiwix/illustration.cpp b/lib/src/main/cpp/libkiwix/illustration.cpp new file mode 100644 index 0000000..f4730d9 --- /dev/null +++ b/lib/src/main/cpp/libkiwix/illustration.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2020 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + + +#include +#include "org_kiwix_libkiwix_Book.h" + +#include "utils.h" +#include "book.h" +#include + +#define NATIVE_TYPE kiwix::Book::Illustration +#define TYPENAME libkiwix_Illustration +#include + +METHOD0(void, dispose) +{ + dispose(env, thisObj); +} + +METHOD0(jint, width) { + return TO_JNI(THIS->width); +} + +METHOD0(jint, height) { + return TO_JNI(THIS->width); +} + +METHOD0(jstring, mimeType) { + return TO_JNI(THIS->mimeType); +} + +METHOD0(jstring, url) { + return TO_JNI(THIS->url); +} + +GETTER(jstring, getData) diff --git a/lib/src/main/cpp/kiwixicu.cpp b/lib/src/main/cpp/libkiwix/kiwixicu.cpp similarity index 80% rename from lib/src/main/cpp/kiwixicu.cpp rename to lib/src/main/cpp/libkiwix/kiwixicu.cpp index a10b578..bc94cf6 100644 --- a/lib/src/main/cpp/kiwixicu.cpp +++ b/lib/src/main/cpp/libkiwix/kiwixicu.cpp @@ -19,26 +19,20 @@ */ #include -#include "org_kiwix_kiwixlib_JNIICU.h" +#include "org_kiwix_libkiwix_JNIICU.h" #include #include -#include "unicode/putil.h" - #include "utils.h" - -std::mutex globalLock; +#include "zim/tools.h" JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIICU_setDataDirectory( JNIEnv* env, jclass kclass, jstring dirStr) { - std::string cPath = jni2c(dirStr, env); - - Lock l; try { - u_setDataDirectory(cPath.c_str()); + zim::setICUDataDirectory(TO_C(dirStr)); } catch (...) { - std::cerr << "Unable to set data directory " << cPath << std::endl; + std::cerr << "Unable to set data directory " << TO_C(dirStr) << std::endl; } } diff --git a/lib/src/main/cpp/libkiwix/kiwixserver.cpp b/lib/src/main/cpp/libkiwix/kiwixserver.cpp new file mode 100644 index 0000000..289e203 --- /dev/null +++ b/lib/src/main/cpp/libkiwix/kiwixserver.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + + +#include +#include "org_kiwix_libkiwix_Server.h" + +#include "server.h" +#include "utils.h" + +#define NATIVE_TYPE kiwix::Server +#define TYPENAME libkiwix_Server +#include + + + +/* Kiwix Reader JNI functions */ +METHOD(void, setNativeServer, jobject jLibrary) +{ + LOG("Attempting to create server"); + try { + auto library = getPtr(env, jLibrary); + SET_PTR(std::make_shared(library.get())); + } catch (std::exception& e) { + LOG("Error creating the server"); + LOG("%s", e.what()); + } +} + + +DISPOSE + +/* Kiwix library functions */ +METHOD(void, setRoot, jstring root) +{ + THIS->setRoot(TO_C(root)); +} + +METHOD(void, setAddress, jstring address) +{ + THIS->setAddress(TO_C(address)); +} + +METHOD(void, setPort, int port) +{ + THIS->setPort(TO_C(port)); +} + +METHOD(void, setNbThreads, int threads) +{ + THIS->setNbThreads(TO_C(threads)); +} + +METHOD(void, setTaskbar, jboolean withTaskbar, jboolean withLibraryButton) +{ + THIS->setTaskbar(TO_C(withTaskbar), TO_C(withLibraryButton)); +} + +METHOD(void, setBlockExternalLinks, jboolean blockExternalLinks) +{ + THIS->setBlockExternalLinks(TO_C(blockExternalLinks)); +} + +METHOD0(jboolean, start) +{ + return THIS->start(); +} + +METHOD0(void, stop) +{ + THIS->stop(); +} diff --git a/lib/src/main/cpp/libkiwix/library.cpp b/lib/src/main/cpp/libkiwix/library.cpp new file mode 100644 index 0000000..e27e60d --- /dev/null +++ b/lib/src/main/cpp/libkiwix/library.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2019-2020 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + + +#include +#include "org_kiwix_libkiwix_Library.h" + +#include "library.h" +#include "utils.h" + +#define NATIVE_TYPE kiwix::Library +#define TYPENAME libkiwix_Library +#include "macros.h" + +/* Kiwix Reader JNI functions */ +METHOD0(void, setNativeHandler) +{ + SET_PTR(std::make_shared()); +} + +DISPOSE + +/* Kiwix library functions */ +METHOD(jboolean, addBook, jobject book) +{ + auto cBook = getPtr(env, book); + + try { + return THIS->addBook(*cBook); + } catch (std::exception& e) { + LOG("Unable to add the book"); + LOG("%s", e.what()); } + return false; +} + +METHOD(jobject, getBookById, jstring id) { + return BUILD_WRAPPER("org/kiwix/libkiwix/Book", THIS->getBookById(TO_C(id))); +} + +METHOD(jobject, getArchiveById, jstring id) { + return BUILD_WRAPPER("org/kiwix/libzim/Archive", THIS->getArchiveById(TO_C(id))); +} + +METHOD(jboolean, removeBookById, jstring id) { + return TO_JNI(THIS->removeBookById(TO_C(id))); +} + +METHOD(jboolean, writeToFile, jstring path) { + return TO_JNI(THIS->writeToFile(TO_C(path))); +} +METHOD(jboolean, writeBookmarksToFile, jstring path) { + return TO_JNI(THIS->writeBookmarksToFile(TO_C(path))); +} + +METHOD(jint, getBookCount, jboolean localBooks, jboolean remoteBooks) { + return TO_JNI(THIS->getBookCount(TO_C(localBooks), TO_C(remoteBooks))); +} + +GETTER(jobjectArray, getBooksIds) + +METHOD(jobjectArray, filter, jobject filterObj) { + auto filter = getPtr(env, filterObj); + return c2jni(THIS->filter(*filter), env); +} + +GETTER(jobjectArray, getBooksLanguages) +GETTER(jobjectArray, getBooksCategories) +GETTER(jobjectArray, getBooksCreators) +GETTER(jobjectArray, getBooksPublishers) + +METHOD(void, addBookmark, jobject bookmark) { + auto cBookmark = getPtr(env, bookmark); + THIS->addBookmark(*cBookmark); +} + +METHOD(jboolean, removeBookmark, jstring zimId, jstring url) { + return TO_JNI(THIS->removeBookmark(TO_C(zimId), TO_C(url))); +} + +METHOD(jobjectArray, getBookmarks, jboolean onlyValidBookmarks) { + auto bookmarks = THIS->getBookmarks(TO_C(onlyValidBookmarks)); + jobjectArray retArray = createArray(env, bookmarks.size(), "org/kiwix/libkiwix/Bookmark"); + size_t index = 0; + for (auto bookmark: bookmarks) { + auto wrapper = BUILD_WRAPPER("org/kiwix/libkiwx/Bookmark", bookmark); + env->SetObjectArrayElement(retArray, index++, wrapper); + } + return retArray; +} diff --git a/lib/src/main/cpp/libkiwix/manager.cpp b/lib/src/main/cpp/libkiwix/manager.cpp new file mode 100644 index 0000000..cc9a9bd --- /dev/null +++ b/lib/src/main/cpp/libkiwix/manager.cpp @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2020 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + + +#include +#include "org_kiwix_libkiwix_Manager.h" + +#include "manager.h" +#include "utils.h" + +#define NATIVE_TYPE kiwix::Manager +#define TYPENAME libkiwix_Manager +#include + +METHOD(void, allocate, jobject libraryObj) +{ + auto lib = getPtr(env, libraryObj); + SET_PTR(std::make_shared(lib.get())); +} + +DISPOSE + +/* Kiwix manager functions */ +METHOD(jboolean, readFile, jstring path) +{ + auto cPath = TO_C(path); + + try { + return THIS->readFile(cPath); + } catch (std::exception& e) { + LOG("Unable to get readFile"); + LOG("%s", e.what()); + } + return false; +} + +METHOD(jboolean, readXml, jstring content, jstring libraryPath) +{ + auto cContent = TO_C(content); + auto cPath = TO_C(libraryPath); + + try { + return THIS->readXml(cContent, false, cPath); + } catch (std::exception& e) { + LOG("Unable to get ZIM id"); + LOG("%s", e.what()); + } + + return false; +} + +METHOD(jboolean, readOpds, jstring content, jstring urlHost) +{ + auto cContent = TO_C(content); + auto cUrl = TO_C(urlHost); + + try { + return THIS->readOpds(cContent, cUrl); + } catch (std::exception& e) { + LOG("Unable to get ZIM id"); + LOG("%s", e.what()); + } + + return false; +} + +METHOD(jboolean, readBookmarkFile, jstring path) +{ + auto cPath = TO_C(path); + + try { + return THIS->readBookmarkFile(cPath); + } catch (std::exception& e) { + LOG("Unable to get ZIM id"); + LOG("%s", e.what()); + } + + return false; +} + +METHOD(jstring, addBookFromPath, jstring pathToOpen, jstring pathToSave, jstring url, jboolean checkMetaData) +{ + auto cPathToOpen = TO_C(pathToOpen); + auto cPathToSave = TO_C(pathToSave); + auto cUrl = TO_C(url); + jstring id = NULL; + + try { + auto cId = THIS->addBookFromPathAndGetId(cPathToOpen, cPathToSave, cUrl, checkMetaData); + if ( !cId.empty() ) { + id = c2jni(cId, env); + } + } catch (std::exception& e) { + LOG("Unable to get ZIM file size"); + LOG("%s", e.what()); + } + + return id; +} diff --git a/lib/src/main/cpp/library.cpp b/lib/src/main/cpp/library.cpp deleted file mode 100644 index f2a0846..0000000 --- a/lib/src/main/cpp/library.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (C) 2019-2020 Matthieu Gautier - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - - -#include -#include "org_kiwix_kiwixlib_Library.h" - -#include "library.h" -#include "reader.h" -#include "utils.h" - -/* Kiwix Reader JNI functions */ -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_Library_allocate( - JNIEnv* env, jobject thisObj) -{ - allocate(env, thisObj); -} - -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_Library_dispose(JNIEnv* env, jobject thisObj) -{ - dispose(env, thisObj); -} - -#define LIBRARY (getPtr(env, thisObj)) - -/* Kiwix library functions */ -JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_Library_addBook( - JNIEnv* env, jobject thisObj, jstring path) -{ - auto cPath = jni2c(path, env); - - try { - kiwix::Reader reader(cPath); - kiwix::Book book; - book.update(reader); - return LIBRARY->addBook(book); - } catch (std::exception& e) { - LOG("Unable to add the book"); - LOG(e.what()); } - 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, "", "()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); -} - -METHOD(jobjectArray, Library, filter, jobject filterObj) { - auto filter = getPtr(env, filterObj); - return c2jni(LIBRARY->filter(*filter), 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); -} - diff --git a/lib/src/main/cpp/libzim/archive.cpp b/lib/src/main/cpp/libzim/archive.cpp new file mode 100644 index 0000000..3392977 --- /dev/null +++ b/lib/src/main/cpp/libzim/archive.cpp @@ -0,0 +1,253 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_Archive.h" + +#include + +#include +#include + +#include +#include + +#define CLASSNAME "org/kiwix/libzim/Archive" +#define NATIVE_TYPE zim::Archive +#define TYPENAME libzim_Archive +#include + +/* Kiwix Reader JNI functions */ +METHOD(void, setNativeArchive, jstring filename) +{ + std::string cPath = TO_C(filename); + + LOG("Attempting to create reader with: %s", cPath.c_str()); + try { + auto archive = std::make_shared(cPath); + SET_PTR(archive); + } catch (std::exception& e) { + LOG("Error opening ZIM file"); + LOG("%s", e.what()); + } +} + +namespace +{ + +int jni2fd(const jobject& fdObj, JNIEnv* env) +{ + jclass class_fdesc = env->FindClass("java/io/FileDescriptor"); + jfieldID field_fd = env->GetFieldID(class_fdesc, "fd", "I"); + if ( field_fd == NULL ) + { + env->ExceptionClear(); + // Under Android the (private) 'fd' field of java.io.FileDescriptor has been + // renamed to 'descriptor'. See, for example, + // https://android.googlesource.com/platform/libcore/+/refs/tags/android-8.1.0_r1/ojluni/src/main/java/java/io/FileDescriptor.java#55 + field_fd = env->GetFieldID(class_fdesc, "descriptor", "I"); + } + return env->GetIntField(fdObj, field_fd); +} + +} // unnamed namespace + +JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveByFD( + JNIEnv* env, jobject thisObj, jobject fdObj) +{ +#ifndef _WIN32 + int fd = jni2fd(fdObj, env); + + LOG("Attempting to create reader with fd: %d", fd); + try { + auto archive = std::make_shared(fd); + SET_PTR(archive); + } catch (std::exception& e) { + LOG("Error opening ZIM file"); + LOG("%s", e.what()); + } +#else + jclass exception = env->FindClass("java/lang/UnsupportedOperationException"); + env->ThrowNew(exception, "org.kiwix.libzim.Archive.setNativeArchiveByFD() is not supported under Windows"); +#endif +} + +JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbedded( + JNIEnv* env, jobject thisObj, jobject fdObj, jlong offset, jlong size) +{ +#ifndef _WIN32 + int fd = jni2fd(fdObj, env); + + LOG("Attempting to create reader with fd: %d", fd); + try { + auto archive = std::make_shared(fd, offset, size); + SET_PTR(archive); + } catch (std::exception& e) { + LOG("Error opening ZIM file"); + LOG("%s", e.what()); + } +#else + jclass exception = env->FindClass("java/lang/UnsupportedOperationException"); + env->ThrowNew(exception, "org.kiwix.libzim.Archive.setNativeArchiveEmbedded() is not supported under Windows"); +#endif +} + +DISPOSE + +GETTER(jstring, getFilename) +GETTER(jlong, getFilesize) +GETTER(jint, getAllEntryCount) +GETTER(jint, getEntryCount) +GETTER(jint, getArticleCount) +GETTER(jint, getMediaCount) + +METHOD0(jstring, getUuid) { + return TO_JNI(std::string(THIS->getUuid())); +} + +METHOD(jstring, getMetadata, jstring name) { + return TO_JNI(THIS->getMetadata(TO_C(name))); +} + +METHOD(jobject, getMetadataItem, jstring name) { + return BUILD_WRAPPER("org/kiwix/libzim/Item", THIS->getMetadataItem(TO_C(name))); +} + +GETTER(jobjectArray, getMetadataKeys) + +METHOD(jobject, getIllustrationItem, jint size) { + return BUILD_WRAPPER("org/kiwix/libzim/Item", THIS->getIllustrationItem(TO_C(size))); +} + +METHOD(jboolean, hasIllustration, jint size) { + return TO_JNI(THIS->hasIllustration(TO_C(size))); +} + +GETTER(jlongArray, getIllustrationSizes) + +METHOD(jobject, getEntryByPath__Ljava_lang_String_2, jstring path) { + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByPath(TO_C(path))); +} + +METHOD(jobject, getEntryByPath__I, jint index) { + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByPath(TO_C(index))); +} + +METHOD(jboolean, hasEntryByPath, jstring path) { + return TO_JNI(THIS->hasEntryByPath(TO_C(path))); +} + +METHOD(jobject, getEntryByTitle__Ljava_lang_String_2, jstring title) { + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByTitle(TO_C(title))); +} + +METHOD(jobject, getEntryByTitle__I, jint index) { + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByTitle(TO_C(index))); +} + +METHOD(jboolean, hasEntryByTitle, jstring title) { + return TO_JNI(THIS->hasEntryByPath(TO_C(title))); +} + +METHOD(jobject, getEntryByClusterOrder, jint index) { + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByClusterOrder(TO_C(index))); +} + +METHOD0(jobject, getMainEntry) { + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getMainEntry()); +} + +GETTER(jboolean, hasMainEntry) + +METHOD0(jobject, getRandomEntry) { + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getRandomEntry()); +} + +GETTER(jboolean, hasFulltextIndex) +GETTER(jboolean, hasTitleIndex) +GETTER(jboolean, hasChecksum) +GETTER(jstring, getChecksum) +GETTER(jboolean, check) +GETTER(jboolean, isMultiPart) +GETTER(jboolean, hasNewNamespaceScheme) + +#define ITER_BY_PATH 0 +#define ITER_BY_TITLE 1 +#define ITER_EFFICIENT 2 +METHOD0(jobject, iterByPath) { + auto range = THIS->iterByPath(); + jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); + jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); + jobject obj = env->NewObject(objClass, initMethod, ITER_BY_PATH); + SET_HANDLE(zim::Archive::iterator, obj, range.begin()); + + auto end_ptr = std::make_shared>(range.end()); + setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); + return obj; +} + +METHOD0(jobject, iterByTitle) { + auto range = THIS->iterByTitle(); + jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); + jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); + jobject obj = env->NewObject(objClass, initMethod, ITER_BY_TITLE); + SET_HANDLE(zim::Archive::iterator, obj, range.begin()); + + auto end_ptr = std::make_shared>(range.end()); + setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); + return obj; +} + +METHOD0(jobject, iterEfficient) { + auto range = THIS->iterEfficient(); + jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); + jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); + jobject obj = env->NewObject(objClass, initMethod, ITER_EFFICIENT); + SET_HANDLE(zim::Archive::iterator, obj, range.begin()); + + auto end_ptr = std::make_shared>(range.end()); + setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); + return obj; +} + +METHOD(jobject, findByPath, jstring path) { + auto range = THIS->findByPath(TO_C(path)); + jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); + jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); + jobject obj = env->NewObject(objClass, initMethod, ITER_BY_PATH); + SET_HANDLE(zim::Archive::iterator, obj, range.begin()); + + auto end_ptr = std::make_shared>(range.end()); + setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); + return obj; +} + +METHOD(jobject, findByTitle, jstring title) { + auto range = THIS->findByTitle(TO_C(title)); + jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); + jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); + jobject obj = env->NewObject(objClass, initMethod, ITER_BY_TITLE); + SET_HANDLE(zim::Archive::iterator, obj, range.begin()); + + auto end_ptr = std::make_shared>(range.end()); + setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); + return obj; +} diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixString.java b/lib/src/main/cpp/libzim/blob.cpp similarity index 65% rename from lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixString.java rename to lib/src/main/cpp/libzim/blob.cpp index 601fb9d..a197e11 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixString.java +++ b/lib/src/main/cpp/libzim/blob.cpp @@ -1,5 +1,6 @@ /* * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,21 +18,24 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +#include +#include +#include "org_kiwix_libzim_Blob.h" -public class JNIKiwixString -{ - public String value; +#include "utils.h" - public JNIKiwixString(String value) { - this.value = value; - } +#include - public JNIKiwixString() { - this(""); - } +#include - public String getValue() { - return value; - } +#define CLASSNAME "org/kiwix/libzim/Blob" +#define NATIVE_TYPE zim::Blob +#define TYPENAME libzim_Blob +#include + +DISPOSE + +METHOD0(jstring, getData) { + return TO_JNI(std::string(*THIS)); } +GETTER(jlong, size) diff --git a/lib/src/main/cpp/libzim/entry.cpp b/lib/src/main/cpp/libzim/entry.cpp new file mode 100644 index 0000000..2652e25 --- /dev/null +++ b/lib/src/main/cpp/libzim/entry.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_Entry.h" + +#include + +#include + +#include +#include + +#define CLASSNAME "org/kiwix/libzim/Entry" +#define NATIVE_TYPE zim::Entry +#define TYPENAME libzim_Entry +#include + + +DISPOSE + +GETTER(jboolean, isRedirect) +GETTER(jstring, getTitle) +GETTER(jstring, getPath) +METHOD(jobject, getItem, jboolean follow) { + return BUILD_WRAPPER("org/kiwix/libzim/Item", THIS->getItem(TO_C(follow))); +} + +METHOD0(jobject, getRedirect) { + return BUILD_WRAPPER("org/kiwix/libzim/Item", THIS->getRedirect()); +} + +METHOD0(jobject, getRedirectEntry) { + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getRedirectEntry()); +} diff --git a/lib/src/main/cpp/libzim/entry_iterator.cpp b/lib/src/main/cpp/libzim/entry_iterator.cpp new file mode 100644 index 0000000..9f19d56 --- /dev/null +++ b/lib/src/main/cpp/libzim/entry_iterator.cpp @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_EntryIterator.h" + +#include + +#include + +#include +#include + +#define CLASSNAME "org/kiwix/libzim/EntryIterator" +#define TYPENAME libzim_EntryIterator +#include "macros.h" + +#define PATH_NATIVE_TYPE zim::Archive::iterator +#define TITLE_NATIVE_TYPE zim::Archive::iterator +#define EFFICIENT_NATIVE_TYPE zim::Archive::iterator + +inline int get_order(JNIEnv* env, jobject thisObj) { + jclass thisClass = env->GetObjectClass(thisObj); + jfieldID fieldId = env->GetFieldID(thisClass, "order", "I"); + return TO_C(env->GetIntField(thisObj, fieldId)); +} + +METHOD0(void, dispose) +{ + // Delete end iterator + switch (get_order(env, thisObj)) { + case 0: + dispose(env, thisObj, "nativeHandleEnd"); + dispose(env, thisObj); + break; + case 1: + dispose(env, thisObj, "nativeHandleEnd"); + dispose(env, thisObj); + break; + case 2: + dispose(env, thisObj, "nativeHandleEnd"); + dispose(env, thisObj); + break; + } +} + + +METHOD0(jboolean, hasNext) { + switch (get_order(env, thisObj)) { + case 0: { + PATH_NATIVE_TYPE next(*GET_PTR(PATH_NATIVE_TYPE)); + next++; + auto end = getPtr(env, thisObj, "nativeHandleEnd"); + return next == *end; + } + case 1: { + TITLE_NATIVE_TYPE next(*GET_PTR(TITLE_NATIVE_TYPE)); + next++; + auto end = getPtr(env, thisObj, "nativeHandleEnd"); + return next == *end; + } + case 2: { + EFFICIENT_NATIVE_TYPE next(*GET_PTR(EFFICIENT_NATIVE_TYPE)); + next++; + auto end = getPtr(env, thisObj, "nativeHandleEnd"); + return next == *end; + } + default: + // unreachable!() + return false; + } +} + +METHOD0(jobject, next) { + switch (get_order(env, thisObj)) { + case 0: { + (*GET_PTR(PATH_NATIVE_TYPE))++; + zim::Entry entry = **GET_PTR(PATH_NATIVE_TYPE); + return BUILD_WRAPPER("org/kiwix/libzim/Entry", entry); + } + case 1: { + (*GET_PTR(TITLE_NATIVE_TYPE))++; + zim::Entry entry = **GET_PTR(TITLE_NATIVE_TYPE); + return BUILD_WRAPPER("org/kiwix/libzim/Entry", entry); + } + case 2: { + (*GET_PTR(EFFICIENT_NATIVE_TYPE))++; + zim::Entry entry = **GET_PTR(EFFICIENT_NATIVE_TYPE); + return BUILD_WRAPPER("org/kiwix/libzim/Entry", entry); + } + default: + // unreachable!() + return nullptr; + } +} + diff --git a/lib/src/main/cpp/libzim/item.cpp b/lib/src/main/cpp/libzim/item.cpp new file mode 100644 index 0000000..500797f --- /dev/null +++ b/lib/src/main/cpp/libzim/item.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_Item.h" + +#include + +#include + +#include + +#define CLASSNAME "org/kiwix/libzim/Item" +#define NATIVE_TYPE zim::Item +#define TYPENAME libzim_Item +#include + +DISPOSE + +GETTER(jstring, getTitle) +GETTER(jstring, getPath) +GETTER(jstring, getMimetype) + +METHOD0(jobject, getData) { + return BUILD_WRAPPER("org/kiwix/libzim/Blob", THIS->getData()); +} + +GETTER(jlong, getSize) + + +METHOD0(jobject, getDirectAccessInformation) { + jobject directObjInfo = newObject("org/kiwix/libzim/DirectAccessInfo", env); + setDaiObjValue("", 0, directObjInfo, env); + + auto cDirectObjInfo = THIS->getDirectAccessInformation(); + setDaiObjValue(cDirectObjInfo.first, cDirectObjInfo.second, directObjInfo, env); + return directObjInfo; +} diff --git a/lib/src/main/cpp/libzim/query.cpp b/lib/src/main/cpp/libzim/query.cpp new file mode 100644 index 0000000..6117b4d --- /dev/null +++ b/lib/src/main/cpp/libzim/query.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_Searcher.h" + +#include + +#include + +#include + +#define CLASSNAME "org/kiwix/libzim/Query" +#define NATIVE_TYPE zim::Query +#define TYPENAME libzim_Query +#include + +METHOD(void, setNativeQuery, jstring query) +{ + auto cQuery = TO_C(query); + try { + auto query = std::make_shared(cQuery); + SET_PTR(query); + } catch (std::exception& e) { + LOG("Cannot create query"); + LOG("%s", e.what()); + } +} + +DISPOSE + +METHOD(jobject, setQuery, jstring query) { + THIS->setQuery(TO_C(query)); + return thisObj; +} + +METHOD(jobject, setGeorange, jfloat latitude, jfloat longitude, jfloat distance) { + THIS->setGeorange(TO_C(latitude), TO_C(longitude), TO_C(distance)); + return thisObj; +} + diff --git a/lib/src/main/cpp/libzim/search.cpp b/lib/src/main/cpp/libzim/search.cpp new file mode 100644 index 0000000..763bb2c --- /dev/null +++ b/lib/src/main/cpp/libzim/search.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_Search.h" + +#include + +#include + +#include + +#define CLASSNAME "org/kiwix/libzim/Search" +#define NATIVE_TYPE zim::Search +#define TYPENAME libzim_Search +#include + +DISPOSE + +METHOD(jobject, getResults, jint start, jint maxResults) { + auto results = THIS->getResults(TO_C(start), TO_C(maxResults)); + auto obj = NEW_OBJECT("ork/kiwix/libzim/SearchIterator"); + SET_HANDLE(zim::SearchIterator, obj, results.begin()); + + // We have to set the nativeHandleEnd but no macro ease our work here. + auto end_ptr = std::make_shared(results.end()); + setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); + return obj; +} + +GETTER(jlong, getEstimatedMatches) diff --git a/lib/src/main/cpp/libzim/search_iterator.cpp b/lib/src/main/cpp/libzim/search_iterator.cpp new file mode 100644 index 0000000..6c04c7d --- /dev/null +++ b/lib/src/main/cpp/libzim/search_iterator.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_SearchIterator.h" + +#include + +#include + +#include +#include + +#define CLASSNAME "org/kiwix/libzim/SearchIterator" +#define NATIVE_TYPE zim::SearchIterator +#define TYPENAME libzim_SearchIterator +#include + + +// We cannot use the default macro to implement `dispose` as we need to delete the end handle +METHOD0(void, dispose) +{ + // Delete end iterator + dispose(env, thisObj, "nativeHandleEnd"); + dispose(env, thisObj); +} + + +GETTER(jstring, getPath) +GETTER(jstring, getTitle) +GETTER(jint, getScore) +GETTER(jstring, getSnippet) +GETTER(jint, getWordCount) +GETTER(jint, getFileIndex) +GETTER(jint, getSize) + +METHOD0(jstring, getZimId) { + return TO_JNI(std::string(THIS->getZimId())); +} + +METHOD0(jboolean, hasNext) { + zim::SearchIterator next(*THIS); + next++; + auto end = getPtr(env, thisObj, "nativeHandleEnd"); + return next == *end; +} + +METHOD0(jobject, next) { + (*THIS)++; + zim::Entry entry = **THIS; + return BUILD_WRAPPER("org/kiwix/libzim/Entry", entry); +} + diff --git a/lib/src/main/cpp/libzim/searcher.cpp b/lib/src/main/cpp/libzim/searcher.cpp new file mode 100644 index 0000000..9810ba2 --- /dev/null +++ b/lib/src/main/cpp/libzim/searcher.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_Searcher.h" + +#include + +#include + +#include + +#define CLASSNAME "org/kiwix/libzim/Searcher" +#define NATIVE_TYPE zim::Searcher +#define TYPENAME libzim_Searcher +#include + +METHOD(void, setNativeSearcher, jobject archive) +{ + auto cArchive = getPtr(env, archive); + try { + auto searcher = std::make_shared(*cArchive); + SET_PTR(searcher); + } catch (std::exception& e) { + LOG("Cannot create searcher"); + LOG("%s", e.what()); + } +} + +METHOD(void, setNativeSearcherMulti, jobjectArray archives) +{ + std::vector cArchives; + jsize nbArchives = env->GetArrayLength(archives); + for(int i=0; iGetObjectArrayElement(archives, i); + auto cArchive = getPtr(env, archive); + cArchives.push_back(*cArchive); + } + try { + auto searcher = std::make_shared(cArchives); + SET_PTR(searcher); + } catch (std::exception& e) { + LOG("Cannot create searcher"); + LOG("%s", e.what()); + } +} + +DISPOSE + +METHOD(jobject, addArchive, jobject archive) { + auto cArchive = getPtr(env, archive); + THIS->addArchive(*cArchive); + return thisObj; +} + +METHOD(jobject, search, jobject query) { + auto cQuery = getPtr(env, query); + return BUILD_WRAPPER("org/kiwix/libzim/Search", THIS->search(*cQuery)); +} + +METHOD(void, setVerbose, jboolean verbose) { + THIS->setVerbose(TO_C(verbose)); +} + diff --git a/lib/src/main/cpp/libzim/suggestion_item.cpp b/lib/src/main/cpp/libzim/suggestion_item.cpp new file mode 100644 index 0000000..35b1c4f --- /dev/null +++ b/lib/src/main/cpp/libzim/suggestion_item.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_SuggestionItem.h" + +#include + +#include + +#include + +#define CLASSNAME "org/kiwix/libzim/SuggestionItem" +#define NATIVE_TYPE zim::SuggestionItem +#define TYPENAME libzim_SuggestionItem +#include + +DISPOSE + +GETTER(jstring, getTitle) +GETTER(jstring, getPath) +GETTER(jstring, getSnippet) +GETTER(jboolean, hasSnippet) diff --git a/lib/src/main/cpp/libzim/suggestion_iterator.cpp b/lib/src/main/cpp/libzim/suggestion_iterator.cpp new file mode 100644 index 0000000..5181ae1 --- /dev/null +++ b/lib/src/main/cpp/libzim/suggestion_iterator.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_SuggestionIterator.h" + +#include + +#include + +#include + +#define CLASSNAME "org/kiwix/libzim/SuggestionIterator" +#define NATIVE_TYPE zim::SuggestionIterator +#define TYPENAME libzim_SuggestionIterator +#include + +// We cannot use the default macro to implement `dispose` as we need to delete the end handle +METHOD0(void, dispose) +{ + // Delete end iterator + dispose(env, thisObj, "nativeHandleEnd"); + dispose(env, thisObj); +} + +METHOD0(jboolean, hasNext) { + NATIVE_TYPE next(*THIS); + next++; + auto end = getPtr(env, thisObj, "nativeHandleEnd"); + return next == *end; +} + +METHOD0(jobject, next) { + (*THIS)++; + zim::SuggestionItem item = **THIS; + return BUILD_WRAPPER("org/kiwix/libzim/SuggestionItem", item); +} + diff --git a/lib/src/main/cpp/libzim/suggestion_search.cpp b/lib/src/main/cpp/libzim/suggestion_search.cpp new file mode 100644 index 0000000..26fa783 --- /dev/null +++ b/lib/src/main/cpp/libzim/suggestion_search.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_SuggestionSearch.h" + +#include + +#include + +#include + +#define CLASSNAME "org/kiwix/libzim/SuggestionSearch" +#define NATIVE_TYPE zim::SuggestionSearch +#define TYPENAME libzim_SuggestionSearch +#include + +DISPOSE + +METHOD(jobject, getResults, jint start, jint maxResults) { + auto results = THIS->getResults(TO_C(start), TO_C(maxResults)); + auto obj = NEW_OBJECT("ork/kiwix/libzim/SuggestionIterator"); + SET_HANDLE(zim::SuggestionIterator, obj, results.begin()); + + // We have to set the nativeHandleEnd but no macro ease our work here. + auto end_ptr = std::make_shared(results.end()); + setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); + return obj; +} + +GETTER(jlong, getEstimatedMatches) diff --git a/lib/src/main/cpp/libzim/suggestion_searcher.cpp b/lib/src/main/cpp/libzim/suggestion_searcher.cpp new file mode 100644 index 0000000..792447e --- /dev/null +++ b/lib/src/main/cpp/libzim/suggestion_searcher.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_SuggestionSearcher.h" + +#include + +#include + +#include + +#define NATIVE_TYPE zim::SuggestionSearcher +#define TYPENAME libzim_SuggestionSearcher +#include + + +METHOD(void, setNativeSearcher, jobject archive) +{ + auto cArchive = getPtr(env, archive); + try { + auto searcher = std::make_shared(*cArchive); + SET_PTR(searcher); + } catch (std::exception& e) { + LOG("Cannot create searcher"); + LOG("%s", e.what()); + } +} + +DISPOSE + +METHOD(jobject, suggest, jstring query) { + return BUILD_WRAPPER("org/kiwix/libzim/SuggestionSearch", THIS->suggest(TO_C(query))); +} + +METHOD(void, setVerbose, jboolean verbose) { + THIS->setVerbose(TO_C(verbose)); +} + diff --git a/lib/src/main/cpp/macros.h b/lib/src/main/cpp/macros.h new file mode 100644 index 0000000..bef647b --- /dev/null +++ b/lib/src/main/cpp/macros.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + + +// You must define NATIVE_TYPE("zim::Archive") and TYPENAME("zim__Archive") + + +#define BUILD_METHOD_NX(TYPE, NAME) Java_org_kiwix_ ## TYPE ## _ ## NAME +#define BUILD_METHOD(TYPE, NAME) BUILD_METHOD_NX(TYPE, NAME) + +#define THIS getPtr(env, thisObj) + +#define METHOD0(retType, name) \ +JNIEXPORT retType JNICALL BUILD_METHOD(TYPENAME, name) ( \ + JNIEnv* env, jobject thisObj) + +#define METHOD(retType, name, ...) \ +JNIEXPORT retType JNICALL BUILD_METHOD(TYPENAME ,name) ( \ + JNIEnv* env, jobject thisObj, __VA_ARGS__) + +#define GETTER(retType, name) METHOD0(retType, name) { \ + return TO_JNI(THIS->name()); \ +} + +#define DISPOSE METHOD0(void, dispose) { dispose(env, thisObj); } diff --git a/lib/src/main/cpp/manager.cpp b/lib/src/main/cpp/manager.cpp deleted file mode 100644 index 4395609..0000000 --- a/lib/src/main/cpp/manager.cpp +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright (C) 2020 Matthieu Gautier - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - - -#include -#include "org_kiwix_kiwixlib_Manager.h" - -#include "manager.h" -#include "utils.h" - - -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_Manager_allocate( - JNIEnv* env, jobject thisObj, jobject libraryObj) -{ - auto lib = getPtr(env, libraryObj); - allocate(env, thisObj, lib); -} - -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_Manager_dispose(JNIEnv* env, jobject thisObj) -{ - dispose(env, thisObj); -} - -#define MANAGER (getPtr(env, thisObj)) - -/* Kiwix manager functions */ -JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_Manager_readFile( - JNIEnv* env, jobject thisObj, jstring path) -{ - auto cPath = jni2c(path, env); - - try { - return MANAGER->readFile(cPath); - } catch (std::exception& e) { - LOG("Unable to get readFile"); - LOG(e.what()); - } - return false; -} - -JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_Manager_readXml( - JNIEnv* env, jobject thisObj, jstring content, jstring libraryPath) -{ - auto cContent = jni2c(content, env); - auto cPath = jni2c(libraryPath, env); - - try { - return MANAGER->readXml(cContent, false, cPath); - } catch (std::exception& e) { - LOG("Unable to get ZIM id"); - LOG(e.what()); - } - - return false; -} - -JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_Manager_readOpds( - JNIEnv* env, jobject thisObj, jstring content, jstring urlHost) -{ - auto cContent = jni2c(content, env); - auto cUrl = jni2c(urlHost, env); - - try { - return MANAGER->readOpds(cContent, cUrl); - } catch (std::exception& e) { - LOG("Unable to get ZIM id"); - LOG(e.what()); - } - - return false; -} - -JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_Manager_readBookmarkFile( - JNIEnv* env, jobject thisObj, jstring path) -{ - auto cPath = jni2c(path, env); - - try { - return MANAGER->readBookmarkFile(cPath); - } catch (std::exception& e) { - LOG("Unable to get ZIM id"); - LOG(e.what()); - } - - return false; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_Manager_addBookFromPath( - JNIEnv* env, jobject thisObj, - jstring pathToOpen, jstring pathToSave, jstring url, jboolean checkMetaData) -{ - auto cPathToOpen = jni2c(pathToOpen, env); - auto cPathToSave = jni2c(pathToSave, env); - auto cUrl = jni2c(url, env); - jstring id = NULL; - - try { - auto cId = MANAGER->addBookFromPathAndGetId(cPathToOpen, cPathToSave, cUrl, checkMetaData); - if ( !cId.empty() ) { - id = c2jni(cId, env); - } - } catch (std::exception& e) { - LOG("Unable to get ZIM file size"); - LOG(e.what()); - } - - return id; -} diff --git a/lib/src/main/cpp/meson.build b/lib/src/main/cpp/meson.build deleted file mode 100644 index de72e49..0000000 --- a/lib/src/main/cpp/meson.build +++ /dev/null @@ -1,55 +0,0 @@ - -java_sources = files([ - 'org/kiwix/kiwixlib/JNIICU.java', - 'org/kiwix/kiwixlib/Book.java', - 'org/kiwix/kiwixlib/JNIKiwixReader.java', - 'org/kiwix/kiwixlib/Library.java', - 'org/kiwix/kiwixlib/Manager.java', - 'org/kiwix/kiwixlib/Filter.java', - 'org/kiwix/kiwixlib/JNIKiwixSearcher.java', - 'org/kiwix/kiwixlib/JNIKiwixServer.java', - 'org/kiwix/kiwixlib/JNIKiwixInt.java', - 'org/kiwix/kiwixlib/JNIKiwixString.java', - 'org/kiwix/kiwixlib/JNIKiwixBool.java', - 'org/kiwix/kiwixlib/JNIKiwixException.java', - 'org/kiwix/kiwixlib/DirectAccessInfo.java' -]) - -kiwix_jni = custom_target('jni', - input: java_sources, - output: ['org_kiwix_kiwixlib_JNIKiwix.h', - 'org_kiwix_kiwixlib_Book.h', - 'org_kiwix_kiwixlib_JNIKiwixReader.h', - 'org_kiwix_kiwixlib_Library.h', - 'org_kiwix_kiwixlib_Manager.h', - 'org_kiwix_kiwixlib_Filter.h', - 'org_kiwix_kiwixlib_JNIKiwixServer.h', - 'org_kiwix_kiwixlib_JNIKiwixSearcher.h', - 'org_kiwix_kiwixlib_JNIKiwixSearcher_Result.h'], - command:['javac', '-d', '@OUTDIR@', '-h', '@OUTDIR@', '@INPUT@'] -) - -jni_sources = files([ - 'kiwixicu.cpp', - 'book.cpp', - 'kiwixreader.cpp', - 'library.cpp', - 'manager.cpp', - 'filter.cpp', - 'kiwixsearcher.cpp', - 'kiwixserver.cpp', -]) - -kiwix_sources += jni_sources + [kiwix_jni] - -if 'java' in wrapper - kiwix_jar = jar('kiwixlib', java_sources) - #junit_jar = files('org/kiwix/testing/junit-4.13.jar') - #test_jar = jar('testing', 'org/kiwix/testing/test.java', - # link_with: [kiwix_jar, junit_jar]) - #test('javatest', test_jar) -endif - -install_subdir('org', install_dir: 'kiwix-lib/java', exclude_directories: ['kiwix/testing']) -install_subdir('res', install_dir: 'kiwix-lib') -install_data('AndroidManifest.xml', install_dir: 'kiwix-lib') diff --git a/lib/src/main/cpp/utils.h b/lib/src/main/cpp/utils.h index 232071d..00529dd 100644 --- a/lib/src/main/cpp/utils.h +++ b/lib/src/main/cpp/utils.h @@ -27,6 +27,7 @@ #include #include #include +#include #include #if __ANDROID__ @@ -36,50 +37,81 @@ #define LOG(...) #endif -extern std::mutex globalLock; +using std::shared_ptr; +// Here is the wrapping structure. +// All java class wrapping a `Native` instance must contain a pointer (cast as a long (J)) called "nativeHandle". +// They also need a constructor taking no argument (or they can, but helper here cannot be used.) +// The "nativeHandle" pointer points to a heap allocated `Handle`. The handle itself IS a shared_ptr. +// !!! This is a shared_ptr heap allocated instance which contains a `Native` heap allocated. +// So if the `Native` instance is own by a smart pointer (shared_ptr), the shared_ptr itself is "own" by a raw pointer. + +// From the jobject and the pointer field ("nativeHandle"), we can access a shared_ptr (not a pointer to a shared_ptr). +// To avoid a memory leak, we must at java wrapper destruction (dispose), delete the heap shared_ptr. + +// Create a java object using a default constructor. No field is set. +inline jobject newObject(const char* className, JNIEnv* env) { + jclass objClass = env->FindClass(className); + jmethodID initMethod = env->GetMethodID(objClass, "", "()V"); + jobject wrapper = env->NewObject(objClass, initMethod); + return wrapper; +} +#define NEW_OBJECT(CLASSNAME) newObject(CLASSNAME, env) + + +// Set the pointer to the wrapped object. template -void setPtr(JNIEnv* env, jobject thisObj, T* ptr) +inline void setPtr(JNIEnv* env, jobject thisObj, shared_ptr&& ptr, const char* handleName = "nativeHandle") { jclass thisClass = env->GetObjectClass(thisObj); - jfieldID fieldId = env->GetFieldID(thisClass, "nativeHandle", "J"); - env->SetLongField(thisObj, fieldId, reinterpret_cast(ptr)); + jfieldID fieldId = env->GetFieldID(thisClass, handleName, "J"); + // if field id is not null, we are leaking the currently stored handle + assert(env->GetLongField(thisObj, fieldId) == 0); + // allocate a shared_ptr on the head + shared_ptr* handle = new shared_ptr(ptr); + env->SetLongField(thisObj, fieldId, reinterpret_cast(handle)); } +#define SET_PTR(SHARED_PTR) setPtr(env, thisObj, std::move(SHARED_PTR)) +// This create a object and set it in the wrapper template -void allocate(JNIEnv* env, jobject thisObj, Args && ...args) +inline void setHandle(JNIEnv* env, jobject thisObj, Args && ...args) { - T* ptr = new T(std::forward(args)...); - setPtr(env, thisObj, ptr); + auto ptr = std::make_shared(std::forward(args)...); + setPtr(env, thisObj, std::move(ptr)); } +#define SET_HANDLE(NATIVE_TYPE, OBJ, VALUE) setHandle(env, OBJ, VALUE) + +// Return a shared_ptr for the handle template -T* getPtr(JNIEnv* env, jobject thisObj) +shared_ptr getPtr(JNIEnv* env, jobject thisObj, const char* handleName = "nativeHandle") { jclass thisClass = env->GetObjectClass(thisObj); - jfieldID fidNumber = env->GetFieldID(thisClass, "nativeHandle", "J"); - return reinterpret_cast(env->GetLongField(thisObj, fidNumber)); + jfieldID fidNumber = env->GetFieldID(thisClass, handleName, "J"); + auto handle = reinterpret_cast*>(env->GetLongField(thisObj, fidNumber)); + return *handle; } +#define GET_PTR(NATIVE_TYPE) getPtr(env, thisObj) +// Delete the heap allocated handle template -void dispose(JNIEnv* env, jobject thisObj) +void dispose(JNIEnv* env, jobject thisObj, const char* handleName = "nativeHandle") { - delete getPtr(env, thisObj); + jclass thisClass = env->GetObjectClass(thisObj); + jfieldID fieldId = env->GetFieldID(thisClass, handleName, "J"); + auto handle = reinterpret_cast*>(env->GetLongField(thisObj, fieldId)); + if (handle != 0) { + delete handle; + } + env->SetLongField(thisObj, fieldId, 0); } -#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, const char* handleName) { jclass c = env->GetObjectClass(obj); // J is the type signature for long: - return env->GetFieldID(c, "nativeHandle", "J"); + return env->GetFieldID(c, handleName, "J"); } inline jobjectArray createArray(JNIEnv* env, size_t length, const std::string& type_sig) @@ -88,64 +120,37 @@ inline jobjectArray createArray(JNIEnv* env, size_t length, const std::string& t return env->NewObjectArray(length, c, NULL); } -class Lock : public std::unique_lock -{ - public: - Lock() : std::unique_lock(globalLock) { } -}; - -template -class LockedHandle; - -template -class Handle -{ - protected: - T* h; - - public: - Handle(T* h) : h(h){}; - - // No destructor. This must and will be handled by dispose method. +template +inline jobject buildWrapper(JNIEnv* env, const char* class_name, T&& obj, const char* handleName = "nativeHandle") { + auto wrapper = newObject(class_name, env); + auto ptr = std::make_shared(std::move(obj)); + setPtr(env, wrapper, std::move(ptr)); + return wrapper; +} +#define BUILD_WRAPPER(CLASSNAME, OBJ) buildWrapper(env, CLASSNAME, std::move(OBJ)) - static LockedHandle getHandle(JNIEnv* env, jobject obj) - { - jlong handle = env->GetLongField(obj, getHandleField(env, obj)); - return LockedHandle(reinterpret_cast*>(handle)); - } - static void dispose(JNIEnv* env, jobject obj) - { - auto lHandle = getHandle(env, obj); - auto handle = lHandle.h; - delete handle->h; - delete handle; - } - friend class LockedHandle; -}; -template -struct LockedHandle : public Lock { - Handle* h; - LockedHandle(Handle* h) : h(h) {} - T* operator->() { return h->h; } - T* operator*() { return h->h; } - operator bool() const { return (h->h != nullptr); } - operator T*() const { return h->h; } -}; +// --------------------------------------------------------------------------- +// Convert things to JAVA +// --------------------------------------------------------------------------- template struct JType { }; template<> struct JType{ typedef jboolean type_t; }; -template<> struct JType{ typedef jint type_t; }; -template<> struct JType{ typedef jlong type_t; }; +template<> struct JType{ typedef jint type_t; }; +template<> struct JType{ typedef jint type_t; }; +template<> struct JType{ typedef jlong type_t; }; +template<> struct JType{ typedef jint type_t; }; template<> struct JType { typedef jlong type_t; }; template<> struct JType { typedef jlong type_t; }; template<> struct JType{ typedef jstring type_t; }; -template<> struct JType>{ typedef jobjectArray type_t; }; +template<> struct JType { typedef jfloat type_t;}; +template<> struct JType { typedef jdouble type_t;}; -template + +template inline typename JType::type_t c2jni(const T& val, JNIEnv* env) { return static_cast::type_t>(val); } @@ -159,6 +164,70 @@ inline jstring c2jni(const std::string& val, JNIEnv* env) return env->NewStringUTF(val.c_str()); } + +template +struct JTypeArray {}; +template<> struct JTypeArray{ + typedef jbooleanArray type_t; + static jbooleanArray createArray(JNIEnv* env, size_t length) { + return env->NewBooleanArray(length); + } + static void setArray(JNIEnv* env, jbooleanArray array, const bool* data, size_t length) { + env->SetBooleanArrayRegion(array, 0, length, reinterpret_cast(data)); + } +}; +template<> struct JTypeArray{ + typedef jintArray type_t; + static jintArray createArray(JNIEnv* env, size_t length) { + return env->NewIntArray(length); + } + static void setArray(JNIEnv* env, jintArray array, const int32_t* data, size_t length) { + env->SetIntArrayRegion(array, 0, length, data); + } +}; +template<> struct JTypeArray{ + typedef jlongArray type_t; + static jlongArray createArray(JNIEnv* env, size_t length) { + return env->NewLongArray(length); + } + static void setArray(JNIEnv* env, jlongArray array, const int64_t* data, size_t length) { + env->SetLongArrayRegion(array, 0, length, data); + } +}; +template<> struct JTypeArray { + typedef jlongArray type_t; + static jlongArray createArray(JNIEnv* env, size_t length) { + return env->NewLongArray(length); + } + static void setArray(JNIEnv* env, jlongArray array, const uint64_t* data, size_t length) { + env->SetLongArrayRegion(array, 0, length, reinterpret_cast(data)); + } +}; +template<> struct JTypeArray { + typedef jlongArray type_t; + static jlongArray createArray(JNIEnv* env, size_t length) { + return env->NewLongArray(length); + } + static void setArray(JNIEnv* env, jlongArray array, const uint32_t* data, size_t length) { + std::vector temp(data, data+length); + env->SetLongArrayRegion(array, 0, length, temp.data()); + } +}; +template<> struct JTypeArray{ + typedef jobjectArray type_t; + static jobjectArray createArray(JNIEnv* env, size_t length) { + return ::createArray(env, length, "java/lang/String"); + } + static void setArray(JNIEnv* env, jobjectArray array, const std::string* data, size_t length) { + size_t index = 0; + for(size_t index=0; indexSetObjectArrayElement(array, index, jElem); + } + } +}; + +/* template<> inline jobjectArray c2jni(const std::vector& val, JNIEnv* env) { @@ -169,8 +238,31 @@ inline jobjectArray c2jni(const std::vector& val, JNIEnv* env) env->SetObjectArrayElement(array, index++, jElem); } return array; +}*/ + +template +inline typename JTypeArray::type_t c2jni(const std::vector& val, JNIEnv* env) +{ + auto array = JTypeArray::createArray(env, val.size()); + JTypeArray::setArray(env, array, val.data(), val.size()); + return array; } +template +inline typename JTypeArray::type_t c2jni(const std::set& val, JNIEnv* env) +{ + std::vector temp(val.begin(), val.end()); + return c2jni(temp, env); +} + +#define TO_JNI(VAL) c2jni(VAL, env) + + +// --------------------------------------------------------------------------- +// Convert things to C +// --------------------------------------------------------------------------- + + template struct CType { }; @@ -178,6 +270,8 @@ template<> struct CType{ typedef bool type_t; }; template<> struct CType{ typedef int type_t; }; template<> struct CType{ typedef long type_t; }; template<> struct CType{ typedef std::string type_t; }; +template<> struct CType { typedef float type_t; }; +template<> struct CType { typedef double type_t; }; template struct CType{ typedef std::vector::type_t> type_t; }; @@ -215,6 +309,8 @@ inline typename CType::type_t jni2c(const jobjectArray& val, JN return v; } +#define TO_C(VAL) jni2c(VAL, env) + /* Method to deal with variable passed by reference */ inline std::string getStringObjValue(const jobject obj, JNIEnv* env) { diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixReader.java b/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixReader.java deleted file mode 100644 index e11d721..0000000 --- a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixReader.java +++ /dev/null @@ -1,182 +0,0 @@ -/* - * Copyright (C) 2013 Emmanuel Engelhart - * Copyright (C) 2017 Matthieu Gautier - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - -package org.kiwix.kiwixlib; - -import org.kiwix.kiwixlib.JNIKiwixException; -import org.kiwix.kiwixlib.JNIKiwixString; -import org.kiwix.kiwixlib.JNIKiwixInt; -import org.kiwix.kiwixlib.JNIKiwixSearcher; -import org.kiwix.kiwixlib.DirectAccessInfo; -import java.io.FileDescriptor; - -public class JNIKiwixReader -{ - public native String getMainPage(); - - public native String getTitle(); - - public native String getId(); - - public native String getLanguage(); - - public native String getMimeType(String url); - - /** - * Check if a url exists and is a redirect or not. - * - * Return an empty string if the url doesn't exist in the reader. - * Return the url of the "final" entry. - * - equal to the input url if the entry is not a redirection. - * - different if the url is a redirection (and the webview should redirect to it). - */ - public native String checkUrl(String url); - - /** - * Get the content of a article. - * - * Return a byte array of the content of the article. - * Set the title, mimeType to the title and mimeType of the article. - * Set the size to the size of the returned array. - * - * If the entry doesn't exist : - * - return a empty byte array - * - set all arguments (except url) to empty/0. - * If the entry exist but is a redirection : - * - return an empty byte array - * - set all arguments (including url) to information of the targeted article. - */ - public native byte[] getContent(JNIKiwixString url, - JNIKiwixString title, - JNIKiwixString mimeType, - JNIKiwixInt size); - - /** - * getContentPart. - * - * Get only a part of the content of the article. - * Return a byte array of `len` size starting from offset `offset`. - * Set `size` to the number of bytes read - * (`len` if everything is ok, 0 in case of error). - * If `len` == 0, no bytes are read but `size` is set to the total size of the - * article. - */ - public native byte[] getContentPart(String url, - int offest, - int len, - JNIKiwixInt size); - - /** - * - * Get the size of an article. - * - * @param url The url of the article. - * @return The size of the final (redirections are resolved) article (in byte). - * Return 0 if the article is not found. - */ - public native long getArticleSize(String url); - - /** - * getDirectAccessInformation. - * - * Return information giving where the content is located in the zim file. - * - * Some contents (binary content) are stored uncompressed in the zim file. - * Knowing this information, it could be interesting to directly open - * the zim file (or zim part) and directly read the content from it (and so - * bypassing the libzim). - * - * Return a `DirectAccessInfo` (filename, offset) where the content is located. - * - * If the content cannot be directly accessed (content is compressed or zim - * file is cut in the middle of the content), the filename is an empty string - * and offset is zero. - */ - public native DirectAccessInfo getDirectAccessInformation(String url); - - public native boolean searchSuggestions(String prefix, int count); - - public native boolean getNextSuggestion(JNIKiwixString title, JNIKiwixString url); - - public native boolean getPageUrlFromTitle(String title, JNIKiwixString url); - - public native String getDescription(); - - public native String getDate(); - - public native String getFavicon(); - - public native String getCreator(); - - public native String getPublisher(); - - public native String getName(); - - public native int getFileSize(); - - public native int getArticleCount(); - - public native int getMediaCount(); - - public native boolean getRandomPage(JNIKiwixString url); - - public JNIKiwixSearcher search(String query, int count) - { - JNIKiwixSearcher searcher = new JNIKiwixSearcher(); - searcher.addKiwixReader(this); - searcher.search(query, count); - return searcher; - } - - public JNIKiwixReader(String filename) throws JNIKiwixException - { - nativeHandle = getNativeReader(filename); - if (nativeHandle == 0) { - throw new JNIKiwixException("Cannot open zimfile "+filename); - } - } - - public JNIKiwixReader(FileDescriptor fd) throws JNIKiwixException - { - nativeHandle = getNativeReaderByFD(fd); - if (nativeHandle == 0) { - throw new JNIKiwixException("Cannot open zimfile by fd "+fd.toString()); - } - } - - public JNIKiwixReader(FileDescriptor fd, long offset, long size) - throws JNIKiwixException - { - nativeHandle = getNativeReaderEmbedded(fd, offset, size); - if (nativeHandle == 0) { - throw new JNIKiwixException(String.format("Cannot open embedded zimfile (fd=%s, offset=%d, size=%d)", fd, offset, size)); - } - } - - public JNIKiwixReader() { - - } - public native void dispose(); - - private native long getNativeReader(String filename); - private native long getNativeReaderByFD(FileDescriptor fd); - private native long getNativeReaderEmbedded(FileDescriptor fd, long offset, long size); - private long nativeHandle; -} diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixSearcher.java b/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixSearcher.java deleted file mode 100644 index 30d7afb..0000000 --- a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixSearcher.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (C) 2013 Emmanuel Engelhart - * Copyright (C) 2017 Matthieu Gautier - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - - -package org.kiwix.kiwixlib; - -import org.kiwix.kiwixlib.JNIKiwixReader; -import java.util.Vector; - -public class JNIKiwixSearcher -{ - public class Result - { - private long nativeHandle; - private JNIKiwixSearcher searcher; - public Result(long handle, JNIKiwixSearcher _searcher) - { - nativeHandle = handle; - searcher = _searcher; - } - public native String getUrl(); - public native String getTitle(); - public native String getContent(); - public native String getSnippet(); - public native void dispose(); - } - - public JNIKiwixSearcher() - { - nativeHandle = getNativeHandle(); - usedReaders = new Vector(); - } - public native void dispose(); - - private native long getNativeHandle(); - private long nativeHandle; - private Vector usedReaders; - - public native void addReader(JNIKiwixReader reader); - public void addKiwixReader(JNIKiwixReader reader) - { - addReader(reader); - usedReaders.addElement(reader); - }; - - public native void search(String query, int count); - - public native Result getNextResult(); - public native boolean hasMoreResult(); -} diff --git a/lib/src/main/java/org/kiwix/kiwixlib/Book.java b/lib/src/main/java/org/kiwix/libkiwix/Book.java similarity index 78% rename from lib/src/main/java/org/kiwix/kiwixlib/Book.java rename to lib/src/main/java/org/kiwix/libkiwix/Book.java index 693e833..a7ddc88 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/Book.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Book.java @@ -1,5 +1,8 @@ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; + +import org.kiwix.libzim.Archive; +import org.kiwix.libkiwix.Illustration; public class Book { @@ -7,13 +10,15 @@ public class Book public native void update(Book book); - public native void update(JNIKiwixReader reader); + public native void update(Archive archive); + @Override protected void finalize() { dispose(); } public native String getId(); public native String getPath(); + public native String getHumanReadableIdFromPath(); public native boolean isPathValid(); public native String getTitle(); public native String getDescription(); @@ -38,9 +43,8 @@ public class Book public native long getMediaCount(); public native long getSize(); - public native String getFavicon(); - public native String getFaviconUrl(); - public native String getFaviconMimeType(); + public native Illustration[] getIllustrations(); + public native Illustration getIllustration(int size); private native void allocate(); private native void dispose(); diff --git a/lib/src/main/java/org/kiwix/libkiwix/Bookmark.java b/lib/src/main/java/org/kiwix/libkiwix/Bookmark.java new file mode 100644 index 0000000..a6c1d4c --- /dev/null +++ b/lib/src/main/java/org/kiwix/libkiwix/Bookmark.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libkiwix; + +public class Bookmark +{ + public Bookmark() { + setNativeBookmark(); + } + + public native void setBookId(String bookId); + public native void setBookTitle(String bookTitle); + public native void setUrl(String url); + public native void setTitle(String title); + public native void setLanguage(String language); + public native void setDate(String Date); + + public native String getBookId(); + public native String getBookTitle(); + public native String getUrl(); + public native String getTitle(); + public native String getLanguage(); + public native String getDate(); + + @Override + protected void finalize() { dispose(); } + + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private native void setNativeBookmark(); + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/kiwixlib/Filter.java b/lib/src/main/java/org/kiwix/libkiwix/Filter.java similarity index 98% rename from lib/src/main/java/org/kiwix/kiwixlib/Filter.java rename to lib/src/main/java/org/kiwix/libkiwix/Filter.java index bf9e719..458856d 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/Filter.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Filter.java @@ -17,7 +17,7 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; public class Filter { diff --git a/lib/src/main/java/org/kiwix/libkiwix/Illustration.java b/lib/src/main/java/org/kiwix/libkiwix/Illustration.java new file mode 100644 index 0000000..771a4e2 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libkiwix/Illustration.java @@ -0,0 +1,17 @@ + +package org.kiwix.libkiwix; + +public class Illustration +{ + public native int width(); + public native int height(); + public native String mimeType(); + public native String url(); + + public native String getData(); + @Override + protected void finalize() { dispose(); } + + private native void dispose(); + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIICU.java b/lib/src/main/java/org/kiwix/libkiwix/JNIICU.java similarity index 97% rename from lib/src/main/java/org/kiwix/kiwixlib/JNIICU.java rename to lib/src/main/java/org/kiwix/libkiwix/JNIICU.java index db8ba6e..f4ff419 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/JNIICU.java +++ b/lib/src/main/java/org/kiwix/libkiwix/JNIICU.java @@ -18,7 +18,7 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; public class JNIICU { diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwix.java b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwix.java similarity index 91% rename from lib/src/main/java/org/kiwix/kiwixlib/JNIKiwix.java rename to lib/src/main/java/org/kiwix/libkiwix/JNIKiwix.java index 57e148b..ea3c639 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwix.java +++ b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwix.java @@ -18,16 +18,17 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; import android.content.Context; import com.getkeepsafe.relinker.ReLinker; -import org.kiwix.kiwixlib.JNIICU; +import org.kiwix.libkiwix.JNIICU; public class JNIKiwix { public JNIKiwix(final Context context){ ReLinker.loadLibrary(context, "kiwix"); + ReLinker.loadLibrary(context, "zim"); } public void setDataDirectory(String icuDataDir) { diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixException.java b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixException.java similarity index 96% rename from lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixException.java rename to lib/src/main/java/org/kiwix/libkiwix/JNIKiwixException.java index dd0e214..da77973 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixException.java +++ b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixException.java @@ -17,7 +17,7 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; public class JNIKiwixException extends Exception { diff --git a/lib/src/main/java/org/kiwix/kiwixlib/Library.java b/lib/src/main/java/org/kiwix/libkiwix/Library.java similarity index 55% rename from lib/src/main/java/org/kiwix/kiwixlib/Library.java rename to lib/src/main/java/org/kiwix/libkiwix/Library.java index 68aa7d2..e9f4c47 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/Library.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Library.java @@ -17,33 +17,50 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; -import org.kiwix.kiwixlib.Book; -import org.kiwix.kiwixlib.JNIKiwixException; +import org.kiwix.libzim.Archive; +import org.kiwix.libzim.Searcher; +import org.kiwix.libkiwix.Book; +import org.kiwix.libkiwix.JNIKiwixException; +import org.kiwix.libkiwix.Bookmark; public class Library { - public native boolean addBook(String path) throws JNIKiwixException; + public Library() + { + setNativeHandler(); + } + public native boolean addBook(Book book) throws JNIKiwixException; public native Book getBookById(String id); - public native int getBookCount(boolean localBooks, boolean remoteBooks); + + public native Archive getArchiveById(String id); + //public native Searcher getSearcherById(String id); + //public native Searcher getSearcherByIds(String ids[]); + + public native boolean removeBookById(String id); + + public native boolean writeToFile(String path); + public native boolean writeBookmarksToFile(String path); + + 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[] getBooksCategories(); public native String[] getBooksCreators(); public native String[] getBooksPublishers(); - public Library() - { - allocate(); - } + public native void addBookmark(Bookmark bookmark); + public native boolean removeBookmark(String zimId, String url); + public native Bookmark[] getBookmarks(boolean onlyValidBookmarks); @Override protected void finalize() { dispose(); } - private native void allocate(); + private native void setNativeHandler(); private native void dispose(); private long nativeHandle; } diff --git a/lib/src/main/java/org/kiwix/kiwixlib/Manager.java b/lib/src/main/java/org/kiwix/libkiwix/Manager.java similarity index 97% rename from lib/src/main/java/org/kiwix/kiwixlib/Manager.java rename to lib/src/main/java/org/kiwix/libkiwix/Manager.java index 2772ca9..5ddbc08 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/Manager.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Manager.java @@ -17,9 +17,9 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; -import org.kiwix.kiwixlib.Library; +import org.kiwix.libkiwix.Library; public class Manager { diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixServer.java b/lib/src/main/java/org/kiwix/libkiwix/Server.java similarity index 78% rename from lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixServer.java rename to lib/src/main/java/org/kiwix/libkiwix/Server.java index 11c5f0d..b8b5a78 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixServer.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Server.java @@ -17,12 +17,12 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; -import org.kiwix.kiwixlib.JNIKiwixException; -import org.kiwix.kiwixlib.Library; +import org.kiwix.libkiwix.JNIKiwixException; +import org.kiwix.libkiwix.Library; -public class JNIKiwixServer +public class Server { public native void setRoot(String root); @@ -40,11 +40,16 @@ public class JNIKiwixServer public native void stop(); - public JNIKiwixServer(Library library) + public Server(Library library) { - nativeHandle = getNativeServer(library); + setNativeServer(library); } - private native long getNativeServer(Library library); + @Override + protected void finalize() { dispose(); } + + + private native void setNativeServer(Library library); + private native void dispose(); private long nativeHandle; } diff --git a/lib/src/main/java/org/kiwix/libzim/Archive.java b/lib/src/main/java/org/kiwix/libzim/Archive.java new file mode 100644 index 0000000..735cc79 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/Archive.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.ZimFileFormatException; +import org.kiwix.libzim.Entry; +import org.kiwix.libzim.Item; +import org.kiwix.libzim.EntryIterator; +import java.io.FileDescriptor; + +public class Archive +{ + + public Archive(String filename) throws ZimFileFormatException + { + setNativeArchive(filename); + if (nativeHandle == 0) { + throw new ZimFileFormatException("Cannot open zimfile "+filename); + } + } + + public Archive(FileDescriptor fd) throws ZimFileFormatException + { + setNativeArchiveByFD(fd); + if (nativeHandle == 0) { + throw new ZimFileFormatException("Cannot open zimfile by fd "+fd.toString()); + } + } + + public Archive(FileDescriptor fd, long offset, long size) + throws ZimFileFormatException + { + setNativeArchiveEmbedded(fd, offset, size); + if (nativeHandle == 0) { + throw new ZimFileFormatException(String.format("Cannot open embedded zimfile (fd=%s, offset=%d, size=%d)", fd, offset, size)); + } + } + + public native String getFilename(); + public native long getFilesize(); + public native int getAllEntryCount(); + public native int getEntryCount(); + public native int getArticleCount(); + public native int getMediaCount(); + public native String getUuid(); + public native String getMetadata(String name); + public native Item getMetadataItem(String name); + public native String[] getMetadataKeys(); + + public native Item getIllustrationItem(int size); + public native boolean hasIllustration(int size); + public native long[] getIllustrationSizes(); + + public native Entry getEntryByPath(String path); + public native Entry getEntryByPath(int index); + public native boolean hasEntryByPath(String path); + + public native Entry getEntryByTitle(String title); + public native Entry getEntryByTitle(int index); + public native boolean hasEntryByTitle(String title); + + public native Entry getEntryByClusterOrder(int index); + + public native Entry getMainEntry(); + public native boolean hasMainEntry(); + + public native Entry getRandomEntry(); + + public native boolean hasFulltextIndex(); + public native boolean hasTitleIndex(); + + public native boolean hasChecksum(); + public native String getChecksum(); + public native boolean check(); + + public native boolean isMultiPart(); + public native boolean hasNewNamespaceScheme(); + + public native EntryIterator iterByPath(); + public native EntryIterator iterByTitle(); + public native EntryIterator iterEfficient(); + public native EntryIterator findByPath(String path); + public native EntryIterator findByTitle(String path); + + + private native void setNativeArchive(String filename); + private native void setNativeArchiveByFD(FileDescriptor fd); + private native void setNativeArchiveEmbedded(FileDescriptor fd, long offset, long size); + + @Override + protected void finalize() { dispose(); } + + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixInt.java b/lib/src/main/java/org/kiwix/libzim/Blob.java similarity index 62% rename from lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixInt.java rename to lib/src/main/java/org/kiwix/libzim/Blob.java index a66c937..93852f2 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixInt.java +++ b/lib/src/main/java/org/kiwix/libzim/Blob.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2022 Matthieu Gautier * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,9 +17,24 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libzim; -public class JNIKiwixInt +import org.kiwix.libzim.Blob; + +public class Blob { - public int value; + public native String getData(); + public native long size(); + + + @Override + protected void finalize() { dispose(); } + + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; } diff --git a/lib/src/main/java/org/kiwix/kiwixlib/DirectAccessInfo.java b/lib/src/main/java/org/kiwix/libzim/DirectAccessInfo.java similarity index 96% rename from lib/src/main/java/org/kiwix/kiwixlib/DirectAccessInfo.java rename to lib/src/main/java/org/kiwix/libzim/DirectAccessInfo.java index 4ba137d..61950a5 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/DirectAccessInfo.java +++ b/lib/src/main/java/org/kiwix/libzim/DirectAccessInfo.java @@ -17,7 +17,7 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libzim; public class DirectAccessInfo { diff --git a/lib/src/main/java/org/kiwix/libzim/Entry.java b/lib/src/main/java/org/kiwix/libzim/Entry.java new file mode 100644 index 0000000..052a9df --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/Entry.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.Item; + +public class Entry +{ + public native boolean isRedirect(); + public native String getTitle(); + public native String getPath(); + + public native Item getItem(boolean follow); + public native Item getRedirect(); + public native Entry getRedirectEntry(); + + @Override + protected void finalize() { dispose(); } + +///--------- The wrapper thing + // To delete our native wrapper + private native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/EntryIterator.java b/lib/src/main/java/org/kiwix/libzim/EntryIterator.java new file mode 100644 index 0000000..b072de1 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/EntryIterator.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import java.util.Iterator; + +public class EntryIterator implements Iterator +{ + private EntryIterator(int order) { + this.order = order; + } + public native boolean hasNext(); + public native Entry next(); + + @Override + protected void finalize() { dispose(); } + + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A marker of the order used for this iterator + private int order; + + // A pointer (as a long) to a native Handle + private long nativeHandle; + + // A pointer (as a long) to the native end + private long nativeHandleEnd; +} diff --git a/lib/src/main/java/org/kiwix/libzim/Item.java b/lib/src/main/java/org/kiwix/libzim/Item.java new file mode 100644 index 0000000..2b59b82 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/Item.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.Blob; +import org.kiwix.libzim.DirectAccessInfo; + +public class Item +{ + public native String getTitle(); + public native String getPath(); + public native String getMimetype(); + + public native Blob getData(); + public native long getSize(); + + public native DirectAccessInfo getDirectAccessInformation(); + + @Override + protected void finalize() { dispose(); } + +///--------- The wrapper thing + // To delete our native wrapper + private native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/Query.java b/lib/src/main/java/org/kiwix/libzim/Query.java new file mode 100644 index 0000000..75599b5 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/Query.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +public class Query +{ + public Query(String query) { + setNativeQuery(query); + } + + public native Query setQuery(String query); + public native Query setGeorange(float latitude, float longitute, float distance); + + @Override + protected void finalize() { dispose(); } + + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private native long setNativeQuery(String query); + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/Search.java b/lib/src/main/java/org/kiwix/libzim/Search.java new file mode 100644 index 0000000..d078886 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/Search.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.SearchIterator; + +public class Search +{ + public native SearchIterator getResults(int start, int maxResults); + public native long getEstimatedMatches(); + + @Override + protected void finalize() { dispose(); } + + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/SearchIterator.java b/lib/src/main/java/org/kiwix/libzim/SearchIterator.java new file mode 100644 index 0000000..0a73912 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/SearchIterator.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.SearchIterator; +import java.util.Iterator; + +public class SearchIterator implements Iterator +{ + public native String getPath(); + public native String getTitle(); + public native int getScore(); + public native String getSnippet(); + public native int getWordCount(); + public native int getFileIndex(); + public native int getSize(); + public native String getZimId(); + + public native boolean hasNext(); + public native Entry next(); + + + @Override + protected void finalize() { dispose(); } + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; + + // A pointer (as a long) to the native end + private long nativeHandleEnd; +} diff --git a/lib/src/main/java/org/kiwix/libzim/Searcher.java b/lib/src/main/java/org/kiwix/libzim/Searcher.java new file mode 100644 index 0000000..cd3c4d6 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/Searcher.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.ZimFileFormatException; +import org.kiwix.libzim.Archive; +import org.kiwix.libzim.Search; +import org.kiwix.libzim.Query; +import java.io.FileDescriptor; + +public class Searcher +{ + + public Searcher(Archive archive) throws Exception + { + setNativeSearcher(archive); + if (nativeHandle == 0) { + throw new Exception("Cannot create searcher"); + } + } + + public Searcher(Archive[] archives) throws Exception + { + setNativeSearcherMulti(archives); + if (nativeHandle == 0) { + throw new Exception("Cannot create searcher"); + } + } + + public native Searcher addArchive(Archive archive); + public native Search search(Query query); + public native void setVerbose(boolean verbose); + + private native void setNativeSearcher(Archive archive); + private native void setNativeSearcherMulti(Archive[] archives); + + @Override + protected void finalize() { dispose(); } + + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/SuggestionItem.java b/lib/src/main/java/org/kiwix/libzim/SuggestionItem.java new file mode 100644 index 0000000..4021cd4 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/SuggestionItem.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +public class SuggestionItem +{ + public native String getTitle(); + public native String getPath(); + public native String getSnippet(); + public native boolean hasSnippet(); + + @Override + protected void finalize() { dispose(); } + +///--------- The wrapper thing + // To delete our native wrapper + private native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/SuggestionIterator.java b/lib/src/main/java/org/kiwix/libzim/SuggestionIterator.java new file mode 100644 index 0000000..20d0298 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/SuggestionIterator.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.SuggestionItem; +import java.util.Iterator; + +public class SuggestionIterator implements Iterator +{ + public native boolean hasNext(); + public native SuggestionItem next(); + + @Override + protected void finalize() { dispose(); } + + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; + + // A pointer (as a long) to the native end + private long nativeHandleEnd; +} diff --git a/lib/src/main/java/org/kiwix/libzim/SuggestionSearch.java b/lib/src/main/java/org/kiwix/libzim/SuggestionSearch.java new file mode 100644 index 0000000..5d2976b --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/SuggestionSearch.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.SuggestionIterator; + +public class SuggestionSearch +{ + public native SuggestionIterator getResults(int start, int maxResults); + public native long getEstimatedMatches(); + + + @Override + protected void finalize() { dispose(); } + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/SuggestionSearcher.java b/lib/src/main/java/org/kiwix/libzim/SuggestionSearcher.java new file mode 100644 index 0000000..c45961e --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/SuggestionSearcher.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.ZimFileFormatException; +import org.kiwix.libzim.Archive; +import org.kiwix.libzim.SuggestionSearch; +import java.io.FileDescriptor; + +public class SuggestionSearcher +{ + + public SuggestionSearcher(Archive archive) throws Exception + { + setNativeSearcher(archive); + if (nativeHandle == 0) { + throw new Exception("Cannot create searcher"); + } + } + + public native SuggestionSearch suggest(String query); + public native void setVerbose(boolean verbose); + + private native void setNativeSearcher(Archive archive); + + + @Override + protected void finalize() { dispose(); } + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixBool.java b/lib/src/main/java/org/kiwix/libzim/ZimFileFormatException.java similarity index 76% rename from lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixBool.java rename to lib/src/main/java/org/kiwix/libzim/ZimFileFormatException.java index 74563d3..72a2897 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixBool.java +++ b/lib/src/main/java/org/kiwix/libzim/ZimFileFormatException.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,9 +17,11 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libzim; -public class JNIKiwixBool +public class ZimFileFormatException extends Exception { - public boolean value; + public ZimFileFormatException(String message) { + super(message); + } }