GTK docking interfaces and more

Important information: Google announced that, from September 2026, Android devices will require ALL apps to be signed by Google, effectively leading to an iOS situation. Value your right to a computer that does what you want; do not tolerate this monopolistic practice! Contact me if you don't understand why it is bad. Click to learn more.

 panthera-www.cc

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