[glib/wip/gapplication] Pass on the desktop file path when lauching



commit 8e699c7381e7395da681c1be3cf1b602705cac3d
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Jun 5 17:53:50 2010 -0400

    Pass on the desktop file path when lauching
    
    Set GIO_LAUNCHED_DESKTOP_FILE and GIO_LAUNCHED_DESKTOP_FILE_PID
    env vars. This will let us read useful information like icon and
    description out of the desktop file without reading all of
    /usr/share/applications.

 gio/gappinfo.c                 |   42 ++++++++++++++++++++++++---------------
 gio/gdesktopappinfo.c          |   12 +++++++++++
 gio/tests/Makefile.am          |    8 +++++++
 gio/tests/appinfo-test.c       |   20 +++++++++++++++++++
 gio/tests/appinfo-test.desktop |    4 +++
 gio/tests/appinfo.c            |   25 +++++++++++++++++++++++
 6 files changed, 95 insertions(+), 16 deletions(-)
---
diff --git a/gio/gappinfo.c b/gio/gappinfo.c
index 6feec82..38e3063 100644
--- a/gio/gappinfo.c
+++ b/gio/gappinfo.c
@@ -33,7 +33,7 @@
  * @short_description: Application information and launch contexts
  * @include: gio/gio.h
  * 
- * #GAppInfo and #GAppLaunchContext are used for describing and launching 
+ * #GAppInfo and #GAppLaunchContext are used for describing and launching
  * applications installed on the system.
  *
  * As of GLib 2.20, URIs will always be converted to POSIX paths
