[gnome-flashback] desktop: add gf_launch_desktop_file and gf_launch_uri



commit 663e87d6e4544a59a829055278de168287f1f853
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Mon Nov 11 17:37:07 2019 +0200

    desktop: add gf_launch_desktop_file and gf_launch_uri

 configure.ac                          |   2 +
 gnome-flashback/libdesktop/gf-utils.c | 139 ++++++++++++++++++++++++++++++++++
 gnome-flashback/libdesktop/gf-utils.h |  10 ++-
 3 files changed, 149 insertions(+), 2 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index e70b672..bc27f10 100644
--- a/configure.ac
+++ b/configure.ac
@@ -154,8 +154,10 @@ PKG_CHECK_MODULES([COMMON], [
 PKG_CHECK_MODULES([DESKTOP], [
   glib-2.0 >= $GLIB_REQUIRED
   gio-2.0 >= $GLIB_REQUIRED
+  gio-unix-2.0 >= $GLIB_REQUIRED
   gnome-desktop-3.0 >= $LIBGNOME_DESKTOP_REQUIRED
   gtk+-3.0 >= $GTK_REQUIRED
+  libsystemd
 ])
 
 PKG_CHECK_MODULES([END_SESSION_DIALOG], [
diff --git a/gnome-flashback/libdesktop/gf-utils.c b/gnome-flashback/libdesktop/gf-utils.c
index 4f0530f..a25b183 100644
--- a/gnome-flashback/libdesktop/gf-utils.c
+++ b/gnome-flashback/libdesktop/gf-utils.c
@@ -18,6 +18,94 @@
 #include "config.h"
 #include "gf-utils.h"
 
+#include <gio/gdesktopappinfo.h>
+#include <systemd/sd-journal.h>
+
+static void
+child_setup (gpointer user_data)
+{
+  GAppInfo *app_info;
+  const gchar *id;
+  gint stdout_fd;
+  gint stderr_fd;
+
+  app_info = G_APP_INFO (user_data);
+  id = g_app_info_get_id (app_info);
+
+  stdout_fd = sd_journal_stream_fd (id, LOG_INFO, FALSE);
+  if (stdout_fd >= 0)
+    {
+      dup2 (stdout_fd, STDOUT_FILENO);
+      close (stdout_fd);
+    }
+
+  stderr_fd = sd_journal_stream_fd (id, LOG_WARNING, FALSE);
+  if (stderr_fd >= 0)
+    {
+      dup2 (stderr_fd, STDERR_FILENO);
+      close (stderr_fd);
+    }
+}
+
+static void
+close_pid (GPid     pid,
+           gint     status,
+           gpointer user_data)
+{
+  g_spawn_close_pid (pid);
+}
+
+static void
+pid_cb (GDesktopAppInfo *app_info,
+        GPid             pid,
+        gpointer         user_data)
+{
+  g_child_watch_add (pid, close_pid, NULL);
+}
+
+static gboolean
+app_info_launch_uris (GDesktopAppInfo  *app_info,
+                      GList            *uris,
+                      GError          **error)
+{
+  GSpawnFlags flags;
+  gboolean ret;
+
+  flags = G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD;
+  ret = g_desktop_app_info_launch_uris_as_manager (app_info, uris, NULL,
+                                                   flags, child_setup, app_info,
+                                                   pid_cb, NULL,
+                                                   error);
+
+  return ret;
+}
+
+static GAppInfo *
+get_app_info_for_uri (const gchar  *uri,
+                      GError      **error)
+{
+  GAppInfo *app_info;
+  gchar *scheme;
+  GFile *file;
+
+  app_info = NULL;
+  scheme = g_uri_parse_scheme (uri);
+
+  if (scheme && scheme[0] != '\0')
+    app_info = g_app_info_get_default_for_uri_scheme (scheme);
+
+  g_free (scheme);
+
+  if (app_info != NULL)
+    return app_info;
+
+  file = g_file_new_for_uri (uri);
+  app_info = g_file_query_default_handler (file, NULL, error);
+  g_object_unref (file);
+
+  return app_info;
+}
+
 char *
 gf_build_attributes_list (const char *first,
                           ...)
@@ -36,3 +124,54 @@ gf_build_attributes_list (const char *first,
 
   return g_string_free (attributes, FALSE);
 }
+
+gboolean
+gf_launch_desktop_file (const char  *desktop_file,
+                        GError     **error)
+{
+  GDesktopAppInfo *app_info;
+
+  app_info = g_desktop_app_info_new (desktop_file);
+
+  if (app_info == NULL)
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                   "Desktop file `%s` does not exists",
+                   desktop_file);
+
+      return FALSE;
+    }
+
+  if (!app_info_launch_uris (app_info, NULL, error))
+    {
+      g_object_unref (app_info);
+
+      return FALSE;
+    }
+
+  g_object_unref (app_info);
+
+  return TRUE;
+}
+
+gboolean
+gf_launch_uri (const char  *uri,
+               GError     **error)
+{
+  GAppInfo *app_info;
+  GList *uris;
+  gboolean launched;
+
+  app_info = get_app_info_for_uri (uri, error);
+
+  if (app_info == NULL)
+    return FALSE;
+
+  uris = g_list_append (NULL, (gchar *) uri);
+  launched = app_info_launch_uris (G_DESKTOP_APP_INFO (app_info), uris, error);
+
+  g_object_unref (app_info);
+  g_list_free (uris);
+
+  return launched;
+}
diff --git a/gnome-flashback/libdesktop/gf-utils.h b/gnome-flashback/libdesktop/gf-utils.h
index 5bb934b..f79334b 100644
--- a/gnome-flashback/libdesktop/gf-utils.h
+++ b/gnome-flashback/libdesktop/gf-utils.h
@@ -22,8 +22,14 @@
 
 G_BEGIN_DECLS
 
-char *gf_build_attributes_list (const char *first,
-                                ...);
+char     *gf_build_attributes_list (const char  *first,
+                                     ...);
+
+gboolean  gf_launch_desktop_file   (const char  *desktop_file,
+                                    GError     **error);
+
+gboolean  gf_launch_uri            (const char  *uri,
+                                    GError     **error);
 
 G_END_DECLS
 


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