ephy-embed.c 26.5 KB
Newer Older
1
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
Xan Lopez's avatar
Xan Lopez committed
2
/* vim: set sw=2 ts=2 sts=2 et: */
3 4
/*
 *  Copyright © 2007 Xan Lopez
5
 *  Copyright © 2008 Jan Alonzo
6
 *  Copyright © 2009 Gustavo Noronha Silva
7
 *  Copyright © 2009 Igalia S.L.
8
 *  Copyright © 2009 Collabora Ltd.
9 10 11 12 13 14 15 16 17 18 19 20
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 23 24 25 26 27 28 29
 */

#include "config.h"
#include "ephy-embed.h"

#include "ephy-debug.h"
#include "ephy-embed-prefs.h"
#include "ephy-embed-shell.h"
30
#include "ephy-embed-utils.h"
31 32 33
#include "ephy-find-toolbar.h"
#include "ephy-prefs.h"
#include "ephy-settings.h"
34
#include "ephy-string.h"
35 36 37 38 39 40 41 42 43 44 45
#include "ephy-web-view.h"
#include "nautilus-floating-bar.h"

#include <glib/gi18n.h>
#include <webkit2/webkit2.h>

static void     ephy_embed_constructed         (GObject *object);
static void     ephy_embed_restored_window_cb  (EphyEmbedShell *shell,
                                                EphyEmbed *embed);

#define EPHY_EMBED_STATUSBAR_TAB_MESSAGE_CONTEXT_DESCRIPTION "tab_message"
46 47
#define MAX_TITLE_LENGTH 512 /* characters */
#define EMPTY_PAGE_TITLE _("Blank page") /* Title for the empty page */
48 49 50 51 52 53 54

typedef struct {
  gchar *text;
  guint context_id;
  guint message_id;
} EphyEmbedStatusbarMsg;

55 56 57
struct _EphyEmbed {
  GtkBox parent_instance;

58 59 60 61 62 63 64 65 66
  EphyFindToolbar *find_toolbar;
  GtkBox *top_widgets_vbox;
  GtkPaned *paned;
  WebKitWebView *web_view;
  GSList *destroy_on_transition_list;
  GtkWidget *floating_bar;
  GtkWidget *progress;
  GtkWidget *fullscreen_message_label;

67
  char *title;
68
  WebKitURIRequest *delayed_request;
69
  guint delayed_request_source_id;
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

  GSList *messages;
  GSList *keys;

  guint seq_context_id;
  guint seq_message_id;

  guint tab_message_id;
  guint pop_statusbar_later_source_id;

  guint fullscreen_message_id;

  guint clear_progress_source_id;

  gulong status_handler_id;
  gulong progress_update_handler_id;
};

88 89
G_DEFINE_TYPE (EphyEmbed, ephy_embed, GTK_TYPE_BOX)

90 91 92
enum
{
  PROP_0,
93
  PROP_WEB_VIEW,
94
  PROP_TITLE,
95
  LAST_PROP
96 97
};

98
static GParamSpec *obj_properties[LAST_PROP];
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

/* Portions of the following code based on GTK+.
 * License block as follows:
 *
 * GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 * GtkStatusbar Copyright (C) 1998 Shawn T. Amundson
 *
 * 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
 * version 2 of the License, or (at your option) any later version.
 *
 * 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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
 *
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
 *
 */

static guint
ephy_embed_statusbar_get_context_id (EphyEmbed *embed, const char  *context_description)
{
  char *string;
  guint id;

  g_return_val_if_fail (EPHY_IS_EMBED (embed), 0);
  g_return_val_if_fail (context_description != NULL, 0);

  /* we need to preserve namespaces on object datas */
  string = g_strconcat ("ephy-embed-status-bar-context:", context_description, NULL);

  id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (embed), string));
  if (id == 0) {
143
    id = embed->seq_context_id++;
144
    g_object_set_data_full (G_OBJECT (embed), string, GUINT_TO_POINTER (id), NULL);
145
    embed->keys = g_slist_prepend (embed->keys, string);
146 147 148 149 150 151
  } else
    g_free (string);

  return id;
}

