gtkapplication.c 55.8 KB
Newer Older
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1
2
/*
 * Copyright © 2010 Codethink Limited
Allison Karlitskaya's avatar
Allison Karlitskaya committed
3
 * Copyright © 2013 Canonical Limited
Matthias Clasen's avatar
Matthias Clasen committed
4
5
6
7
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
Allison Karlitskaya's avatar
Allison Karlitskaya committed
8
 * version 2 of the licence, or (at your option) any later version.
Matthias Clasen's avatar
Matthias Clasen committed
9
10
11
12
13
14
15
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Matthias Clasen's avatar
Matthias Clasen committed
17
 *
Allison Karlitskaya's avatar
Allison Karlitskaya committed
18
 * Author: Ryan Lortie <desrt@desrt.ca>
Matthias Clasen's avatar
Matthias Clasen committed
19
20
21
22
 */

#include "config.h"

23
#include "gtkapplication.h"
Christian Hergert's avatar
Christian Hergert committed
24
25
26
27
28
#include "gdkprivate.h"

#ifdef G_OS_UNIX
#include <gio/gunixfdlist.h>
#endif
29

Matthias Clasen's avatar
Matthias Clasen committed
30
#include <stdlib.h>
31
32

#ifdef HAVE_UNISTD_H
Matthias Clasen's avatar
Matthias Clasen committed
33
#include <unistd.h>
34
35
#endif

36
#include "gdk/gdk-private.h"
37

38
#include "gtkapplicationprivate.h"
39
#include "gtkclipboardprivate.h"
40
#include "gtkmarshalers.h"
Matthias Clasen's avatar
Matthias Clasen committed
41
#include "gtkmain.h"
42
#include "gtkrecentmanager.h"
43
#include "gtkaccelmapprivate.h"
44
#include "gtkicontheme.h"
Allison Karlitskaya's avatar
Allison Karlitskaya committed
45
#include "gtkbuilder.h"
46
#include "gtkshortcutswindow.h"
47
#include "gtkintl.h"
48

Allison Karlitskaya's avatar
Allison Karlitskaya committed
49
50
51
/* NB: please do not add backend-specific GDK headers here.  This should
 * be abstracted via GtkApplicationImpl.
 */
Matthias Clasen's avatar
Matthias Clasen committed
52
53
54
55
56
57
58
59
60
61

/**
 * SECTION:gtkapplication
 * @title: GtkApplication
 * @short_description: Application class
 *
 * #GtkApplication is a class that handles many important aspects
 * of a GTK+ application in a convenient fashion, without enforcing
 * a one-size-fits-all application model.
 *
Matthias Clasen's avatar
Matthias Clasen committed
62
 * Currently, GtkApplication handles GTK+ initialization, application
Matthias Clasen's avatar
Matthias Clasen committed
63
64
65
66
 * uniqueness, session management, provides some basic scriptability and
 * desktop shell integration by exporting actions and menus and manages a
 * list of toplevel windows whose life-cycle is automatically tied to the
 * life-cycle of your application.
67
68
69
 *
 * While GtkApplication works fine with plain #GtkWindows, it is recommended
 * to use it together with #GtkApplicationWindow.
Matthias Clasen's avatar
Matthias Clasen committed
70
 *
71
72
73
74
75
76
 * When GDK threads are enabled, GtkApplication will acquire the GDK
 * lock when invoking actions that arrive from other processes.  The GDK
 * lock is not touched for local action invocations.  In order to have
 * actions invoked in a predictable context it is therefore recommended
 * that the GDK lock be held while invoking actions locally with
 * g_action_group_activate_action().  The same applies to actions
77
 * associated with #GtkApplicationWindow and to the “activate” and
78
 * “open” #GApplication methods.
79
 *
80
81
 * ## Automatic resources ## {#automatic-resources}
 *
82
 * #GtkApplication will automatically load menus from the #GtkBuilder
83
 * resource located at "gtk/menus.ui", relative to the application's
84
85
86
87
88
89
90
 * resource base path (see g_application_set_resource_base_path()).  The
 * menu with the ID "app-menu" is taken as the application's app menu
 * and the menu with the ID "menubar" is taken as the application's
 * menubar.  Additional menus (most interesting submenus) can be named
 * and accessed via gtk_application_get_menu_by_id() which allows for
 * dynamic population of a part of the menu structure.
 *
91
 * If the resources "gtk/menus-appmenu.ui" or "gtk/menus-traditional.ui" are
92
93
94
95
96
 * present then these files will be used in preference, depending on the value
 * of gtk_application_prefers_app_menu(). If the resource "gtk/menus-common.ui"
 * is present it will be loaded as well. This is useful for storing items that
 * are referenced from both "gtk/menus-appmenu.ui" and
 * "gtk/menus-traditional.ui".
97
98
99
 *
 * It is also possible to provide the menus manually using
 * gtk_application_set_app_menu() and gtk_application_set_menubar().
Matthias Clasen's avatar
Matthias Clasen committed
100
 *
101
102
103
104
105
106
 * #GtkApplication will also automatically setup an icon search path for
 * the default icon theme by appending "icons" to the resource base
 * path.  This allows your application to easily store its icons as
 * resources.  See gtk_icon_theme_add_resource_path() for more
 * information.
 *
107
108
 * If there is a resource located at "gtk/help-overlay.ui" which
 * defines a #GtkShortcutsWindow with ID "help_overlay" then GtkApplication
109
110
 * associates an instance of this shortcuts window with each
 * #GtkApplicationWindow and sets up keyboard accelerators (Control-F1
111
112
 * and Control-?) to open it. To create a menu item that displays the
 * shortcuts window, associate the item with the action win.show-help-overlay.
113
 *
114
115
 * ## A simple application ## {#gtkapplication}
 *
116
 * [A simple example](https://git.gnome.org/browse/gtk+/tree/examples/bp/bloatpad.c)
Matthias Clasen's avatar
Matthias Clasen committed
117
118
 *
 * GtkApplication optionally registers with a session manager
119
 * of the users session (if you set the #GtkApplication:register-session
Matthias Clasen's avatar
Matthias Clasen committed
120
121
122
123
124
125
126
127
128
129
 * property) and offers various functionality related to the session
 * life-cycle.
 *
 * An application can block various ways to end the session with
 * the gtk_application_inhibit() function. Typical use cases for
 * this kind of inhibiting are long-running, uninterruptible operations,
 * such as burning a CD or performing a disk backup. The session
 * manager may not honor the inhibitor, but it can be expected to
 * inform the user about the negative consequences of ending the
 * session while inhibitors are present.
130
131
 *
 * ## See Also ## {#seealso}
132
133
 * [HowDoI: Using GtkApplication](https://wiki.gnome.org/HowDoI/GtkApplication),
 * [Getting Started with GTK+: Basics](https://developer.gnome.org/gtk3/stable/gtk-getting-started.html#id-1.2.3.3)
Matthias Clasen's avatar
Matthias Clasen committed
134
135
 */

