~midori/midori/trunk

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
261
262
263
264
265
266
267
268
269
270
/*
 Copyright (C) 2008-2011 Christian Dywan <christian@twotoasts.de>

 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.1 of the License, or (at your option) any later version.

 See the file COPYING for the full license text.
*/

#include <midori/midori.h>

typedef struct
{
    const gchar* label;
    gdouble level;
} ZoomLevel;

const ZoomLevel zoom_levels[] =
{
    { "200%", 2.0  },
    { "175%", 1.75 },
    { "150%", 1.5  },
    { "125%", 1.25 },
    { "100%", 1.0  },
    { "50%" , 0.5  },
    { "25%" , 0.25 }
};

static void
statusbar_features_app_add_browser_cb (MidoriApp*       app,
                                       MidoriBrowser*   browser,
                                       MidoriExtension* extension);

static void
statusbar_features_toolbar_notify_toolbar_style_cb (GtkWidget*  toolbar,
                                                    GParamSpec* pspec,
                                                    GtkWidget*  button)
{
    GtkToolbarStyle style = katze_object_get_enum (toolbar, "toolbar-style");
    const gchar* text = g_object_get_data (G_OBJECT (button), "feature-label");
    switch (style)
    {
        case GTK_TOOLBAR_BOTH:
        case GTK_TOOLBAR_BOTH_HORIZ:
            gtk_button_set_label (GTK_BUTTON (button), text);
            gtk_widget_show (gtk_button_get_image (GTK_BUTTON (button)));
            break;
        case GTK_TOOLBAR_TEXT:
            gtk_button_set_label (GTK_BUTTON (button), text);
            gtk_widget_hide (gtk_button_get_image (GTK_BUTTON (button)));
            break;
        case GTK_TOOLBAR_ICONS:
            gtk_button_set_label (GTK_BUTTON (button), "");
            gtk_widget_show (gtk_button_get_image (GTK_BUTTON (button)));
            break;
        default:
            g_assert_not_reached ();
    }
}

static void
statusbar_features_browser_notify_tab_cb (MidoriBrowser* browser,
                                          GParamSpec*    pspec,
                                          GtkWidget*     combobox)
{
    MidoriView* view = MIDORI_VIEW (midori_browser_get_current_tab (browser));
    gchar* text;

    if (view == NULL)
        return;

    text = g_strdup_printf ("%d%%", (gint)(midori_view_get_zoom_level (view) * 100));
    gtk_entry_set_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combobox))), text);
    g_free (text);
}

static void
statusbar_features_deactivate_cb (MidoriExtension* extension,
                                  GtkWidget*       bbox)
{
    MidoriApp* app = midori_extension_get_app (extension);
    MidoriBrowser* browser = midori_browser_get_for_widget (bbox);
    GtkWidget* toolbar = katze_object_get_object (browser, "navigationbar");

    gtk_widget_destroy (bbox);
    g_signal_handlers_disconnect_matched (toolbar, G_SIGNAL_MATCH_FUNC,
        0, -1, NULL, statusbar_features_toolbar_notify_toolbar_style_cb, NULL);
    g_object_unref (toolbar);
    g_signal_handlers_disconnect_by_func (
        extension, statusbar_features_deactivate_cb, bbox);
    g_signal_handlers_disconnect_by_func (
        app, statusbar_features_app_add_browser_cb, extension);
    g_signal_handlers_disconnect_matched (browser, G_SIGNAL_MATCH_FUNC,
        0, -1, NULL, statusbar_features_browser_notify_tab_cb, NULL);
}

static void
statusbar_features_zoom_level_changed_cb (GtkWidget*     combobox,
                                          MidoriBrowser* browser)
{
    MidoriView* view = MIDORI_VIEW (midori_browser_get_current_tab (browser));
    GtkWidget* entry = gtk_bin_get_child (GTK_BIN (combobox));
    const gchar* zoom_level_text = gtk_entry_get_text (GTK_ENTRY (entry));
    gdouble zoom_level = g_ascii_strtod (zoom_level_text, NULL);
    midori_view_set_zoom_level (view, zoom_level / 100.0);
}

