]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplication.c
application: Plug a leak
[~andy/gtk] / gtk / gtkapplication.c
1 /* GTK - The GIMP Toolkit
2  *
3  * Copyright (C) 2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Colin Walters <walters@verbum.org>
21  */
22
23 /*
24  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
25  * file for a list of people on the GTK+ Team.  See the ChangeLog
26  * files for a list of changes.  These files are distributed with
27  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28  */
29
30 #include "config.h"
31
32 #include <stdlib.h>
33 #include <unistd.h>
34
35 #include "gtkapplication.h"
36 #include "gtkmain.h"
37 #include "gtkmarshalers.h"
38 #include "gtkintl.h"
39 #include "gtkprivate.h"
40
41 #include "gtkalias.h"
42
43 #include <gdk/gdk.h>
44 #ifdef GDK_WINDOWING_X11
45 #include <gdk/x11/gdkx.h>
46 #endif
47
48 /**
49  * SECTION:gtkapplication
50  * @title: GtkApplication
51  * @short_description: Application class
52  *
53  * #GtkApplication is a class that handles many important aspects
54  * of a GTK+ application in a convenient fashion, without enforcing
55  * a one-size-fits-all application model.
56  *
57  * Currently, GtkApplication handles application uniqueness, provides
58  * some basic scriptability by exporting 'actions', implements some
59  * standard actions itself (such as 'Quit') and provides a main window
60  * whose life-cycle is automatically tied to the life-cycle of your
61  * application.
62  *
63  * <example id="gtkapplication"><title>A simple application</title>
64  * <programlisting>
65  * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gtk/tests/gtk-example-application.c">
66  *  <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
67  * </xi:include>
68  * </programlisting>
69  * </example>
70  */
71 enum
72 {
73   PROP_0,
74
75   PROP_WINDOW
76 };
77
78 enum
79 {
80   ACTIVATED,
81   QUIT,
82   ACTION,
83
84   LAST_SIGNAL
85 };
86
87 static guint gtk_application_signals[LAST_SIGNAL] = { 0 };
88
89 struct _GtkApplicationPrivate
90 {
91   GtkActionGroup *main_actions;
92
93   GtkWindow *default_window;
94   GSList *windows;
95 };
96
97 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
98
99 static void
100 process_timestamp_from_platform_data (GVariant *platform_data)
101 {
102   /* TODO - extract timestamp from here, update GDK time */
103 }
104
105 static gboolean
106 gtk_application_default_quit (GtkApplication *application)
107 {
108   gtk_main_quit ();
109   return TRUE;
110 }
111
112 static gboolean
113 gtk_application_default_quit_with_data (GApplication *application,
114                                         GVariant     *platform_data)
115 {
116   gboolean result;
117
118   process_timestamp_from_platform_data (platform_data);
119   
120   g_signal_emit (application, gtk_application_signals[QUIT], 0, &result);
121
122   return result;
123 }
124
125 static void
126 gtk_application_default_run (GApplication *application)
127 {
128   gtk_main ();
129 }
130
131 static void
132 gtk_application_default_prepare_activation (GApplication *application,
133                                             GVariant     *arguments,
134                                             GVariant     *platform_data)
135 {
136   GVariantIter iter;
137   gchar *key;
138   GVariant *value;
139
140   g_variant_iter_init (&iter, platform_data);
141   while (g_variant_iter_next (&iter, "{&sv}", &key, &value))
142     {
143       if (strcmp (key, "startup-notification-id") == 0 &&
144           g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
145         gdk_notify_startup_complete_with_id (g_variant_get_string (value, NULL));
146
147       g_free (key);
148       g_variant_unref (value);
149     }
150   
151   g_signal_emit (G_OBJECT (application), gtk_application_signals[ACTIVATED], 0, arguments);
152 }
153
154 static void
155 gtk_application_default_activated (GtkApplication *application,
156                                    GVariant       *arguments)
157 {
158   GtkApplicationPrivate *priv = application->priv;
159
160   /* TODO: should we raise the last focused window instead ? */
161   if (priv->default_window != NULL)
162     gtk_window_present (priv->default_window);
163 }
164
165 static void
166 gtk_application_default_action (GtkApplication *application,
167                                 const gchar    *action_name)
168 {
169   GtkApplicationPrivate *priv = application->priv;
170   GtkAction *action;
171
172   action = gtk_action_group_get_action (priv->main_actions, action_name);
173   if (action)
174     gtk_action_activate (action);
175 }
176
177 static GtkWindow *
178 gtk_application_default_create_window (GtkApplication *application)
179 {
180   return GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
181 }
182
183 static void
184 gtk_application_default_action_with_data (GApplication *application,
185                                           const gchar  *action_name,
186                                           GVariant     *platform_data)
187 {
188   process_timestamp_from_platform_data (platform_data);
189
190   g_signal_emit (application, gtk_application_signals[ACTION], g_quark_from_string (action_name));
191 }
192
193 static GVariant *
194 gtk_application_format_activation_data (void)
195 {
196   const gchar *startup_id = NULL;
197   GdkDisplay *display = gdk_display_get_default ();
198   GVariantBuilder builder;
199
200   g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
201
202   /* try and get the startup notification id from GDK, the environment
203    * or, if everything else failed, fake one.
204    */
205 #ifdef GDK_WINDOWING_X11
206   startup_id = gdk_x11_display_get_startup_notification_id (display);
207 #endif /* GDK_WINDOWING_X11 */
208
209   if (startup_id)
210     g_variant_builder_add (&builder, "{sv}", "startup-notification-id",
211                            g_variant_new ("s", startup_id));
212   return g_variant_builder_end (&builder);
213 }
214
215 static GVariant *
216 variant_from_argv (int    argc,
217                    char **argv)
218 {
219   GVariantBuilder builder;
220   int i;
221
222   g_variant_builder_init (&builder, G_VARIANT_TYPE ("aay"));
223
224   for (i = 1; i < argc; i++)
225     {
226       guint8 *argv_bytes;
227
228       argv_bytes = (guint8*) argv[i];
229       g_variant_builder_add_value (&builder,
230                                    g_variant_new_byte_array (argv_bytes, -1));
231     }
232   
233   return g_variant_builder_end (&builder);
234 }
235
236 /**
237  * gtk_application_new:
238  * @appid: System-dependent application identifier
239  * @argc: (allow-none) (inout): System argument count
240  * @argv: (allow-none) (inout): System argument vector
241  *
242  * Create a new #GtkApplication, or if one has already been initialized
243  * in this process, return the existing instance. This function will as
244  * a side effect initialize the display system; see gtk_init().
245  *
246  * For the behavior if this application is running in another process,
247  * see g_application_new().
248  *
249  * Returns: (transfer full): A newly-referenced #GtkApplication
250  *
251  * Since: 3.0
252  */
253 GtkApplication*
254 gtk_application_new (const gchar   *appid,
255                      gint          *argc,
256                      gchar       ***argv)
257 {
258   GtkApplication *app;
259   gint argc_for_app;
260   gchar **argv_for_app;
261   GVariant *argv_variant;
262   GError *error = NULL;
263
264   gtk_init (argc, argv);
265
266   if (argc)
267     argc_for_app = *argc;
268   else
269     argc_for_app = 0;
270
271   if (argv)
272     argv_for_app = *argv;
273   else
274     argv_for_app = NULL;
275
276   argv_variant = variant_from_argv (argc_for_app, argv_for_app);
277
278   app = g_initable_new (GTK_TYPE_APPLICATION, 
279                         NULL,
280                         &error,
281                         "application-id", appid, 
282                         "argv", argv_variant, 
283                         NULL);
284   if (app == NULL)
285     {
286       g_error ("%s", error->message);
287       g_clear_error (&error);
288       return NULL;
289     }
290
291   return app;
292 }
293
294 static void
295 on_action_sensitive (GtkAction      *action,
296                      GParamSpec     *pspec,
297                      GtkApplication *app)
298 {
299   g_application_set_action_enabled (G_APPLICATION (app),
300                                     gtk_action_get_name (action),
301                                     gtk_action_get_sensitive (action));
302 }
303
304 /**
305  * gtk_application_set_action_group:
306  * @app: A #GtkApplication
307  * @group: A #GtkActionGroup
308  *
309  * Set @group as this application's global action group.
310  * This will ensure the operating system interface uses
311  * these actions as follows:
312  *
313  * <itemizedlist>
314  *   <listitem>In GNOME 2 this exposes the actions for scripting.</listitem>
315  *   <listitem>In GNOME 3, this function populates the application menu.</listitem>
316  *   <listitem>In Windows prior to version 7, this function does nothing.</listitem>
317  *   <listitem>In Windows 7, this function adds "Tasks" to the Jump List.</listitem>
318  *   <listitem>In Mac OS X, this function extends the Dock menu.</listitem>
319  * </itemizedlist>
320  *
321  * It is an error to call this function more than once.
322  *
323  * Since: 3.0
324  */
325 void
326 gtk_application_set_action_group (GtkApplication *app,
327                                   GtkActionGroup *group)
328 {
329   GList *actions, *iter;
330
331   g_return_if_fail (GTK_IS_APPLICATION (app));
332   g_return_if_fail (GTK_IS_ACTION_GROUP (group));
333   g_return_if_fail (app->priv->main_actions == NULL);
334
335   app->priv->main_actions = g_object_ref (group);
336   actions = gtk_action_group_list_actions (group);
337   for (iter = actions; iter; iter = iter->next)
338     {
339       GtkAction *action = iter->data;
340       g_application_add_action (G_APPLICATION (app),
341                                 gtk_action_get_name (action),
342                                 gtk_action_get_tooltip (action));
343       g_signal_connect (action, "notify::sensitive",
344                         G_CALLBACK (on_action_sensitive), app);
345     }
346   g_list_free (actions);
347 }
348
349 static gboolean
350 gtk_application_on_window_destroy (GtkWidget *window,
351                                    gpointer   user_data)
352 {
353   GtkApplication *app = GTK_APPLICATION (user_data);
354
355   app->priv->windows = g_slist_remove (app->priv->windows, window);
356
357   if (app->priv->windows == NULL)
358     gtk_application_quit (app);
359
360   return FALSE;
361 }
362
363 static gchar *default_title;
364
365 /**
366  * gtk_application_add_window:
367  * @app: a #GtkApplication
368  * @window: a toplevel window to add to @app
369  *
370  * Adds a window to the #GtkApplication.
371  *
372  * If the user closes all of the windows added to @app, the default
373  * behaviour is to call gtk_application_quit().
374  *
375  * If your application uses only a single toplevel window, you can
376  * use gtk_application_get_window(). If you are using a sub-class
377  * of #GtkApplication you should call gtk_application_create_window()
378  * to let the #GtkApplication instance create a #GtkWindow and add
379  * it to the list of toplevels of the application. You should call
380  * this function only to add #GtkWindow<!-- -->s that you created
381  * directly using gtk_window_new().
382  *
383  * Since: 3.0
384  */
385 void
386 gtk_application_add_window (GtkApplication *app,
387                             GtkWindow      *window)
388 {
389   GtkApplicationPrivate *priv;
390
391   g_return_if_fail (GTK_IS_APPLICATION (app));
392   g_return_if_fail (GTK_IS_WINDOW (window));
393
394   priv = app->priv;
395
396   if (g_slist_find (priv->windows, window) != NULL)
397     return;
398
399   priv->windows = g_slist_prepend (priv->windows, window);
400
401   if (priv->default_window == NULL)
402     priv->default_window = window;
403
404   if (gtk_window_get_title (window) == NULL && default_title != NULL)
405     gtk_window_set_title (window, default_title);
406
407   g_signal_connect (window, "destroy",
408                     G_CALLBACK (gtk_application_on_window_destroy),
409                     app);
410 }
411
412 /**
413  * gtk_application_get_window:
414  * @app: a #GtkApplication
415  *
416  * A simple #GtkApplication has a "default window". This window should
417  * act as the primary user interaction point with your application.
418  * The window returned by this function is of type #GTK_WINDOW_TYPE_TOPLEVEL
419  * and its properties such as "title" and "icon-name" will be initialized
420  * as appropriate for the platform.
421  *
422  * If the user closes this window, and your application hasn't created
423  * any other windows, the default action will be to call gtk_application_quit().
424  *
425  * If your application has more than one toplevel window (e.g. an
426  * single-document-interface application with multiple open documents),
427  * or if you are constructing your toplevel windows yourself (e.g. using
428  * #GtkBuilder), use gtk_application_create_window() or
429  * gtk_application_add_window() instead.
430  *
431  * Returns: (transfer none): The default #GtkWindow for this application
432  *
433  * Since: 3.0
434  */
435 GtkWindow *
436 gtk_application_get_window (GtkApplication *app)
437 {
438   GtkApplicationPrivate *priv;
439   GtkWindow *window;
440
441   g_return_val_if_fail (GTK_IS_APPLICATION (app), NULL);
442
443   priv = app->priv;
444
445   if (priv->default_window != NULL)
446     return priv->default_window;
447
448   return gtk_application_create_window (app);
449 }
450
451 /**
452  * gtk_application_create_window:
453  * @app: a #GtkApplication
454  *
455  * Creates a new #GtkWindow for the application.
456  *
457  * This function calls the #GtkApplication::create_window() virtual function,
458  * which can be overridden by sub-classes, for instance to use #GtkBuilder to
459  * create the user interface. After creating a new #GtkWindow instance, it will
460  * be added to the list of toplevels associated to the application.
461  *
462  * Return value: (transfer none): the newly created application #GtkWindow
463  *
464  * Since: 3.0
465  */
466 GtkWindow *
467 gtk_application_create_window (GtkApplication *app)
468 {
469   GtkWindow *window;
470
471   g_return_val_if_fail (GTK_IS_APPLICATION (app), NULL);
472
473   window = GTK_APPLICATION_GET_CLASS (app)->create_window (app);
474   gtk_application_add_window (app, window);
475
476   return window;
477 }
478
479 /**
480  * gtk_application_run:
481  * @app: a #GtkApplication
482  *
483  * Runs the main loop; see g_application_run().
484  * The default implementation for #GtkApplication uses gtk_main().
485  *
486  * Since: 3.0
487  */
488 void
489 gtk_application_run (GtkApplication *app)
490 {
491   g_application_run (G_APPLICATION (app));
492 }
493
494 /**
495  * gtk_application_quit:
496  * @app: a #GtkApplication
497  *
498  * Request the application exit.  This function invokes
499  * g_application_quit_with_data(), which normally will
500  * in turn cause @app to emit #GtkApplication::quit.
501  *
502  * To control an application's quit behavior (for example, to ask for
503  * files to be saved), connect to the #GtkApplication::quit signal
504  * handler.
505  *
506  * Since: 3.0
507  */
508 void
509 gtk_application_quit (GtkApplication *app)
510 {
511   GVariantBuilder builder;
512   GVariant *platform_data;
513
514   g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
515   g_variant_builder_add (&builder, "{sv}",
516                          "timestamp",
517                          g_variant_new ("u", gtk_get_current_event_time ()));
518   platform_data = g_variant_builder_end (&builder);
519
520   g_application_quit_with_data (G_APPLICATION (app), platform_data);
521
522   g_variant_unref (platform_data);
523 }
524
525 static void
526 gtk_application_get_property (GObject    *object,
527                               guint       prop_id,
528                               GValue     *value,
529                               GParamSpec *pspec)
530 {
531   GtkApplication *app = GTK_APPLICATION (object);
532
533   switch (prop_id)
534     {
535       case PROP_WINDOW:
536         g_value_set_object (value, gtk_application_get_window (app));
537         break;
538       default:
539         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
540     }
541 }
542
543 static void
544 gtk_application_set_property (GObject      *object,
545                               guint         prop_id,
546                               const GValue *value,
547                               GParamSpec   *pspec)
548 {
549   GtkApplication *app = GTK_APPLICATION (object);
550
551   g_assert (app != NULL);
552
553   switch (prop_id)
554     {
555       default:
556         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
557     }
558 }
559
560 static void
561 setup_default_window_decorations (void)
562 {
563   const gchar *pid;
564   const gchar *filename;
565   GKeyFile *keyfile;
566   gchar *title;
567   gchar *icon_name;
568
569   pid = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE_PID");
570   filename = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
571
572   keyfile = g_key_file_new ();
573
574   if (pid != NULL && filename != NULL && atoi (pid) == getpid () &&
575       g_key_file_load_from_file (keyfile, filename, 0, NULL))
576     {
577       title = g_key_file_get_locale_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
578       icon_name = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL);
579
580       if (default_title == NULL)
581         default_title = title;
582
583       if (gtk_window_get_default_icon_name () == NULL)
584         gtk_window_set_default_icon_name (icon_name);
585
586       g_free (icon_name);
587     }
588
589   g_key_file_free (keyfile);
590 }
591
592 static void
593 gtk_application_init (GtkApplication *application)
594 {
595   application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application, GTK_TYPE_APPLICATION, GtkApplicationPrivate);
596
597   g_object_set (application, "platform-data", gtk_application_format_activation_data (), NULL);
598
599   setup_default_window_decorations ();
600 }
601
602
603 static GObject*
604 gtk_application_constructor (GType                  type,
605                              guint                  n_construct_properties,
606                              GObjectConstructParam *construct_params)
607 {
608   GObject *object;
609
610   /* Last ditch effort here */
611   gtk_init (0, NULL);
612
613   object = (* G_OBJECT_CLASS (gtk_application_parent_class)->constructor) (type,
614                                                                            n_construct_properties,
615                                                                            construct_params);
616
617   return object;
618 }
619
620 static void
621 gtk_application_class_init (GtkApplicationClass *klass)
622 {
623   GObjectClass *gobject_class;
624   GApplicationClass *application_class;
625
626   gobject_class = G_OBJECT_CLASS (klass);
627   application_class = G_APPLICATION_CLASS (klass);
628
629   gobject_class->constructor = gtk_application_constructor;
630   gobject_class->get_property = gtk_application_get_property;
631   gobject_class->set_property = gtk_application_set_property;
632
633   application_class->run = gtk_application_default_run;
634   application_class->quit_with_data = gtk_application_default_quit_with_data;
635   application_class->action_with_data = gtk_application_default_action_with_data;
636   application_class->prepare_activation = gtk_application_default_prepare_activation;
637
638   klass->quit = gtk_application_default_quit;
639   klass->action = gtk_application_default_action;
640   klass->create_window = gtk_application_default_create_window;
641
642   klass->activated = gtk_application_default_activated;
643
644   /**
645    * GtkApplication::activated:
646    * @arguments: A #GVariant with the signature "aay"
647    *
648    * This signal is emitted when a non-primary process for a given
649    * application is invoked while your application is running; for
650    * example, when a file browser launches your program to open a
651    * file.  The raw operating system arguments are passed in the
652    * variant @arguments.
653    */
654
655   gtk_application_signals[ACTIVATED] =
656     g_signal_new (g_intern_static_string ("activated"),
657                   G_OBJECT_CLASS_TYPE (klass),
658                   G_SIGNAL_RUN_LAST,
659                   G_STRUCT_OFFSET (GtkApplicationClass, activated),
660                   NULL, NULL,
661                   g_cclosure_marshal_VOID__VARIANT,
662                   G_TYPE_NONE, 1,
663                   G_TYPE_VARIANT);
664
665   /**
666    * GtkApplication::quit:
667    * @application: the object on which the signal is emitted
668    *
669    * This signal is emitted when a quit is initiated.  See also
670    * the #GApplication::quit-with-data signal which may in
671    * turn trigger this signal.
672    *
673    * The default handler for this signal exits the mainloop of the
674    * application.
675    *
676    * Returns: %TRUE if the signal has been handled, %FALSE to continue
677    *   signal emission
678    */
679   gtk_application_signals[QUIT] =
680     g_signal_new (g_intern_static_string ("quit"),
681                   G_OBJECT_CLASS_TYPE (klass),
682                   G_SIGNAL_RUN_LAST,
683                   G_STRUCT_OFFSET (GtkApplicationClass, quit),
684                   g_signal_accumulator_true_handled, NULL,
685                   _gtk_marshal_BOOLEAN__VOID,
686                   G_TYPE_BOOLEAN, 0);
687
688   /**
689    * GtkApplication::action:
690    * @application: the object on which the signal is emitted
691    * @name: The name of the activated action
692    *
693    * This signal is emitted when an action is activated. The action name
694    * is passed as the first argument, but also as signal detail, so it
695    * is possible to connect to this signal for individual actions.
696    *
697    * See also the #GApplication::action-with-data signal which may in
698    * turn trigger this signal.
699    *
700    * The signal is never emitted for disabled actions.
701    */
702   gtk_application_signals[ACTION] =
703     g_signal_new (g_intern_static_string ("action"),
704                   G_OBJECT_CLASS_TYPE (klass),
705                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED,
706                   G_STRUCT_OFFSET (GtkApplicationClass, action),
707                   NULL, NULL,
708                   g_cclosure_marshal_VOID__STRING,
709                   G_TYPE_NONE, 1,
710                   G_TYPE_STRING);
711                  
712   g_type_class_add_private (gobject_class, sizeof (GtkApplicationPrivate));
713 }
714
715 #define __GTK_APPLICATION_C__
716 #include "gtkaliasdef.c"