152 153 154
static void
ephy_embed_set_statusbar_label (EphyEmbed *embed, const char *label)
{
155
  nautilus_floating_bar_set_primary_label (NAUTILUS_FLOATING_BAR (embed->floating_bar), label);
156 157

  if (label == NULL || label[0] == '\0') {
158 159
    gtk_widget_hide (embed->floating_bar);
    gtk_widget_set_halign (embed->floating_bar, GTK_ALIGN_START);
160
  } else
161
    gtk_widget_show (embed->floating_bar);
162 163
}

164 165 166 167 168
static void
ephy_embed_statusbar_update (EphyEmbed *embed, const char *text)
{
  g_return_if_fail (EPHY_IS_EMBED (embed));

169
  ephy_embed_set_statusbar_label (embed, text);
170 171 172 173 174 175 176 177 178 179 180 181 182 183
}

static guint
ephy_embed_statusbar_push (EphyEmbed *embed, guint context_id, const char *text)
{
  EphyEmbedStatusbarMsg *msg;

  g_return_val_if_fail (EPHY_IS_EMBED (embed), 0);
  g_return_val_if_fail (context_id != 0, 0);
  g_return_val_if_fail (text != NULL, 0);

  msg = g_slice_new (EphyEmbedStatusbarMsg);
  msg->text = g_strdup (text);
  msg->context_id = context_id;
184
  msg->message_id = embed->seq_message_id++;
185

186
  embed->messages = g_slist_prepend (embed->messages, msg);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

  ephy_embed_statusbar_update (embed, text);

  return msg->message_id;
}

/* End of code based on GTK+ GtkStatusbar. */

static void
ephy_embed_statusbar_pop (EphyEmbed *embed, guint context_id)
{
  EphyEmbedStatusbarMsg *msg;
  GSList *list;

  g_return_if_fail (EPHY_IS_EMBED (embed));
  g_return_if_fail (context_id != 0);

204
  for (list = embed->messages; list; list = list->next) {
205
    msg = list->data;
206 207

    if (msg->context_id == context_id) {
208
      embed->messages = g_slist_remove_link (embed->messages, list);
209 210 211 212 213 214 215
      g_free (msg->text);
      g_slice_free (EphyEmbedStatusbarMsg, msg);
      g_slist_free_1 (list);
      break;
    }
  }

216
  msg = embed->messages ? embed->messages->data : NULL;
217 218 219
  ephy_embed_statusbar_update (embed, msg ? msg->text : NULL);
}

220 221 222 223 224
static void
ephy_embed_destroy_top_widgets (EphyEmbed *embed)
{
  GSList *iter;

225
  for (iter = embed->destroy_on_transition_list; iter; iter = iter->next)
226 227 228 229 230 231 232 233
    gtk_widget_destroy (GTK_WIDGET (iter->data));
}

static void
remove_from_destroy_list_cb (GtkWidget *widget, EphyEmbed *embed)
{
  GSList *list;

234
  list = embed->destroy_on_transition_list;
235
  list = g_slist_remove (list, widget);
236
  embed->destroy_on_transition_list = list;
237 238
}

239 240 241 242 243 244 245 246 247 248 249 250 251
static void
ephy_embed_set_title (EphyEmbed *embed,
                      const char *title)
{
  char *new_title;

  new_title = g_strdup (title);
  if (new_title == NULL || g_strstrip (new_title)[0] == '\0') {
    const char *address;

    g_free (new_title);
    new_title = NULL;

252
    address = ephy_web_view_get_address (EPHY_WEB_VIEW (embed->web_view));
253 254 255 256 257 258 259 260 261
    if (address && strcmp (address, "about:blank") != 0)
      new_title = ephy_embed_utils_get_title_from_address (address);

    if (new_title == NULL || new_title[0] == '\0') {
      g_free (new_title);
      new_title = g_strdup (EMPTY_PAGE_TITLE);
    }
  }

262 263
  g_free (embed->title);
  embed->title = ephy_string_shorten (new_title, MAX_TITLE_LENGTH);
264

265
  g_object_notify_by_pspec (G_OBJECT (embed), obj_properties[PROP_TITLE]);
266 267 268 269 270 271 272 273 274 275
}

static void
web_view_title_changed_cb (WebKitWebView *web_view,
                           GParamSpec *spec,
                           EphyEmbed *embed)
{
  ephy_embed_set_title (embed, webkit_web_view_get_title (web_view));
}

