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.
This commit is contained in:
Ryan Lortie 2010-10-28 22:49:12 -04:00
parent 99d2c2eef5
commit 7aa2e50262
6 changed files with 92 additions and 1 deletions

View File

@ -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>

View File

@ -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);

View File

@ -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
@ -297,6 +305,69 @@ g_application_command_line_get_cwd (GApplicationCommandLine *cmdline)
return NULL;
}
/**
* 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

View File

@ -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);

View File

@ -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

View File

@ -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