[gnome-software] trivial: Abstract out the URL splitting functions for future use
- From: Richard Hughes <rhughes src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-software] trivial: Abstract out the URL splitting functions for future use
- Date: Thu, 2 Feb 2017 12:50:56 +0000 (UTC)
commit f34d24433ef016d5b32a1db889d49769608323e1
Author: Richard Hughes <richard hughsie com>
Date: Thu Feb 2 10:11:54 2017 +0000
trivial: Abstract out the URL splitting functions for future use
src/gs-application.c | 29 ++++++++---------------
src/gs-self-test.c | 23 ++++++++++++++++++
src/gs-utils.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++
src/gs-utils.h | 3 ++
4 files changed, 97 insertions(+), 19 deletions(-)
---
diff --git a/src/gs-application.c b/src/gs-application.c
index fb8e2c3..43d8fe7 100644
--- a/src/gs-application.c
+++ b/src/gs-application.c
@@ -913,34 +913,25 @@ gs_application_open (GApplication *application,
for (i = 0; i < n_files; i++) {
g_autofree gchar *str = g_file_get_uri (files[i]);
- g_autoptr(SoupURI) uri = NULL;
- const gchar *host;
- const gchar *path;
+ g_autofree gchar *scheme = NULL;
+ g_autofree gchar *path = NULL;
- uri = soup_uri_new (str);
- if (!SOUP_URI_IS_VALID (uri))
+ scheme = gs_utils_get_url_scheme (str);
+ if (scheme == NULL)
+ continue;
+ path = gs_utils_get_url_path (str);
+ if (path == NULL)
continue;
- /* foo://bar -> scheme: foo, host: bar, path: / */
- /* foo:bar -> scheme: foo, host: (empty string), path: /bar */
- host = soup_uri_get_host (uri);
- path = soup_uri_get_path (uri);
- if (host != NULL && (strlen (host) > 0))
- path = host;
-
- /* trim any leading slashes */
- while (*path == '/')
- path++;
-
- if (g_strcmp0 (soup_uri_get_scheme (uri), "appstream") == 0) {
+ if (g_strcmp0 (scheme, "appstream") == 0) {
g_action_group_activate_action (G_ACTION_GROUP (app),
"details",
g_variant_new ("(ss)", path, ""));
- } else if (g_strcmp0 (soup_uri_get_scheme (uri), "apt") == 0) {
+ } else if (g_strcmp0 (scheme, "apt") == 0) {
g_action_group_activate_action (G_ACTION_GROUP (app),
"details-pkg",
g_variant_new ("(ss)", path, "apt"));
- } else if (g_strcmp0 (soup_uri_get_scheme (uri), "snap") == 0) {
+ } else if (g_strcmp0 (scheme, "snap") == 0) {
g_action_group_activate_action (G_ACTION_GROUP (app),
"details-pkg",
g_variant_new ("(ss)", path, "snap"));
diff --git a/src/gs-self-test.c b/src/gs-self-test.c
index 3383066..8a72ece 100644
--- a/src/gs-self-test.c
+++ b/src/gs-self-test.c
@@ -58,6 +58,28 @@ gs_app_list_filter_cb (GsApp *app, gpointer user_data)
}
static void
+gs_utils_url_func (void)
+{
+ g_autofree gchar *path1 = NULL;
+ g_autofree gchar *path2 = NULL;
+ g_autofree gchar *path3 = NULL;
+ g_autofree gchar *scheme1 = NULL;
+ g_autofree gchar *scheme2 = NULL;
+
+ scheme1 = gs_utils_get_url_scheme ("appstream://gimp.desktop");
+ g_assert_cmpstr (scheme1, ==, "appstream");
+ scheme1 = gs_utils_get_url_scheme ("appstream:gimp.desktop");
+ g_assert_cmpstr (scheme1, ==, "appstream");
+
+ path1 = gs_utils_get_url_path ("appstream://gimp.desktop");
+ g_assert_cmpstr (path1, ==, "gimp.desktop");
+ path2 = gs_utils_get_url_path ("appstream:gimp.desktop");
+ g_assert_cmpstr (path2, ==, "gimp.desktop");
+ path3 = gs_utils_get_url_path ("apt:/gimp");
+ g_assert_cmpstr (path3, ==, "gimp");
+}
+
+static void
gs_utils_wilson_func (void)
{
g_assert_cmpint ((gint64) gs_utils_get_wilson_rating (0, 0, 0, 0, 0), ==, -1);
@@ -1534,6 +1556,7 @@ main (int argc, char **argv)
g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
/* generic tests go here */
+ g_test_add_func ("/gnome-software/utils{url}", gs_utils_url_func);
g_test_add_func ("/gnome-software/utils{wilson}", gs_utils_wilson_func);
g_test_add_func ("/gnome-software/utils{error}", gs_utils_error_func);
g_test_add_func ("/gnome-software/os-release", gs_os_release_func);
diff --git a/src/gs-utils.c b/src/gs-utils.c
index 9c2c7e6..8d5df37 100644
--- a/src/gs-utils.c
+++ b/src/gs-utils.c
@@ -35,6 +35,7 @@
#include <errno.h>
#include <fnmatch.h>
#include <math.h>
+#include <string.h>
#include <glib/gstdio.h>
#include <json-glib/json-glib.h>
@@ -831,4 +832,64 @@ gs_utils_error_convert_appstream (GError **perror)
return TRUE;
}
+/**
+ * gs_utils_get_url_scheme:
+ * @url: A URL, e.g. "appstream://gimp.desktop"
+ *
+ * Gets the scheme from the URL string.
+ *
+ * Returns: the URL scheme, e.g. "appstream"
+ */
+gchar *
+gs_utils_get_url_scheme (const gchar *url)
+{
+ g_autoptr(SoupURI) uri = NULL;
+
+ /* no data */
+ if (url == NULL)
+ return NULL;
+
+ /* create URI from URL */
+ uri = soup_uri_new (url);
+ if (!SOUP_URI_IS_VALID (uri))
+ return NULL;
+
+ /* success */
+ return g_strdup (soup_uri_get_scheme (uri));
+}
+
+/**
+ * gs_utils_get_url_path:
+ * @url: A URL, e.g. "appstream://gimp.desktop"
+ *
+ * Gets the path from the URL string, removing any leading slashes.
+ *
+ * Returns: the URL path, e.g. "gimp.desktop"
+ */
+gchar *
+gs_utils_get_url_path (const gchar *url)
+{
+ g_autoptr(SoupURI) uri = NULL;
+ const gchar *host;
+ const gchar *path;
+
+ uri = soup_uri_new (url);
+ if (!SOUP_URI_IS_VALID (uri))
+ return NULL;
+
+ /* foo://bar -> scheme: foo, host: bar, path: / */
+ /* foo:bar -> scheme: foo, host: (empty string), path: /bar */
+ host = soup_uri_get_host (uri);
+ path = soup_uri_get_path (uri);
+ if (host != NULL && (strlen (host) > 0))
+ path = host;
+
+ /* trim any leading slashes */
+ while (*path == '/')
+ path++;
+
+ /* success */
+ return g_strdup (path);
+}
+
/* vim: set noexpandtab: */
diff --git a/src/gs-utils.h b/src/gs-utils.h
index 3a55cd3..85cd6f2 100644
--- a/src/gs-utils.h
+++ b/src/gs-utils.h
@@ -79,6 +79,9 @@ gboolean gs_utils_error_convert_gdk_pixbuf(GError **perror);
gboolean gs_utils_error_convert_json_glib (GError **perror);
gboolean gs_utils_error_convert_appstream (GError **perror);
+gchar *gs_utils_get_url_scheme (const gchar *url);
+gchar *gs_utils_get_url_path (const gchar *url);
+
G_END_DECLS
#endif /* __GS_UTILS_H */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]