GIO Reference Manual | ||||
---|---|---|---|---|
Top | Description | Object Hierarchy | Implemented Interfaces | Properties | Signals |
GApplication; struct GApplicationClass; enum GApplicationFlags; gboolean g_application_id_is_valid (const gchar *application_id
); GApplication * g_application_new (const gchar *application_id
,GApplicationFlags flags
); const gchar * g_application_get_application_id (GApplication *application
); void g_application_set_application_id (GApplication *application
,const gchar *application_id
); guint g_application_get_inactivity_timeout (GApplication *application
); void g_application_set_inactivity_timeout (GApplication *application
,guint inactivity_timeout
); GApplicationFlags g_application_get_flags (GApplication *application
); void g_application_set_flags (GApplication *application
,GApplicationFlags flags
); void g_application_set_action_group (GApplication *application
,GActionGroup *action_group
); gboolean g_application_get_is_registered (GApplication *application
); gboolean g_application_get_is_remote (GApplication *application
); gboolean g_application_register (GApplication *application
,GCancellable *cancellable
,GError **error
); void g_application_hold (GApplication *application
); void g_application_release (GApplication *application
); void g_application_activate (GApplication *application
); void g_application_open (GApplication *application
,GFile **files
,gint n_files
,const gchar *hint
); int g_application_run (GApplication *application
,int argc
,char **argv
);
"action-group" GActionGroup* : Write "application-id" gchar* : Read / Write / Construct "flags" GApplicationFlags : Read / Write "inactivity-timeout" guint : Read / Write "is-registered" gboolean : Read "is-remote" gboolean : Read
A GApplication is the foundation of an application, unique for a given application identifier. The GApplication class wraps some low-level platform-specific services and is intended to act as the foundation for higher-level application classes such as GtkApplication or MxApplication. In general, you should not use this class outside of a higher level framework.
One of the core features that GApplication provides is process uniqueness, in the context of a "session". The session concept is platform-dependent, but corresponds roughly to a graphical desktop login. When your application is launched again, its arguments are passed through platform communication to the already running program. The already running instance of the program is called the primary instance.
Before using GApplication, you must choose an "application identifier".
The expected form of an application identifier is very close to that of
of a DBus bus name.
Examples include: "com.example.MyApp", "org.example.internal-apps.Calculator".
For details on valid application identifiers, see
g_application_id_is_valid()
.
The application identifier is claimed by the application as a well-known bus name on the user's session bus. This means that the uniqueness of your application is scoped to the current session. It also means that your application may provide additional services (through registration of other object paths) at that bus name.
The registration of these object paths should be done with the shared
GDBus session bus. Note that due to the internal architecture of
GDBus, method calls can be dispatched at any time (even if a main
loop is not running). For this reason, you must ensure that any
object paths that you wish to register are registered before
GApplication attempts to acquire the bus name of your application
(which happens in g_application_register()
). Unfortunately, this
means that you can not use g_application_get_is_remote()
to decide if
you want to register object paths.
GApplication provides convenient life cycle management by maintaining
a use count for the primary application instance.
The use count can be changed using g_application_hold()
and
g_application_release()
. If it drops to zero, the application exits.
GApplication also implements the GActionGroup interface and lets you
easily export actions by adding them with g_application_set_action_group()
.
When invoking an action by calling g_action_group_activate_action()
on
the application, it is always invoked in the primary instance.
There is a number of different entry points into a GApplication:
The "startup" signal lets you handle the application initialization for all of these in a single place.
Regardless of which of these entry points is used to start the application,
GApplication passes some platform
data from the launching instance to the primary instance,
in the form of a GVariant dictionary mapping strings to variants.
To use platform data, override the before_emit
or after_emit
virtual
functions in your GApplication subclass. When dealing with
GApplicationCommandline objects, the platform data is directly
available via g_application_command_line_get_cwd()
,
g_application_command_line_get_environ()
and
g_application_command_line_get_platform_data()
.
As the name indicates, the platform data may vary depending on the
operating system, but it always includes the current directory (key
"cwd"), and optionally the environment (ie the set of environment
variables and their values) of the calling process (key "environ").
The environment is only added to the platform data if the
G_APPLICATION_SEND_ENVIONMENT flag is set. GApplication subclasses
can add their own platform data by overriding the add_platform_data
virtual function. For instance, GtkApplication adds startup notification
data in this way.
To parse commandline arguments you may handle the
"command-line" signal or override the local_command_line()
vfunc, to parse them in either the primary instance or the local instance,
respectively.
Example 13. Opening files with a GApplication
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include <gio/gio.h> #include <stdlib.h> #include <string.h> static void activate (GApplication *application) { g_print ("activated\n"); /* Note: when doing a longer-lasting action here that returns * to the mainloop, you should use g_application_hold() and * g_application_release() to keep the application alive until * the action is completed. */ } static void open (GApplication *application, GFile **files, gint n_files, const gchar *hint) { gint i; for (i = 0; i < n_files; i++) { gchar *uri = g_file_get_uri (files[i]); g_print ("open %s\n", uri); g_free (uri); } /* Note: when doing a longer-lasting action here that returns * to the mainloop, you should use g_application_hold() and * g_application_release() to keep the application alive until * the action is completed. */ } int main (int argc, char **argv) { GApplication *app; int status; app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_HANDLES_OPEN); g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); g_signal_connect (app, "open", G_CALLBACK (open), NULL); g_application_set_inactivity_timeout (app, 10000); status = g_application_run (app, argc, argv); g_object_unref (app); return status; } |
Example 14. A GApplication with actions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
#include <gio/gio.h> #include <stdlib.h> #include <string.h> static void activate (GApplication *application) { g_print ("activated\n"); } static void activate_action (GAction *action, GVariant *parameter, gpointer data) { g_print ("action %s activated\n", g_action_get_name (action)); } static void activate_toggle_action (GAction *action, GVariant *parameter, gpointer data) { GVariant *state; gboolean b; g_print ("action %s activated\n", g_action_get_name (action)); state = g_action_get_state (action); b = g_variant_get_boolean (state); g_variant_unref (state); g_action_set_state (action, g_variant_new_boolean (!b)); g_print ("state change %d -> %d\n", b, !b); } static void add_actions (GApplication *app) { GSimpleActionGroup *actions; GSimpleAction *action; actions = g_simple_action_group_new (); action = g_simple_action_new ("simple-action", NULL); g_signal_connect (action, "activate", G_CALLBACK (activate_action), app); g_simple_action_group_insert (actions, G_ACTION (action)); g_object_unref (action); action = g_simple_action_new_stateful ("toggle-action", NULL, g_variant_new_boolean (FALSE)); g_signal_connect (action, "activate", G_CALLBACK (activate_toggle_action), app); g_simple_action_group_insert (actions, G_ACTION (action)); g_object_unref (action); g_application_set_action_group (app, G_ACTION_GROUP (actions)); g_object_unref (actions); } int main (int argc, char **argv) { GApplication *app; int status; app = g_application_new ("org.gtk.TestApplication", 0); g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); g_application_set_inactivity_timeout (app, 10000); add_actions (app); if (argc > 1 && strcmp (argv[1], "--simple-action") == 0) { g_application_register (app, NULL, NULL); g_action_group_activate_action (G_ACTION_GROUP (app), "simple-action", NULL); exit (0); } else if (argc > 1 && strcmp (argv[1], "--toggle-action") == 0) { g_application_register (app, NULL, NULL); g_action_group_activate_action (G_ACTION_GROUP (app), "toggle-action", NULL); exit (0); } status = g_application_run (app, argc, argv); g_object_unref (app); return status; } |
typedef struct _GApplication GApplication;
The GApplication structure contains private data and should only be accessed using the provided API
Since 2.28
struct GApplicationClass { /* signals */ void (* startup) (GApplication *application); void (* activate) (GApplication *application); void (* open) (GApplication *application, GFile **files, gint n_files, const gchar *hint); int (* command_line) (GApplication *application, GApplicationCommandLine *command_line); /* vfuncs */ gboolean (* local_command_line) (GApplication *application, gchar ***arguments, int *exit_status); void (* before_emit) (GApplication *application, GVariant *platform_data); void (* after_emit) (GApplication *application, GVariant *platform_data); void (* add_platform_data) (GApplication *application, GVariantBuilder *builder); void (* quit_mainloop) (GApplication *application); void (* run_mainloop) (GApplication *application); };
invoked on the primary instance immediately after registration | |
invoked on the primary instance when an activation occurs | |
invoked on the primary instance when there are files to open | |
invoked on the primary instance when a command-line is not handled locally | |
invoked (locally) when the process has been invoked
via commandline execution. The virtual function has the chance to
inspect (and possibly replace) the list of command line arguments.
See g_application_run() for more information. |
|
invoked on the primary instance before 'activate', 'open', 'command-line' or any action invocation, gets the 'platform data' from the calling instance | |
invoked on the primary instance after 'activate', 'open', 'command-line' or any action invocation, gets the 'platform data' from the calling instance | |
invoked (locally) to add 'platform data' to be sent to the primary instance when activating, opening or invoking actions | |
invoked on the primary instance when the use count of the application drops to zero (and after any inactivity timeout, if requested) | |
invoked on the primary instance from g_application_run()
if the use-count is non-zero |
Since 2.28
typedef enum { G_APPLICATION_FLAGS_NONE, G_APPLICATION_IS_SERVICE = (1 << 0), G_APPLICATION_IS_LAUNCHER = (1 << 1), G_APPLICATION_HANDLES_OPEN = (1 << 2), G_APPLICATION_HANDLES_COMMAND_LINE = (1 << 3), G_APPLICATION_SEND_ENVIRONMENT = (1 << 4) } GApplicationFlags;
Flags used to define the behaviour of a GApplication.
Default | |
Run as a service. In this mode, registration fails if the service is already running, and the application will stay around for a while when the use count falls to zero. | |
Don't try to become the primary instance. | |
This application handles opening files (in
the primary instance). Note that this flag only affects the default
implementation of local_command_line() , and has no effect if
G_APPLICATION_HANDLES_COMMAND_LINE is given.
See g_application_run() for details.
|
|
This application handles command line
arguments (in the primary instance). Note that this flag only affect
the default implementation of local_command_line() .
See g_application_run() for details.
|
|
Send the environment of the
launching process to the primary instance. Set this flag if your
application is expected to behave differently depending on certain
environment variables. For instance, an editor might be expected
to use the GIT_COMMITTER_NAME environment variable
when editing a git commit message. The environment is available
to the "command-line" signal handler, via
g_application_command_line_getenv() .
|
Since 2.28
gboolean g_application_id_is_valid (const gchar *application_id
);
Checks if application_id
is a valid application identifier.
A valid ID is required for calls to g_application_new()
and
g_application_set_application_id()
.
For convenience, the restrictions on application identifiers are reproduced here:
|
a potential application identifier |
Returns : |
TRUE if application_id is valid |
GApplication * g_application_new (const gchar *application_id
,GApplicationFlags flags
);
Creates a new GApplication instance.
This function calls g_type_init()
for you.
The application id must be valid. See g_application_id_is_valid()
.
|
the application id |
|
the application flags |
Returns : |
a new GApplication instance |
const gchar * g_application_get_application_id
(GApplication *application
);
Gets the unique identifier for application
.
|
a GApplication |
Returns : |
the identifier for application , owned by application
|
Since 2.28
void g_application_set_application_id (GApplication *application
,const gchar *application_id
);
Sets the unique identifier for application
.
The application id can only be modified if application
has not yet
been registered.
The application id must be valid. See g_application_id_is_valid()
.
|
a GApplication |
|
the identifier for application
|
Since 2.28
guint g_application_get_inactivity_timeout
(GApplication *application
);
Gets the current inactivity timeout for the application.
This is the amount of time (in milliseconds) after the last call to
g_application_release()
before the application stops running.
|
a GApplication |
Returns : |
the timeout, in milliseconds |
Since 2.28
void g_application_set_inactivity_timeout (GApplication *application
,guint inactivity_timeout
);
Sets the current inactivity timeout for the application.
This is the amount of time (in milliseconds) after the last call to
g_application_release()
before the application stops running.
This call has no side effects of its own. The value set here is only
used for next time g_application_release()
drops the use count to
zero. Any timeouts currently in progress are not impacted.
|
a GApplication |
|
the timeout, in milliseconds |
Since 2.28
GApplicationFlags g_application_get_flags (GApplication *application
);
Gets the flags for application
.
See GApplicationFlags.
|
a GApplication |
Returns : |
the flags for application
|
Since 2.28
void g_application_set_flags (GApplication *application
,GApplicationFlags flags
);
Sets the flags for application
.
The flags can only be modified if application
has not yet been
registered.
See GApplicationFlags.
|
a GApplication |
|
the flags for application
|
Since 2.28
void g_application_set_action_group (GApplication *application
,GActionGroup *action_group
);
Sets or unsets the group of actions associated with the application.
These actions are the actions that can be remotely invoked.
It is an error to call this function after the application has been registered.
|
a GApplication |
|
a GActionGroup, or NULL . [allow-none]
|
Since 2.28
gboolean g_application_get_is_registered (GApplication *application
);
Checks if application
is registered.
An application is registered if g_application_register()
has been
successfully called.
|
a GApplication |
Returns : |
TRUE if application is registered |
Since 2.28
gboolean g_application_get_is_remote (GApplication *application
);
Checks if application
is remote.
If application
is remote then it means that another instance of
application already exists (the 'primary' instance). Calls to
perform actions on application
will result in the actions being
performed by the primary instance.
The value of this property can not be accessed before
g_application_register()
has been called. See
g_application_get_is_registered()
.
|
a GApplication |
Returns : |
TRUE if application is remote |
Since 2.28
gboolean g_application_register (GApplication *application
,GCancellable *cancellable
,GError **error
);
Attempts registration of the application.
This is the point at which the application discovers if it is the primary instance or merely acting as a remote for an already-existing primary instance. This is implemented by attempting to acquire the application identifier as a unique bus name on the session bus using GDBus.
Due to the internal architecture of GDBus, method calls can be dispatched at any time (even if a main loop is not running). For this reason, you must ensure that any object paths that you wish to register are registered before calling this function.
If the application has already been registered then TRUE
is
returned with no work performed.
The "startup" signal is emitted if registration succeeds
and application
is the primary instance.
In the event of an error (such as cancellable
being cancelled, or a
failure to connect to the session bus), FALSE
is returned and error
is set appropriately.
Note: the return value of this function is not an indicator that this
instance is or is not the primary instance of the application. See
g_application_get_is_remote()
for that.
|
a GApplication |
|
a GCancellable, or NULL
|
|
a pointer to a NULL GError, or NULL
|
Returns : |
TRUE if registration succeeded |
Since 2.28
void g_application_hold (GApplication *application
);
Increases the use count of application
.
Use this function to indicate that the application has a reason to
continue to run. For example, g_application_hold()
is called by GTK+
when a toplevel window is on the screen.
To cancel the hold, call g_application_release()
.
|
a GApplication |
void g_application_release (GApplication *application
);
Decrease the use count of application
.
When the use count reaches zero, the application will stop running.
Never call this function except to cancel the effect of a previous
call to g_application_hold()
.
|
a GApplication |
void g_application_activate (GApplication *application
);
Activates the application.
In essence, this results in the GApplication::activate()
signal being
emitted in the primary instance.
The application must be registered before calling this function.
|
a GApplication |
Since 2.28
void g_application_open (GApplication *application
,GFile **files
,gint n_files
,const gchar *hint
);
Opens the given files.
In essence, this results in the "open" signal being emitted in the primary instance.
n_files
must be greater than zero.
hint
is simply passed through to the ::open signal. It is
intended to be used by applications that have multiple modes for
opening files (eg: "view" vs "edit", etc). Unless you have a need
for this functionality, you should use "".
The application must be registered before calling this function
and it must have the G_APPLICATION_HANDLES_OPEN
flag set.
|
a GApplication |
|
an array of GFiles to open. [array length=n_files] |
|
the length of the files array |
|
a hint (or ""), but never NULL
|
Since 2.28
int g_application_run (GApplication *application
,int argc
,char **argv
);
Runs the application.
This function is intended to be run from main()
and its return value
is intended to be returned by main()
. Although you are expected to pass
the argc
, argv
parameters from main()
to this function, it is possible
to pass NULL
if argv
is not available or commandline handling is not
required.
First, the local_command_line()
virtual function is invoked.
This function always runs on the local instance. It gets passed a pointer
to a NULL
-terminated copy of argv
and is expected to remove the arguments
that it handled (shifting up remaining arguments). See
Example 16, “Split commandline handling” for an example of
parsing argv
manually. Alternatively, you may use the GOptionContext API,
after setting argc = g_strv_length (argv);
.
The last argument to local_command_line()
is a pointer to the status
variable which can used to set the exit status that is returned from
g_application_run()
.
If local_command_line()
returns TRUE
, the command line is expected
to be completely handled, including possibly registering as the primary
instance, calling g_application_activate()
or g_application_open()
, etc.
If local_command_line()
returns FALSE
then the application is registered
and the "command-line" signal is emitted in the primary
instance (which may or may not be this instance). The signal handler
gets passed a GApplicationCommandline object that (among other things)
contains the remaining commandline arguments that have not been handled
by local_command_line()
.
If the application has the G_APPLICATION_HANDLES_COMMAND_LINE
flag set then the default implementation of local_command_line()
always returns FALSE
immediately, resulting in the commandline
always being handled in the primary instance.
Otherwise, the default implementation of local_command_line()
tries
to do a couple of things that are probably reasonable for most
applications. First, g_application_register()
is called to attempt
to register the application. If that works, then the command line
arguments are inspected. If no commandline arguments are given, then
g_application_activate()
is called. If commandline arguments are
given and the G_APPLICATION_HANDLES_OPEN
flag is set then they
are assumed to be filenames and g_application_open()
is called.
If you need to handle commandline arguments that are not filenames,
and you don't mind commandline handling to happen in the primary
instance, you should set G_APPLICATION_HANDLED_COMMAND_LINE
and
process the commandline arguments in your "command-line"
signal handler, either manually or using the GOptionContext API.
If you are interested in doing more complicated local handling of the
commandline then you should implement your own GApplication subclass
and override local_command_line()
. In this case, you most likely want
to return TRUE
from your local_command_line()
implementation to
suppress the default handling. See
Example 16, “Split commandline handling” for an example.
If, after the above is done, the use count of the application is zero then the exit status is returned immediately. If the use count is non-zero then the mainloop is run until the use count falls to zero, at which point 0 is returned.
If the G_APPLICATION_IS_SERVICE
flag is set, then the exiting at
use count of zero is delayed for a while (ie: the instance stays
around to provide its service to others).
|
a GApplication |
|
the argc from main() (or 0 if argv is NULL ) |
|
the argv from main() , or NULL . [array length=argc]
|
Returns : |
the exit status |
Since 2.28
"action-group"
property"action-group" GActionGroup* : Write
The group of actions that the application exports.
"application-id"
property"application-id" gchar* : Read / Write / Construct
The unique identifier for the application.
Default value: NULL
"flags"
property"flags" GApplicationFlags : Read / Write
Flags specifying the behaviour of the application.
"inactivity-timeout"
property"inactivity-timeout" guint : Read / Write
Iime (ms) to stay alive after becoming idle.
Default value: 0
"is-registered"
property"is-registered" gboolean : Read
If g_application_register() has been called.
Default value: FALSE
"is-remote"
property"is-remote" gboolean : Read
If this application instance is remote.
Default value: FALSE
"activate"
signalvoid user_function (GApplication *application,
gpointer user_data) : Run Last
The ::activate signal is emitted on the primary instance when an
activation occurs. See g_application_activate()
.
|
the application |
|
user data set when the signal handler was connected. |
"command-line"
signalgint user_function (GApplication *application,
GApplicationCommandLine *command_line,
gpointer user_data) : Run Last
The ::command-line signal is emitted on the primary instance when
a commandline is not handled locally. See g_application_run()
and
the GApplicationCommandline documentation for more information.
|
the application |
|
a GApplicationCommandLine representing the passed commandline |
|
user data set when the signal handler was connected. |
Returns : |
An integer that is set as the exit status for the calling
process. See g_application_command_line_set_exit_status() . |
"open"
signalvoid user_function (GApplication *application,
gpointer files,
gint n_files,
gchar *hint,
gpointer user_data) : Run Last
The ::open signal is emitted on the primary instance when there are
files to open. See g_application_open()
for more information.
|
the application |
|
an array of GFiles. [array length=n_files][element-type GFile] |
|
the length of files
|
|
a hint provided by the calling instance |
|
user data set when the signal handler was connected. |
"startup"
signalvoid user_function (GApplication *application,
gpointer user_data) : Run Last
The ::startup signal is emitted on the primary instance immediately
after registration. See g_application_register()
.
|
the application |
|
user data set when the signal handler was connected. |