136
137
138
enum {
  WINDOW_ADDED,
  WINDOW_REMOVED,
139
  QUERY_END,
140
141
142
143
144
  LAST_SIGNAL
};

static guint gtk_application_signals[LAST_SIGNAL];

145
146
enum {
  PROP_ZERO,
147
  PROP_REGISTER_SESSION,
148
  PROP_SCREENSAVER_ACTIVE,
149
  PROP_APP_MENU,
150
  PROP_MENUBAR,
151
152
  PROP_ACTIVE_WINDOW,
  NUM_PROPERTIES
153
154
};

155
156
static GParamSpec *gtk_application_props[NUM_PROPERTIES];

157
struct _GtkApplicationPrivate
Matthias Clasen's avatar
Matthias Clasen committed
158
{
Allison Karlitskaya's avatar
Allison Karlitskaya committed
159
  GtkApplicationImpl *impl;
160
  GtkApplicationAccels *accels;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
161

162
  GList *windows;
163

164
165
  GMenuModel      *app_menu;
  GMenuModel      *menubar;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
166
  guint            last_window_id;
167

168
  gboolean         register_session;
169
  gboolean         screensaver_active;
170
  GtkActionMuxer  *muxer;
171
  GtkBuilder      *menus_builder;
172
  gchar           *help_overlay_path;
Christian Hergert's avatar
Christian Hergert committed
173
  guint            profiler_id;
174
};
Matthias Clasen's avatar
Matthias Clasen committed
175

176
177
G_DEFINE_TYPE_WITH_PRIVATE (GtkApplication, gtk_application, G_TYPE_APPLICATION)

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
static gboolean
gtk_application_focus_in_event_cb (GtkWindow      *window,
                                   GdkEventFocus  *event,
                                   GtkApplication *application)
{
  GtkApplicationPrivate *priv = application->priv;
  GList *link;

  /* Keep the window list sorted by most-recently-focused. */
  link = g_list_find (priv->windows, window);
  if (link != NULL && link != priv->windows)
    {
      priv->windows = g_list_remove_link (priv->windows, link);
      priv->windows = g_list_concat (link, priv->windows);
    }

194
195
196
  if (application->priv->impl)
    gtk_application_impl_active_window_changed (application->priv->impl, window);

197
  g_object_notify_by_pspec (G_OBJECT (application), gtk_application_props[PROP_ACTIVE_WINDOW]);
198

swilmet's avatar
swilmet committed
199
  return GDK_EVENT_PROPAGATE;
200
201
}

