GApplication

GApplication — Core application class

Functions

gboolean g_application_id_is_valid ()
GApplication * g_application_new ()
const gchar * g_application_get_application_id ()
void g_application_set_application_id ()
guint g_application_get_inactivity_timeout ()
void g_application_set_inactivity_timeout ()
GApplicationFlags g_application_get_flags ()
void g_application_set_flags ()
const gchar * g_application_get_resource_base_path ()
void g_application_set_resource_base_path ()
GDBusConnection * g_application_get_dbus_connection ()
const gchar * g_application_get_dbus_object_path ()
void g_application_set_action_group ()
gboolean g_application_get_is_registered ()
gboolean g_application_get_is_remote ()
gboolean g_application_register ()
void g_application_hold ()
void g_application_release ()
void g_application_quit ()
void g_application_activate ()
void g_application_open ()
void g_application_send_notification ()
void g_application_withdraw_notification ()
int g_application_run ()
void g_application_add_main_option_entries ()
void g_application_add_main_option ()
void g_application_add_option_group ()
void g_application_set_option_context_parameter_string ()
void g_application_set_option_context_summary ()
void g_application_set_option_context_description ()
void g_application_set_default ()
GApplication * g_application_get_default ()
void g_application_mark_busy ()
void g_application_unmark_busy ()
gboolean g_application_get_is_busy ()
void g_application_bind_busy_property ()
void g_application_unbind_busy_property ()

Properties

GActionGroup * action-group Write
char * application-id Read / Write / Construct
GApplicationFlags flags Read / Write
guint inactivity-timeout Read / Write
gboolean is-busy Read
gboolean is-registered Read
gboolean is-remote Read
char * resource-base-path Read / Write

Signals

void activate Run Last
int command-line Run Last
int handle-local-options Run Last
gboolean name-lost Run Last
void open Run Last
void shutdown Run Last
void startup Run First

Types and Values

Object Hierarchy

    GFlags
    ╰── GApplicationFlags
    GObject
    ╰── GApplication

Implemented Interfaces

GApplication implements GActionGroup and GActionMap.

Includes

#include <gio/gio.h>

Description

A GApplication is the foundation of an application. It 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.

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. Higher-level classes such as GtkApplication employ the use count to ensure that the application stays alive as long as it has any opened windows.

Another feature that GApplication (optionally) provides is process uniqueness. Applications can make use of this functionality by providing a unique application ID. If given, only one application with this ID can be running at a time per 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"; for non-unique applications this is always the current instance. On Linux, the D-Bus session bus is used for communication.

The use of GApplication differs from some other commonly-used uniqueness libraries (such as libunique) in important ways. The application is not expected to manually register itself and check if it is the primary instance. Instead, the main() function of a GApplication should do very little more than instantiating the application instance, possibly connecting signal handlers, then calling g_application_run(). All checks for uniqueness are done internally. If the application is the primary instance then the startup signal is emitted and the mainloop runs. If the application is not the primary instance then a signal is sent to the primary instance and g_application_run() promptly returns. See the code examples below.

If used, the expected form of an application identifier is the same as that of of a D-Bus well-known bus name. Examples include: com.example.MyApp, org.example.internal_apps.Calculator, org._7_zip.Archiver. For details on valid application identifiers, see g_application_id_is_valid().

On Linux, the application identifier is claimed 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 cannot use g_application_get_is_remote() to decide if you want to register object paths.

GApplication also implements the GActionGroup and GActionMap interfaces and lets you easily export actions by adding them with g_action_map_add_action(). When invoking an action by calling g_action_group_activate_action() on the application, it is always invoked in the primary instance. The actions are also exported on the session bus, and GIO provides the GDBusActionGroup wrapper to conveniently access them remotely. GIO provides a GDBusMenuModel wrapper for remote access to exported GMenuModels.

There is a number of different entry points into a GApplication:

  • via 'Activate' (i.e. just starting the application)

  • via 'Open' (i.e. opening some files)

  • by handling a command-line

  • via activating an action

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_ENVIRONMENT 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.

For an example of opening files with a GApplication, see gapplication-example-open.c.

For an example of using actions with GApplication, see gapplication-example-actions.c.

For an example of using extra D-Bus hooks with GApplication, see gapplication-example-dbushooks.c.

Functions

g_application_id_is_valid ()

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().

