Skip to content

Commit

Permalink
Support Flatpak < 1.15.6 for permissions (#992)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp authored Dec 11, 2024
1 parent 5506bf8 commit aa93296
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/Extensions/Extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import Interface from "./Extensions.blp" with { type: "uri" };
import illustration from "./extensions.svg";

import "./Extension.js";
import { getFlatpakInfo, settings } from "../util.js";
import { settings } from "../util.js";
import { getFlatpakInfo } from "../flatpak.js";

export const action_extensions = new Gio.SimpleAction({
name: "extensions",
Expand Down
6 changes: 3 additions & 3 deletions src/Permissions/Permissions.blp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Adw.Dialog dialog {
}

Label {
label: _("Workbench needs additional permissions. Please run the following command in a terminal and restart the app.");
label: _("Workbench needs additional permissions. Please run the following command in a terminal and restart Workbench.");
wrap: true;
justify: center;
}
Expand Down Expand Up @@ -133,13 +133,13 @@ Adw.Dialog dialog {
]
}

Adw.ActionRow {
Adw.ActionRow action_row_device {
[prefix]
Image {
icon-name: "re.sonny.Workbench-gamepad-symbolic";
}

title: _("--device=input");
// title: _("--device=input");
subtitle: _("Access to input device such as gamepads");

styles [
Expand Down
23 changes: 16 additions & 7 deletions src/Permissions/Permissions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Gio from "gi://Gio";
import GLib from "gi://GLib";
import Gtk from "gi://Gtk";

import { build } from "../../troll/src/main.js";
Expand All @@ -8,21 +7,31 @@ import Interface from "./Permissions.blp" with { type: "uri" };

import illustration from "./permissions.svg";

import { getFlatpakInfo } from "../util.js";
import {
getFlatpakId,
getFlatpakInfo,
isDeviceInputOverrideAvailable,
} from "../flatpak.js";

const action_permissions = new Gio.SimpleAction({
name: "permissions",
parameter_type: null,
});

export function Permissions({ window }) {
const { dialog, picture_illustration, label_command, button_info } =
build(Interface);
const {
dialog,
picture_illustration,
label_command,
button_info,
action_row_device,
} = build(Interface);

picture_illustration.set_resource(illustration);
label_command.label = `flatpak override --user --share=network --socket=pulseaudio --device=input ${GLib.getenv(
"FLATPAK_ID",
)}`;

const device = isDeviceInputOverrideAvailable() ? "input" : "all";
label_command.label = `flatpak override --user --share=network --socket=pulseaudio --device=${device} ${getFlatpakId()}`;
action_row_device.title = `--input=${device}`;

button_info.connect("clicked", () => {
new Gtk.UriLauncher({
Expand Down
2 changes: 1 addition & 1 deletion src/about.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getGjsVersion,
getGLibVersion,
} from "../troll/src/util.js";
import { getFlatpakInfo } from "./util.js";
import { getFlatpakInfo } from "./flatpak.js";

export default function About({ application }) {
const flatpak_info = getFlatpakInfo();
Expand Down
36 changes: 36 additions & 0 deletions src/flatpak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import GLib from "gi://GLib";

let flatpak_info;
export function getFlatpakInfo() {
if (flatpak_info) return flatpak_info;
flatpak_info = new GLib.KeyFile();
try {
flatpak_info.load_from_file("/.flatpak-info", GLib.KeyFileFlags.NONE);
} catch (err) {
if (!err.matches(GLib.FileError, GLib.FileError.NOENT)) {
console.error(err);
}
return null;
}
return flatpak_info;
}

export function getFlatpakId() {
return getFlatpakInfo().get_string("Application", "name");
}

// https://repology.org/project/flatpak/versions
export function isDeviceInputOverrideAvailable(flatpak_version) {
flatpak_version ??= getFlatpakInfo().get_string(
"Instance",
"flatpak-version",
);

// https://github.com/flatpak/flatpak/releases/tag/1.15.6
return (
flatpak_version.localeCompare("1.15.6", undefined, {
numeric: true,
sensitivity: "base",
}) > -1
);
}
20 changes: 3 additions & 17 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Gio from "gi://Gio";
import Xdp from "gi://Xdp";
import GObject from "gi://GObject";
import { getLanguage } from "./common.js";
import { getFlatpakId } from "./flatpak.js";

export const portal = new Xdp.Portal();

Expand All @@ -25,21 +26,6 @@ export function ensureDir(file) {
}
}

let flatpak_info;
export function getFlatpakInfo() {
if (flatpak_info) return flatpak_info;
flatpak_info = new GLib.KeyFile();
try {
flatpak_info.load_from_file("/.flatpak-info", GLib.KeyFileFlags.NONE);
} catch (err) {
if (!err.matches(GLib.FileError, GLib.FileError.NOENT)) {
console.error(err);
}
return null;
}
return flatpak_info;
}

export { getLanguage };

export function listenProperty(object, property, fn, { initial = false } = {}) {
Expand Down Expand Up @@ -158,8 +144,8 @@ export function makeDropdownFlat(dropdown) {

export function buildRuntimePath(...args) {
return GLib.build_filenamev([
GLib.getenv("XDG_RUNTIME_DIR"),
GLib.getenv("FLATPAK_ID"),
GLib.get_user_runtime_dir(),
getFlatpakId(),
...args,
]);
}
Expand Down
29 changes: 29 additions & 0 deletions test/isDeviceInputOverrideAvailable.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import "../src/init.js";

import tst, { assert } from "../troll/tst/tst.js";

import { isDeviceInputOverrideAvailable } from "../src/flatpak.js";

const test = tst("isDeviceInputOverrideAvailable");

test("returns a boolean", () => {
assert.equal(typeof isDeviceInputOverrideAvailable(), "boolean");
});

test("returns true if Flatpak version is equal to 1.15.6", () => {
assert.equal(isDeviceInputOverrideAvailable("1.15.6"), true);
});

test("returns true if Flatpak version is higher than 1.15.6", () => {
assert.equal(isDeviceInputOverrideAvailable("1.15.7"), true);
assert.equal(isDeviceInputOverrideAvailable("1.16.5"), true);
assert.equal(isDeviceInputOverrideAvailable("2.15.4"), true);
});

test("returns false if Flatpak version is lower than 1.15.6", () => {
assert.equal(isDeviceInputOverrideAvailable("1.15.5"), false);
assert.equal(isDeviceInputOverrideAvailable("1.14.7"), false);
assert.equal(isDeviceInputOverrideAvailable("0.16.7"), false);
});

export default test;

0 comments on commit aa93296

Please sign in to comment.