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 convenience, the restrictions on application identifiers are reproduced
67 * <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-" and must not begin with a digit.</listitem>
68 * <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least two elements).</listitem>
69 * <listitem>Application identifiers must not begin with a '.' (period) character.</listitem>
70 * <listitem>Application identifiers must not contain consecutive '.' (period) characters.</listitem>
71 * <listitem>Application identifiers must not exceed 255 characters.</listitem>
75 struct _GApplicationPrivate
77 GApplicationFlags flags;
80 GActionGroup *actions;
83 guint inactivity_timeout_id;
84 guint inactivity_timeout;
87 guint is_registered : 1;
90 GApplicationImpl *impl;
100 PROP_INACTIVITY_TIMEOUT,
114 static guint g_application_signals[NR_SIGNALS];
116 static void g_application_action_group_iface_init (GActionGroupInterface *);
117 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
118 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
119 g_application_action_group_iface_init))
121 /* vfunc defaults {{{1 */
123 g_application_real_before_emit (GApplication *application,
124 GVariant *platform_data)
129 g_application_real_after_emit (GApplication *application,
130 GVariant *platform_data)
135 g_application_real_startup (GApplication *application)
140 g_application_real_activate (GApplication *application)
142 if (!g_signal_has_handler_pending (application,
143 g_application_signals[SIGNAL_ACTIVATE],
146 static gboolean warned;
151 g_warning ("Your application does not implement "
152 "g_application_activate() and has no handlers connected "
153 "to the 'activate' signal. It should do one of these.");
159 g_application_real_open (GApplication *application,
164 if (!g_signal_has_handler_pending (application,
165 g_application_signals[SIGNAL_OPEN],
168 static gboolean warned;
173 g_warning ("Your application claims to support opening files "
174 "but does not implement g_application_open() and has no "
175 "handlers connected to the 'open' signal.");
181 g_application_real_command_line (GApplication *application,
182 GApplicationCommandLine *cmdline)
184 static gboolean warned;
189 g_warning ("Your application claims to support custom command line "
190 "handling but does not implement g_application_command_line() "
191 "and has no handlers connected to the 'command-line' signal.");
199 g_application_real_local_command_line (GApplication *application,
203 if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
208 GError *error = NULL;
211 if (!g_application_register (application, NULL, &error))
213 g_critical ("%s", error->message);
214 g_error_free (error);
219 n_args = g_strv_length (*arguments);
221 if (application->priv->flags & G_APPLICATION_IS_SERVICE)
225 g_printerr ("GApplication service mode takes no arguments.\n");
226 application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
234 g_application_activate (application);
240 if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
242 g_critical ("This application can not open files.");
251 n_files = n_args - 1;
252 files = g_new (GFile *, n_files);
254 for (i = 0; i < n_files; i++)
255 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
257 g_application_open (application, files, n_files, "");
259 for (i = 0; i < n_files; i++)
260 g_object_unref (files[i]);
272 g_application_real_add_platform_data (GApplication *application,
273 GVariantBuilder *builder)
278 g_application_real_quit_mainloop (GApplication *application)
280 if (application->priv->mainloop != NULL)
281 g_main_loop_quit (application->priv->mainloop);
285 g_application_real_run_mainloop (GApplication *application)
287 if (application->priv->mainloop == NULL)
288 application->priv->mainloop = g_main_loop_new (NULL, FALSE);
290 g_main_loop_run (application->priv->mainloop);
293 /* GObject implementation stuff {{{1 */
295 g_application_set_property (GObject *object,
300 GApplication *application = G_APPLICATION (object);
304 case PROP_APPLICATION_ID:
305 g_application_set_application_id (application,
306 g_value_get_string (value));
310 g_application_set_flags (application, g_value_get_flags (value));
313 case PROP_INACTIVITY_TIMEOUT:
314 g_application_set_inactivity_timeout (application,
315 g_value_get_uint (value));
318 case PROP_ACTION_GROUP:
319 g_application_set_action_group (application,
320 g_value_get_object (value));
324 g_assert_not_reached ();
329 * g_application_set_action_group:
330 * @application: a #GApplication
331 * @action_group: a #GActionGroup, or %NULL
333 * Sets or unsets the group of actions associated with the application.
335 * These actions are the actions that can be remotely invoked.
337 * It is an error to call this function after the application has been
343 g_application_set_action_group (GApplication *application,
344 GActionGroup *action_group)
346 g_return_if_fail (G_IS_APPLICATION (application));
347 g_return_if_fail (application->priv->is_registered);
349 if (application->priv->actions != NULL)
350 g_object_unref (application->priv->actions);
352 application->priv->actions = action_group;
354 if (application->priv->actions != NULL)
355 g_object_ref (application->priv->actions);
359 g_application_get_property (GObject *object,
364 GApplication *application = G_APPLICATION (object);
368 case PROP_APPLICATION_ID:
369 g_value_set_string (value,
370 g_application_get_application_id (application));
374 g_value_set_flags (value,
375 g_application_get_flags (application));
378 case PROP_IS_REGISTERED:
379 g_value_set_boolean (value,
380 g_application_get_is_registered (application));
384 g_value_set_boolean (value,
385 g_application_get_is_remote (application));
388 case PROP_INACTIVITY_TIMEOUT:
389 g_value_set_uint (value,
390 g_application_get_inactivity_timeout (application));
394 g_assert_not_reached ();
399 g_application_constructed (GObject *object)
401 GApplication *application = G_APPLICATION (object);
403 g_assert (application->priv->id != NULL);
407 g_application_finalize (GObject *object)
409 GApplication *application = G_APPLICATION (object);
411 if (application->priv->impl)
412 g_application_impl_destroy (application->priv->impl);
413 g_free (application->priv->id);
415 if (application->priv->mainloop)
416 g_main_loop_unref (application->priv->mainloop);
418 G_OBJECT_CLASS (g_application_parent_class)
423 g_application_init (GApplication *application)
425 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
427 GApplicationPrivate);
431 g_application_class_init (GApplicationClass *class)
433 GObjectClass *object_class = G_OBJECT_CLASS (class);
435 object_class->constructed = g_application_constructed;
436 object_class->finalize = g_application_finalize;
437 object_class->get_property = g_application_get_property;
438 object_class->set_property = g_application_set_property;
440 class->before_emit = g_application_real_before_emit;
441 class->after_emit = g_application_real_after_emit;
442 class->startup = g_application_real_startup;
443 class->activate = g_application_real_activate;
444 class->open = g_application_real_open;
445 class->command_line = g_application_real_command_line;
446 class->local_command_line = g_application_real_local_command_line;
447 class->add_platform_data = g_application_real_add_platform_data;
448 class->quit_mainloop = g_application_real_quit_mainloop;
449 class->run_mainloop = g_application_real_run_mainloop;
451 g_object_class_install_property (object_class, PROP_APPLICATION_ID,
452 g_param_spec_string ("application-id",
453 P_("Application identifier"),
454 P_("The unique identifier for the application"),
455 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
456 G_PARAM_STATIC_STRINGS));
458 g_object_class_install_property (object_class, PROP_FLAGS,
459 g_param_spec_flags ("flags",
460 P_("Application flags"),
461 P_("Flags specifying the behaviour of the application"),
462 G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
463 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
465 g_object_class_install_property (object_class, PROP_IS_REGISTERED,
466 g_param_spec_boolean ("is-registered",
468 P_("If g_application_register() has been called"),
469 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
471 g_object_class_install_property (object_class, PROP_IS_REMOTE,
472 g_param_spec_boolean ("is-remote",
474 P_("If this application instance is remote"),
475 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
477 g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
478 g_param_spec_boolean ("inactivity-timeout",
479 P_("Inactivity timeout"),
480 P_("Iime (ms) to stay alive after becoming idle"),
481 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
483 g_object_class_install_property (object_class, PROP_ACTION_GROUP,
484 g_param_spec_object ("action-group",
486 P_("The group of actions that the application exports"),
488 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
491 * GApplication::startup:
492 * @application: the application
494 * The ::startup signal is emitted on the primary instance immediately
495 * after registration. See g_activation_register().
497 g_application_signals[SIGNAL_STARTUP] =
498 g_signal_new ("startup", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
499 G_STRUCT_OFFSET (GApplicationClass, startup),
500 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
503 * GApplication::activate:
504 * @application: the application
506 * The ::activate signal is emitted on the primary instance when an
507 * activation occurs. See g_application_activate().
509 g_application_signals[SIGNAL_ACTIVATE] =
510 g_signal_new ("activate", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
511 G_STRUCT_OFFSET (GApplicationClass, activate),
512 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
516 * GApplication::open:
517 * @application: the application
518 * @files: an array of #GFile objects
519 * @n_files: the length of @files
520 * @hint: a hint provided by the calling instance
522 * The ::open signal is emitted on the primary instance when there are
523 * files to open. See g_application_open() for more information.
525 g_application_signals[SIGNAL_OPEN] =
526 g_signal_new ("open", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
527 G_STRUCT_OFFSET (GApplicationClass, open),
528 NULL, NULL, _gio_marshal_VOID__POINTER_INT_STRING,
529 G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
532 * GApplication::command-line:
533 * @application: the application
534 * @command_line: a #GApplicationCommandLine representing the
537 * The ::command-line signal is emitted on the primary instance when
538 * a commandline is not handled locally. See g_application_run() for
541 g_application_signals[SIGNAL_COMMAND_LINE] =
542 g_signal_new ("command-line", G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
543 G_STRUCT_OFFSET (GApplicationClass, command_line),
544 g_signal_accumulator_first_wins, NULL,
545 _gio_marshal_INT__OBJECT,
546 G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
548 g_type_class_add_private (class, sizeof (GApplicationPrivate));
552 get_platform_data (GApplication *application)
554 GVariantBuilder *builder;
557 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
560 gchar *cwd = g_get_current_dir ();
561 g_variant_builder_add (builder, "{sv}", "cwd",
562 g_variant_new_bytestring (cwd));
566 G_APPLICATION_GET_CLASS (application)->
567 add_platform_data (application, builder);
569 result = g_variant_builder_end (builder);
570 g_variant_builder_unref (builder);
575 /* Application ID validity {{{1 */
578 * g_application_id_is_valid:
579 * @application_id: a potential application identifier
580 * @returns: %TRUE if @application_id is valid
582 * Checks if @application_id is a valid application ID. A valid ID is
583 * required for calls to g_application_new() and
584 * g_application_set_application_id().
587 g_application_id_is_valid (const gchar *application_id)
591 if (strlen (application_id) > 255)
594 if (!g_ascii_isalpha (*application_id))
599 for (; *application_id; application_id++)
601 if (g_ascii_isalnum (*application_id) ||
602 (*application_id == '-') ||
603 (*application_id == '_'))
605 else if (allow_dot && *application_id == '.')
614 /* Public Constructor {{{1 */
617 * @application_id: the application id
618 * @flags: the application flags
619 * @returns: a new #GApplication instance
621 * Creates a new #GApplication instance.
623 * This function calls g_type_init() for you.
625 * The application id must be valid. See g_application_id_is_valid().
628 g_application_new (const gchar *application_id,
629 GApplicationFlags flags)
631 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
635 return g_object_new (G_TYPE_APPLICATION,
636 "application-id", application_id,
641 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
643 * g_application_get_application_id:
644 * @application: a #GApplication
645 * @returns: the identifier for @application, owned by @application
647 * Gets the unique identifier for @application.
652 g_application_get_application_id (GApplication *application)
654 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
656 return application->priv->id;
660 * g_application_set_application_id:
661 * @application: a #GApplication
662 * @application_id: the identifier for @application
664 * Sets the unique identifier for @application.
666 * The application id can only be modified if @application has not yet
669 * The application id must be valid. See g_application_id_is_valid().
674 g_application_set_application_id (GApplication *application,
675 const gchar *application_id)
677 g_return_if_fail (G_IS_APPLICATION (application));
679 if (g_strcmp0 (application->priv->id, application_id) != 0)
681 g_return_if_fail (g_application_id_is_valid (application_id));
682 g_return_if_fail (!application->priv->is_registered);
684 g_free (application->priv->id);
685 application->priv->id = g_strdup (application_id);
687 g_object_notify (G_OBJECT (application), "application-id");
692 * g_application_get_flags:
693 * @application: a #GApplication
694 * @returns: the flags for @application
696 * Gets the flags for @application.
698 * See #GApplicationFlags.
703 g_application_get_flags (GApplication *application)
705 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
707 return application->priv->flags;
711 * g_application_set_flags:
712 * @application: a #GApplication
713 * @flags: the flags for @application
715 * Sets the flags for @application.
717 * The flags can only be modified if @application has not yet been
720 * See #GApplicationFlags.
725 g_application_set_flags (GApplication *application,
726 GApplicationFlags flags)
728 g_return_if_fail (G_IS_APPLICATION (application));
730 if (application->priv->flags != flags)
732 g_return_if_fail (!application->priv->is_registered);
734 application->priv->flags = flags;
736 g_object_notify (G_OBJECT (application), "flags");
741 * g_application_get_inactivity_timeout:
742 * @application: a #GApplication
744 * Gets the current inactivity timeout for the application.
746 * This is the amount of time (in milliseconds) after the last call to
747 * g_application_release() before the application stops running.
749 * Returns: the timeout, in milliseconds
754 g_application_get_inactivity_timeout (GApplication *application)
756 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
758 return application->priv->inactivity_timeout;
762 * g_application_set_inactivity_timeout:
763 * @application: a #GApplication
764 * @inactivity_timeout: the timeout, in milliseconds
766 * Sets the current inactivity timeout for the application.
768 * This is the amount of time (in milliseconds) after the last call to
769 * g_application_release() before the application stops running.
771 * This call has no side effects of its own. The value set here is only
772 * used for next time g_application_release() drops the use count to
773 * zero. Any timeouts currently in progress are not impacted.
775 * Returns: the timeout, in milliseconds
780 g_application_set_inactivity_timeout (GApplication *application,
781 guint inactivity_timeout)
783 g_return_if_fail (G_IS_APPLICATION (application));
785 if (application->priv->inactivity_timeout != inactivity_timeout)
787 application->priv->inactivity_timeout = inactivity_timeout;
789 g_object_notify (G_OBJECT (application), "inactivity-timeout");
792 /* Read-only property getters (is registered, is remote) {{{1 */
794 * g_application_get_is_registered:
795 * @application: a #GApplication
796 * @returns: %TRUE if @application is registered
798 * Checks if @application is registered.
800 * An application is registered if g_application_register() has been
801 * successfully called.
806 g_application_get_is_registered (GApplication *application)
808 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
810 return application->priv->is_registered;
814 * g_application_get_is_remote:
815 * @application: a #GApplication
816 * @returns: %TRUE if @application is remote
818 * Checks if @application is remote.
820 * If @application is remote then it means that another instance of
821 * application already exists (the 'primary' instance). Calls to
822 * perform actions on @application will result in the actions being
823 * performed by the primary instance.
825 * The value of this property can not be accessed before
826 * g_application_register() has been called. See
827 * g_application_get_is_registered().
832 g_application_get_is_remote (GApplication *application)
834 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
835 g_return_val_if_fail (application->priv->is_registered, FALSE);
837 return application->priv->is_remote;
842 * g_application_register:
843 * @application: a #GApplication
844 * @cancellable: a #GCancellable, or %NULL
845 * @error: a pointer to a NULL #GError, or %NULL
846 * @returns: %TRUE if registration succeeded
848 * Attempts registration of the application.
850 * This is the point at which the application discovers if it is the
851 * primary instance or merely acting as a remote for an already-existing
854 * If the application has already been registered then %TRUE is
855 * returned with no work performed.
857 * The startup() virtual function is invoked if registration succeeds
858 * and @application is the primary instance.
860 * In the event of an error (such as @cancellable being cancelled, or a
861 * failure to connect to the session bus), %FALSE is returned and @error
862 * is set appropriately.
864 * Note: the return value of this function is not an indicator that this
865 * instance is or is not the primary instance of the application. See
866 * g_application_get_is_remote() for that.
871 g_application_register (GApplication *application,
872 GCancellable *cancellable,
875 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
877 if (!application->priv->is_registered)
882 /* don't try to be the primary instance if
883 * G_APPLICATION_IS_LAUNCHER was specified.
885 try = !(application->priv->flags & G_APPLICATION_IS_LAUNCHER);
887 application->priv->impl =
888 g_application_impl_register (application, application->priv->id,
889 application->priv->flags,
890 &is_remote, cancellable, error);
892 if (application->priv->impl == NULL)
895 application->priv->is_remote = is_remote;
896 application->priv->is_registered = TRUE;
898 g_object_notify (G_OBJECT (application), "is-registered");
901 g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
907 /* Hold/release {{{1 */
909 * g_application_hold:
910 * @application: a #GApplication
912 * Increases the use count of @application.
914 * Use this function to indicate that the application has a reason to
915 * continue to run. For example, g_application_hold() is called by Gtk
916 * when a toplevel window is on the screen.
918 * To cancel the hold, call g_application_release().
921 g_application_hold (GApplication *application)
923 if (application->priv->inactivity_timeout_id)
925 g_source_remove (application->priv->inactivity_timeout_id);
926 application->priv->inactivity_timeout_id = 0;
929 application->priv->use_count++;
933 inactivity_timeout_expired (gpointer data)
935 GApplication *application = G_APPLICATION (data);
937 G_APPLICATION_GET_CLASS (application)
938 ->quit_mainloop (application);
945 * g_application_release:
946 * @application: a #GApplication
948 * Decrease the use count of @application.
950 * When the use count reaches zero, the application will stop running.
952 * Never call this function except to cancel the effect of a previous
953 * call to g_application_hold().
956 g_application_release (GApplication *application)
958 application->priv->use_count--;
960 if (application->priv->use_count == 0)
962 if (application->priv->inactivity_timeout)
963 application->priv->inactivity_timeout_id =
964 g_timeout_add (application->priv->inactivity_timeout,
965 inactivity_timeout_expired, application);
968 G_APPLICATION_GET_CLASS (application)
969 ->quit_mainloop (application);
973 /* Activate, Open {{{1 */
975 * g_application_activate:
976 * @application: a #GApplication
978 * Activates the application.
980 * In essence, this results in the activate() virtual function being
981 * invoked in the primary instance.
983 * The application must be registered before calling this function.
988 g_application_activate (GApplication *application)
990 g_return_if_fail (G_IS_APPLICATION (application));
991 g_return_if_fail (application->priv->is_registered);
993 if (application->priv->is_remote)
994 g_application_impl_activate (application->priv->impl,
995 get_platform_data (application));
998 g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
1002 * g_application_open:
1003 * @application: a #GApplication
1004 * @files: an array of #GFile<!-- -->s to open
1005 * @n_files: the length of the @files array
1006 * @hint: a hint (or ""), but never %NULL
1008 * Opens the given files.
1010 * In essence, this results in the open() virtual function being invoked
1011 * in the primary instance.
1013 * @n_files must be greater than zero.
1015 * @hint is simply passed through to the open() virtual function. It is
1016 * intended to be used by applications that have multiple modes for
1017 * opening files (eg: "view" vs "edit", etc). Unless you have a need
1018 * for this functionality, you should use "".
1020 * The application must be registered before calling this function and
1021 * it must have the %G_APPLICATION_HANDLES_OPEN flag set. The open()
1022 * virtual function should also be implemented in order for anything
1023 * meaningful to happen.
1028 g_application_open (GApplication *application,
1033 g_return_if_fail (G_IS_APPLICATION (application));
1034 g_return_if_fail (application->priv->flags &
1035 G_APPLICATION_HANDLES_OPEN);
1036 g_return_if_fail (application->priv->is_registered);
1038 if (application->priv->is_remote)
1039 g_application_impl_open (application->priv->impl,
1040 files, n_files, hint,
1041 get_platform_data (application));
1044 g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
1045 0, files, n_files, hint);
1050 * g_application_run:
1051 * @application: a #GApplication
1052 * @argc: the argc from main()
1053 * @argv: the argv from main()
1054 * @returns: the exit status
1056 * Runs the application.
1058 * This function is intended to be run from main() and its return value
1059 * is intended to be returned by main().
1061 * First, the handle_command_line() virtual function is invoked. This
1062 * function always runs on the local instance. If that function returns
1063 * %FALSE then the application is registered and the #GApplication::command-line
1064 * signal is emitted in the primary instance (which may or may not be
1067 * If the application has the %G_APPLICATION_HANDLES_COMMAND_LINE
1068 * flag set then the default implementation of handle_command_line()
1069 * always returns %FALSE immediately, resulting in the commandline
1070 * always being handled in the primary instance.
1072 * Otherwise, the default implementation of handle_command_line() tries
1073 * to do a couple of things that are probably reasonable for most
1074 * applications. First, g_application_register() is called to attempt
1075 * to register the application. If that works, then the command line
1076 * arguments are inspected. If no commandline arguments are given, then
1077 * g_application_activate() is called. If commandline arguments are
1078 * given and the %G_APPLICATION_HANDLES_OPEN flag is set then they
1079 * are assumed to be filenames and g_application_open() is called.
1081 * If you are interested in doing more complicated local handling of the
1082 * commandline then you should override handle_command_line().
1084 * If, after the above is done, the use count of the application is zero
1085 * then the exit status is returned immediately. If the use count is
1086 * non-zero then the mainloop is run until the use count falls to zero,
1087 * at which point 0 is returned.
1089 * If the %G_APPLICATION_IS_SERVICE flag is set, then the exiting at
1090 * use count of zero is delayed for a while (ie: the instance stays
1091 * around to provide its <emphasis>service</emphasis> to others).
1096 g_application_run (GApplication *application,
1104 g_return_val_if_fail (G_IS_APPLICATION (application), 1);
1105 g_return_val_if_fail (argc == 0 || argv != NULL, 1);
1107 arguments = g_new (gchar *, argc + 1);
1108 for (i = 0; i < argc; i++)
1109 arguments[i] = g_strdup (argv[i]);
1110 arguments[i] = NULL;
1112 if (g_get_prgname () == NULL && argc > 0)
1116 prgname = g_path_get_basename (argv[0]);
1117 g_set_prgname (prgname);
1121 if (!G_APPLICATION_GET_CLASS (application)
1122 ->local_command_line (application, &arguments, &status))
1124 GError *error = NULL;
1126 if (!g_application_register (application, NULL, &error))
1128 g_printerr ("%s", error->message);
1129 g_error_free (error);
1133 if (application->priv->is_remote)
1135 GVariant *platform_data;
1137 platform_data = get_platform_data (application);
1138 status = g_application_impl_command_line (application->priv->impl,
1139 arguments, platform_data);
1143 GApplicationCommandLine *cmdline;
1146 v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
1147 cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
1148 "arguments", v, NULL);
1149 g_signal_emit (application,
1150 g_application_signals[SIGNAL_COMMAND_LINE],
1151 0, cmdline, &status);
1152 g_object_unref (cmdline);
1156 g_strfreev (arguments);
1158 if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
1159 !application->priv->use_count &&
1160 !application->priv->inactivity_timeout_id)
1162 application->priv->inactivity_timeout_id =
1163 g_timeout_add (10000, inactivity_timeout_expired, application);
1166 if (application->priv->use_count ||
1167 application->priv->inactivity_timeout_id)
1169 G_APPLICATION_GET_CLASS (application)
1170 ->run_mainloop (application);
1174 if (application->priv->impl)
1175 g_application_impl_flush (application->priv->impl);
1181 g_application_has_action (GActionGroup *action_group,
1182 const gchar *action_name)
1184 GApplication *application = G_APPLICATION (action_group);
1186 g_return_val_if_fail (application->priv->is_registered, FALSE);
1188 return application->priv->actions &&
1189 g_action_group_has_action (application->priv->actions, action_name);
1193 g_application_list_actions (GActionGroup *action_group)
1195 GApplication *application = G_APPLICATION (action_group);
1197 g_return_val_if_fail (application->priv->is_registered, NULL);
1199 if (application->priv->actions != NULL)
1200 return g_action_group_list_actions (application->priv->actions);
1203 /* empty string array */
1204 return g_new0 (gchar *, 1);
1208 g_application_get_action_enabled (GActionGroup *action_group,
1209 const gchar *action_name)
1211 GApplication *application = G_APPLICATION (action_group);
1213 g_return_val_if_fail (application->priv->actions != NULL, FALSE);
1214 g_return_val_if_fail (application->priv->is_registered, FALSE);
1216 return g_action_group_get_action_enabled (application->priv->actions,
1220 static const GVariantType *
1221 g_application_get_action_parameter_type (GActionGroup *action_group,
1222 const gchar *action_name)
1224 GApplication *application = G_APPLICATION (action_group);
1226 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1227 g_return_val_if_fail (application->priv->is_registered, NULL);
1229 return g_action_group_get_action_parameter_type (application->priv->actions,
1233 static const GVariantType *
1234 g_application_get_action_state_type (GActionGroup *action_group,
1235 const gchar *action_name)
1237 GApplication *application = G_APPLICATION (action_group);
1239 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1240 g_return_val_if_fail (application->priv->is_registered, NULL);
1242 return g_action_group_get_action_state_type (application->priv->actions,
1247 g_application_get_action_state_hint (GActionGroup *action_group,
1248 const gchar *action_name)
1250 GApplication *application = G_APPLICATION (action_group);
1252 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1253 g_return_val_if_fail (application->priv->is_registered, NULL);
1255 return g_action_group_get_action_state_hint (application->priv->actions,
1260 g_application_get_action_state (GActionGroup *action_group,
1261 const gchar *action_name)
1263 GApplication *application = G_APPLICATION (action_group);
1265 g_return_val_if_fail (application->priv->actions != NULL, NULL);
1266 g_return_val_if_fail (application->priv->is_registered, NULL);
1268 return g_action_group_get_action_state (application->priv->actions,
1273 g_application_change_action_state (GActionGroup *action_group,
1274 const gchar *action_name,
1277 GApplication *application = G_APPLICATION (action_group);
1279 g_return_if_fail (application->priv->actions != NULL);
1280 g_return_if_fail (application->priv->is_registered);
1282 g_action_group_change_action_state (application->priv->actions,
1283 action_name, value);
1287 g_application_activate_action (GActionGroup *action_group,
1288 const gchar *action_name,
1289 GVariant *parameter)
1291 GApplication *application = G_APPLICATION (action_group);
1293 g_return_if_fail (application->priv->actions != NULL);
1294 g_return_if_fail (application->priv->is_registered);
1296 g_action_group_activate_action (application->priv->actions,
1297 action_name, parameter);
1301 g_application_action_group_iface_init (GActionGroupInterface *iface)
1303 iface->has_action = g_application_has_action;
1304 iface->list_actions = g_application_list_actions;
1306 iface->get_action_enabled = g_application_get_action_enabled;
1307 iface->get_action_parameter_type = g_application_get_action_parameter_type;
1308 iface->get_action_state_type = g_application_get_action_state_type;
1309 iface->get_action_state_hint = g_application_get_action_state_hint;
1310 iface->get_action_state = g_application_get_action_state;
1311 iface->change_action_state = g_application_change_action_state;
1312 iface->activate_action = g_application_activate_action;
1316 /* vim:set foldmethod=marker: */