From 4fed0149c153feeac2907e50a658a9345fc3ebea Mon Sep 17 00:00:00 2001 From: DisableGraphics Date: Sat, 28 May 2022 16:55:40 +0200 Subject: [PATCH] Added ability to save the position --- Makefile | 14 ++++-- src/extensions/pos_save.cpp | 86 +++++++++++++++++++++++++++++++++++++ src/functions.hpp | 26 +++++++++-- src/global_variables.hpp | 3 +- src/main_window.hpp | 15 ++++++- src/signal_functions.hpp | 1 + src/web_contents.h | 8 ++-- 7 files changed, 141 insertions(+), 12 deletions(-) create mode 100644 src/extensions/pos_save.cpp diff --git a/Makefile b/Makefile index 4a6bbba..f4b835f 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -all: korai +all: korai extensions korai: #----------------------------------------------------- @@ -6,8 +6,16 @@ korai: #----------------------------------------------------- mkdir -p build & g++ src/main.cpp `pkg-config gtkmm-3.0 --libs --cflags` `pkg-config webkit2gtk-4.0 --libs --cflags` `pkg-config vte-2.91 --libs --cflags` -larchive -pthread -O3 -std=c++17 -o build/korai -korai-nodownload: +korai-nodownload: extensions #----------------------------------------------------- # BUILDING KORAI-NODOWNLOAD #----------------------------------------------------- - mkdir -p build & g++ src/main.cpp `pkg-config gtkmm-3.0 --libs --cflags` `pkg-config webkit2gtk-4.0 --libs --cflags` -DNODOWNLOAD -larchive -pthread -O3 -std=c++17 -o build/korai-nodownload \ No newline at end of file + mkdir -p build & g++ src/main.cpp `pkg-config gtkmm-3.0 --libs --cflags` `pkg-config webkit2gtk-4.0 --libs --cflags` -DNODOWNLOAD -larchive -pthread -O3 -std=c++17 -o build/korai-nodownload + +extensions: + #----------------------------------------------------- + # BUILDING EXTENSIONS + #----------------------------------------------------- + mkdir -p build + mkdir -p build/korai-extensions + g++ src/extensions/pos_save.cpp -shared -fPIC -o build/korai-extensions/savepos.so `pkg-config webkit2gtk-web-extension-4.0 --libs --cflags` -std=c++17 \ No newline at end of file diff --git a/src/extensions/pos_save.cpp b/src/extensions/pos_save.cpp new file mode 100644 index 0000000..d445baa --- /dev/null +++ b/src/extensions/pos_save.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include +#include +#include + +#define SLEEP_PERIOD 5 + +using namespace std::filesystem; + +bool t{false}; + +static void save_pos(WebKitWebPage * web_page) +{ + std::ofstream tmp_file; + tmp_file.open((std::string) std::filesystem::current_path() + "/poslck"); + tmp_file << "e"; + tmp_file.close(); + sleep(SLEEP_PERIOD + 1); + std::filesystem::remove((std::string) std::filesystem::current_path() + "/poslck"); + WebKitDOMDocument * doc = webkit_web_page_get_dom_document(web_page); + WebKitDOMDOMWindow * win = webkit_dom_document_get_default_view(doc); + + std::ofstream o; + + while(true && !std::filesystem::exists((std::string) std::filesystem::current_path() + "/poslck")) + { + sleep(SLEEP_PERIOD); + o.open(std::filesystem::current_path().string() + "/pos.conf"); + o << webkit_dom_dom_window_get_scroll_y(win); + o.close(); + std::cout << "Y: " << webkit_dom_dom_window_get_scroll_y(win) << "\nX: " << webkit_dom_dom_window_get_scroll_x(win) << "\n"; + + } + +} + +// +// Boilerplate code / signal callback for attaching methods when a +// new javascript context is created. +// +static void +window_object_cleared_callback (WebKitScriptWorld *world, + WebKitWebPage *web_page, + WebKitFrame *frame, + gpointer user_data) +{ + + if(!t) + { + std::thread dothesaving(std::bind(save_pos, web_page)); + dothesaving.detach(); + } +} + +static void +web_page_created_callback (WebKitWebExtension *extension, + WebKitWebPage *web_page, + gpointer user_data) +{ + g_print ("Page %d created for %s\n", + webkit_web_page_get_id (web_page), + webkit_web_page_get_uri (web_page)); + +} + +// +// Extension initialization thing. +// +extern "C" G_MODULE_EXPORT void +webkit_web_extension_initialize (WebKitWebExtension *extension) +{ + std::cout << "EXTENSION INITIALIZED\n"; + + g_signal_connect (webkit_script_world_get_default(), + "window-object-cleared", + G_CALLBACK (window_object_cleared_callback), + NULL); + g_signal_connect (extension, "page-created", + G_CALLBACK (web_page_created_callback), + NULL); + + +} diff --git a/src/functions.hpp b/src/functions.hpp index 61b6e99..292b768 100644 --- a/src/functions.hpp +++ b/src/functions.hpp @@ -129,17 +129,37 @@ inline void load_homepage(WebKitWebView * webview) //Generates the page for a chapter inline std::string generateWebPage() { + std::string scrollto{"0"}; + if(std::filesystem::exists((std::string)std::filesystem::current_path() + "/pos.conf") && gotopos) + { + std::ifstream i; + i.open((std::string)std::filesystem::current_path() + "/pos.conf"); + i >> scrollto; + i.close(); + } + if(comp::isCompressed(file)) { decompress(file); } std::vector filesInFolda{getFilesInFolder(tmp_folder)}; - std::string generated_html{html::initial_html}; - for(auto & fayel : filesInFolda) + std::string generated_html{html::initial_html(scrollto)}; + if(gotopos) { - generated_html += "
\n"; //Lazy loading to improve performance + for(auto & fayel : filesInFolda) + { + generated_html += "
\n"; //Lazy loading prevents Korai from going to the initial position correctly + } + } + else + { + for(auto & fayel : filesInFolda) + { + generated_html += "
\n"; //Lazy loading to improve performance otherwise + } } generated_html += " "; + gotopos = false; return generated_html; } diff --git a/src/global_variables.hpp b/src/global_variables.hpp index 1ab7bb5..1e268bf 100644 --- a/src/global_variables.hpp +++ b/src/global_variables.hpp @@ -4,4 +4,5 @@ inline int position{-1}; //Variables to know different details inline std::string file{""}, folder{""}, saveFile; -inline std::string tmp_folder{""}; \ No newline at end of file +inline std::string tmp_folder{""}; +inline bool gotopos{true}; \ No newline at end of file diff --git a/src/main_window.hpp b/src/main_window.hpp index 0285498..d228566 100644 --- a/src/main_window.hpp +++ b/src/main_window.hpp @@ -7,6 +7,7 @@ #include "gtkmm/window.h" #include #include +#include #include #include #include "icon.xpm" @@ -15,6 +16,7 @@ #include "comp.hpp" #include "signal_functions.hpp" #include "downloader.hpp" +#include "web_contents.h" #include "webkitdom/webkitdomdefines.h" #include @@ -69,6 +71,8 @@ class MainWindow : public Gtk::Window buttonsBox.pack_start(openButton); buttonsBox.pack_start(nextButton); + webkit_web_context_set_web_extensions_directory(webkit_web_context_get_default(), (std::filesystem::current_path().string() + "/korai-extensions").c_str()); + WebKitSettings * settings = WEBKIT_SETTINGS(webkit_settings_new()); webkit_settings_set_enable_smooth_scrolling(settings, TRUE); webkit_settings_set_allow_file_access_from_file_urls(settings, TRUE); @@ -115,6 +119,8 @@ class MainWindow : public Gtk::Window set_titlebar(titleBar); titleBar.set_show_close_button(); + + if(tutorial) { //Yes, I need these absolute f*ckton of arguments. And there's no argument copied by value. (All of them are pointers and references) @@ -134,12 +140,13 @@ class MainWindow : public Gtk::Window } } - + show_all(); webkit_web_view_reload(webview); g_signal_connect_object(webview,"load-changed",G_CALLBACK(on_load_changed), spin.gobj(), G_CONNECT_AFTER); + g_signal_connect(webkit_web_context_get_default(), "initialize-web-extensions",G_CALLBACK(on_get_extensions), NULL); //These all the goddamn buttons. Man, not using Glade didn't pay off... openButton.signal_clicked().connect(sigc::bind(sigc::ptr_fun(open), webview, &titleBar)); nextButton.signal_clicked().connect(sigc::bind(sigc::ptr_fun(next_chapter), webview, &titleBar)); @@ -178,4 +185,10 @@ class MainWindow : public Gtk::Window Gtk::VBox menuBox; Gtk::Separator sep1, sepabout, sep2; + + static void on_get_extensions(WebKitWebContext *context, gpointer user_data) + { + std::string f{std::filesystem::current_path()}; + webkit_web_context_set_web_extensions_directory(context, f.c_str()); + } }; \ No newline at end of file diff --git a/src/signal_functions.hpp b/src/signal_functions.hpp index 9acc222..13e33cc 100644 --- a/src/signal_functions.hpp +++ b/src/signal_functions.hpp @@ -224,6 +224,7 @@ inline void open_mangadex(WebKitWebView * webView, Gtk::HeaderBar * titleBar, st //When korai is opened, this is executed inline void on_load(WebKitWebView * webView, Gtk::HeaderBar& titlebar) { + //Creates the 'tmp' directory std::filesystem::create_directory(((std::string)std::filesystem::current_path() + "/tmp/")); //Loads the chapter file diff --git a/src/web_contents.h b/src/web_contents.h index 07c0c06..350083f 100644 --- a/src/web_contents.h +++ b/src/web_contents.h @@ -15,7 +15,7 @@ namespace css "display: block;\n" "margin-left: auto;\n" "margin-right: auto;\n" - "max-width: 95%;\n" + "width: 95%;\n" "max-height: calc((29.7 / 21) * 95%);\n" "}\n" }; @@ -34,17 +34,17 @@ namespace html }); */ //Used to create a new chapter page - inline std::string initial_html{" " "" "" - ""}; + "";}; } \ No newline at end of file