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>by handling a command-line</listitem>
98 * <listitem>via activating an action</listitem>
100 * The #GApplication::startup signal lets you handle the application
101 * initialization for all of these in a single place.
103 * Regardless of which of these entry points is used to start the application,
104 * GApplication passes some <firstterm id="platform-data">platform
105 * data</firstterm> from the launching instance to the primary instance,
106 * in the form of a #GVariant dictionary mapping strings to variants.
107 * To use platform data, override the @before_emit or @after_emit virtual
108 * functions in your #GApplication subclass.
110 * As the name indicates, the platform data may vary depending on the
111 * operating system, but it always includes the current directory (key
112 * "cwd"), and optionally the environment (ie the set of environment
113 * variables and their values) of the calling process (key "environ").
114 * The environment is only added to the platform data if the
115 * #G_APPLICATION_SEND_ENVIONMENT flag is set. GApplication subclasses
116 * can add their own platform data by overriding the @add_platform_data
117 * virtual function. For instance, #GtkApplication adds startup notification
118 * information in this way.
120 * <example id="gapplication-example-open"><title>Opening files with a GApplication</title>
122 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-open.c">
123 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
128 * <example id="gapplication-example-actions"><title>A GApplication with actions</title>
130 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-actions.c">
131 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
137 struct _GApplicationPrivate
139 GApplicationFlags flags;
142 GActionGroup *actions;
145 guint inactivity_timeout_id;
146 guint inactivity_timeout;
149 guint is_registered : 1;
152 GHashTable *remote_actions; /* string -> RemoteActionInfo */
153 GApplicationImpl *impl;
163 PROP_INACTIVITY_TIMEOUT,
177 static guint g_application_signals[NR_SIGNALS];
179 static void g_application_action_group_iface_init (GActionGroupInterface *);
180 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
181 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
182 g_application_action_group_iface_init))
184 /* vfunc defaults {{{1 */
186 g_application_real_before_emit (GApplication *application,
187 GVariant *platform_data)
192 g_application_real_after_emit (GApplication *application,
193 GVariant *platform_data)
198 g_application_real_startup (GApplication *application)
203 g_application_real_activate (GApplication *application)
205 if (!g_signal_has_handler_pending (application,
206 g_application_signals[SIGNAL_ACTIVATE],
208 G_APPLICATION_GET_CLASS (application)->activate == g_application_real_activate)
210 static gboolean warned;
215 g_warning ("Your application does not implement "
216 "g_application_activate() and has no handlers connected "
217 "to the 'activate' signal. It should do one of these.");
223 g_application_real_open (GApplication *application,
228 if (!g_signal_has_handler_pending (application,
229 g_application_signals[SIGNAL_OPEN],
231 G_APPLICATION_GET_CLASS (application)->open == g_application_real_open)
233 static gboolean warned;
238 g_warning ("Your application claims to support opening files "
239 "but does not implement g_application_open() and has no "
240 "handlers connected to the 'open' signal.");
246 g_application_real_command_line (GApplication *application,
247 GApplicationCommandLine *cmdline)
249 static gboolean warned;
254 g_warning ("Your application claims to support custom command line "
255 "handling but does not implement g_application_command_line() "
256 "and has no handlers connected to the 'command-line' signal.");
264 g_application_real_local_command_line (GApplication *application,
268 if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
273 GError *error = NULL;
276 if (!g_application_register (application, NULL, &error))
278 g_critical ("%s", error->message);
279 g_error_free (error);
284 n_args = g_strv_length (*arguments);
286 if (application->priv->flags & G_APPLICATION_IS_SERVICE)
288 if ((*exit_status = n_args > 1))
290 g_printerr ("GApplication service mode takes no arguments.\n");
291 application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
299 g_application_activate (application);
305 if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
307 g_critical ("This application can not open files.");
316 n_files = n_args - 1;
317 files = g_new (GFile *, n_files);
319 for (i = 0; i < n_files; i++)
320 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
322 g_application_open (application, files, n_files, "");
324 for (i = 0; i < n_files; i++)
325 g_object_unref (files[i]);
337 g_application_real_add_platform_data (GApplication *application,
338 GVariantBuilder *builder)
343 g_application_real_quit_mainloop (GApplication *application)
345 if (application->priv->mainloop != NULL)
346 g_main_loop_quit (application->priv->mainloop);
350 g_application_real_run_mainloop (GApplication *application)
352 if (application->priv->mainloop == NULL)
353 application->priv->mainloop = g_main_loop_new (NULL, FALSE);
355 g_main_loop_run (application->priv->mainloop);
358 /* GObject implementation stuff {{{1 */
360 g_application_set_property (GObject *object,
365 GApplication *application = G_APPLICATION (object);
369 case PROP_APPLICATION_ID:
370 g_application_set_application_id (application,
371 g_value_get_string (value));
375 g_application_set_flags (application, g_value_get_flags (value));
378 case PROP_INACTIVITY_TIMEOUT:
379 g_application_set_inactivity_timeout (application,
380 g_value_get_uint (value));
383 case PROP_ACTION_GROUP:
384 g_application_set_action_group (application,
385 g_value_get_object (value));
389 g_assert_not_reached ();
394 * g_application_set_action_group:
395 * @application: a #GApplication
396 * @action_group: (allow-none): a #GActionGroup, or %NULL
398 * Sets or unsets the group of actions associated with the application.
400 * These actions are the actions that can be remotely invoked.
402 * It is an error to call this function after the application has been
408 g_application_set_action_group (GApplication *application,
409 GActionGroup *action_group)
411 g_return_if_fail (G_IS_APPLICATION (application));
412 g_return_if_fail (!application->priv->is_registered);
414 if (application->priv->actions != NULL)
415 g_object_unref (application->priv->actions);
417 application->priv->actions = action_group;
419 if (application->priv->actions != NULL)
420 g_object_ref (application->priv->actions);
424 g_application_get_property (GObject *object,
429 GApplication *application = G_APPLICATION (object);
433 case PROP_APPLICATION_ID:
434 g_value_set_string (value,
435 g_application_get_application_id (application));
439 g_value_set_flags (value,
440 g_application_get_flags (application));
443 case PROP_IS_REGISTERED:
444 g_value_set_boolean (value,
445 g_application_get_is_registered (application));
449 g_value_set_boolean (value,
450 g_application_get_is_remote (application));
453 case PROP_INACTIVITY_TIMEOUT:
454 g_value_set_uint (value,
455 g_application_get_inactivity_timeout (application));
459 g_assert_not_reached ();
464 g_application_constructed (GObject *object)
466 GApplication *application = G_APPLICATION (object);
468 g_assert (application->priv->id != NULL);
472 g_application_finalize (GObject *object)
474 GApplication *application = G_APPLICATION (object);
476 if (application->priv->impl)
477 g_application_impl_destroy (application->priv->impl);
478 g_free (application->priv->id);
480 if (application->priv->mainloop)
481 g_main_loop_unref (application->priv->mainloop);
483 G_OBJECT_CLASS (g_application_parent_class)
488 g_application_init (GApplication *application)
490 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
492 GApplicationPrivate);
496 g_application_class_init (GApplicationClass *class)
498 GObjectClass *object_class = G_OBJECT_CLASS (class);
500 object_class->constructed = g_application_constructed;
501 object_class->finalize = g_application_finalize;
502 object_class->get_property = g_application_get_property;
503 object_class->set_property = g_application_set_property;
505 class->before_emit = g_application_real_before_emit;
506 class->after_emit = g_application_real_after_emit;
507 class->startup = g_application_real_startup;
508 class->activate = g_application_real_activate;
509 class->open = g_application_real_open;
510 class->command_line = g_application_real_command_line;
511 class->local_command_line = g_application_real_local_command_line;
512 class->add_platform_data = g_application_real_add_platform_data;
513 class->quit_mainloop = g_application_real_quit_mainloop;
514 class->run_mainloop = g_application_real_run_mainloop;
516 g_object_class_install_property (object_class, PROP_APPLICATION_ID,
517 g_param_spec_string ("application-id",
518 P_("Application identifier"),
519 P_("The unique identifier for the application"),
520 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
521 G_PARAM_STATIC_STRINGS));
523 g_object_class_install_property (object_class, PROP_FLAGS,
524 g_param_spec_flags ("flags",
525 P_("Application flags"),
526 P_("Flags specifying the behaviour of the application"),
527 G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
528 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
530 g_object_class_install_property (object_class, PROP_IS_REGISTERED,
531 g_param_spec_boolean ("is-registered",
533 P_("If g_application_register() has been called"),
534 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
536 g_object_class_install_property (object_class, PROP_IS_REMOTE,
537 g_param_spec_boolean ("is-remote",
539 P_("If this application instance is remote"),
540 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
542 g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
543 g_param_spec_uint ("inactivity-timeout",
544 P_("Inactivity timeout"),
545 P_("Iime (ms) to stay alive after becoming idle"),
547 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
549 g_object_class_install_property (object_class, PROP_ACTION_GROUP,
550 g_param_spec_object ("action-group",
552 P_("The group of actions that the application exports"),
554 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
557 * GApplication::startup:
558 * @application: the application
560 * The ::startup signal is emitted on the primary instance immediately
561 * after registration. See g_application_register().
563 g_application_signals[SIGNAL_STARTUP] =
564 g_signal_new ("startup", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
565 G_STRUCT_OFFSET (GApplicationClass, startup),
566 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
569 * GApplication::activate:
570 * @application: the application
572 * The ::activate signal is emitted on the primary instance when an
573 * activation occurs. See g_application_activate().
575 g_application_signals[SIGNAL_ACTIVATE] =
576 g_signal_new ("activate", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
577 G_STRUCT_OFFSET (GApplicationClass, activate),
578 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
582 * GApplication::open:
583 * @application: the application
584 * @files: (array length=n_files) (element-type GFile): an array of #GFiles
585 * @n_files: the length of @files
586 * @hint: a hint provided by the calling instance
588 * The ::open signal is emitted on the primary instance when there are
589 * files to open. See g_application_open() for more information.
591 g_application_signals[SIGNAL_OPEN] =
592 g_signal_new ("open", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
593 G_STRUCT_OFFSET (GApplicationClass, open),
594 NULL, NULL, _gio_marshal_VOID__POINTER_INT_STRING,
595 G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
598 * GApplication::command-line:
599 * @application: the application
600 * @command_line: a #GApplicationCommandLine representing the
603 * The ::command-line signal is emitted on the primary instance when
604 * a commandline is not handled locally. See g_application_run() and
605 * the #GApplicationCommandline documentation for more information.
607 * Returns: An integer that is set as the exit status for the calling
608 * process. See g_application_command_line_set_exit_status().
610 g_application_signals[SIGNAL_COMMAND_LINE] =
611 g_signal_new ("command-line", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
612 G_STRUCT_OFFSET (GApplicationClass, command_line),
613 g_signal_accumulator_first_wins, NULL,
614 _gio_marshal_INT__OBJECT,
615 G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
617 g_type_class_add_private (class, sizeof (GApplicationPrivate));
621 get_platform_data (GApplication *application)
623 GVariantBuilder *builder;
626 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
629 gchar *cwd = g_get_current_dir ();
630 g_variant_builder_add (builder, "{sv}", "cwd",
631 g_variant_new_bytestring (cwd));
635 if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
640 envp = g_get_environ ();
641 array = g_variant_new_bytestring_array ((const gchar **) envp, -1);
644 g_variant_builder_add (builder, "{sv}", "environ", array);
647 G_APPLICATION_GET_CLASS (application)->
648 add_platform_data (application, builder);
650 result = g_variant_builder_end (builder);
651 g_variant_builder_unref (builder);
656 /* Application ID validity {{{1 */
659 * g_application_id_is_valid:
660 * @application_id: a potential application identifier
661 * @returns: %TRUE if @application_id is valid
663 * Checks if @application_id is a valid application identifier.
665 * A valid ID is required for calls to g_application_new() and
666 * g_application_set_application_id().
668 * For convenience, the restrictions on application identifiers are
671 * <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-." and must not begin with a digit.</listitem>
672 * <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least three elements).</listitem>
673 * <listitem>Application identifiers must not begin or end with a '.' (period) character.</listitem>
674 * <listitem>Application identifiers must not contain consecutive '.' (period) characters.</listitem>
675 * <listitem>Application identifiers must not exceed 255 characters.</listitem>
679 g_application_id_is_valid (const gchar *application_id)
685 len = strlen (application_id);
690 if (!g_ascii_isalpha (application_id[0]))
693 if (application_id[len-1] == '.')
699 for (; *application_id; application_id++)
701 if (g_ascii_isalnum (*application_id) ||
702 (*application_id == '-') ||
703 (*application_id == '_'))
707 else if (allow_dot && *application_id == '.')
722 /* Public Constructor {{{1 */
725 * @application_id: the application id
726 * @flags: the application flags
727 * @returns: a new #GApplication instance
729 * Creates a new #GApplication instance.
731 * This function calls g_type_init() for you.
733 * The application id must be valid. See g_application_id_is_valid().
736 g_application_new (const gchar *application_id,
737 GApplicationFlags flags)
739 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
743 return g_object_new (G_TYPE_APPLICATION,
744 "application-id", application_id,
749 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
751 * g_application_get_application_id:
752 * @application: a #GApplication
753 * @returns: the identifier for @application, owned by @application
755 * Gets the unique identifier for @application.
760 g_application_get_application_id (GApplication *application)
762 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
764 return application->priv->id;
768 * g_application_set_application_id:
769 * @application: a #GApplication
770 * @application_id: the identifier for @application
772 * Sets the unique identifier for @application.
774 * The application id can only be modified if @application has not yet
777 * The application id must be valid. See g_application_id_is_valid().
782 g_application_set_application_id (GApplication *application,
783 const gchar *application_id)
785 g_return_if_fail (G_IS_APPLICATION (application));
787 if (g_strcmp0 (application->priv->id, application_id) != 0)
789 g_return_if_fail (g_application_id_is_valid (application_id));
790 g_return_if_fail (!application->priv->is_registered);
792 g_free (application->priv->id);
793 application->priv->id = g_strdup (application_id);
795 g_object_notify (G_OBJECT (application), "application-id");
800 * g_application_get_flags:
801 * @application: a #GApplication
802 * @returns: the flags for @application
804 * Gets the flags for @application.
806 * See #GApplicationFlags.
811 g_application_get_flags (GApplication *application)
813 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
815 return application->priv->flags;
819 * g_application_set_flags:
820 * @application: a #GApplication
821 * @flags: the flags for @application
823 * Sets the flags for @application.
825 * The flags can only be modified if @application has not yet been
828 * See #GApplicationFlags.
833 g_application_set_flags (GApplication *application,
834 GApplicationFlags flags)
836 g_return_if_fail (G_IS_APPLICATION (application));
838 if (application->priv->flags != flags)
840 g_return_if_fail (!application->priv->is_registered);
842 application->priv->flags = flags;
844 g_object_notify (G_OBJECT (application), "flags");
849 * g_application_get_inactivity_timeout:
850 * @application: a #GApplication
852 * Gets the current inactivity timeout for the application.
854 * This is the amount of time (in milliseconds) after the last call to
855 * g_application_release() before the application stops running.
857 * Returns: the timeout, in milliseconds
862 g_application_get_inactivity_timeout (GApplication *application)
864 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
866 return application->priv->inactivity_timeout;
870 * g_application_set_inactivity_timeout:
871 * @application: a #GApplication
872 * @inactivity_timeout: the timeout, in milliseconds
874 * Sets the current inactivity timeout for the application.
876 * This is the amount of time (in milliseconds) after the last call to
877 * g_application_release() before the application stops running.
879 * This call has no side effects of its own. The value set here is only
880 * used for next time g_application_release() drops the use count to
881 * zero. Any timeouts currently in progress are not impacted.
883 * Returns: the timeout, in milliseconds
888 g_application_set_inactivity_timeout (GApplication *application,
889 guint inactivity_timeout)
891 g_return_if_fail (G_IS_APPLICATION (application));
893 if (application->priv->inactivity_timeout != inactivity_timeout)
895 application->priv->inactivity_timeout = inactivity_timeout;
897 g_object_notify (G_OBJECT (application), "inactivity-timeout");
900 /* Read-only property getters (is registered, is remote) {{{1 */
902 * g_application_get_is_registered:
903 * @application: a #GApplication
904 * @returns: %TRUE if @application is registered
906 * Checks if @application is registered.
908 * An application is registered if g_application_register() has been
909 * successfully called.
914 g_application_get_is_registered (GApplication *application)
916 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
918 return application->priv->is_registered;
922 * g_application_get_is_remote:
923 * @application: a #GApplication
924 * @returns: %TRUE if @application is remote
926 * Checks if @application is remote.
928 * If @application is remote then it means that another instance of
929 * application already exists (the 'primary' instance). Calls to
930 * perform actions on @application will result in the actions being
931 * performed by the primary instance.
933 * The value of this property can not be accessed before
934 * g_application_register() has been called. See
935 * g_application_get_is_registered().
940 g_application_get_is_remote (GApplication *application)
942 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
943 g_return_val_if_fail (application->priv->is_registered, FALSE);
945 return application->priv->is_remote;
950 * g_application_register:
951 * @application: a #GApplication
952 * @cancellable: a #GCancellable, or %NULL
953 * @error: a pointer to a NULL #GError, or %NULL
954 * @returns: %TRUE if registration succeeded
956 * Attempts registration of the application.
958 * This is the point at which the application discovers if it is the
959 * primary instance or merely acting as a remote for an already-existing
960 * primary instance. This is implemented by attempting to acquire the
961 * application identifier as a unique bus name on the session bus using
964 * Due to the internal architecture of GDBus, method calls can be
965 * dispatched at any time (even if a main loop is not running). For
966 * this reason, you must ensure that any object paths that you wish to
967 * register are registered before calling this function.
969 * If the application has already been registered then %TRUE is
970 * returned with no work performed.
972 * The #GApplication::startup signal is emitted if registration succeeds
973 * and @application is the primary instance.
975 * In the event of an error (such as @cancellable being cancelled, or a
976 * failure to connect to the session bus), %FALSE is returned and @error
977 * is set appropriately.
979 * Note: the return value of this function is not an indicator that this
980 * instance is or is not the primary instance of the application. See
981 * g_application_get_is_remote() for that.
986 g_application_register (GApplication *application,
987 GCancellable *cancellable,
990 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
992 if (!application->priv->is_registered)
994 application->priv->impl =
995 g_application_impl_register (application, application->priv->id,
996 application->priv->flags,
997 &application->priv->remote_actions,
1000 if (application->priv->impl == NULL)
1003 application->priv->is_remote = application->priv->remote_actions != NULL;
1004 application->priv->is_registered = TRUE;
1006 g_object_notify (G_OBJECT (application), "is-registered");
1008 if (!application->priv->is_remote)
1009 g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
1015 /* Hold/release {{{1 */
1017 * g_application_hold:
1018 * @application: a #GApplication
1020 * Increases the use count of @application.
1022 * Use this function to indicate that the application has a reason to
1023 * continue to run. For example, g_application_hold() is called by GTK+
1024 * when a toplevel window is on the screen.
1026 * To cancel the hold, call g_application_release().
1029 g_application_hold (GApplication *application)
1031 if (application->priv->inactivity_timeout_id)
1033 g_source_remove (application->priv->inactivity_timeout_id);
1034 application->priv->inactivity_timeout_id = 0;
1037 application->priv->use_count++;
1041 inactivity_timeout_expired (gpointer data)
1043 GApplication *application = G_APPLICATION (data);
1045 G_APPLICATION_GET_CLASS (application)
1046 ->quit_mainloop (application);
1053 * g_application_release:
1054 * @application: a #GApplication
1056 * Decrease the use count of @application.
1058 * When the use count reaches zero, the application will stop running.
1060 * Never call this function except to cancel the effect of a previous
1061 * call to g_application_hold().
1064 g_application_release (GApplication *application)
1066 application->priv->use_count--;
1068 if (application->priv->use_count == 0)
1070 if (application->priv->inactivity_timeout)
1071 application->priv->inactivity_timeout_id =
1072 g_timeout_add (application->priv->inactivity_timeout,
1073 inactivity_timeout_expired, application);
1076 G_APPLICATION_GET_CLASS (application)
1077 ->quit_mainloop (application);
1081 /* Activate, Open {{{1 */
1083 * g_application_activate:
1084 * @application: a #GApplication
1086 * Activates the application.
1088 * In essence, this results in the #GApplication::activate() signal being
1089 * emitted in the primary instance.
1091 * The application must be registered before calling this function.
1096 g_application_activate (GApplication *application)
1098 g_return_if_fail (G_IS_APPLICATION (application));
1099 g_return_if_fail (application->priv->is_registered);
1101 if (application->priv->is_remote)
1102 g_application_impl_activate (application->priv->impl,
1103 get_platform_data (application));
1106 g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
1110 * g_application_open:
1111 * @application: a #GApplication
1112 * @files: (array length=n_files): an array of #GFiles to open
1113 * @n_files: the length of the @files array
1114 * @hint: a hint (or ""), but never %NULL
1116 * Opens the given files.
1118 * In essence, this results in the #GApplication::open signal being emitted
1119 * in the primary instance.
1121 * @n_files must be greater than zero.
1123 * @hint is simply passed through to the ::open signal. It is
1124 * intended to be used by applications that have multiple modes for
1125 * opening files (eg: "view" vs "edit", etc). Unless you have a need
1126 * for this functionality, you should use "".
1128 * The application must be registered before calling this function
1129 * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
1134 g_application_open (GApplication *application,
1139 g_return_if_fail (G_IS_APPLICATION (application));
1140 g_return_if_fail (application->priv->flags &
1141 G_APPLICATION_HANDLES_OPEN);
1142 g_return_if_fail (application->priv->is_registered);
1144 if (application->priv->is_remote)
1145 g_application_impl_open (application->priv->impl,
1146 files, n_files, hint,
1147 get_platform_data (application));
1150 g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
1151 0, files, n_files, hint);
1156 * g_application_run:
1157 * @application: a #GApplication
1158 * @argc: the argc from main()
1159 * @argv: (array length=argc): the argv from main()
1160 * @returns: the exit status
1162 * Runs the application.
1164 * This function is intended to be run from main() and its return value
1165 * is intended to be returned by main().
1167 * First, the local_command_line() virtual function is invoked.
1168 * This function always runs on the local instance. It gets passed
1169 * pointers to @argc and @argv and is expected to remove the arguments
1170 * that it handled from @argv (shifting up remaining arguments, and
1171 * reducing @argc accordingly). See <xref linkend="gapplication-example-cmdline2"/> for an example. The last argument to local_command_line() is a
1172 * pointer to the @status variable which can used to set the exit
1173 * status that is returned from g_application_run().
1175 * If local_command_line() returns %TRUE, the command line is expected
1176 * to be completely handled, including possibly registering as the primary
1177 * instance, calling g_application_activate() or g_application_open(), etc.
1179 * If local_command_line() returns %FALSE then the application is registered
1180 * and the #GApplication::command-line signal is emitted in the primary
1181 * instance (which may or may not be this instance).
1183 * If the application has the %G_APPLICATION_HANDLES_COMMAND_LINE
1184 * flag set then the default implementation of local_command_line()
1185 * always returns %FALSE immediately, resulting in the commandline
1186 * always being handled in the primary instance.
1188 * Otherwise, the default implementation of local_command_line() tries
1189 * to do a couple of things that are probably reasonable for most
1190 * applications. First, g_application_register() is called to attempt
1191 * to register the application. If that works, then the command line
1192 * arguments are inspected. If no commandline arguments are given, then
1193 * g_application_activate() is called. If commandline arguments are
1194 * given and the %G_APPLICATION_HANDLES_OPEN flag is set then they
1195 * are assumed to be filenames and g_application_open() is called.
1197 * If you are interested in doing more complicated local handling of the
1198 * commandline then you should implement your own #GApplication subclass
1199 * and override local_command_line(). In this case, you most likely want
1200 * to return %TRUE from your local_command_line() implementation to
1201 * suppress the default handling. See
1202 * <xref linkend="gapplication-example-cmdline2"/> for an example.
1204 * If, after the above is done, the use count of the application is zero
1205 * then the exit status is returned immediately. If the use count is
1206 * non-zero then the mainloop is run until the use count falls to zero,
1207 * at which point 0 is returned.
1209 * If the %G_APPLICATION_IS_SERVICE flag is set, then the exiting at
1210 * use count of zero is delayed for a while (ie: the instance stays
1211 * around to provide its <emphasis>service</emphasis> to others).
1216 g_application_run (GApplication *application,
1224 g_return_val_if_fail (G_IS_APPLICATION (application), 1);
1225 g_return_val_if_fail (argc == 0 || argv != NULL, 1);
1227 arguments = g_new (gchar *, argc + 1);
1228 for (i = 0; i < argc; i++)
1229 arguments[i] = g_strdup (argv[i]);
1230 arguments[i] = NULL;
1232 if (g_get_prgname () == NULL && argc > 0)
1236 prgname = g_path_get_basename (argv[0]);
1237 g_set_prgname (prgname);
1241 if (!G_APPLICATION_GET_CLASS (application)
1242 ->local_command_line (application, &arguments, &status))
1244 GError *error = NULL;
1246 if (!g_application_register (application, NULL, &error))
1248 g_printerr ("%s", error->message);
1249 g_error_free (error);
1253 if (application->priv->is_remote)
1255 GVariant *platform_data;
1257 platform_data = get_platform_data (application);
1258 status = g_application_impl_command_line (application->priv->impl,
1259 arguments, platform_data);
1263 GApplicationCommandLine *cmdline;
1266 v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
1267 cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
1268 "arguments", v, NULL);
1269 g_signal_emit (application,
1270 g_application_signals[SIGNAL_COMMAND_LINE],
1271 0, cmdline, &status);
1272 g_object_unref (cmdline);
1276 g_strfreev (arguments);
1278 if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
1279 application->priv->is_registered &&
1280 !application->priv->use_count &&
1281 !application->priv->inactivity_timeout_id)
1283 application->priv->inactivity_timeout_id =
1284 g_timeout_add (10000, inactivity_timeout_expired, application);
1287 if (application->priv->use_count ||
1288 application->priv->inactivity_timeout_id)
1290 G_APPLICATION_GET_CLASS (application)
1291 ->run_mainloop (application);
1295 if (application->priv->impl)
1296 g_application_impl_flush (application->priv->impl);
1302 g_application_has_action (GActionGroup *action_group,
1303 const gchar *action_name)
1305 GApplication *application = G_APPLICATION (action_group);
1307 g_return_val_if_fail (application->priv->is_registered, FALSE);
1309 if (application->priv->remote_actions != NULL)
1310 return g_hash_table_lookup (application->priv->remote_actions,
1311 action_name) != NULL;
1313 return application->priv->actions &&
1314 g_action_group_has_action (application->priv->actions, action_name);
1318 g_application_list_actions (GActionGroup *action_group)
1320 GApplication *application = G_APPLICATION (action_group);
1322 g_return_val_if_fail (application->priv->is_registered, NULL);
1324 if (application->priv->remote_actions != NULL)
1326 GHashTableIter iter;
1331 n = g_hash_table_size (application->priv->remote_actions);
1332 keys = g_new (gchar *, n + 1);
1334 g_hash_table_iter_init (&iter, application->priv->remote_actions);
1335 while (g_hash_table_iter_next (&iter, &key, NULL))
1336 keys[i++] = g_strdup (key);
1337 g_assert_cmpint (i, ==, n);
1343 else if (application->priv->actions != NULL)
1344 return g_action_group_list_actions (application->priv->actions);
1347 /* empty string array */
1348 return g_new0 (gchar *, 1);
1352 g_application_get_action_enabled (GActionGroup *action_group,
1353 const gchar *action_name)
1355 GApplication *application = G_APPLICATION (action_group);
1357 g_return_val_if_fail (application->priv->actions != NULL, FALSE);
1358 g_return_val_if_fail (application->priv->is_registered, FALSE);
1360 if (application->priv->remote_actions)
1362 RemoteActionInfo *info;
1364 info = g_hash_table_lookup (application->priv->remote_actions,
1367 return info && info->enabled;
1370 return g_action_group_get_action_enabled (application->priv->actions,
1374 static const GVariantType *
1375 g_application_get_action_parameter_type (GActionGroup *action_group,
1376 const gchar *action_name)
1378 GApplication *application = G_APPLICATION (action_group);
1380 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1381 g_return_val_if_fail (application->priv->is_registered, NULL);
1383 if (application->priv->remote_actions)
1385 RemoteActionInfo *info;
1387 info = g_hash_table_lookup (application->priv->remote_actions,
1391 return info->parameter_type;
1396 return g_action_group_get_action_parameter_type (application->priv->actions,
1400 static const GVariantType *
1401 g_application_get_action_state_type (GActionGroup *action_group,
1402 const gchar *action_name)
1404 GApplication *application = G_APPLICATION (action_group);
1406 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1407 g_return_val_if_fail (application->priv->is_registered, NULL);
1409 if (application->priv->remote_actions)
1411 RemoteActionInfo *info;
1413 info = g_hash_table_lookup (application->priv->remote_actions,
1416 if (info && info->state)
1417 return g_variant_get_type (info->state);
1422 return g_action_group_get_action_state_type (application->priv->actions,
1427 g_application_get_action_state (GActionGroup *action_group,
1428 const gchar *action_name)
1430 GApplication *application = G_APPLICATION (action_group);
1432 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1433 g_return_val_if_fail (application->priv->is_registered, NULL);
1435 if (application->priv->remote_actions)
1437 RemoteActionInfo *info;
1439 info = g_hash_table_lookup (application->priv->remote_actions,
1442 if (info && info->state)
1443 return g_variant_ref (info->state);
1448 return g_action_group_get_action_state (application->priv->actions,
1453 g_application_change_action_state (GActionGroup *action_group,
1454 const gchar *action_name,
1457 GApplication *application = G_APPLICATION (action_group);
1459 g_return_if_fail (application->priv->is_registered);
1461 if (application->priv->is_remote)
1462 g_application_impl_change_action_state (application->priv->impl,
1464 get_platform_data (application));
1467 g_action_group_change_action_state (application->priv->actions,
1468 action_name, value);
1472 g_application_activate_action (GActionGroup *action_group,
1473 const gchar *action_name,
1474 GVariant *parameter)
1476 GApplication *application = G_APPLICATION (action_group);
1478 g_return_if_fail (application->priv->is_registered);
1480 if (application->priv->is_remote)
1481 g_application_impl_activate_action (application->priv->impl,
1482 action_name, parameter,
1483 get_platform_data (application));
1486 g_action_group_activate_action (application->priv->actions,
1487 action_name, parameter);
1491 g_application_action_group_iface_init (GActionGroupInterface *iface)
1493 iface->has_action = g_application_has_action;
1494 iface->list_actions = g_application_list_actions;
1496 iface->get_action_enabled = g_application_get_action_enabled;
1497 iface->get_action_parameter_type = g_application_get_action_parameter_type;
1498 iface->get_action_state_type = g_application_get_action_state_type;
1499 iface->get_action_state = g_application_get_action_state;
1500 iface->change_action_state = g_application_change_action_state;
1501 iface->activate_action = g_application_activate_action;
1505 /* vim:set foldmethod=marker: */