~noskcaj/ubuntu/wily/epiphany-browser/merge

« back to all changes in this revision

Viewing changes to src/ephy-lockdown.c

  • Committer: Package Import Robot
  • Author(s): Gustavo Noronha Silva
  • Date: 2013-03-20 20:45:41 UTC
  • mto: (120.1.2 experimental) (1.10.1) (105.1.14 sid)
  • mto: This revision was merged to the branch mainline in revision 121.
  • Revision ID: package-import@ubuntu.com-20130320204541-0t2adqjzvpflkni2
Tags: upstream-3.7.91
Import upstream version 3.7.91

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 
1
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
2
/*
3
3
 *  Copyright © 2000, 2001, 2002, 2003, 2004 Marco Pesenti Gritti
4
4
 *  Copyright © 2003, 2004, 2005 Christian Persch
5
 
 *  Copyright © 2010 Igalia S.L.
 
5
 *  Copyright © 2010, 2012 Igalia S.L.
6
6
 *
7
7
 *  This program is free software; you can redistribute it and/or modify
8
8
 *  it under the terms of the GNU General Public License as published by
28
28
#include "ephy-embed-container.h"
29
29
#include "ephy-embed-shell.h"
30
30
#include "ephy-embed-utils.h"
31
 
#include "ephy-extension.h"
32
31
#include "ephy-prefs.h"
33
32
#include "ephy-private.h"
34
33
#include "ephy-settings.h"
35
 
#include "ephy-web-view.h"
36
34
 
37
35
#include <gtk/gtk.h>
38
 
#include <string.h>
39
36
 
40
37
#define LOCKDOWN_FLAG 1 << 8
41
38
 
42
 
static void ephy_lockdown_iface_init (EphyExtensionIface *iface);
43
 
 
44
39
static int
45
40
find_name (GtkActionGroup *action_group,
46
 
           const char *name)
 
41
           const char *name)
47
42
{
48
 
        return strcmp (gtk_action_group_get_name (action_group), name);
 
43
  return g_strcmp0 (gtk_action_group_get_name (action_group), name);
49
44
}
50
45
 
51
46
static GtkActionGroup *
52
47
find_action_group (GtkUIManager *manager,
53
 
                   const char *name)
 
48
                   const char *name)
54
49
{
55
 
        GList *list, *element;
56
 
 
57
 
        list = gtk_ui_manager_get_action_groups (manager);
58
 
        element = g_list_find_custom (list, name, (GCompareFunc) find_name);
59
 
        g_return_val_if_fail (element != NULL, NULL);
60
 
 
61
 
        return GTK_ACTION_GROUP (element->data);
 
50
  GList *list, *element;
 
51
 
 
52
  list = gtk_ui_manager_get_action_groups (manager);
 
53
  element = g_list_find_custom (list, name, (GCompareFunc) find_name);
 
54
  g_return_val_if_fail (element != NULL, NULL);
 
55
 
 
56
  return GTK_ACTION_GROUP (element->data);
62
57
}
63
58
 
64
59
static void
65
60
arbitrary_url_cb (GSettings *settings,
66
 
                  char *key,
67
 
                  EphyWindow *window)
 
61
                  char *key,
 
62
                  EphyWindow *window)
68
63
{
69
 
        EphyEmbed *embed;
70
 
        const char *address;
71
 
 
72
 
        /* Restore the real web page address when disabling entry */
73
 
        if (g_settings_get_boolean (settings, key))
74
 
        {
75
 
                embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (window));
76
 
                /* embed is NULL on startup */
77
 
                if (embed == NULL)
78
 
                        return;
79
 
 
80
 
                address = ephy_web_view_get_address (ephy_embed_get_web_view (embed));
81
 
                ephy_window_set_location (window, address);
82
 
                ephy_web_view_set_typed_address (ephy_embed_get_web_view (embed), NULL);
83
 
        }
 
64
  EphyEmbed *embed;
 
65
  const char *address;
 
66
 
 
67
  /* Restore the real web page address when disabling entry */
 
68
  if (g_settings_get_boolean (settings, key)) {
 
69
    embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (window));
 
70
    /* embed is NULL on startup */
 
71
    if (embed == NULL)
 
72
      return;
 
73
 
 
74
    address = ephy_web_view_get_address (ephy_embed_get_web_view (embed));
 
75
    ephy_window_set_location (window, address);
 
