[gnome-initial-setup: 2/3] language, privacy, summary: Use g_get_os_info()



commit b487d4666adecbe4bc29e0f8bb26000d599904a7
Author: Will Thompson <will willthompson co uk>
Date:   Sun Feb 9 15:23:59 2020 +0000

    language, privacy, summary: Use g_get_os_info()
    
    This replaces some hand-rolled parsing copy-pasted in three places.
    
    Fixes #93

 gnome-initial-setup/meson.build                    |  1 +
 .../pages/language/gis-language-page.c             | 46 +-----------------
 .../pages/privacy/gis-privacy-page.c               | 54 ++--------------------
 .../pages/summary/gis-summary-page.c               | 46 +-----------------
 4 files changed, 6 insertions(+), 141 deletions(-)
---
diff --git a/gnome-initial-setup/meson.build b/gnome-initial-setup/meson.build
index 3a7a8b8..bc31385 100644
--- a/gnome-initial-setup/meson.build
+++ b/gnome-initial-setup/meson.build
@@ -36,6 +36,7 @@ dependencies = [
     dependency ('goa-1.0'),
     dependency ('goa-backend-1.0'),
     dependency ('gtk+-3.0', version: '>= 3.11.3'),
+    dependency ('glib-2.0', version: '>= 2.63.1'),
     dependency ('gio-unix-2.0', version: '>= 2.53.0'),
     dependency ('gdm', version: '>= 3.8.3'),
     dependency ('geocode-glib-1.0'),
diff --git a/gnome-initial-setup/pages/language/gis-language-page.c 
b/gnome-initial-setup/pages/language/gis-language-page.c
index 5acd5f6..2e5ae02 100644
--- a/gnome-initial-setup/pages/language/gis-language-page.c
+++ b/gnome-initial-setup/pages/language/gis-language-page.c
@@ -179,45 +179,11 @@ localed_proxy_ready (GObject      *source,
   priv->localed = proxy;
 }
 
-static char *
-get_item (const char *buffer, const char *name)
-{
-  char *label, *start, *end, *result;
-  char end_char;
-
-  result = NULL;
-  start = NULL;
-  end = NULL;
-  label = g_strconcat (name, "=", NULL);
-  if ((start = strstr (buffer, label)) != NULL)
-    {
-      start += strlen (label);
-      end_char = '\n';
-      if (*start == '"')
-        {
-          start++;
-          end_char = '"';
-        }
-
-      end = strchr (start, end_char);
-    }
-
-    if (start != NULL && end != NULL)
-      {
-        result = g_strndup (start, end - start);
-      }
-
-  g_free (label);
-
-  return result;
-}
-
 static void
 update_distro_logo (GisLanguagePage *page)
 {
   GisLanguagePagePrivate *priv = gis_language_page_get_instance_private (page);
-  char *buffer;
-  char *id;
+  g_autofree char *id = g_get_os_info (G_OS_INFO_KEY_ID);
   gsize i;
 
   static const struct {
@@ -233,14 +199,6 @@ update_distro_logo (GisLanguagePage *page)
     { "SLES",                           "suse-logo-icon" },
   };
 
-  id = NULL;
-
-  if (g_file_get_contents ("/etc/os-release", &buffer, NULL, NULL))
-    {
-      id = get_item (buffer, "ID");
-      g_free (buffer);
-    }
-
   for (i = 0; i < G_N_ELEMENTS (id_to_logo); i++)
     {
       if (g_strcmp0 (id, id_to_logo[i].id) == 0)
@@ -249,8 +207,6 @@ update_distro_logo (GisLanguagePage *page)
           break;
         }
     }
-
-  g_free (id);
 }
 
 static void
diff --git a/gnome-initial-setup/pages/privacy/gis-privacy-page.c 
b/gnome-initial-setup/pages/privacy/gis-privacy-page.c
index dc2b03f..80c40c3 100644
--- a/gnome-initial-setup/pages/privacy/gis-privacy-page.c
+++ b/gnome-initial-setup/pages/privacy/gis-privacy-page.c
@@ -50,58 +50,14 @@ typedef struct _GisPrivacyPagePrivate GisPrivacyPagePrivate;
 
 G_DEFINE_TYPE_WITH_PRIVATE (GisPrivacyPage, gis_privacy_page, GIS_TYPE_PAGE);
 
