summaryrefslogtreecommitdiff
path: root/src/ephy-lockdown.c
blob: 171919b43f5adf2f290d40afb2ccd1fbdec253dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 *  Copyright © 2000, 2001, 2002, 2003, 2004 Marco Pesenti Gritti
 *  Copyright © 2003, 2004, 2005 Christian Persch
 *  Copyright © 2010, 2012 Igalia S.L.
 *
 *  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, see <http://www.gnu.org/licenses/>.
 */

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

#include "ephy-action-helper.h"
#include "ephy-debug.h"
#include "ephy-embed-container.h"
#include "ephy-embed-shell.h"
#include "ephy-embed-utils.h"
#include "ephy-prefs.h"
#include "ephy-private.h"
#include "ephy-settings.h"

#include <gtk/gtk.h>

#define LOCKDOWN_FLAG 1 << 8

struct _EphyLockdown {
  GObject parent_instance;
};

G_DEFINE_TYPE (EphyLockdown, ephy_lockdown, G_TYPE_OBJECT)

static void
arbitrary_url_cb (GSettings  *settings,
                  const char *key,
                  EphyWindow *window)
{
  EphyEmbed *embed;
  const char *address;

  /* Restore the real web page address when disabling entry */
  if (g_settings_get_boolean (settings, key)) {
    embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (window));
    /* embed is NULL on startup */
    if (embed == NULL)
      return;

    address = ephy_web_view_get_display_address (ephy_embed_get_web_view (embed));
    ephy_window_set_location (window, address);
    ephy_web_view_set_typed_address (ephy_embed_get_web_view (embed), NULL);
  }
}

static void
fullscreen_cb (GSettings  *settings,
               const char *key,
               EphyWindow *window)
{
  if (g_settings_get_boolean (settings, key))
    gtk_window_fullscreen (GTK_WINDOW (window));
  else
    gtk_window_unfullscreen (GTK_WINDOW (window));
}

typedef struct {
  const char *key;
  const char *action;
  const char *prop;
} BindAction;

static const BindAction app_actions[] = {
  { EPHY_PREFS_LOCKDOWN_FULLSCREEN, "new-window", "enabled" },
  { EPHY_PREFS_LOCKDOWN_FULLSCREEN, "new-incognito", "enabled" }
};

static const BindAction window_actions[] = {
  { EPHY_PREFS_LOCKDOWN_ARBITRARY_URL, "location", "enabled"},

  { EPHY_PREFS_LOCKDOWN_BOOKMARK_EDITING, "bookmark-page", "enabled" },

  { EPHY_PREFS_LOCKDOWN_FULLSCREEN, "fullscreen", "enabled" },

  { EPHY_PREFS_LOCKDOWN_PRINTING, "print", "enabled" },

  { EPHY_PREFS_LOCKDOWN_SAVE_TO_DISK, "save-as", "enabled" }
};

static const BindAction popup_actions[] = {
  { EPHY_PREFS_LOCKDOWN_SAVE_TO_DISK, "download-link-as", "enabled" },
  { EPHY_PREFS_LOCKDOWN_SAVE_TO_DISK, "save-image-as", "enabled" },
  { EPHY_PREFS_LOCKDOWN_BOOKMARK_EDITING, "context-bookmark-page", "enabled" },

  { EPHY_PREFS_LOCKDOWN_FULLSCREEN, "open-link-in-new-window", "enabled" }
};

static const BindAction tab_actions[] = {
  { EPHY_PREFS_LOCKDOWN_FULLSCREEN, "detach", "enabled" }
};

static const BindAction toolbar_actions[] = {
  { EPHY_PREFS_LOCKDOWN_HISTORY, "navigation-back", "enabled" },
  { EPHY_PREFS_LOCKDOWN_HISTORY, "navigation-forward", "enabled" }
};

static gboolean
sensitive_get_mapping (GValue   *value,
                       GVariant *variant,
                       gpointer  data)
{
  GAction *action;
  gboolean active, before, after;

  action = G_ACTION (data);
  active = g_variant_get_boolean (variant);

  before = g_action_get_enabled (action);
  ephy_action_change_sensitivity_flags (G_SIMPLE_ACTION (action), LOCKDOWN_FLAG, active);
  after = g_action_get_enabled (action);

  /* Set (GAction::enabled) to the value in GSettings _only if_
   * the LOCKDOWN_FLAG had some real effect in the GAction */
  g_value_set_boolean (value, (before != after) ? after : before);

  return TRUE;
}

