From 7c95917a52e7b3af8ade834fb99b378c5f93e33c Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 1 Feb 2023 18:20:36 +0100 Subject: [PATCH] Add bookmark wrapper. --- lib/src/main/cpp/CMakeLists.txt | 1 + lib/src/main/cpp/libkiwix/bookmark.cpp | 72 +++++++++++++++++++ lib/src/main/cpp/libkiwix/library.cpp | 23 ++++++ .../java/org/kiwix/libkiwix/Bookmark.java | 53 ++++++++++++++ .../main/java/org/kiwix/libkiwix/Library.java | 6 ++ 5 files changed, 155 insertions(+) create mode 100644 lib/src/main/cpp/libkiwix/bookmark.cpp create mode 100644 lib/src/main/java/org/kiwix/libkiwix/Bookmark.java diff --git a/lib/src/main/cpp/CMakeLists.txt b/lib/src/main/cpp/CMakeLists.txt index c335b10..f768ff2 100644 --- a/lib/src/main/cpp/CMakeLists.txt +++ b/lib/src/main/cpp/CMakeLists.txt @@ -50,6 +50,7 @@ add_library( libkiwix/kiwixicu.cpp libkiwix/kiwixserver.cpp libkiwix/library.cpp + libkiwix/bookmark.cpp libkiwix/manager.cpp libkiwix/illustration.cpp ) 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/libkiwix/library.cpp b/lib/src/main/cpp/libkiwix/library.cpp index 909ca01..e27e60d 100644 --- a/lib/src/main/cpp/libkiwix/library.cpp +++ b/lib/src/main/cpp/libkiwix/library.cpp @@ -64,6 +64,9 @@ METHOD(jboolean, removeBookById, jstring 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))); @@ -80,3 +83,23 @@ 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/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/libkiwix/Library.java b/lib/src/main/java/org/kiwix/libkiwix/Library.java index 5d0b213..e9f4c47 100644 --- a/lib/src/main/java/org/kiwix/libkiwix/Library.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Library.java @@ -23,6 +23,7 @@ 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 { @@ -41,6 +42,7 @@ public class Library 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); @@ -52,6 +54,10 @@ public class Library public native String[] getBooksCreators(); public native String[] getBooksPublishers(); + 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 setNativeHandler();