202
203
204
205
206
207
208
209
210
211
static void
gtk_application_load_resources (GtkApplication *application)
{
  const gchar *base_path;

  base_path = g_application_get_resource_base_path (G_APPLICATION (application));

  if (base_path == NULL)
    return;

212
213
214
215
216
217
  /* Expand the icon search path */
  {
    GtkIconTheme *default_theme;
    gchar *iconspath;

    default_theme = gtk_icon_theme_get_default ();
218
    iconspath = g_strconcat (base_path, "/icons/", NULL);
219
220
221
222
    gtk_icon_theme_add_resource_path (default_theme, iconspath);
    g_free (iconspath);
  }

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
  /* Load the menus */
  {
    gchar *menuspath;

    /* If the user has given a specific file for the variant of menu
     * that we are looking for, use it with preference.
     */
    if (gtk_application_prefers_app_menu (application))
      menuspath = g_strconcat (base_path, "/gtk/menus-appmenu.ui", NULL);
    else
      menuspath = g_strconcat (base_path, "/gtk/menus-traditional.ui", NULL);

    if (g_resources_get_info (menuspath, G_RESOURCE_LOOKUP_FLAGS_NONE, NULL, NULL, NULL))
      application->priv->menus_builder = gtk_builder_new_from_resource (menuspath);
    g_free (menuspath);

    /* If we didn't get the specific file, fall back. */
    if (application->priv->menus_builder == NULL)
      {
        menuspath = g_strconcat (base_path, "/gtk/menus.ui", NULL);
        if (g_resources_get_info (menuspath, G_RESOURCE_LOOKUP_FLAGS_NONE, NULL, NULL, NULL))
          application->priv->menus_builder = gtk_builder_new_from_resource (menuspath);
        g_free (menuspath);
      }

248
249
250
251
252
253
254
255
256
257
258
259
260
261
    /* Always load from -common as well, if we have it */
    menuspath = g_strconcat (base_path, "/gtk/menus-common.ui", NULL);
    if (g_resources_get_info (menuspath, G_RESOURCE_LOOKUP_FLAGS_NONE, NULL, NULL, NULL))
      {
        GError *error = NULL;

        if (application->priv->menus_builder == NULL)
          application->priv->menus_builder = gtk_builder_new ();

        if (!gtk_builder_add_from_resource (application->priv->menus_builder, menuspath, &error))
          g_error ("failed to load menus-common.ui: %s", error->message);
      }
    g_free (menuspath);

262
263
264
265
266
267
268
269
270
271
272
273
    if (application->priv->menus_builder)
      {
        GObject *menu;

        menu = gtk_builder_get_object (application->priv->menus_builder, "app-menu");
        if (menu != NULL && G_IS_MENU_MODEL (menu))
          gtk_application_set_app_menu (application, G_MENU_MODEL (menu));
        menu = gtk_builder_get_object (application->priv->menus_builder, "menubar");
        if (menu != NULL && G_IS_MENU_MODEL (menu))
          gtk_application_set_menubar (application, G_MENU_MODEL (menu));
      }
  }
274
275
276
277
278
279
280

  /* Help overlay */
  {
    gchar *path;

    path = g_strconcat (base_path, "/gtk/help-overlay.ui", NULL);
    if (g_resources_get_info (path, G_RESOURCE_LOOKUP_FLAGS_NONE, NULL, NULL, NULL))
swilmet's avatar
swilmet committed
281
282
      {
        const gchar * const accels[] = { "<Primary>F1", "<Primary>question", NULL };
283

swilmet's avatar
swilmet committed
284
285
286
287
288
289
290
        application->priv->help_overlay_path = path;
        gtk_application_set_accels_for_action (application, "win.show-help-overlay", accels);
      }
    else
      {
        g_free (path);
      }
291
  }
292
293
294
}


Matthias Clasen's avatar
Matthias Clasen committed
295
static void
296
gtk_application_startup (GApplication *g_application)
297
{
298
299
  GtkApplication *application = GTK_APPLICATION (g_application);

300
  G_APPLICATION_CLASS (gtk_application_parent_class)->startup (g_application);
301
302

  gtk_action_muxer_insert (application->priv->muxer, "app", G_ACTION_GROUP (application));
303

swilmet's avatar
swilmet committed
304
  gtk_init (NULL, NULL);
305

Allison Karlitskaya's avatar
Allison Karlitskaya committed
306
307
  application->priv->impl = gtk_application_impl_new (application, gdk_display_get_default ());
  gtk_application_impl_startup (application->priv->impl, application->priv->register_session);
308
309

  gtk_application_load_resources (application);
310
311
}

312
static void
Allison Karlitskaya's avatar
Allison Karlitskaya committed
313
gtk_application_shutdown (GApplication *g_application)
314
{
Allison Karlitskaya's avatar
Allison Karlitskaya committed
315
  GtkApplication *application = GTK_APPLICATION (g_application);
316

317
318
319
  if (application->priv->impl == NULL)
    return;

Allison Karlitskaya's avatar
Allison Karlitskaya committed
320
321
  gtk_application_impl_shutdown (application->priv->impl);
  g_clear_object (&application->priv->impl);
322

323
324
  gtk_action_muxer_remove (application->priv->muxer, "app");

325
326
  /* Keep this section in sync with gtk_main() */

327
328
329
330
331
332
  /* Try storing all clipboard data we have */
  _gtk_clipboard_store_all ();

  /* Synchronize the recent manager singleton */
  _gtk_recent_manager_sync ();

333
  G_APPLICATION_CLASS (gtk_application_parent_class)->shutdown (g_application);
334
335
}