static void
bind_settings_and_actions (GSettings        *settings,
                           GActionGroup     *action_group,
                           const BindAction *actions,
                           int               actions_n)
{
  int i;

  for (i = 0; i < actions_n; i++) {
    GAction *action;

    action = g_action_map_lookup_action (G_ACTION_MAP (action_group),
                                         actions[i].action);

    /* We need a custom get_mapping for 'enabled'
     * properties, see usage of
     * ephy_action_change_sensitivity_flags in
     * ephy-window.c. */
    g_settings_bind_with_mapping (settings, actions[i].key,
                                  action, actions[i].prop,
                                  G_SETTINGS_BIND_GET,
                                  sensitive_get_mapping,
                                  NULL,
                                  action, NULL);
  }
}

static void
bind_location_controller (GSettings              *settings,
                          EphyLocationController *controller)
{
  g_settings_bind (settings, EPHY_PREFS_LOCKDOWN_ARBITRARY_URL,
                   controller, "editable",
                   G_SETTINGS_BIND_GET |
                   G_SETTINGS_BIND_INVERT_BOOLEAN);
}

static void
window_added_cb (GtkApplication *application,
                 GtkWindow      *window,
                 EphyLockdown   *lockdown)
{
  GActionGroup *action_group;
  GAction *action;
  GSettings *settings;
  EphyLocationController *location_controller;

  if (!EPHY_IS_WINDOW (window))
    return;

  g_signal_connect (EPHY_SETTINGS_LOCKDOWN,
                    "changed::" EPHY_PREFS_LOCKDOWN_FULLSCREEN,
                    G_CALLBACK (fullscreen_cb), window);
  g_signal_connect (EPHY_SETTINGS_LOCKDOWN,
                    "changed::" EPHY_PREFS_LOCKDOWN_ARBITRARY_URL,
                    G_CALLBACK (arbitrary_url_cb), window);

  /* Trigger an initial state on these elements. */
  fullscreen_cb (EPHY_SETTINGS_LOCKDOWN,
                 EPHY_PREFS_LOCKDOWN_FULLSCREEN, EPHY_WINDOW (window));
  arbitrary_url_cb (EPHY_SETTINGS_LOCKDOWN,
                    EPHY_PREFS_LOCKDOWN_ARBITRARY_URL, EPHY_WINDOW (window));

  action_group = G_ACTION_GROUP (G_APPLICATION (gtk_window_get_application (GTK_WINDOW (window))));
  bind_settings_and_actions (EPHY_SETTINGS_LOCKDOWN,
                             action_group, app_actions,
                             G_N_ELEMENTS (app_actions));

  action_group = gtk_widget_get_action_group (GTK_WIDGET (window),
                                              "win");
  bind_settings_and_actions (EPHY_SETTINGS_LOCKDOWN,
                             action_group,
                             window_actions,
                             G_N_ELEMENTS (window_actions));

  action_group = gtk_widget_get_action_group (GTK_WIDGET (window),
                                              "tab");
  bind_settings_and_actions (EPHY_SETTINGS_LOCKDOWN,
                             action_group,
                             tab_actions,
                             G_N_ELEMENTS (tab_actions));

  action_group = gtk_widget_get_action_group (GTK_WIDGET (window),
                                              "toolbar");
  bind_settings_and_actions (EPHY_SETTINGS_LOCKDOWN,
                             action_group,
                             toolbar_actions,
                             G_N_ELEMENTS (toolbar_actions));

  action_group = gtk_widget_get_action_group (GTK_WIDGET (window),
                                              "popup");
  bind_settings_and_actions (EPHY_SETTINGS_LOCKDOWN,
                             action_group, popup_actions,
                             G_N_ELEMENTS (popup_actions));

  action = g_action_map_lookup_action (G_ACTION_MAP (action_group),
                                       "set-image-as-background");
  settings = ephy_settings_get ("org.gnome.desktop.background");
  g_settings_bind_writable (settings, "picture-filename",
                            action, "enabled", FALSE);

  if (ephy_embed_shell_get_mode (ephy_embed_shell_get_default ()) != EPHY_EMBED_SHELL_MODE_APPLICATION) {
    location_controller = ephy_window_get_location_controller (EPHY_WINDOW (window));
    bind_location_controller (EPHY_SETTINGS_LOCKDOWN, location_controller);
  }
}

static void
ephy_lockdown_init (EphyLockdown *lockdown)
{
  EphyShell *shell;

  LOG ("EphyLockdown initialising");

  shell = ephy_shell_get_default ();

  g_signal_connect (shell, "window-added",
                    G_CALLBACK (window_added_cb), lockdown);
}

static void
ephy_lockdown_class_init (EphyLockdownClass *klass)
{
}