diff options
author | Robert Ancell <robert.ancell@canonical.com> | 2016-11-02 21:57:30 +1300 |
---|---|---|
committer | Robert Ancell <robert.ancell@canonical.com> | 2016-11-02 21:57:30 +1300 |
commit | c414b626430ff0abe02c7f1ac4a1f964e860f868 (patch) | |
tree | 9cc7d27c1cf6cddcd41b5248d30c1114badcebc0 | |
parent | 73acddfda7721ea6039d81d0f6d477c03d11dfdc (diff) | |
download | lightdm-git-c414b626430ff0abe02c7f1ac4a1f964e860f868.tar.gz |
Add functions for getting fields from /etc/os-release
-rw-r--r-- | doc/lightdm-gobject-1-sections.txt | 5 | ||||
-rw-r--r-- | liblightdm-gobject/lightdm/system.h | 10 | ||||
-rw-r--r-- | liblightdm-gobject/system.c | 140 | ||||
-rw-r--r-- | liblightdm-qt/QLightDM/greeter.h | 10 | ||||
-rw-r--r-- | liblightdm-qt/greeter.cpp | 25 |
5 files changed, 189 insertions, 1 deletions
diff --git a/doc/lightdm-gobject-1-sections.txt b/doc/lightdm-gobject-1-sections.txt index c8b055c8..bb5b92d8 100644 --- a/doc/lightdm-gobject-1-sections.txt +++ b/doc/lightdm-gobject-1-sections.txt @@ -144,6 +144,11 @@ lightdm_session_get_type <SECTION> <FILE>system</FILE> lightdm_get_hostname +lightdm_get_os_id +lightdm_get_os_name +lightdm_get_os_pretty_name +lightdm_get_os_version +lightdm_get_os_version_id </SECTION> <SECTION> diff --git a/liblightdm-gobject/lightdm/system.h b/liblightdm-gobject/lightdm/system.h index 74b81896..b09d058b 100644 --- a/liblightdm-gobject/lightdm/system.h +++ b/liblightdm-gobject/lightdm/system.h @@ -17,6 +17,16 @@ G_BEGIN_DECLS const gchar *lightdm_get_hostname (void); +const gchar *lightdm_get_os_id (void); + +const gchar *lightdm_get_os_name (void); + +const gchar *lightdm_get_os_pretty_name (void); + +const gchar *lightdm_get_os_version (void); + +const gchar *lightdm_get_os_version_id (void); + G_END_DECLS #endif /* LIGHTDM_HOSTNAME_H_ */ diff --git a/liblightdm-gobject/system.c b/liblightdm-gobject/system.c index d1ee4cab..d5b2a670 100644 --- a/liblightdm-gobject/system.c +++ b/liblightdm-gobject/system.c @@ -8,7 +8,7 @@ * See http://www.gnu.org/copyleft/lgpl.html the full text of the license. */ -#include <sys/utsname.h> +#include <string.h> #include "lightdm/system.h" @@ -31,3 +31,141 @@ lightdm_get_hostname (void) { return g_get_host_name (); } + +static gboolean os_release_loaded = FALSE; +static gchar *os_id = NULL; +static gchar *os_name = NULL; +static gchar *os_version = NULL; +static gchar *os_version_id = NULL; +static gchar *os_pretty_name = NULL; + +static void +use_os_value (const gchar *name, const gchar *value) +{ + if (strcmp (name, "ID") == 0) + os_id = g_strdup (value); + if (strcmp (name, "NAME") == 0) + os_name = g_strdup (value); + if (strcmp (name, "VERSION") == 0) + os_version = g_strdup (value); + if (strcmp (name, "VERSION_ID") == 0) + os_version_id = g_strdup (value); + if (strcmp (name, "PRETTY_NAME") == 0) + os_pretty_name = g_strdup (value); +} + +static void +load_os_release (void) +{ + gchar *data; + gchar **lines; + guint i; + + if (os_release_loaded) + return; + + if (!g_file_get_contents ("/etc/os-release", &data, NULL, NULL)) + return; + + lines = g_strsplit (data, "\n", -1); + for (i = 0; lines[i] != NULL; i++) + { + gchar **tokens; + tokens = g_strsplit (lines[i], "=", 2); + if (tokens[0] != NULL && tokens[1] != NULL) + { + gchar *name, *value; + size_t value_length; + + name = g_strstrip (tokens[0]); + value = g_strstrip (tokens[1]); + value_length = strlen (value); + if (value_length > 1 && value[0] == '\"' && value[value_length - 1] == '\"') + { + value[value_length - 1] = '\0'; + value++; + use_os_value (name, value); + } + } + g_strfreev (tokens); + } + g_strfreev (lines); + g_free (data); + + os_release_loaded = TRUE; +} + +/** + * lightdm_get_os_id: + * + * Get a word describing the OS, suitable for checking which OS the greeter is running on. + * e.g. "ubuntu" + * + * Return value: (nullable): a string (ID variable from /etc/os-release) or %NULL if not set. + **/ +const gchar * +lightdm_get_os_id (void) +{ + load_os_release (); + return os_id; +} + +/** + * lightdm_get_os_name: + * + * Get a line of text describing the OS without version information, suitable for presentation to the user. + * e.g. "Ubuntu" + * + * Return value: (nullable): a string (NAME variable from /etc/os-release) or %NULL if not set. + **/ +const gchar * +lightdm_get_os_name (void) +{ + load_os_release (); + return os_name; +} + +/** + * lightdm_get_os_pretty_name: + * + * Get a line of text describing the OS, suitable for presentation to the user. + * e.g. "Ubuntu 16.04.1 LTS" + * + * Return value: (nullable): a string (PRETTY_NAME variable from /etc/os-release) or %NULL if not set. + **/ +const gchar * +lightdm_get_os_pretty_name (void) +{ + load_os_release (); + return os_pretty_name; +} + +/** + * lightdm_get_os_version: + * + * Get a line of text describing the OS version, suitable for presentation to the user. + * e.g. "16.04.1 LTS (Xenial Xapus)" + * + * Return value: (nullable): a string (VERSION variable from /etc/os-release) or %NULL if not set. + **/ +const gchar * +lightdm_get_os_version (void) +{ + load_os_release (); + return os_version; +} + +/** + * lightdm_get_os_version_id: + * + * Get a word descibing the OS version, suitable for checking which version of the OS this greeter is running on. + * e.g. "16.04" + * + * Return value: (nullable): a string (VERSION_ID variable from /etc/os-release) or %NULL if not set. + **/ +const gchar * +lightdm_get_os_version_id (void) +{ + load_os_release (); + return os_version_id; +} diff --git a/liblightdm-qt/QLightDM/greeter.h b/liblightdm-qt/QLightDM/greeter.h index 548d13e5..b1605297 100644 --- a/liblightdm-qt/QLightDM/greeter.h +++ b/liblightdm-qt/QLightDM/greeter.h @@ -31,6 +31,11 @@ class Q_DECL_EXPORT Greeter : public QObject Q_PROPERTY(bool selectGuest READ selectGuestHint CONSTANT) Q_PROPERTY(QString hostname READ hostname CONSTANT) + Q_PROPERTY(QString osId READ osId CONSTANT) + Q_PROPERTY(QString osName READ osName CONSTANT) + Q_PROPERTY(QString osPrettyName READ osPrettyName CONSTANT) + Q_PROPERTY(QString osVersion READ osVersion CONSTANT) + Q_PROPERTY(QString osVersionId READ osVersionId CONSTANT) Q_PROPERTY(bool hasGuestAccount READ hasGuestAccountHint CONSTANT) Q_PROPERTY(bool locked READ lockHint CONSTANT) @@ -69,6 +74,11 @@ public: bool isAuthenticated() const; QString authenticationUser() const; QString hostname() const; + QString osId() const; + QString osName() const; + QString osPrettyName() const; + QString osVersion() const; + QString osVersionId() const; public Q_SLOTS: bool connectToDaemonSync(); diff --git a/liblightdm-qt/greeter.cpp b/liblightdm-qt/greeter.cpp index d535a022..bd213a00 100644 --- a/liblightdm-qt/greeter.cpp +++ b/liblightdm-qt/greeter.cpp @@ -292,6 +292,31 @@ QString Greeter::hostname() const return QString::fromUtf8(lightdm_get_hostname()); } +QString Greeter::osName() const +{ + return QString::fromUtf8(lightdm_get_os_name()); +} + +QString Greeter::osId() const +{ + return QString::fromUtf8(lightdm_get_os_id()); +} + +QString Greeter::osPrettyName() const +{ + return QString::fromUtf8(lightdm_get_os_pretty_name()); +} + +QString Greeter::osVersion() const +{ + return QString::fromUtf8(lightdm_get_os_version()); +} + +QString Greeter::osVersionId() const +{ + return QString::fromUtf8(lightdm_get_os_version_id()); +} + #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) #include "greeter_moc5.cpp" #else |