276 277 278 279 280
static void
load_changed_cb (WebKitWebView *web_view,
                 WebKitLoadEvent load_event,
                 EphyEmbed *embed)
{
281 282
  switch (load_event) {
  case WEBKIT_LOAD_COMMITTED:
283
    ephy_embed_destroy_top_widgets (embed);
284 285 286 287 288 289 290 291 292
    break;
  case WEBKIT_LOAD_FINISHED:
    if (ephy_web_view_get_is_blank (EPHY_WEB_VIEW (web_view)) ||
        !webkit_web_view_get_title (web_view))
      ephy_embed_set_title (embed, NULL);
    break;
  default:
    break;
  }
293
}
294

295 296 297 298 299
static void
ephy_embed_grab_focus (GtkWidget *widget)
{
  GtkWidget *child;

300
  child = GTK_WIDGET (ephy_embed_get_web_view (EPHY_EMBED (widget)));
301 302 303 304 305

  if (child)
    gtk_widget_grab_focus (child);
}

306 307 308 309

static gboolean
fullscreen_message_label_hide (EphyEmbed *embed)
{
310 311 312 313
  if (embed->fullscreen_message_id) {
    gtk_widget_hide (embed->fullscreen_message_label);
    g_source_remove (embed->fullscreen_message_id);
    embed->fullscreen_message_id = 0;
314 315 316 317 318 319 320 321
  }

  return FALSE;
}

void
ephy_embed_entering_fullscreen (EphyEmbed *embed)
{
322
  if (!g_settings_get_boolean (EPHY_SETTINGS_LOCKDOWN, EPHY_PREFS_LOCKDOWN_FULLSCREEN)) {
323
    gtk_widget_show (embed->fullscreen_message_label);
324

325 326
    if (embed->fullscreen_message_id)
      g_source_remove (embed->fullscreen_message_id);
327

328
    embed->fullscreen_message_id = g_timeout_add_seconds (5,
329 330
                                                                (GSourceFunc)fullscreen_message_label_hide,
                                                                embed);
331
    g_source_set_name_by_id (embed->fullscreen_message_id, "[epiphany] fullscreen_message_label_hide");
332
  }
333 334 335 336 337 338 339 340
}

void
ephy_embed_leaving_fullscreen (EphyEmbed *embed)
{
  fullscreen_message_label_hide (embed);
}

341 342 343 344 345
static void
ephy_embed_dispose (GObject *object)
{
  EphyEmbed *embed = EPHY_EMBED (object);

346 347 348
  if (embed->pop_statusbar_later_source_id) {
    g_source_remove (embed->pop_statusbar_later_source_id);
    embed->pop_statusbar_later_source_id = 0;
349 350
  }

351 352 353
  if (embed->clear_progress_source_id) {
    g_source_remove (embed->clear_progress_source_id);
    embed->clear_progress_source_id = 0;
354 355
  }

356 357 358
  if (embed->delayed_request_source_id) {
    g_source_remove (embed->delayed_request_source_id);
    embed->delayed_request_source_id = 0;
359 360
  }

361 362
  /* Do not listen to status message notifications anymore, if we try
   * to update the statusbar after dispose we might crash. */
363 364 365
  if (embed->status_handler_id) {
    g_signal_handler_disconnect (embed->web_view, embed->status_handler_id);
    embed->status_handler_id = 0;
366 367
  }

368 369 370
  if (embed->progress_update_handler_id) {
    g_signal_handler_disconnect (embed->web_view, embed->progress_update_handler_id);
    embed->progress_update_handler_id = 0;
371 372
  }

373 374 375
  if (embed->fullscreen_message_id) {
    g_source_remove (embed->fullscreen_message_id);
    embed->fullscreen_message_id = 0;
376 377
  }

378
  g_clear_object (&embed->delayed_request);
379

380 381 382
  G_OBJECT_CLASS (ephy_embed_parent_class)->dispose (object);
}