336
337
338
339
340
341
342
343
344
345
static gboolean
gtk_application_local_command_line (GApplication   *application,
                                    gchar        ***arguments,
                                    gint           *exit_status)
{
  g_application_add_option_group (application, gtk_get_option_group (FALSE));

  return G_APPLICATION_CLASS (gtk_application_parent_class)->local_command_line (application, arguments, exit_status);
}

Matthias Clasen's avatar
Matthias Clasen committed
346
static void
Allison Karlitskaya's avatar
Allison Karlitskaya committed
347
348
gtk_application_add_platform_data (GApplication    *application,
                                   GVariantBuilder *builder)
Matthias Clasen's avatar
Matthias Clasen committed
349
{
Allison Karlitskaya's avatar
Allison Karlitskaya committed
350
351
352
353
354
355
356
  /* This is slightly evil.
   *
   * We don't have an impl here because we're remote so we can't figure
   * out what to do on a per-display-server basis.
   *
   * So we do all the things... which currently is just one thing.
   */
357
358
359
  const gchar *desktop_startup_id =
    GDK_PRIVATE_CALL (gdk_get_desktop_startup_id) ();
  if (desktop_startup_id)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
360
    g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
361
                           g_variant_new_string (desktop_startup_id));
Matthias Clasen's avatar
Matthias Clasen committed
362
363
364
}

static void
Allison Karlitskaya's avatar
Allison Karlitskaya committed
365
gtk_application_before_emit (GApplication *g_application,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
366
                             GVariant     *platform_data)
Matthias Clasen's avatar
Matthias Clasen committed
367
{
Allison Karlitskaya's avatar
Allison Karlitskaya committed
368
  GtkApplication *application = GTK_APPLICATION (g_application);
Matthias Clasen's avatar
Matthias Clasen committed
369

370
371
  gdk_threads_enter ();

Allison Karlitskaya's avatar
Allison Karlitskaya committed
372
  gtk_application_impl_before_emit (application->priv->impl, platform_data);
Matthias Clasen's avatar
Matthias Clasen committed
373
374
375
}

static void
Allison Karlitskaya's avatar
Allison Karlitskaya committed
376
377
gtk_application_after_emit (GApplication *application,
                            GVariant     *platform_data)
Matthias Clasen's avatar
Matthias Clasen committed
378
{
379
380
381
382
383
384
385
386
387
388
389
390
  const char *startup_notification_id = NULL;

  g_variant_lookup (platform_data, "desktop-startup-id", "&s", &startup_notification_id);
  if (startup_notification_id)
    {
      GdkDisplay *display;

      display = gdk_display_get_default ();
      if (display)
        gdk_display_notify_startup_complete (display, startup_notification_id);
    }

391
  gdk_threads_leave ();
Matthias Clasen's avatar
Matthias Clasen committed
392
393
394
395
396
}

static void
gtk_application_init (GtkApplication *application)
{
397
  application->priv = gtk_application_get_instance_private (application);
398

399
400
  application->priv->muxer = gtk_action_muxer_new ();

401
  application->priv->accels = gtk_application_accels_new ();
402

403
404
  /* getenv now at the latest */
  GDK_PRIVATE_CALL (gdk_get_desktop_startup_id) ();
Matthias Clasen's avatar
Matthias Clasen committed
405
406
}

407
408
409
410
411
412
static void
gtk_application_window_added (GtkApplication *application,
                              GtkWindow      *window)
{
  GtkApplicationPrivate *priv = application->priv;

Allison Karlitskaya's avatar
Allison Karlitskaya committed
413
  if (GTK_IS_APPLICATION_WINDOW (window))
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
    {
      gtk_application_window_set_id (GTK_APPLICATION_WINDOW (window), ++priv->last_window_id);
      if (priv->help_overlay_path)
        {
          GtkBuilder *builder;
          GtkWidget *help_overlay;

          builder = gtk_builder_new_from_resource (priv->help_overlay_path);
          help_overlay = GTK_WIDGET (gtk_builder_get_object (builder, "help_overlay"));
          if (GTK_IS_SHORTCUTS_WINDOW (help_overlay))
            gtk_application_window_set_help_overlay (GTK_APPLICATION_WINDOW (window),
                                                     GTK_SHORTCUTS_WINDOW (help_overlay));
          g_object_unref (builder);
        }
    }
Allison Karlitskaya's avatar
Allison Karlitskaya committed
429

430
431
432
433
434
435
436
  priv->windows = g_list_prepend (priv->windows, window);
  gtk_window_set_application (window, application);
  g_application_hold (G_APPLICATION (application));

  g_signal_connect (window, "focus-in-event",
                    G_CALLBACK (gtk_application_focus_in_event_cb),
                    application);
437

438
439
440
  gtk_application_impl_window_added (priv->impl, window);

  gtk_application_impl_active_window_changed (priv->impl, window);
441

442
  g_object_notify_by_pspec (G_OBJECT (application), gtk_application_props[PROP_ACTIVE_WINDOW]);
443
444
445
446
447
448
449
}