Application identifiers follow the same format as D-Bus well-known bus names. For convenience, the restrictions on application identifiers are reproduced here:

  • Application identifiers are composed of 1 or more elements separated by a period (.) character. All elements must contain at least one character.

  • Each element must only contain the ASCII characters [A-Z][a-z][0-9]_-, with - discouraged in new application identifiers. Each element must not begin with a digit.

  • Application identifiers must contain at least one . (period) character (and thus at least two elements).

  • Application identifiers must not begin with a . (period) character.

  • Application identifiers must not exceed 255 characters.

Note that the hyphen (-) character is allowed in application identifiers, but is problematic or not allowed in various specifications and APIs that refer to D-Bus, such as Flatpak application IDs, the DBusActivatable interface in the Desktop Entry Specification, and the convention that an application's "main" interface and object path resemble its application identifier and bus name. To avoid situations that require special-case handling, it is recommended that new application identifiers consistently replace hyphens with underscores.

Like D-Bus interface names, application identifiers should start with the reversed DNS domain name of the author of the interface (in lower-case), and it is conventional for the rest of the application identifier to consist of words run together, with initial capital letters.

As with D-Bus interface names, if the author's DNS domain name contains hyphen/minus characters they should be replaced by underscores, and if it contains leading digits they should be escaped by prepending an underscore. For example, if the owner of 7-zip.org used an application identifier for an archiving application, it might be named org._7_zip.Archiver.

Parameters

application_id

a potential application identifier

 

Returns

TRUE if application_id is valid


g_application_new ()

GApplication *
g_application_new (const gchar *application_id,
                   GApplicationFlags flags);

Creates a new GApplication instance.

If non-NULL, the application id must be valid. See g_application_id_is_valid().

If no application ID is given then some features of GApplication (most notably application uniqueness) will be disabled.

Parameters

application_id

the application id.

[nullable]

flags

the application flags

 

Returns

a new GApplication instance


g_application_get_application_id ()

const gchar *
g_application_get_application_id (GApplication *application);

Gets the unique identifier for application .

Parameters

application

a GApplication

 

Returns

the identifier for application , owned by application .

[nullable]

Since: 2.28


g_application_set_application_id ()

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.

If non-NULL, the application id must be valid. See g_application_id_is_valid().

Parameters

application

a GApplication

 

application_id

the identifier for application .

[nullable]

Since: 2.28


g_application_get_inactivity_timeout ()

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.

Parameters

application

a GApplication

 

Returns

the timeout, in milliseconds

Since: 2.28


g_application_set_inactivity_timeout ()

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.

Parameters

application

a GApplication

 

inactivity_timeout

the timeout, in milliseconds

 

Since: 2.28


g_application_get_flags ()

GApplicationFlags
g_application_get_flags (GApplication *application);

Gets the flags for application .

See GApplicationFlags.

Parameters

application

a GApplication

 

Returns

the flags for application

Since: 2.28


g_application_set_flags ()

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.

Parameters

application

a GApplication

 

flags

the flags for application

 

Since: 2.28


g_application_get_resource_base_path ()

const gchar *
g_application_get_resource_base_path (GApplication *application);

Gets the resource base path of application .

See g_application_set_resource_base_path() for more information.

Parameters

application

a GApplication

 

Returns

the base resource path, if one is set.

[nullable]

Since: 2.42


g_application_set_resource_base_path ()

void
g_application_set_resource_base_path (GApplication *application,
                                      const gchar *resource_path);

Sets (or unsets) the base resource path of application .

The path is used to automatically load various application resources such as menu layouts and action descriptions. The various types of resources will be found at fixed names relative to the given base path.

By default, the resource base path is determined from the application ID by prefixing '/' and replacing each '.' with '/'. This is done at the time that the GApplication object is constructed. Changes to the application ID after that point will not have an impact on the resource base path.

As an example, if the application has an ID of "org.example.app" then the default resource base path will be "/org/example/app". If this is a GtkApplication (and you have not manually changed the path) then Gtk will then search for the menus of the application at "/org/example/app/gtk/menus.ui".

See GResource for more information about adding resources to your application.

You can disable automatic resource loading functionality by setting the path to NULL.

Changing the resource base path once the application is running is not recommended. The point at which the resource path is consulted for forming paths for various purposes is unspecified. When writing a sub-class of GApplication you should either set the “resource-base-path” property at construction time, or call this function during the instance initialization. Alternatively, you can call this function in the GApplicationClass.startup virtual function, before chaining up to the parent implementation.

Parameters

application

a GApplication

 

resource_path

the resource path to use.

