From f9d9b13c61605b4950fedea57c37b793c2616e71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danielle=20For=C3=A9?= Date: Mon, 1 Jan 2024 15:10:38 -0800 Subject: [PATCH 1/3] Plug: inline namespace, cleanup, build UI earlier --- src/Plug.vala | 248 +++++++++++++++-------------------- src/Views/FirewallPanel.vala | 4 +- 2 files changed, 107 insertions(+), 145 deletions(-) diff --git a/src/Plug.vala b/src/Plug.vala index 015b8e65..fe7e5307 100644 --- a/src/Plug.vala +++ b/src/Plug.vala @@ -1,92 +1,79 @@ -// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*- -/*- - * Copyright (c) 2014-2018 elementary LLC. (https://elementary.io) - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301 USA - * +/* + * SPDX-License-Identifier: LGPL-3.0-or-later + * SPDX-FileCopyrightText: 2014-2023 elementary, Inc. (https://elementary.io) * Authored by: Corentin Noël */ -namespace SecurityPrivacy { +public class SecurityPrivacy.Plug : Switchboard.Plug { public static Gtk.LockButton lock_button; - public static LocationPanel location; - public static FirewallPanel firewall; - public static HouseKeepingPanel housekeeping; - public static TrackPanel tracking; - - public class Plug : Switchboard.Plug { - Gtk.Grid main_grid; - Gtk.Stack stack; - - private const string FIREWALL = "firewall"; - private const string HOUSEKEEPING = "housekeeping"; - private const string HISTORY = "tracking"; - private const string LOCKING = "locking"; - private const string LOCATION = "location"; - - public Plug () { - GLib.Intl.bindtextdomain (Build.GETTEXT_PACKAGE, Build.LOCALEDIR); - GLib.Intl.bind_textdomain_codeset (Build.GETTEXT_PACKAGE, "UTF-8"); - - Object (category: Category.PERSONAL, - code_name: "io.elementary.switchboard.security-privacy", - display_name: _("Security & Privacy"), - description: _("Configure firewall, screen lock, and activity information"), - icon: "preferences-system-privacy", - supported_settings: new Gee.TreeMap (null, null)); - - supported_settings.set ("privacy", HISTORY); - supported_settings.set ("privacy/location", LOCATION); - supported_settings.set ("privacy/trash", HOUSEKEEPING); - supported_settings.set ("security/firewall", FIREWALL); - supported_settings.set ("security/locking", LOCKING); - supported_settings.set ("security", null); - - // DEPRECATED - supported_settings.set ("security/housekeeping", HOUSEKEEPING); - supported_settings.set ("security/privacy", HISTORY); - supported_settings.set ("security/privacy/location", LOCATION); - supported_settings.set ("security/screensaver", LOCKING); - } - - public override Gtk.Widget get_widget () { - if (main_grid == null) { - main_grid = new Gtk.Grid (); - } - return main_grid; - } - - public override void shown () { - if (main_grid.get_children ().length () > 0) { - return; - } - - stack = new Gtk.Stack (); + private Gtk.Paned paned; + private Gtk.Stack stack; + + private const string FIREWALL = "firewall"; + private const string HOUSEKEEPING = "housekeeping"; + private const string HISTORY = "tracking"; + private const string LOCKING = "locking"; + private const string LOCATION = "location"; + + public Plug () { + GLib.Intl.bindtextdomain (Build.GETTEXT_PACKAGE, Build.LOCALEDIR); + GLib.Intl.bind_textdomain_codeset (Build.GETTEXT_PACKAGE, "UTF-8"); + + Object (category: Category.PERSONAL, + code_name: "io.elementary.switchboard.security-privacy", + display_name: _("Security & Privacy"), + description: _("Configure firewall, screen lock, and activity information"), + icon: "preferences-system-privacy", + supported_settings: new Gee.TreeMap (null, null)); + + supported_settings.set ("privacy", HISTORY); + supported_settings.set ("privacy/location", LOCATION); + supported_settings.set ("privacy/trash", HOUSEKEEPING); + supported_settings.set ("security/firewall", FIREWALL); + supported_settings.set ("security/locking", LOCKING); + supported_settings.set ("security", null); + + // DEPRECATED + supported_settings.set ("security/housekeeping", HOUSEKEEPING); + supported_settings.set ("security/privacy", HISTORY); + supported_settings.set ("security/privacy/location", LOCATION); + supported_settings.set ("security/screensaver", LOCKING); + } + public override Gtk.Widget get_widget () { + if (paned == null) { var label = new Gtk.Label (_("Some settings require administrator rights to be changed")); var infobar = new Gtk.InfoBar () { - message_type = Gtk.MessageType.INFO + message_type = INFO, + revealed = false }; infobar.get_content_area ().add (label); - var grid = new Gtk.Grid (); - grid.attach (infobar, 0, 0); - grid.attach (stack, 0, 1); + var tracking = new TrackPanel (); + var locking = new LockPanel (); + var firewall = new FirewallPanel (); + var housekeeping = new HouseKeepingPanel (); + var location = new LocationPanel (); + + stack = new Gtk.Stack (); + stack.add_titled (tracking, HISTORY, _("Privacy")); + stack.add_titled (locking, LOCKING, _("Locking")); + stack.add_titled (firewall, FIREWALL, _("Firewall")); + stack.add_titled (housekeeping, HOUSEKEEPING, _("Housekeeping")); + stack.add_titled (location, LOCATION, _("Location Services")); + + var box = new Gtk.Box (VERTICAL, 0); + box.add (infobar); + box.add (stack); + + var settings_sidebar = new Granite.SettingsSidebar (stack); + + paned = new Gtk.Paned (HORIZONTAL); + paned.add1 (settings_sidebar); + paned.add2 (box); + paned.show_all (); try { var permission = new Polkit.Permission.sync ( @@ -96,11 +83,10 @@ namespace SecurityPrivacy { lock_button = new Gtk.LockButton (permission); - infobar.revealed = false; infobar.get_action_area ().add (lock_button); stack.notify["visible-child-name"].connect (() => { - if (permission.allowed == false && stack.visible_child_name == "firewall") { + if (permission.allowed == false && stack.visible_child_name == FIREWALL) { infobar.revealed = true; } else { infobar.revealed = false; @@ -108,7 +94,7 @@ namespace SecurityPrivacy { }); permission.notify["allowed"].connect (() => { - if (permission.allowed == false && stack.visible_child_name == "firewall") { + if (permission.allowed == false && stack.visible_child_name == FIREWALL) { infobar.revealed = true; } else { infobar.revealed = false; @@ -117,75 +103,51 @@ namespace SecurityPrivacy { } catch (Error e) { critical (e.message); } - - tracking = new TrackPanel (); - var locking = new LockPanel (); - firewall = new FirewallPanel (); - housekeeping = new HouseKeepingPanel (); - location = new LocationPanel (); - - stack.add_titled (tracking, HISTORY, _("Privacy")); - stack.add_titled (locking, LOCKING, _("Locking")); - stack.add_titled (firewall, FIREWALL, _("Firewall")); - stack.add_titled (housekeeping, HOUSEKEEPING, _("Housekeeping")); - stack.add_titled (location, LOCATION, _("Location Services")); - - var settings_sidebar = new Granite.SettingsSidebar (stack); - - var paned = new Gtk.Paned (Gtk.Orientation.HORIZONTAL); - paned.position = 200; - paned.add1 (settings_sidebar); - paned.add2 (grid); - - main_grid.add (paned); - main_grid.show_all (); } - public override void hidden () { + return paned; + } - } + public override void shown () { } - public override void search_callback (string location) { - if (main_grid.get_children ().length () == 0) { - shown (); - } + public override void hidden () { } - stack.set_visible_child_name (location); - } + public override void search_callback (string location) { + stack.set_visible_child_name (location); + } - // 'search' returns results like ("Keyboard → Behavior → Duration", "keyboardbehavior") - public override async Gee.TreeMap search (string search) { - var map = new Gee.TreeMap (null, null); - map.set ("%s → %s".printf (display_name, _("History")), HISTORY); - map.set ("%s → %s → %s".printf (display_name, _("History"), _("Clear History")), HISTORY); - map.set ("%s → %s".printf (display_name, _("Locking")), LOCKING); - map.set ("%s → %s → %s".printf (display_name, _("Locking"), _("Lock on sleep")), HISTORY); - map.set ("%s → %s → %s".printf (display_name, _("Locking"), _("Lock after sceen turns off")), HISTORY); - map.set ("%s → %s".printf (display_name, _("Firewall")), FIREWALL); - map.set ("%s → %s".printf (display_name, _("Housekeeping")), HOUSEKEEPING); - map.set ("%s → %s → %s".printf ( - display_name, - _("Housekeeping"), - _("Automatically delete old temporary files") - ), HOUSEKEEPING); - map.set ("%s → %s → %s".printf ( - display_name, - _("Housekeeping"), - _("Automatically delete old screenshot files") - ), HOUSEKEEPING); - map.set ("%s → %s → %s".printf ( - display_name, - _("Housekeeping"), - _("Automatically delete old trashed files") - ), HOUSEKEEPING); - map.set ("%s → %s → %s".printf ( - display_name, - _("Housekeeping"), - _("Number of days to keep trashed and temporary files") - ), HOUSEKEEPING); - map.set ("%s → %s".printf (display_name, _("Location Services")), LOCATION); - return map; - } + // 'search' returns results like ("Keyboard → Behavior → Duration", "keyboardbehavior") + public override async Gee.TreeMap search (string search) { + var map = new Gee.TreeMap (null, null); + map.set ("%s → %s".printf (display_name, _("History")), HISTORY); + map.set ("%s → %s → %s".printf (display_name, _("History"), _("Clear History")), HISTORY); + map.set ("%s → %s".printf (display_name, _("Locking")), LOCKING); + map.set ("%s → %s → %s".printf (display_name, _("Locking"), _("Lock on sleep")), HISTORY); + map.set ("%s → %s → %s".printf (display_name, _("Locking"), _("Lock after sceen turns off")), HISTORY); + map.set ("%s → %s".printf (display_name, _("Firewall")), FIREWALL); + map.set ("%s → %s".printf (display_name, _("Housekeeping")), HOUSEKEEPING); + map.set ("%s → %s → %s".printf ( + display_name, + _("Housekeeping"), + _("Automatically delete old temporary files") + ), HOUSEKEEPING); + map.set ("%s → %s → %s".printf ( + display_name, + _("Housekeeping"), + _("Automatically delete old screenshot files") + ), HOUSEKEEPING); + map.set ("%s → %s → %s".printf ( + display_name, + _("Housekeeping"), + _("Automatically delete old trashed files") + ), HOUSEKEEPING); + map.set ("%s → %s → %s".printf ( + display_name, + _("Housekeeping"), + _("Number of days to keep trashed and temporary files") + ), HOUSEKEEPING); + map.set ("%s → %s".printf (display_name, _("Location Services")), LOCATION); + return map; } } diff --git a/src/Views/FirewallPanel.vala b/src/Views/FirewallPanel.vala index a1be668f..5da802b8 100644 --- a/src/Views/FirewallPanel.vala +++ b/src/Views/FirewallPanel.vala @@ -64,9 +64,9 @@ public class SecurityPrivacy.FirewallPanel : Granite.SimpleSettingsPage { sensitive = false; - lock_button.get_permission ().notify["allowed"].connect (() => { + Plug.lock_button.get_permission ().notify["allowed"].connect (() => { loading = true; - sensitive = lock_button.get_permission ().allowed; + sensitive = Plug.lock_button.get_permission ().allowed; status_switch.active = UFWHelpers.get_status (); list_store.clear (); remove_button.sensitive = false; From 60efbf45ef5b1d973b15d9a1ce7fabdae31c0757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danielle=20For=C3=A9?= Date: Wed, 3 Jan 2024 13:01:35 -0800 Subject: [PATCH 2/3] Pass a ref instead of public static --- src/Plug.vala | 50 +++++++++++++++--------------------- src/Views/FirewallPanel.vala | 17 +++++++----- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/Plug.vala b/src/Plug.vala index fe7e5307..5560c66b 100644 --- a/src/Plug.vala +++ b/src/Plug.vala @@ -5,8 +5,7 @@ */ public class SecurityPrivacy.Plug : Switchboard.Plug { - public static Gtk.LockButton lock_button; - + private Polkit.Permission permission; private Gtk.Paned paned; private Gtk.Stack stack; @@ -43,17 +42,29 @@ public class SecurityPrivacy.Plug : Switchboard.Plug { public override Gtk.Widget get_widget () { if (paned == null) { + try { + permission = new Polkit.Permission.sync ( + "io.elementary.switchboard.security-privacy", + new Polkit.UnixProcess (Posix.getpid ()) + ); + } catch (Error e) { + critical (e.message); + } + var label = new Gtk.Label (_("Some settings require administrator rights to be changed")); + var lock_button = new Gtk.LockButton (permission); + var infobar = new Gtk.InfoBar () { message_type = INFO, revealed = false }; infobar.get_content_area ().add (label); + infobar.get_action_area ().add (lock_button); var tracking = new TrackPanel (); var locking = new LockPanel (); - var firewall = new FirewallPanel (); + var firewall = new FirewallPanel (permission); var housekeeping = new HouseKeepingPanel (); var location = new LocationPanel (); @@ -75,34 +86,13 @@ public class SecurityPrivacy.Plug : Switchboard.Plug { paned.add2 (box); paned.show_all (); - try { - var permission = new Polkit.Permission.sync ( - "io.elementary.switchboard.security-privacy", - new Polkit.UnixProcess (Posix.getpid ()) - ); + stack.notify["visible-child"].connect (() => { + infobar.revealed = !permission.allowed && stack.visible_child == firewall; + }); - lock_button = new Gtk.LockButton (permission); - - infobar.get_action_area ().add (lock_button); - - stack.notify["visible-child-name"].connect (() => { - if (permission.allowed == false && stack.visible_child_name == FIREWALL) { - infobar.revealed = true; - } else { - infobar.revealed = false; - } - }); - - permission.notify["allowed"].connect (() => { - if (permission.allowed == false && stack.visible_child_name == FIREWALL) { - infobar.revealed = true; - } else { - infobar.revealed = false; - } - }); - } catch (Error e) { - critical (e.message); - } + permission.notify["allowed"].connect (() => { + infobar.revealed = !permission.allowed && stack.visible_child == firewall; + }); } return paned; diff --git a/src/Views/FirewallPanel.vala b/src/Views/FirewallPanel.vala index 5da802b8..9552e57e 100644 --- a/src/Views/FirewallPanel.vala +++ b/src/Views/FirewallPanel.vala @@ -21,6 +21,8 @@ */ public class SecurityPrivacy.FirewallPanel : Granite.SimpleSettingsPage { + public Polkit.Permission permission { get; construct; } + private Gtk.ListStore list_store; private Gtk.TreeView view; private Gtk.ActionBar actionbar; @@ -42,10 +44,13 @@ public class SecurityPrivacy.FirewallPanel : Granite.SimpleSettingsPage { N_COLUMNS } - public FirewallPanel () { - Object (activatable: true, - icon_name: "network-firewall", - title: _("Firewall")); + public FirewallPanel (Polkit.Permission permission) { + Object ( + activatable: true, + icon_name: "network-firewall", + title: _("Firewall"), + permission: permission + ); } construct { @@ -64,9 +69,9 @@ public class SecurityPrivacy.FirewallPanel : Granite.SimpleSettingsPage { sensitive = false; - Plug.lock_button.get_permission ().notify["allowed"].connect (() => { + permission.notify["allowed"].connect (() => { loading = true; - sensitive = Plug.lock_button.get_permission ().allowed; + sensitive = permission.allowed; status_switch.active = UFWHelpers.get_status (); list_store.clear (); remove_button.sensitive = false; From c4dc1893aab0afc87c6f0f3c028ed1cdbdbe1375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danielle=20For=C3=A9?= Date: Wed, 3 Jan 2024 13:51:47 -0800 Subject: [PATCH 3/3] Update Plug.vala --- src/Plug.vala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Plug.vala b/src/Plug.vala index 5560c66b..2ade9a99 100644 --- a/src/Plug.vala +++ b/src/Plug.vala @@ -87,11 +87,11 @@ public class SecurityPrivacy.Plug : Switchboard.Plug { paned.show_all (); stack.notify["visible-child"].connect (() => { - infobar.revealed = !permission.allowed && stack.visible_child == firewall; + infobar.revealed = stack.visible_child == firewall && !permission.allowed; }); permission.notify["allowed"].connect (() => { - infobar.revealed = !permission.allowed && stack.visible_child == firewall; + infobar.revealed = stack.visible_child == firewall && !permission.allowed; }); }