Skip to content

Commit

Permalink
UbuntuInstaller: get permission async (#214)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryo Nakano <[email protected]>
  • Loading branch information
danirabbit and ryonakano authored Jun 29, 2024
1 parent b7a733e commit 1d87c96
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 78 deletions.
2 changes: 2 additions & 0 deletions data/locale.metainfo.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
<issue url="https://github.com/elementary/switchboard-plug-locale/issues/15">"Chinese" language listed in the list not displayed in Chinese characters</issue>
<issue url="https://github.com/elementary/switchboard-plug-locale/issues/70">Formats dropdown is slow to navigate</issue>
<issue url="https://github.com/elementary/switchboard-plug-locale/issues/81">Setting default temperature units depending on locale</issue>
<issue url="https://github.com/elementary/switchboard-plug-locale/issues/112">Throw an error dialog when an installation of language failed</issue>
<issue url="https://github.com/elementary/switchboard-plug-locale/issues/200">Font missing after uninstalling language</issue>
<issue url="https://github.com/elementary/switchboard-plug-locale/issues/204">Clicking on plug while authentication dialog open results in persistent "Not responding" warning</issue>
</issues>
</release>

Expand Down
99 changes: 65 additions & 34 deletions src/Installer/UbuntuInstaller.vala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ public class SwitchboardPlugLocale.Installer.UbuntuInstaller : Object {
}
}