[nullable]

Since: 2.42


g_application_get_dbus_connection ()

GDBusConnection *
g_application_get_dbus_connection (GApplication *application);

Gets the GDBusConnection being used by the application, or NULL.

If GApplication is using its D-Bus backend then this function will return the GDBusConnection being used for uniqueness and communication with the desktop environment and other instances of the application.

If GApplication is not using D-Bus then this function will return NULL. This includes the situation where the D-Bus backend would normally be in use but we were unable to connect to the bus.

This function must not be called before the application has been registered. See g_application_get_is_registered().

Parameters

application

a GApplication

 

Returns

a GDBusConnection, or NULL.

[nullable][transfer none]

Since: 2.34


g_application_get_dbus_object_path ()

const gchar *
g_application_get_dbus_object_path (GApplication *application);

Gets the D-Bus object path being used by the application, or NULL.

If GApplication is using its D-Bus backend then this function will return the D-Bus object path that GApplication is using. If the application is the primary instance then there is an object published at this path. If the application is not the primary instance then the result of this function is undefined.

If GApplication is not using D-Bus then this function will return NULL. This includes the situation where the D-Bus backend would normally be in use but we were unable to connect to the bus.

This function must not be called before the application has been registered. See g_application_get_is_registered().

Parameters

application

a GApplication

 

Returns

the object path, or NULL.

[nullable]

Since: 2.34


g_application_set_action_group ()

void
g_application_set_action_group (GApplication *application,
                                GActionGroup *action_group);

g_application_set_action_group has been deprecated since version 2.32 and should not be used in newly-written code.

Use the GActionMap interface instead. Never ever mix use of this API with use of GActionMap on the same application or things will go very badly wrong. This function is known to introduce buggy behaviour (ie: signals not emitted on changes to the action group), so you should really use GActionMap instead.

This used to be how actions were associated with a GApplication. Now there is GActionMap for that.

Parameters

application

a GApplication

 

action_group

a GActionGroup, or NULL.

[nullable]

Since: 2.28


g_application_get_is_registered ()

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.

Parameters

application

a GApplication

 

Returns

TRUE if application is registered

Since: 2.28


g_application_get_is_remote ()

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 cannot be accessed before g_application_register() has been called. See g_application_get_is_registered().

Parameters

application

a GApplication

 

Returns

TRUE if application is remote

Since: 2.28


g_application_register ()

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.

If there is no application ID or if G_APPLICATION_NON_UNIQUE was given, then this process will always become the primary instance.

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 (including the non-unique case).

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.

Parameters

application

a GApplication

 

cancellable

a GCancellable, or NULL.

[nullable]

error

a pointer to a NULL GError, or NULL

 

Returns

TRUE if registration succeeded

Since: 2.28


g_application_hold ()

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().

Parameters

application

a GApplication

 

g_application_release ()

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().

Parameters

application

a GApplication

 

g_application_quit ()

void
g_application_quit (GApplication *application);

Immediately quits the application.

Upon return to the mainloop, g_application_run() will return, calling only the 'shutdown' function before doing so.

The hold count is ignored. Take care if your code has called g_application_hold() on the application and is therefore still expecting it to exist. (Note that you may have called g_application_hold() indirectly, for example through gtk_application_add_window().)

The result of calling g_application_run() again after it returns is unspecified.

Parameters

application

a GApplication

 

Since: 2.32


g_application_activate ()

void
g_application_activate (GApplication *application);

Activates the application.

In essence, this results in the “activate” signal being emitted in the primary instance.

The application must be registered before calling this function.

Parameters

application

a GApplication

 

Since: 2.28


g_application_open ()

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.

Parameters

application

a GApplication

 

files

an array of GFiles to open.

[array length=n_files]

n_files

the length of the files array

 

hint

a hint (or ""), but never NULL

 

Since: 2.28


g_application_send_notification ()

void
g_application_send_notification (GApplication *application,
                                 const gchar *id,
                                 GNotification *notification);

Sends a notification on behalf of application to the desktop shell. There is no guarantee that the notification is displayed immediately, or even at all.

Notifications may persist after the application exits. It will be D-Bus-activated when the notification or one of its actions is activated.

Modifying notification after this call has no effect. However, the object can be reused for a later call to this function.

id may be any string that uniquely identifies the event for the application. It does not need to be in any special format. For example, "new-message" might be appropriate for a notification about new messages.

