[gnome-software] Add a plugin helper for checking the distro-id



commit 438e9ddff0e99e885a7e59319f1139ed1bb87895
Author: Richard Hughes <richard hughsie com>
Date:   Sat Feb 1 20:33:22 2014 +0000

    Add a plugin helper for checking the distro-id

 src/gs-plugin.c                               |   34 +++++++++++++++++++++++++
 src/gs-plugin.h                               |    2 +
 src/plugins/gs-plugin-fedora-tagger-ratings.c |   25 ++----------------
 src/plugins/gs-plugin-fedora-tagger-usage.c   |   30 +++------------------
 4 files changed, 44 insertions(+), 47 deletions(-)
---
diff --git a/src/gs-plugin.c b/src/gs-plugin.c
index adcf67a..b602313 100644
--- a/src/gs-plugin.c
+++ b/src/gs-plugin.c
@@ -25,6 +25,8 @@
 
 #include "gs-plugin.h"
 
+#define GS_PLUGIN_OS_RELEASE_FN                "/etc/os-release"
+
 /**
  * gs_plugin_status_to_string:
  */
@@ -58,6 +60,38 @@ gs_plugin_set_enabled (GsPlugin *plugin, gboolean enabled)
 }
 
 /**
+ * gs_plugin_check_distro_id:
+ **/
+gboolean
+gs_plugin_check_distro_id (GsPlugin *plugin, const gchar *distro_id)
+{
+       GError *error = NULL;
+       gboolean ret;
+       gchar *data = NULL;
+       gchar *search = NULL;
+
+       /* check that we are running on Fedora */
+       ret = g_file_get_contents (GS_PLUGIN_OS_RELEASE_FN,
+                                  &data, NULL, &error);
+       if (!ret) {
+               g_warning ("%s could not be read: %s",
+                          GS_PLUGIN_OS_RELEASE_FN,
+                          error->message);
+               g_error_free (error);
+               goto out;
+       }
+       search = g_strdup_printf ("ID=%s\n", distro_id);
+       if (g_strstr_len (data, -1, search) == NULL) {
+               ret = FALSE;
+               goto out;
+       }
+out:
+       g_free (data);
+       g_free (search);
+       return ret;
+}
+
+/**
  * gs_plugin_add_app:
  **/
 void
diff --git a/src/gs-plugin.h b/src/gs-plugin.h
index ae28de3..fb663fe 100644
--- a/src/gs-plugin.h
+++ b/src/gs-plugin.h
@@ -148,6 +148,8 @@ void                 gs_plugin_initialize                   (GsPlugin       *plugin);
 void            gs_plugin_destroy                      (GsPlugin       *plugin);
 void            gs_plugin_set_enabled                  (GsPlugin       *plugin,
                                                         gboolean        enabled);
+gboolean        gs_plugin_check_distro_id              (GsPlugin       *plugin,
+                                                        const gchar    *distro_id);
 void            gs_plugin_add_app                      (GList          **list,
                                                         GsApp          *app);
 void            gs_plugin_list_free                    (GList          *list);
diff --git a/src/plugins/gs-plugin-fedora-tagger-ratings.c b/src/plugins/gs-plugin-fedora-tagger-ratings.c
index 772e9ff..d73d3e6 100644
--- a/src/plugins/gs-plugin-fedora-tagger-ratings.c
+++ b/src/plugins/gs-plugin-fedora-tagger-ratings.c
@@ -45,7 +45,6 @@ gs_plugin_get_name (void)
        return "fedora-tagger-ratings";
 }
 
-#define GS_PLUGIN_FEDORA_TAGGER_OS_RELEASE_FN  "/etc/os-release"
 #define GS_PLUGIN_FEDORA_TAGGER_SERVER         "https://apps.fedoraproject.org/tagger";
 
 /* 3 months */
