[glib] GApplication: support environment passing



commit 7aa2e5026260a51e089d10dd18017b8f129b7adf
Author: Ryan Lortie <desrt desrt ca>
Date:   Thu Oct 28 22:49:12 2010 -0400

    GApplication: support environment passing
    
    Add support for passing the full contents of the environment to the
    primary instance (by storing it in the platform_data) when
    G_APPLICATION_SEND_ENVIRONMENT is in the flags.

 docs/reference/gio/gio-sections.txt |    2 +
 gio/gapplication.c                  |    8 ++++
 gio/gapplicationcommandline.c       |   71 +++++++++++++++++++++++++++++++++++
 gio/gapplicationcommandline.h       |    5 ++
 gio/gio.symbols                     |    2 +
 gio/gioenums.h                      |    5 ++-
 6 files changed, 92 insertions(+), 1 deletions(-)
---
diff --git a/docs/reference/gio/gio-sections.txt b/docs/reference/gio/gio-sections.txt
index df4c1b1..bdd683d 100644
--- a/docs/reference/gio/gio-sections.txt
+++ b/docs/reference/gio/gio-sections.txt
@@ -2702,6 +2702,8 @@ GApplicationCommandLineClass
 <SUBSECTION>
 g_application_command_line_get_arguments
 g_application_command_line_get_cwd
+g_application_command_line_get_environ
+g_application_command_line_getenv
 g_application_command_line_get_is_remote
 g_application_command_line_get_platform_data
 <SUBSECTION>
diff --git a/gio/gapplication.c b/gio/gapplication.c
index e03d648..544875f 100644
--- a/gio/gapplication.c
+++ b/gio/gapplication.c
@@ -571,6 +571,14 @@ get_platform_data (GApplication *application)
     g_free (cwd);
   }
 
+  if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
+    {
+      gchar **envp = g_get_environ ();
+      g_variant_builder_add (builder, "{sv}", "environ",
+                             g_variant_new_strv ((const gchar **) envp, -1));
+      g_strfreev (envp);
+    }
+
   G_APPLICATION_GET_CLASS (application)->
     add_platform_data (application, builder);
 
diff --git a/gio/gapplicationcommandline.c b/gio/gapplicationcommandline.c
index 396bafc..853cde2 100644
--- a/gio/gapplicationcommandline.c
+++ b/gio/gapplicationcommandline.c
@@ -83,6 +83,8 @@ struct _GApplicationCommandLinePrivate
   GVariant *platform_data;
   GVariant *arguments;
   GVariant *cwd;
+
+  const gchar **environ;
   gint exit_status;
 };
 
@@ -105,6 +107,12 @@ grok_platform_data (GApplicationCommandLine *cmdline)
         if (!cmdline->priv->cwd)
           cmdline->priv->cwd = g_variant_ref (value);
       }
+
+    else if (strcmp (key, "environ") == 0)
+      {
+        if (!cmdline->priv->environ)
+          cmdline->priv->environ = g_variant_get_strv (value, NULL);
+      }
 }
 
 static void
