summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorges Basile Stavracas Neto <georges.stavracas@gmail.com>2016-07-11 14:25:59 -0300
committerGeorges Basile Stavracas Neto <georges.stavracas@gmail.com>2016-07-12 12:45:02 -0300
commit8b70c44b5a7868c0895e2b358323fc350f3ccba9 (patch)
treee3cd673b4be305787662956e700d06faedfd1053
parenta58f9dd3f8a998508e46be40c0c62ffad18d04d4 (diff)
downloadgnome-control-center-8b70c44b5a7868c0895e2b358323fc350f3ccba9.tar.gz
info: factor out os info retrieval function
This code will be reused in the future to retrieve information stored in /etc/os-release file and, as preparation for the next patch that retrieves and displays the build-id of the OS. https://bugzilla.gnome.org/show_bug.cgi?id=768693
-rw-r--r--panels/info/cc-info-panel.c84
1 files changed, 64 insertions, 20 deletions
diff --git a/panels/info/cc-info-panel.c b/panels/info/cc-info-panel.c
index 4b523a3d7..d6c47b08d 100644
--- a/panels/info/cc-info-panel.c
+++ b/panels/info/cc-info-panel.c
@@ -392,35 +392,79 @@ cc_info_panel_class_init (CcInfoPanelClass *klass)
object_class->finalize = cc_info_panel_finalize;
}
-static char *
-get_os_type (void)
+static GHashTable*
+get_os_info (void)
{
- int bits;
- char *buffer;
- char *name;
- char *result;
+ GHashTable *hashtable;
+ gchar *buffer;
- name = NULL;
+ hashtable = NULL;
if (g_file_get_contents ("/etc/os-release", &buffer, NULL, NULL))
{
- char *start, *end;
+ gchar **lines;
+ gint i;
+
+ lines = g_strsplit (buffer, "\n", -1);
+
+ for (i = 0; lines[i] != NULL; i++)
+ {
+ gchar *delimiter, *key, *value;
+
+ /* Initialize the hash table if needed */
+ if (!hashtable)
+ hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+
+ delimiter = strstr (lines[i], "=");
+ value = NULL;
+ key = NULL;
+
+ if (delimiter != NULL)
+ {
+ gint size;
+
+ key = g_strndup (lines[i], delimiter - lines[i]);
+
+ /* Jump the '=' */
+ delimiter += strlen ("=");
+
+ /* Eventually jump the ' " ' character */
+ if (g_str_has_prefix (delimiter, "\""))
+ delimiter += strlen ("\"");
+
+ size = strlen (delimiter);
- start = end = NULL;
- if ((start = strstr (buffer, "PRETTY_NAME=\"")) != NULL)
- {
- start += strlen ("PRETTY_NAME=\"");
- end = strchr (start, '"');
- }
+ /* Don't consider the last ' " ' too */
+ if (g_str_has_suffix (delimiter, "\""))
+ size -= strlen ("\"");
- if (start != NULL && end != NULL)
- {
- name = g_strndup (start, end - start);
- }
+ value = g_strndup (delimiter, size);
- g_free (buffer);
+ g_hash_table_insert (hashtable, key, value);
+ }
+ }
+
+ g_strfreev (lines);
+ g_free (buffer);
}
+ return hashtable;
+}
+
+static char *
+get_os_type (void)
+{
+ GHashTable *os_info;
+ gchar *name, *result;
+ int bits;
+
+ os_info = get_os_info ();
+
+ if (!os_info)
+ return NULL;
+
+ name = g_hash_table_lookup (os_info, "PRETTY_NAME");
+
if (GLIB_SIZEOF_VOID_P == 8)
bits = 64;
else
@@ -434,7 +478,7 @@ get_os_type (void)
else
result = g_strdup_printf (_("%d-bit"), bits);
- g_free (name);
+ g_clear_pointer (&os_info, g_hash_table_destroy);
return result;
}