public void install (string language) {
public async void install (string language) throws Error {
var has_permission = yield get_permission ();
if (!has_permission) {
throw (new DBusError.ACCESS_DENIED (""));
}

transaction_mode = TransactionMode.INSTALL;
var packages = get_remaining_packages_for_language (language);
transaction_language_code = language;
Expand All @@ -68,63 +73,89 @@ public class SwitchboardPlugLocale.Installer.UbuntuInstaller : Object {
message ("Packet: %s", packet);
}

aptd.install_packages.begin (packages, (obj, res) => {
try {
var transaction_id = aptd.install_packages.end (res);
transactions.@set (transaction_id, "i-" + language);
run_transaction (transaction_id);
} catch (Error e) {
warning ("Could not queue downloads: %s", e.message);
}
});
}

private void install_packages (string [] packages) {
foreach (var packet in packages) {
message ("will install: %s", packet);
try {
var transaction_id = yield aptd.install_packages (packages);
transactions.@set (transaction_id, "i-" + language);
run_transaction (transaction_id);
} catch (Error e) {
throw (e);
}

aptd.install_packages.begin (packages, (obj, res) => {
try {
var transaction_id = aptd.install_packages.end (res);
transactions.@set (transaction_id, "install-missing");
run_transaction (transaction_id);
} catch (Error e) {
warning ("Could not queue downloads: %s", e.message);
}
});
}

public async void check_missing_languages () {
missing_packages = yield Utils.get_missing_languages ();
check_missing_finished (missing_packages);
}

public void install_missing_languages () {
public async void install_missing_languages () throws Error {
if (missing_packages == null || missing_packages.length == 0) {
return;
}

var has_permission = yield get_permission ();
if (!has_permission) {
throw (new DBusError.ACCESS_DENIED (""));
}

transaction_mode = TransactionMode.INSTALL_MISSING;

install_packages (missing_packages);
foreach (unowned var package in missing_packages) {
message ("will install: %s", package);
}

try {
var transaction_id = yield aptd.install_packages (missing_packages);
transactions.@set (transaction_id, "install-missing");
run_transaction (transaction_id);
} catch (Error e) {
throw (e);
}
}

public void remove (string languagecode) {
public async void remove (string languagecode) throws Error {
var has_permission = yield get_permission ();
if (!has_permission) {
throw (new DBusError.ACCESS_DENIED (""));
}

transaction_mode = TransactionMode.REMOVE;
transaction_language_code = languagecode;

var installed = get_to_remove_packages_for_language (languagecode);

aptd.remove_packages.begin (installed, (obj, res) => {
try {
var transaction_id = yield aptd.remove_packages (installed);
transactions.@set (transaction_id, "r-" + languagecode);
run_transaction (transaction_id);
} catch (Error e) {
throw (e);
}
}

private static Polkit.Permission? permission = null;
private static async bool get_permission () {
if (permission == null) {
try {
var transaction_id = aptd.remove_packages.end (res);
transactions.@set (transaction_id, "r-" + languagecode);
run_transaction (transaction_id);
permission = yield new Polkit.Permission (
"io.elementary.settings.locale.administration",
new Polkit.UnixProcess (Posix.getpid ())
);
} catch (Error e) {
warning ("Could not queue deletions: %s", e.message);
critical (e.message);
return false;
}
});
}

if (!permission.allowed) {
try {
yield permission.acquire_async ();
} catch (Error e) {
critical (e.message);
return false;
}
}

return true;
}

public void cancel_install () {
Expand Down
33 changes: 0 additions & 33 deletions src/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -219,39 +219,6 @@ namespace SwitchboardPlugLocale {
return region_name;
}

private static Polkit.Permission? permission = null;

public static Polkit.Permission? get_permission () {
if (permission != null) {
return permission;
}

try {
permission = new Polkit.Permission.sync (
"io.elementary.settings.locale.administration",
new Polkit.UnixProcess (Posix.getpid ())
);
return permission;
} catch (Error e) {
critical (e.message);
return null;
}
}

public static bool allowed_permission () {
var permission = Utils.get_permission ();
if (!permission.allowed) {
try {
permission.acquire (null);
} catch (Error e) {
critical (e.message);
return false;
}
}

return true;
}

static Utils? instance = null;

public static Utils get_default () {
Expand Down
25 changes: 24 additions & 1 deletion src/Widgets/LocaleSetting.vala
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,30 @@ namespace SwitchboardPlugLocale.Widgets {

missing_lang_infobar.response.connect (() => {
missing_lang_infobar.revealed = false;
installer.install_missing_languages ();

installer.install_missing_languages.begin ((obj, res) => {
try {
installer.install_missing_languages.end (res);
} catch (Error e) {
missing_lang_infobar.revealed = true;

if (e.matches (GLib.DBusError.quark (), GLib.DBusError.ACCESS_DENIED)) {
return;
}

var dialog = new Granite.MessageDialog (
_("Couldn't install missing language packs"),
e.message,
new ThemedIcon ("preferences-desktop-locale")
) {
badge_icon = new ThemedIcon ("dialog-error"),
modal = true,
transient_for = ((Gtk.Application) Application.get_default ()).active_window
};
dialog.present ();
dialog.response.connect (dialog.destroy);
}
});
});

set_button.clicked.connect (() => {
Expand Down
52 changes: 42 additions & 10 deletions src/Widgets/LocaleView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,27 @@ namespace SwitchboardPlugLocale.Widgets {
});

remove_button.clicked.connect (() => {
if (!Utils.allowed_permission ()) {
return;
}

installer.remove (list_box.get_selected_language_code ());
installer.remove.begin (list_box.get_selected_language_code (), (obj, res) => {
try {
installer.remove.end (res);
} catch (Error e) {
if (e.matches (GLib.DBusError.quark (), GLib.DBusError.ACCESS_DENIED)) {
return;
}

var dialog = new Granite.MessageDialog (
_("Couldn't remove language pack"),
e.message,
new ThemedIcon ("preferences-desktop-locale")
) {
badge_icon = new ThemedIcon ("dialog-error"),
modal = true,
transient_for = ((Gtk.Application) Application.get_default ()).active_window
};
dialog.present ();
dialog.response.connect (dialog.destroy);
}
});
});

add_button.clicked.connect (() => {
Expand All @@ -134,11 +150,27 @@ namespace SwitchboardPlugLocale.Widgets {
});

install_dialog.language_selected.connect ((lang) => {
if (!Utils.allowed_permission ()) {
return;
}

installer.install (lang);
installer.install.begin (lang, (obj, res) => {
try {
installer.install.end (res);
} catch (Error e) {
if (e.matches (GLib.DBusError.quark (), GLib.DBusError.ACCESS_DENIED)) {
return;
}

var dialog = new Granite.MessageDialog (
_("Couldn't install language pack"),
e.message,
new ThemedIcon ("preferences-desktop-locale")
) {
badge_icon = new ThemedIcon ("dialog-error"),
modal = true,
transient_for = ((Gtk.Application) Application.get_default ()).active_window
};
dialog.present ();
dialog.response.connect (dialog.destroy);
}
});
});
}

Expand Down

0 comments on commit 1d87c96

Please sign in to comment.