Skip to content

Commit

Permalink
Wrap new methods migrateBookmarks and getBestTargetBookId of libk…
Browse files Browse the repository at this point in the history
…iwix.
  • Loading branch information
mgautierfr authored and kelson42 committed Feb 25, 2024
1 parent 1aeb144 commit 58bff5f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ task buildHeaders(type: Exec) {
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/BookmarkMigrationResult.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 " +
Expand Down
33 changes: 33 additions & 0 deletions lib/src/main/cpp/libkiwix/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ METHOD(jboolean, removeBookmark, jstring zimId, jstring url) {
return TO_JNI(THIS->removeBookmark(TO_C(zimId), TO_C(url)));
} CATCH_EXCEPTION(false)

METHOD(jobject, migrateBookmarks, jboolean allowDowngrade) {
jobject migrationResult = newObject("org/kiwix/libkiwix/BookmarkMigrationResult", env);
int nbMigrated, nbTotal;
std::tie(nbMigrated, nbTotal) = THIS->migrateBookmarks(
TO_C(allowDowngrade) ? kiwix::ALLOW_DOWNGRADE : kiwix::UPGRADE_ONLY
);
setMigrationResultValue(nbMigrated, nbTotal, migrationResult, env);
return migrationResult;
} CATCH_EXCEPTION(nullptr)

METHOD(jint, migrateBookmarks, jstring sourceBookId, jboolean allowDowngrade) {
return TO_JNI(THIS->migrateBookmarks(
TO_C(sourceBookId),
allowDowngrade? kiwix::ALLOW_DOWNGRADE : kiwix::UPGRADE_ONLY
));
} CATCH_EXCEPTION(0)

METHOD(jint, migrateBookmarks, jstring sourceBookId, jstring targetBookId) {
return TO_JNI(THIS->migrateBookmarks(TO_C(sourceBookId), TO_C(targetBookId)));
} CATCH_EXCEPTION(0)

METHOD(jobjectArray, getBookmarks, jboolean onlyValidBookmarks) {
auto bookmarks = THIS->getBookmarks(TO_C(onlyValidBookmarks));
jobjectArray retArray = createArray(env, bookmarks.size(), "org/kiwix/libkiwix/Bookmark");
Expand All @@ -108,3 +129,15 @@ METHOD(jobjectArray, getBookmarks, jboolean onlyValidBookmarks) {
}
return retArray;
} CATCH_EXCEPTION(nullptr)

METHOD(jstring, getBestTargetBookId, jobject bookmark, jboolean allowDowngrade) {
auto cBookmark = getPtr<kiwix::Bookmark>(env, bookmark);
return TO_JNI(THIS->getBestTargetBookId(
*cBookmark,
allowDowngrade ? kiwix::ALLOW_DOWNGRADE : kiwix::UPGRADE_ONLY
));
} CATCH_EXCEPTION(nullptr)

METHOD(jstring, getBestTargetBookId, jstring bookName, jstring preferedFlavour, jstring minDate) {
return TO_JNI(THIS->getBestTargetBookId(TO_C(bookName), TO_C(preferedFlavour), TO_C(minDate)));
} CATCH_EXCEPTION(nullptr)
9 changes: 9 additions & 0 deletions lib/src/main/cpp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,15 @@ inline void setDaiObjValue(const std::string& filename, const long offset,
env->SetLongField(obj, offsetFid, offset);
}

inline void setMigrationResultValue(long nbMigrated, long nbTotalInvalid, const jobject obj, JNIEnv* env)
{
jclass objClass = env->GetObjectClass(obj);
jfieldID nbMigratedFid = env->GetFieldID(objClass, "nbMigratedBookmarks", "J");
env->SetLongField(obj, nbMigratedFid, nbMigrated);
jfieldID nbTotalFid = env->GetFieldID(objClass, "nbInvalidBookmarks", "J");
env->SetLongField(obj, nbTotalFid, nbTotalInvalid);
}

inline int throwException(JNIEnv* env, const char* exception, const char* message) {
return env->ThrowNew(env->FindClass(exception), message);
}
Expand Down
26 changes: 26 additions & 0 deletions lib/src/main/java/org/kiwix/libkiwix/BookmarkMigrationResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2024 Matthieu Gautier <[email protected]>
*
* 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 BookmarkMigrationResult

Check warning on line 22 in lib/src/main/java/org/kiwix/libkiwix/BookmarkMigrationResult.java

View check run for this annotation

Codecov / codecov/patch

lib/src/main/java/org/kiwix/libkiwix/BookmarkMigrationResult.java#L22

Added line #L22 was not covered by tests
{
public long nbMigratedBookmarks;
public long nbInvalidBookmarks;
}
13 changes: 13 additions & 0 deletions lib/src/main/java/org/kiwix/libkiwix/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.kiwix.libkiwix.Book;
import org.kiwix.libkiwix.JNIKiwixException;
import org.kiwix.libkiwix.Bookmark;
import org.kiwix.libkiwix.BookmarkMigrationResult;

public class Library
{
Expand Down Expand Up @@ -56,8 +57,20 @@ public Library()

public native void addBookmark(Bookmark bookmark);
public native boolean removeBookmark(String zimId, String url);
public native BookmarkMigrationResult migrateBookmarks(boolean allowDowngrade);
public native int migrateBookmarks(String sourceBookId, boolean allowDowngrade);
public native int migrateBookmarks(String sourceBookId, String targetBookId);
public native Bookmark[] getBookmarks(boolean onlyValidBookmarks);

public native String getBestTargetBookId(Bookmark bookmark, boolean allowDowngrade);
public String getBestTargetBookId(String bookName) {
return getBestTargetBookId(bookName, "", "");

Check warning on line 67 in lib/src/main/java/org/kiwix/libkiwix/Library.java

View check run for this annotation

Codecov / codecov/patch

lib/src/main/java/org/kiwix/libkiwix/Library.java#L67

Added line #L67 was not covered by tests
}
public String getBestTargetBookId(String bookName, String preferedFlavour) {
return getBestTargetBookId(bookName, preferedFlavour, "");

Check warning on line 70 in lib/src/main/java/org/kiwix/libkiwix/Library.java

View check run for this annotation

Codecov / codecov/patch

lib/src/main/java/org/kiwix/libkiwix/Library.java#L70

Added line #L70 was not covered by tests
}
public native String getBestTargetBookId(String bookName, String preferedFlavour, String minDate);

@Override
protected void finalize() { dispose(); }
private native void setNativeHandler();
Expand Down

0 comments on commit 58bff5f

Please sign in to comment.