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

Add Locale1 support for keyboard configuration, respect "main country" for locale selection #66

Merged
merged 2 commits into from
Sep 10, 2019
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
34 changes: 32 additions & 2 deletions src/Helpers/LocaleHelper.vala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@
*/

namespace LocaleHelper {
[DBus (name = "org.freedesktop.locale1")]
interface Locale1 : Object {
[DBus (name = "SetLocale")]
public abstract void set_locale (string[] locale, bool user_interaction) throws GLib.Error;
[DBus (name = "SetVConsoleKeyboard")]
public abstract void set_vconsole_keyboard (string keymap, string keymap_toggle, bool convert, bool user_interaction) throws GLib.Error;
[DBus (name = "SetX11Keyboard")]
public abstract void set_x11_keyboard (string layout, string model, string variant, string options, bool convert, bool user_interaction) throws GLib.Error;
[DBus (name = "Locale")]
public abstract string[] locale { owned get; }
[DBus (name = "VConsoleKeymap")]
public abstract string vconsole_keymap { owned get; }
[DBus (name = "VConsoleKeymapToggle")]
public abstract string vconsole_keymap_toggle { owned get; }
[DBus (name = "X11Layout")]
public abstract string x11_layout { owned get; }
[DBus (name = "X11Model")]
public abstract string x11_model { owned get; }
[DBus (name = "X11Variant")]
public abstract string x11_variant { owned get; }
[DBus (name = "X11Options")]
public abstract string x11_options { owned get; }
}

public class LangEntry {
public string alpha_3;
public string? alpha_2;
Expand All @@ -27,11 +51,12 @@ namespace LocaleHelper {
countries = {};
}

public string get_code () {
public unowned string get_code () {
return alpha_2 ?? alpha_3;
}

public void add_country (CountryEntry country_entry) {
country_entry.lang = this;
var _countries = countries;
_countries += country_entry;
countries = _countries;
Expand All @@ -42,10 +67,15 @@ namespace LocaleHelper {
public string alpha_2;
public string alpha_3;
public string name;
public unowned LangEntry lang;

public string get_code () {
public unowned string get_code () {
return alpha_2 ?? alpha_3;
}

public string get_full_code () {
return "%s_%s".printf (lang.get_code (), get_code ());
}
}

private static Gee.HashMap<string, LangEntry?> lang_entries;
Expand Down
1 change: 0 additions & 1 deletion src/Objects/Configuration.vala
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@ public class Configuration : GLib.Object {
public string? country { get; set; default = null; }
public string keyboard_layout { get; set; }
public string? keyboard_variant { get; set; default = null; }
public string keyboard { get; set; }
}
12 changes: 12 additions & 0 deletions src/Views/KeyboardLayoutView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ public class KeyboardLayoutView : AbstractInstallerView {
var settings = new Settings ("org.gnome.desktop.input-sources");
settings.set_value ("sources", list);
settings.set_uint ("current", 0);

try {
LocaleHelper.Locale1 locale1 = Bus.get_proxy_sync (
BusType.SYSTEM,
"org.freedesktop.locale1",
"/org/freedesktop/locale1"
);

locale1.set_x11_keyboard (configuration.keyboard_layout, "", configuration.keyboard_variant ?? "", "", true, true);
} catch (Error e) {
critical ("Unable to get Locale1 interface");
}
}
});

Expand Down
51 changes: 35 additions & 16 deletions src/Views/LanguageView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class Installer.LanguageView : AbstractInstallerView {
size_group.add_widget (image);

lang_variant_widget = new VariantWidget ();
lang_variant_widget.variant_listbox.set_sort_func ((Gtk.ListBoxSortFunc) CountryRow.compare);

lang_variant_widget.variant_listbox.row_activated.connect (() => {
next_button.activate ();
Expand Down Expand Up @@ -128,12 +129,13 @@ public class Installer.LanguageView : AbstractInstallerView {
}

private void row_selected (Gtk.ListBoxRow? row) {
var lang_entry = ((LangRow) row).lang_entry;
unowned LocaleHelper.LangEntry lang_entry = ((LangRow) row).lang_entry;
unowned string lang_code = lang_entry.get_code ();

foreach (Gtk.Widget child in lang_variant_widget.main_listbox.get_children ()) {
foreach (weak Gtk.Widget child in lang_variant_widget.main_listbox.get_children ()) {
if (child is LangRow) {
var lang_row = (LangRow) child;
if (lang_row.lang_entry.get_code () == lang_entry.get_code ()) {
weak LangRow lang_row = (LangRow) child;
if (lang_row.lang_entry.get_code () == lang_code) {
lang_row.selected = true;
} else {
lang_row.selected = false;
Expand All @@ -145,10 +147,10 @@ public class Installer.LanguageView : AbstractInstallerView {
}

private void variant_row_selected (Gtk.ListBoxRow? row) {
var country_entry = ((CountryRow) row).country_entry;
foreach (Gtk.Widget child in lang_variant_widget.variant_listbox.get_children ()) {
unowned LocaleHelper.CountryEntry country_entry = ((CountryRow) row).country_entry;
foreach (weak Gtk.Widget child in lang_variant_widget.variant_listbox.get_children ()) {
if (child is CountryRow) {
var country_row = (CountryRow) child;
weak CountryRow country_row = (CountryRow) child;
if (country_row.country_entry.alpha_2 == country_entry.alpha_2) {
country_row.selected = true;
} else {
Expand All @@ -157,28 +159,38 @@ public class Installer.LanguageView : AbstractInstallerView {
}
}

next_button.label = LocaleHelper.lang_gettext (N_("Select"), country_entry.get_full_code ());
next_button.sensitive = true;
}

private void row_activated (Gtk.ListBoxRow row) {
var lang_entry = ((LangRow) row).lang_entry;
var countries = lang_entry.countries;
unowned LocaleHelper.CountryEntry[] countries = lang_entry.countries;
if (countries.length == 0) {
next_button.sensitive = true;
return;
}

unowned string lang_code = lang_entry.get_code ();
string? main_country = LocaleHelper.get_main_country (lang_code);

lang_variant_widget.variant_listbox.row_selected.disconnect (variant_row_selected);
lang_variant_widget.clear_variants ();
lang_variant_widget.variant_listbox.row_selected.connect (variant_row_selected);
foreach (var country in countries) {
lang_variant_widget.variant_listbox.add (new CountryRow (country));
foreach (unowned LocaleHelper.CountryEntry country in countries) {
var country_row = new CountryRow (country);
lang_variant_widget.variant_listbox.add (country_row);
if (country.get_code () == main_country) {
lang_variant_widget.variant_listbox.select_row (country_row);
}
}

lang_variant_widget.variant_listbox.select_row (lang_variant_widget.variant_listbox.get_row_at_index (0));
if (main_country == null || lang_variant_widget.variant_listbox.get_selected_row () == null) {
lang_variant_widget.variant_listbox.select_row (lang_variant_widget.variant_listbox.get_row_at_index (0));
}

lang_variant_widget.variant_listbox.show_all ();
Environment.set_variable ("LANGUAGE", lang_entry.get_code (), true);
Environment.set_variable ("LANGUAGE", lang_code, true);
Intl.textdomain (Build.GETTEXT_PACKAGE);
lang_variant_widget.show_variants (_("Languages"), "<b>%s</b>".printf (lang_entry.name));
}
Expand All @@ -195,11 +207,13 @@ public class Installer.LanguageView : AbstractInstallerView {
if (lang_entry.countries.length > 0) {
row = lang_variant_widget.variant_listbox.get_selected_row ();
if (row != null) {
var country = ((CountryRow) row).country_entry;
configuration.country = country.alpha_2;
lang += "_%s".printf (country.get_code ());
unowned LocaleHelper.CountryEntry country = ((CountryRow) row).country_entry;
configuration.country = country.get_code ();
lang = country.get_full_code ();
} else {
lang += "_%s".printf (lang_entry.countries[0].get_code ());
unowned string country = lang_entry.countries[0].get_code ();
lang += "_%s".printf (country);
configuration.country = country;
}
}

Expand Down Expand Up @@ -328,6 +342,11 @@ public class Installer.LanguageView : AbstractInstallerView {

add (grid);
}


public static int compare (CountryRow countryrow1, CountryRow countryrow2) {
return countryrow1.country_entry.name.collate (countryrow2.country_entry.name);
}
}
}