383 384 385 386
static void
ephy_embed_finalize (GObject *object)
{
  EphyEmbed *embed = EPHY_EMBED (object);
387
  EphyEmbedShell *shell = ephy_embed_shell_get_default ();
388
  GSList *list;
389

390 391
  g_signal_handlers_disconnect_by_func(shell, ephy_embed_restored_window_cb, embed);

392
  list = embed->destroy_on_transition_list;
393 394 395 396
  for (; list; list = list->next) {
    GtkWidget *widget = GTK_WIDGET (list->data);
    g_signal_handlers_disconnect_by_func (widget, remove_from_destroy_list_cb, embed);
  }
397
  g_slist_free (embed->destroy_on_transition_list);
398

399
  for (list = embed->messages; list; list = list->next) {
400 401 402 403 404 405 406
    EphyEmbedStatusbarMsg *msg;

    msg = list->data;
    g_free (msg->text);
    g_slice_free (EphyEmbedStatusbarMsg, msg);
  }

407 408
  g_slist_free (embed->messages);
  embed->messages = NULL;
409

410
  for (list = embed->keys; list; list = list->next)
411 412
    g_free (list->data);

413 414
  g_slist_free (embed->keys);
  embed->keys = NULL;
415

416
  g_free (embed->title);
417

418 419 420
  G_OBJECT_CLASS (ephy_embed_parent_class)->finalize (object);
}

421 422 423 424 425 426 427 428 429 430
static void
ephy_embed_set_property (GObject *object,
                         guint prop_id,
                         const GValue *value,
                         GParamSpec *pspec)
{
  EphyEmbed *embed = EPHY_EMBED (object);

  switch (prop_id)
  {
431
  case PROP_WEB_VIEW:
432
    embed->web_view = g_value_get_object (value);
433
    break;
434
  case PROP_TITLE:
435
    ephy_embed_set_title (embed, g_value_get_string (value));
436
    break;
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    break;
  }
}

static void
ephy_embed_get_property (GObject *object,
                         guint prop_id,
                         GValue *value,
                         GParamSpec *pspec)
{
  EphyEmbed *embed = EPHY_EMBED (object);

  switch (prop_id)
  {
453 454 455
  case PROP_WEB_VIEW:
    g_value_set_object (value, ephy_embed_get_web_view (embed));
    break;
456 457 458
  case PROP_TITLE:
    g_value_set_string (value, ephy_embed_get_title (embed));
    break;
459 460 461 462 463 464
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    break;
  }
}

465 466 467 468
static void
ephy_embed_find_toolbar_close_cb (EphyFindToolbar *toolbar,
                                  EphyEmbed *embed)
{
469
  ephy_find_toolbar_close (embed->find_toolbar);
470 471 472 473

  gtk_widget_grab_focus (GTK_WIDGET (embed));
}