@@ -298,6 +306,69 @@ g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)
 }
 
 /**
+ * g_application_command_line_get_environ:
+ * @cmdline: a #GApplicationCommandLine
+ *
+ * Gets the contents of the 'environ' variable of the command line
+ * invocation, as would be returned by g_get_environ().  The strings may
+ * contain non-utf8 data.
+ *
+ * The remote application usually does not send an environment.  Use
+ * %G_APPLICATION_SEND_ENVIRONMENT to affect that.  Even with this flag
+ * set it is possible that the environment is still not available (due
+ * to invocation messages from other applications).
+ *
+ * The return value should not be modified or freed and is valid for as
+ * long as @cmdline exists.
+ *
+ * Returns: the environment strings, or %NULL if they were not sent
+ * 
+ * Since: 2.28
+ **/
+const gchar * const *
+g_application_command_line_get_environ (GApplicationCommandLine *cmdline)
+{
+  return cmdline->priv->environ;
+}
+
+/**
+ * g_application_command_line_getenv:
+ * @cmdline: a #GApplicationCommandLine
+ *
+ * Gets the value of a particular environment variable of the command
+ * line invocation, as would be returned by g_getenv().  The strings may
+ * contain non-utf8 data.
+ *
+ * The remote application usually does not send an environment.  Use
+ * %G_APPLICATION_SEND_ENVIRONMENT to affect that.  Even with this flag
+ * set it is possible that the environment is still not available (due
+ * to invocation messages from other applications).
+ *
+ * The return value should not be modified or freed and is valid for as
+ * long as @cmdline exists.
+ *
+ * Returns: the value of the variable, or %NULL if unset or unsent
+ * 
+ * Since: 2.28
+ **/
+const gchar *
+g_application_command_line_getenv (GApplicationCommandLine *cmdline,
+                                   const gchar             *name)
+{
+  gint length = strlen (name);
+  gint i;
+
+  /* TODO: expand on windows */
+  if (cmdline->priv->environ)
+    for (i = 0; cmdline->priv->environ[i]; i++)
+      if (strncmp (cmdline->priv->environ[i], name, length) == 0 &&
+          cmdline->priv->environ[i][length] == '=')
+        return cmdline->priv->environ[i] + length + 1;
+
+  return NULL;
+}
+
+/**
  * g_application_command_line_get_is_remote:
  * @cmdline: a #GApplicationCommandLine
  *
diff --git a/gio/gapplicationcommandline.h b/gio/gapplicationcommandline.h
index 79493c5..9b4762f 100644
--- a/gio/gapplicationcommandline.h
+++ b/gio/gapplicationcommandline.h
@@ -91,6 +91,11 @@ GType                   g_application_command_line_get_type             (void) G
 gchar **                g_application_command_line_get_arguments        (GApplicationCommandLine   *cmdline,
                                                                          int                       *argc);
 
+const gchar * const *   g_application_command_line_get_environ          (GApplicationCommandLine   *cmdline);
+
+const gchar *           g_application_command_line_getenv               (GApplicationCommandLine   *cmdline,
+                                                                         const gchar               *name);
+
 const gchar *           g_application_command_line_get_cwd              (GApplicationCommandLine   *cmdline);
 
 gboolean                g_application_command_line_get_is_remote        (GApplicationCommandLine   *cmdline);
diff --git a/gio/gio.symbols b/gio/gio.symbols
index eb8a927..b8fa2a5 100644
--- a/gio/gio.symbols
+++ b/gio/gio.symbols
@@ -52,6 +52,8 @@ g_application_set_inactivity_timeout
 #if IN_FILE(__G_APPLICATION_COMMAND_LINE_C__)
 g_application_command_line_get_arguments
 g_application_command_line_get_cwd
+g_application_command_line_get_environ
+g_application_command_line_getenv
 g_application_command_line_get_exit_status
 g_application_command_line_get_is_remote
 g_application_command_line_get_platform_data
diff --git a/gio/gioenums.h b/gio/gioenums.h
index ca68183..121f54c 100644
--- a/gio/gioenums.h
+++ b/gio/gioenums.h
@@ -1227,6 +1227,8 @@ typedef enum
  *     primary instance)
  * @G_APPLICATION_HANDLES_COMMAND_LINE: This application handles command line
  *     arguments (in the primary instance)
+ * @G_APPLICATION_SEND_ENVIRONMENT: Send the environment of the
+ *     launching process to the primary instance
  *
  * Flags used to define the behaviour of a #GApplication.
  *
@@ -1239,7 +1241,8 @@ typedef enum
   G_APPLICATION_IS_LAUNCHER =          (1 << 1),
 
   G_APPLICATION_HANDLES_OPEN =         (1 << 2),
-  G_APPLICATION_HANDLES_COMMAND_LINE = (1 << 3)
+  G_APPLICATION_HANDLES_COMMAND_LINE = (1 << 3),
+  G_APPLICATION_SEND_ENVIRONMENT    =  (1 << 4)
 } GApplicationFlags;
 
 G_END_DECLS



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