ephy-embed-prefs.c 18.6 KB
Newer Older
1
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2
/* vim: set sw=2 ts=2 sts=2 et: */
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*  Copyright © 2008 Xan Lopez <xan@gnome.org>
 *
 *  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
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

21
#include <config.h>
22
#include "ephy-embed-prefs.h"
Xan Lopez's avatar
Xan Lopez committed
23

24
#include "ephy-embed-shell.h"
25
#include "ephy-embed-utils.h"
26
#include "ephy-file-helpers.h"
27
#include "ephy-langs.h"
28 29
#include "ephy-prefs.h"
#include "ephy-settings.h"
30

Xan Lopez's avatar
Xan Lopez committed
31
#include <glib.h>
32
#include <math.h>
Carlos Garcia Campos's avatar
Carlos Garcia Campos committed
33
#include <webkit2/webkit2.h>
Xan Lopez's avatar
Xan Lopez committed
34

35 36
typedef struct
{
37 38
  char *schema;
  char *key;
39
  char *webkit_pref;
40
  void (*callback) (GSettings *settings, char *key, gpointer data);
41 42
} PrefData;

43 44 45
#define ENABLE_SCRIPTS_SETTING "enable-javascript"
#define DEFAULT_ENCODING_SETTING "default-charset"
static WebKitSettings *webkit_settings = NULL;
46
static WebKitWebViewGroup *web_view_group = NULL;
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
static void
user_style_sheet_output_stream_splice_cb (GOutputStream *output_stream,
                                          GAsyncResult *result,
                                          gpointer user_data)
{
  gssize bytes;

  bytes = g_output_stream_splice_finish (output_stream, result, NULL);
  if (bytes > 0) {
    webkit_web_view_group_add_user_style_sheet (web_view_group,
                                                g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (output_stream)),
                                                NULL, NULL, NULL, WEBKIT_INJECTED_CONTENT_FRAMES_ALL);
  }
}

static void
user_style_seet_read_cb (GFile *file,
                         GAsyncResult *result,
                         gpointer user_data)
{
  GFileInputStream *input_stream;
  GOutputStream *output_stream;

  input_stream = g_file_read_finish (file, result, NULL);
  if (!input_stream)
    return;

  output_stream = g_memory_output_stream_new_resizable ();
  g_output_stream_splice_async (output_stream, G_INPUT_STREAM (input_stream),
                                G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
                                G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
                                G_PRIORITY_DEFAULT,
                                NULL,
                                (GAsyncReadyCallback)user_style_sheet_output_stream_splice_cb,
                                NULL);
  g_object_unref (input_stream);
  g_object_unref (output_stream);
}

87
static void
88 89
webkit_pref_callback_user_stylesheet (GSettings *settings,
                                      char *key,
90 91
                                      gpointer data)
{
92
  gboolean value;
93

94
  value = g_settings_get_boolean (settings, key);
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109
  if (!value)
    webkit_web_view_group_remove_all_user_style_sheets (web_view_group);
  else {
    GFile *file;
    char *filename;

    filename = g_build_filename (ephy_dot_dir (), USER_STYLESHEET_FILENAME, NULL);
    file = g_file_new_for_path (filename);
    g_free (filename);

    g_file_read_async (file, G_PRIORITY_DEFAULT, NULL,
                       (GAsyncReadyCallback)user_style_seet_read_cb, NULL);
    g_object_unref (file);
  }
110 111
}

Vincent Untz's avatar
Vincent Untz committed
112
static char *
113
webkit_pref_get_vendor_user_agent (void)
Vincent Untz's avatar
Vincent Untz committed
114
{
115
  char *vendor_user_agent = NULL;
Vincent Untz's avatar
Vincent Untz committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  GKeyFile *branding_keyfile;

  branding_keyfile = g_key_file_new ();

  if (g_key_file_load_from_file (branding_keyfile, SHARE_DIR"/branding.conf",
                                 G_KEY_FILE_NONE, NULL)) {
    char *vendor;
    char *vendor_sub;
    char *vendor_comment;

    vendor = g_key_file_get_string (branding_keyfile,
                                    "User Agent", "Vendor", NULL);
    vendor_sub = g_key_file_get_string (branding_keyfile,
                                        "User Agent", "VendorSub", NULL);
    vendor_comment = g_key_file_get_string (branding_keyfile,
                                            "User Agent", "VendorComment", NULL);

    if (vendor) {
      vendor_user_agent = g_strconcat (vendor,
                                       vendor_sub ? "/" : "",
                                       vendor_sub ? vendor_sub : "",
                                       vendor_comment ? " (" : "",
                                       vendor_comment ? vendor_comment : "",
                                       vendor_comment ? ")" : "",
                                       NULL);
    }

    g_free (vendor);
    g_free (vendor_sub);
    g_free (vendor_comment);
  }

  g_key_file_free (branding_keyfile);

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  return vendor_user_agent;
}