474
static void
475
ephy_embed_class_init (EphyEmbedClass *klass)
476
{
Xan Lopez's avatar
Xan Lopez committed
477
  GObjectClass *object_class = (GObjectClass *)klass;
478 479
  GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;

Xan Lopez's avatar
Xan Lopez committed
480
  object_class->constructed = ephy_embed_constructed;
481
  object_class->finalize = ephy_embed_finalize;
482
  object_class->dispose = ephy_embed_dispose;
483 484
  object_class->set_property = ephy_embed_set_property;
  object_class->get_property = ephy_embed_get_property;
485 486
  widget_class->grab_focus = ephy_embed_grab_focus;

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
  obj_properties[PROP_WEB_VIEW] =
    g_param_spec_object ("web-view",
                         "Web View",
                         "The WebView contained in the embed",
                         EPHY_TYPE_WEB_VIEW,
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

  obj_properties[PROP_TITLE] =
    g_param_spec_string ("title",
                         "Title",
                         "The embed's title",
                         NULL,
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, LAST_PROP, obj_properties);
502 503
}

504 505 506 507 508 509 510 511
static gboolean
ephy_embed_attach_inspector_cb (WebKitWebInspector *inspector,
                                EphyEmbed *embed)
{
  GtkWidget *inspector_view = GTK_WIDGET (webkit_web_inspector_get_web_view (inspector));
  int inspected_view_height;
  guint attached_height;

512
  inspected_view_height = gtk_widget_get_allocated_height (GTK_WIDGET (embed->web_view));
513
  attached_height = webkit_web_inspector_get_attached_height (inspector);
514
  gtk_paned_set_position (embed->paned, inspected_view_height - attached_height);
515

516
  gtk_paned_add2 (embed->paned, inspector_view);
517 518 519 520
  gtk_widget_show (inspector_view);

  return TRUE;
}
Xan Lopez's avatar
Xan Lopez committed
521

522 523 524 525 526 527 528
static void
ephy_embed_set_fullscreen_message (EphyEmbed *embed,
                                   gboolean is_html5_fullscreen)
{
  char *message;

  /* Translators: 'ESC' and 'F11' are keyboard keys. */
529
  message = g_strdup_printf (_("Press %s to exit fullscreen"), is_html5_fullscreen ? _("ESC") : _("F11"));
530
  gtk_label_set_text (GTK_LABEL (embed->fullscreen_message_label),
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
                      message);
  g_free (message);
}

static gboolean
entering_fullscreen_cb (WebKitWebView *web_view,
                        EphyEmbed *embed)
{
  ephy_embed_set_fullscreen_message (embed, TRUE);
  return FALSE;
}

static gboolean
leaving_fullscreen_cb (WebKitWebView *web_view,
                       EphyEmbed *embed)
{
  ephy_embed_set_fullscreen_message (embed, FALSE);
  return FALSE;
}

551 552 553 554 555
static gboolean
pop_statusbar_later_cb (gpointer data)
{
  EphyEmbed *embed = EPHY_EMBED (data);

556 557
  ephy_embed_statusbar_pop (embed, embed->tab_message_id);
  embed->pop_statusbar_later_source_id = 0;
558 559 560
  return FALSE;
}

561 562 563 564 565 566 567
static void
status_message_notify_cb (EphyWebView *view, GParamSpec *pspec, EphyEmbed *embed)
{
  const char *message;

  message = ephy_web_view_get_status_message (view);

568
  if (message) {
569 570 571
    if (embed->pop_statusbar_later_source_id) {
      g_source_remove (embed->pop_statusbar_later_source_id);
      embed->pop_statusbar_later_source_id = 0;
572
    }
573

574 575
    ephy_embed_statusbar_pop (embed, embed->tab_message_id);
    ephy_embed_statusbar_push (embed, embed->tab_message_id, message);
576 577 578
  } else {
    /* A short timeout before hiding the statusbar ensures that while moving
      over a series of links, the overlay widget doesn't flicker on and off. */
579 580 581
    if (embed->pop_statusbar_later_source_id == 0) {
      embed->pop_statusbar_later_source_id = g_timeout_add (250, pop_statusbar_later_cb, embed);
      g_source_set_name_by_id (embed->pop_statusbar_later_source_id, "[epiphany] pop_statusbar_later_cb");
582
    }
583
  }
584 585
}

586 587 588
static gboolean
clear_progress_cb (EphyEmbed *embed)
{
589 590
  gtk_widget_hide (embed->progress);
  embed->clear_progress_source_id = 0;
591 592 593 594

  return FALSE;
}

595 596 597 598 599
static void
progress_update (EphyWebView *view, GParamSpec *pspec, EphyEmbed *embed)
{
  gdouble progress;
  gboolean loading;
600
  const char *uri;
601

602 603 604
  if (embed->clear_progress_source_id) {
    g_source_remove (embed->clear_progress_source_id);
    embed->clear_progress_source_id = 0;
605 606
  }

607
  uri = webkit_web_view_get_uri (embed->web_view);
608
  if (!uri || g_str_has_prefix (uri, "ephy-about:") ||
609
      g_str_has_prefix (uri, "about:")) {
610
    gtk_widget_hide (embed->progress);
611
    return;
612
  }
613

614 615
  progress = webkit_web_view_get_estimated_load_progress (embed->web_view);
  loading = ephy_web_view_is_loading (EPHY_WEB_VIEW (embed->web_view));
616

617
  if (progress == 1.0 || !loading) {
618 619 620 621
    embed->clear_progress_source_id = g_timeout_add (500,
                                                     (GSourceFunc)clear_progress_cb,
                                                     embed);
    g_source_set_name_by_id (embed->clear_progress_source_id, "[epiphany] clear_progress_cb");
622
  } else
623
    gtk_widget_show (embed->progress);
624

625
  gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (embed->progress),
626 627 628
                                 (loading || progress == 1.0) ? progress : 0.0);
}