If a previous notification was sent with the same id , it will be replaced with notification and shown again as if it was a new notification. This works even for notifications sent from a previous execution of the application, as long as id is the same string.

id may be NULL, but it is impossible to replace or withdraw notifications without an id.

If notification is no longer relevant, it can be withdrawn with g_application_withdraw_notification().

Parameters

application

a GApplication

 

id

id of the notification, or NULL.

[nullable]

notification

the GNotification to send

 

Since: 2.40


g_application_withdraw_notification ()

void
g_application_withdraw_notification (GApplication *application,
                                     const gchar *id);

Withdraws a notification that was sent with g_application_send_notification().

This call does nothing if a notification with id doesn't exist or the notification was never sent.

This function works even for notifications sent in previous executions of this application, as long id is the same as it was for the sent notification.

Note that notifications are dismissed when the user clicks on one of the buttons in a notification or triggers its default action, so there is no need to explicitly withdraw the notification in that case.

Parameters

application

a GApplication

 

id

id of a previously sent notification

 

Since: 2.40


g_application_run ()

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. Note that on Windows, argc and argv are ignored, and g_win32_get_command_line() is called internally (for proper support of Unicode commandline arguments).

GApplication will attempt to parse the commandline arguments. You can add commandline flags to the list of recognised options by way of g_application_add_main_option_entries(). After this, the “handle-local-options” signal is emitted, from which the application can inspect the values of its GOptionEntrys.

“handle-local-options” is a good place to handle options such as --version, where an immediate reply from the local process is desired (instead of communicating with an already-running instance). A “handle-local-options” handler can stop further processing by returning a non-negative value, which then becomes the exit status of the process.

What happens next depends on the flags: if G_APPLICATION_HANDLES_COMMAND_LINE was specified then the remaining commandline arguments are sent to the primary instance, where a “command-line” signal is emitted. Otherwise, the remaining commandline arguments are assumed to be a list of files. If there are no files listed, the application is activated via the “activate” signal. If there are one or more files, and G_APPLICATION_HANDLES_OPEN was specified then the files are opened via the “open” signal.

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 gapplication-example-cmdline2.c 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 default main context is iterated until the use count falls to zero, at which point 0 is returned.

If the G_APPLICATION_IS_SERVICE flag is set, then the service will run for as much as 10 seconds with a use count of zero while waiting for the message that caused the activation to arrive. After that, if the use count falls to zero the application will exit immediately, except in the case that g_application_set_inactivity_timeout() is in use.

This function sets the prgname (g_set_prgname()), if not already set, to the basename of argv[0].

Much like g_main_loop_run(), this function will acquire the main context for the duration that the application is running.

Since 2.40, applications that are not explicitly flagged as services or launchers (ie: neither G_APPLICATION_IS_SERVICE or G_APPLICATION_IS_LAUNCHER are given as flags) will check (from the default handler for local_command_line) if "--gapplication-service" was given in the command line. If this flag is present then normal commandline processing is interrupted and the G_APPLICATION_IS_SERVICE flag is set. This provides a "compromise" solution whereby running an application directly from the commandline will invoke it in the normal way (which can be useful for debugging) while still allowing applications to be D-Bus activated in service mode. The D-Bus service file should invoke the executable with "--gapplication-service" as the sole commandline argument. This approach is suitable for use by most graphical applications but should not be used from applications like editors that need precise control over when processes invoked via the commandline will exit and what their exit status will be.

Parameters

application

a GApplication

 

argc

the argc from main() (or 0 if argv is NULL)

 

argv

the argv from main(), or NULL.

[array length=argc][element-type filename][nullable]

Returns

the exit status

Since: 2.28


g_application_add_main_option_entries ()

void
g_application_add_main_option_entries (GApplication *application,
                                       const GOptionEntry *entries);

Adds main option entries to be handled by application .

This function is comparable to g_option_context_add_main_entries().

After the commandline arguments are parsed, the “handle-local-options” signal will be emitted. At this point, the application can inspect the values pointed to by arg_data in the given GOptionEntrys.

Unlike GOptionContext, GApplication supports giving a NULL arg_data for a non-callback GOptionEntry. This results in the argument in question being packed into a GVariantDict which is also passed to “handle-local-options”, where it can be inspected and modified. If G_APPLICATION_HANDLES_COMMAND_LINE is set, then the resulting dictionary is sent to the primary instance, where g_application_command_line_get_options_dict() will return it. This "packing" is done according to the type of the argument -- booleans for normal flags, strings for strings, bytestrings for filenames, etc. The packing only occurs if the flag is given (ie: we do not pack a "false" GVariant in the case that a flag is missing).

