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

Set hostname #113

Merged
merged 23 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d9d2a39
Add SystemView
meisenzahl Aug 7, 2021
d40906c
Set previous view
meisenzahl Aug 7, 2021
755a525
Set hostname
meisenzahl Aug 7, 2021
cbfc247
Add SystemView to POTFILES
meisenzahl Aug 7, 2021
8e96561
Read hostname from D-Bus interface
meisenzahl Aug 8, 2021
3c023e3
AccountView: Set hostname
meisenzahl Aug 8, 2021
e0bc9bb
AccountView: Increase spacing to hostname
meisenzahl Aug 9, 2021
4d5b3f4
Utils: set static hostname
meisenzahl Aug 9, 2021
188a30e
Utils: Set pretty hostname and generate valid static hostname
meisenzahl Aug 9, 2021
be5e553
AccountView: Validate hostname
meisenzahl Aug 10, 2021
87c6411
Utils: Show dialog on error
meisenzahl Aug 10, 2021
cb5e6b7
Merge branch 'master' into set-hostname
cassidyjames Aug 12, 2021
90f45bb
Merge branch 'master' into set-hostname
meisenzahl Aug 12, 2021
5d18695
AccountView: Set hostname after entry is mapped
meisenzahl Aug 12, 2021
3a05e6c
AccountView: Check if hostname is valid to finish setup
meisenzahl Aug 12, 2021
40de890
Merge branch 'set-hostname' of github.com:elementary/initial-setup in…
meisenzahl Aug 12, 2021
c4cafdc
Update 49-io.elementary.initial-setup.pkla
danirabbit Aug 12, 2021
420d53b
Update io.elementary.initial-setup.appdata.xml.in
danirabbit Aug 12, 2021
91b5627
Update 49-io.elementary.initial-setup.pkla
danirabbit Aug 12, 2021
86490cb
Update Utils.vala
danirabbit Aug 12, 2021
9f97092
Apply suggestions from code review
meisenzahl Aug 12, 2021
7dbfc16
Merge branch 'master' into set-hostname
cassidyjames Aug 16, 2021
1211371
Update io.elementary.initial-setup.appdata.xml.in
cassidyjames Aug 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Action=org.freedesktop.hostname1.set-hostname;org.freedesktop.hostname1.set-static-hostname;
Action=org.freedesktop.hostname1.set-pretty-hostname;org.freedesktop.hostname1.set-static-hostname;

I'm not familiar with the pkla file format at all, unfortunately, but that might work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current value is fine, see here for possible values: https://www.freedesktop.org/software/systemd/man/org.freedesktop.hostname1.html#Security

ResultAny=yes
ResultInactive=yes
ResultActive=yes
9 changes: 9 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,15 @@
<binary>io.elementary.initial-setup</binary>
</provides>
<releases>
<release version="6.1.0" date="2021-08-11" urgency="medium">
<description>
<p>Minor updates:</p>
<ul>
<li>Confirm and change device name</li>
<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 ().down ();
meisenzahl marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
38 changes: 37 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,27 @@ 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.")) {
max_width_chars = 60,
meisenzahl marked this conversation as resolved.
Show resolved Hide resolved
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 +113,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 +154,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 +251,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