629 630
static gboolean
load_delayed_request_if_mapped (gpointer user_data)
631
{
632
  EphyEmbed *embed = EPHY_EMBED (user_data);
633 634
  EphyWebView *web_view;

635
  embed->delayed_request_source_id = 0;
636

637 638
  if (!gtk_widget_get_mapped (GTK_WIDGET (embed)))
    return G_SOURCE_REMOVE;
639

640
  web_view = ephy_embed_get_web_view (embed);
641 642
  ephy_web_view_load_request (web_view, embed->delayed_request);
  g_clear_object (&embed->delayed_request);
643 644 645 646 647

  /* This is to allow UI elements watching load status to show that the page is
   * loading as soon as possible.
   */
  g_signal_emit_by_name (web_view, "load-changed", WEBKIT_LOAD_STARTED);
648 649 650 651 652 653 654

  return G_SOURCE_REMOVE;
}

static void
ephy_embed_maybe_load_delayed_request (EphyEmbed *embed)
{
655
  if (!embed->delayed_request || embed->delayed_request_source_id != 0)
656 657 658 659 660 661
    return;

  /* Add a very small delay before loading the request, so that if the user
   * is scrolling rapidly through a bunch of delayed tabs, we don't start
   * loading them all.
   */
662 663
  embed->delayed_request_source_id = g_timeout_add (300, load_delayed_request_if_mapped, embed);
  g_source_set_name_by_id (embed->delayed_request_source_id, "[epiphany] load_delayed_request_if_mapped");
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
}

static void
ephy_embed_restored_window_cb (EphyEmbedShell *shell, EphyEmbed *embed)
{
  if (!gtk_widget_get_mapped (GTK_WIDGET (embed)))
    return;

  ephy_embed_maybe_load_delayed_request (embed);
}

static void
ephy_embed_mapped_cb (GtkWidget *widget, gpointer data)
{
  ephy_embed_maybe_load_delayed_request ((EphyEmbed*)widget);
}

