class Gio::Application


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 #Gtk::Application 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 #Gtk::Application 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:

The #GApplication::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, #Gtk::Application adds startup notification data in this way.

To parse commandline arguments you may handle the #GApplication::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.

Included Modules

Direct Known Subclasses

Defined in:



Class Method Summary

Instance Method Summary

Instance methods inherited from module Gio::ActionMap

add_action(action : Gio::Action) : Nil add_action, add_action_entries(entries : Enumerable(Gio::ActionEntry), user_data : Pointer(Void)?) : Nil add_action_entries, lookup_action(action_name : String) : Gio::Action? lookup_action, remove_action(action_name : String) : Nil remove_action, to_unsafe to_unsafe

Class methods inherited from module Gio::ActionMap

g_type : UInt64 g_type

Instance methods inherited from module Gio::ActionGroup

action_added(action_name : String) : Nil action_added, action_added_signal action_added_signal, action_enabled(action_name : String) : Bool action_enabled, action_enabled_changed(action_name : String, enabled : Bool) : Nil action_enabled_changed, action_enabled_changed_signal action_enabled_changed_signal, action_parameter_type(action_name : String) : GLib::VariantType? action_parameter_type, action_removed(action_name : String) : Nil action_removed, action_removed_signal action_removed_signal, action_state(action_name : String) : GLib::Variant? action_state, action_state_changed(action_name : String, state : _) : Nil action_state_changed, action_state_changed_signal action_state_changed_signal, action_state_hint(action_name : String) : GLib::Variant? action_state_hint, action_state_type(action_name : String) : GLib::VariantType? action_state_type, activate_action(action_name : String, parameter : _?) : Nil activate_action, change_action_state(action_name : String, value : _) : Nil change_action_state, has_action(action_name : String) : Bool has_action, list_actions : Enumerable(String) list_actions, query_action(action_name : String, enabled : Bool) : Bool query_action, to_unsafe to_unsafe

Class methods inherited from module Gio::ActionGroup

g_type : UInt64 g_type

Instance methods inherited from class GObject::Object

bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void)? data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void)? qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void)?) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void)? steal_data, steal_qdata(quark : UInt32) : Pointer(Void)? steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, cast?(obj : GObject::Object) : self? cast?, new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

compat_control(what : UInt64, data : Pointer(Void)?) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Constructor Detail

def : String?, flags : Gio::ApplicationFlags) : self #

Creates a new #GApplication instance.

If non-nil, 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.

def #

Initialize a new Application.

def*, action_group : Gio::ActionGroup? = nil, application_id : String? = nil, flags : Gio::ApplicationFlags? = nil, inactivity_timeout : UInt32? = nil, is_busy : Bool? = nil, is_registered : Bool? = nil, is_remote : Bool? = nil, resource_base_path : String? = nil) #

Class Method Detail

def self.default : Gio::Application? #

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 nil is returned.

def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.

def self.id_is_valid(application_id : String) : Bool #

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 used an application identifier for an archiving application, it might be named org._7_zip.Archiver.

Instance Method Detail

def action_group=(action_group : Gio::ActionGroup?) : Nil #

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

def activate : Nil #

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.

def activate_signal #

def add_main_option(long_name : String, short_name : Int8, flags : GLib::OptionFlags, arg : GLib::OptionArg, description : String, arg_description : String?) : Nil #

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

The parsed arguments will be packed into a #GVariantDict which is passed to #GApplication::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.

def add_main_option_entries(entries : Enumerable(GLib::OptionEntry)) : Nil #

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 #GApplication::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 nil @arg_data for a non-callback #GOptionEntry. This results in the argument in question being packed into a #GVariantDict which is also passed to #GApplication::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 #GApplication::handle-local-options needs to see the list of filenames, then the use of %G_OPTION_REMAINING is recommended. If @arg_data is nil 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 &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 ^&ay
  • for %G_OPTION_ARG_STRING_ARRAY, use ^a&s
  • for %G_OPTION_ARG_FILENAME_ARRAY, use ^a&ay

def add_main_option_entries(*entries : GLib::OptionEntry) #

def add_option_group(group : GLib::OptionGroup) : Nil #

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 nil @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 #GApplication::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.

def application_id : String? #

Gets the unique identifier for @application.

def application_id=(value : String) : String #

def application_id=(application_id : String?) : Nil #

Sets the unique identifier for @application.

The application id can only be modified if @application has not yet been registered.

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

def bind_busy_property(object : GObject::Object, property : String) : Nil #

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.

def command_line_signal #

def dbus_connection : Gio::DBusConnection? #

Gets the #GDBusConnection being used by the application, or nil.

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

def dbus_object_path : String? #

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

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

def flags : Gio::ApplicationFlags #

Gets the flags for @application.

See #GApplicationFlags.

def flags=(flags : Gio::ApplicationFlags) : Nil #

Sets the flags for @application.

The flags can only be modified if @application has not yet been registered.

See #GApplicationFlags.

def handle_local_options_signal #

def hold : Nil #

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

def inactivity_timeout : UInt32 #

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.

def inactivity_timeout=(inactivity_timeout : UInt32) : Nil #

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.

def is_busy : Bool #

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

def is_busy? : Bool #

def is_registered : Bool #

Checks if @application is registered.

An application is registered if g_application_register() has been successfully called.

def is_registered? : Bool #

def is_remote : Bool #

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

def is_remote? : Bool #

def mark_busy : Nil #

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

The application must be registered before calling this function.

def name_lost_signal #

def open(files : Enumerable(Gio::File), hint : String) : Nil #

Opens the given files.

In essence, this results in the #GApplication::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.

def open_signal #

def option_context_description=(description : String?) : Nil #

Adds a description to the @application option context.

See g_option_context_set_description() for more information.

def option_context_parameter_string=(parameter_string : String?) : Nil #

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.

def option_context_summary=(summary : String?) : Nil #

Adds a summary to the @application option context.

See g_option_context_set_summary() for more information.

def quit : Nil #

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.

def register(cancellable : Gio::Cancellable?) : Bool #

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 #GApplication::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.

def release : Nil #

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

def resource_base_path : String? #

Gets the resource base path of @application.

See g_application_set_resource_base_path() for more information.

def resource_base_path=(value : String) : String #

def resource_base_path=(resource_path : String?) : Nil #

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

The path is used to automatically load various [application resources][gresource] 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 "" then the default resource base path will be "/org/example/app". If this is a #Gtk::Application (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 nil.

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 #GApplication: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.

def run(*argv : String) #

def send_notification(id : String?, notification : Gio::Notification) : Nil #

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

def set_default : Nil #

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

def shutdown_signal #

def startup_signal #

def unbind_busy_property(object : GObject::Object, property : String) : Nil #

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

def unmark_busy : Nil #

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

def withdraw_notification(id : String) : Nil #

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.