Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to HdyTabBar #91

Merged
merged 7 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 48 additions & 49 deletions src/CaptiveLogin.vala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2017 elementary LLC. (http://launchpad.net/capnet-assist)
* Copyright 2015-2022 elementary, Inc. (https://elementary.io)
danirabbit marked this conversation as resolved.
Show resolved Hide resolved
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
Expand All @@ -23,8 +23,7 @@ public class CaptiveLogin : Hdy.ApplicationWindow {

private CertButton cert_button;
private Gtk.Label title_label;

private Granite.Widgets.DynamicNotebook notebook;
private Hdy.TabView tabview;

// When a download is passed to the browser, it triggers the load failed signal
private bool download_requested = false;
Expand Down Expand Up @@ -68,80 +67,77 @@ public class CaptiveLogin : Hdy.ApplicationWindow {
};
header.get_style_context ().add_class ("default-decoration");

notebook = new Granite.Widgets.DynamicNotebook () {
add_button_visible = false,
allow_drag = false,
allow_new_window = false,
allow_restoring = false,
expand = true,
tab_bar_behavior = Granite.Widgets.DynamicNotebook.TabBarBehavior.SINGLE
tabview = new Hdy.TabView () {
expand = true
leolost2605 marked this conversation as resolved.
Show resolved Hide resolved
};

var tabbar = new Hdy.TabBar () {
danirabbit marked this conversation as resolved.
Show resolved Hide resolved
expand_tabs = false,
inverted = true,
view = tabview
};

var grid = new Gtk.Grid ();
grid.attach (header, 0, 0);
grid.attach (notebook, 0, 1);
var box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
box.add (header);
box.add (tabbar);
box.add (tabview);

add (grid);
add (box);

set_keep_above (true);
skip_taskbar_hint = true;
stick ();

connect_signals ();
}

bool is_privacy_mode_enabled () {
var privacy_settings = new GLib.Settings ("org.gnome.desktop.privacy");
return !privacy_settings.get_boolean ("remember-recent-files") ||
!privacy_settings.get_boolean ("remember-app-usage");
}

private void connect_signals () {
this.destroy.connect (application.quit);

notebook.tab_switched.connect ((old_tab, new_tab) => {
var captive_view = (TabbedWebView) new_tab;
title_label.label = captive_view.label;

cert_button.security = captive_view.security;
tabview.notify["selected-page"].connect (() => {
var webview = (TabbedWebView) tabview.get_selected_page ().child;
title_label.label = webview.title;
cert_button.security = webview.security;
});

notebook.close_tab_requested.connect ((tab) => {
if (notebook.n_tabs == 1) {
tabview.close_page.connect ((page) => {
tabview.close_page_finish (page, true);

if (tabview.n_pages == 0) {
application.quit ();
} else if (notebook.n_tabs == 2) {
notebook.show_tabs = false;
}

return true;
return Gdk.EVENT_STOP;
});
}

bool is_privacy_mode_enabled () {
var privacy_settings = new GLib.Settings ("org.gnome.desktop.privacy");
return !privacy_settings.get_boolean ("remember-recent-files") ||
!privacy_settings.get_boolean ("remember-app-usage");
}

private TabbedWebView create_tab (string uri) {
var tab = new TabbedWebView (uri, !is_privacy_mode_enabled ());
var webview = new TabbedWebView (!is_privacy_mode_enabled ());
var tabpage = tabview.append (webview);

notebook.insert_tab (tab, notebook.n_tabs);
notebook.show_tabs = notebook.n_tabs > 1;
webview.bind_property ("title", tabpage, "title", SYNC_CREATE);

tab.notify["label"].connect ((view, param_spec) => {
if (tab == this.notebook.current) {
title_label.set_text (tab.label);
webview.notify["title"].connect ((view, param_spec) => {
if (tabpage == tabview.get_selected_page ()) {
title_label.set_text (webview.title);
}
});

tab.notify["security"].connect ((view, param_spec) => {
if (tab == this.notebook.current) {
cert_button.security = tab.security;
webview.notify["security"].connect ((view, param_spec) => {
if (tabpage == tabview.get_selected_page ()) {
cert_button.security = webview.security;
}
});

tab.web_view.create.connect ((navigation_action)=> {
webview.create.connect ((navigation_action)=> {
create_tab (navigation_action.get_request ().get_uri ());

return null;
});

tab.web_view.decide_policy.connect ((decision, type) => {
webview.decide_policy.connect ((decision, type) => {
switch (type) {
case WebKit.PolicyDecisionType.NEW_WINDOW_ACTION:
if (decision is WebKit.ResponsePolicyDecision) {
Expand Down Expand Up @@ -169,25 +165,28 @@ public class CaptiveLogin : Hdy.ApplicationWindow {
return true;
});

return tab;
webview.load_uri (uri);
show_all ();

return webview;
}

public bool get_tls_info (out TlsCertificate certificate, out TlsCertificateFlags errors) {
var web_view = ((TabbedWebView) notebook.current).web_view;
var web_view = (TabbedWebView) tabview.get_selected_page ().child;

return web_view.get_tls_info (out certificate, out errors);
}

public string get_uri () {
var web_view = ((TabbedWebView) notebook.current).web_view;
var web_view = (TabbedWebView) tabview.get_selected_page ().child;

return web_view.get_uri ();
}

public void start (string? browser_url) {
var default_tab = create_tab (browser_url ?? DUMMY_URL);

default_tab.web_view.load_failed.connect ((event, uri, error) => {
default_tab.load_failed.connect ((event, uri, error) => {
// The user has canceled the page loading eg. by clicking on a link.
if ((Error) error is WebKit.NetworkError.CANCELLED) {
return true;
Expand Down
61 changes: 26 additions & 35 deletions src/TabbedWebView.vala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2017 elementary LLC (http://launchpad.net/capnet-assist)
* Copyright 2016-2022 elementary, Inc. (https://elementary.io)
danirabbit marked this conversation as resolved.
Show resolved Hide resolved
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
Expand All @@ -18,26 +18,38 @@
*
*/

public class TabbedWebView : Granite.Widgets.Tab {
public WebKit.WebView web_view;
public class TabbedWebView : WebKit.WebView {
public bool load_cookies { get; construct; }
public CertButton.Security security { get; private set; }

public TabbedWebView (string uri, bool load_cookies) {
web_view = new WebKit.WebView ();
public TabbedWebView (bool load_cookies) {
Object (load_cookies: load_cookies);
}

page = web_view;
construct {
if (load_cookies) {
var cookies_db_path = Path.build_path (
Path.DIR_SEPARATOR_S,
Environment.get_user_config_dir (),
"epiphany",
"cookies.sqlite"
);

setup_web_view (load_cookies);
if (FileUtils.test (cookies_db_path, FileTest.IS_REGULAR)) {
var cookie_manager = get_context ().get_cookie_manager ();

web_view.insecure_content_detected.connect (() => {
security = CertButton.Security.MIXED_CONTENT;
});
cookie_manager.set_accept_policy (WebKit.CookieAcceptPolicy.ALWAYS);
cookie_manager.set_persistent_storage (cookies_db_path, WebKit.CookiePersistentStorage.SQLITE);
} else {
critical ("No cookies store found, not saving the cookies…");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this being a critical now intentional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I don't think this does anything anymore since we started to ship Epiphany in Flatpak, so I think it's good for it to make noise so we don't forget that it's broken 😅 I'm not sure if the ultimate solution is to remove this or what but I can revert that if you'd like :)

}
}

web_view.notify["title"].connect ((view, param_spec) => {
label = web_view.get_title ();
insecure_content_detected.connect (() => {
security = CertButton.Security.MIXED_CONTENT;
});

web_view.load_changed.connect ((view, event) => {
load_changed.connect ((view, event) => {
switch (event) {
case WebKit.LoadEvent.STARTED:
security = CertButton.Security.LOADING;
Expand All @@ -47,16 +59,14 @@ public class TabbedWebView : Granite.Widgets.Tab {
break;
}
});

web_view.load_uri (uri);
}

private void update_tls_info () {
TlsCertificate cert;
TlsCertificateFlags cert_flags;
bool is_secure;

if (!web_view.get_tls_info (out cert, out cert_flags)) {
if (!get_tls_info (out cert, out cert_flags)) {
// The page is served over HTTP
is_secure = false;
} else {
Expand All @@ -71,23 +81,4 @@ public class TabbedWebView : Granite.Widgets.Tab {
security = CertButton.Security.NONE;
}
}

private void setup_web_view (bool load_cookies) {
if (load_cookies) {
var cookies_db_path = Path.build_path (Path.DIR_SEPARATOR_S,
Environment.get_user_config_dir (),
"epiphany",
"cookies.sqlite");

if (!FileUtils.test (cookies_db_path, FileTest.IS_REGULAR)) {
debug ("No cookies store found, not saving the cookies…");
return;
}

var cookie_manager = web_view.get_context ().get_cookie_manager ();

cookie_manager.set_accept_policy (WebKit.CookieAcceptPolicy.ALWAYS);
cookie_manager.set_persistent_storage (cookies_db_path, WebKit.CookiePersistentStorage.SQLITE);
}
}
}