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 * GApplication provides convenient life cycle management by maintaining
68 * a <firstterm>use count</firstterm> for the primary application instance.
69 * The use count can be changed using g_application_hold() and
70 * g_application_release(). If it drops to zero, the application exits.
72 * GApplication also implements the #GActionGroup interface and lets you
73 * easily export actions by adding them with g_application_set_action_group().
74 * When invoking an action by calling g_action_group_activate_action() on
75 * the application, it is always invoked in the primary instance.
77 * There is a number of different entry points into a #GApplication:
79 * <listitem>via 'Activate' (i.e. just starting the application)</listitem>
80 * <listitem>via 'Open' (i.e. opening some files)</listitem>
81 * <listitem>via activating an action</listitem>
83 * The #GApplication::startup signal lets you handle the application
84 * initialization for all of these in a single place.
86 * <example id="gapplication-example-open"><title>Opening files with a GApplication</title>
88 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-open.c">
89 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
94 * <example id="gapplication-example-actions"><title>A GApplication with actions</title>
96 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-actions.c">
97 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
103 struct _GApplicationPrivate
105 GApplicationFlags flags;
108 GActionGroup *actions;
111 guint inactivity_timeout_id;
112 guint inactivity_timeout;
115 guint is_registered : 1;
118 GHashTable *remote_actions; /* string -> RemoteActionInfo */
119 GApplicationImpl *impl;
129 PROP_INACTIVITY_TIMEOUT,
143 static guint g_application_signals[NR_SIGNALS];
145 static void g_application_action_group_iface_init (GActionGroupInterface *);
146 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
147 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
148 g_application_action_group_iface_init))
150 /* vfunc defaults {{{1 */
152 g_application_real_before_emit (GApplication *application,
153 GVariant *platform_data)
158 g_application_real_after_emit (GApplication *application,
159 GVariant *platform_data)
164 g_application_real_startup (GApplication *application)
169 g_application_real_activate (GApplication *application)
171 if (!g_signal_has_handler_pending (application,
172 g_application_signals[SIGNAL_ACTIVATE],
175 static gboolean warned;
180 g_warning ("Your application does not implement "
181 "g_application_activate() and has no handlers connected "
182 "to the 'activate' signal. It should do one of these.");
188 g_application_real_open (GApplication *application,
193 if (!g_signal_has_handler_pending (application,
194 g_application_signals[SIGNAL_OPEN],
197 static gboolean warned;
202 g_warning ("Your application claims to support opening files "
203 "but does not implement g_application_open() and has no "
204 "handlers connected to the 'open' signal.");
210 g_application_real_command_line (GApplication *application,
211 GApplicationCommandLine *cmdline)
213 static gboolean warned;
218 g_warning ("Your application claims to support custom command line "
219 "handling but does not implement g_application_command_line() "
220 "and has no handlers connected to the 'command-line' signal.");
228 g_application_real_local_command_line (GApplication *application,
232 if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
237 GError *error = NULL;
240 if (!g_application_register (application, NULL, &error))
242 g_critical ("%s", error->message);
243 g_error_free (error);
248 n_args = g_strv_length (*arguments);
250 if (application->priv->flags & G_APPLICATION_IS_SERVICE)
252 if ((*exit_status = n_args > 1))
254 g_printerr ("GApplication service mode takes no arguments.\n");
255 application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
263 g_application_activate (application);
269 if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
271 g_critical ("This application can not open files.");
280 n_files = n_args - 1;
281 files = g_new (GFile *, n_files);
283 for (i = 0; i < n_files; i++)
284 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
286 g_application_open (application, files, n_files, "");
288 for (i = 0; i < n_files; i++)
289 g_object_unref (files[i]);
301 g_application_real_add_platform_data (GApplication *application,
302 GVariantBuilder *builder)
307 g_application_real_quit_mainloop (GApplication *application)
309 if (application->priv->mainloop != NULL)
310 g_main_loop_quit (application->priv->mainloop);
314 g_application_real_run_mainloop (GApplication *application)
316 if (application->priv->mainloop == NULL)
317 application->priv->mainloop = g_main_loop_new (NULL, FALSE);
319 g_main_loop_run (application->priv->mainloop);
322 /* GObject implementation stuff {{{1 */
324 g_application_set_property (GObject *object,
329 GApplication *application = G_APPLICATION (object);
333 case PROP_APPLICATION_ID:
334 g_application_set_application_id (application,
335 g_value_get_string (value));
339 g_application_set_flags (application, g_value_get_flags (value));
342 case PROP_INACTIVITY_TIMEOUT:
343 g_application_set_inactivity_timeout (application,
344 g_value_get_uint (value));
347 case PROP_ACTION_GROUP:
348 g_application_set_action_group (application,
349 g_value_get_object (value));
353 g_assert_not_reached ();
358 * g_application_set_action_group:
359 * @application: a #GApplication
360 * @action_group: (allow-none): a #GActionGroup, or %NULL
362 * Sets or unsets the group of actions associated with the application.
364 * These actions are the actions that can be remotely invoked.
366 * It is an error to call this function after the application has been
372 g_application_set_action_group (GApplication *application,
373 GActionGroup *action_group)
375 g_return_if_fail (G_IS_APPLICATION (application));
376 g_return_if_fail (!application->priv->is_registered);
378 if (application->priv->actions != NULL)
379 g_object_unref (application->priv->actions);
381 application->priv->actions = action_group;
383 if (application->priv->actions != NULL)
384 g_object_ref (application->priv->actions);
388 g_application_get_property (GObject *object,
393 GApplication *application = G_APPLICATION (object);
397 case PROP_APPLICATION_ID:
398 g_value_set_string (value,
399 g_application_get_application_id (application));
403 g_value_set_flags (value,
404 g_application_get_flags (application));
407 case PROP_IS_REGISTERED:
408 g_value_set_boolean (value,
409 g_application_get_is_registered (application));
413 g_value_set_boolean (value,
414 g_application_get_is_remote (application));
417 case PROP_INACTIVITY_TIMEOUT:
418 g_value_set_uint (value,
419 g_application_get_inactivity_timeout (application));
423 g_assert_not_reached ();
428 g_application_constructed (GObject *object)
430 GApplication *application = G_APPLICATION (object);
432 g_assert (application->priv->id != NULL);
436 g_application_finalize (GObject *object)
438 GApplication *application = G_APPLICATION (object);
440 if (application->priv->impl)
441 g_application_impl_destroy (application->priv->impl);
442 g_free (application->priv->id);
444 if (application->priv->mainloop)
445 g_main_loop_unref (application->priv->mainloop);
447 G_OBJECT_CLASS (g_application_parent_class)
452 g_application_init (GApplication *application)
454 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
456 GApplicationPrivate);
460 g_application_class_init (GApplicationClass *class)
462 GObjectClass *object_class = G_OBJECT_CLASS (class);
464 object_class->constructed = g_application_constructed;
465 object_class->finalize = g_application_finalize;
466 object_class->get_property = g_application_get_property;
467 object_class->set_property = g_application_set_property;
469 class->before_emit = g_application_real_before_emit;
470 class->after_emit = g_application_real_after_emit;
471 class->startup = g_application_real_startup;
472 class->activate = g_application_real_activate;
473 class->open = g_application_real_open;
474 class->command_line = g_application_real_command_line;
475 class->local_command_line = g_application_real_local_command_line;
476 class->add_platform_data = g_application_real_add_platform_data;
477 class->quit_mainloop = g_application_real_quit_mainloop;
478 class->run_mainloop = g_application_real_run_mainloop;
480 g_object_class_install_property (object_class, PROP_APPLICATION_ID,
481 g_param_spec_string ("application-id",
482 P_("Application identifier"),
483 P_("The unique identifier for the application"),
484 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
485 G_PARAM_STATIC_STRINGS));
487 g_object_class_install_property (object_class, PROP_FLAGS,
488 g_param_spec_flags ("flags",
489 P_("Application flags"),
490 P_("Flags specifying the behaviour of the application"),
491 G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
492 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
494 g_object_class_install_property (object_class, PROP_IS_REGISTERED,
495 g_param_spec_boolean ("is-registered",
497 P_("If g_application_register() has been called"),
498 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
500 g_object_class_install_property (object_class, PROP_IS_REMOTE,
501 g_param_spec_boolean ("is-remote",
503 P_("If this application instance is remote"),
504 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
506 g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
507 g_param_spec_uint ("inactivity-timeout",
508 P_("Inactivity timeout"),
509 P_("Iime (ms) to stay alive after becoming idle"),
511 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
513 g_object_class_install_property (object_class, PROP_ACTION_GROUP,
514 g_param_spec_object ("action-group",
516 P_("The group of actions that the application exports"),
518 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
521 * GApplication::startup:
522 * @application: the application
524 * The ::startup signal is emitted on the primary instance immediately
525 * after registration. See g_activation_register().
527 g_application_signals[SIGNAL_STARTUP] =
528 g_signal_new ("startup", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
529 G_STRUCT_OFFSET (GApplicationClass, startup),
530 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
533 * GApplication::activate:
534 * @application: the application
536 * The ::activate signal is emitted on the primary instance when an
537 * activation occurs. See g_application_activate().
539 g_application_signals[SIGNAL_ACTIVATE] =
540 g_signal_new ("activate", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
541 G_STRUCT_OFFSET (GApplicationClass, activate),
542 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
546 * GApplication::open:
547 * @application: the application
548 * @files: (array length=n_files) (element-type GFile): an array of #GFiles
549 * @n_files: the length of @files
550 * @hint: a hint provided by the calling instance
552 * The ::open signal is emitted on the primary instance when there are
553 * files to open. See g_application_open() for more information.
555 g_application_signals[SIGNAL_OPEN] =
556 g_signal_new ("open", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
557 G_STRUCT_OFFSET (GApplicationClass, open),
558 NULL, NULL, _gio_marshal_VOID__POINTER_INT_STRING,
559 G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
562 * GApplication::command-line:
563 * @application: the application
564 * @command_line: a #GApplicationCommandLine representing the
567 * The ::command-line signal is emitted on the primary instance when
568 * a commandline is not handled locally. See g_application_run() for
571 g_application_signals[SIGNAL_COMMAND_LINE] =
572 g_signal_new ("command-line", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
573 G_STRUCT_OFFSET (GApplicationClass, command_line),
574 g_signal_accumulator_first_wins, NULL,
575 _gio_marshal_INT__OBJECT,
576 G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
578 g_type_class_add_private (class, sizeof (GApplicationPrivate));
582 get_platform_data (GApplication *application)
584 GVariantBuilder *builder;
587 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
590 gchar *cwd = g_get_current_dir ();
591 g_variant_builder_add (builder, "{sv}", "cwd",
592 g_variant_new_bytestring (cwd));
596 if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
601 envp = g_get_environ ();
602 array = g_variant_new_bytestring_array ((const gchar **) envp, -1);
605 g_variant_builder_add (builder, "{sv}", "environ", array);
608 G_APPLICATION_GET_CLASS (application)->
609 add_platform_data (application, builder);
611 result = g_variant_builder_end (builder);
612 g_variant_builder_unref (builder);
617 /* Application ID validity {{{1 */
620 * g_application_id_is_valid:
621 * @application_id: a potential application identifier
622 * @returns: %TRUE if @application_id is valid
624 * Checks if @application_id is a valid application identifier.
626 * A valid ID is required for calls to g_application_new() and
627 * g_application_set_application_id().
629 * For convenience, the restrictions on application identifiers are
632 * <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-" and must not begin with a digit.</listitem>
633 * <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least two elements).</listitem>
634 * <listitem>Application identifiers must not begin with a '.' (period) character.</listitem>
635 * <listitem>Application identifiers must not contain consecutive '.' (period) characters.</listitem>
636 * <listitem>Application identifiers must not exceed 255 characters.</listitem>
640 g_application_id_is_valid (const gchar *application_id)
644 if (strlen (application_id) > 255)
647 if (!g_ascii_isalpha (*application_id))
652 for (; *application_id; application_id++)
654 if (g_ascii_isalnum (*application_id) ||
655 (*application_id == '-') ||
656 (*application_id == '_'))
658 else if (allow_dot && *application_id == '.')
667 /* Public Constructor {{{1 */
670 * @application_id: the application id
671 * @flags: the application flags
672 * @returns: a new #GApplication instance
674 * Creates a new #GApplication instance.
676 * This function calls g_type_init() for you.
678 * The application id must be valid. See g_application_id_is_valid().
681 g_application_new (const gchar *application_id,
682 GApplicationFlags flags)
684 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
688 return g_object_new (G_TYPE_APPLICATION,
689 "application-id", application_id,
694 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
696 * g_application_get_application_id:
697 * @application: a #GApplication
698 * @returns: the identifier for @application, owned by @application
700 * Gets the unique identifier for @application.
705 g_application_get_application_id (GApplication *application)
707 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
709 return application->priv->id;
713 * g_application_set_application_id:
714 * @application: a #GApplication
715 * @application_id: the identifier for @application
717 * Sets the unique identifier for @application.
719 * The application id can only be modified if @application has not yet
722 * The application id must be valid. See g_application_id_is_valid().
727 g_application_set_application_id (GApplication *application,
728 const gchar *application_id)
730 g_return_if_fail (G_IS_APPLICATION (application));
732 if (g_strcmp0 (application->priv->id, application_id) != 0)
734 g_return_if_fail (g_application_id_is_valid (application_id));
735 g_return_if_fail (!application->priv->is_registered);
737 g_free (application->priv->id);
738 application->priv->id = g_strdup (application_id);
740 g_object_notify (G_OBJECT (application), "application-id");
745 * g_application_get_flags:
746 * @application: a #GApplication
747 * @returns: the flags for @application
749 * Gets the flags for @application.
751 * See #GApplicationFlags.
756 g_application_get_flags (GApplication *application)
758 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
760 return application->priv->flags;
764 * g_application_set_flags:
765 * @application: a #GApplication
766 * @flags: the flags for @application
768 * Sets the flags for @application.
770 * The flags can only be modified if @application has not yet been
773 * See #GApplicationFlags.
778 g_application_set_flags (GApplication *application,
779 GApplicationFlags flags)
781 g_return_if_fail (G_IS_APPLICATION (application));
783 if (application->priv->flags != flags)
785 g_return_if_fail (!application->priv->is_registered);
787 application->priv->flags = flags;
789 g_object_notify (G_OBJECT (application), "flags");
794 * g_application_get_inactivity_timeout:
795 * @application: a #GApplication
797 * Gets the current inactivity timeout for the application.
799 * This is the amount of time (in milliseconds) after the last call to
800 * g_application_release() before the application stops running.
802 * Returns: the timeout, in milliseconds
807 g_application_get_inactivity_timeout (GApplication *application)
809 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
811 return application->priv->inactivity_timeout;
815 * g_application_set_inactivity_timeout:
816 * @application: a #GApplication
817 * @inactivity_timeout: the timeout, in milliseconds
819 * Sets the current inactivity timeout for the application.
821 * This is the amount of time (in milliseconds) after the last call to
822 * g_application_release() before the application stops running.
824 * This call has no side effects of its own. The value set here is only
825 * used for next time g_application_release() drops the use count to
826 * zero. Any timeouts currently in progress are not impacted.
828 * Returns: the timeout, in milliseconds
833 g_application_set_inactivity_timeout (GApplication *application,
834 guint inactivity_timeout)
836 g_return_if_fail (G_IS_APPLICATION (application));
838 if (application->priv->inactivity_timeout != inactivity_timeout)
840 application->priv->inactivity_timeout = inactivity_timeout;
842 g_object_notify (G_OBJECT (application), "inactivity-timeout");
845 /* Read-only property getters (is registered, is remote) {{{1 */
847 * g_application_get_is_registered:
848 * @application: a #GApplication
849 * @returns: %TRUE if @application is registered
851 * Checks if @application is registered.
853 * An application is registered if g_application_register() has been
854 * successfully called.
859 g_application_get_is_registered (GApplication *application)
861 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
863 return application->priv->is_registered;
867 * g_application_get_is_remote:
868 * @application: a #GApplication
869 * @returns: %TRUE if @application is remote
871 * Checks if @application is remote.
873 * If @application is remote then it means that another instance of
874 * application already exists (the 'primary' instance). Calls to
875 * perform actions on @application will result in the actions being
876 * performed by the primary instance.
878 * The value of this property can not be accessed before
879 * g_application_register() has been called. See
880 * g_application_get_is_registered().
885 g_application_get_is_remote (GApplication *application)
887 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
888 g_return_val_if_fail (application->priv->is_registered, FALSE);
890 return application->priv->is_remote;
895 * g_application_register:
896 * @application: a #GApplication
897 * @cancellable: a #GCancellable, or %NULL
898 * @error: a pointer to a NULL #GError, or %NULL
899 * @returns: %TRUE if registration succeeded
901 * Attempts registration of the application.
903 * This is the point at which the application discovers if it is the
904 * primary instance or merely acting as a remote for an already-existing
907 * If the application has already been registered then %TRUE is
908 * returned with no work performed.
910 * The #GApplication::startup signal is emitted if registration succeeds
911 * and @application is the primary instance.
913 * In the event of an error (such as @cancellable being cancelled, or a
914 * failure to connect to the session bus), %FALSE is returned and @error
915 * is set appropriately.
917 * Note: the return value of this function is not an indicator that this
918 * instance is or is not the primary instance of the application. See
919 * g_application_get_is_remote() for that.
924 g_application_register (GApplication *application,
925 GCancellable *cancellable,
928 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
930 if (!application->priv->is_registered)
932 application->priv->impl =
933 g_application_impl_register (application, application->priv->id,
934 application->priv->flags,
935 &application->priv->remote_actions,
938 if (application->priv->impl == NULL)
941 application->priv->is_remote = application->priv->remote_actions != NULL;
942 application->priv->is_registered = TRUE;
944 g_object_notify (G_OBJECT (application), "is-registered");
946 if (!application->priv->is_remote)
947 g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
953 /* Hold/release {{{1 */
955 * g_application_hold:
956 * @application: a #GApplication
958 * Increases the use count of @application.
960 * Use this function to indicate that the application has a reason to
961 * continue to run. For example, g_application_hold() is called by GTK+
962 * when a toplevel window is on the screen.
964 * To cancel the hold, call g_application_release().
967 g_application_hold (GApplication *application)
969 if (application->priv->inactivity_timeout_id)
971 g_source_remove (application->priv->inactivity_timeout_id);
972 application->priv->inactivity_timeout_id = 0;
975 application->priv->use_count++;
979 inactivity_timeout_expired (gpointer data)
981 GApplication *application = G_APPLICATION (data);
983 G_APPLICATION_GET_CLASS (application)
984 ->quit_mainloop (application);
991 * g_application_release:
992 * @application: a #GApplication
994 * Decrease the use count of @application.
996 * When the use count reaches zero, the application will stop running.
998 * Never call this function except to cancel the effect of a previous
999 * call to g_application_hold().
1002 g_application_release (GApplication *application)
1004 application->priv->use_count--;
1006 if (application->priv->use_count == 0)
1008 if (application->priv->inactivity_timeout)
1009 application->priv->inactivity_timeout_id =
1010 g_timeout_add (application->priv->inactivity_timeout,
1011 inactivity_timeout_expired, application);
1014 G_APPLICATION_GET_CLASS (application)
1015 ->quit_mainloop (application);
1019 /* Activate, Open {{{1 */
1021 * g_application_activate:
1022 * @application: a #GApplication
1024 * Activates the application.
1026 * In essence, this results in the #GApplication::activate() signal being
1027 * emitted in the primary instance.
1029 * The application must be registered before calling this function.
1034 g_application_activate (GApplication *application)
1036 g_return_if_fail (G_IS_APPLICATION (application));
1037 g_return_if_fail (application->priv->is_registered);
1039 if (application->priv->is_remote)
1040 g_application_impl_activate (application->priv->impl,
1041 get_platform_data (application));
1044 g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
1048 * g_application_open:
1049 * @application: a #GApplication
1050 * @files: (array length=n_files): an array of #GFiles to open
1051 * @n_files: the length of the @files array
1052 * @hint: a hint (or ""), but never %NULL
1054 * Opens the given files.
1056 * In essence, this results in the #GApplication::open signal being emitted
1057 * in the primary instance.
1059 * @n_files must be greater than zero.
1061 * @hint is simply passed through to the ::open signal. It is
1062 * intended to be used by applications that have multiple modes for
1063 * opening files (eg: "view" vs "edit", etc). Unless you have a need
1064 * for this functionality, you should use "".
1066 * The application must be registered before calling this function
1067 * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
1072 g_application_open (GApplication *application,
1077 g_return_if_fail (G_IS_APPLICATION (application));
1078 g_return_if_fail (application->priv->flags &
1079 G_APPLICATION_HANDLES_OPEN);
1080 g_return_if_fail (application->priv->is_registered);
1082 if (application->priv->is_remote)
1083 g_application_impl_open (application->priv->impl,
1084 files, n_files, hint,
1085 get_platform_data (application));
1088 g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
1089 0, files, n_files, hint);
1094 * g_application_run:
1095 * @application: a #GApplication
1096 * @argc: the argc from main()
1097 * @argv: (array length=argc): the argv from main()
1098 * @returns: the exit status
1100 * Runs the application.
1102 * This function is intended to be run from main() and its return value
1103 * is intended to be returned by main().
1105 * First, the local_command_line() virtual function is invoked. This
1106 * function always runs on the local instance. If that function returns
1107 * %FALSE then the application is registered and the #GApplication::command-line
1108 * signal is emitted in the primary instance (which may or may not be
1111 * If the application has the %G_APPLICATION_HANDLES_COMMAND_LINE
1112 * flag set then the default implementation of local_command_line()
1113 * always returns %FALSE immediately, resulting in the commandline
1114 * always being handled in the primary instance.
1116 * Otherwise, the default implementation of local_command_line() tries
1117 * to do a couple of things that are probably reasonable for most
1118 * applications. First, g_application_register() is called to attempt
1119 * to register the application. If that works, then the command line
1120 * arguments are inspected. If no commandline arguments are given, then
1121 * g_application_activate() is called. If commandline arguments are
1122 * given and the %G_APPLICATION_HANDLES_OPEN flag is set then they
1123 * are assumed to be filenames and g_application_open() is called.
1125 * If you are interested in doing more complicated local handling of the
1126 * commandline then you should implement your own #GApplication subclass
1127 * and override local_command_line(). See
1128 * <xref linkend="gapplication-example-cmdline2"/> for an example.
1130 * If, after the above is done, the use count of the application is zero
1131 * then the exit status is returned immediately. If the use count is
1132 * non-zero then the mainloop is run until the use count falls to zero,
1133 * at which point 0 is returned.
1135 * If the %G_APPLICATION_IS_SERVICE flag is set, then the exiting at
1136 * use count of zero is delayed for a while (ie: the instance stays
1137 * around to provide its <emphasis>service</emphasis> to others).
1142 g_application_run (GApplication *application,
1150 g_return_val_if_fail (G_IS_APPLICATION (application), 1);
1151 g_return_val_if_fail (argc == 0 || argv != NULL, 1);
1153 arguments = g_new (gchar *, argc + 1);
1154 for (i = 0; i < argc; i++)
1155 arguments[i] = g_strdup (argv[i]);
1156 arguments[i] = NULL;
1158 if (g_get_prgname () == NULL && argc > 0)
1162 prgname = g_path_get_basename (argv[0]);
1163 g_set_prgname (prgname);
1167 if (!G_APPLICATION_GET_CLASS (application)
1168 ->local_command_line (application, &arguments, &status))
1170 GError *error = NULL;
1172 if (!g_application_register (application, NULL, &error))
1174 g_printerr ("%s", error->message);
1175 g_error_free (error);
1179 if (application->priv->is_remote)
1181 GVariant *platform_data;
1183 platform_data = get_platform_data (application);
1184 status = g_application_impl_command_line (application->priv->impl,
1185 arguments, platform_data);
1189 GApplicationCommandLine *cmdline;
1192 v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
1193 cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
1194 "arguments", v, NULL);
1195 g_signal_emit (application,
1196 g_application_signals[SIGNAL_COMMAND_LINE],
1197 0, cmdline, &status);
1198 g_object_unref (cmdline);
1202 g_strfreev (arguments);
1204 if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
1205 application->priv->is_registered &&
1206 !application->priv->use_count &&
1207 !application->priv->inactivity_timeout_id)
1209 application->priv->inactivity_timeout_id =
1210 g_timeout_add (10000, inactivity_timeout_expired, application);
1213 if (application->priv->use_count ||
1214 application->priv->inactivity_timeout_id)
1216 G_APPLICATION_GET_CLASS (application)
1217 ->run_mainloop (application);
1221 if (application->priv->impl)
1222 g_application_impl_flush (application->priv->impl);
1228 g_application_has_action (GActionGroup *action_group,
1229 const gchar *action_name)
1231 GApplication *application = G_APPLICATION (action_group);
1233 g_return_val_if_fail (application->priv->is_registered, FALSE);
1235 if (application->priv->remote_actions != NULL)
1236 return g_hash_table_lookup (application->priv->remote_actions,
1237 action_name) != NULL;
1239 return application->priv->actions &&
1240 g_action_group_has_action (application->priv->actions, action_name);
1244 g_application_list_actions (GActionGroup *action_group)
1246 GApplication *application = G_APPLICATION (action_group);
1248 g_return_val_if_fail (application->priv->is_registered, NULL);
1250 if (application->priv->remote_actions != NULL)
1252 GHashTableIter iter;
1257 n = g_hash_table_size (application->priv->remote_actions);
1258 keys = g_new (gchar *, n + 1);
1260 g_hash_table_iter_init (&iter, application->priv->remote_actions);
1261 while (g_hash_table_iter_next (&iter, &key, NULL))
1262 keys[i++] = g_strdup (key);
1263 g_assert_cmpint (i, ==, n);
1269 else if (application->priv->actions != NULL)
1270 return g_action_group_list_actions (application->priv->actions);
1273 /* empty string array */
1274 return g_new0 (gchar *, 1);
1278 g_application_get_action_enabled (GActionGroup *action_group,
1279 const gchar *action_name)
1281 GApplication *application = G_APPLICATION (action_group);
1283 g_return_val_if_fail (application->priv->actions != NULL, FALSE);
1284 g_return_val_if_fail (application->priv->is_registered, FALSE);
1286 if (application->priv->remote_actions)
1288 RemoteActionInfo *info;
1290 info = g_hash_table_lookup (application->priv->remote_actions,
1293 return info && info->enabled;
1296 return g_action_group_get_action_enabled (application->priv->actions,
1300 static const GVariantType *
1301 g_application_get_action_parameter_type (GActionGroup *action_group,
1302 const gchar *action_name)
1304 GApplication *application = G_APPLICATION (action_group);
1306 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1307 g_return_val_if_fail (application->priv->is_registered, NULL);
1309 if (application->priv->remote_actions)
1311 RemoteActionInfo *info;
1313 info = g_hash_table_lookup (application->priv->remote_actions,
1317 return info->parameter_type;
1322 return g_action_group_get_action_parameter_type (application->priv->actions,
1326 static const GVariantType *
1327 g_application_get_action_state_type (GActionGroup *action_group,
1328 const gchar *action_name)
1330 GApplication *application = G_APPLICATION (action_group);
1332 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1333 g_return_val_if_fail (application->priv->is_registered, NULL);
1335 if (application->priv->remote_actions)
1337 RemoteActionInfo *info;
1339 info = g_hash_table_lookup (application->priv->remote_actions,
1342 if (info && info->state)
1343 return g_variant_get_type (info->state);
1348 return g_action_group_get_action_state_type (application->priv->actions,
1353 g_application_get_action_state (GActionGroup *action_group,
1354 const gchar *action_name)
1356 GApplication *application = G_APPLICATION (action_group);
1358 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1359 g_return_val_if_fail (application->priv->is_registered, NULL);
1361 if (application->priv->remote_actions)
1363 RemoteActionInfo *info;
1365 info = g_hash_table_lookup (application->priv->remote_actions,
1368 if (info && info->state)
1369 return g_variant_ref (info->state);
1374 return g_action_group_get_action_state (application->priv->actions,
1379 g_application_change_action_state (GActionGroup *action_group,
1380 const gchar *action_name,
1383 GApplication *application = G_APPLICATION (action_group);
1385 g_return_if_fail (application->priv->is_registered);
1387 if (application->priv->is_remote)
1388 g_application_impl_change_action_state (application->priv->impl,
1390 get_platform_data (application));
1393 g_action_group_change_action_state (application->priv->actions,
1394 action_name, value);
1398 g_application_activate_action (GActionGroup *action_group,
1399 const gchar *action_name,
1400 GVariant *parameter)
1402 GApplication *application = G_APPLICATION (action_group);
1404 g_return_if_fail (application->priv->is_registered);
1406 if (application->priv->is_remote)
1407 g_application_impl_activate_action (application->priv->impl,
1408 action_name, parameter,
1409 get_platform_data (application));
1412 g_action_group_activate_action (application->priv->actions,
1413 action_name, parameter);
1417 g_application_action_group_iface_init (GActionGroupInterface *iface)
1419 iface->has_action = g_application_has_action;
1420 iface->list_actions = g_application_list_actions;
1422 iface->get_action_enabled = g_application_get_action_enabled;
1423 iface->get_action_parameter_type = g_application_get_action_parameter_type;
1424 iface->get_action_state_type = g_application_get_action_state_type;
1425 iface->get_action_state = g_application_get_action_state;
1426 iface->change_action_state = g_application_change_action_state;
1427 iface->activate_action = g_application_activate_action;
1431 /* vim:set foldmethod=marker: */