[gnome-software/wip/rancell/ubuntu-3-20-1: 13/72] trivial: Factor out gs_utils_get_content_type()



commit 7ca7248ab15c3aa3a7a87af29ccb0dfb05711b6a
Author: Richard Hughes <richard hughsie com>
Date:   Thu Apr 7 08:51:26 2016 +0100

    trivial: Factor out gs_utils_get_content_type()

 src/gs-utils.c                             |   27 +++++++++++++
 src/gs-utils.h                             |    3 +
 src/plugins/gs-plugin-fwupd.c              |   51 +++---------------------
 src/plugins/gs-plugin-packagekit-refresh.c |   59 +++++----------------------
 src/plugins/gs-plugin-xdg-app.c            |   52 +++---------------------
 5 files changed, 55 insertions(+), 137 deletions(-)
---
diff --git a/src/gs-utils.c b/src/gs-utils.c
index fbdb1a6..4cd1799 100644
--- a/src/gs-utils.c
+++ b/src/gs-utils.c
@@ -490,4 +490,31 @@ gs_utils_get_permission (const gchar *id)
 #endif
 }
 
+/**
+ * gs_utils_get_content_type:
+ */
+gchar *
+gs_utils_get_content_type (const gchar *filename,
+                          GCancellable *cancellable,
+                          GError **error)
+{
+       const gchar *tmp;
+       g_autoptr(GFile) file = NULL;
+       g_autoptr(GFileInfo) info = NULL;
+
+       /* get content type */
+       file = g_file_new_for_path (filename);
+       info = g_file_query_info (file,
+                                 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
+                                 G_FILE_QUERY_INFO_NONE,
+                                 cancellable,
+                                 error);
+       if (info == NULL)
+               return NULL;
+       tmp = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
+       if (tmp == NULL)
+               return NULL;
+       return g_strdup (tmp);
+}
+
 /* vim: set noexpandtab: */
diff --git a/src/gs-utils.h b/src/gs-utils.h
index f65e6e5..561054c 100644
--- a/src/gs-utils.h
+++ b/src/gs-utils.h
@@ -35,6 +35,9 @@ void   gs_container_remove_all        (GtkContainer   *container);
 void    gs_grab_focus_when_mapped      (GtkWidget      *widget);
 
 guint   gs_utils_get_file_age          (GFile          *file);