681
static void
Xan Lopez's avatar
Xan Lopez committed
682
ephy_embed_constructed (GObject *object)
683
{
Xan Lopez's avatar
Xan Lopez committed
684
  EphyEmbed *embed = (EphyEmbed*)object;
685
  EphyEmbedShell *shell = ephy_embed_shell_get_default ();
686
  GtkWidget *paned;
Xan Lopez's avatar
Xan Lopez committed
687
  WebKitWebInspector *inspector;
688
  GtkWidget *overlay;
689

690 691 692 693 694 695
  g_signal_connect (shell, "window-restored",
                    G_CALLBACK (ephy_embed_restored_window_cb), embed);

  g_signal_connect (embed, "map",
                    G_CALLBACK (ephy_embed_mapped_cb), NULL);

696
  /* Skeleton */
697
  overlay = gtk_overlay_new ();
698

699 700 701
  gtk_widget_add_events (overlay, 
                         GDK_ENTER_NOTIFY_MASK |
                         GDK_LEAVE_NOTIFY_MASK);
702
  gtk_container_add (GTK_CONTAINER (overlay), GTK_WIDGET (embed->web_view));
703

704
  /* Floating message popup for fullscreen mode. */
705 706 707 708 709 710
  embed->fullscreen_message_label = gtk_label_new (NULL);
  gtk_widget_set_name (embed->fullscreen_message_label, "fullscreen-popup");
  gtk_widget_set_halign (embed->fullscreen_message_label, GTK_ALIGN_CENTER);
  gtk_widget_set_valign (embed->fullscreen_message_label, GTK_ALIGN_CENTER);
  gtk_widget_set_no_show_all (embed->fullscreen_message_label, TRUE);
  gtk_overlay_add_overlay (GTK_OVERLAY (overlay), embed->fullscreen_message_label);
711 712
  ephy_embed_set_fullscreen_message (embed, FALSE);

713
  /* statusbar is hidden by default */
714 715 716 717
  embed->floating_bar = nautilus_floating_bar_new (NULL, NULL, FALSE);
  gtk_widget_set_halign (embed->floating_bar, GTK_ALIGN_START);
  gtk_widget_set_valign (embed->floating_bar, GTK_ALIGN_END);
  gtk_widget_set_no_show_all (embed->floating_bar, TRUE);
718

719
  gtk_overlay_add_overlay (GTK_OVERLAY (overlay), embed->floating_bar);
720

721 722
  embed->progress = gtk_progress_bar_new ();
  gtk_style_context_add_class (gtk_widget_get_style_context (embed->progress),
723
                               GTK_STYLE_CLASS_OSD);
724 725 726
  gtk_widget_set_halign (embed->progress, GTK_ALIGN_FILL);
  gtk_widget_set_valign (embed->progress, GTK_ALIGN_START);
  gtk_overlay_add_overlay (GTK_OVERLAY (overlay), embed->progress);
727

728 729
  embed->find_toolbar = ephy_find_toolbar_new (embed->web_view);
  g_signal_connect (embed->find_toolbar, "close",
730 731 732 733
                    G_CALLBACK (ephy_embed_find_toolbar_close_cb),
                    embed);

  gtk_box_pack_start (GTK_BOX (embed),
734
                      GTK_WIDGET (embed->find_toolbar),
735 736
                      FALSE, FALSE, 0);

737
  paned = GTK_WIDGET (embed->paned);
738

739 740
  embed->progress_update_handler_id = g_signal_connect (embed->web_view, "notify::estimated-load-progress",
                                                        G_CALLBACK (progress_update), object);
741
  gtk_paned_pack1 (GTK_PANED (paned), GTK_WIDGET (overlay),
742 743 744
                   TRUE, FALSE);

  gtk_box_pack_start (GTK_BOX (embed),
745
                      GTK_WIDGET (embed->top_widgets_vbox),
746
                      FALSE, FALSE, 0);
747
  gtk_box_pack_start (GTK_BOX (embed), paned, TRUE, TRUE, 0);
748

749 750
  gtk_widget_show (GTK_WIDGET (embed->top_widgets_vbox));
  gtk_widget_show (GTK_WIDGET (embed->web_view));
751
  gtk_widget_show_all (paned);
752

753
  g_object_connect (embed->web_view,
754
                    "signal::notify::title", G_CALLBACK (web_view_title_changed_cb), embed,
755 756 757 758
                    "signal::load-changed", G_CALLBACK (load_changed_cb), embed,
                    "signal::enter-fullscreen", G_CALLBACK (entering_fullscreen_cb), embed,
                    "signal::leave-fullscreen", G_CALLBACK (leaving_fullscreen_cb), embed,
                    NULL);
759

760 761 762
  embed->status_handler_id = g_signal_connect (embed->web_view, "notify::status-message",
                                               G_CALLBACK (status_message_notify_cb),
                                               embed);
763

764
  /* The inspector */
765
  inspector = webkit_web_view_get_inspector (embed->web_view);
Xan Lopez's avatar
Xan Lopez committed
766

767 768 769
  g_signal_connect (inspector, "attach",
                    G_CALLBACK (ephy_embed_attach_inspector_cb),
                    embed);
770 771
}

Xan Lopez's avatar
Xan Lopez committed
772 773 774
static void
ephy_embed_init (EphyEmbed *embed)
{
775 776 777
  gtk_orientable_set_orientation (GTK_ORIENTABLE (embed),
                                  GTK_ORIENTATION_VERTICAL);

778 779 780 781 782
  embed->paned = GTK_PANED (gtk_paned_new (GTK_ORIENTATION_VERTICAL));
  embed->top_widgets_vbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_VERTICAL, 0));
  embed->seq_context_id = 1;
  embed->seq_message_id = 1;
  embed->tab_message_id = ephy_embed_statusbar_get_context_id (embed, EPHY_EMBED_STATUSBAR_TAB_MESSAGE_CONTEXT_DESCRIPTION);
Xan Lopez's avatar
Xan Lopez committed
783 784
}

785 786 787 788 789 790
/**
 * ephy_embed_get_web_view:
 * @embed: and #EphyEmbed
 * 
 * Returns the #EphyWebView wrapped by @embed.
 * 
791
 * Returns: (transfer none): an #EphyWebView
792 793 794 795 796 797
 **/