In general, it is recommended that all commandline arguments are parsed locally. The options dictionary should then be used to transmit the result of the parsing to the primary instance, where g_variant_dict_lookup() can be used. For local options, it is possible to either use arg_data in the usual way, or to consult (and potentially remove) the option from the options dictionary.

This function is new in GLib 2.40. Before then, the only real choice was to send all of the commandline arguments (options and all) to the primary instance for handling. GApplication ignored them completely on the local side. Calling this function "opts in" to the new behaviour, and in particular, means that unrecognised options will be treated as errors. Unrecognised options have never been ignored when G_APPLICATION_HANDLES_COMMAND_LINE is unset.

If “handle-local-options” needs to see the list of filenames, then the use of G_OPTION_REMAINING is recommended. If arg_data is NULL then G_OPTION_REMAINING can be used as a key into the options dictionary. If you do use G_OPTION_REMAINING then you need to handle these arguments for yourself because once they are consumed, they will no longer be visible to the default handling (which treats them as filenames to be opened).

It is important to use the proper GVariant format when retrieving the options with g_variant_dict_lookup():

  • for G_OPTION_ARG_NONE, use b

  • for G_OPTION_ARG_STRING, use &amp;s

  • for G_OPTION_ARG_INT, use i

  • for G_OPTION_ARG_INT64, use x

  • for G_OPTION_ARG_DOUBLE, use d

  • for G_OPTION_ARG_FILENAME, use ^&amp;ay

  • for G_OPTION_ARG_STRING_ARRAY, use ^a&amp;s

  • for G_OPTION_ARG_FILENAME_ARRAY, use ^a&amp;ay

Parameters

application

a GApplication

 

entries

(array zero-terminated=1) (element-type GOptionEntry) a NULL-terminated list of GOptionEntrys

 

Since: 2.40


g_application_add_main_option ()

void
g_application_add_main_option (GApplication *application,
                               const char *long_name,
                               char short_name,
                               GOptionFlags flags,
                               GOptionArg arg,
                               const char *description,
                               const char *arg_description);

Add an option to be handled by application .

Calling this function is the equivalent of calling g_application_add_main_option_entries() with a single GOptionEntry that has its arg_data member set to NULL.

The parsed arguments will be packed into a GVariantDict which is passed to “handle-local-options”. If G_APPLICATION_HANDLES_COMMAND_LINE is set, then it will also be sent to the primary instance. See g_application_add_main_option_entries() for more details.

See GOptionEntry for more documentation of the arguments.

Parameters

application

the GApplication

 

long_name

the long name of an option used to specify it in a commandline

 

short_name

the short name of an option

 

flags

flags from GOptionFlags

 

arg

the type of the option, as a GOptionArg

 

description

the description for the option in --help output

 

arg_description

the placeholder to use for the extra argument parsed by the option in --help output.

[nullable]

Since: 2.42


g_application_add_option_group ()

void
g_application_add_option_group (GApplication *application,
                                GOptionGroup *group);

Adds a GOptionGroup to the commandline handling of application .

This function is comparable to g_option_context_add_group().

Unlike g_application_add_main_option_entries(), this function does not deal with NULL arg_data and never transmits options to the primary instance.

The reason for that is because, by the time the options arrive at the primary instance, it is typically too late to do anything with them. Taking the GTK option group as an example: GTK will already have been initialised by the time the “command-line” handler runs. In the case that this is not the first-running instance of the application, the existing instance may already have been running for a very long time.

This means that the options from GOptionGroup are only really usable in the case that the instance of the application being run is the first instance. Passing options like --display= or --gdk-debug= on future runs will have no effect on the existing primary instance.

Calling this function will cause the options in the supplied option group to be parsed, but it does not cause you to be "opted in" to the new functionality whereby unrecognised options are rejected even if G_APPLICATION_HANDLES_COMMAND_LINE was given.

Parameters

application

the GApplication

 

group

a GOptionGroup.

[transfer full]

Since: 2.40


g_application_set_option_context_parameter_string ()

void
g_application_set_option_context_parameter_string
                               (GApplication *application,
                                const gchar *parameter_string);

Sets the parameter string to be used by the commandline handling of application .

This function registers the argument to be passed to g_option_context_new() when the internal GOptionContext of application is created.

See g_option_context_new() for more information about parameter_string .

