Skip to content

Commit

Permalink
Set hostname (#113)
Browse files Browse the repository at this point in the history
* Add SystemView

* Set previous view

* Set hostname

* Add SystemView to POTFILES

* Read hostname from D-Bus interface

* AccountView: Set hostname

* AccountView: Increase spacing to hostname

* Utils: set static hostname

* Utils: Set pretty hostname and generate valid static hostname

* AccountView: Validate hostname

* Utils: Show dialog on error

* AccountView: Set hostname after entry is mapped

* AccountView: Check if hostname is valid to finish setup

* Update 49-io.elementary.initial-setup.pkla

* Update io.elementary.initial-setup.appdata.xml.in

* Update 49-io.elementary.initial-setup.pkla

* Update Utils.vala

* Apply suggestions from code review

Co-authored-by: Cassidy James Blaede <cassidy@elementary.io>

* Update io.elementary.initial-setup.appdata.xml.in

Co-authored-by: Cassidy James Blaede <cassidy@elementary.io>
Co-authored-by: Daniel Foré <daniel@elementary.io>
  • Loading branch information
3 people committed Aug 20, 2021
1 parent f4bd679 commit 2051efc
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 1 deletion.
7 changes: 7 additions & 0 deletions data/49-io.elementary.initial-setup.pkla
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ Action=org.freedesktop.accounts.user-administration;io.elementary.pantheon.Accou
ResultAny=yes
ResultInactive=yes
ResultActive=yes

[Host name]
Identity=unix-user:lightdm
Action=org.freedesktop.hostname1.set-hostname;org.freedesktop.hostname1.set-static-hostname;
ResultAny=yes
ResultInactive=yes
ResultActive=yes
12 changes: 12 additions & 0 deletions data/io.elementary.initial-setup.appdata.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@
<binary>io.elementary.initial-setup</binary>
</provides>
<releases>
<release version="6.1.0" date="2021-08-11" urgency="medium">
<description>
<p>New features:</p>
<ul>
<li>Confirm and change device name</li>
</ul>
<p>Minor updates:</p>
<ul>
<li>Translation updates</li>
</ul>
</description>
</release>
<release version="6.0.0" date="2021-07-14" urgency="medium">
<description>
<p>Minor updates:</p>
Expand Down
100 changes: 100 additions & 0 deletions src/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,104 @@ namespace Utils {

return username;
}

public static string gen_hostname (string pretty_hostname) {
string hostname = "";
bool met_alpha = false;
bool whitespace_before = false;

foreach (char c in pretty_hostname.to_ascii ().to_utf8 ()) {
if (c.isalpha ()) {
hostname += c.to_string ();
met_alpha = true;
whitespace_before = false;
} else if (c.isdigit () && met_alpha) {
hostname += c.to_string ();
whitespace_before = false;
} else if (c.isspace () && !whitespace_before) {
hostname += "-";
whitespace_before = true;
}
}

return hostname;
}

[DBus (name = "org.freedesktop.hostname1")]
interface HostnameInterface : Object {
public abstract string pretty_hostname { owned get; }
public abstract string static_hostname { owned get; }

public abstract void set_pretty_hostname (string hostname, bool interactive) throws GLib.Error;
public abstract void set_static_hostname (string hostname, bool interactive) throws GLib.Error;
}

private static HostnameInterface? hostname_interface_instance;
private static void get_hostname_interface_instance () {
if (hostname_interface_instance == null) {
try {
hostname_interface_instance = Bus.get_proxy_sync (
BusType.SYSTEM,
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1"
);
} catch (GLib.Error e) {
warning ("%s", e.message);
}
}
}

public static string get_hostname () {
get_hostname_interface_instance ();

string hostname = hostname_interface_instance.pretty_hostname;

if (hostname.length == 0) {
hostname = hostname_interface_instance.static_hostname;
}

return hostname;
}

public static bool set_hostname (string hostname) {
string? primary_text = null;
string secondary_text = _("Initial Setup could not set your hostname.");
string? error_message = null;

try {
var permission = new Polkit.Permission.sync ("org.freedesktop.hostname1.set-static-hostname", new Polkit.UnixProcess (Posix.getpid ()));

if (permission != null && permission.allowed) {
get_hostname_interface_instance ();
hostname_interface_instance.set_pretty_hostname (hostname, false);
hostname_interface_instance.set_static_hostname (gen_hostname (hostname), false);
} else {
primary_text = _("No Permission to set hostname '%s'").printf (hostname);
}
} catch (GLib.Error e) {
primary_text = _("Unable to set Hostname '%s'").printf (hostname);
error_message = e.message;
}

if (primary_text != null) {
var error_dialog = new Granite.MessageDialog.with_image_from_icon_name (
primary_text,
secondary_text,
"dialog-error",
Gtk.ButtonsType.CLOSE
);

if (error_message != null) {
error_dialog.show_error_details (error_message);
}


error_dialog.run ();
error_dialog.destroy ();

return false;
}

return true;
}
}
39 changes: 38 additions & 1 deletion src/Views/AccountView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Installer.AccountView : AbstractInstallerView {
private Granite.ValidatedEntry username_entry;
private ValidatedEntry pw_entry;
private Gtk.LevelBar pw_levelbar;
private Granite.ValidatedEntry hostname_entry;

construct {
var avatar = new Hdy.Avatar (48, null, true) {
Expand Down Expand Up @@ -74,6 +75,28 @@ public class Installer.AccountView : AbstractInstallerView {
confirm_entry_revealer = new ErrorRevealer (".");
confirm_entry_revealer.label_widget.get_style_context ().add_class (Gtk.STYLE_CLASS_ERROR);

var hostname_label = new Granite.HeaderLabel (_("Device name")) {
margin_top = 16
};

hostname_entry = new Granite.ValidatedEntry () {
activates_default = true,
hexpand = true
};

hostname_entry.map.connect (() => {
hostname_entry.text = Utils.get_hostname ();
});

var hostname_info = new Gtk.Label (_("Visible to other devices when sharing, e.g. with Bluetooth or over the network.")) {
// Wrap without expanding the view
max_width_chars = 0,
margin_bottom = 18,
wrap = true,
xalign = 0
};
hostname_info.get_style_context ().add_class (Gtk.STYLE_CLASS_DIM_LABEL);

var form_grid = new Gtk.Grid ();
form_grid.row_spacing = 3;
form_grid.valign = Gtk.Align.CENTER;
Expand All @@ -91,6 +114,9 @@ public class Installer.AccountView : AbstractInstallerView {
form_grid.attach (confirm_label, 0, 10, 1, 1);
form_grid.attach (confirm_entry, 0, 11, 1, 1);
form_grid.attach (confirm_entry_revealer, 0, 12, 1, 1);
form_grid.attach (hostname_label, 0, 13, 1, 1);
form_grid.attach (hostname_entry, 0, 14, 1, 1);
form_grid.attach (hostname_info, 0, 15, 1, 1);

content_area.attach (avatar, 0, 0);
content_area.attach (title_label, 0, 1, 1, 1);
Expand Down Expand Up @@ -129,13 +155,20 @@ public class Installer.AccountView : AbstractInstallerView {
update_finish_button ();
});

hostname_entry.changed.connect (() => {
hostname_entry.is_valid = check_hostname ();
update_finish_button ();
});

finish_button.clicked.connect (() => {
string fullname = realname_entry.text;
string username = username_entry.text;
string password = pw_entry.text;

created = Utils.create_new_user (fullname, username, password);

Utils.set_hostname (hostname_entry.text);

next_step ();
});

Expand Down Expand Up @@ -219,8 +252,12 @@ public class Installer.AccountView : AbstractInstallerView {
return false;
}

private bool check_hostname () {
return Utils.gen_hostname (hostname_entry.text).length > 0;
}

private void update_finish_button () {
if (username_entry.is_valid && pw_entry.is_valid && confirm_entry.is_valid) {
if (username_entry.is_valid && pw_entry.is_valid && confirm_entry.is_valid && hostname_entry.is_valid) {
finish_button.sensitive = true;
finish_button.has_default = true;
} else {
Expand Down

0 comments on commit 2051efc

Please sign in to comment.