-static char *
-get_item (const char *buffer, const char *name)
-{
-  char *label, *start, *end, *result;
-  char end_char;
-
-  result = NULL;
-  start = NULL;
-  end = NULL;
-  label = g_strconcat (name, "=", NULL);
-  if ((start = strstr (buffer, label)) != NULL)
-    {
-      start += strlen (label);
-      end_char = '\n';
-      if (*start == '"')
-        {
-          start++;
-          end_char = '"';
-        }
-
-      end = strchr (start, end_char);
-    }
-
-    if (start != NULL && end != NULL)
-      {
-        result = g_strndup (start, end - start);
-      }
-
-  g_free (label);
-
-  return result;
-}
-
 static void
 update_os_data (GisPrivacyPage *page)
 {
   GisPrivacyPagePrivate *priv = gis_privacy_page_get_instance_private (page);
-  char *buffer;
-  char *name;
-  char *privacy_policy;
+  g_autofree char *name = g_get_os_info (G_OS_INFO_KEY_NAME);
+  g_autofree char *privacy_policy = g_get_os_info (G_OS_INFO_KEY_PRIVACY_POLICY_URL);
   char *text;
 
-  name = NULL;
-  privacy_policy = NULL;
-
-  if (g_file_get_contents ("/etc/os-release", &buffer, NULL, NULL))
-    {
-      name = get_item (buffer, "NAME");
-      privacy_policy = get_item (buffer, "PRIVACY_POLICY_URL");
-      g_free (buffer);
-    }
-
   if (!name)
     name = g_strdup ("GNOME");
 
@@ -119,19 +75,15 @@ update_os_data (GisPrivacyPage *page)
        * like "Fedora" or "Ubuntu". It falls back to "GNOME" if we can't
        * detect any distribution.
        */
-      char *distro_label = g_strdup_printf (_("Problem data will be collected by %s:"), name);
+      g_autofree char *distro_label = g_strdup_printf (_("Problem data will be collected by %s:"), name);
       text = g_strdup_printf ("%s <a href='%s'>%s</a>", distro_label, privacy_policy, _("Privacy Policy"));
       gtk_label_set_markup (GTK_LABEL (priv->distro_privacy_policy_label), text);
-      g_free (distro_label);
       g_free (text);
     }
   else
     {
       gtk_widget_hide (priv->distro_privacy_policy_label);
     }
-
-  g_free (name);
-  g_free (privacy_policy);
 }
 
 static void
diff --git a/gnome-initial-setup/pages/summary/gis-summary-page.c 
b/gnome-initial-setup/pages/summary/gis-summary-page.c
index cdeb45c..a3f8e2c 100644
--- a/gnome-initial-setup/pages/summary/gis-summary-page.c
+++ b/gnome-initial-setup/pages/summary/gis-summary-page.c
@@ -211,55 +211,13 @@ gis_summary_page_shown (GisPage *page)
   gtk_widget_grab_focus (priv->start_button);
 }
 
-static char *
-get_item (const char *buffer, const char *name)
-{
-  char *label, *start, *end, *result;
-  char end_char;
-
-  result = NULL;
-  start = NULL;
-  end = NULL;
-  label = g_strconcat (name, "=", NULL);
-  if ((start = strstr (buffer, label)) != NULL)
-    {
-      start += strlen (label);
-      end_char = '\n';
-      if (*start == '"')
-        {
-          start++;
-          end_char = '"';
-        }
-
-      end = strchr (start, end_char);
-    }
-
-    if (start != NULL && end != NULL)
-      {
-        result = g_strndup (start, end - start);
-      }
-
-  g_free (label);
-
-  return result;
-}
-
 static void
 update_distro_name (GisSummaryPage *page)
 {
   GisSummaryPagePrivate *priv = gis_summary_page_get_instance_private (page);
-  char *buffer;
-  char *name;
+  g_autofree char *name = g_get_os_info (G_OS_INFO_KEY_NAME);
   char *text;
 
-  name = NULL;
-
-  if (g_file_get_contents ("/etc/os-release", &buffer, NULL, NULL))
-    {
-      name = get_item (buffer, "NAME");
-      g_free (buffer);
-    }
-
   if (!name)
     name = g_strdup ("GNOME 3");
 
@@ -276,8 +234,6 @@ update_distro_name (GisSummaryPage *page)
   text = g_strdup_printf (_("%s is ready to be used. We hope that you love it!"), name);
   gtk_label_set_label (GTK_LABEL (priv->tagline), text);
   g_free (text);
-
-  g_free (name);
 }
 
 static void


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