Parameters

application

the GApplication

 

parameter_string

a string which is displayed in the first line of --help output, after the usage summary programname [OPTION...].

[nullable]

Since: 2.56


g_application_set_option_context_summary ()

void
g_application_set_option_context_summary
                               (GApplication *application,
                                const gchar *summary);

Adds a summary to the application option context.

See g_option_context_set_summary() for more information.

Parameters

application

the GApplication

 

summary

a string to be shown in --help output before the list of options, or NULL.

[nullable]

Since: 2.56


g_application_set_option_context_description ()

void
g_application_set_option_context_description
                               (GApplication *application,
                                const gchar *description);

Adds a description to the application option context.

See g_option_context_set_description() for more information.

Parameters

application

the GApplication

 

description

a string to be shown in --help output after the list of options, or NULL.

[nullable]

Since: 2.56


g_application_set_default ()

void
g_application_set_default (GApplication *application);

Sets or unsets the default application for the process, as returned by g_application_get_default().

This function does not take its own reference on application . If application is destroyed then the default application will revert back to NULL.

Parameters

application

the application to set as default, or NULL.

[nullable]

Since: 2.32


g_application_get_default ()

GApplication *
g_application_get_default (void);

Returns the default GApplication instance for this process.

Normally there is only one GApplication per process and it becomes the default when it is created. You can exercise more control over this by using g_application_set_default().

If there is no default application then NULL is returned.

Returns

the default application for this process, or NULL.

[nullable][transfer none]

Since: 2.32


g_application_mark_busy ()

void
g_application_mark_busy (GApplication *application);

Increases the busy count of application .

Use this function to indicate that the application is busy, for instance while a long running operation is pending.

The busy state will be exposed to other processes, so a session shell will use that information to indicate the state to the user (e.g. with a spinner).

To cancel the busy indication, use g_application_unmark_busy().

Parameters

application

a GApplication

 

Since: 2.38


g_application_unmark_busy ()

void
g_application_unmark_busy (GApplication *application);

Decreases the busy count of application .

When the busy count reaches zero, the new state will be propagated to other processes.

This function must only be called to cancel the effect of a previous call to g_application_mark_busy().

Parameters

application

a GApplication

 

Since: 2.38


g_application_get_is_busy ()

gboolean
g_application_get_is_busy (GApplication *application);

Gets the application's current busy state, as set through g_application_mark_busy() or g_application_bind_busy_property().

Parameters

application

a GApplication

 

Returns

TRUE if application is currently marked as busy

Since: 2.44


g_application_bind_busy_property ()

void
g_application_bind_busy_property (GApplication *application,
                                  gpointer object,
                                  const gchar *property);

Marks application as busy (see g_application_mark_busy()) while property on object is TRUE.

The binding holds a reference to application while it is active, but not to object . Instead, the binding is destroyed when object is finalized.

Parameters

application

a GApplication

 

object

a GObject.

[type GObject.Object]

property

the name of a boolean property of object

 

Since: 2.44


g_application_unbind_busy_property ()

void
g_application_unbind_busy_property (GApplication *application,
                                    gpointer object,
                                    const gchar *property);

Destroys a binding between property and the busy state of application that was previously created with g_application_bind_busy_property().

Parameters

application

a GApplication

 

object

a GObject.

[type GObject.Object]

property

the name of a boolean property of object

 

Since: 2.44

Types and Values

GApplication

typedef struct _GApplication GApplication;

GApplication is an opaque data structure and can only be accessed using the following functions.

Since: 2.28


struct GApplicationClass

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 */

  /**
   * GApplicationClass::local_command_line:
   * @application: a #GApplication
   * @arguments: (inout) (array zero-terminated=1): array of command line arguments
   * @exit_status: (out): exit status to fill after processing the command line.
   *
   * This virtual function is always invoked in the local instance. It
   * gets passed a pointer to a %NULL-terminated copy of @argv and is
   * expected to remove arguments that it handled (shifting up remaining
   * arguments).
   *
   * 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().
   *
   * See g_application_run() for more details on #GApplication startup.
   *
   * Returns: %TRUE if the commandline has been completely handled
   */
  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);
  void                      (* shutdown)            (GApplication              *application);

  gboolean                  (* dbus_register)       (GApplication              *application,
                                                     GDBusConnection           *connection,
                                                     const gchar               *object_path,
                                                     GError                   **error);
  void                      (* dbus_unregister)     (GApplication              *application,
                                                     GDBusConnection           *connection,
                                                     const gchar               *object_path);
  gint                      (* handle_local_options)(GApplication              *application,
                                                     GVariantDict              *options);
  gboolean                  (* name_lost)           (GApplication              *application);
};