76
    ephy_web_view_set_typed_address (ephy_embed_get_web_view (embed), NULL);
 
77
  }
84
78
}
85
79
 
86
80
static void
87
81
fullscreen_cb (GSettings *settings,
88
 
               char *key,
89
 
               EphyWindow *window)
 
82
               char *key,
 
83
               EphyWindow *window)
90
84
{
91
 
        if (g_settings_get_boolean (settings, key))
92
 
                gtk_window_fullscreen (GTK_WINDOW (window));
93
 
        else
94
 
                gtk_window_unfullscreen (GTK_WINDOW (window));
 
85
  if (g_settings_get_boolean (settings, key))
 
86
    gtk_window_fullscreen (GTK_WINDOW (window));
 
87
  else
 
88
    gtk_window_unfullscreen (GTK_WINDOW (window));
95
89
}
96
90
 
97
91
typedef struct {
98
 
        char *key;
99
 
        char *action;
100
 
        char *prop;
 
92
  char *key;
 
93
  char *action;
 
94
  char *prop;
101
95
} BindAction;
102
96
 
103
97
static const BindAction window_actions[] = {
104
 
        { EPHY_PREFS_LOCKDOWN_PRINTING, "FilePrint", "sensitive" },
105
 
 
106
 
        { EPHY_PREFS_LOCKDOWN_BOOKMARK_EDITING, "FileBookmarkPage", "sensitive" },
107
 
 
108
 
        { EPHY_PREFS_LOCKDOWN_ARBITRARY_URL, "GoLocation", "sensitive" },
109
 
        { EPHY_PREFS_LOCKDOWN_SAVE_TO_DISK, "FileSaveAs", "sensitive" },
110
 
 
111
 
        { EPHY_PREFS_LOCKDOWN_FULLSCREEN, "ViewFullscreen", "sensitive" },
112
 
        { EPHY_PREFS_LOCKDOWN_FULLSCREEN, "TabsDetach", "sensitive" }
 
98
  { EPHY_PREFS_LOCKDOWN_PRINTING, "FilePrint", "sensitive" },
 
99
 
 
100
  { EPHY_PREFS_LOCKDOWN_BOOKMARK_EDITING, "FileBookmarkPage", "sensitive" },
 
101
 
 
102
  { EPHY_PREFS_LOCKDOWN_ARBITRARY_URL, "GoLocation", "sensitive" },
 
103
  { EPHY_PREFS_LOCKDOWN_SAVE_TO_DISK, "FileSaveAs", "sensitive" },
 
104
 
 
105
  { EPHY_PREFS_LOCKDOWN_FULLSCREEN, "ViewFullscreen", "sensitive" },
 
106
  { EPHY_PREFS_LOCKDOWN_FULLSCREEN, "TabsDetach", "sensitive" }
113
107
};
114
108
 
115
109
static const BindAction popup_actions[] = {
116
 
        { EPHY_PREFS_LOCKDOWN_SAVE_TO_DISK, "DownloadLink", "sensitive" },
117
 
        { EPHY_PREFS_LOCKDOWN_SAVE_TO_DISK, "DownloadLinkAs", "sensitive" },
118
 
        { EPHY_PREFS_LOCKDOWN_SAVE_TO_DISK, "SaveImageAs", "sensitive" },
119
 
        { EPHY_PREFS_LOCKDOWN_SAVE_TO_DISK, "OpenImage", "sensitive" },
120
 
        { EPHY_PREFS_LOCKDOWN_BOOKMARK_EDITING, "BookmarkLink", "sensitive" }
 
110
  { EPHY_PREFS_LOCKDOWN_SAVE_TO_DISK, "DownloadLink", "sensitive" },
 
111
  { EPHY_PREFS_LOCKDOWN_SAVE_TO_DISK, "DownloadLinkAs", "sensitive" },
 
112
  { EPHY_PREFS_LOCKDOWN_SAVE_TO_DISK, "SaveImageAs", "sensitive" },
 
113
  { EPHY_PREFS_LOCKDOWN_SAVE_TO_DISK, "OpenImage", "sensitive" },
 
114
  { EPHY_PREFS_LOCKDOWN_BOOKMARK_EDITING, "BookmarkLink", "sensitive" }
121
115
};
122
116
 
