[glib/portal: 9/17] Move some portal support to its own files



commit de5b18dfc38fa2105d5ba6057e5bfa8e5a0fadd8
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Jun 21 23:15:25 2016 -0400

    Move some portal support to its own files
    
    This is better than copying these utility functions around to
    multiple places.

 gio/Makefile.am             |    2 +
 gio/gappinfo.c              |   23 +-------------
 gio/gnetworkmonitorportal.c |   45 ++--------------------------
 gio/gportalsupport.c        |   69 +++++++++++++++++++++++++++++++++++++++++++
 gio/gportalsupport.h        |   30 ++++++++++++++++++
 5 files changed, 106 insertions(+), 63 deletions(-)
---
diff --git a/gio/Makefile.am b/gio/Makefile.am
index 58d51b8..dd50399 100644
--- a/gio/Makefile.am
+++ b/gio/Makefile.am
@@ -354,6 +354,8 @@ $(xdp_dbus_built_sources) : $(srcdir)/org.freedesktop.portal.NetworkMonitor.xml
                $(NULL)
 
 portal_sources = \
+       gportalsupport.c                \
+       gportalsupport.h                \
        gnetworkmonitorportal.c         \
        gnetworkmonitorportal.h         \
        $(xdp_dbus_built_sources)       \
diff --git a/gio/gappinfo.c b/gio/gappinfo.c
index 963607b..0ea8051 100644
--- a/gio/gappinfo.c
+++ b/gio/gappinfo.c
@@ -31,6 +31,7 @@
 #include "glibintl.h"
 #include <gioerror.h>
 #include <gfile.h>
+#include "gportalsupport.h"
 
 
 /**
@@ -670,26 +671,6 @@ g_app_info_should_show (GAppInfo *appinfo)
 }
 
 static gboolean
-should_use_portal (void)
-{
-  const char *use_portal;
-  char *path;
-
-  path = g_strdup_printf ("/run/user/%d/flatpak-info", getuid());
-  if (g_file_test (path, G_FILE_TEST_EXISTS))
-    use_portal = "1";
-  else
-    {
-      use_portal = g_getenv ("GTK_USE_PORTAL");
-      if (!use_portal)
-        use_portal = "";
-    }
-  g_free (path);
-
-  return g_str_equal (use_portal, "1");
-}
-
-static gboolean
 launch_default_with_portal (const char         *uri,
                             GAppLaunchContext  *context,
                             GError            **error)
@@ -754,7 +735,7 @@ g_app_info_launch_default_for_uri (const char         *uri,
   GList l;
   gboolean res;
 
-  if (should_use_portal ())
+  if (glib_should_use_portal ())
     return launch_default_with_portal (uri, launch_context, error);
 
   /* g_file_query_default_handler() calls
diff --git a/gio/gnetworkmonitorportal.c b/gio/gnetworkmonitorportal.c
index 655f787..d56c7fb 100644
--- a/gio/gnetworkmonitorportal.c
+++ b/gio/gnetworkmonitorportal.c
@@ -23,6 +23,7 @@
 #include "giomodule-priv.h"
 #include "gnetworkmonitor.h"
 #include "xdp-dbus.h"
+#include "gportalsupport.h"
 
 
 static void g_network_monitor_portal_iface_init (GNetworkMonitorInterface *iface);
@@ -104,46 +105,6 @@ proxy_changed (XdpNetworkMonitor     *proxy,
     g_signal_emit_by_name (nm, "network-changed", available);
 }
 
-
-static gboolean
-should_use_portal (void)
-{
-  const char *use_portal;
-  char *path;
-
-  path = g_strdup_printf ("/run/user/%d/flatpak-info", getuid());
-  if (g_file_test (path, G_FILE_TEST_EXISTS))
-    use_portal = "1";
-  else
-    {
-      use_portal = g_getenv ("GTK_USE_PORTAL");
-      if (!use_portal)
-        use_portal = "";
-    }
-  g_free (path);
-
-  return g_str_equal (use_portal, "1");
-}
-
-static gboolean
-network_available_in_sandbox (void)
-{
-  char *path;
-  g_autoptr(GKeyFile) keyfile = g_key_file_new ();
-
-  path = g_strdup_printf ("/run/user/%d/flatpak-info", getuid());
-  if (g_key_file_load_from_file (keyfile, path, G_KEY_FILE_NONE, NULL))
-    {
-      g_auto(GStrv) shared = NULL;
-
-      shared = g_key_file_get_string_list (keyfile, "Context", "shared", NULL, NULL);
-
-      return g_strv_contains ((const char * const *)shared, "network");
-    }
-
-  return TRUE;
-}
-
 static gboolean
 g_network_monitor_portal_initable_init (GInitable     *initable,
                                         GCancellable  *cancellable,
@@ -153,7 +114,7 @@ g_network_monitor_portal_initable_init (GInitable     *initable,
   XdpNetworkMonitor *proxy;
   gchar *name_owner = NULL;
 
-  if (!should_use_portal ())
+  if (!glib_should_use_portal ())
     {
       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Not using portals");
       return FALSE;
@@ -181,7 +142,7 @@ g_network_monitor_portal_initable_init (GInitable     *initable,
 
   g_signal_connect (G_OBJECT (proxy), "changed", G_CALLBACK (proxy_changed), nm);
   nm->priv->proxy = proxy;
-  nm->priv->network_available = network_available_in_sandbox ();
+  nm->priv->network_available = glib_network_available_in_sandbox ();
 
   return TRUE;
 }
diff --git a/gio/gportalsupport.c b/gio/gportalsupport.c
new file mode 100644
index 0000000..d7ba523
--- /dev/null
+++ b/gio/gportalsupport.c
@@ -0,0 +1,69 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright 2016 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <unistd.h>
+
+#include "gportalsupport.h"
+
+gboolean
+glib_should_use_portal (void)
+{
+  const char *use_portal;
+  char *path;
+
+  path = g_strdup_printf ("/run/user/%d/flatpak-info", getuid());
+  if (g_file_test (path, G_FILE_TEST_EXISTS))
+    use_portal = "1";
+  else
+    {
+      use_portal = g_getenv ("GTK_USE_PORTAL");
+      if (!use_portal)
+        use_portal = "";
+    }
+  g_free (path);
+
+  return g_str_equal (use_portal, "1");
+}
+
+gboolean
+glib_network_available_in_sandbox (void)
+{
+  char *path;
+  GKeyFile *keyfile;
+  gboolean available = TRUE;
+
+  path = g_strdup_printf ("/run/user/%d/flatpak-info", getuid());
+
+  keyfile = g_key_file_new ();
+  if (g_key_file_load_from_file (keyfile, path, G_KEY_FILE_NONE, NULL))
+    {
+      char **shared = NULL;
+
+      shared = g_key_file_get_string_list (keyfile, "Context", "shared", NULL, NULL);
+      available = g_strv_contains ((const char * const *)shared, "network");
+      g_strfreev (shared);
+    }
+
+  g_key_file_free (keyfile);
+  g_free (path);
+
+  return available;
+}
+
diff --git a/gio/gportalsupport.h b/gio/gportalsupport.h
new file mode 100644
index 0000000..a92e07c
--- /dev/null
+++ b/gio/gportalsupport.h
@@ -0,0 +1,30 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright 2016 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __G_PORTAL_SUPPORT_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+gboolean glib_should_use_portal (void);
+gboolean glib_network_available_in_sandbox (void);
+
+G_END_DECLS
+
+#endif


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