Virtual function table for GApplication.

Members

startup ()

invoked on the primary instance immediately after registration

 

activate ()

invoked on the primary instance when an activation occurs

 

open ()

invoked on the primary instance when there are files to open

 

command_line ()

invoked on the primary instance when a command-line is not handled locally

 

local_command_line ()

invoked (locally). The virtual function has the chance to inspect (and possibly replace) command line arguments. See g_application_run() for more information. Also see the “handle-local-options” signal, which is a simpler alternative to handling some commandline options locally

 

before_emit ()

invoked on the primary instance before 'activate', 'open', 'command-line' or any action invocation, gets the 'platform data' from the calling instance

 

after_emit ()

invoked on the primary instance after 'activate', 'open', 'command-line' or any action invocation, gets the 'platform data' from the calling instance

 

add_platform_data ()

invoked (locally) to add 'platform data' to be sent to the primary instance when activating, opening or invoking actions

 

quit_mainloop ()

Used to be invoked on the primary instance when the use count of the application drops to zero (and after any inactivity timeout, if requested). Not used anymore since 2.32

 

run_mainloop ()

Used to be invoked on the primary instance from g_application_run() if the use-count is non-zero. Since 2.32, GApplication is iterating the main context directly and is not using run_mainloop anymore

 

shutdown ()

invoked only on the registered primary instance immediately after the main loop terminates

 

dbus_register ()

invoked locally during registration, if the application is using its D-Bus backend. You can use this to export extra objects on the bus, that need to exist before the application tries to own the bus name. The function is passed the GDBusConnection to to session bus, and the object path that GApplication will use to export is D-Bus API. If this function returns TRUE, registration will proceed; otherwise registration will abort. Since: 2.34

 

dbus_unregister ()

invoked locally during unregistration, if the application is using its D-Bus backend. Use this to undo anything done by the dbus_register vfunc. Since: 2.34

 

handle_local_options ()

invoked locally after the parsing of the commandline options has occurred. Since: 2.40

 

name_lost ()

invoked when another instance is taking over the name. Since: 2.60

 

Since: 2.28


enum GApplicationFlags

Flags used to define the behaviour of a GApplication.

Members

G_APPLICATION_FLAGS_NONE

Default

 

G_APPLICATION_IS_SERVICE

Run as a service. In this mode, registration fails if the service is already running, and the application will initially wait up to 10 seconds for an initial activation message to arrive.

 

G_APPLICATION_IS_LAUNCHER

Don't try to become the primary instance.

 

G_APPLICATION_HANDLES_OPEN

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.

 

G_APPLICATION_HANDLES_COMMAND_LINE

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.

 

G_APPLICATION_SEND_ENVIRONMENT

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().

 

G_APPLICATION_NON_UNIQUE

Make no attempts to do any of the typical single-instance application negotiation, even if the application ID is given. The application neither attempts to become the owner of the application ID nor does it check if an existing owner already exists. Everything occurs in the local process. Since: 2.30.

 

G_APPLICATION_CAN_OVERRIDE_APP_ID

Allow users to override the application ID from the command line with --gapplication-app-id. Since: 2.48

 

G_APPLICATION_ALLOW_REPLACEMENT

Allow another instance to take over the bus name. Since: 2.60

 

G_APPLICATION_REPLACE

Take over from another instance. This flag is usually set by passing --gapplication-replace on the commandline. Since: 2.60

 

Since: 2.28

Property Details

The “action-group” property

  “action-group”             GActionGroup *

The group of actions that the application exports.

Owner: GApplication

Flags: Write


The “application-id” property

  “application-id”           char *

The unique identifier for the application.

Owner: GApplication

Flags: Read / Write / Construct

Default value: NULL


The “flags” property

  “flags”                    GApplicationFlags

Flags specifying the behaviour of the application.

Owner: GApplication

Flags: Read / Write


The “inactivity-timeout” property

  “inactivity-timeout”       guint

Time (ms) to stay alive after becoming idle.

Owner: GApplication

Flags: Read / Write

Default value: 0


The “is-busy” property

  “is-busy”                  gboolean

Whether the application is currently marked as busy through g_application_mark_busy() or g_application_bind_busy_property().

Owner: GApplication