GtkWidget*
statusbar_features_property_proxy (MidoriWebSettings* settings,
                                   const gchar*       property,
                                   GtkWidget*         toolbar)
{
    const gchar* kind = NULL;
    GtkWidget* button;
    GtkWidget* image;
    if (!strcmp (property, "auto-load-images")
     || !strcmp (property, "enable-javascript")
     || !strcmp (property, "enable-plugins"))
        kind = "toggle";
    else if (!strcmp (property, "identify-as"))
        kind = "custom-user-agent";
    else if (strstr (property, "font") != NULL)
        kind = "font";
    else if (!strcmp (property, "zoom-level"))
    {
        MidoriBrowser* browser = midori_browser_get_for_widget (toolbar);
        gint i;
        button = gtk_combo_box_text_new_with_entry ();
        gtk_entry_set_width_chars (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (button))), 4);
        for (i = 0; i < G_N_ELEMENTS (zoom_levels); i++)
            gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (button), zoom_levels[i].label);
        g_signal_connect (button, "changed",
            G_CALLBACK (statusbar_features_zoom_level_changed_cb), browser);
        g_signal_connect (browser, "notify::tab",
            G_CALLBACK (statusbar_features_browser_notify_tab_cb), button);
        statusbar_features_browser_notify_tab_cb (browser, NULL, button);
        return button;
    }

    button = katze_property_proxy (settings, property, kind);
    if (GTK_IS_BIN (button))
    {
        GtkWidget* label = gtk_bin_get_child (GTK_BIN (button));
        if (GTK_IS_LABEL (label))
            gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
    }

    if (!strcmp (property, "auto-load-images"))
    {
        g_object_set_data (G_OBJECT (button), "feature-label", _("Images"));
        image = gtk_image_new_from_stock (STOCK_IMAGE, GTK_ICON_SIZE_MENU);
        gtk_button_set_image (GTK_BUTTON (button), image);
        gtk_widget_set_tooltip_text (button, _("Load images automatically"));
        statusbar_features_toolbar_notify_toolbar_style_cb (toolbar, NULL, button);
        g_signal_connect (toolbar, "notify::toolbar-style",
            G_CALLBACK (statusbar_features_toolbar_notify_toolbar_style_cb), button);
    }
    if (!strcmp (property, "enable-javascript"))
    {
        g_object_set_data (G_OBJECT (button), "feature-label", _("Scripts"));
        image = gtk_image_new_from_stock (STOCK_SCRIPT, GTK_ICON_SIZE_MENU);
        gtk_button_set_image (GTK_BUTTON (button), image);
        gtk_widget_set_tooltip_text (button, _("Enable scripts"));
        statusbar_features_toolbar_notify_toolbar_style_cb (toolbar, NULL, button);
        g_signal_connect (toolbar, "notify::toolbar-style",
            G_CALLBACK (statusbar_features_toolbar_notify_toolbar_style_cb), button);
    }
    else if (!strcmp (property, "enable-plugins"))
    {
        if (!midori_web_settings_has_plugin_support ())
            gtk_widget_hide (button);
        g_object_set_data (G_OBJECT (button), "feature-label", _("Netscape plugins"));
        image = gtk_image_new_from_stock (MIDORI_STOCK_PLUGINS, GTK_ICON_SIZE_MENU);
        gtk_button_set_image (GTK_BUTTON (button), image);
        gtk_widget_set_tooltip_text (button, _("Enable Netscape plugins"));
        statusbar_features_toolbar_notify_toolbar_style_cb (toolbar, NULL, button);
        g_signal_connect (toolbar, "notify::toolbar-style",
            G_CALLBACK (statusbar_features_toolbar_notify_toolbar_style_cb), button);
    }
    return button;
}

static void
statusbar_features_app_add_browser_cb (MidoriApp*       app,
                                       MidoriBrowser*   browser,
                                       MidoriExtension* extension)
{
    GtkWidget* statusbar;
    GtkWidget* bbox;
    MidoriWebSettings* settings;
    GtkWidget* toolbar;
    GtkWidget* button;
    gsize i;
    gchar** filters;

    /* FIXME: Monitor each view and modify its settings individually
              instead of merely replicating the global preferences. */

    statusbar = katze_object_get_object (browser, "statusbar");
    bbox = gtk_hbox_new (FALSE, 0);
    settings = midori_browser_get_settings (browser);
    toolbar = katze_object_get_object (browser, "navigationbar");

    filters = midori_extension_get_string_list (extension, "items", NULL);
    if (filters && *filters)
    {
        i = 0;
        while (filters[i] != NULL)
        {
            button = statusbar_features_property_proxy (settings, filters[i], toolbar);
            gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 2);
            i++;
        }
    }
    else
    {
        button = statusbar_features_property_proxy (settings, "auto-load-images", toolbar);
        gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 2);
        button = statusbar_features_property_proxy (settings, "enable-javascript", toolbar);
        gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 2);
        button = statusbar_features_property_proxy (settings, "enable-plugins", toolbar);
        gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 2);
        button = statusbar_features_property_proxy (settings, "identify-as", toolbar);
        gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 2);
        button = statusbar_features_property_proxy (settings, "zoom-level", toolbar);
        gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 2);
    }
    gtk_widget_show_all (bbox);
    gtk_box_pack_end (GTK_BOX (statusbar), bbox, FALSE, FALSE, 3);
    g_object_unref (statusbar);
    g_object_unref (toolbar);

    g_strfreev (filters);
    g_signal_connect (extension, "deactivate",
        G_CALLBACK (statusbar_features_deactivate_cb), bbox);
}

static void
statusbar_features_activate_cb (MidoriExtension* extension,
                                MidoriApp*       app)
{
    KatzeArray* browsers;
    MidoriBrowser* browser;

    browsers = katze_object_get_object (app, "browsers");
    KATZE_ARRAY_FOREACH_ITEM (browser, browsers)
        statusbar_features_app_add_browser_cb (app, browser, extension);
    g_signal_connect (app, "add-browser",
        G_CALLBACK (statusbar_features_app_add_browser_cb), extension);
    g_object_unref (browsers);
}

MidoriExtension*
extension_init (void)
{
    MidoriExtension* extension = g_object_new (MIDORI_TYPE_EXTENSION,
        "name", _("Statusbar Features"),
        "description", _("Easily toggle features on web pages on and off"),
        "version", "0.1" MIDORI_VERSION_SUFFIX,
        "authors", "Christian Dywan <christian@twotoasts.de>",
        NULL);
    midori_extension_install_string_list (extension, "items", NULL, G_MAXSIZE);

    g_signal_connect (extension, "activate",
        G_CALLBACK (statusbar_features_activate_cb), NULL);

    return extension;
}