+gchar  *gs_utils_get_content_type      (const gchar    *filename,
+                                        GCancellable   *cancellable,
+                                        GError         **error);
 
 void    gs_app_notify_installed        (GsApp          *app);
 void    gs_app_notify_failed_modal     (GsApp          *app,
diff --git a/src/plugins/gs-plugin-fwupd.c b/src/plugins/gs-plugin-fwupd.c
index 5fb858a..e7a84b1 100644
--- a/src/plugins/gs-plugin-fwupd.c
+++ b/src/plugins/gs-plugin-fwupd.c
@@ -1039,45 +1039,6 @@ gs_plugin_app_update (GsPlugin *plugin,
 }
 
 /**
- * gs_plugin_fwupd_content_type_matches:
- */
-static gboolean
-gs_plugin_fwupd_content_type_matches (const gchar *filename,
-                                     gboolean *matches,
-                                     GCancellable *cancellable,
-                                     GError **error)
-{
-       const gchar *tmp;
-       guint i;
-       g_autoptr(GFile) file = NULL;
-       g_autoptr(GFileInfo) info = NULL;
-       const gchar *mimetypes[] = {
-               "application/vnd.ms-cab-compressed",
-               NULL };
-
-       /* get content type */
-       file = g_file_new_for_path (filename);
-       info = g_file_query_info (file,
-                                 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
-                                 G_FILE_QUERY_INFO_NONE,
-                                 cancellable,
-                                 error);
-       if (info == NULL)
-               return FALSE;
-       tmp = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
-
-       /* match any */
-       *matches = FALSE;
-       for (i = 0; mimetypes[i] != NULL; i++) {
-               if (g_strcmp0 (tmp, mimetypes[i]) == 0) {
-                       *matches = TRUE;
-                       break;
-               }
-       }
-       return TRUE;
-}
-
-/**
  * gs_plugin_filename_to_app:
  */
 gboolean
@@ -1093,6 +1054,7 @@ gs_plugin_filename_to_app (GsPlugin *plugin,
        GVariant *variant;
        const gchar *key;
        gboolean supported;
+       g_autofree gchar *content_type = NULL;
        gint fd;
        gint retval;
        g_autoptr(AsIcon) icon = NULL;
@@ -1102,14 +1064,15 @@ gs_plugin_filename_to_app (GsPlugin *plugin,
        g_autoptr(GsApp) app = NULL;
        g_autoptr(GUnixFDList) fd_list = NULL;
        g_autoptr(GVariantIter) iter = NULL;
+       const gchar *mimetypes[] = {
+               "application/vnd.ms-cab-compressed",
+               NULL };
 
        /* does this match any of the mimetypes we support */
-       if (!gs_plugin_fwupd_content_type_matches (filename,
-                                                  &supported,
-                                                  cancellable,
-                                                  error))
+       content_type = gs_utils_get_content_type (filename, cancellable, error);
+       if (content_type == NULL)
                return FALSE;
-       if (!supported)
+       if (!g_strv_contains (mimetypes, content_type))
                return TRUE;
 
        /* get request */
diff --git a/src/plugins/gs-plugin-packagekit-refresh.c b/src/plugins/gs-plugin-packagekit-refresh.c
index 551ae18..5d3de2e 100644
--- a/src/plugins/gs-plugin-packagekit-refresh.c
+++ b/src/plugins/gs-plugin-packagekit-refresh.c
@@ -26,6 +26,7 @@
 #include <glib/gi18n.h>
 
 #include <gs-plugin.h>
+#include <gs-utils.h>
 
 #include "packagekit-common.h"
 
@@ -197,48 +198,6 @@ gs_plugin_packagekit_refresh_set_text (GsApp *app, const gchar *text)
 }
 
 /**
- * gs_plugin_packagekit_refresh_content_type_matches:
- */
-static gboolean
-gs_plugin_packagekit_refresh_content_type_matches (const gchar *filename,
-                                                  gboolean *matches,
-                                                  GCancellable *cancellable,
-                                                  GError **error)
-{
-       const gchar *tmp;
-       guint i;
-       g_autoptr(GFile) file = NULL;
-       g_autoptr(GFileInfo) info = NULL;
-       const gchar *mimetypes[] = {
-               "application/x-app-package",
-               "application/x-deb",
-               "application/x-redhat-package-manager",
-               "application/x-rpm",
-               NULL };
-
-       /* get content type */
-       file = g_file_new_for_path (filename);
-       info = g_file_query_info (file,
-                                 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
-                                 G_FILE_QUERY_INFO_NONE,
-                                 cancellable,
-                                 error);
-       if (info == NULL)
-               return FALSE;
-       tmp = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
-
-       /* match any */
-       *matches = FALSE;
-       for (i = 0; mimetypes[i] != NULL; i++) {
-               if (g_strcmp0 (tmp, mimetypes[i]) == 0) {
-                       *matches = TRUE;
-                       break;
-               }
-       }
-       return TRUE;
-}
-
-/**
  * gs_plugin_packagekit_refresh_guess_app_id:
  */
 static gboolean
@@ -311,24 +270,28 @@ gs_plugin_filename_to_app (GsPlugin *plugin,
                           GError **error)
 {
        const gchar *package_id;
-       gboolean supported;
        PkDetails *item;
        ProgressData data;
        g_autoptr (PkResults) results = NULL;
        g_autofree gchar *basename = NULL;
+       g_autofree gchar *content_type = NULL;
        g_autofree gchar *license_spdx = NULL;
        g_auto(GStrv) files = NULL;
        g_auto(GStrv) split = NULL;
        g_autoptr(GPtrArray) array = NULL;
        g_autoptr(GsApp) app = NULL;
+       const gchar *mimetypes[] = {
+               "application/x-app-package",
+               "application/x-deb",
+               "application/x-redhat-package-manager",
+               "application/x-rpm",
+               NULL };
 
        /* does this match any of the mimetypes we support */
-       if (!gs_plugin_packagekit_refresh_content_type_matches (filename,
-                                                               &supported,
-                                                               cancellable,
-                                                               error))
+       content_type = gs_utils_get_content_type (filename, cancellable, error);
+       if (content_type == NULL)
                return FALSE;
-       if (!supported)
+       if (!g_strv_contains (mimetypes, content_type))
                return TRUE;
 
        data.plugin = plugin;
diff --git a/src/plugins/gs-plugin-xdg-app.c b/src/plugins/gs-plugin-xdg-app.c
index e61e92a..e69ceb2 100644
--- a/src/plugins/gs-plugin-xdg-app.c
+++ b/src/plugins/gs-plugin-xdg-app.c
@@ -1401,45 +1401,6 @@ gs_plugin_app_update (GsPlugin *plugin,
 }
 
 /**
- * gs_plugin_xdg_app_content_type_matches:
- */
-static gboolean
-gs_plugin_xdg_app_content_type_matches (const gchar *filename,
-                                     gboolean *matches,
-                                     GCancellable *cancellable,
-                                     GError **error)
-{
-       const gchar *tmp;
-       guint i;
-       g_autoptr(GFile) file = NULL;
-       g_autoptr(GFileInfo) info = NULL;
-       const gchar *mimetypes[] = {
-               "application/vnd.xdgapp",
-               NULL };
-
-       /* get content type */
-       file = g_file_new_for_path (filename);
-       info = g_file_query_info (file,
-                                 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
-                                 G_FILE_QUERY_INFO_NONE,
-                                 cancellable,
-                                 error);
-       if (info == NULL)
-               return FALSE;
-       tmp = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
-
-       /* match any */
-       *matches = FALSE;
-       for (i = 0; mimetypes[i] != NULL; i++) {
-               if (g_strcmp0 (tmp, mimetypes[i]) == 0) {
-                       *matches = TRUE;
-                       break;
-               }
-       }
-       return TRUE;
-}
-
-/**
  * gs_plugin_filename_to_app:
  */
 gboolean
@@ -1449,7 +1410,7 @@ gs_plugin_filename_to_app (GsPlugin *plugin,
                           GCancellable *cancellable,
                           GError **error)
 {
-       gboolean supported;
+       g_autofree gchar *content_type = NULL;
        g_autofree gchar *id_prefixed = NULL;
        g_autoptr(GBytes) appstream_gz = NULL;
        g_autoptr(GBytes) icon_data = NULL;
@@ -1457,14 +1418,15 @@ gs_plugin_filename_to_app (GsPlugin *plugin,
        g_autoptr(GFile) file = NULL;
        g_autoptr(GsApp) app = NULL;
        g_autoptr(XdgAppBundleRef) xref_bundle = NULL;
+       const gchar *mimetypes[] = {
+               "application/vnd.xdgapp",
+               NULL };
 
        /* does this match any of the mimetypes we support */
-       if (!gs_plugin_xdg_app_content_type_matches (filename,
-                                                    &supported,
-                                                    cancellable,
-                                                    error))
+       content_type = gs_utils_get_content_type (filename, cancellable, error);
+       if (content_type == NULL)
                return FALSE;
-       if (!supported)
+       if (!g_strv_contains (mimetypes, content_type))
                return TRUE;
 
        /* load bundle */


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