EphyWebView*
ephy_embed_get_web_view (EphyEmbed *embed)
{
  g_return_val_if_fail (EPHY_IS_EMBED (embed), NULL);

798
  return EPHY_WEB_VIEW (embed->web_view);
799
}
800

801 802 803 804 805 806 807 808 809 810 811 812 813
/**
 * ephy_embed_get_find_toolbar:
 * @embed: and #EphyEmbed
 * 
 * Returns the #EphyFindToolbar wrapped by @embed.
 * 
 * Returns: (transfer none): an #EphyFindToolbar
 **/
EphyFindToolbar*
ephy_embed_get_find_toolbar (EphyEmbed *embed)
{
  g_return_val_if_fail (EPHY_IS_EMBED (embed), NULL);

814
  return EPHY_FIND_TOOLBAR (embed->find_toolbar);
815 816 817
}


818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
/**
 * ephy_embed_add_top_widget:
 * @embed: an #EphyEmbed
 * @widget: a #GtkWidget
 * @destroy_on_transition: whether the widget be automatically
 * destroyed on page transitions
 *
 * Adds a #GtkWidget to the top of the embed.
 */
void
ephy_embed_add_top_widget (EphyEmbed *embed, GtkWidget *widget, gboolean destroy_on_transition)
{
  GSList *list;

  if (destroy_on_transition) {
833
    list = embed->destroy_on_transition_list;
834
    list = g_slist_prepend (list, widget);
835
    embed->destroy_on_transition_list = list;
836 837 838 839

    g_signal_connect (widget, "destroy", G_CALLBACK (remove_from_destroy_list_cb), embed);
  }

840
  gtk_box_pack_end (embed->top_widgets_vbox,
841
                    GTK_WIDGET (widget), FALSE, FALSE, 0);
842 843 844 845 846 847 848 849
}

/**
 * ephy_embed_remove_top_widget:
 * @embed: an #EphyEmbed
 * @widget: a #GtkWidget
 *
 * Removes an #GtkWidget from the top of the embed. The #GtkWidget
850
 * must have been added using ephy_embed_add_top_widget(), and not
851 852 853 854 855 856
 * have been removed by other means. See gtk_container_remove() for
 * details.
 */
void
ephy_embed_remove_top_widget (EphyEmbed *embed, GtkWidget *widget)
{
857
  if (g_slist_find (embed->destroy_on_transition_list, widget)) {
858 859 860
    GSList *list;
    g_signal_handlers_disconnect_by_func (widget, remove_from_destroy_list_cb, embed);

861
    list = embed->destroy_on_transition_list;
862
    list = g_slist_remove (list, widget);
863
    embed->destroy_on_transition_list = list;
864 865
  }

866
  gtk_container_remove (GTK_CONTAINER (embed->top_widgets_vbox),
867 868
                        GTK_WIDGET (widget));
}
869

870 871 872 873 874 875 876 877
/**
 * ephy_embed_set_delayed_load_request:
 * @embed: a #EphyEmbed
 * @request: a #WebKitNetworkRequest
 *
 * Sets the #WebKitNetworkRequest that should be loaded when the tab this embed
 * is on is switched to.
 */
878 879
void
ephy_embed_set_delayed_load_request (EphyEmbed *embed, WebKitURIRequest *request)
880 881
{
  g_return_if_fail (EPHY_IS_EMBED (embed));
882
  g_return_if_fail (WEBKIT_IS_URI_REQUEST (request));
883

884
  g_clear_object (&embed->delayed_request);
885 886

  g_object_ref (request);
887
  embed->delayed_request = request;
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
}

/**
 * ephy_embed_has_load_pending:
 * @embed: a #EphyEmbed
 *
 * Checks whether a load has been delayed for this #EphyEmbed.
 *
 * Returns: %TRUE or %FALSE
 */
gboolean
ephy_embed_has_load_pending (EphyEmbed *embed)
{
  g_return_val_if_fail (EPHY_IS_EMBED (embed), FALSE);

903
  return !!embed->delayed_request;
904 905
}

906 907 908 909 910
const char *
ephy_embed_get_title (EphyEmbed *embed)
{
  g_return_val_if_fail (EPHY_IS_EMBED (embed), NULL);

911
  return embed->title;
912
}