123
117
static const BindAction special_toolbar_actions[] = {
124
 
        { EPHY_PREFS_LOCKDOWN_HISTORY, "NavigationBack", "visible" },
125
 
        { EPHY_PREFS_LOCKDOWN_HISTORY, "NavigationBack", "sensitive" },
126
 
        { EPHY_PREFS_LOCKDOWN_HISTORY, "NavigationForward", "visible" },
127
 
        { EPHY_PREFS_LOCKDOWN_HISTORY, "NavigationForward", "sensitive" },
 
118
  { EPHY_PREFS_LOCKDOWN_HISTORY, "NavigationBack", "visible" },
 
119
  { EPHY_PREFS_LOCKDOWN_HISTORY, "NavigationBack", "sensitive" },
 
120
  { EPHY_PREFS_LOCKDOWN_HISTORY, "NavigationForward", "visible" },
 
121
  { EPHY_PREFS_LOCKDOWN_HISTORY, "NavigationForward", "sensitive" },
128
122
};
129
123
 
130
124
static gboolean
131
125
sensitive_get_mapping (GValue *value,
132
 
                       GVariant *variant,
133
 
                       gpointer data)
 
126
                       GVariant *variant,
 
127
                       gpointer data)
134
128
{
135
 
        GtkAction *action;
136
 
        gboolean active, before, after;
137
 
 
138
 
        action = GTK_ACTION (data);
139
 
        active = g_variant_get_boolean (variant);
140
 
 
141
 
        before = gtk_action_get_sensitive (action);
142
 
        ephy_action_change_sensitivity_flags (action, LOCKDOWN_FLAG, active);
143
 
        after = gtk_action_get_sensitive (action);
144
 
 
145
 
        /* Set (GtkAction::sensitive) to the value in GSettings _only if_
146
 
         * the LOCKDOWN_FLAG had some real effect in the GtkAction */
147
 
        g_value_set_boolean (value, (before != after) ? after : before);
148
 
 
149
 
        return TRUE;
 
129
  GtkAction *action;
 
130
  gboolean active, before, after;
 
131
 
 
132
  action = GTK_ACTION (data);
 
133
  active = g_variant_get_boolean (variant);
 
134
 
 
135
  before = gtk_action_get_sensitive (action);
 
136
  ephy_action_change_sensitivity_flags (action, LOCKDOWN_FLAG, active);
 
137
  after = gtk_action_get_sensitive (action);
 
138
 
 
139
  /* Set (GtkAction::sensitive) to the value in GSettings _only if_
 
140
   * the LOCKDOWN_FLAG had some real effect in the GtkAction */
 
141
  g_value_set_boolean (value, (before != after) ? after : before);
 
142
 
 
143
  return TRUE;
150
144
}
151
145
 
152
146
static void
153
147
bind_settings_and_actions (GSettings *settings,
154
 
                           GtkActionGroup *action_group,
155
 
                           const BindAction *actions,
156
 
                           int actions_n)
 
148
                           GtkActionGroup *action_group,
 
149
                           const BindAction *actions,
 
150
                           int actions_n)
157
151
{
158
 
        int i;
159
 
 
160
 
        for (i = 0; i < actions_n; i++)
161
 
        {
162
 
                GtkAction *action;
163
 
 
164
 
                action = gtk_action_group_get_action (action_group,
165
 
                                                      actions[i].action);
166
 
 
167
 
                if (g_strcmp0 (actions[i].prop, "visible") == 0)
168
 
                {
169
 
                        g_settings_bind (settings, actions[i].key,
170
 
                                         action, actions[i].prop,
171
 
                                         G_SETTINGS_BIND_GET |
172
 
                                         G_SETTINGS_BIND_INVERT_BOOLEAN);
173
 
                }
174
 
                else
175
 
                {
176
 
                        /* We need a custom get_mapping for 'sensitive'
177
 
                         * properties, see usage of
178
 
                         * ephy_action_change_sensitivity_flags in
179
 
                         * ephy-window.c. */
180
 
                        g_settings_bind_with_mapping (settings, actions[i].key,
181
 
                                                      action, actions[i].prop,
182
 
                                                      G_SETTINGS_BIND_GET,
183
 
                                                      sensitive_get_mapping,
184
 
                                                      NULL,
185
 
                                                      action, NULL);
186
 
                }
187
 
        }
 
152
  int i;
 
153
 
 
154
  for (i = 0; i < actions_n; i++) {
 
155
    GtkAction *action;
 
156
 
 
157
    action = gtk_action_group_get_action (action_group,
 
158
                  actions[i].action);
 
159
 
 
160
    if (g_strcmp0 (actions[i].prop, "visible") == 0) {
 
161
      g_settings_bind (settings, actions[i].key,
 
162
                       action, actions[i].prop,
 
163
                       G_SETTINGS_BIND_GET |
 
164
                       G_SETTINGS_BIND_INVERT_BOOLEAN);
 
165
    } else {
 
166
      /* We need a custom get_mapping for 'sensitive'
 
167
       * properties, see usage of
 
168
       * ephy_action_change_sensitivity_flags in
 
169
       * ephy-window.c. */
 
170
      g_settings_bind_with_mapping (settings, actions[i].key,
 
171
                                    action, actions[i].prop,
 
172
                                    G_SETTINGS_BIND_GET,
 
173
                                    sensitive_get_mapping,
 
174
                                    NULL,
 
175
                                    action, NULL);
 
176
    }
 
177
  }
188
178
}
189
179
 