@@ -117,9 +117,9 @@ g_app_info_dup (GAppInfo *appinfo)
 
 /**
  * g_app_info_equal:
- * @appinfo1: the first #GAppInfo.  
+ * @appinfo1: the first #GAppInfo.
  * @appinfo2: the second #GAppInfo.
- * 
+ *
  * Checks if two #GAppInfo<!-- -->s are equal.
  *
  * Returns: %TRUE if @appinfo1 is equal to @appinfo2. %FALSE otherwise.
@@ -461,7 +461,7 @@ g_app_info_get_icon (GAppInfo *appinfo)
  * @launch_context: a #GAppLaunchContext or %NULL
  * @error: a #GError
  * 
- * Launches the application. Passes @files to the launched application 
+ * Launches the application. Passes @files to the launched application
  * as arguments, using the optional @launch_context to get information
  * about the details of the launcher (like what screen it is on).
  * On error, @error will be set accordingly.
@@ -476,8 +476,17 @@ g_app_info_get_icon (GAppInfo *appinfo)
  * unsupported uris with strange formats like mailto:), so if you have
  * a textual uri you want to pass in as argument, consider using
  * g_app_info_launch_uris() instead.
- * 
- * Returns: %TRUE on successful launch, %FALSE otherwise. 
+ *
+ * On UNIX, this function sets the <envvar>GIO_LAUNCHED_DESKTOP_FILE</envvar>
+ * environment variable with the path of the launched desktop file and
+ * <envvar>GIO_LAUNCHED_DESKTOP_FILE_PID</envvar> to the process
+ * id of the launched process. This can be used to ignore
+ * <envvar>GIO_LAUNCHED_DESKTOP_FILE</envvar>, should it be inherited
+ * by further processes. The <envvar>DISPLAY</envvar> and
+ * <envvar>DESKTOP_STARTUP_ID</envvar> environment variables are also
+ * set, based on information provided in @launch_context.
+ *
+ * Returns: %TRUE on successful launch, %FALSE otherwise.
  **/
 gboolean
 g_app_info_launch (GAppInfo           *appinfo,
@@ -540,11 +549,11 @@ g_app_info_supports_files (GAppInfo *appinfo)
 /**
  * g_app_info_launch_uris:
  * @appinfo: a #GAppInfo
- * @uris: a #GList containing URIs to launch. 
+ * @uris: a #GList containing URIs to launch.
  * @launch_context: a #GAppLaunchContext or %NULL
  * @error: a #GError
  * 
- * Launches the application. Passes @uris to the launched application 
+ * Launches the application. Passes @uris to the launched application
  * as arguments, using the optional @launch_context to get information
  * about the details of the launcher (like what screen it is on).
  * On error, @error will be set accordingly.
@@ -555,7 +564,7 @@ g_app_info_supports_files (GAppInfo *appinfo)
  * can fail to start if it runs into problems during startup. There is
  * no way to detect this.
  *
- * Returns: %TRUE on successful launch, %FALSE otherwise. 
+ * Returns: %TRUE on successful launch, %FALSE otherwise.
  **/
 gboolean
 g_app_info_launch_uris (GAppInfo           *appinfo,
@@ -600,7 +609,7 @@ g_app_info_should_show (GAppInfo *appinfo)
  * @launch_context: an optional #GAppLaunchContext.
  * @error: a #GError.
  *
- * Utility function that launches the default application 
+ * Utility function that launches the default application
  * registered to handle the specified uri. Synchronous I/O
  * is done on the uri to detect the type of the file if
  * required.
@@ -726,10 +735,10 @@ g_app_launch_context_init (GAppLaunchContext *launch_context)
  * @info: a #GAppInfo
  * @files: a #GList of #GFile objects
  *
- * Gets the display string for the display. This is used to ensure new
- * applications are started on the same display as the launching 
- * application.
- * 
+ * Gets the display string for the @context. This is used to ensure new
+ * applications are started on the same display as the launching
+ * application, by setting the <envvar>DISPLAY</envvar> environment variable.
+ *
  * Returns: a display string for the display.
  **/
 char *
@@ -757,13 +766,14 @@ g_app_launch_context_get_display (GAppLaunchContext *context,
  * @files: a #GList of of #GFile objects
  * 
  * Initiates startup notification for the application and returns the
- * DESKTOP_STARTUP_ID for the launched operation, if supported.
+ * <envvar>DESKTOP_STARTUP_ID</envvar> for the launched operation,
+ * if supported.
  *
  * Startup notification IDs are defined in the <ulink
  * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt";>
  * FreeDesktop.Org Startup Notifications standard</ulink>.
  *
- * Returns: a startup notification ID for the application, or %NULL if 
+ * Returns: a startup notification ID for the application, or %NULL if
  *     not supported.
  **/
 char *
diff --git a/gio/gdesktopappinfo.c b/gio/gdesktopappinfo.c
index ddccf34..493a3cd 100644
--- a/gio/gdesktopappinfo.c
+++ b/gio/gdesktopappinfo.c
@@ -879,6 +879,7 @@ typedef struct
 {
   char *display;
   char *sn_id;
+  char *desktop_file;
 } ChildSetupData;
 
 static void
@@ -891,6 +892,16 @@ child_setup (gpointer user_data)
 
   if (data->sn_id)
     g_setenv ("DESKTOP_STARTUP_ID", data->sn_id, TRUE);
+
+  if (data->desktop_file)
+    {
+      gchar pid[20];
+
+      g_setenv ("GIO_LAUNCHED_DESKTOP_FILE", data->desktop_file, TRUE);
+
+      g_snprintf (pid, 20, "%d", getpid ());
+      g_setenv ("GIO_LAUNCHED_DESKTOP_FILE_PID", pid, TRUE);
+    }
 }
 
 static gboolean
@@ -927,6 +938,7 @@ g_desktop_app_info_launch_uris (GAppInfo           *appinfo,
 
       data.display = NULL;
       data.sn_id = NULL;
+      data.desktop_file = info->filename;
 
       if (launch_context)
 	{
diff --git a/gio/tests/Makefile.am b/gio/tests/Makefile.am
index 5a3fe89..8fcdd9c 100644
--- a/gio/tests/Makefile.am
+++ b/gio/tests/Makefile.am
@@ -56,6 +56,7 @@ TEST_PROGS +=			\
 	gdbus-exit-on-close	\
 	application		\
 	testapps		\
+	appinfo			\
 	$(NULL)
 endif
 
@@ -76,6 +77,7 @@ SAMPLE_PROGS = 				\
 	gdbus-example-peer		\
 	gdbus-example-proxy-subclass	\
 	testapp				\
+	appinfo-test			\
 	$(NULL)
 
 
@@ -254,6 +256,12 @@ gdbus_example_export_LDADD   = $(progs_ldadd)
 application_SOURCES = application.c
 application_LDADD   = $(progs_ldadd)
 
+appinfo_SOURCES = appinfo.c
+appinfo_LDADD   = $(progs_ldadd)
+
+appinfo_test_SOURCES = appinfo-test.c
+appinfo_test_LDADD   = $(progs_ldadd)
+
 testapp_SOURCES = testapp.c
 testapp_LDADD   = $(progs_ldadd)
 
diff --git a/gio/tests/appinfo-test.c b/gio/tests/appinfo-test.c
new file mode 100644
index 0000000..9d6a5ff
--- /dev/null
+++ b/gio/tests/appinfo-test.c
@@ -0,0 +1,20 @@
+#include <stdlib.h>
+#include <gio/gio.h>
+
+int
+main (int argc, char *argv[])
+{
+  const gchar *envvar;
+  gint pid_from_env;
+
+  envvar = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE_PID");
+  g_assert (envvar != NULL);
+  pid_from_env = atoi (envvar);
+  g_assert_cmpint (pid_from_env, ==, getpid ());
+
+  envvar = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
+  g_assert_cmpstr (envvar, ==, SRCDIR "/appinfo-test.desktop");
+
+  return 0;
+}
+
diff --git a/gio/tests/appinfo-test.desktop b/gio/tests/appinfo-test.desktop
new file mode 100644
index 0000000..becc1c4
--- /dev/null
+++ b/gio/tests/appinfo-test.desktop
@@ -0,0 +1,4 @@
+[Desktop Entry]
+Type=Application
+Name=appinfo-test
+Exec=./appinfo-test
diff --git a/gio/tests/appinfo.c b/gio/tests/appinfo.c
new file mode 100644
index 0000000..099068a
--- /dev/null
+++ b/gio/tests/appinfo.c
@@ -0,0 +1,25 @@
+#include <gio/gio.h>
+#include <gio/gdesktopappinfo.h>
+
+static void
+test_launch (void)
+{
+  GAppInfo *appinfo;
+
+  appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
+  g_assert (appinfo != NULL);
+
+  g_assert (g_app_info_launch (appinfo, NULL, NULL, NULL));
+}
+
+int
+main (int argc, char *argv[])
+{
+  g_type_init ();
+  g_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/appinfo/launch", test_launch);
+
+  return g_test_run ();
+}
+



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