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

AppSettingsView: DRY row creation #218

Merged
merged 6 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
164 changes: 57 additions & 107 deletions src/Permissions/Widgets/AppSettingsView.vala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 elementary, Inc. (https://elementary.io)
* Copyright 2020-2024 elementary, Inc. (https://elementary.io)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
Expand Down Expand Up @@ -28,93 +28,20 @@ public class Permissions.Widgets.AppSettingsView : Switchboard.SettingsPage {
construct {
notify["selected-app"].connect (update_view);

var homefolder_widget = new PermissionSettingsWidget (
Plug.permission_names["filesystems=home"],
_("Access your entire Home folder, including any hidden folders."),
"user-home",
new Backend.PermissionSettings ("filesystems=home")
);

var sysfolders_widget = new PermissionSettingsWidget (
Plug.permission_names["filesystems=host"],
_("Access system folders, not including the operating system or system internals. This includes users' Home folders."),
"drive-harddisk",
new Backend.PermissionSettings ("filesystems=host")
);

var devices_widget = new PermissionSettingsWidget (
Plug.permission_names["devices=all"],
_("Access all devices, such as webcams, microphones, and connected USB devices."),
"camera-web",
new Backend.PermissionSettings ("devices=all")
);

var network_widget = new PermissionSettingsWidget (
Plug.permission_names["shared=network"],
_("Access the Internet and local networks."),
"preferences-system-network",
new Backend.PermissionSettings ("shared=network")
);

var bluetooth_widget = new PermissionSettingsWidget (
Plug.permission_names["features=bluetooth"],
_("Manage Bluetooth devices including pairing, unpairing, and discovery."),
"bluetooth",
new Backend.PermissionSettings ("features=bluetooth")
);

var printing_widget = new PermissionSettingsWidget (
Plug.permission_names["sockets=cups"],
_("Access printers."),
"printer",
new Backend.PermissionSettings ("sockets=cups")
);

var ssh_widget = new PermissionSettingsWidget (
Plug.permission_names["sockets=ssh-auth"],
_("Access other devices on the network via SSH."),
"utilities-terminal",
new Backend.PermissionSettings ("sockets=ssh-auth")
);

var gpu_widget = new PermissionSettingsWidget (
Plug.permission_names["devices=dri"],
_("Accelerate graphical output."),
"application-x-firmware",
new Backend.PermissionSettings ("devices=dri")
);

list_box = new Gtk.ListBox () {
hexpand = true,
vexpand = true,
valign = START,
selection_mode = NONE
};
list_box.add_css_class ("boxed-list");
list_box.add_css_class (Granite.STYLE_CLASS_RICH_LIST);
list_box.append (homefolder_widget);
list_box.append (sysfolders_widget);
list_box.append (devices_widget);
list_box.append (network_widget);
list_box.append (bluetooth_widget);
list_box.append (printing_widget);
list_box.append (ssh_widget);
list_box.append (gpu_widget);

child = list_box;

reset_button = add_button (_("Reset to Defaults"));

update_view ();

homefolder_widget.changed_permission_settings.connect (change_permission_settings);
sysfolders_widget.changed_permission_settings.connect (change_permission_settings);
devices_widget.changed_permission_settings.connect (change_permission_settings);
network_widget.changed_permission_settings.connect (change_permission_settings);
bluetooth_widget.changed_permission_settings.connect (change_permission_settings);
printing_widget.changed_permission_settings.connect (change_permission_settings);
ssh_widget.changed_permission_settings.connect (change_permission_settings);
gpu_widget.changed_permission_settings.connect (change_permission_settings);

reset_button.clicked.connect (() => {
if (selected_app != null) {
selected_app.reset_settings_to_standard ();
Expand All @@ -123,51 +50,74 @@ public class Permissions.Widgets.AppSettingsView : Switchboard.SettingsPage {
});
}

private void initialize_settings_view () {
var children = list_box.observe_children ();
for (var iter = 0; iter < children.get_n_items (); iter++) {
if (children.get_item (iter) is PermissionSettingsWidget) {
var widget = (PermissionSettingsWidget) children.get_item (iter);
widget.do_notify = false;
widget.settings.standard = false;
widget.settings.enabled = false;
widget.do_notify = true;
}
}
}

private void update_view () {
initialize_settings_view ();
list_box.remove_all ();

if (selected_app == null) {
list_box.sensitive = false;
reset_button.sensitive = false;
return;
}

var should_enable_reset = false;
selected_app.settings.foreach ((settings) => {
var children = list_box.observe_children ();
for (var iter = 0; iter < children.get_n_items (); iter++) {
if (children.get_item (iter) is PermissionSettingsWidget) {
var widget = (PermissionSettingsWidget) children.get_item (iter);
if (widget.settings.context == settings.context) {
widget.do_notify = false;
widget.settings.standard = settings.standard;
widget.settings.enabled = settings.enabled;
widget.do_notify = true;

if (settings.enabled != settings.standard) {
should_enable_reset = true;
}
}
}
string description = "Unknown";
string icon_name = "image-missing";

switch (settings.context) {
case "filesystems=home":
description = _("Access your entire Home folder, including any hidden folders.");
icon_name = "user-home";
break;
case "filesystems=host":
description = _("Access system folders, not including the operating system or system internals. This includes users' Home folders.");
icon_name = "drive-harddisk";
break;
case "devices=all":
description = _("Access all devices, such as webcams, microphones, and connected USB devices.");
icon_name = "camera-web";
break;
case "shared=network":
description = _("Access the Internet and local networks.");
icon_name = "preferences-system-network";
break;
case "features=bluetooth":
description = _("Manage Bluetooth devices including pairing, unpairing, and discovery.");
icon_name = "bluetooth";
break;
case "sockets=cups":
description = _("Access printers.");
icon_name = "printer";
break;
case "sockets=ssh-auth":
description = _("Access other devices on the network via SSH.");
icon_name = "utilities-terminal";
break;
case "devices=dri":
description = _("Accelerate graphical output.");
icon_name = "application-x-firmware";
break;
}

list_box.sensitive = true;
reset_button.sensitive = should_enable_reset;
var override_row = new PermissionSettingsWidget (
Plug.permission_names[settings.context],
description,
icon_name
);

settings.bind_property ("enabled", override_row, "active", SYNC_CREATE | BIDIRECTIONAL);
settings.notify["enabled"].connect (() => {
change_permission_settings (settings);
});

if (settings.enabled != settings.standard) {
should_enable_reset = true;
}

list_box.append (override_row);
});

reset_button.sensitive = should_enable_reset;

update_property (Gtk.AccessibleProperty.LABEL, _("%s permissions").printf (selected_app.name), -1);
title = selected_app.name;
icon = selected_app.icon;
Expand Down
22 changes: 6 additions & 16 deletions src/Permissions/Widgets/PermissionSettingsWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,23 @@
*/

public class Permissions.Widgets.PermissionSettingsWidget : Gtk.ListBoxRow {
public signal void changed_permission_settings (Backend.PermissionSettings settings);

public string description { get; construct set; }
public string icon_name { get; construct set; }
public string primary_text { get; construct set; }
public Backend.PermissionSettings settings { get; construct set; }

public bool do_notify { get; set; default = true; }
public bool active { get; set; }

public PermissionSettingsWidget (string primary_text, string description, string icon_name, Backend.PermissionSettings settings) {
public PermissionSettingsWidget (string primary_text, string description, string icon_name) {
GLib.Object (
description: description,
icon_name: icon_name,
primary_text: primary_text,
settings: settings
primary_text: primary_text
);
}

construct {
var icon = new Gtk.Image.from_icon_name (icon_name) {
icon_size = LARGE,
tooltip_text = settings.context
};

var name_label = new Gtk.Label (primary_text) {
Expand All @@ -57,6 +52,7 @@ public class Permissions.Widgets.PermissionSettingsWidget : Gtk.ListBoxRow {
description_label.add_css_class (Granite.STYLE_CLASS_SMALL_LABEL);

var allow_switch = new Gtk.Switch () {
focusable = false,
valign = Gtk.Align.CENTER
};

Expand All @@ -70,16 +66,10 @@ public class Permissions.Widgets.PermissionSettingsWidget : Gtk.ListBoxRow {

child = grid;

bind_property ("active", allow_switch, "active", BIDIRECTIONAL);

activate.connect (() => {
allow_switch.activate ();
});

settings.bind_property ("enabled", allow_switch, "active", BindingFlags.BIDIRECTIONAL);

settings.notify["enabled"].connect (() => {
if (do_notify) {
changed_permission_settings (settings);
}
});
}
}
Loading