190
180
static void
191
181
bind_location_controller (GSettings *settings,
192
 
                          EphyLocationController *controller)
 
182
                          EphyLocationController *controller)
193
183
{
194
 
        g_settings_bind (settings, EPHY_PREFS_LOCKDOWN_ARBITRARY_URL,
195
 
                         controller, "editable",
196
 
                         G_SETTINGS_BIND_GET |
197
 
                         G_SETTINGS_BIND_INVERT_BOOLEAN);
 
184
  g_settings_bind (settings, EPHY_PREFS_LOCKDOWN_ARBITRARY_URL,
 
185
                   controller, "editable",
 
186
                   G_SETTINGS_BIND_GET |
 
187
                   G_SETTINGS_BIND_INVERT_BOOLEAN);
198
188
}
199
189
 
200
190
static void
201
 
impl_attach_window (EphyExtension *extension,
202
 
                    EphyWindow *window)
 
191
window_added_cb (GtkApplication *application,
 
192
                 GtkWindow *window,
 
193
                 EphyLockdown *lockdown)
203
194
{
204
 
        GtkUIManager *manager;
205
 
        GtkActionGroup *action_group;
206
 
        GtkAction *action;
207
 
        GSettings *settings;
208
 
        EphyLocationController *location_controller;
209
 
 
210
 
        g_signal_connect (EPHY_SETTINGS_LOCKDOWN,
211
 
                          "changed::" EPHY_PREFS_LOCKDOWN_FULLSCREEN,
212
 
                          G_CALLBACK (fullscreen_cb), window);
213
 
        g_signal_connect (EPHY_SETTINGS_LOCKDOWN,
214
 
                          "changed::" EPHY_PREFS_LOCKDOWN_ARBITRARY_URL,
215
 
                          G_CALLBACK (arbitrary_url_cb), window);
216
 
 
217
 
        /* Trigger an initial state on these elements. */
218
 
        fullscreen_cb (EPHY_SETTINGS_LOCKDOWN,
219
 
                       EPHY_PREFS_LOCKDOWN_FULLSCREEN, window);
220
 
        arbitrary_url_cb (EPHY_SETTINGS_LOCKDOWN,
221
 
                          EPHY_PREFS_LOCKDOWN_ARBITRARY_URL, window);
222
 
 
223
 
        manager = GTK_UI_MANAGER (ephy_window_get_ui_manager (window));
224
 
 
225
 
        action_group = find_action_group (manager, "WindowActions");
226
 
        bind_settings_and_actions (EPHY_SETTINGS_LOCKDOWN,
227
 
                                   action_group, window_actions,
228
 
                                   G_N_ELEMENTS (window_actions));
229
 
 
230
 
        action_group = find_action_group (manager, "PopupsActions");
231
 
        bind_settings_and_actions (EPHY_SETTINGS_LOCKDOWN,
232
 
                                   action_group, popup_actions,
233
 
                                   G_N_ELEMENTS (popup_actions));
234
 
 
235
 
        action = gtk_action_group_get_action (action_group,
236
 
                                              "SetImageAsBackground");
237
 
        settings = ephy_settings_get ("org.gnome.desktop.background");
238
 
        g_settings_bind_writable (settings, "picture-filename",
239
 
                                  action, "sensitive", FALSE);
240
 
 
241
 
        action_group = find_action_group (manager, "SpecialToolbarActions");
242
 
        bind_settings_and_actions (EPHY_SETTINGS_LOCKDOWN,
243
 
                                   action_group, special_toolbar_actions,
244
 
                                   G_N_ELEMENTS (special_toolbar_actions));
245
 
 
246
 
        location_controller = ephy_window_get_location_controller (window);
247
 
        bind_location_controller (EPHY_SETTINGS_LOCKDOWN, location_controller);
 
195
  GtkUIManager *manager;
 
196
  GtkActionGroup *action_group;
 
197
  GtkAction *action;
 
198
  GSettings *settings;
 
199
  EphyLocationController *location_controller;
 
200
 
 
201
  if (!EPHY_IS_WINDOW (window))
 
202
    return;
 
203
 
 
204
  g_signal_connect (EPHY_SETTINGS_LOCKDOWN,
 
205
                    "changed::" EPHY_PREFS_LOCKDOWN_FULLSCREEN,
 
206
                    G_CALLBACK (fullscreen_cb), window);
 
207
  g_signal_connect (EPHY_SETTINGS_LOCKDOWN,
 
208
                    "changed::" EPHY_PREFS_LOCKDOWN_ARBITRARY_URL,
 
209
                    G_CALLBACK (arbitrary_url_cb), window);
 
210
 
 
211
  /* Trigger an initial state on these elements. */
 
212
  fullscreen_cb (EPHY_SETTINGS_LOCKDOWN,
 
213
                 EPHY_PREFS_LOCKDOWN_FULLSCREEN, EPHY_WINDOW (window));
 
214
  arbitrary_url_cb (EPHY_SETTINGS_LOCKDOWN,
 
215
                    EPHY_PREFS_LOCKDOWN_ARBITRARY_URL, EPHY_WINDOW (window));
 
216
 
 
217
  manager = GTK_UI_MANAGER (ephy_window_get_ui_manager (EPHY_WINDOW (window)));
 
218
 
 
219
  action_group = find_action_group (manager, "WindowActions");
 
220
  bind_settings_and_actions (EPHY_SETTINGS_LOCKDOWN,
 
221
                             action_group, window_actions,
 
222
                             G_N_ELEMENTS (window_actions));
 
223
 
 
224
  action_group = find_action_group (manager, "PopupsActions");
 
225
  bind_settings_and_actions (EPHY_SETTINGS_LOCKDOWN,
 
226
                             action_group, popup_actions,
 
227
                             G_N_ELEMENTS (popup_actions));
 
228
 
 
229
  action = gtk_action_group_get_action (action_group,
 
230
                                        "SetImageAsBackground");
 
231
  settings = ephy_settings_get ("org.gnome.desktop.background");
 
232
  g_settings_bind_writable (settings, "picture-filename",
 
233
                            action, "sensitive", FALSE);
 
234
 
 
235
  action_group = find_action_group (manager, "SpecialToolbarActions");
 
236
  bind_settings_and_actions (EPHY_SETTINGS_LOCKDOWN,
 
237
                             action_group, special_toolbar_actions,
 
238
                             G_N_ELEMENTS (special_toolbar_actions));
 
239
  
 
240
  location_controller = ephy_window_get_location_controller (EPHY_WINDOW (window));
 
241
  bind_location_controller (EPHY_SETTINGS_LOCKDOWN, location_controller);
248
242
}
 
243
 
 
244
G_DEFINE_TYPE (EphyLockdown, ephy_lockdown, G_TYPE_OBJECT)
249
245
 
250
246
static void
251
247
ephy_lockdown_init (EphyLockdown *lockdown)
252
248
{
253
 
        LOG ("EphyLockdown initialising");
254
 
}
255
 
 
256
 
G_DEFINE_TYPE_WITH_CODE (EphyLockdown, ephy_lockdown, G_TYPE_OBJECT,
257
 
                         G_IMPLEMENT_INTERFACE (EPHY_TYPE_EXTENSION,
258
 
                                                ephy_lockdown_iface_init))
259
 
 
260
 
static void
261
 
ephy_lockdown_iface_init (EphyExtensionIface *iface)
262
 
{
263
 
        iface->attach_window = impl_attach_window;
 
249
  EphyShell *shell;
 
250
 
 
251
  LOG ("EphyLockdown initialising");
 
252
 
 
253
  shell = ephy_shell_get_default ();
 
254
 
 
255
  g_signal_connect (shell, "window-added",
 
256
                    G_CALLBACK (window_added_cb), lockdown);
264
257
}
265
258
 
266
259
static void