[gnome-control-center] info: factor out os info retrieval function



commit 8b70c44b5a7868c0895e2b358323fc350f3ccba9
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Mon Jul 11 14:25:59 2016 -0300

    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

 panels/info/cc-info-panel.c |   84 ++++++++++++++++++++++++++++++++----------
 1 files changed, 64 insertions(+), 20 deletions(-)
---
diff --git a/panels/info/cc-info-panel.c b/panels/info/cc-info-panel.c
index 4b523a3..d6c47b0 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;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]