2 * Copyright © 2010 Codethink Limited
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2 of the licence or (at
7 * your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Authors: Ryan Lortie <desrt@desrt.ca>
25 #include "gapplication.h"
27 #include "gapplicationcommandline.h"
28 #include "gapplicationimpl.h"
29 #include "gactiongroup.h"
31 #include "gioenumtypes.h"
32 #include "gio-marshal.h"
41 * SECTION:gapplication
42 * @title: GApplication
43 * @short_description: Core application class
45 * A #GApplication is the foundation of an application, unique for a
46 * given application identifier. The GApplication class wraps some
47 * low-level platform-specific services and is intended to act as the
48 * foundation for higher-level application classes such as
49 * #GtkApplication or #MxApplication. In general, you should not use
50 * this class outside of a higher level framework.
52 * One of the core features that GApplication provides is process
53 * uniqueness, in the context of a "session". The session concept is
54 * platform-dependent, but corresponds roughly to a graphical desktop
55 * login. When your application is launched again, its arguments
56 * are passed through platform communication to the already running
57 * program. The already running instance of the program is called the
58 * <firstterm>primary instance</firstterm>.
60 * Before using GApplication, you must choose an "application identifier".
61 * The expected form of an application identifier is very close to that of
62 * of a <ulink url="http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-interface">DBus bus name</ulink>.
63 * Examples include: "com.example.MyApp", "org.example.internal-apps.Calculator".
64 * For details on valid application identifiers, see
65 * g_application_id_is_valid().
67 * The application identifier is claimed by the application as a
68 * well-known bus name on the user's session bus. This means that the
69 * uniqueness of your application is scoped to the current session. It
70 * also means that your application may provide additional services
71 * (through registration of other object paths) at that bus name.
73 * The registration of these object paths should be done with the shared
74 * GDBus session bus. Note that due to the internal architecture of
75 * GDBus, method calls can be dispatched at any time (even if a main
76 * loop is not running). For this reason, you must ensure that any
77 * object paths that you wish to register are registered before
78 * #GApplication attempts to acquire the bus name of your application
79 * (which happens in g_application_register()). Unfortunately, this
80 * means that you can not use g_application_get_is_remote() to decide if
81 * you want to register object paths.
83 * GApplication provides convenient life cycle management by maintaining
84 * a <firstterm>use count</firstterm> for the primary application instance.
85 * The use count can be changed using g_application_hold() and
86 * g_application_release(). If it drops to zero, the application exits.
88 * GApplication also implements the #GActionGroup interface and lets you
89 * easily export actions by adding them with g_application_set_action_group().
90 * When invoking an action by calling g_action_group_activate_action() on
91 * the application, it is always invoked in the primary instance.
93 * There is a number of different entry points into a #GApplication:
95 * <listitem>via 'Activate' (i.e. just starting the application)</listitem>
96 * <listitem>via 'Open' (i.e. opening some files)</listitem>
97 * <listitem>via activating an action</listitem>
99 * The #GApplication::startup signal lets you handle the application
100 * initialization for all of these in a single place.
102 * <example id="gapplication-example-open"><title>Opening files with a GApplication</title>
104 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-open.c">
105 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
110 * <example id="gapplication-example-actions"><title>A GApplication with actions</title>
112 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-actions.c">
113 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
119 struct _GApplicationPrivate
121 GApplicationFlags flags;
124 GActionGroup *actions;
127 guint inactivity_timeout_id;
128 guint inactivity_timeout;
131 guint is_registered : 1;
134 GHashTable *remote_actions; /* string -> RemoteActionInfo */
135 GApplicationImpl *impl;
145 PROP_INACTIVITY_TIMEOUT,
159 static guint g_application_signals[NR_SIGNALS];
161 static void g_application_action_group_iface_init (GActionGroupInterface *);
162 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
163 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
164 g_application_action_group_iface_init))
166 /* vfunc defaults {{{1 */
168 g_application_real_before_emit (GApplication *application,
169 GVariant *platform_data)
174 g_application_real_after_emit (GApplication *application,
175 GVariant *platform_data)
180 g_application_real_startup (GApplication *application)
185 g_application_real_activate (GApplication *application)
187 if (!g_signal_has_handler_pending (application,
188 g_application_signals[SIGNAL_ACTIVATE],
190 G_APPLICATION_GET_CLASS (application)->activate == g_application_real_activate)
192 static gboolean warned;
197 g_warning ("Your application does not implement "
198 "g_application_activate() and has no handlers connected "
199 "to the 'activate' signal. It should do one of these.");
205 g_application_real_open (GApplication *application,
210 if (!g_signal_has_handler_pending (application,
211 g_application_signals[SIGNAL_OPEN],
213 G_APPLICATION_GET_CLASS (application)->open == g_application_real_open)
215 static gboolean warned;
220 g_warning ("Your application claims to support opening files "
221 "but does not implement g_application_open() and has no "
222 "handlers connected to the 'open' signal.");
228 g_application_real_command_line (GApplication *application,
229 GApplicationCommandLine *cmdline)
231 static gboolean warned;
236 g_warning ("Your application claims to support custom command line "
237 "handling but does not implement g_application_command_line() "
238 "and has no handlers connected to the 'command-line' signal.");
246 g_application_real_local_command_line (GApplication *application,
250 if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
255 GError *error = NULL;
258 if (!g_application_register (application, NULL, &error))
260 g_critical ("%s", error->message);
261 g_error_free (error);
266 n_args = g_strv_length (*arguments);
268 if (application->priv->flags & G_APPLICATION_IS_SERVICE)
270 if ((*exit_status = n_args > 1))
272 g_printerr ("GApplication service mode takes no arguments.\n");
273 application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
281 g_application_activate (application);
287 if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
289 g_critical ("This application can not open files.");
298 n_files = n_args - 1;
299 files = g_new (GFile *, n_files);
301 for (i = 0; i < n_files; i++)
302 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
304 g_application_open (application, files, n_files, "");
306 for (i = 0; i < n_files; i++)
307 g_object_unref (files[i]);
319 g_application_real_add_platform_data (GApplication *application,
320 GVariantBuilder *builder)
325 g_application_real_quit_mainloop (GApplication *application)
327 if (application->priv->mainloop != NULL)
328 g_main_loop_quit (application->priv->mainloop);
332 g_application_real_run_mainloop (GApplication *application)
334 if (application->priv->mainloop == NULL)
335 application->priv->mainloop = g_main_loop_new (NULL, FALSE);
337 g_main_loop_run (application->priv->mainloop);
340 /* GObject implementation stuff {{{1 */
342 g_application_set_property (GObject *object,
347 GApplication *application = G_APPLICATION (object);
351 case PROP_APPLICATION_ID:
352 g_application_set_application_id (application,
353 g_value_get_string (value));
357 g_application_set_flags (application, g_value_get_flags (value));
360 case PROP_INACTIVITY_TIMEOUT:
361 g_application_set_inactivity_timeout (application,
362 g_value_get_uint (value));
365 case PROP_ACTION_GROUP:
366 g_application_set_action_group (application,
367 g_value_get_object (value));
371 g_assert_not_reached ();
376 * g_application_set_action_group:
377 * @application: a #GApplication
378 * @action_group: (allow-none): a #GActionGroup, or %NULL
380 * Sets or unsets the group of actions associated with the application.
382 * These actions are the actions that can be remotely invoked.
384 * It is an error to call this function after the application has been
390 g_application_set_action_group (GApplication *application,
391 GActionGroup *action_group)
393 g_return_if_fail (G_IS_APPLICATION (application));
394 g_return_if_fail (!application->priv->is_registered);
396 if (application->priv->actions != NULL)
397 g_object_unref (application->priv->actions);
399 application->priv->actions = action_group;
401 if (application->priv->actions != NULL)
402 g_object_ref (application->priv->actions);
406 g_application_get_property (GObject *object,
411 GApplication *application = G_APPLICATION (object);
415 case PROP_APPLICATION_ID:
416 g_value_set_string (value,
417 g_application_get_application_id (application));
421 g_value_set_flags (value,
422 g_application_get_flags (application));
425 case PROP_IS_REGISTERED:
426 g_value_set_boolean (value,
427 g_application_get_is_registered (application));
431 g_value_set_boolean (value,
432 g_application_get_is_remote (application));
435 case PROP_INACTIVITY_TIMEOUT:
436 g_value_set_uint (value,
437 g_application_get_inactivity_timeout (application));
441 g_assert_not_reached ();
446 g_application_constructed (GObject *object)
448 GApplication *application = G_APPLICATION (object);
450 g_assert (application->priv->id != NULL);
454 g_application_finalize (GObject *object)
456 GApplication *application = G_APPLICATION (object);
458 if (application->priv->impl)
459 g_application_impl_destroy (application->priv->impl);
460 g_free (application->priv->id);
462 if (application->priv->mainloop)
463 g_main_loop_unref (application->priv->mainloop);
465 G_OBJECT_CLASS (g_application_parent_class)
470 g_application_init (GApplication *application)
472 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
474 GApplicationPrivate);
478 g_application_class_init (GApplicationClass *class)
480 GObjectClass *object_class = G_OBJECT_CLASS (class);
482 object_class->constructed = g_application_constructed;
483 object_class->finalize = g_application_finalize;
484 object_class->get_property = g_application_get_property;
485 object_class->set_property = g_application_set_property;
487 class->before_emit = g_application_real_before_emit;
488 class->after_emit = g_application_real_after_emit;
489 class->startup = g_application_real_startup;
490 class->activate = g_application_real_activate;
491 class->open = g_application_real_open;
492 class->command_line = g_application_real_command_line;
493 class->local_command_line = g_application_real_local_command_line;
494 class->add_platform_data = g_application_real_add_platform_data;
495 class->quit_mainloop = g_application_real_quit_mainloop;
496 class->run_mainloop = g_application_real_run_mainloop;
498 g_object_class_install_property (object_class, PROP_APPLICATION_ID,
499 g_param_spec_string ("application-id",
500 P_("Application identifier"),
501 P_("The unique identifier for the application"),
502 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
503 G_PARAM_STATIC_STRINGS));
505 g_object_class_install_property (object_class, PROP_FLAGS,
506 g_param_spec_flags ("flags",
507 P_("Application flags"),
508 P_("Flags specifying the behaviour of the application"),
509 G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
510 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
512 g_object_class_install_property (object_class, PROP_IS_REGISTERED,
513 g_param_spec_boolean ("is-registered",
515 P_("If g_application_register() has been called"),
516 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
518 g_object_class_install_property (object_class, PROP_IS_REMOTE,
519 g_param_spec_boolean ("is-remote",
521 P_("If this application instance is remote"),
522 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
524 g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
525 g_param_spec_uint ("inactivity-timeout",
526 P_("Inactivity timeout"),
527 P_("Iime (ms) to stay alive after becoming idle"),
529 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
531 g_object_class_install_property (object_class, PROP_ACTION_GROUP,
532 g_param_spec_object ("action-group",
534 P_("The group of actions that the application exports"),
536 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
539 * GApplication::startup:
540 * @application: the application
542 * The ::startup signal is emitted on the primary instance immediately
543 * after registration. See g_application_register().
545 g_application_signals[SIGNAL_STARTUP] =
546 g_signal_new ("startup", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
547 G_STRUCT_OFFSET (GApplicationClass, startup),
548 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
551 * GApplication::activate:
552 * @application: the application
554 * The ::activate signal is emitted on the primary instance when an
555 * activation occurs. See g_application_activate().
557 g_application_signals[SIGNAL_ACTIVATE] =
558 g_signal_new ("activate", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
559 G_STRUCT_OFFSET (GApplicationClass, activate),
560 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
564 * GApplication::open:
565 * @application: the application
566 * @files: (array length=n_files) (element-type GFile): an array of #GFiles
567 * @n_files: the length of @files
568 * @hint: a hint provided by the calling instance
570 * The ::open signal is emitted on the primary instance when there are
571 * files to open. See g_application_open() for more information.
573 g_application_signals[SIGNAL_OPEN] =
574 g_signal_new ("open", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
575 G_STRUCT_OFFSET (GApplicationClass, open),
576 NULL, NULL, _gio_marshal_VOID__POINTER_INT_STRING,
577 G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
580 * GApplication::command-line:
581 * @application: the application
582 * @command_line: a #GApplicationCommandLine representing the
585 * The ::command-line signal is emitted on the primary instance when
586 * a commandline is not handled locally. See g_application_run() and
587 * the #GApplicationCommandline documentation for more information.
589 * Returns: An integer that is set as the exit status for the calling
590 * process. See g_application_command_line_set_exit_status().
592 g_application_signals[SIGNAL_COMMAND_LINE] =
593 g_signal_new ("command-line", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
594 G_STRUCT_OFFSET (GApplicationClass, command_line),
595 g_signal_accumulator_first_wins, NULL,
596 _gio_marshal_INT__OBJECT,
597 G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
599 g_type_class_add_private (class, sizeof (GApplicationPrivate));
603 get_platform_data (GApplication *application)
605 GVariantBuilder *builder;
608 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
611 gchar *cwd = g_get_current_dir ();
612 g_variant_builder_add (builder, "{sv}", "cwd",
613 g_variant_new_bytestring (cwd));
617 if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
622 envp = g_get_environ ();
623 array = g_variant_new_bytestring_array ((const gchar **) envp, -1);
626 g_variant_builder_add (builder, "{sv}", "environ", array);
629 G_APPLICATION_GET_CLASS (application)->
630 add_platform_data (application, builder);
632 result = g_variant_builder_end (builder);
633 g_variant_builder_unref (builder);
638 /* Application ID validity {{{1 */
641 * g_application_id_is_valid:
642 * @application_id: a potential application identifier
643 * @returns: %TRUE if @application_id is valid
645 * Checks if @application_id is a valid application identifier.
647 * A valid ID is required for calls to g_application_new() and
648 * g_application_set_application_id().
650 * For convenience, the restrictions on application identifiers are
653 * <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-." and must not begin with a digit.</listitem>
654 * <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least three elements).</listitem>
655 * <listitem>Application identifiers must not begin or end with a '.' (period) character.</listitem>
656 * <listitem>Application identifiers must not contain consecutive '.' (period) characters.</listitem>
657 * <listitem>Application identifiers must not exceed 255 characters.</listitem>
661 g_application_id_is_valid (const gchar *application_id)
667 len = strlen (application_id);
672 if (!g_ascii_isalpha (application_id[0]))
675 if (application_id[len-1] == '.')
681 for (; *application_id; application_id++)
683 if (g_ascii_isalnum (*application_id) ||
684 (*application_id == '-') ||
685 (*application_id == '_'))
689 else if (allow_dot && *application_id == '.')
704 /* Public Constructor {{{1 */
707 * @application_id: the application id
708 * @flags: the application flags
709 * @returns: a new #GApplication instance
711 * Creates a new #GApplication instance.
713 * This function calls g_type_init() for you.
715 * The application id must be valid. See g_application_id_is_valid().
718 g_application_new (const gchar *application_id,
719 GApplicationFlags flags)
721 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
725 return g_object_new (G_TYPE_APPLICATION,
726 "application-id", application_id,
731 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
733 * g_application_get_application_id:
734 * @application: a #GApplication
735 * @returns: the identifier for @application, owned by @application
737 * Gets the unique identifier for @application.
742 g_application_get_application_id (GApplication *application)
744 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
746 return application->priv->id;
750 * g_application_set_application_id:
751 * @application: a #GApplication
752 * @application_id: the identifier for @application
754 * Sets the unique identifier for @application.
756 * The application id can only be modified if @application has not yet
759 * The application id must be valid. See g_application_id_is_valid().
764 g_application_set_application_id (GApplication *application,
765 const gchar *application_id)
767 g_return_if_fail (G_IS_APPLICATION (application));
769 if (g_strcmp0 (application->priv->id, application_id) != 0)
771 g_return_if_fail (g_application_id_is_valid (application_id));
772 g_return_if_fail (!application->priv->is_registered);
774 g_free (application->priv->id);
775 application->priv->id = g_strdup (application_id);
777 g_object_notify (G_OBJECT (application), "application-id");
782 * g_application_get_flags:
783 * @application: a #GApplication
784 * @returns: the flags for @application
786 * Gets the flags for @application.
788 * See #GApplicationFlags.
793 g_application_get_flags (GApplication *application)
795 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
797 return application->priv->flags;
801 * g_application_set_flags:
802 * @application: a #GApplication
803 * @flags: the flags for @application
805 * Sets the flags for @application.
807 * The flags can only be modified if @application has not yet been
810 * See #GApplicationFlags.
815 g_application_set_flags (GApplication *application,
816 GApplicationFlags flags)
818 g_return_if_fail (G_IS_APPLICATION (application));
820 if (application->priv->flags != flags)
822 g_return_if_fail (!application->priv->is_registered);
824 application->priv->flags = flags;
826 g_object_notify (G_OBJECT (application), "flags");
831 * g_application_get_inactivity_timeout:
832 * @application: a #GApplication
834 * Gets the current inactivity timeout for the application.
836 * This is the amount of time (in milliseconds) after the last call to
837 * g_application_release() before the application stops running.
839 * Returns: the timeout, in milliseconds
844 g_application_get_inactivity_timeout (GApplication *application)
846 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
848 return application->priv->inactivity_timeout;
852 * g_application_set_inactivity_timeout:
853 * @application: a #GApplication
854 * @inactivity_timeout: the timeout, in milliseconds
856 * Sets the current inactivity timeout for the application.
858 * This is the amount of time (in milliseconds) after the last call to
859 * g_application_release() before the application stops running.
861 * This call has no side effects of its own. The value set here is only
862 * used for next time g_application_release() drops the use count to
863 * zero. Any timeouts currently in progress are not impacted.
865 * Returns: the timeout, in milliseconds
870 g_application_set_inactivity_timeout (GApplication *application,
871 guint inactivity_timeout)
873 g_return_if_fail (G_IS_APPLICATION (application));
875 if (application->priv->inactivity_timeout != inactivity_timeout)
877 application->priv->inactivity_timeout = inactivity_timeout;
879 g_object_notify (G_OBJECT (application), "inactivity-timeout");
882 /* Read-only property getters (is registered, is remote) {{{1 */
884 * g_application_get_is_registered:
885 * @application: a #GApplication
886 * @returns: %TRUE if @application is registered
888 * Checks if @application is registered.
890 * An application is registered if g_application_register() has been
891 * successfully called.
896 g_application_get_is_registered (GApplication *application)
898 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
900 return application->priv->is_registered;
904 * g_application_get_is_remote:
905 * @application: a #GApplication
906 * @returns: %TRUE if @application is remote
908 * Checks if @application is remote.
910 * If @application is remote then it means that another instance of
911 * application already exists (the 'primary' instance). Calls to
912 * perform actions on @application will result in the actions being
913 * performed by the primary instance.
915 * The value of this property can not be accessed before
916 * g_application_register() has been called. See
917 * g_application_get_is_registered().
922 g_application_get_is_remote (GApplication *application)
924 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
925 g_return_val_if_fail (application->priv->is_registered, FALSE);
927 return application->priv->is_remote;
932 * g_application_register:
933 * @application: a #GApplication
934 * @cancellable: a #GCancellable, or %NULL
935 * @error: a pointer to a NULL #GError, or %NULL
936 * @returns: %TRUE if registration succeeded
938 * Attempts registration of the application.
940 * This is the point at which the application discovers if it is the
941 * primary instance or merely acting as a remote for an already-existing
942 * primary instance. This is implemented by attempting to acquire the
943 * application identifier as a unique bus name on the session bus using
946 * Due to the internal architecture of GDBus, method calls can be
947 * dispatched at any time (even if a main loop is not running). For
948 * this reason, you must ensure that any object paths that you wish to
949 * register are registered before calling this function.
951 * If the application has already been registered then %TRUE is
952 * returned with no work performed.
954 * The #GApplication::startup signal is emitted if registration succeeds
955 * and @application is the primary instance.
957 * In the event of an error (such as @cancellable being cancelled, or a
958 * failure to connect to the session bus), %FALSE is returned and @error
959 * is set appropriately.
961 * Note: the return value of this function is not an indicator that this
962 * instance is or is not the primary instance of the application. See
963 * g_application_get_is_remote() for that.
968 g_application_register (GApplication *application,
969 GCancellable *cancellable,
972 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
974 if (!application->priv->is_registered)
976 application->priv->impl =
977 g_application_impl_register (application, application->priv->id,
978 application->priv->flags,
979 &application->priv->remote_actions,
982 if (application->priv->impl == NULL)
985 application->priv->is_remote = application->priv->remote_actions != NULL;
986 application->priv->is_registered = TRUE;
988 g_object_notify (G_OBJECT (application), "is-registered");
990 if (!application->priv->is_remote)
991 g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
997 /* Hold/release {{{1 */
999 * g_application_hold:
1000 * @application: a #GApplication
1002 * Increases the use count of @application.
1004 * Use this function to indicate that the application has a reason to
1005 * continue to run. For example, g_application_hold() is called by GTK+
1006 * when a toplevel window is on the screen.
1008 * To cancel the hold, call g_application_release().
1011 g_application_hold (GApplication *application)
1013 if (application->priv->inactivity_timeout_id)
1015 g_source_remove (application->priv->inactivity_timeout_id);
1016 application->priv->inactivity_timeout_id = 0;
1019 application->priv->use_count++;
1023 inactivity_timeout_expired (gpointer data)
1025 GApplication *application = G_APPLICATION (data);
1027 G_APPLICATION_GET_CLASS (application)
1028 ->quit_mainloop (application);
1035 * g_application_release:
1036 * @application: a #GApplication
1038 * Decrease the use count of @application.
1040 * When the use count reaches zero, the application will stop running.
1042 * Never call this function except to cancel the effect of a previous
1043 * call to g_application_hold().
1046 g_application_release (GApplication *application)
1048 application->priv->use_count--;
1050 if (application->priv->use_count == 0)
1052 if (application->priv->inactivity_timeout)
1053 application->priv->inactivity_timeout_id =
1054 g_timeout_add (application->priv->inactivity_timeout,
1055 inactivity_timeout_expired, application);
1058 G_APPLICATION_GET_CLASS (application)
1059 ->quit_mainloop (application);
1063 /* Activate, Open {{{1 */
1065 * g_application_activate:
1066 * @application: a #GApplication
1068 * Activates the application.
1070 * In essence, this results in the #GApplication::activate() signal being
1071 * emitted in the primary instance.
1073 * The application must be registered before calling this function.
1078 g_application_activate (GApplication *application)
1080 g_return_if_fail (G_IS_APPLICATION (application));
1081 g_return_if_fail (application->priv->is_registered);
1083 if (application->priv->is_remote)
1084 g_application_impl_activate (application->priv->impl,
1085 get_platform_data (application));
1088 g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
1092 * g_application_open:
1093 * @application: a #GApplication
1094 * @files: (array length=n_files): an array of #GFiles to open
1095 * @n_files: the length of the @files array
1096 * @hint: a hint (or ""), but never %NULL
1098 * Opens the given files.
1100 * In essence, this results in the #GApplication::open signal being emitted
1101 * in the primary instance.
1103 * @n_files must be greater than zero.
1105 * @hint is simply passed through to the ::open signal. It is
1106 * intended to be used by applications that have multiple modes for
1107 * opening files (eg: "view" vs "edit", etc). Unless you have a need
1108 * for this functionality, you should use "".
1110 * The application must be registered before calling this function
1111 * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
1116 g_application_open (GApplication *application,
1121 g_return_if_fail (G_IS_APPLICATION (application));
1122 g_return_if_fail (application->priv->flags &
1123 G_APPLICATION_HANDLES_OPEN);
1124 g_return_if_fail (application->priv->is_registered);
1126 if (application->priv->is_remote)
1127 g_application_impl_open (application->priv->impl,
1128 files, n_files, hint,
1129 get_platform_data (application));
1132 g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
1133 0, files, n_files, hint);
1138 * g_application_run:
1139 * @application: a #GApplication
1140 * @argc: the argc from main()
1141 * @argv: (array length=argc): the argv from main()
1142 * @returns: the exit status
1144 * Runs the application.
1146 * This function is intended to be run from main() and its return value
1147 * is intended to be returned by main().
1149 * First, the local_command_line() virtual function is invoked. This
1150 * function always runs on the local instance. If that function returns
1151 * %FALSE then the application is registered and the #GApplication::command-line
1152 * signal is emitted in the primary instance (which may or may not be
1155 * If the application has the %G_APPLICATION_HANDLES_COMMAND_LINE
1156 * flag set then the default implementation of local_command_line()
1157 * always returns %FALSE immediately, resulting in the commandline
1158 * always being handled in the primary instance.
1160 * Otherwise, the default implementation of local_command_line() tries
1161 * to do a couple of things that are probably reasonable for most
1162 * applications. First, g_application_register() is called to attempt
1163 * to register the application. If that works, then the command line
1164 * arguments are inspected. If no commandline arguments are given, then
1165 * g_application_activate() is called. If commandline arguments are
1166 * given and the %G_APPLICATION_HANDLES_OPEN flag is set then they
1167 * are assumed to be filenames and g_application_open() is called.
1169 * If you are interested in doing more complicated local handling of the
1170 * commandline then you should implement your own #GApplication subclass
1171 * and override local_command_line(). See
1172 * <xref linkend="gapplication-example-cmdline2"/> for an example.
1174 * If, after the above is done, the use count of the application is zero
1175 * then the exit status is returned immediately. If the use count is
1176 * non-zero then the mainloop is run until the use count falls to zero,
1177 * at which point 0 is returned.
1179 * If the %G_APPLICATION_IS_SERVICE flag is set, then the exiting at
1180 * use count of zero is delayed for a while (ie: the instance stays
1181 * around to provide its <emphasis>service</emphasis> to others).
1186 g_application_run (GApplication *application,
1194 g_return_val_if_fail (G_IS_APPLICATION (application), 1);
1195 g_return_val_if_fail (argc == 0 || argv != NULL, 1);
1197 arguments = g_new (gchar *, argc + 1);
1198 for (i = 0; i < argc; i++)
1199 arguments[i] = g_strdup (argv[i]);
1200 arguments[i] = NULL;
1202 if (g_get_prgname () == NULL && argc > 0)
1206 prgname = g_path_get_basename (argv[0]);
1207 g_set_prgname (prgname);
1211 if (!G_APPLICATION_GET_CLASS (application)
1212 ->local_command_line (application, &arguments, &status))
1214 GError *error = NULL;
1216 if (!g_application_register (application, NULL, &error))
1218 g_printerr ("%s", error->message);
1219 g_error_free (error);
1223 if (application->priv->is_remote)
1225 GVariant *platform_data;
1227 platform_data = get_platform_data (application);
1228 status = g_application_impl_command_line (application->priv->impl,
1229 arguments, platform_data);
1233 GApplicationCommandLine *cmdline;
1236 v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
1237 cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
1238 "arguments", v, NULL);
1239 g_signal_emit (application,
1240 g_application_signals[SIGNAL_COMMAND_LINE],
1241 0, cmdline, &status);
1242 g_object_unref (cmdline);
1246 g_strfreev (arguments);
1248 if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
1249 application->priv->is_registered &&
1250 !application->priv->use_count &&
1251 !application->priv->inactivity_timeout_id)
1253 application->priv->inactivity_timeout_id =
1254 g_timeout_add (10000, inactivity_timeout_expired, application);
1257 if (application->priv->use_count ||
1258 application->priv->inactivity_timeout_id)
1260 G_APPLICATION_GET_CLASS (application)
1261 ->run_mainloop (application);
1265 if (application->priv->impl)
1266 g_application_impl_flush (application->priv->impl);
1272 g_application_has_action (GActionGroup *action_group,
1273 const gchar *action_name)
1275 GApplication *application = G_APPLICATION (action_group);
1277 g_return_val_if_fail (application->priv->is_registered, FALSE);
1279 if (application->priv->remote_actions != NULL)
1280 return g_hash_table_lookup (application->priv->remote_actions,
1281 action_name) != NULL;
1283 return application->priv->actions &&
1284 g_action_group_has_action (application->priv->actions, action_name);
1288 g_application_list_actions (GActionGroup *action_group)
1290 GApplication *application = G_APPLICATION (action_group);
1292 g_return_val_if_fail (application->priv->is_registered, NULL);
1294 if (application->priv->remote_actions != NULL)
1296 GHashTableIter iter;
1301 n = g_hash_table_size (application->priv->remote_actions);
1302 keys = g_new (gchar *, n + 1);
1304 g_hash_table_iter_init (&iter, application->priv->remote_actions);
1305 while (g_hash_table_iter_next (&iter, &key, NULL))
1306 keys[i++] = g_strdup (key);
1307 g_assert_cmpint (i, ==, n);
1313 else if (application->priv->actions != NULL)
1314 return g_action_group_list_actions (application->priv->actions);
1317 /* empty string array */
1318 return g_new0 (gchar *, 1);
1322 g_application_get_action_enabled (GActionGroup *action_group,
1323 const gchar *action_name)
1325 GApplication *application = G_APPLICATION (action_group);
1327 g_return_val_if_fail (application->priv->actions != NULL, FALSE);
1328 g_return_val_if_fail (application->priv->is_registered, FALSE);
1330 if (application->priv->remote_actions)
1332 RemoteActionInfo *info;
1334 info = g_hash_table_lookup (application->priv->remote_actions,
1337 return info && info->enabled;
1340 return g_action_group_get_action_enabled (application->priv->actions,
1344 static const GVariantType *
1345 g_application_get_action_parameter_type (GActionGroup *action_group,
1346 const gchar *action_name)
1348 GApplication *application = G_APPLICATION (action_group);
1350 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1351 g_return_val_if_fail (application->priv->is_registered, NULL);
1353 if (application->priv->remote_actions)
1355 RemoteActionInfo *info;
1357 info = g_hash_table_lookup (application->priv->remote_actions,
1361 return info->parameter_type;
1366 return g_action_group_get_action_parameter_type (application->priv->actions,
1370 static const GVariantType *
1371 g_application_get_action_state_type (GActionGroup *action_group,
1372 const gchar *action_name)
1374 GApplication *application = G_APPLICATION (action_group);
1376 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1377 g_return_val_if_fail (application->priv->is_registered, NULL);
1379 if (application->priv->remote_actions)
1381 RemoteActionInfo *info;
1383 info = g_hash_table_lookup (application->priv->remote_actions,
1386 if (info && info->state)
1387 return g_variant_get_type (info->state);
1392 return g_action_group_get_action_state_type (application->priv->actions,
1397 g_application_get_action_state (GActionGroup *action_group,
1398 const gchar *action_name)
1400 GApplication *application = G_APPLICATION (action_group);
1402 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1403 g_return_val_if_fail (application->priv->is_registered, NULL);
1405 if (application->priv->remote_actions)
1407 RemoteActionInfo *info;
1409 info = g_hash_table_lookup (application->priv->remote_actions,
1412 if (info && info->state)
1413 return g_variant_ref (info->state);
1418 return g_action_group_get_action_state (application->priv->actions,
1423 g_application_change_action_state (GActionGroup *action_group,
1424 const gchar *action_name,
1427 GApplication *application = G_APPLICATION (action_group);
1429 g_return_if_fail (application->priv->is_registered);
1431 if (application->priv->is_remote)
1432 g_application_impl_change_action_state (application->priv->impl,
1434 get_platform_data (application));
1437 g_action_group_change_action_state (application->priv->actions,
1438 action_name, value);
1442 g_application_activate_action (GActionGroup *action_group,
1443 const gchar *action_name,
1444 GVariant *parameter)
1446 GApplication *application = G_APPLICATION (action_group);
1448 g_return_if_fail (application->priv->is_registered);
1450 if (application->priv->is_remote)
1451 g_application_impl_activate_action (application->priv->impl,
1452 action_name, parameter,
1453 get_platform_data (application));
1456 g_action_group_activate_action (application->priv->actions,
1457 action_name, parameter);
1461 g_application_action_group_iface_init (GActionGroupInterface *iface)
1463 iface->has_action = g_application_has_action;
1464 iface->list_actions = g_application_list_actions;
1466 iface->get_action_enabled = g_application_get_action_enabled;
1467 iface->get_action_parameter_type = g_application_get_action_parameter_type;
1468 iface->get_action_state_type = g_application_get_action_state_type;
1469 iface->get_action_state = g_application_get_action_state;
1470 iface->change_action_state = g_application_change_action_state;
1471 iface->activate_action = g_application_activate_action;
1475 /* vim:set foldmethod=marker: */