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 * <example id="gapplication-example-open"><title>Opening files with a GApplication</title>
74 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-open.c">
75 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
81 struct _GApplicationPrivate
83 GApplicationFlags flags;
86 GActionGroup *actions;
89 guint inactivity_timeout_id;
90 guint inactivity_timeout;
93 guint is_registered : 1;
96 GHashTable *remote_actions; /* string -> RemoteActionInfo */
97 GApplicationImpl *impl;
107 PROP_INACTIVITY_TIMEOUT,
121 static guint g_application_signals[NR_SIGNALS];
123 static void g_application_action_group_iface_init (GActionGroupInterface *);
124 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
125 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
126 g_application_action_group_iface_init))
128 /* vfunc defaults {{{1 */
130 g_application_real_before_emit (GApplication *application,
131 GVariant *platform_data)
136 g_application_real_after_emit (GApplication *application,
137 GVariant *platform_data)
142 g_application_real_startup (GApplication *application)
147 g_application_real_activate (GApplication *application)
149 if (!g_signal_has_handler_pending (application,
150 g_application_signals[SIGNAL_ACTIVATE],
153 static gboolean warned;
158 g_warning ("Your application does not implement "
159 "g_application_activate() and has no handlers connected "
160 "to the 'activate' signal. It should do one of these.");
166 g_application_real_open (GApplication *application,
171 if (!g_signal_has_handler_pending (application,
172 g_application_signals[SIGNAL_OPEN],
175 static gboolean warned;
180 g_warning ("Your application claims to support opening files "
181 "but does not implement g_application_open() and has no "
182 "handlers connected to the 'open' signal.");
188 g_application_real_command_line (GApplication *application,
189 GApplicationCommandLine *cmdline)
191 static gboolean warned;
196 g_warning ("Your application claims to support custom command line "
197 "handling but does not implement g_application_command_line() "
198 "and has no handlers connected to the 'command-line' signal.");
206 g_application_real_local_command_line (GApplication *application,
210 if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
215 GError *error = NULL;
218 if (!g_application_register (application, NULL, &error))
220 g_critical ("%s", error->message);
221 g_error_free (error);
226 n_args = g_strv_length (*arguments);
228 if (application->priv->flags & G_APPLICATION_IS_SERVICE)
230 if ((*exit_status = n_args > 1))
232 g_printerr ("GApplication service mode takes no arguments.\n");
233 application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
241 g_application_activate (application);
247 if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
249 g_critical ("This application can not open files.");
258 n_files = n_args - 1;
259 files = g_new (GFile *, n_files);
261 for (i = 0; i < n_files; i++)
262 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
264 g_application_open (application, files, n_files, "");
266 for (i = 0; i < n_files; i++)
267 g_object_unref (files[i]);
279 g_application_real_add_platform_data (GApplication *application,
280 GVariantBuilder *builder)
285 g_application_real_quit_mainloop (GApplication *application)
287 if (application->priv->mainloop != NULL)
288 g_main_loop_quit (application->priv->mainloop);
292 g_application_real_run_mainloop (GApplication *application)
294 if (application->priv->mainloop == NULL)
295 application->priv->mainloop = g_main_loop_new (NULL, FALSE);
297 g_main_loop_run (application->priv->mainloop);
300 /* GObject implementation stuff {{{1 */
302 g_application_set_property (GObject *object,
307 GApplication *application = G_APPLICATION (object);
311 case PROP_APPLICATION_ID:
312 g_application_set_application_id (application,
313 g_value_get_string (value));
317 g_application_set_flags (application, g_value_get_flags (value));
320 case PROP_INACTIVITY_TIMEOUT:
321 g_application_set_inactivity_timeout (application,
322 g_value_get_uint (value));
325 case PROP_ACTION_GROUP:
326 g_application_set_action_group (application,
327 g_value_get_object (value));
331 g_assert_not_reached ();
336 * g_application_set_action_group:
337 * @application: a #GApplication
338 * @action_group: a #GActionGroup, or %NULL
340 * Sets or unsets the group of actions associated with the application.
342 * These actions are the actions that can be remotely invoked.
344 * It is an error to call this function after the application has been
350 g_application_set_action_group (GApplication *application,
351 GActionGroup *action_group)
353 g_return_if_fail (G_IS_APPLICATION (application));
354 g_return_if_fail (!application->priv->is_registered);
356 if (application->priv->actions != NULL)
357 g_object_unref (application->priv->actions);
359 application->priv->actions = action_group;
361 if (application->priv->actions != NULL)
362 g_object_ref (application->priv->actions);
366 g_application_get_property (GObject *object,
371 GApplication *application = G_APPLICATION (object);
375 case PROP_APPLICATION_ID:
376 g_value_set_string (value,
377 g_application_get_application_id (application));
381 g_value_set_flags (value,
382 g_application_get_flags (application));
385 case PROP_IS_REGISTERED:
386 g_value_set_boolean (value,
387 g_application_get_is_registered (application));
391 g_value_set_boolean (value,
392 g_application_get_is_remote (application));
395 case PROP_INACTIVITY_TIMEOUT:
396 g_value_set_uint (value,
397 g_application_get_inactivity_timeout (application));
401 g_assert_not_reached ();
406 g_application_constructed (GObject *object)
408 GApplication *application = G_APPLICATION (object);
410 g_assert (application->priv->id != NULL);
414 g_application_finalize (GObject *object)
416 GApplication *application = G_APPLICATION (object);
418 if (application->priv->impl)
419 g_application_impl_destroy (application->priv->impl);
420 g_free (application->priv->id);
422 if (application->priv->mainloop)
423 g_main_loop_unref (application->priv->mainloop);
425 G_OBJECT_CLASS (g_application_parent_class)
430 g_application_init (GApplication *application)
432 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
434 GApplicationPrivate);
438 g_application_class_init (GApplicationClass *class)
440 GObjectClass *object_class = G_OBJECT_CLASS (class);
442 object_class->constructed = g_application_constructed;
443 object_class->finalize = g_application_finalize;
444 object_class->get_property = g_application_get_property;
445 object_class->set_property = g_application_set_property;
447 class->before_emit = g_application_real_before_emit;
448 class->after_emit = g_application_real_after_emit;
449 class->startup = g_application_real_startup;
450 class->activate = g_application_real_activate;
451 class->open = g_application_real_open;
452 class->command_line = g_application_real_command_line;
453 class->local_command_line = g_application_real_local_command_line;
454 class->add_platform_data = g_application_real_add_platform_data;
455 class->quit_mainloop = g_application_real_quit_mainloop;
456 class->run_mainloop = g_application_real_run_mainloop;
458 g_object_class_install_property (object_class, PROP_APPLICATION_ID,
459 g_param_spec_string ("application-id",
460 P_("Application identifier"),
461 P_("The unique identifier for the application"),
462 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
463 G_PARAM_STATIC_STRINGS));
465 g_object_class_install_property (object_class, PROP_FLAGS,
466 g_param_spec_flags ("flags",
467 P_("Application flags"),
468 P_("Flags specifying the behaviour of the application"),
469 G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
470 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
472 g_object_class_install_property (object_class, PROP_IS_REGISTERED,
473 g_param_spec_boolean ("is-registered",
475 P_("If g_application_register() has been called"),
476 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
478 g_object_class_install_property (object_class, PROP_IS_REMOTE,
479 g_param_spec_boolean ("is-remote",
481 P_("If this application instance is remote"),
482 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
484 g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
485 g_param_spec_uint ("inactivity-timeout",
486 P_("Inactivity timeout"),
487 P_("Iime (ms) to stay alive after becoming idle"),
489 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
491 g_object_class_install_property (object_class, PROP_ACTION_GROUP,
492 g_param_spec_object ("action-group",
494 P_("The group of actions that the application exports"),
496 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
499 * GApplication::startup:
500 * @application: the application
502 * The ::startup signal is emitted on the primary instance immediately
503 * after registration. See g_activation_register().
505 g_application_signals[SIGNAL_STARTUP] =
506 g_signal_new ("startup", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
507 G_STRUCT_OFFSET (GApplicationClass, startup),
508 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
511 * GApplication::activate:
512 * @application: the application
514 * The ::activate signal is emitted on the primary instance when an
515 * activation occurs. See g_application_activate().
517 g_application_signals[SIGNAL_ACTIVATE] =
518 g_signal_new ("activate", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
519 G_STRUCT_OFFSET (GApplicationClass, activate),
520 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
524 * GApplication::open:
525 * @application: the application
526 * @files: an array of #GFile objects
527 * @n_files: the length of @files
528 * @hint: a hint provided by the calling instance
530 * The ::open signal is emitted on the primary instance when there are
531 * files to open. See g_application_open() for more information.
533 g_application_signals[SIGNAL_OPEN] =
534 g_signal_new ("open", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
535 G_STRUCT_OFFSET (GApplicationClass, open),
536 NULL, NULL, _gio_marshal_VOID__POINTER_INT_STRING,
537 G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
540 * GApplication::command-line:
541 * @application: the application
542 * @command_line: a #GApplicationCommandLine representing the
545 * The ::command-line signal is emitted on the primary instance when
546 * a commandline is not handled locally. See g_application_run() for
549 g_application_signals[SIGNAL_COMMAND_LINE] =
550 g_signal_new ("command-line", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
551 G_STRUCT_OFFSET (GApplicationClass, command_line),
552 g_signal_accumulator_first_wins, NULL,
553 _gio_marshal_INT__OBJECT,
554 G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
556 g_type_class_add_private (class, sizeof (GApplicationPrivate));
560 get_platform_data (GApplication *application)
562 GVariantBuilder *builder;
565 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
568 gchar *cwd = g_get_current_dir ();
569 g_variant_builder_add (builder, "{sv}", "cwd",
570 g_variant_new_bytestring (cwd));
574 if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
579 envp = g_get_environ ();
580 array = g_variant_new_bytestring_array ((const gchar **) envp, -1);
583 g_variant_builder_add (builder, "{sv}", "environ", array);
586 G_APPLICATION_GET_CLASS (application)->
587 add_platform_data (application, builder);
589 result = g_variant_builder_end (builder);
590 g_variant_builder_unref (builder);
595 /* Application ID validity {{{1 */
598 * g_application_id_is_valid:
599 * @application_id: a potential application identifier
600 * @returns: %TRUE if @application_id is valid
602 * Checks if @application_id is a valid application identifier.
604 * A valid ID is required for calls to g_application_new() and
605 * g_application_set_application_id().
607 * For convenience, the restrictions on application identifiers are
610 * <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-" and must not begin with a digit.</listitem>
611 * <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least two elements).</listitem>
612 * <listitem>Application identifiers must not begin with a '.' (period) character.</listitem>
613 * <listitem>Application identifiers must not contain consecutive '.' (period) characters.</listitem>
614 * <listitem>Application identifiers must not exceed 255 characters.</listitem>
618 g_application_id_is_valid (const gchar *application_id)
622 if (strlen (application_id) > 255)
625 if (!g_ascii_isalpha (*application_id))
630 for (; *application_id; application_id++)
632 if (g_ascii_isalnum (*application_id) ||
633 (*application_id == '-') ||
634 (*application_id == '_'))
636 else if (allow_dot && *application_id == '.')
645 /* Public Constructor {{{1 */
648 * @application_id: the application id
649 * @flags: the application flags
650 * @returns: a new #GApplication instance
652 * Creates a new #GApplication instance.
654 * This function calls g_type_init() for you.
656 * The application id must be valid. See g_application_id_is_valid().
659 g_application_new (const gchar *application_id,
660 GApplicationFlags flags)
662 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
666 return g_object_new (G_TYPE_APPLICATION,
667 "application-id", application_id,
672 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
674 * g_application_get_application_id:
675 * @application: a #GApplication
676 * @returns: the identifier for @application, owned by @application
678 * Gets the unique identifier for @application.
683 g_application_get_application_id (GApplication *application)
685 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
687 return application->priv->id;
691 * g_application_set_application_id:
692 * @application: a #GApplication
693 * @application_id: the identifier for @application
695 * Sets the unique identifier for @application.
697 * The application id can only be modified if @application has not yet
700 * The application id must be valid. See g_application_id_is_valid().
705 g_application_set_application_id (GApplication *application,
706 const gchar *application_id)
708 g_return_if_fail (G_IS_APPLICATION (application));
710 if (g_strcmp0 (application->priv->id, application_id) != 0)
712 g_return_if_fail (g_application_id_is_valid (application_id));
713 g_return_if_fail (!application->priv->is_registered);
715 g_free (application->priv->id);
716 application->priv->id = g_strdup (application_id);
718 g_object_notify (G_OBJECT (application), "application-id");
723 * g_application_get_flags:
724 * @application: a #GApplication
725 * @returns: the flags for @application
727 * Gets the flags for @application.
729 * See #GApplicationFlags.
734 g_application_get_flags (GApplication *application)
736 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
738 return application->priv->flags;
742 * g_application_set_flags:
743 * @application: a #GApplication
744 * @flags: the flags for @application
746 * Sets the flags for @application.
748 * The flags can only be modified if @application has not yet been
751 * See #GApplicationFlags.
756 g_application_set_flags (GApplication *application,
757 GApplicationFlags flags)
759 g_return_if_fail (G_IS_APPLICATION (application));
761 if (application->priv->flags != flags)
763 g_return_if_fail (!application->priv->is_registered);
765 application->priv->flags = flags;
767 g_object_notify (G_OBJECT (application), "flags");
772 * g_application_get_inactivity_timeout:
773 * @application: a #GApplication
775 * Gets the current inactivity timeout for the application.
777 * This is the amount of time (in milliseconds) after the last call to
778 * g_application_release() before the application stops running.
780 * Returns: the timeout, in milliseconds
785 g_application_get_inactivity_timeout (GApplication *application)
787 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
789 return application->priv->inactivity_timeout;
793 * g_application_set_inactivity_timeout:
794 * @application: a #GApplication
795 * @inactivity_timeout: the timeout, in milliseconds
797 * Sets 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 * This call has no side effects of its own. The value set here is only
803 * used for next time g_application_release() drops the use count to
804 * zero. Any timeouts currently in progress are not impacted.
806 * Returns: the timeout, in milliseconds
811 g_application_set_inactivity_timeout (GApplication *application,
812 guint inactivity_timeout)
814 g_return_if_fail (G_IS_APPLICATION (application));
816 if (application->priv->inactivity_timeout != inactivity_timeout)
818 application->priv->inactivity_timeout = inactivity_timeout;
820 g_object_notify (G_OBJECT (application), "inactivity-timeout");
823 /* Read-only property getters (is registered, is remote) {{{1 */
825 * g_application_get_is_registered:
826 * @application: a #GApplication
827 * @returns: %TRUE if @application is registered
829 * Checks if @application is registered.
831 * An application is registered if g_application_register() has been
832 * successfully called.
837 g_application_get_is_registered (GApplication *application)
839 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
841 return application->priv->is_registered;
845 * g_application_get_is_remote:
846 * @application: a #GApplication
847 * @returns: %TRUE if @application is remote
849 * Checks if @application is remote.
851 * If @application is remote then it means that another instance of
852 * application already exists (the 'primary' instance). Calls to
853 * perform actions on @application will result in the actions being
854 * performed by the primary instance.
856 * The value of this property can not be accessed before
857 * g_application_register() has been called. See
858 * g_application_get_is_registered().
863 g_application_get_is_remote (GApplication *application)
865 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
866 g_return_val_if_fail (application->priv->is_registered, FALSE);
868 return application->priv->is_remote;
873 * g_application_register:
874 * @application: a #GApplication
875 * @cancellable: a #GCancellable, or %NULL
876 * @error: a pointer to a NULL #GError, or %NULL
877 * @returns: %TRUE if registration succeeded
879 * Attempts registration of the application.
881 * This is the point at which the application discovers if it is the
882 * primary instance or merely acting as a remote for an already-existing
885 * If the application has already been registered then %TRUE is
886 * returned with no work performed.
888 * The #GApplication::startup signal is emitted if registration succeeds
889 * and @application is the primary instance.
891 * In the event of an error (such as @cancellable being cancelled, or a
892 * failure to connect to the session bus), %FALSE is returned and @error
893 * is set appropriately.
895 * Note: the return value of this function is not an indicator that this
896 * instance is or is not the primary instance of the application. See
897 * g_application_get_is_remote() for that.
902 g_application_register (GApplication *application,
903 GCancellable *cancellable,
906 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
908 if (!application->priv->is_registered)
910 application->priv->impl =
911 g_application_impl_register (application, application->priv->id,
912 application->priv->flags,
913 &application->priv->remote_actions,
916 if (application->priv->impl == NULL)
919 application->priv->is_remote = application->priv->remote_actions != NULL;
920 application->priv->is_registered = TRUE;
922 g_object_notify (G_OBJECT (application), "is-registered");
924 if (!application->priv->is_remote)
925 g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
931 /* Hold/release {{{1 */
933 * g_application_hold:
934 * @application: a #GApplication
936 * Increases the use count of @application.
938 * Use this function to indicate that the application has a reason to
939 * continue to run. For example, g_application_hold() is called by GTK+
940 * when a toplevel window is on the screen.
942 * To cancel the hold, call g_application_release().
945 g_application_hold (GApplication *application)
947 if (application->priv->inactivity_timeout_id)
949 g_source_remove (application->priv->inactivity_timeout_id);
950 application->priv->inactivity_timeout_id = 0;
953 application->priv->use_count++;
957 inactivity_timeout_expired (gpointer data)
959 GApplication *application = G_APPLICATION (data);
961 G_APPLICATION_GET_CLASS (application)
962 ->quit_mainloop (application);
969 * g_application_release:
970 * @application: a #GApplication
972 * Decrease the use count of @application.
974 * When the use count reaches zero, the application will stop running.
976 * Never call this function except to cancel the effect of a previous
977 * call to g_application_hold().
980 g_application_release (GApplication *application)
982 application->priv->use_count--;
984 if (application->priv->use_count == 0)
986 if (application->priv->inactivity_timeout)
987 application->priv->inactivity_timeout_id =
988 g_timeout_add (application->priv->inactivity_timeout,
989 inactivity_timeout_expired, application);
992 G_APPLICATION_GET_CLASS (application)
993 ->quit_mainloop (application);
997 /* Activate, Open {{{1 */
999 * g_application_activate:
1000 * @application: a #GApplication
1002 * Activates the application.
1004 * In essence, this results in the #GApplication::activate() signal being
1005 * emitted in the primary instance.
1007 * The application must be registered before calling this function.
1012 g_application_activate (GApplication *application)
1014 g_return_if_fail (G_IS_APPLICATION (application));
1015 g_return_if_fail (application->priv->is_registered);
1017 if (application->priv->is_remote)
1018 g_application_impl_activate (application->priv->impl,
1019 get_platform_data (application));
1022 g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
1026 * g_application_open:
1027 * @application: a #GApplication
1028 * @files: an array of #GFiles to open
1029 * @n_files: the length of the @files array
1030 * @hint: a hint (or ""), but never %NULL
1032 * Opens the given files.
1034 * In essence, this results in the #GApplication::open signal being emitted
1035 * in the primary instance.
1037 * @n_files must be greater than zero.
1039 * @hint is simply passed through to the ::open signal. It is
1040 * intended to be used by applications that have multiple modes for
1041 * opening files (eg: "view" vs "edit", etc). Unless you have a need
1042 * for this functionality, you should use "".
1044 * The application must be registered before calling this function
1045 * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
1050 g_application_open (GApplication *application,
1055 g_return_if_fail (G_IS_APPLICATION (application));
1056 g_return_if_fail (application->priv->flags &
1057 G_APPLICATION_HANDLES_OPEN);
1058 g_return_if_fail (application->priv->is_registered);
1060 if (application->priv->is_remote)
1061 g_application_impl_open (application->priv->impl,
1062 files, n_files, hint,
1063 get_platform_data (application));
1066 g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
1067 0, files, n_files, hint);
1072 * g_application_run:
1073 * @application: a #GApplication
1074 * @argc: the argc from main()
1075 * @argv: the argv from main()
1076 * @returns: the exit status
1078 * Runs the application.
1080 * This function is intended to be run from main() and its return value
1081 * is intended to be returned by main().
1083 * First, the local_command_line() virtual function is invoked. This
1084 * function always runs on the local instance. If that function returns
1085 * %FALSE then the application is registered and the #GApplication::command-line
1086 * signal is emitted in the primary instance (which may or may not be
1089 * If the application has the %G_APPLICATION_HANDLES_COMMAND_LINE
1090 * flag set then the default implementation of local_command_line()
1091 * always returns %FALSE immediately, resulting in the commandline
1092 * always being handled in the primary instance.
1094 * Otherwise, the default implementation of local_command_line() tries
1095 * to do a couple of things that are probably reasonable for most
1096 * applications. First, g_application_register() is called to attempt
1097 * to register the application. If that works, then the command line
1098 * arguments are inspected. If no commandline arguments are given, then
1099 * g_application_activate() is called. If commandline arguments are
1100 * given and the %G_APPLICATION_HANDLES_OPEN flag is set then they
1101 * are assumed to be filenames and g_application_open() is called.
1103 * If you are interested in doing more complicated local handling of the
1104 * commandline then you should implement your own #GApplication subclass
1105 * and override local_command_line(). See
1106 * <xref linkend="gapplication-example-cmdline2"/> for an example.
1108 * If, after the above is done, the use count of the application is zero
1109 * then the exit status is returned immediately. If the use count is
1110 * non-zero then the mainloop is run until the use count falls to zero,
1111 * at which point 0 is returned.
1113 * If the %G_APPLICATION_IS_SERVICE flag is set, then the exiting at
1114 * use count of zero is delayed for a while (ie: the instance stays
1115 * around to provide its <emphasis>service</emphasis> to others).
1120 g_application_run (GApplication *application,
1128 g_return_val_if_fail (G_IS_APPLICATION (application), 1);
1129 g_return_val_if_fail (argc == 0 || argv != NULL, 1);
1131 arguments = g_new (gchar *, argc + 1);
1132 for (i = 0; i < argc; i++)
1133 arguments[i] = g_strdup (argv[i]);
1134 arguments[i] = NULL;
1136 if (g_get_prgname () == NULL && argc > 0)
1140 prgname = g_path_get_basename (argv[0]);
1141 g_set_prgname (prgname);
1145 if (!G_APPLICATION_GET_CLASS (application)
1146 ->local_command_line (application, &arguments, &status))
1148 GError *error = NULL;
1150 if (!g_application_register (application, NULL, &error))
1152 g_printerr ("%s", error->message);
1153 g_error_free (error);
1157 if (application->priv->is_remote)
1159 GVariant *platform_data;
1161 platform_data = get_platform_data (application);
1162 status = g_application_impl_command_line (application->priv->impl,
1163 arguments, platform_data);
1167 GApplicationCommandLine *cmdline;
1170 v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
1171 cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
1172 "arguments", v, NULL);
1173 g_signal_emit (application,
1174 g_application_signals[SIGNAL_COMMAND_LINE],
1175 0, cmdline, &status);
1176 g_object_unref (cmdline);
1180 g_strfreev (arguments);
1182 if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
1183 application->priv->is_registered &&
1184 !application->priv->use_count &&
1185 !application->priv->inactivity_timeout_id)
1187 application->priv->inactivity_timeout_id =
1188 g_timeout_add (10000, inactivity_timeout_expired, application);
1191 if (application->priv->use_count ||
1192 application->priv->inactivity_timeout_id)
1194 G_APPLICATION_GET_CLASS (application)
1195 ->run_mainloop (application);
1199 if (application->priv->impl)
1200 g_application_impl_flush (application->priv->impl);
1206 g_application_has_action (GActionGroup *action_group,
1207 const gchar *action_name)
1209 GApplication *application = G_APPLICATION (action_group);
1211 g_return_val_if_fail (application->priv->is_registered, FALSE);
1213 if (application->priv->remote_actions != NULL)
1214 return g_hash_table_lookup (application->priv->remote_actions,
1215 action_name) != NULL;
1217 return application->priv->actions &&
1218 g_action_group_has_action (application->priv->actions, action_name);
1222 g_application_list_actions (GActionGroup *action_group)
1224 GApplication *application = G_APPLICATION (action_group);
1226 g_return_val_if_fail (application->priv->is_registered, NULL);
1228 if (application->priv->remote_actions != NULL)
1230 GHashTableIter iter;
1235 n = g_hash_table_size (application->priv->remote_actions);
1236 keys = g_new (gchar *, n + 1);
1238 g_hash_table_iter_init (&iter, application->priv->remote_actions);
1239 while (g_hash_table_iter_next (&iter, &key, NULL))
1240 keys[i++] = g_strdup (key);
1241 g_assert_cmpint (i, ==, n);
1247 else if (application->priv->actions != NULL)
1248 return g_action_group_list_actions (application->priv->actions);
1251 /* empty string array */
1252 return g_new0 (gchar *, 1);
1256 g_application_get_action_enabled (GActionGroup *action_group,
1257 const gchar *action_name)
1259 GApplication *application = G_APPLICATION (action_group);
1261 g_return_val_if_fail (application->priv->actions != NULL, FALSE);
1262 g_return_val_if_fail (application->priv->is_registered, FALSE);
1264 if (application->priv->remote_actions)
1266 RemoteActionInfo *info;
1268 info = g_hash_table_lookup (application->priv->remote_actions,
1271 return info && info->enabled;
1274 return g_action_group_get_action_enabled (application->priv->actions,
1278 static const GVariantType *
1279 g_application_get_action_parameter_type (GActionGroup *action_group,
1280 const gchar *action_name)
1282 GApplication *application = G_APPLICATION (action_group);
1284 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1285 g_return_val_if_fail (application->priv->is_registered, NULL);
1287 if (application->priv->remote_actions)
1289 RemoteActionInfo *info;
1291 info = g_hash_table_lookup (application->priv->remote_actions,
1295 return info->parameter_type;
1300 return g_action_group_get_action_parameter_type (application->priv->actions,
1304 static const GVariantType *
1305 g_application_get_action_state_type (GActionGroup *action_group,
1306 const gchar *action_name)
1308 GApplication *application = G_APPLICATION (action_group);
1310 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1311 g_return_val_if_fail (application->priv->is_registered, NULL);
1313 if (application->priv->remote_actions)
1315 RemoteActionInfo *info;
1317 info = g_hash_table_lookup (application->priv->remote_actions,
1320 if (info && info->state)
1321 return g_variant_get_type (info->state);
1326 return g_action_group_get_action_state_type (application->priv->actions,
1331 g_application_get_action_state (GActionGroup *action_group,
1332 const gchar *action_name)
1334 GApplication *application = G_APPLICATION (action_group);
1336 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1337 g_return_val_if_fail (application->priv->is_registered, NULL);
1339 if (application->priv->remote_actions)
1341 RemoteActionInfo *info;
1343 info = g_hash_table_lookup (application->priv->remote_actions,
1346 if (info && info->state)
1347 return g_variant_ref (info->state);
1352 return g_action_group_get_action_state (application->priv->actions,
1357 g_application_change_action_state (GActionGroup *action_group,
1358 const gchar *action_name,
1361 GApplication *application = G_APPLICATION (action_group);
1363 g_return_if_fail (application->priv->is_registered);
1365 if (application->priv->is_remote)
1366 g_application_impl_change_action_state (application->priv->impl,
1368 get_platform_data (application));
1371 g_action_group_change_action_state (application->priv->actions,
1372 action_name, value);
1376 g_application_activate_action (GActionGroup *action_group,
1377 const gchar *action_name,
1378 GVariant *parameter)
1380 GApplication *application = G_APPLICATION (action_group);
1382 g_return_if_fail (application->priv->is_registered);
1384 if (application->priv->is_remote)
1385 g_application_impl_activate_action (application->priv->impl,
1386 action_name, parameter,
1387 get_platform_data (application));
1390 g_action_group_activate_action (application->priv->actions,
1391 action_name, parameter);
1395 g_application_action_group_iface_init (GActionGroupInterface *iface)
1397 iface->has_action = g_application_has_action;
1398 iface->list_actions = g_application_list_actions;
1400 iface->get_action_enabled = g_application_get_action_enabled;
1401 iface->get_action_parameter_type = g_application_get_action_parameter_type;
1402 iface->get_action_state_type = g_application_get_action_state_type;
1403 iface->get_action_state = g_application_get_action_state;
1404 iface->change_action_state = g_application_change_action_state;
1405 iface->activate_action = g_application_activate_action;
1409 /* vim:set foldmethod=marker: */