From 93cc06931ddd8a7891da0ee89adba9ae70eda0e5 Mon Sep 17 00:00:00 2001 From: Giovanni Campagna Date: Sun, 20 Oct 2013 23:40:55 +0200 Subject: [PATCH] Add support for pretty hostnames Read and parse /etc/machine-info to obtain a pretty human readable hostname (eg. "Bob's Laptop" instead of "bob-laptop"). If the file is not found, or it can't be parsed, we fall back on the normal hostname. In GNOME, the pretty hostname can be configured in the control center details and sharing panels. In other systems, it can be changed using "hostnamectl --pretty". https://bugzilla.gnome.org/show_bug.cgi?id=724138 --- data/rygel.conf | 2 +- src/librygel-core/rygel-root-device-factory.vala | 25 ++++++++++++++++++++++++ src/librygel-server/rygel-media-object.vala | 9 +++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/data/rygel.conf b/data/rygel.conf index 1347dc0..84dbeb8 100644 --- a/data/rygel.conf +++ b/data/rygel.conf @@ -115,7 +115,7 @@ virtual-folders=true [Playbin] enabled=true -title=Audio/Video playback on @HOSTNAME@ +title=Audio/Video playback on @PRETTY_HOSTNAME@ [ZDFMediathek] enabled=false diff --git a/src/librygel-core/rygel-root-device-factory.vala b/src/librygel-core/rygel-root-device-factory.vala index 6e35175..926d640 100644 --- a/src/librygel-core/rygel-root-device-factory.vala +++ b/src/librygel-core/rygel-root-device-factory.vala @@ -32,6 +32,30 @@ public errordomain RootDeviceFactoryError { XML_PARSE, } +namespace Rygel { + public string get_pretty_host_name () { + string machine_info; + + try { + FileUtils.get_contents ("/etc/machine-info", out machine_info); + + var lines = machine_info.split ("\n"); + + foreach (var line in lines) { + var parts = line.split ("="); + + if (parts[0] == "PRETTY_HOSTNAME") { + return string.joinv("=", parts[1:parts.length]); + } + } + } catch (GLib.Error e) { + debug("Failed to parse /etc/machine-info: %s", e.message); + } + + return Environment.get_host_name (); + } +} + /** * This is a factory to create #RygelRootDevice objects for * a given UPnP context. @@ -125,6 +149,7 @@ public class Rygel.RootDeviceFactory : Object, title = title.replace ("@REALNAME@", Environment.get_real_name ()); title = title.replace ("@USERNAME@", Environment.get_user_name ()); title = title.replace ("@HOSTNAME@", Environment.get_host_name ()); + title = title.replace ("@PRETTY_HOSTNAME@", get_pretty_host_name ()); return title; } diff --git a/src/librygel-server/rygel-media-object.vala b/src/librygel-server/rygel-media-object.vala index 36b9ba0..039e547 100644 --- a/src/librygel-server/rygel-media-object.vala +++ b/src/librygel-server/rygel-media-object.vala @@ -37,6 +37,7 @@ public abstract class Rygel.MediaObject : GLib.Object { private static Regex real_name_regex; private static Regex user_name_regex; private static Regex host_name_regex; + private static Regex pretty_name_regex; public string id { get; set construct; } public string ref_id { get; set; } @@ -96,6 +97,8 @@ public abstract class Rygel.MediaObject : GLib.Object { * - @@USERNAME@ will be substituted by the users's login ID. * - @@HOSTNAME@ will be substituted by the name of the machine. * - @@ADDRESS@ will be substituted by the IP address of network interface used for the UpNP communication. + * - @@PRETTY_HOSTNAME@ will be substituted by the human readable name of the machine + * (PRETTY_HOSTNAME field of /etc/machine-info) */ public string title { get { @@ -119,6 +122,11 @@ public abstract class Rygel.MediaObject : GLib.Object { -1, 0, Environment.get_host_name ()); + this._title = pretty_name_regex.replace_literal + (this._title, + -1, + 0, + get_pretty_host_name ()); } catch (GLib.RegexError err) { assert_not_reached (); } @@ -138,6 +146,7 @@ public abstract class Rygel.MediaObject : GLib.Object { real_name_regex = new Regex (Regex.escape_string ("@REALNAME@")); user_name_regex = new Regex (Regex.escape_string ("@USERNAME@")); host_name_regex = new Regex (Regex.escape_string ("@HOSTNAME@")); + pretty_name_regex = new Regex (Regex.escape_string ("@PRETTY_HOSTNAME@")); } catch (GLib.RegexError err) { assert_not_reached (); } -- 2.7.4