[gnome-control-center] Make /etc/os-release support common
- From: Robert Ancell <rancell src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-control-center] Make /etc/os-release support common
- Date: Mon, 9 Sep 2019 21:45:44 +0000 (UTC)
commit 1752cf3bb573d300e990ea10006dea70a322d8df
Author: Robert Ancell <robert ancell canonical com>
Date: Wed Aug 21 16:55:51 2019 +1200
Make /etc/os-release support common
The existing code is used in multiple places with slightly different
implementations.
panels/common/cc-os-release.c | 84 +++++++++++++++++++++++++++++
panels/common/cc-os-release.h | 30 +++++++++++
panels/common/meson.build | 1 +
panels/info/cc-info-overview-panel.c | 64 ++--------------------
panels/privacy/cc-privacy-panel.c | 102 ++++-------------------------------
5 files changed, 129 insertions(+), 152 deletions(-)
---
diff --git a/panels/common/cc-os-release.c b/panels/common/cc-os-release.c
new file mode 100644
index 000000000..4f04da952
--- /dev/null
+++ b/panels/common/cc-os-release.c
@@ -0,0 +1,84 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2019 Canonical Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "cc-os-release.h"
+
+gchar *
+cc_os_release_get_value (const gchar *key)
+{
+ g_autoptr(GHashTable) values = NULL;
+
+ values = cc_os_release_get_values ();
+ if (values == NULL)
+ return NULL;
+
+ return g_strdup (g_hash_table_lookup (values, key));
+}
+
+GHashTable *
+cc_os_release_get_values (void)
+{
+ g_autoptr(GHashTable) values = NULL;
+ g_autofree gchar *buffer = NULL;
+ g_auto(GStrv) lines = NULL;
+ int i;
+ g_autoptr(GError) error = NULL;
+
+ values = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+
+ if (!g_file_get_contents ("/etc/os-release", &buffer, NULL, &error))
+ {
+ if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
+ return NULL;
+
+ if (!g_file_get_contents ("/usr/lib/os-release", &buffer, NULL, NULL))
+ return NULL;
+ }
+
+ /* Default values in spec */
+ g_hash_table_insert (values, g_strdup ("NAME"), g_strdup ("Linux"));
+ g_hash_table_insert (values, g_strdup ("ID"), g_strdup ("Linux"));
+ g_hash_table_insert (values, g_strdup ("PRETTY_NAME"), g_strdup ("Linux"));
+
+ lines = g_strsplit (buffer, "\n", -1);
+ for (i = 0; lines[i] != NULL; i++)
+ {
+ gchar *line = lines[i];
+ g_auto(GStrv) tokens = NULL;
+ const gchar *key, *value;
+ g_autofree gchar *unquoted_value = NULL;
+
+ /* Skip comments */
+ if (g_str_has_prefix (line, "#"))
+ continue;
+
+ tokens = g_strsplit (line, "=", 2);
+ if (g_strv_length (tokens) < 2)
+ continue;
+ key = tokens[0];
+ value = tokens[1];
+ unquoted_value = g_shell_unquote (value, NULL);
+ if (unquoted_value != NULL)
+ value = unquoted_value;
+
+ g_hash_table_insert (values, g_strdup (key), g_strdup (value));
+ }
+
+ return g_steal_pointer (&values);
+}
diff --git a/panels/common/cc-os-release.h b/panels/common/cc-os-release.h
new file mode 100644
index 000000000..3213d8512
--- /dev/null
+++ b/panels/common/cc-os-release.h
@@ -0,0 +1,30 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2019 Canonical Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#pragma once
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+gchar *cc_os_release_get_value (const gchar *key);
+
+GHashTable *cc_os_release_get_values (void);
+
+G_END_DECLS
diff --git a/panels/common/meson.build b/panels/common/meson.build
index c0705f9ba..a528c567b 100644
--- a/panels/common/meson.build
+++ b/panels/common/meson.build
@@ -26,6 +26,7 @@ common_sources += gnome.mkenums(
sources = files(
'cc-hostname-entry.c',
+ 'cc-os-release.c',
'hostname-helper.c',
'list-box-helper.c',
)
diff --git a/panels/info/cc-info-overview-panel.c b/panels/info/cc-info-overview-panel.c
index 7fce8691e..09a589dcd 100644
--- a/panels/info/cc-info-overview-panel.c
+++ b/panels/info/cc-info-overview-panel.c
@@ -22,6 +22,7 @@
#include <config.h>
#include "cc-hostname-entry.h"
+#include "cc-os-release.h"
#include "cc-info-resources.h"
#include "info-cleanup.h"
@@ -371,70 +372,15 @@ get_graphics_data (void)
return result;
}
-static GHashTable*
-get_os_info (void)
-{
- GHashTable *hashtable;
- g_autofree gchar *buffer = NULL;
-
- hashtable = NULL;
-
- if (g_file_get_contents ("/etc/os-release", &buffer, NULL, NULL))
- {
- g_auto(GStrv) lines = NULL;
- gint i;
-
- lines = g_strsplit (buffer, "\n", -1);
-
- for (i = 0; lines[i] != NULL; i++)
- {
- gchar *delimiter;
-
- /* 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], "=");
-
- if (delimiter != NULL)
- {
- gint size;
- gchar *key, *value;
-
- 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);
-
- /* Don't consider the last ' " ' too */
- if (g_str_has_suffix (delimiter, "\""))
- size -= strlen ("\"");
-
- value = g_strndup (delimiter, size);
-
- g_hash_table_insert (hashtable, key, value);
- }
- }
- }
-
- return hashtable;
-}
-
static char *
get_os_name (void)
{
- GHashTable *os_info;
- gchar *name, *version_id, *pretty_name, *build_id;
+ g_autoptr(GHashTable) os_info = NULL;
+ const gchar *name, *version_id, *pretty_name, *build_id;
gchar *result = NULL;
g_autofree gchar *name_version = NULL;
- os_info = get_os_info ();
+ os_info = cc_os_release_get_values ();
if (!os_info)
return g_strdup (_("Unknown"));
@@ -464,8 +410,6 @@ get_os_name (void)
result = g_strdup (name_version);
}
- g_clear_pointer (&os_info, g_hash_table_destroy);
-
return result;
}
diff --git a/panels/privacy/cc-privacy-panel.c b/panels/privacy/cc-privacy-panel.c
index f30669add..c3664aaba 100644
--- a/panels/privacy/cc-privacy-panel.c
+++ b/panels/privacy/cc-privacy-panel.c
@@ -19,6 +19,7 @@
*/
#include "list-box-helper.h"
+#include "cc-os-release.h"
#include "cc-privacy-panel.h"
#include "cc-privacy-resources.h"
#include "cc-util.h"
@@ -101,88 +102,6 @@ struct _CcPrivacyPanel
CC_PANEL_REGISTER (CcPrivacyPanel, cc_privacy_panel)
-static char *
-get_os_name (void)
-{
- char *buffer;
- char *name;
-
- name = NULL;
-
- if (g_file_get_contents ("/etc/os-release", &buffer, NULL, NULL))
- {
- char *start, *end;
-
- start = end = NULL;
- if ((start = strstr (buffer, "NAME=")) != NULL)
- {
- start += strlen ("NAME=");
- end = strchr (start, '\n');
- }
-
- if (start != NULL && end != NULL)
- {
- name = g_strndup (start, end - start);
- }
-
- g_free (buffer);
- }
-
- if (name && *name != '\0')
- {
- char *tmp;
- tmp = g_shell_unquote (name, NULL);
- g_free (name);
- name = tmp;
- }
-
- if (name == NULL)
- name = g_strdup ("GNOME");
-
- return name;
-}
-
-static char *
-get_privacy_policy_url (void)
-{
- char *buffer;
- char *url;
-
- url = NULL;
-
- if (g_file_get_contents ("/etc/os-release", &buffer, NULL, NULL))
- {
- char *start, *end;
-
- start = end = NULL;
- if ((start = strstr (buffer, "PRIVACY_POLICY_URL=")) != NULL)
- {
- start += strlen ("PRIVACY_POLICY_URL=");
- end = strchr (start, '\n');
- }
-
- if (start != NULL && end != NULL)
- {
- url = g_strndup (start, end - start);
- }
-
- g_free (buffer);
- }
-
- if (url && *url != '\0')
- {
- char *tmp;
- tmp = g_shell_unquote (url, NULL);
- g_free (url);
- url = tmp;
- }
-
- if (url == NULL)
- url = g_strdup ("http://www.gnome.org/privacy-policy");
-
- return url;
-}
-
static void
update_lock_screen_sensitivity (CcPrivacyPanel *self)
{
@@ -1269,7 +1188,9 @@ static void
add_abrt (CcPrivacyPanel *self)
{
GtkWidget *w;
- char *os_name, *url, *msg;
+ g_autofree gchar *os_name = NULL;
+ g_autofree gchar *url = NULL;
+ char *msg;
w = get_abrt_label (self->privacy_settings, REPORT_TECHNICAL_PROBLEMS);
gtk_widget_show (w);
@@ -1283,22 +1204,19 @@ add_abrt (CcPrivacyPanel *self)
self->abrt_switch, "active",
G_SETTINGS_BIND_DEFAULT);
- os_name = get_os_name ();
+ os_name = cc_os_release_get_value ("NAME");
+ if (os_name == NULL)
+ os_name = g_strdup ("GNOME");
/* translators: '%s' is the distributor's name, such as 'Fedora' */
msg = g_strdup_printf (_("Sending reports of technical problems helps us improve %s. Reports are sent
anonymously and are scrubbed of personal data."),
os_name);
- g_free (os_name);
gtk_label_set_text (self->abrt_explanation_label, msg);
g_free (msg);
- url = get_privacy_policy_url ();
- if (!url)
- {
- g_debug ("Not watching for ABRT appearing, /etc/os-release lacks a privacy policy URL");
- return;
- }
+ url = cc_os_release_get_value ("PRIVACY_POLICY_URL");
+ if (url == NULL)
+ url = g_strdup ("http://www.gnome.org/privacy-policy");
msg = g_strdup_printf ("<a href=\"%s\">%s</a>", url, _("Privacy Policy"));
- g_free (url);
gtk_label_set_markup (self->abrt_policy_link_label, msg);
g_free (msg);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]