static void
gtk_application_window_removed (GtkApplication *application,
                                GtkWindow      *window)
{
  GtkApplicationPrivate *priv = application->priv;
450
451
452
  gpointer old_active;

  old_active = priv->windows;
453

454
455
  if (priv->impl)
    gtk_application_impl_window_removed (priv->impl, window);
456

457
458
459
460
461
462
  g_signal_handlers_disconnect_by_func (window,
                                        gtk_application_focus_in_event_cb,
                                        application);

  g_application_release (G_APPLICATION (application));
  priv->windows = g_list_remove (priv->windows, window);
463
  gtk_window_set_application (window, NULL);
464

465
  if (priv->windows != old_active && priv->impl)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
466
    {
467
      gtk_application_impl_active_window_changed (priv->impl, priv->windows ? priv->windows->data : NULL);
468
      g_object_notify_by_pspec (G_OBJECT (application), gtk_application_props[PROP_ACTIVE_WINDOW]);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
469
    }
470
471
}

472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
static void
extract_accel_from_menu_item (GMenuModel     *model,
                              gint            item,
                              GtkApplication *app)
{
  GMenuAttributeIter *iter;
  const gchar *key;
  GVariant *value;
  const gchar *accel = NULL;
  const gchar *action = NULL;
  GVariant *target = NULL;

  iter = g_menu_model_iterate_item_attributes (model, item);
  while (g_menu_attribute_iter_get_next (iter, &key, &value))
    {
      if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
        action = g_variant_get_string (value, NULL);
      else if (g_str_equal (key, "accel") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
        accel = g_variant_get_string (value, NULL);
      else if (g_str_equal (key, "target"))
        target = g_variant_ref (value);
      g_variant_unref (value);
    }
  g_object_unref (iter);

  if (accel && action)
swilmet's avatar
swilmet committed
498
499
500
501
502
503
504
505
    {
      const gchar *accels[2] = { accel, NULL };
      gchar *detailed_action_name;

      detailed_action_name = g_action_print_detailed_name (action, target);
      gtk_application_set_accels_for_action (app, detailed_action_name, accels);
      g_free (detailed_action_name);
    }
506
507
508
509
510
511
512
513
514
515
516
517
518

  if (target)
    g_variant_unref (target);
}

static void
extract_accels_from_menu (GMenuModel     *model,
                          GtkApplication *app)
{
  gint i;

  for (i = 0; i < g_menu_model_get_n_items (model); i++)
    {
519
520
521
      GMenuLinkIter *iter;
      GMenuModel *sub_model;

522
523
524
      extract_accel_from_menu_item (model, i, app);

      iter = g_menu_model_iterate_item_links (model, i);
525
      while (g_menu_link_iter_get_next (iter, NULL, &sub_model))
526
        {
527
528
          extract_accels_from_menu (sub_model, app);
          g_object_unref (sub_model);
529
530
531
532
533
        }
      g_object_unref (iter);
    }
}

534
535
536
537
538
539
540
541
542
543
544
545
546
547
static void
gtk_application_get_property (GObject    *object,
                              guint       prop_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
  GtkApplication *application = GTK_APPLICATION (object);

  switch (prop_id)
    {
    case PROP_REGISTER_SESSION:
      g_value_set_boolean (value, application->priv->register_session);
      break;

548
549
550
551
    case PROP_SCREENSAVER_ACTIVE:
      g_value_set_boolean (value, application->priv->screensaver_active);
      break;

552
553
554
555
556
557
558
559
    case PROP_APP_MENU:
      g_value_set_object (value, gtk_application_get_app_menu (application));
      break;

    case PROP_MENUBAR:
      g_value_set_object (value, gtk_application_get_menubar (application));
      break;

560
561
562
563
    case PROP_ACTIVE_WINDOW:
      g_value_set_object (value, gtk_application_get_active_window (application));
      break;

564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_application_set_property (GObject      *object,
                              guint         prop_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
  GtkApplication *application = GTK_APPLICATION (object);

  switch (prop_id)
    {
    case PROP_REGISTER_SESSION:
      application->priv->register_session = g_value_get_boolean (value);
      break;

584
585
586
587
588
589
590
591
    case PROP_APP_MENU:
      gtk_application_set_app_menu (application, g_value_get_object (value));
      break;

    case PROP_MENUBAR:
      gtk_application_set_menubar (application, g_value_get_object (value));
      break;

592
593
594
595
596
597
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

598
599
600
601
602
static void
gtk_application_finalize (GObject *object)
{
  GtkApplication *application = GTK_APPLICATION (object);

603
  g_clear_object (&application->priv->menus_builder);
604
605
  g_clear_object (&application->priv->app_menu);
  g_clear_object (&application->priv->menubar);
606
  g_clear_object (&application->priv->muxer);
607
  g_clear_object (&application->priv->accels);
608

609
610
611
  g_free (application->priv->help_overlay_path);

  G_OBJECT_CLASS (gtk_application_parent_class)->finalize (object);
612
613
}

Christian Hergert's avatar
Christian Hergert committed
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
#ifdef G_OS_UNIX

static const gchar org_gnome_Sysprof3_Profiler_xml[] =
  "<node>"
    "<interface name='org.gnome.Sysprof3.Profiler'>"
      "<property name='Capabilities' type='a{sv}' access='read'/>"
      "<method name='Start'>"
        "<arg type='a{sv}' name='options' direction='in'/>"
        "<arg type='h' name='fd' direction='in'/>"
      "</method>"
      "<method name='Stop'>"
      "</method>"
    "</interface>"
  "</node>";

static GDBusInterfaceInfo *org_gnome_Sysprof3_Profiler;

static void
sysprof_profiler_method_call (GDBusConnection       *connection,
                              const gchar           *sender,
                              const gchar           *object_path,
                              const gchar           *interface_name,
                              const gchar           *method_name,
                              GVariant              *parameters,
                              GDBusMethodInvocation *invocation,
                              gpointer               user_data)
{
  if (strcmp (method_name, "Start") == 0)
    {
      GDBusMessage *message;
      GUnixFDList *fd_list;
      GVariant *options;
      int fd = -1;
      int idx;

      if (GDK_PRIVATE_CALL (gdk_profiler_is_running) ())
        {
          g_dbus_method_invocation_return_error (invocation,
                                                 G_DBUS_ERROR,
                                                 G_DBUS_ERROR_FAILED,
                                                 "Profiler already running");
          return;
        }

      g_variant_get (parameters, "(@a{sv}h)", &options, &idx);

      message = g_dbus_method_invocation_get_message (invocation);
      fd_list = g_dbus_message_get_unix_fd_list (message);
      if (fd_list)
        fd = g_unix_fd_list_get (fd_list, idx, NULL);

      GDK_PRIVATE_CALL (gdk_profiler_start) (fd);

      g_variant_unref (options);
    }
  else if (strcmp (method_name, "Stop") == 0)
    {
      if (!GDK_PRIVATE_CALL (gdk_profiler_is_running) ())
        {
          g_dbus_method_invocation_return_error (invocation,
                                                 G_DBUS_ERROR,
                                                 G_DBUS_ERROR_FAILED,
                                                 "Profiler not running");
          return;
        }

      GDK_PRIVATE_CALL (gdk_profiler_stop) ();
    }
  else
    {
      g_dbus_method_invocation_return_error (invocation,
                                             G_DBUS_ERROR,
                                             G_DBUS_ERROR_UNKNOWN_METHOD,
                                             "Unknown method");
      return;
    }

  g_dbus_method_invocation_return_value (invocation, NULL);
}

static gboolean
gtk_application_dbus_register (GApplication     *application,
                               GDBusConnection  *connection,
                               const char       *obect_path,
                               GError          **error)
{
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (GTK_APPLICATION (application));
  GDBusInterfaceVTable vtable = {
    sysprof_profiler_method_call,
    NULL,
    NULL
  };

  if (org_gnome_Sysprof3_Profiler == NULL)
    {
      GDBusNodeInfo *info;

      info = g_dbus_node_info_new_for_xml (org_gnome_Sysprof3_Profiler_xml, error);
      if (info == NULL)
        return FALSE;

      org_gnome_Sysprof3_Profiler = g_dbus_node_info_lookup_interface (info, "org.gnome.Sysprof3.Profiler");
      g_dbus_interface_info_ref (org_gnome_Sysprof3_Profiler);
      g_dbus_node_info_unref (info);
    }

  priv->profiler_id = g_dbus_connection_register_object (connection,
                                                         "/org/gtk/Profiler",
                                                         org_gnome_Sysprof3_Profiler,
                                                         &vtable,
                                                         NULL,
                                                         NULL,
                                                         error);

  return TRUE;
}

static void
gtk_application_dbus_unregister (GApplication     *application,
                                 GDBusConnection  *connection,
                                 const char       *obect_path)
{
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (GTK_APPLICATION (application));

  g_dbus_connection_unregister_object (connection, priv->profiler_id);
}

#else

static gboolean
gtk_application_dbus_register (GApplication     *application,
                               GDBusConnection  *connection,
                               const char       *obect_path,
                               GError          **error)
{
  return TRUE;
}

static void
gtk_application_dbus_unregister (GApplication     *application,
                                 GDBusConnection  *connection,
                                 const char       *obect_path)
{
}

#endif

Matthias Clasen's avatar
Matthias Clasen committed
761
static void
Allison Karlitskaya's avatar
Allison Karlitskaya committed
762
gtk_application_class_init (GtkApplicationClass *class)
Matthias Clasen's avatar
Matthias Clasen committed
763
{
764
  GObjectClass *object_class = G_OBJECT_CLASS (class);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
765
  GApplicationClass *application_class = G_APPLICATION_CLASS (class);
Matthias Clasen's avatar
Matthias Clasen committed
766

767
768
  object_class->get_property = gtk_application_get_property;
  object_class->set_property = gtk_application_set_property;
769
  object_class->finalize = gtk_application_finalize;
770

771
  application_class->local_command_line = gtk_application_local_command_line;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
772
773
774
775
  application_class->add_platform_data = gtk_application_add_platform_data;
  application_class->before_emit = gtk_application_before_emit;
  application_class->after_emit = gtk_application_after_emit;
  application_class->startup = gtk_application_startup;
776
  application_class->shutdown = gtk_application_shutdown;
Christian Hergert's avatar
Christian Hergert committed
777
778
  application_class->dbus_register = gtk_application_dbus_register;
  application_class->dbus_unregister = gtk_application_dbus_unregister;
779

780
781
782
783
784
785
786
787
788
  class->window_added = gtk_application_window_added;
  class->window_removed = gtk_application_window_removed;

  /**
   * GtkApplication::window-added:
   * @application: the #GtkApplication which emitted the signal
   * @window: the newly-added #GtkWindow
   *
   * Emitted when a #GtkWindow is added to @application through
789
   * gtk_application_add_window().
790
791
792
793
   *
   * Since: 3.2
   */
  gtk_application_signals[WINDOW_ADDED] =
794
    g_signal_new (I_("window-added"), GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
795
796
                  G_STRUCT_OFFSET (GtkApplicationClass, window_added),
                  NULL, NULL,
797
                  NULL,
798
799
800
801
802
803
804
805
806
807
808
809
810
811
                  G_TYPE_NONE, 1, GTK_TYPE_WINDOW);

  /**
   * GtkApplication::window-removed:
   * @application: the #GtkApplication which emitted the signal
   * @window: the #GtkWindow that is being removed
   *
   * Emitted when a #GtkWindow is removed from @application,
   * either as a side-effect of being destroyed or explicitly
   * through gtk_application_remove_window().
   *
   * Since: 3.2
   */
  gtk_application_signals[WINDOW_REMOVED] =
812
    g_signal_new (I_("window-removed"), GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
813
814
                  G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
                  NULL, NULL,
815
                  NULL,
816
                  G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
817

818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
  /**
   * GtkApplication::query-end:
   * @application: the #GtkApplication which emitted the signal
   *
   * Emitted when the session manager is about to end the session, only
   * if #GtkApplication::register-session is %TRUE. Applications can
   * connect to this signal and call gtk_application_inhibit() with
   * %GTK_APPLICATION_INHIBIT_LOGOUT to delay the end of the session
   * until state has been saved.
   *
   * Since: 3.24.8
   */
  gtk_application_signals[QUERY_END] =
    g_signal_new (I_("query-end"), GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
                  0,
                  NULL, NULL,
                  NULL,
                  G_TYPE_NONE, 0);
836
  /**
837
   * GtkApplication:register-session:
838
   *
839
   * Set this property to %TRUE to register with the session manager.
840
841
842
   *
   * Since: 3.4
   */
843
  gtk_application_props[PROP_REGISTER_SESSION] =
844
845
846
    g_param_spec_boolean ("register-session",
                          P_("Register session"),
                          P_("Register with the session manager"),
847
848
                          FALSE,
                          G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS);
849

850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
  /**
   * GtkApplication:screensaver-active:
   *
   * This property is %TRUE if GTK+ believes that the screensaver is
   * currently active. GTK+ only tracks session state (including this)
   * when #GtkApplication::register-session is set to %TRUE.
   *
   * Tracking the screensaver state is supported on Linux.
   *
   * Since: 3.24
   */
  gtk_application_props[PROP_SCREENSAVER_ACTIVE] =
    g_param_spec_boolean ("screensaver-active",
                          P_("Screensaver Active"),
                          P_("Whether the screensaver is active"),
                          FALSE,
                          G_PARAM_READABLE|G_PARAM_STATIC_STRINGS);

868
  gtk_application_props[PROP_APP_MENU] =
869
870
871
872
    g_param_spec_object ("app-menu",
                         P_("Application menu"),
                         P_("The GMenuModel for the application menu"),
                         G_TYPE_MENU_MODEL,
873
                         G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS);
874

875
  gtk_application_props[PROP_MENUBAR] =
876
877
878
879
    g_param_spec_object ("menubar",
                         P_("Menubar"),
                         P_("The GMenuModel for the menubar"),
                         G_TYPE_MENU_MODEL,
880
                         G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS);
881

882
  gtk_application_props[PROP_ACTIVE_WINDOW] =
883
884
885
886
    g_param_spec_object ("active-window",
                         P_("Active window"),
                         P_("The window which most recently had focus"),
                         GTK_TYPE_WINDOW,
887
888
889
                         G_PARAM_READABLE|G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, NUM_PROPERTIES, gtk_application_props);
890
891
}

Matthias Clasen's avatar
Matthias Clasen committed
892
893
/**
 * gtk_application_new:
894
 * @application_id: (allow-none): The application ID.
Matthias Clasen's avatar
Matthias Clasen committed
895
896
897
898
 * @flags: the application flags
 *
 * Creates a new #GtkApplication instance.
 *
Matthias Clasen's avatar
Matthias Clasen committed
899
900
901
 * When using #GtkApplication, it is not necessary to call gtk_init()
 * manually. It is called as soon as the application gets registered as
 * the primary instance.
Matthias Clasen's avatar
Matthias Clasen committed
902
 *
903
 * Concretely, gtk_init() is called in the default handler for the
904
 * #GApplication::startup signal. Therefore, #GtkApplication subclasses should
905
 * chain up in their #GApplication::startup handler before using any GTK+ API.
906
 *
907
908
909
 * Note that commandline arguments are not passed to gtk_init().
 * All GTK+ functionality that is available via commandline arguments
 * can also be achieved by setting suitable environment variables
910
 * such as `G_DEBUG`, so this should not be a big
911
912
913
914
 * problem. If you absolutely must support GTK+ commandline arguments,
 * you can explicitly call gtk_init() before creating the application
 * instance.
 *
915
916
917
918
919
920
 * If non-%NULL, the application ID must be valid.  See
 * g_application_id_is_valid().
 *
 * If no application ID is given then some features (most notably application 
 * uniqueness) will be disabled. A null application ID is only allowed with 
 * GTK+ 3.6 or later.
Matthias Clasen's avatar
Matthias Clasen committed
921
922
 *
 * Returns: a new #GtkApplication instance
923
924
 *
 * Since: 3.0
Matthias Clasen's avatar
Matthias Clasen committed
925
 */
926
927
928
929
GtkApplication *
gtk_application_new (const gchar       *application_id,
                     GApplicationFlags  flags)
{
930
  g_return_val_if_fail (application_id == NULL || g_application_id_is_valid (application_id), NULL);
931
932
933
934
935
936
937
938

  return g_object_new (GTK_TYPE_APPLICATION,
                       "application-id", application_id,
                       "flags", flags,
                       NULL);
}

/**
Javier Jardón's avatar
Javier Jardón committed
939
 * gtk_application_add_window:
940
941
942
 * @application: a #GtkApplication
 * @window: a #GtkWindow
 *
943
 * Adds a window to @application.
944
 *
945
946
947
948
 * This call can only happen after the @application has started;
 * typically, you should add new application windows in response
 * to the emission of the #GApplication::activate signal.
 *
Javier Jardón's avatar
Javier Jardón committed
949
 * This call is equivalent to setting the #GtkWindow:application
950
951
 * property of @window to @application.
 *
952
953
954
955
 * Normally, the connection between the application and the window
 * will remain until the window is destroyed, but you can explicitly
 * remove it with gtk_application_remove_window().
 *
956
 * GTK+ will keep the @application running as long as it has
957
958
 * any windows.
 *
959
960
961
962
963
964
965
 * Since: 3.0
 **/
void
gtk_application_add_window (GtkApplication *application,
                            GtkWindow      *window)
{
  g_return_if_fail (GTK_IS_APPLICATION (application));
swilmet's avatar
swilmet committed
966
  g_return_if_fail (GTK_IS_WINDOW (window));
967

968
969
970
971
972
973
974
  if (!g_application_get_is_registered (G_APPLICATION (application)))
    {
      g_critical ("New application windows must be added after the "
                  "GApplication::startup signal has been emitted.");
      return;
    }

975
976
977
  if (!g_list_find (application->priv->windows, window))
    g_signal_emit (application,
                   gtk_application_signals[WINDOW_ADDED], 0, window);
978
979
980
981
982
983
984
985
986
987
}

/**
 * gtk_application_remove_window:
 * @application: a #GtkApplication
 * @window: a #GtkWindow
 *
 * Remove a window from @application.
 *
 * If @window belongs to @application then this call is equivalent to
Javier Jardón's avatar
Javier Jardón committed
988
 * setting the #GtkWindow:application property of @window to
989
990
991
992
993
994
995
996
997
998
999
1000
 * %NULL.
 *
 * The application may stop running as a result of a call to this
 * function.
 *
 * Since: 3.0
 **/
void
gtk_application_remove_window (GtkApplication *application,
                               GtkWindow      *window)
{
  g_return_if_fail (GTK_IS_APPLICATION (application));
For faster browsing, not all history is shown. View entire blame