panthera-www.cc
C++ source, ASCII text, with very long lines (382)
1#include "gpanthera.hh" 2#include <gtkmm.h> 3#include <glibmm.h> 4#include <glibmm/ustring.h> 5#include <iostream> 6#include <memory> 7#include <libintl.h> 8#include <locale.h> 9#include <gtk/gtk.h> 10#include <gdk/gdk.h> 11#include <webkit/webkit.h> 12#include <fstream> 13#include <nlohmann/json.hpp> 14#include <iomanip> 15#include <sstream> 16#include <dlfcn.h> 17#include "external/sqlite_orm/sqlite_orm.h" 18//#include "plugin_api.h" 19#define _(STRING) gettext(STRING) 20 21using plugin_entrypoint_type = void(*)(void); 22 23std::string url_encode(const std::string &value) { 24// Thanks https://stackoverflow.com/a/17708801 25std::ostringstream escaped; 26escaped.fill('0'); 27escaped << std::hex; 28 29for(char c : value) { 30// Keep alphanumeric and other accepted characters intact 31if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') { 32escaped << c; 33continue; 34} 35 36// Any other characters are percent-encoded 37escaped << std::uppercase; 38escaped << '%' << std::setw(2) << int((unsigned char) c); 39escaped << std::nouppercase; 40} 41 42return escaped.str(); 43} 44 45bool starts_with(const Glib::ustring &str, const Glib::ustring &prefix) { 46return str.size() >= prefix.size() && str.compare(0, prefix.size(), prefix) == 0; 47} 48 49bool ends_with(const Glib::ustring &str, const Glib::ustring &suffix) { 50return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; 51} 52 53struct SearchEngine { 54Glib::ustring name; 55Glib::ustring url; 56Glib::ustring get_search_url(const std::string &query) { 57auto pos = url.find("%s"); 58if(pos == Glib::ustring::npos) { 59throw std::runtime_error("Invalid search engine URL: missing '%s' placeholder"); 60} 61auto new_url = url; 62new_url.replace(pos, 2, url_encode(query)); 63return new_url; 64} 65}; 66 67struct HistoryEntry { 68int id; 69std::string url, title; 70int64_t timestamp; 71}; 72 73using StorageType = decltype(sqlite_orm::make_storage("", 74sqlite_orm::make_table("history", 75sqlite_orm::make_column("id", &HistoryEntry::id, sqlite_orm::primary_key()), 76sqlite_orm::make_column("url", &HistoryEntry::url), 77sqlite_orm::make_column("title", &HistoryEntry::title), 78sqlite_orm::make_column("timestamp", &HistoryEntry::timestamp) 79) 80)); 81 82class HistoryManager { 83public: 84static auto make_storage(const std::string& path) { 85return sqlite_orm::make_storage(path, 86sqlite_orm::make_table("history", 87sqlite_orm::make_column("id", &HistoryEntry::id, sqlite_orm::primary_key()), 88sqlite_orm::make_column("url", &HistoryEntry::url), 89sqlite_orm::make_column("title", &HistoryEntry::title), 90sqlite_orm::make_column("timestamp", &HistoryEntry::timestamp) 91) 92); 93} 94StorageType storage; 95explicit HistoryManager(const std::string &db_path) : storage(make_storage(db_path)) { 96storage.sync_schema(); 97} 98 99void log_url(const std::string &url, const std::string title, const int64_t timestamp = Glib::DateTime::create_now_local().to_unix()) { 100storage.insert(HistoryEntry{-1, url, title, timestamp}); 101} 102 103std::vector<HistoryEntry> get_history() { 104return storage.get_all<HistoryEntry>(); 105} 106 107std::vector<HistoryEntry> get_history_between(int64_t start_time, int64_t end_time) { 108using namespace sqlite_orm; 109return storage.get_all<HistoryEntry>( 110where(c(&HistoryEntry::timestamp) >= start_time and c(&HistoryEntry::timestamp) < end_time), 111order_by(&HistoryEntry::timestamp).desc() 112); 113} 114}; 115 116std::vector<WebKitWebView*> collect_webviews(Gtk::Widget const &root) { 117std::vector<WebKitWebView*> result; 118std::function<void(const Gtk::Widget&)> recurse = [&](const Gtk::Widget &widget) { 119gpointer gobj = G_OBJECT(widget.gobj()); 120if(WEBKIT_IS_WEB_VIEW(gobj)) { 121result.push_back(WEBKIT_WEB_VIEW(gobj)); 122} else for(auto *child = widget.get_first_child(); child; child = child->get_next_sibling()) { 123recurse(*child); 124} 125}; 126recurse(root); 127return result; 128} 129 130class HistoryViewer : public gPanthera::DockablePane { 131protected: 132std::shared_ptr<HistoryManager> history_manager; 133Gtk::Calendar *calendar; 134Gtk::Label *date_label; 135Gtk::Box *box_place; 136public: 137sigc::signal<void(const std::string &url)> signal_open_url; 138HistoryViewer(std::shared_ptr<gPanthera::LayoutManager> layout_manager, std::shared_ptr<HistoryManager> history_manager) : gPanthera::DockablePane(layout_manager, *Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 0), "history", _("History"), Gtk::make_managed<Gtk::Image>(Gio::Icon::create("document-open-recent-symbolic"))), history_manager(std::move(history_manager)) { 139auto history_toolbar = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 0); 140auto history_button_previous = Gtk::make_managed<Gtk::Button>(); 141history_button_previous->set_child(*Gtk::make_managed<Gtk::Image>(Gio::Icon::create("go-previous-symbolic"))); 142history_button_previous->set_tooltip_text(_("Previous day")); 143auto history_button_next = Gtk::make_managed<Gtk::Button>(); 144history_button_next->set_child(*Gtk::make_managed<Gtk::Image>(Gio::Icon::create("go-next-symbolic"))); 145history_button_next->set_tooltip_text(_("Next day")); 146 147date_label = Gtk::make_managed<Gtk::Label>(""); 148auto date_button = Gtk::make_managed<Gtk::MenuButton>(); 149date_button->set_child(*date_label); 150date_button->set_tooltip_text(_("Select date")); 151date_button->set_direction(Gtk::ArrowType::NONE); 152auto date_popover = Gtk::make_managed<Gtk::Popover>(); 153calendar = Gtk::make_managed<Gtk::Calendar>(); 154date_popover->set_child(*calendar); 155date_button->set_popover(*date_popover); 156date_button->set_has_frame(false); 157date_button->set_hexpand(true); 158 159history_toolbar->append(*history_button_previous); 160history_toolbar->append(*date_button); 161history_toolbar->append(*history_button_next); 162auto box = dynamic_cast<Gtk::Box*>(this->child); 163box->append(*history_toolbar); 164 165history_button_next->signal_clicked().connect([this]() { 166Glib::DateTime calendar_day = calendar->get_date(); 167Glib::DateTime datetime = Glib::DateTime::create_local( 168calendar_day.get_year(), 169calendar_day.get_month(), 170calendar_day.get_day_of_month(), 1710, 0, 0 172); 173auto next_entries = this->history_manager->storage.get_all<HistoryEntry>( 174sqlite_orm::where(sqlite_orm::c(&HistoryEntry::timestamp) >= datetime.to_unix() + 86400), 175sqlite_orm::order_by(&HistoryEntry::timestamp).asc() 176); 177if(next_entries.empty()) { 178return; 179} 180auto last_entry = next_entries.front(); 181Glib::DateTime next_day = Glib::DateTime::create_now_local(last_entry.timestamp); 182Glib::DateTime new_date = Glib::DateTime::create_local( 183next_day.get_year(), 184next_day.get_month(), 185next_day.get_day_of_month(), 1860, 0, 0 187); 188calendar->select_day(new_date); 189reload_history(); 190}); 191 192history_button_previous->signal_clicked().connect([this]() { 193Glib::DateTime calendar_day = calendar->get_date(); 194Glib::DateTime datetime = Glib::DateTime::create_local( 195calendar_day.get_year(), 196calendar_day.get_month(), 197calendar_day.get_day_of_month(), 1980, 0, 0 199); 200auto previous_entries = this->history_manager->storage.get_all<HistoryEntry>( 201sqlite_orm::where(sqlite_orm::c(&HistoryEntry::timestamp) < datetime.to_unix()), 202sqlite_orm::order_by(&HistoryEntry::timestamp).desc() 203); 204if(previous_entries.empty()) { 205return; 206} 207auto last_entry = previous_entries.front(); 208Glib::DateTime previous_day = Glib::DateTime::create_now_local(last_entry.timestamp); 209Glib::DateTime new_date = Glib::DateTime::create_local( 210previous_day.get_year(), 211previous_day.get_month(), 212previous_day.get_day_of_month(), 2130, 0, 0 214); 215calendar->select_day(new_date); 216reload_history(); 217}); 218 219calendar->signal_day_selected().connect(sigc::mem_fun(*this, &HistoryViewer::reload_history)); 220calendar->signal_prev_month().connect(sigc::mem_fun(*this, &HistoryViewer::reload_history)); 221calendar->signal_next_month().connect(sigc::mem_fun(*this, &HistoryViewer::reload_history)); 222calendar->signal_prev_year().connect(sigc::mem_fun(*this, &HistoryViewer::reload_history)); 223calendar->signal_next_year().connect(sigc::mem_fun(*this, &HistoryViewer::reload_history)); 224Glib::DateTime datetime = Glib::DateTime::create_now_local(); 225calendar->select_day(datetime); 226 227this->signal_pane_shown.connect([this]() { 228// Load the current date 229reload_history(); 230}); 231 232auto scrolled_window = Gtk::make_managed<Gtk::ScrolledWindow>(); 233box_place = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 0); 234auto viewport = Gtk::make_managed<Gtk::Viewport>(nullptr, nullptr); 235viewport->set_child(*box_place); 236scrolled_window->set_child(*viewport); 237scrolled_window->set_policy(Gtk::PolicyType::AUTOMATIC, Gtk::PolicyType::AUTOMATIC); 238scrolled_window->set_vexpand(true); 239scrolled_window->set_hexpand(true); 240box->append(*scrolled_window); 241 242// TODO: allow deleting entries 243} 244 245void reload_history() { 246Glib::DateTime calendar_day = calendar->get_date(); 247Glib::DateTime datetime = Glib::DateTime::create_local( 248calendar_day.get_year(), 249calendar_day.get_month(), 250calendar_day.get_day_of_month(), 2510, 0, 0 252); 253date_label->set_text(datetime.format("%x")); 254auto box = Gtk::make_managed<Gtk::ListBox>(); 255auto history_entries = history_manager->get_history_between(datetime.to_unix(), datetime.to_unix() + 86400); 256for(const auto &entry : history_entries) { 257auto row = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 4); 258auto title = Gtk::make_managed<Gtk::Label>(entry.title.empty() ? "Untitled" : entry.title); 259title->set_halign(Gtk::Align::START); 260title->set_ellipsize(Pango::EllipsizeMode::MIDDLE); 261auto label = Gtk::make_managed<Gtk::Label>(entry.url); 262label->set_halign(Gtk::Align::START); 263label->set_ellipsize(Pango::EllipsizeMode::MIDDLE); 264row->append(*title); 265row->append(*label); 266if(auto time = Glib::DateTime::create_now_local(entry.timestamp)) { 267label->set_tooltip_text(time.format("%c")); 268box->append(*row); 269} 270} 271if(box_place->get_first_child()) { 272box_place->remove(*box_place->get_first_child()); 273} 274box->signal_row_activated().connect([this](Gtk::ListBoxRow *row) { 275if(auto box_row = dynamic_cast<Gtk::Box*>(row->get_child())) { 276if(auto label = dynamic_cast<Gtk::Label*>(box_row->get_last_child())) { 277auto url = label->get_text(); 278if(!url.empty()) { 279signal_open_url.emit(url); 280} 281} 282} 283}); 284box_place->append(*box); 285} 286}; 287 288class PantheraWindow; 289 290class PantheraWww : public Gtk::Application { 291private: 292std::shared_ptr<gPanthera::ContentManager> content_manager; 293std::string cookie_file; 294std::vector<SearchEngine> search_engines; 295SearchEngine *default_search_engine; 296std::shared_ptr<HistoryManager> history_manager; 297Glib::RefPtr<Gio::Menu> main_menu; 298std::string new_tab_page; 299gPanthera::ContentStack *controlled_stack = nullptr; 300 301static void load_change_callback(WebKitWebView* object, WebKitLoadEvent load_event, gpointer data); 302static void notify_callback(GObject* object, GParamSpec* pspec, gpointer data); 303static void notify_focused_callback(GObject* object, GParamSpec* pspec, gpointer data); 304static void on_back_pressed(GtkGestureClick* gesture, int n_press, double x, double y, gpointer user_data); 305static void on_forward_pressed(GtkGestureClick* gesture, int n_press, double x, double y, gpointer user_data); 306static gboolean on_decide_policy(WebKitWebView* source, WebKitPolicyDecision* decision, WebKitPolicyDecisionType type, gpointer user_data); 307static void close_callback(WebKitWebView *source, gpointer user_data); 308 309protected: 310void on_startup() override; 311void on_activate() override; 312void on_shutdown() override; 313PantheraWindow *make_window(); 314 315public: 316PantheraWww(); 317~PantheraWww(); 318friend class PantheraWindow; 319void on_new_tab(gPanthera::ContentStack* stack, const Glib::ustring& url = "", bool focus = true, bool new_window = false); 320void set_search_engines_from_json(const std::string& json_string); 321static Glib::RefPtr<PantheraWww> create(); 322std::shared_ptr<gPanthera::ContentManager> get_content_manager(); 323void load_plugin(const std::string& plugin_path); 324}; 325 326class PantheraWindow : public Gtk::ApplicationWindow { 327private: 328std::shared_ptr<gPanthera::LayoutManager> layout_manager; 329Gtk::Entry *url_bar; 330HistoryViewer *history_viewer; 331gPanthera::ContentPage *controlled_page = nullptr; 332gPanthera::ContentStack *controlled_stack = nullptr; 333Glib::RefPtr<Gio::SimpleAction> back_action, forward_action, reload_action; 334Gtk::Button *go_button; 335 336void enable_controls() { 337back_action->set_enabled(true); 338forward_action->set_enabled(true); 339reload_action->set_enabled(true); 340url_bar->set_sensitive(true); 341go_button->set_sensitive(true); 342} 343 344void disable_controls() { 345back_action->set_enabled(false); 346forward_action->set_enabled(false); 347reload_action->set_enabled(false); 348url_bar->set_sensitive(false); 349url_bar->set_text(""); 350go_button->set_sensitive(false); 351} 352 353sigc::connection moving_connection, moved_connection; 354public: 355friend class PantheraWww; 356 357explicit PantheraWindow(Gtk::Application *application) : Gtk::ApplicationWindow() { 358// There is a constructor with Glib::RefPtr<Gtk::Application>, but it is not appropriate 359// because the window will own the application, creating a cycle 360auto panthera = dynamic_cast<PantheraWww*>(application); 361if(!panthera) { 362throw std::runtime_error("Application is not a PantheraWww instance"); 363} 364panthera->add_window(*this); 365this->set_default_size(800, 600); 366layout_manager = std::make_shared<gPanthera::LayoutManager>(); 367 368auto dock_stack_1 = Gtk::make_managed<gPanthera::DockStack>(layout_manager, _("One"), "one"); 369auto switcher_1 = Gtk::make_managed<gPanthera::DockStackSwitcher>(dock_stack_1, Gtk::Orientation::HORIZONTAL); 370auto dock_stack_2 = Gtk::make_managed<gPanthera::DockStack>(layout_manager, _("Two"), "two"); 371auto switcher_2 = Gtk::make_managed<gPanthera::DockStackSwitcher>(dock_stack_2, Gtk::Orientation::VERTICAL); 372auto pane_1_content = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 0); 373auto debug_button = Gtk::make_managed<Gtk::Button>("Debug"); 374auto print_history_button = Gtk::make_managed<Gtk::Button>("Print history"); 375print_history_button->signal_clicked().connect([this, panthera]() { 376auto history = panthera->history_manager->get_history(); 377for(const auto &entry : history) { 378std::cout << "entry " << entry.id << ", url " << entry.url << ", unix time " << entry.timestamp << std::endl; 379// TODO: exclude redirecting URLs 380} 381}); 382pane_1_content->append(*debug_button); 383pane_1_content->append(*print_history_button); 384auto pane_1_icon = Gtk::make_managed<Gtk::Image>(Gio::Icon::create("help-about-symbolic")); 385auto pane_1 = Gtk::make_managed<gPanthera::DockablePane>(layout_manager, *pane_1_content, "debug", _("Debugging options"), pane_1_icon); 386 387dock_stack_1->set_transition_type(Gtk::StackTransitionType::SLIDE_LEFT_RIGHT); 388dock_stack_1->set_transition_duration(125); 389dock_stack_1->set_expand(true); 390dock_stack_2->set_transition_type(Gtk::StackTransitionType::SLIDE_UP_DOWN); 391dock_stack_2->set_transition_duration(125); 392dock_stack_2->set_expand(true); 393 394auto outer_grid = Gtk::make_managed<Gtk::Grid>(); 395outer_grid->attach(*switcher_2, 0, 2, 1, 1); 396outer_grid->attach(*switcher_1, 1, 3, 1, 1); 397auto outer_paned = Gtk::make_managed<Gtk::Paned>(Gtk::Orientation::HORIZONTAL); 398outer_paned->set_start_child(*dock_stack_2); 399auto inner_paned = Gtk::make_managed<Gtk::Paned>(Gtk::Orientation::VERTICAL); 400auto content = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 0); 401std::function<bool(gPanthera::ContentPage*)> detach_handler; 402 403detach_handler = [this](gPanthera::ContentPage *widget) { 404auto new_stack = Gtk::make_managed<gPanthera::ContentStack>(widget->content_manager, widget->get_stack()->get_detach_handler()); 405auto new_switcher = Gtk::make_managed<gPanthera::ContentTabBar>(new_stack, Gtk::Orientation::HORIZONTAL, dynamic_cast<gPanthera::ContentTabBar*>(widget->get_stack()->get_parent()->get_first_child())->get_extra_child_function()); 406auto new_notebook = Gtk::make_managed<gPanthera::ContentNotebook>(new_stack, new_switcher); 407auto window = new gPanthera::ContentWindow(new_notebook); 408widget->redock(new_stack); 409window->present(); 410window->signal_close_request().connect([window]() { 411auto webviews = collect_webviews(*window); 412for(auto view : webviews) { 413webkit_web_view_try_close(view); 414} 415webviews = collect_webviews(*window); 416if(webviews.empty()) { 417// All documents have been closed safely, the window can be closed 418return false; 419} 420return true; 421}, false); 422controlled_page = nullptr; 423controlled_stack = nullptr; 424disable_controls(); 425return true; 426}; 427 428auto return_extra_child = [this, panthera](gPanthera::ContentTabBar *switcher) { 429auto new_tab_button = Gtk::make_managed<Gtk::Button>(); 430new_tab_button->set_child(*Gtk::make_managed<Gtk::Image>(Gio::Icon::create("list-add-symbolic"))); 431new_tab_button->set_tooltip_text(_("New Tab")); 432new_tab_button->signal_clicked().connect([this, switcher, panthera]() { 433panthera->on_new_tab(switcher->get_stack()); 434}); 435return new_tab_button; 436}; 437controlled_stack = Gtk::make_managed<gPanthera::ContentStack>(panthera->content_manager, detach_handler); 438panthera->controlled_stack = controlled_stack; 439auto content_stack_switcher = Gtk::make_managed<gPanthera::ContentTabBar>(controlled_stack, Gtk::Orientation::HORIZONTAL, return_extra_child); 440panthera->content_manager->add_stack(controlled_stack); 441WebKitWebView *webview = WEBKIT_WEB_VIEW(webkit_web_view_new()); // for some reason, this has to be created 442content->set_name("content_box"); 443auto content_notebook = Gtk::make_managed<gPanthera::ContentNotebook>(controlled_stack, content_stack_switcher, Gtk::PositionType::TOP); 444content->append(*content_notebook); 445inner_paned->set_start_child(*content); 446inner_paned->set_end_child(*dock_stack_1); 447outer_paned->set_end_child(*inner_paned); 448outer_grid->attach(*outer_paned, 1, 2, 1, 1); 449// Create the toolbar 450auto main_toolbar = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 8); 451// URL bar 452url_bar = Gtk::make_managed<Gtk::Entry>(); 453url_bar->set_placeholder_text(_("Enter URL")); 454url_bar->set_hexpand(true); 455auto load_url_callback = [this, panthera]() { 456bool has_protocol = url_bar->get_text().find("://") != std::string::npos; 457if(!has_protocol) { 458url_bar->set_text("http://" + url_bar->get_text()); 459} 460if(controlled_page) { 461if(auto webview = WEBKIT_WEB_VIEW(controlled_page->get_child()->get_first_child()->gobj())) { 462webkit_web_view_load_uri(webview, url_bar->get_text().c_str()); 463} 464} 465}; 466// Go 467go_button = Gtk::make_managed<Gtk::Button>(_("Go")); 468go_button->signal_clicked().connect(load_url_callback); 469url_bar->signal_activate().connect(load_url_callback); 470panthera->content_manager->signal_page_operated.connect([this, panthera](gPanthera::ContentPage *page) { 471if(!page) { 472return; 473} 474if(!page->get_child()) { 475return; 476} 477if(!page->get_child()->get_first_child()) { 478return; 479} 480if(page->get_root() != this) { 481// The page is in some other window 482return; 483} 484controlled_page = page; 485controlled_stack = page->get_stack(); 486panthera->controlled_stack = page->get_stack(); 487if(WEBKIT_WEB_VIEW(page->get_child()->get_first_child()->gobj())) { 488if(webkit_web_view_get_uri(WEBKIT_WEB_VIEW(page->get_child()->get_first_child()->gobj()))) { 489url_bar->set_text(webkit_web_view_get_uri(WEBKIT_WEB_VIEW(page->get_child()->get_first_child()->gobj()))); 490} 491guint url_update_handler = g_signal_connect(page->get_child()->get_first_child()->gobj(), "notify", G_CALLBACK(panthera->notify_focused_callback), this); 492std::shared_ptr<sigc::connection> control_signal_handler = std::make_shared<sigc::connection>(); 493*control_signal_handler = page->signal_control_status_changed.connect([this, page, control_signal_handler, url_update_handler](bool controlled) { 494if(!controlled) { 495control_signal_handler->disconnect(); 496if(page->get_child() && page->get_child()->get_first_child() && WEBKIT_IS_WEB_VIEW(page->get_child()->get_first_child()->gobj())) { 497g_signal_handler_disconnect(page->get_child()->get_first_child()->gobj(), url_update_handler); 498} 499} 500}); 501} 502 503enable_controls(); 504}); 505history_viewer = Gtk::make_managed<HistoryViewer>(layout_manager, panthera->history_manager); 506history_viewer->signal_open_url.connect([this, panthera](const std::string &url) { 507panthera->on_new_tab(nullptr, url, true); 508}); 509// Back, forward, reload 510auto back_button = Gtk::make_managed<Gtk::Button>(); 511back_button->set_child(*Gtk::make_managed<Gtk::Image>(Gio::Icon::create("go-previous-symbolic"))); 512back_button->set_tooltip_text(_("Back")); 513auto forward_button = Gtk::make_managed<Gtk::Button>(); 514forward_button->set_child(*Gtk::make_managed<Gtk::Image>(Gio::Icon::create("go-next-symbolic"))); 515forward_button->set_tooltip_text(_("Forward")); 516auto reload_button = Gtk::make_managed<Gtk::Button>(); 517reload_button->set_child(*Gtk::make_managed<Gtk::Image>(Gio::Icon::create("view-refresh-symbolic"))); 518reload_button->set_tooltip_text(_("Reload")); 519 520back_action = Gio::SimpleAction::create("go_back"); 521add_action(back_action); 522back_action->signal_activate().connect([this, panthera](const Glib::VariantBase&) { 523if(controlled_page) { 524if(auto webview = WEBKIT_WEB_VIEW(controlled_page->get_child()->get_first_child()->gobj())) { 525webkit_web_view_go_back(webview); 526} 527} 528}); 529back_button->set_action_name("win.go_back"); 530 531forward_action = Gio::SimpleAction::create("go_forward"); 532add_action(forward_action); 533forward_action->signal_activate().connect([this, panthera](const Glib::VariantBase&) { 534if(controlled_page) { 535if(auto webview = WEBKIT_WEB_VIEW(controlled_page->get_child()->get_first_child()->gobj())) { 536webkit_web_view_go_forward(webview); 537} 538} 539}); 540forward_button->set_action_name("win.go_forward"); 541 542reload_action = Gio::SimpleAction::create("reload"); 543add_action(reload_action); 544reload_action->signal_activate().connect([this, panthera](const Glib::VariantBase&) { 545if(controlled_page) { 546if(auto webview = WEBKIT_WEB_VIEW(controlled_page->get_child()->get_first_child()->gobj())) { 547webkit_web_view_reload(webview); 548} 549} 550}); 551reload_button->set_action_name("win.reload"); 552 553// Search bar 554// TODO: provide history, provide a menu button with the other engines 555auto search_bar = Gtk::make_managed<Gtk::Entry>(); 556search_bar->set_placeholder_text(_("Search")); 557search_bar->set_hexpand(true); 558if(panthera->search_engines.empty()) { 559search_bar->set_sensitive(false); 560search_bar->set_placeholder_text(_("No search")); 561} 562auto search_callback = [this, search_bar, panthera]() { 563// Create a new tab with the search results 564panthera->on_new_tab(controlled_stack); 565auto page = panthera->content_manager->get_last_operated_page(); 566if(page) { 567if(auto webview = WEBKIT_WEB_VIEW(page->get_child()->get_first_child()->gobj())) { 568auto search_url = panthera->default_search_engine->get_search_url(search_bar->get_text()); 569webkit_web_view_load_uri(webview, search_url.c_str()); 570} 571} 572}; 573if(panthera->default_search_engine) { 574search_bar->signal_activate().connect(search_callback); 575} 576 577// Assemble the toolbar 578main_toolbar->append(*back_button); 579main_toolbar->append(*forward_button); 580main_toolbar->append(*reload_button); 581main_toolbar->append(*url_bar); 582main_toolbar->append(*go_button); 583main_toolbar->append(*search_bar); 584if(panthera->default_search_engine) { 585auto search_button = Gtk::make_managed<Gtk::Button>(panthera->default_search_engine->name); 586search_button->signal_clicked().connect(search_callback); 587main_toolbar->append(*search_button); 588} 589outer_grid->attach(*main_toolbar, 0, 1, 2, 1); 590this->set_child(*outer_grid); 591debug_button->signal_clicked().connect([this, panthera]() { 592if(panthera->content_manager->get_last_operated_page()) { 593std::cout << "Last operated page: " << panthera->content_manager->get_last_operated_page()->get_name() << std::endl; 594} else { 595std::cout << "No page operated!" << std::endl; 596} 597}); 598// TODO: Use the last operated page and allow opening tabs next to the last operated page using certain panes 599// Load the existing layout, if it exists 600std::ifstream layout_file_in("layout.json"); 601if(layout_file_in) { 602std::string layout_json((std::istreambuf_iterator<char>(layout_file_in)), std::istreambuf_iterator<char>()); 603layout_file_in.close(); 604layout_manager->restore_json_layout(layout_json); 605} else { 606// Create a new layout if the file doesn't exist 607layout_file_in.close(); 608 609dock_stack_1->add_pane(*pane_1); 610dock_stack_1->add_pane(*history_viewer); 611 612std::ofstream layout_file_out("layout.json"); 613layout_file_out << layout_manager->get_layout_as_json(); 614layout_file_out.close(); 615} 616// Save the layout when changed 617layout_manager->signal_pane_moved.connect([this, panthera](gPanthera::DockablePane *pane) { 618std::ofstream layout_file_out("layout.json"); 619layout_file_out << layout_manager->get_layout_as_json(); 620layout_file_out.close(); 621std::cout << "Layout changed: " << layout_manager->get_layout_as_json() << std::endl; 622}); 623set_show_menubar(true); 624 625// As the window is initially empty, disable most actions 626disable_controls(); 627 628moving_connection = panthera->content_manager->signal_page_moving.connect([this, panthera](gPanthera::ContentPage *page) { 629if(page->get_root() == this) { 630controlled_page = nullptr; 631controlled_stack = nullptr; 632disable_controls(); 633} 634}); 635moved_connection = panthera->content_manager->signal_page_moved.connect([this, panthera](gPanthera::ContentPage *page) { 636if(page->get_root() == this) { 637panthera->content_manager->set_last_operated_page(page); 638page->get_stack()->set_visible_child(*page); 639enable_controls(); 640} 641}); 642 643signal_close_request().connect([this]() { 644auto webviews = collect_webviews(*this); 645for(auto view : webviews) { 646webkit_web_view_try_close(view); 647} 648webviews = collect_webviews(*this); 649if(webviews.empty()) { 650// All documents have been closed safely, the window can be closed 651return false; 652} 653return true; 654}, false); 655} 656~PantheraWindow() override { 657moving_connection.disconnect(); 658moved_connection.disconnect(); 659} 660}; 661 662void PantheraWww::load_change_callback(WebKitWebView *object, WebKitLoadEvent load_event, gpointer data) { 663if(auto self = static_cast<PantheraWww*>(data)) { 664if(load_event == WEBKIT_LOAD_COMMITTED) { 665if(webkit_web_view_get_title(WEBKIT_WEB_VIEW(object)) == nullptr) { 666self->history_manager->log_url(webkit_web_view_get_uri(WEBKIT_WEB_VIEW(object)), ""); 667} else { 668self->history_manager->log_url(webkit_web_view_get_uri(WEBKIT_WEB_VIEW(object)), 669webkit_web_view_get_title(WEBKIT_WEB_VIEW(object))); 670} 671// TODO: reload visible history viewers 672} 673} 674} 675 676void PantheraWww::notify_callback(GObject *object, GParamSpec *pspec, gpointer data) { 677if(!gtk_widget_get_parent(GTK_WIDGET(object))) { 678return; 679} 680if(auto self = static_cast<PantheraWww*>(data)) { 681auto parent = gtk_widget_get_parent(gtk_widget_get_parent(GTK_WIDGET(object))); 682if(auto page = dynamic_cast<gPanthera::ContentPage*>(Glib::wrap(parent))) { 683if(g_strcmp0(pspec->name, "title") == 0) { 684if(auto label = dynamic_cast<Gtk::Label*>(page->tab_widget->get_last_child())) { 685if(strlen(webkit_web_view_get_title(WEBKIT_WEB_VIEW(object))) == 0) { 686label->set_label(_("Untitled")); 687} else { 688label->set_label(webkit_web_view_get_title(WEBKIT_WEB_VIEW(object))); 689} 690} 691} else if(g_strcmp0(pspec->name, "favicon") == 0) { 692// Update favicons 693if(auto image = dynamic_cast<Gtk::Image*>(page->tab_widget->get_first_child())) { 694image->set_from_icon_name("image-loading-symbolic"); 695if(auto favicon = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(object))) { 696gtk_image_set_from_paintable(image->gobj(), GDK_PAINTABLE(favicon)); 697} 698} 699} 700} 701} 702} 703 704void PantheraWww::notify_focused_callback(GObject *object, GParamSpec *pspec, gpointer data) { 705if(!gtk_widget_get_parent(GTK_WIDGET(object))) { 706return; 707} 708auto this_ = static_cast<PantheraWww*>(data); 709auto parent = gtk_widget_get_parent(gtk_widget_get_parent(GTK_WIDGET(object))); 710if(auto page = dynamic_cast<gPanthera::ContentPage*>(Glib::wrap(parent))) { 711if(g_strcmp0(pspec->name, "uri") == 0) { 712if(auto main_window = dynamic_cast<PantheraWindow*>(page->get_root())) { 713main_window->url_bar->set_text(webkit_web_view_get_uri(WEBKIT_WEB_VIEW(object))); 714} 715} else if(g_strcmp0(pspec->name, "title") == 0) { 716if(auto window = dynamic_cast<Gtk::Window*>(page->get_root())) { 717if(webkit_web_view_get_title(WEBKIT_WEB_VIEW(object))) { 718window->set_title(webkit_web_view_get_title(WEBKIT_WEB_VIEW(object))); 719} else { 720window->set_title(_("Untitled")); 721} 722} 723} 724} 725} 726 727void PantheraWww::on_back_pressed(GtkGestureClick *gesture, int n_press, double x, double y, gpointer user_data) { 728WebKitWebView *webview = WEBKIT_WEB_VIEW(user_data); 729webkit_web_view_go_back(webview); 730} 731 732void PantheraWww::on_forward_pressed(GtkGestureClick *gesture, int n_press, double x, double y, gpointer user_data) { 733WebKitWebView *webview = WEBKIT_WEB_VIEW(user_data); 734webkit_web_view_go_forward(webview); 735} 736 737gboolean PantheraWww::on_decide_policy(WebKitWebView *source, WebKitPolicyDecision *decision, WebKitPolicyDecisionType type, gpointer user_data) { 738if(auto self = static_cast<PantheraWww*>(user_data)) { 739if(type == WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION) { 740auto action = webkit_navigation_policy_decision_get_navigation_action(WEBKIT_NAVIGATION_POLICY_DECISION(decision)); 741Glib::ustring url = Glib::ustring(webkit_uri_request_get_uri(webkit_navigation_action_get_request(action))); 742if(!starts_with(url, "about:") && url.find("://") == Glib::ustring::npos) { 743// It is a special scheme (mailto:, tel: etc.) 744try { 745Gio::AppInfo::launch_default_for_uri(url); 746} catch(const Glib::Error &exception) { 747std::cerr << "Failed to launch special URI: " << exception.what() << std::endl; 748} 749webkit_policy_decision_ignore(decision); 750return true; 751} 752if(webkit_navigation_action_get_mouse_button(action) == 2) { 753// Middle-click opens in a new window 754self->on_new_tab(nullptr, url, false); 755webkit_policy_decision_ignore(decision); 756return true; 757} 758} else if(type == WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION) { 759auto action = webkit_navigation_policy_decision_get_navigation_action(WEBKIT_NAVIGATION_POLICY_DECISION(decision)); 760self->on_new_tab(nullptr, webkit_uri_request_get_uri(webkit_navigation_action_get_request(action)), false, true); 761return true; 762} 763} 764return false; 765} 766 767void PantheraWww::close_callback(WebKitWebView *source, gpointer user_data) { 768if(auto self = static_cast<PantheraWww*>(user_data)) { 769auto parent = gtk_widget_get_parent(gtk_widget_get_parent(GTK_WIDGET(source))); 770if(auto page = dynamic_cast<gPanthera::ContentPage*>(Glib::wrap(parent))) { 771if(page->get_next_sibling()) { 772page->content_manager->set_last_operated_page(static_cast<gPanthera::ContentPage*>(page->get_next_sibling())); 773page->get_stack()->set_visible_child(*page->get_next_sibling()); 774} else if(page->get_prev_sibling()) { 775page->content_manager->set_last_operated_page(static_cast<gPanthera::ContentPage*>(page->get_prev_sibling())); 776page->get_stack()->set_visible_child(*page->get_prev_sibling()); 777} 778webkit_web_view_terminate_web_process(source); 779if(page->content_manager->get_last_operated_page() == page) { 780page->content_manager->set_last_operated_page(nullptr); 781} 782page->redock(nullptr); 783} 784} 785} 786 787void PantheraWww::on_new_tab(gPanthera::ContentStack *stack, const Glib::ustring &url, bool focus, bool new_window) { 788if(!stack) { 789// Find the current area 790stack = controlled_stack; 791} 792Glib::ustring url_ = url; 793if(url.empty()) { 794url_ = new_tab_page; 795} 796 797WebKitWebView *webview = WEBKIT_WEB_VIEW(webkit_web_view_new()); 798gtk_widget_set_hexpand(GTK_WIDGET(webview), true); 799gtk_widget_set_vexpand(GTK_WIDGET(webview), true); 800auto page_content = Gtk::make_managed<Gtk::Box>(); 801gtk_box_append(page_content->gobj(), GTK_WIDGET(webview)); 802auto page_tab = new Gtk::Box(); 803page_tab->set_orientation(Gtk::Orientation::HORIZONTAL); 804auto initial_icon = Gtk::make_managed<Gtk::Image>(Gio::Icon::create("image-loading-symbolic")); 805page_tab->append(*initial_icon); 806page_tab->append(*Gtk::make_managed<Gtk::Label>(_("Untitled"))); 807page_tab->set_spacing(4); 808auto page = Gtk::make_managed<gPanthera::ContentPage>(content_manager, stack, page_content, page_tab); 809page->signal_close.connect([webview]() { 810webkit_web_view_try_close(WEBKIT_WEB_VIEW(webview)); 811return true; 812}); 813g_signal_connect(webview, "close", G_CALLBACK(close_callback), this); 814g_signal_connect(webview, "notify", G_CALLBACK(notify_callback), this); 815g_signal_connect(webview, "load-changed", G_CALLBACK(load_change_callback), this); 816g_signal_connect(webview, "decide-policy", G_CALLBACK(on_decide_policy), this); 817webkit_web_view_load_uri(webview, url_.data()); 818auto cookie_manager = webkit_network_session_get_cookie_manager(webkit_web_view_get_network_session(webview)); 819webkit_cookie_manager_set_persistent_storage(cookie_manager, cookie_file.c_str(), WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT); 820webkit_cookie_manager_set_accept_policy(cookie_manager, WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS); 821auto website_data_manager = webkit_network_session_get_website_data_manager(webkit_web_view_get_network_session(webview)); 822webkit_website_data_manager_set_favicons_enabled(website_data_manager, true); 823GtkEventController *click_controller_back = GTK_EVENT_CONTROLLER(gtk_gesture_click_new()); 824gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(click_controller_back), 8); 825g_signal_connect(click_controller_back, "pressed", G_CALLBACK(on_back_pressed), webview); 826gtk_widget_add_controller(GTK_WIDGET(webview), click_controller_back); 827 828GtkEventController *click_controller_forward = GTK_EVENT_CONTROLLER(gtk_gesture_click_new()); 829gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(click_controller_forward), 9); 830g_signal_connect(click_controller_forward, "pressed", G_CALLBACK(on_forward_pressed), webview); 831gtk_widget_add_controller(GTK_WIDGET(webview), click_controller_forward); 832 833stack->add_page(*page); 834if(focus) { 835stack->set_visible_child(*page); 836content_manager->set_last_operated_page(page); 837} 838if(new_window) { 839bool result = stack->signal_detach.emit(page); 840} 841} 842 843PantheraWindow *PantheraWww::make_window() { 844auto window = Gtk::make_managed<PantheraWindow>(this); 845return window; 846} 847 848void PantheraWww::on_startup() { 849bindtextdomain("panthera-www", "./locales"); 850textdomain("panthera-www"); 851content_manager = std::make_shared<gPanthera::ContentManager>(); 852Gtk::Application::on_startup(); 853// Get search engines 854std::ifstream search_engines_file_in("search_engines.json"); 855if(search_engines_file_in) { 856std::string search_engines_json((std::istreambuf_iterator<char>(search_engines_file_in)), std::istreambuf_iterator<char>()); 857search_engines_file_in.close(); 858set_search_engines_from_json(search_engines_json); 859} else { 860search_engines_file_in.close(); 861auto empty_json = nlohmann::json::array(); 862std::ofstream search_engines_file_out("search_engines.json"); 863search_engines_file_out << empty_json.dump(4); 864} 865 866// Set window titles on tab switch 867content_manager->signal_page_operated.connect([this](gPanthera::ContentPage *page) { 868if(!page) { 869return; 870} 871if(auto window = dynamic_cast<Gtk::Window*>(page->get_root())) { 872if(webkit_web_view_get_title(WEBKIT_WEB_VIEW(page->get_child()->get_first_child()->gobj()))) { 873window->set_title(webkit_web_view_get_title(WEBKIT_WEB_VIEW(page->get_child()->get_first_child()->gobj()))); 874} else { 875window->set_title(_("Untitled")); 876} 877} 878}); 879 880// Load settings 881new_tab_page = "about:blank"; 882std::ifstream settings_file_in("settings.json"); 883if(settings_file_in) { 884std::string settings_json((std::istreambuf_iterator<char>(settings_file_in)), std::istreambuf_iterator<char>()); 885settings_file_in.close(); 886auto json = nlohmann::json::parse(settings_json); 887if(json.contains("ntp")) { 888new_tab_page = json["ntp"].get<std::string>(); 889} 890} else { 891settings_file_in.close(); 892auto empty_json = nlohmann::json::object(); 893empty_json["ntp"] = "about:blank"; 894std::ofstream settings_file_out("settings.json"); 895settings_file_out << empty_json.dump(4); 896settings_file_out.close(); 897} 898 899// Load plugins 900std::ifstream plugin_file_in("plugins.json"); 901if(plugin_file_in) { 902std::string plugin_json((std::istreambuf_iterator<char>(plugin_file_in)), std::istreambuf_iterator<char>()); 903plugin_file_in.close(); 904auto json = nlohmann::json::parse(plugin_json); 905for(auto &plugin : json) { 906auto plugin_path = plugin["path"].get<std::string>(); 907//load_plugin(plugin_path); 908} 909} else { 910plugin_file_in.close(); 911auto empty_json = nlohmann::json::array(); 912std::ofstream plugin_file_out("plugins.json"); 913plugin_file_out << empty_json.dump(4); 914plugin_file_out.close(); 915} 916history_manager = std::make_shared<HistoryManager>("history.db"); 917 918// Known bug <https://gitlab.gnome.org/GNOME/epiphany/-/issues/2714>: 919// JS can't veto the application's accelerators using `preventDefault()`. This is 920// not possible in WebKitGTK, or at least I can't find a way to do it. 921 922main_menu = Gio::Menu::create(); 923// File 924auto file_menu = Gio::Menu::create(); 925// New tab 926auto new_tab_action = Gio::SimpleAction::create("new_tab"); 927new_tab_action->signal_activate().connect([this](const Glib::VariantBase&) { 928on_new_tab(nullptr); 929}); 930add_action(new_tab_action); 931set_accels_for_action("app.new_tab", {"<Primary>T"}); 932file_menu->append(_("New _Tab"), "app.new_tab"); 933// Close tab 934auto close_tab_action = Gio::SimpleAction::create("close_tab"); 935close_tab_action->signal_activate().connect([this](const Glib::VariantBase&) { 936auto page = content_manager->get_last_operated_page(); 937if(page) { 938page->close(); 939} 940}); 941add_action(close_tab_action); 942set_accels_for_action("app.close_tab", {"<Primary>W"}); 943file_menu->append(_("_Close Tab"), "app.close_tab"); 944// New window 945auto new_window_action = Gio::SimpleAction::create("new_window"); 946new_window_action->signal_activate().connect([this](const Glib::VariantBase&) { 947on_activate(); 948}); 949add_action(new_window_action); 950set_accels_for_action("app.new_window", {"<Primary>N"}); 951file_menu->append(_("_New Window"), "app.new_window"); 952// Quit 953auto quit_action = Gio::SimpleAction::create("quit"); 954quit_action->signal_activate().connect([this](const Glib::VariantBase&) { 955quit(); 956}); 957add_action(quit_action); 958set_accels_for_action("app.quit", {"<Primary>Q"}); 959file_menu->append(_("_Quit"), "app.quit"); 960 961main_menu->append_submenu(_("_File"), file_menu); 962 963// View 964auto view_menu = Gio::Menu::create(); 965// Reload 966set_accels_for_action("win.reload", {"F5", "<Primary>R"}); 967view_menu->append(_("_Reload"), "win.reload"); 968 969main_menu->append_submenu(_("_View"), view_menu); 970 971// Go 972auto go_menu = Gio::Menu::create(); 973// Back 974set_accels_for_action("win.go_back", {"<Alt>Left"}); 975go_menu->append(_("_Back"), "win.go_back"); 976// Forward 977set_accels_for_action("win.go_forward", {"<Alt>Right"}); 978go_menu->append(_("_Forward"), "win.go_forward"); 979 980main_menu->append_submenu(_("_Go"), go_menu); 981 982set_menubar(main_menu); 983} 984 985void PantheraWww::on_activate() { 986auto window = make_window(); 987window->present(); 988} 989 990void PantheraWww::on_shutdown() { 991Gtk::Application::on_shutdown(); 992} 993 994void PantheraWww::set_search_engines_from_json(const std::string &json_string) { 995auto json = nlohmann::json::parse(json_string); 996Glib::ustring default_search_engine_name; 997for(auto &engine : json) { 998SearchEngine search_engine; 999search_engine.name = engine["name"].get<std::string>(); 1000search_engine.url = engine["url"].get<std::string>(); 1001search_engines.push_back(search_engine); 1002if(engine.contains("default") && engine["default"].get<bool>()) { 1003default_search_engine_name = engine["name"].get<std::string>(); 1004} 1005} 1006for(auto &search_engine : search_engines) { 1007if(search_engine.name == default_search_engine_name) { 1008default_search_engine = &search_engine; 1009break; 1010} 1011} 1012} 1013 1014PantheraWww::PantheraWww() : Gtk::Application("com.roundabout_host.roundabout.PantheraWww", Gio::Application::Flags::NONE) { 1015} 1016 1017Glib::RefPtr<PantheraWww> PantheraWww::create() { 1018return Glib::make_refptr_for_instance<PantheraWww>(new PantheraWww()); 1019} 1020 1021void PantheraWww::load_plugin(const std::string &plugin_path) { 1022// TODO: needs to be reworked, to work with multi-window 1023void *handle = dlopen(plugin_path.c_str(), RTLD_LAZY | RTLD_GLOBAL); 1024if(!handle) { 1025std::cerr << "Failed to load plugin: " << dlerror() << std::endl; 1026return; 1027} 1028auto entrypoint = (plugin_entrypoint_type)dlsym(handle, "plugin_init"); 1029if(!entrypoint) { 1030std::cerr << "Failed to find entrypoint in plugin: " << dlerror() << std::endl; 1031dlclose(handle); 1032return; 1033} 1034entrypoint(); 1035} 1036 1037PantheraWww::~PantheraWww() = default; 1038 1039/* 1040extern "C" { 1041void panthera_log(const char *message) { 1042std::cerr << message << std::endl; 1043} 1044 1045void panthera_add_pane(const char *id, const char *label, GtkImage *icon, GtkWidget *pane) { 1046auto pane_child = Glib::wrap(pane); 1047auto icon_cc = Glib::wrap(icon); 1048auto label_cc = Glib::ustring(label); 1049auto id_cc = Glib::ustring(id); 1050//auto new_pane = Gtk::make_managed<gPanthera::DockablePane>(app->get_layout_manager(), *pane_child, id_cc, label_cc, icon_cc); 1051// TODO: Port to multi-window 1052//app->get_layout_manager()->add_pane(new_pane); 1053} 1054} 1055*/ 1056 1057int main(int argc, char *argv[]) { 1058gPanthera::init(); 1059 1060auto app = PantheraWww::create(); 1061 1062return app->run(argc, argv); 1063} 1064