@@ -57,10 +56,6 @@ gs_plugin_get_name (void)
 void
 gs_plugin_initialize (GsPlugin *plugin)
 {
-       GError *error = NULL;
-       gboolean ret;
-       gchar *data = NULL;
-
        plugin->priv = GS_PLUGIN_GET_PRIVATE (GsPluginPrivate);
        plugin->priv->db_path = g_build_filename (g_get_home_dir (),
                                                  ".local",
@@ -70,25 +65,11 @@ gs_plugin_initialize (GsPlugin *plugin)
                                                  NULL);
 
        /* check that we are running on Fedora */
-       ret = g_file_get_contents (GS_PLUGIN_FEDORA_TAGGER_OS_RELEASE_FN,
-                                  &data, NULL, &error);
-       if (!ret) {
-               gs_plugin_set_enabled (plugin, FALSE);
-               g_warning ("disabling '%s' as %s could not be read: %s",
-                          plugin->name,
-                          GS_PLUGIN_FEDORA_TAGGER_OS_RELEASE_FN,
-                          error->message);
-               g_error_free (error);
-               goto out;
-       }
-       if (g_strstr_len (data, -1, "ID=fedora\n") == NULL) {
+       if (!gs_plugin_check_distro_id (plugin, "fedora")) {
                gs_plugin_set_enabled (plugin, FALSE);
-               g_debug ("disabling '%s' as %s suggests we're not Fedora",
-                        plugin->name, GS_PLUGIN_FEDORA_TAGGER_OS_RELEASE_FN);
-               goto out;
+               g_debug ("disabling '%s' as we're not Fedora", plugin->name);
+               return;
        }
-out:
-       g_free (data);
 }
 
 /**
diff --git a/src/plugins/gs-plugin-fedora-tagger-usage.c b/src/plugins/gs-plugin-fedora-tagger-usage.c
index 907d18f..345e9fc 100644
--- a/src/plugins/gs-plugin-fedora-tagger-usage.c
+++ b/src/plugins/gs-plugin-fedora-tagger-usage.c
@@ -42,7 +42,6 @@ gs_plugin_get_name (void)
        return "fedora-tagger-usage";
 }
 
-#define GS_PLUGIN_FEDORA_TAGGER_OS_RELEASE_FN  "/etc/os-release"
 #define GS_PLUGIN_FEDORA_TAGGER_SERVER         "https://apps.fedoraproject.org/tagger";
 
 /**
@@ -51,41 +50,22 @@ gs_plugin_get_name (void)
 void
 gs_plugin_initialize (GsPlugin *plugin)
 {
-       GError *error = NULL;
-       gboolean ret;
-       gchar *data = NULL;
-
        plugin->priv = GS_PLUGIN_GET_PRIVATE (GsPluginPrivate);
 
        /* this is opt-in, and turned off by default */
-       ret = g_settings_get_boolean (plugin->settings, "enable-usage");
-       if (!ret) {
+       if (!g_settings_get_boolean (plugin->settings, "enable-usage")) {
                gs_plugin_set_enabled (plugin, FALSE);
                g_debug ("disabling '%s' as 'enable-usage' disabled in GSettings",
                         plugin->name);
-               goto out;
+               return;
        }
 
        /* check that we are running on Fedora */
-       ret = g_file_get_contents (GS_PLUGIN_FEDORA_TAGGER_OS_RELEASE_FN,
-                                  &data, NULL, &error);
-       if (!ret) {
+       if (!gs_plugin_check_distro_id (plugin, "fedora")) {
                gs_plugin_set_enabled (plugin, FALSE);
-               g_warning ("disabling '%s' as %s could not be read: %s",
-                          plugin->name,
-                          GS_PLUGIN_FEDORA_TAGGER_OS_RELEASE_FN,
-                          error->message);
-               g_error_free (error);
-               goto out;
+               g_debug ("disabling '%s' as we're not Fedora", plugin->name);
+               return;
        }
-       if (g_strstr_len (data, -1, "ID=fedora\n") == NULL) {
-               gs_plugin_set_enabled (plugin, FALSE);
-               g_debug ("disabling '%s' as %s suggests we're not Fedora",
-                        plugin->name, GS_PLUGIN_FEDORA_TAGGER_OS_RELEASE_FN);
-               goto out;
-       }
-out:
-       g_free (data);
 }
 
 /**


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