Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
awissu committed Nov 4, 2021
1 parent a2e2314 commit 6dffb54
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 14 deletions.
38 changes: 37 additions & 1 deletion src/LocaleManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace SwitchboardPlugLocale {
account_proxy = connection.get_proxy_sync<AccountProxy> (
"org.freedesktop.Accounts",
"/org/freedesktop/Accounts/User%u".printf (uid),
DBusProxyFlags.NONE
DBusProxyFlags.DO_NOT_LOAD_PROPERTIES
);
} catch (IOError e) {
critical (e.message);
Expand Down Expand Up @@ -289,6 +289,42 @@ namespace SwitchboardPlugLocale {
}
}

public string? get_localectl_settings (string? locale, string? format, string current_format) {
try {
string output;
Process.spawn_command_line_sync ("localectl", out output);

var lines = output.split ("\n");
foreach (var line in lines) {
if (locale != null) {
if ("LANG" in line) {
if (locale in line) {
return locale;
}
}
}
if (format != null) {
if ("LC_" in line) {
if (format in line) {
return format;
}
}
}
}

if (locale == null && format == null) {
if ("LC_" in string.joinv(" ", lines)) {
return null;
}
return current_format;
}

} catch (Error e) {
warning (e.message);
}
return null;
}

private static LocaleManager? instance = null;

public static unowned LocaleManager get_default () {
Expand Down
55 changes: 42 additions & 13 deletions src/Widgets/LocaleSetting.vala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace SwitchboardPlugLocale.Widgets {
public class LocaleSetting : Granite.SimpleSettingsPage {
private Gtk.Button set_button;
private Gtk.Button set_system_button;
private Gtk.ComboBox format_combobox;
private Gtk.ComboBox region_combobox;
private Gtk.ListStore format_store;
Expand Down Expand Up @@ -102,8 +103,9 @@ namespace SwitchboardPlugLocale.Widgets {
set_button.sensitive = false;
set_button.get_style_context ().add_class (Gtk.STYLE_CLASS_SUGGESTED_ACTION);

var set_system_button = new Gtk.Button.with_label (_("Set System Language"));
set_system_button = new Gtk.Button.with_label (_("Set System Language"));
set_system_button.tooltip_text = _("Set language for login screen, guest account and new user accounts");
set_system_button.sensitive = false;

var keyboard_button = new Gtk.Button.with_label (_("Keyboard Settings…"));

Expand All @@ -127,13 +129,12 @@ namespace SwitchboardPlugLocale.Widgets {
debug ("Setting user language to '%s'", locale);
lm.set_user_language (locale);

compare ();

var format = get_format ();
debug ("Setting user format to '%s'", format);
lm.set_user_format (format);

settings_changed ();
compare ();
});

set_system_button.clicked.connect (() => {
Expand Down Expand Up @@ -186,11 +187,32 @@ namespace SwitchboardPlugLocale.Widgets {
}

private void compare () {
string? compared_format = null;

var selected_locale = get_selected_locale ();
var selected_format = get_format ();

if (set_button != null) {
if (lm.get_user_language () == get_selected_locale () && lm.get_user_format () == get_format ()) {
set_button.sensitive = false;
if (lm.get_user_language () == selected_locale && lm.get_user_format () == selected_format) {
set_button.sensitive = false;
} else {
set_button.sensitive = true;
}
}

if (selected_locale != selected_format) {
compared_format = selected_format;
}

compare_system_settings (selected_locale, compared_format, selected_locale, selected_format);
}

private void compare_system_settings (string locale, string? unknown, string selected, string format) {
if (set_system_button != null) {
if (lm.get_localectl_settings (locale, null, selected) == locale && lm.get_localectl_settings (null, unknown, selected) == format) {
set_system_button.sensitive = false;
} else {
set_button.sensitive = true;
set_system_button.sensitive = true;
}
}
}
Expand Down Expand Up @@ -257,7 +279,7 @@ namespace SwitchboardPlugLocale.Widgets {
public void reload_formats (Gee.ArrayList<string>? locales) {
format_store.clear ();
var user_format = lm.get_user_format ();
int format_id = 0;
string? format_id = null;

int i = 0;
foreach (var locale in locales) {
Expand All @@ -269,15 +291,21 @@ namespace SwitchboardPlugLocale.Widgets {
format_store.set (iter, 0, country, 1, locale);

if (locale == user_format) {
format_id = i;
format_id = locale;
}

i++;
}
}

format_combobox.id_column = 1;
format_combobox.sensitive = i != 1; // set to unsensitive if only have one item
format_combobox.active = format_id;

if (format_id != null) {
format_combobox.active_id = format_id;
} else {
format_combobox.active = 0;
}

format_store.set_sort_column_id (0, Gtk.SortType.ASCENDING);

Expand All @@ -289,12 +317,13 @@ namespace SwitchboardPlugLocale.Widgets {
}

private void on_applied_to_system () {
var selected_locale = get_selected_locale ();
var selected_format = get_format ();
debug ("Setting system language to '%s' and format to '%s'", selected_locale, selected_format);
lm.apply_to_system (selected_locale, selected_format);
var selected_system_locale = get_selected_locale ();
var selected_system_format = get_format ();
debug ("Setting system language to '%s' and format to '%s'", selected_system_locale, selected_system_format);
lm.apply_to_system (selected_system_locale, selected_system_format);

settings_changed ();
compare();
}
}
}

0 comments on commit 6dffb54

Please sign in to comment.