Skip to content

Commit

Permalink
sanki-attach
Browse files Browse the repository at this point in the history
  • Loading branch information
Szybet committed Jan 22, 2024
1 parent 7236e8f commit c3e0846
Show file tree
Hide file tree
Showing 16 changed files with 205 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,4 @@ nickel_app/sanki/.adds/sanki/lib/*
nickel_app/sanki/.adds/sanki/sanki.bin
inkbox_userapp/sanki_inkbox.zip
nickel_app/sanki_nickel.zip
inkbox_userapp/sanki/app-bin/s2png
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
[submodule "libraries/ereader/inkbox"]
path = libraries/ereader/inkbox
url = https://github.com/Kobo-InkBox/inkbox.git
[submodule "anki-attach/s2png"]
path = anki-attach/s2png
url = https://github.com/dbohdan/s2png.git
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ To sync sanki with anki ( On Linux host ):
6. On the ereader, input your host IP address and port like that: <ip_address>:\<port> for example 192.168.1.1:8766
7. Sync :)

### anki-attach
This tool allows to open koreader and read books directly from sanki. Notes:
- Works only on InkBox OS
- Don't resize the images with sanki-sync if using this

### Installation
- For Nickel, just extract the zip to the main folder when connecting via usb - **You also need Nickel Menu, Kfmon and Koreader installed ( from the one click package )**
- For InkBox, extract it to `.apps` folder
Expand Down
10 changes: 10 additions & 0 deletions anki-attach/anki-drop-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

cargo install ripdrag
cargo install s2png

cp anki-drop.desktop ~/.local/share/applications/
cp anki-drop.sh ~/.local/bin/

kbuildsycoca5 --noincremental || true
update-desktop-database || true
7 changes: 7 additions & 0 deletions anki-attach/anki-drop.desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[Desktop Entry]
Name=anki-drop
Exec=~/.local/bin/anki-drop.sh %f
Icon=/usr/share/pixmaps/anki.png
Type=Application
Categories=Utility;
MimeType=application/txt;application/pdf;application/epub+zip;
15 changes: 15 additions & 0 deletions anki-attach/anki-drop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

remove_non_ascii() {
echo "$1" | iconv -c -t ASCII//TRANSLIT
}

file="$1"

filename=$(basename "$file")
filename_clear=$(remove_non_ascii "$filename")

rm -rf /tmp/anki-drop/
mkdir /tmp/anki-drop/
s2png -o "/tmp/anki-drop/${filename_clear}.png" -b "$filename_clear" -s "$file"
ripdrag -x -a -n -d "/tmp/anki-drop/${filename_clear}.png"
1 change: 1 addition & 0 deletions anki-attach/s2png
Submodule s2png added at dc3e4d
86 changes: 85 additions & 1 deletion cardView/deckPlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include <QGestureEvent>
#include <QGestureRecognizer>

#include <QMessageBox>
#include <QPalette>

#include <algorithm>
#include <random>

Expand Down Expand Up @@ -50,6 +53,22 @@ DeckPlay::DeckPlay(QWidget *parent) :
ui->cardStatsLabel->setStyleSheet("font-size: 6pt;");
ui->zoomLabel->setStyleSheet("font-size: 6pt;");

// We cant disable selection because of clicking on links
//ui->textBackCard->setStyleSheet("selection-color: black; selection-background-color: white;");
//ui->textFrontCard->setStyleSheet("selection-color: black; selection-background-color: white;");
// {
// QPalette pal = ui->textBackCard->palette();
// pal.setColor(QPalette::HighlightedText, QColor("white"));
// pal.setColor(QPalette::Highlight, QColor("white"));
// ui->textBackCard->setPalette(pal);
// }
// {
// QPalette pal = ui->textFrontCard->palette();
// pal.setColor(QPalette::HighlightedText, QColor("white"));
// pal.setColor(QPalette::Highlight, QColor("white"));
// ui->textFrontCard->setPalette(pal);
// }

manageGestures();
}

Expand Down Expand Up @@ -566,7 +585,7 @@ bool DeckPlay::event(QEvent *event)
} else if(QGesture* gesture = gEvent->gesture(Qt::TapGesture)) {
Q_UNUSED(gesture);
if(enabledTapGesture == true) {
qDebug() << "Tap gesture detected";
//qDebug() << "Tap gesture detected";
int elapsed = gestureTimer->elapsed();
if(gestureTimer->isValid() == true && elapsed > 850) {
// doesnt work with qpoint, idk it gives relative point to the widget?...
Expand Down Expand Up @@ -602,3 +621,68 @@ void DeckPlay::receiveDeckCall(QString call) {
reversedCards = !reversedCards;
}
}

void DeckPlay::linkClicked(const QUrl &arg1) {
qDebug() << "Link clicked:" << arg1;
if(ereaderVars::inkboxUserApp == true) {
QString link = arg1.url();
QMessageBox msgBox;
msgBox.setText("Do you want to open the book?");
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
int ret = msgBox.exec();
if(ret == QMessageBox::Yes) {
qDebug() << "The answer was yes";
QString fileName = "/app-temp/" + link.split(BOOK_LINK_SPLIT).first().replace(".png", "");
QString filePath = ui->textBackCard->searchPaths().first() + "/" + link.split(BOOK_LINK_SPLIT).last();
qDebug() << "fileName of book:" << fileName;
qDebug() << "filePath of book:" << filePath;

{
QProcess process;
process.setProgram("/app-bin/s2png");
QStringList arguments;
arguments << "-o" << fileName << filePath;
process.setArguments(arguments);
process.start();
process.waitForStarted();
process.waitForFinished(-1);
}
{
QProcess process;
process.setProgram("/system-onboard/.apps/koreader/koreader/app-bin/koreader/koreader.sh");
QStringList arguments;
arguments << fileName;
process.setArguments(arguments);
process.start();
process.waitForStarted();
process.waitForFinished(-1);
}
QFile file(fileName);
file.remove();
}
}
}

void DeckPlay::on_textBackCard_anchorClicked(const QUrl &arg1)
{
linkClicked(arg1);
}

void DeckPlay::on_textFrontCard_anchorClicked(const QUrl &arg1)
{
linkClicked(arg1);
}

void DeckPlay::on_textBackCard_selectionChanged()
{
QTextCursor cursor = ui->textBackCard->textCursor();
cursor.clearSelection();
ui->textBackCard->setTextCursor(cursor);
}

void DeckPlay::on_textFrontCard_selectionChanged()
{
QTextCursor cursor = ui->textFrontCard->textCursor();
cursor.clearSelection();
ui->textFrontCard->setTextCursor(cursor);
}
8 changes: 8 additions & 0 deletions cardView/deckPlay.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class DeckPlay : public QWidget

void receiveDeckCall(QString call);
bool reversedCards = false;
void linkClicked(const QUrl &arg1);

protected:
bool event(QEvent* event) override;
Expand All @@ -72,6 +73,13 @@ public slots:
private slots:
void on_horizontalScrollBar_valueChanged(int value);

void on_textBackCard_anchorClicked(const QUrl &arg1);

void on_textFrontCard_anchorClicked(const QUrl &arg1);
void on_textBackCard_selectionChanged();

void on_textFrontCard_selectionChanged();

private:
Ui::DeckPlay *ui;
void cardSizeManage(QTextBrowser* text);
Expand Down
7 changes: 5 additions & 2 deletions cardView/deckPlay.ui
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ li.checked::marker { content: &quot;\2612&quot;; }
<number>0</number>
</property>
<property name="textInteractionFlags">
<set>Qt::NoTextInteraction</set>
<set>Qt::TextBrowserInteraction</set>
</property>
<property name="openExternalLinks">
<bool>false</bool>
</property>
<property name="openLinks">
<bool>false</bool>
Expand Down Expand Up @@ -242,7 +245,7 @@ li.checked::marker { content: &quot;\2612&quot;; }
<number>0</number>
</property>
<property name="textInteractionFlags">
<set>Qt::NoTextInteraction</set>
<set>Qt::TextBrowserInteraction</set>
</property>
<property name="openExternalLinks">
<bool>false</bool>
Expand Down
51 changes: 51 additions & 0 deletions cardView/functions/helperFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ void centerText(QTextBrowser* text) {
}
}

bool containsBook(QString str) {
if(str.contains(".epub") || str.contains(".pdf") || str.contains(".txt")) {
return true;
} else {
return false;
}
}

void correctMainCard(QString* mainCard, QFile mediaFile)
{
// turn those weird image id's to file names
Expand Down Expand Up @@ -66,8 +74,51 @@ void correctMainCard(QString* mainCard, QFile mediaFile)
{
qDebug() << "mainCard contains items_last";
mainCard = &mainCard->replace(replace_items.last(), replace_items.first());
QString lastItem = replace_items.last();
if(containsBook(lastItem)) {
qDebug() << "It contains a book, detected in json handling";
QString last = replace_items.last();
QString first = replace_items.first();
last.chop(1);
first.remove(0, 1);
QString newLink = last + BOOK_LINK_SPLIT + first;
qDebug() << "newLink:" << newLink;

mainCard = &mainCard->replace(replace_items.first(), newLink);
}
}
}
}

if(containsBook(*mainCard)) {
qDebug() << "This card contains a book" << *mainCard;

QStringList splittedhtml = mainCard->split("<");
QString tmpCard = splittedhtml.first();
splittedhtml.removeFirst();
foreach(QString str, splittedhtml) {
if(str.startsWith("img")) {
if(containsBook(str)) {
qDebug() << "Detected in in img";
QStringList split2 = str.split(">");
QString newStr = split2.first().replace("img src=\"", "");

while(newStr.endsWith(">") || newStr.endsWith(" ") || newStr.endsWith("\"")) {
qDebug() << "Removing...";
newStr.chop(1);
}
qDebug() << "Clear link is:" << newStr;

tmpCard.append("<a href=\"" + newStr + "\">" + newStr.split(BOOK_LINK_SPLIT).first() + "</a>" + split2.last());
} else {
tmpCard.append("<" + str);
}
} else {
tmpCard.append("<" + str);
}
}
*mainCard = tmpCard;
qDebug() << "Final link:" << *mainCard;
}

// Weird: &nbsp; ( hard space in html ) sometimes doesn't get parsed
Expand Down
2 changes: 2 additions & 0 deletions cardView/functions/helperFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <QSqlQuery>
#include <QRandomGenerator>

#define BOOK_LINK_SPLIT "@#$$#@"

void splitMainCard(QString mainCard, QString* frontCard, QString* backCard, DeckPlay* parent);
void centerText(QTextBrowser* text);
void correctMainCard(QString* mainCard, QFile mediaFile);
Expand Down
2 changes: 1 addition & 1 deletion cardView/modes/boxes/boxes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void boxes::loop() {
}
parent->changeStatusBarTextSlot(deckName);

qDebug() << "Choosed card:" << choosenCard;
qDebug() << "Choosed card:" << *choosenCard;
choosenCard->count += 1;

QString mainCard = cardExtract(choosenCard, parent);
Expand Down
2 changes: 1 addition & 1 deletion inkbox_userapp/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"IconPath": "/app-misc/sanki.svg",
"Name": "sanki",
"SupportedDevices": [ "n306" ],
"RequiredFeatures": [ 3, 4, 7, 8 ] ,
"RequiredFeatures": [ 3, 4, 8, 9 ] ,
"Version": "v0.3.2"
}
}
5 changes: 5 additions & 0 deletions inkbox_userapp/create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
cd ../

cp build/ereader/sanki inkbox_userapp/sanki/app-bin/sanki.bin
cd anki-attach/
cd s2png/
cross build --release --target arm-unknown-linux-musleabihf
cd ../../
cp anki-attach/s2png/target/arm-unknown-linux-musleabihf/release/s2png inkbox_userapp/sanki/app-bin/

rm -rf inkbox_userapp/sanki/app-lib/*
cp libraries/ereader/ereaderdev-lib/ereaderdev/build/* inkbox_userapp/sanki/app-lib/
Expand Down
8 changes: 5 additions & 3 deletions sanki-sync/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ struct Args {
port: u16,
#[arg(short, long, default_value_t = String::from("decks"))]
target_directory: String,
#[arg(short, long, default_value_t = true)]
#[arg(short, long, default_value_t = false)]
omit_default_deck: bool,
#[arg(short, long, default_value_t = true)]
#[arg(short, long, default_value_t = false)]
resize_images: bool,
#[arg(long, default_value_t = 1024)]
img_height: u32,
Expand Down Expand Up @@ -160,7 +160,7 @@ async fn main() {
.remove(decks.result.iter().position(|r| r == "Default").unwrap());
}

std::fs::remove_dir_all(args.target_directory.clone()).unwrap();
let _ = std::fs::remove_dir_all(args.target_directory.clone());
std::fs::create_dir_all(args.target_directory.clone()).unwrap();
let path_dir = std::fs::canonicalize(PathBuf::from(args.target_directory.clone()))
.unwrap()
Expand Down Expand Up @@ -247,6 +247,8 @@ async fn main() {

debug!("Removing directory: {}", tmp_dir_path);
std::fs::remove_dir_all(tmp_dir_path.clone()).unwrap_or_else(|_| panic!("failed to remove dir {}", tmp_dir_path));
} else {
info!("Not resizing images");
}
}

Expand Down

0 comments on commit c3e0846

Please sign in to comment.