Flags: Read

Default value: FALSE

Since: 2.44


The “is-registered” property

  “is-registered”            gboolean

If g_application_register() has been called.

Owner: GApplication

Flags: Read

Default value: FALSE


The “is-remote” property

  “is-remote”                gboolean

If this application instance is remote.

Owner: GApplication

Flags: Read

Default value: FALSE


The “resource-base-path” property

  “resource-base-path”       char *

The base resource path for the application.

Owner: GApplication

Flags: Read / Write

Default value: NULL

Signal Details

The “activate” signal

void
user_function (GApplication *application,
               gpointer      user_data)

The ::activate signal is emitted on the primary instance when an activation occurs. See g_application_activate().

Parameters

application

the application

 

user_data

user data set when the signal handler was connected.

 

Flags: Run Last


The “command-line” signal

int
user_function (GApplication            *application,
               GApplicationCommandLine *command_line,
               gpointer                 user_data)

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.

Parameters

application

the application

 

command_line

a GApplicationCommandLine representing the passed commandline

 

user_data

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().

Flags: Run Last


The “handle-local-options” signal

int
user_function (GApplication *application,
               GVariantDict *options,
               gpointer      user_data)

The ::handle-local-options signal is emitted on the local instance after the parsing of the commandline options has occurred.

You can add options to be recognised during commandline option parsing using g_application_add_main_option_entries() and g_application_add_option_group().

Signal handlers can inspect options (along with values pointed to from the arg_data of an installed GOptionEntrys) in order to decide to perform certain actions, including direct local handling (which may be useful for options like --version).

In the event that the application is marked G_APPLICATION_HANDLES_COMMAND_LINE the "normal processing" will send the options dictionary to the primary instance where it can be read with g_application_command_line_get_options_dict(). The signal handler can modify the dictionary before returning, and the modified dictionary will be sent.

In the event that G_APPLICATION_HANDLES_COMMAND_LINE is not set, "normal processing" will treat the remaining uncollected command line arguments as filenames or URIs. If there are no arguments, the application is activated by g_application_activate(). One or more arguments results in a call to g_application_open().

If you want to handle the local commandline arguments for yourself by converting them to calls to g_application_open() or g_action_group_activate_action() then you must be sure to register the application first. You should probably not call g_application_activate() for yourself, however: just return -1 and allow the default handler to do it for you. This will ensure that the --gapplication-service switch works properly (i.e. no activation in that case).

Note that this signal is emitted from the default implementation of local_command_line(). If you override that function and don't chain up then this signal will never be emitted.

You can override local_command_line() if you need more powerful capabilities than what is provided here, but this should not normally be required.

Parameters

application

the application

 

options

the options dictionary

 

user_data

user data set when the signal handler was connected.

 

Returns

an exit code. If you have handled your options and want to exit the process, return a non-negative option, 0 for success, and a positive value for failure. To continue, return -1 to let the default option processing continue.

Flags: Run Last

Since: 2.40


The “name-lost” signal

gboolean
user_function (GApplication *application,
               gpointer      user_data)

The ::name-lost signal is emitted only on the registered primary instance when a new instance has taken over. This can only happen if the application is using the G_APPLICATION_ALLOW_REPLACEMENT flag.

The default handler for this signal calls g_application_quit().

Parameters

application

the application

 

user_data

user data set when the signal handler was connected.

 

Returns

TRUE if the signal has been handled

Flags: Run Last

Since: 2.60


The “open” signal

void
user_function (GApplication *application,
               gpointer      files,
               int           n_files,
               char         *hint,
               gpointer      user_data)

The ::open signal is emitted on the primary instance when there are files to open. See g_application_open() for more information.

Parameters

application

the application

 

files

an array of GFiles.

[array length=n_files][element-type GFile]

n_files

the length of files

 

hint

a hint provided by the calling instance

 

user_data

user data set when the signal handler was connected.

 

Flags: Run Last


The “shutdown” signal

void
user_function (GApplication *application,
               gpointer      user_data)

The ::shutdown signal is emitted only on the registered primary instance immediately after the main loop terminates.

Parameters

application

the application

 

user_data

user data set when the signal handler was connected.

 

Flags: Run Last


The “startup” signal

void
user_function (GApplication *application,
               gpointer      user_data)

The ::startup signal is emitted on the primary instance immediately after registration. See g_application_register().

Parameters

application

the application

 

user_data

user data set when the signal handler was connected.

 

Flags: Run First