static const char *
webkit_pref_get_internal_user_agent (void)
{
  static char *user_agent = NULL;
  static gboolean initialized = FALSE;
  char *vendor_user_agent;
  const char *webkit_user_agent;

  if (initialized)
    return user_agent;

  initialized = TRUE;

  vendor_user_agent = webkit_pref_get_vendor_user_agent ();
  if (!vendor_user_agent)
    return NULL;

  webkit_user_agent = webkit_settings_get_user_agent (webkit_settings);
  user_agent = g_strdup_printf ("%s %s Epiphany/%s", webkit_user_agent,
                                vendor_user_agent, VERSION);
  g_free (vendor_user_agent);

  return user_agent;
}
Vincent Untz's avatar
Vincent Untz committed
177 178

static void
179 180
webkit_pref_callback_user_agent (GSettings *settings,
                                 char *key,
Vincent Untz's avatar
Vincent Untz committed
181 182
                                 gpointer data)
{
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
  char *value;
  const char *internal_user_agent;

  value = g_settings_get_string (settings, key);
  if (value != NULL && value[0] != '\0') {
    webkit_settings_set_user_agent (webkit_settings, value);
    g_free (value);
    return;
  }
  g_free (value);

  internal_user_agent = webkit_pref_get_internal_user_agent ();
  if (internal_user_agent)
    webkit_settings_set_user_agent (webkit_settings, internal_user_agent);
  else
    webkit_settings_set_user_agent_with_application_details (webkit_settings,
                                                             "Epiphany", VERSION);
Vincent Untz's avatar
Vincent Untz committed
200 201
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
static gdouble
get_screen_dpi (GdkScreen *screen)
{
  gdouble dpi;
  gdouble dp, di;

  dpi = gdk_screen_get_resolution (screen);
  if (dpi != -1)
    return dpi;

  dp = hypot (gdk_screen_get_width (screen), gdk_screen_get_height (screen));
  di = hypot (gdk_screen_get_width_mm (screen), gdk_screen_get_height_mm (screen)) / 25.4;

  return dp / di;
}

static guint
normalize_font_size (gdouble font_size)
{
  /* WebKit2 uses font sizes in pixels. */
  GdkScreen *screen;
  gdouble dpi;

  /* FIXME: We should use the view screen instead of the detault one
   * but we don't have access to the view here.
   */
  screen = gdk_screen_get_default ();
  dpi = screen ? get_screen_dpi (screen) : 96;

  return font_size / 72.0 * dpi;
}

234
static void
235 236
webkit_pref_callback_font_size (GSettings *settings,
                                char *key,
237 238 239
                                gpointer data)
{
  char *webkit_pref = data;
240
  char *value = NULL;
241 242
  int size = 9; /* FIXME: What to use here? */

243 244 245 246 247 248 249 250 251 252 253 254 255
  char *schema = NULL;
  g_object_get (settings, "schema", &schema, NULL);

  /* If we are changing a GNOME font value and we are not using GNOME fonts in
   * Epiphany, return. */
  if (g_strcmp0 (schema, EPHY_PREFS_WEB_SCHEMA) != 0 &&
      g_settings_get_boolean (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_USE_GNOME_FONTS) != TRUE)
  {
    g_free (schema);
    return;
  }
  g_free (schema);

256
  value = g_settings_get_string (settings, key);
257 258 259 260 261 262 263 264 265 266

  if (value) {
    PangoFontDescription* desc;

    desc = pango_font_description_from_string (value);
    size = pango_font_description_get_size (desc);
    if (pango_font_description_get_size_is_absolute (desc) == FALSE)
      size /= PANGO_SCALE;
    pango_font_description_free (desc);
  }
267 268

  g_object_set (webkit_settings, webkit_pref, normalize_font_size (size), NULL);
269
  g_free (value);
270 271
}

272
static void
273 274
webkit_pref_callback_font_family (GSettings *settings,
                                  char *key,
275 276 277
                                  gpointer data)
{
  char *webkit_pref = data;
278
  char *value = NULL;
279

280 281 282 283 284 285 286 287 288 289 290 291 292
  char *schema = NULL;
  g_object_get (settings, "schema", &schema, NULL);

  /* If we are changing a GNOME font value and we are not using GNOME fonts in
   * Epiphany, return. */
  if (g_strcmp0 (schema, EPHY_PREFS_WEB_SCHEMA) != 0 &&
      g_settings_get_boolean (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_USE_GNOME_FONTS) != TRUE)
  {
    g_free (schema);
    return;
  }
  g_free (schema);

293
  value = g_settings_get_string (settings, key);
294 295 296 297 298 299 300

  if (value) {
    PangoFontDescription* desc;
    const char *family = NULL;

    desc = pango_font_description_from_string (value);
    family = pango_font_description_get_family (desc);
301
    g_object_set (webkit_settings, webkit_pref, family, NULL);
302 303
    pango_font_description_free (desc);
  }
304 305

  g_free (value);
306 307
}

308 309 310
/* Based on Christian Persch's code from gecko backend of epiphany
   (old transform_accept_languages_list() function) */
static void
311 312
webkit_pref_callback_accept_languages (GSettings *settings,
                                       char *key,
313 314 315
                                       gpointer data)
{
  GArray *array;
316 317
  char **languages;
  int i;
318

319
  languages = g_settings_get_strv (settings, key);
320 321 322

  array = g_array_new (TRUE, FALSE, sizeof (char *));

323 324 325 326 327 328 329 330
  for (i = 0; languages[i]; i++) {
      if (!g_strcmp0 (languages[i], "system")) {
        ephy_langs_append_languages (array);
      } else if (languages[i][0] != '\0') {
        char *str = g_ascii_strdown (languages[i], -1);
        g_array_append_val (array, str);
      }
  }
331 332 333

  ephy_langs_sanitise (array);

334 335 336 337 338
  webkit_web_context_set_preferred_languages (webkit_web_context_get_default (),
                                              (const char * const *)array->data);

  g_strfreev (languages);
  g_array_free (array, TRUE);
339 340
}

Carlos Garcia Campos's avatar
Carlos Garcia Campos committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

void
ephy_embed_prefs_set_cookie_accept_policy (WebKitCookieManager *cookie_manager,
                                           const char *settings_policy)
{
  WebKitCookieAcceptPolicy policy;

  if (g_str_equal (settings_policy, "never"))
    policy = WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
  else if (g_str_equal (settings_policy, "always"))
    policy = WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
  else if (g_str_equal (settings_policy, "no-third-party"))
    policy = WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
  else {
    g_warn_if_reached ();
    return;
  }

  webkit_cookie_manager_set_accept_policy (cookie_manager, policy);
}
361 362

static void
363 364
webkit_pref_callback_cookie_accept_policy (GSettings *settings,
                                           char *key,
365 366
                                           gpointer data)
{
Carlos Garcia Campos's avatar
Carlos Garcia Campos committed
367 368 369 370 371 372 373 374 375 376
  WebKitCookieManager *cookie_manager;
  char *value;

  value = g_settings_get_string (settings, key);
  if (!value)
    return;

  cookie_manager = webkit_web_context_get_cookie_manager (webkit_web_context_get_default ());
  ephy_embed_prefs_set_cookie_accept_policy (cookie_manager, value);
  g_free (value);
377 378
}

379 380 381 382 383 384 385 386
static void
webkit_pref_callback_gnome_fonts (GSettings *ephy_settings,
                                  char *key,
                                  gpointer data)
{
  GSettings *settings;

  if (g_settings_get_boolean (ephy_settings, key)) {
387 388 389 390
    g_object_set (webkit_settings,
                  "default-font-family", "serif",
                  "sans-serif-font-family", "sans-serif",
                  "monospace-font-family", "monospace",
391 392
                  "default-font-size", normalize_font_size (12),
                  "default-monospace-font-size", normalize_font_size (10),
393
                  NULL);
394 395 396 397
  } else {
    /* Sync with Epiphany values */
    settings = ephy_settings;

398
    webkit_pref_callback_font_size (settings, EPHY_PREFS_WEB_SERIF_FONT,
399 400 401 402
                                    "default-font-size");
    webkit_pref_callback_font_size (settings, EPHY_PREFS_WEB_MONOSPACE_FONT,
                                    "default-monospace-font-size");

403
    webkit_pref_callback_font_family (settings, EPHY_PREFS_WEB_SERIF_FONT,
404 405 406 407 408 409 410 411 412 413
                                      "default-font-family");
    webkit_pref_callback_font_family (settings, EPHY_PREFS_WEB_SANS_SERIF_FONT,
                                      "sans-serif-font-family");
    webkit_pref_callback_font_family (settings, EPHY_PREFS_WEB_MONOSPACE_FONT,
                                      "monospace-font-family");
    webkit_pref_callback_font_family (settings, EPHY_PREFS_WEB_SERIF_FONT,
                                      "serif-font-family");
  }
}

414 415
static char **
normalize_languages (char **languages)
416 417
{
  int i;
418 419 420
  GPtrArray *langs;

  langs = g_ptr_array_new ();
421

422
  for (i = 0; languages && languages[i]; i++) {
423
    if (g_str_equal (languages[i], "system")) {
424 425
      char **sys_langs = ephy_langs_get_languages ();
      int j;
426

427 428
      for (j = 0; sys_langs && sys_langs[j]; j++)
        g_ptr_array_add (langs, g_strdelimit (g_strdup (sys_langs[i]), "-", '_'));
429 430

      g_strfreev (sys_langs);
431 432
    } else
      g_ptr_array_add (langs, g_strdelimit (g_strdup (languages[i]), "-", '_'));
433
  }
434 435 436 437

  g_ptr_array_add (langs, NULL);

  return (char **)g_ptr_array_free (langs, FALSE);
438 439
}

440 441 442 443 444
static void
webkit_pref_callback_enable_spell_checking (GSettings *settings,
                                            char *key,
                                            gpointer data)
{
445
  WebKitWebContext *web_context = NULL;
446
  gboolean value = FALSE;
447
  char **languages = NULL;
448 449 450 451 452
  char *langs = NULL;

  value = g_settings_get_boolean (settings, key);

  if (value) {
453 454
    char **normalized;

455
    languages = g_settings_get_strv (settings, EPHY_PREFS_WEB_LANGUAGE);
456
    normalized = normalize_languages (languages);
457
    g_strfreev (languages);
458 459 460

    languages = normalized;
    langs = g_strjoinv (",", languages);
461 462
  }

463 464
  web_context = webkit_web_context_get_default ();
  webkit_web_context_set_spell_checking_enabled (web_context, value);
465
  webkit_web_context_set_spell_checking_languages (web_context, (const char* const *)languages);
466 467

  g_free (langs);
468
  g_strfreev (languages);
469 470
}

471 472
static const PrefData webkit_pref_entries[] =
  {
473 474
    /* Epiphany font settings */
    { EPHY_PREFS_WEB_SCHEMA,
475
      EPHY_PREFS_WEB_SERIF_FONT,
476 477 478 479
      "default-font-size",
      webkit_pref_callback_font_size },
    { EPHY_PREFS_WEB_SCHEMA,
      EPHY_PREFS_WEB_MONOSPACE_FONT,
480
      "default-monospace-font-size",
481
      webkit_pref_callback_font_size },
482
    { EPHY_PREFS_WEB_SCHEMA,
483
      EPHY_PREFS_WEB_SERIF_FONT,
484 485 486 487 488 489 490 491
      "default-font-family",
      webkit_pref_callback_font_family },
    { EPHY_PREFS_WEB_SCHEMA,
      EPHY_PREFS_WEB_SANS_SERIF_FONT,
      "sans-serif-font-family",
      webkit_pref_callback_font_family },
    { EPHY_PREFS_WEB_SCHEMA,
      EPHY_PREFS_WEB_MONOSPACE_FONT,
492
      "monospace-font-family",
493
      webkit_pref_callback_font_family },
494 495 496 497 498 499 500 501 502
    { EPHY_PREFS_WEB_SCHEMA,
      EPHY_PREFS_WEB_SERIF_FONT,
      "serif-font-family",
      webkit_pref_callback_font_family },

    { EPHY_PREFS_WEB_SCHEMA,
      EPHY_PREFS_WEB_USE_GNOME_FONTS,
      NULL,
      webkit_pref_callback_gnome_fonts },
503 504 505 506 507

    { EPHY_PREFS_WEB_SCHEMA,
      EPHY_PREFS_WEB_ENABLE_SPELL_CHECKING,
      NULL,
      webkit_pref_callback_enable_spell_checking },
508

509 510
    { EPHY_PREFS_WEB_SCHEMA,
      EPHY_PREFS_WEB_ENABLE_USER_CSS,
511
      "user-stylesheet-uri",
512
      webkit_pref_callback_user_stylesheet },
513 514
    { EPHY_PREFS_WEB_SCHEMA,
      EPHY_PREFS_WEB_LANGUAGE,
515
      "accept-language",
516
      webkit_pref_callback_accept_languages },
517 518
    { EPHY_PREFS_SCHEMA,
      EPHY_PREFS_USER_AGENT,
Vincent Untz's avatar
Vincent Untz committed
519
      "user-agent",
520
      webkit_pref_callback_user_agent },
521 522
    { EPHY_PREFS_WEB_SCHEMA,
      EPHY_PREFS_WEB_COOKIES_POLICY,
523
      "accept-policy",
524
      webkit_pref_callback_cookie_accept_policy },
525 526
  };

527
void
528
ephy_embed_prefs_init (void)
529
{
530
  int i;
531 532 533 534 535 536 537 538 539
  web_view_group = webkit_web_view_group_new ("Ephy WebView Group");
  webkit_settings = webkit_web_view_group_get_settings (web_view_group);

  g_object_set (webkit_settings,
                "enable-developer-extras", TRUE,
                "enable-fullscreen", TRUE,
                "enable-site-specific-quirks", TRUE,
                "enable-dns-prefetching", TRUE,
                NULL);
540

541
  for (i = 0; i < G_N_ELEMENTS (webkit_pref_entries); i++) {
542 543 544 545 546
    GSettings *settings;
    char *key;

    settings = ephy_settings_get (webkit_pref_entries[i].schema);
    key = g_strconcat ("changed::", webkit_pref_entries[i].key, NULL);
547

548 549 550 551
    webkit_pref_entries[i].callback (settings,
                                     webkit_pref_entries[i].key,
                                     webkit_pref_entries[i].webkit_pref);

552
    g_signal_connect (settings, key,
553
                      G_CALLBACK (webkit_pref_entries[i].callback),
554 555
                      webkit_pref_entries[i].webkit_pref);
    g_free (key);
556
  }
557 558 559

  g_settings_bind (EPHY_SETTINGS_WEB,
                   EPHY_PREFS_WEB_ENABLE_JAVASCRIPT,
560
                   webkit_settings, ENABLE_SCRIPTS_SETTING,
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
                   G_SETTINGS_BIND_GET);
  g_settings_bind (EPHY_SETTINGS_MAIN,
                   EPHY_PREFS_ENABLE_CARET_BROWSING,
                   webkit_settings, "enable-caret-browsing",
                   G_SETTINGS_BIND_GET);
  g_settings_bind (EPHY_SETTINGS_WEB,
                   EPHY_PREFS_WEB_ENABLE_POPUPS,
                   webkit_settings, "javascript-can-open-windows-automatically",
                   G_SETTINGS_BIND_GET);
  g_settings_bind (EPHY_SETTINGS_WEB,
                   EPHY_PREFS_WEB_ENABLE_PLUGINS,
                   webkit_settings, "enable-plugins",
                   G_SETTINGS_BIND_GET);
  g_settings_bind (EPHY_SETTINGS_WEB,
                   EPHY_PREFS_WEB_FONT_MIN_SIZE,
576
                   webkit_settings, "minimum-font-size",
577 578 579
                   G_SETTINGS_BIND_GET);
  g_settings_bind (EPHY_SETTINGS_WEB,
                   EPHY_PREFS_WEB_DEFAULT_ENCODING,
580
                   webkit_settings, DEFAULT_ENCODING_SETTING,
581
                   G_SETTINGS_BIND_GET);
582 583 584 585
  g_settings_bind (EPHY_SETTINGS_WEB,
                   EPHY_PREFS_WEB_ENABLE_WEBGL,
                   webkit_settings, "enable-webgl",
                   G_SETTINGS_BIND_GET);
586 587 588 589
  g_settings_bind (EPHY_SETTINGS_WEB,
                   EPHY_PREFS_WEB_ENABLE_WEBAUDIO,
                   webkit_settings, "enable-webaudio",
                   G_SETTINGS_BIND_GET);
590 591 592 593
  g_settings_bind (EPHY_SETTINGS_MAIN,
                   EPHY_PREFS_ENABLE_SMOOTH_SCROLLING,
                   webkit_settings, "enable-smooth-scrolling",
                   G_SETTINGS_BIND_GET);
594 595 596
}

void
597
ephy_embed_prefs_shutdown (void)
598
{
599
  g_object_unref (web_view_group);
600 601
}

602 603 604 605 606
WebKitWebViewGroup *
ephy_embed_prefs_get_web_view_group (void)
{
  return web_view_group;
}