~midori/midori/cmake-make-dist

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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
   Copyright (C) 2013 André Stösel <andre@stoesel.de>
   Copyright (C) 2014 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.
*/

namespace About {
    private abstract class Page : GLib.Object {
        public abstract string uri { get; set; }
        public abstract void get_contents (Midori.View view, string uri);
        protected void load_html (Midori.View view, string content, string uri) {
            #if HAVE_WEBKIT2 
                view.web_view.load_html (content, uri);
            #else
                view.web_view.load_html_string (content, uri);
            #endif
        }
    }

    private class Widgets : Page {
        public override string uri { get; set; default = "about:widgets"; }
        public override void get_contents (Midori.View view, string uri) {
            string[] widgets = {
                "<input value=\"demo\"%s>",
                "<p><input type=\"password\" value=\"demo\"%s></p>",
                "<p><input type=\"checkbox\" value=\"demo\"%s> demo</p>",
                "<p><input type=\"radio\" value=\"demo\"%s> demo</p>",
                "<p><select%s><option>foo bar</option><option selected>spam eggs</option></select></p>",
                "<p><select%s size=\"3\"><option>foo bar</option><option selected>spam eggs</option></select></p>",
                "<p><input type=\"file\"%s></p>",
                "<p><input type=\"file\" multiple%s></p>",
                "<input type=\"button\" value=\"demo\"%s>",
                "<p><input type=\"email\" value=\"user@localhost.com\"%s></p>",
                "<input type=\"url\" value=\"http://www.example.com\"%s>",
                "<input type=\"tel\" value=\"+1 234 567 890\" pattern=\"^[0+][1-9 /-]*$\"%s>",
                "<input type=\"number\" min=1 max=9 step=1 value=\"4\"%s>",
                "<input type=\"range\" min=1 max=9 step=1 value=\"4\"%s>",
                "<input type=\"date\" min=1990-01-01 max=2010-01-01%s>",
                "<input type=\"search\" placeholder=\"demo\"%s>",
                "<textarea%s>Lorem ipsum doloret sit amet…</textarea>",
                "<input type=\"color\" value=\"#d1eeb9\"%s>",
                "<progress min=1 max=9 value=4 %s></progress>",
                "<keygen type=\"rsa\" challenge=\"235ldahlae983dadfar\"%s>",
                "<p><input type=\"reset\"%s></p>",
                "<input type=\"submit\"%s>"
            };

            string content = """<html>
                <head>
                    <style>
                        .fallback::-webkit-slider-thumb,
                        .fallback, .fallback::-webkit-file-upload-button {
                            -webkit-appearance: none !important;
                        }
                        .column {
                            display:inline-block; vertical-align:top;
                            width:25%;
                            margin-right:1%;
                        }
                    </style>
                    <title>%s</title>
                </head>
                <body>
                    <h1>%s</h1>
                    <div class="column">%s</div>
                    <div class="column">%s</div>
                    <div class="column">%s</div>
                    <p><a href="http://example.com" target="wp" onclick="javascript:window.open('http://example.com','wp','width=320, height=240, toolbar=false'); return false;">Popup window</a></p>
                </body>
            </html>""";

            string[] widget_options = {"", " disabled", " class=\"fallback\""};
            string[] widgets_formated = {"", "", ""};

            for (int i = 0; i < widget_options.length && i < widgets_formated.length; i++) {
                for (int j = 0; j < widgets.length; j++) {
                    widgets_formated[i] = widgets_formated[i] + widgets[j].printf (widget_options[i]);
                }
            }

            this.load_html (view, content.printf (uri, uri, widgets_formated[0], widgets_formated[1], widgets_formated[2]), uri);
        }
    }

    private class Version : Page {
        public override string uri { get; set; }
        private GLib.HashTable<string, Page> about_pages;

        public Version (string alias, HashTable<string, Page> about_pages) {
            this.uri = alias;
            this.about_pages = about_pages;
        }

        private string list_about_uris () {
            string links = "";
            foreach (unowned string uri in about_pages.get_keys ())
                links = links + "<a href=\"%s\">%s</a> &nbsp;".printf (uri, uri);
            return "<p>%s</p>".printf (links);
        }

        public override void get_contents (Midori.View view, string uri) {
            string contents = """<html>
                <head><title>about:version</title></head>
                <body>
                    <h1>a<span style="position: absolute; left: -1000px; top: -1000px">lias a=b; echo Copy carefully #</span>bout:version</h1>
                    <p>%s</p>
                    <img src="res://logo-shade.png" style="position: absolute; right: 15px; bottom: 15px; z-index: -9;">
                    <table>
                        <tr><td>Command line %s</td></tr>
                        %s
                        <tr><td>Platform %s %s %s</td></tr>
                        <tr><td>Identification %s</td></tr>
                        %s
                    </table>
                    <table>
                        %s
                    </table>
                    %s
                </body>
            </html>""";

            GLib.StringBuilder versions = new GLib.StringBuilder ();
            Midori.View.list_versions (versions, true);

            string ident; 
            unowned string architecture;
            unowned string platform;
            unowned string sys_name = Midori.WebSettings.get_system_name (out architecture, out platform);
            view.settings.get ("user-agent", out ident);

            GLib.StringBuilder video_formats = new GLib.StringBuilder ();
            view.list_video_formats (video_formats, true);

            GLib.StringBuilder ns_plugins = new GLib.StringBuilder ();
            view.list_plugins (ns_plugins, true);

            /* TODO: list active extensions */

            this.load_html (view, contents.printf (
                _("Version numbers in brackets show the version used at runtime."),
                Midori.Paths.get_command_line_str (true),
                versions.str,
                platform, sys_name, architecture != null ? architecture : "",
                ident,
                video_formats.str,
                ns_plugins.str,
                this.list_about_uris ()
            ), uri);
        }
    }

    private class Private : Page {
        public override string uri { get; set; default = "about:private"; }
        public override void get_contents (Midori.View view, string uri) {
            this.load_html (view, """<html dir="ltr">
                <head>
                    <title>%s</title>
                    <link rel="stylesheet" type="text/css" href="res://about.css">
                </head>
                <body>
                    <img id="logo" src="res://logo-shade.png" />
                    <div id="main" style="background-image: url(stock://dialog/gtk-dialog-info);">
                    <div id="text">
                    <h1>%s</h1>
                    <p class="message">%s</p><ul class=" suggestions"><li>%s</li><li>%s</li><li>%s</li></ul>
                    <p class="message">%s</p><ul class=" suggestions"><li>%s</li><li>%s</li><li>%s</li><li>%s</li></ul>
                    </div><br style="clear: both"></div>
                </body>
            </html>""".printf (
                _("Private Browsing"), _("Private Browsing"),
                _("Midori doesn't store any personal data:"),
                _("No history or web cookies are being saved."),
                _("Extensions are disabled."),
                _("HTML5 storage, local database and application caches are disabled."),
                _("Midori prevents websites from tracking the user:"),
                _("Referrer URLs are stripped down to the hostname."),
                _("DNS prefetching is disabled."),
                _("The language and timezone are not revealed to websites."),
                _("Flash and other Netscape plugins cannot be listed by websites.")
            ), uri);
        }
    }

    private class Paths : Page {
        public override string uri { get; set; default = "about:paths"; }
        public override void get_contents (Midori.View view, string uri) {
            string res_dir = Midori.Paths.get_res_filename ("about.css");
            string lib_dir = Midori.Paths.get_lib_path (PACKAGE_NAME);
            this.load_html (view, """<html>
                <body>
                    <h1>%s</h1>
                    <p>config: <code>%s</code></p>
                    <p>res: <code>%s</code></p>
                    <p>data: <code>%s/%s</code></p>
                    <p>lib: <code>%s</code></p>
                    <p>cache: <code>%s</code></p>
                    <p>tmp: <code>%s</code></p>
                </body>
            </html>""".printf (
                uri, Midori.Paths.get_config_dir_for_reading (), res_dir,
                Midori.Paths.get_user_data_dir_for_reading (), PACKAGE_NAME,
                lib_dir, Midori.Paths.get_cache_dir_for_reading (), Midori.Paths.get_tmp_dir ()
            ), uri);
        }
    }

    private class Dial : Page {
        public override string uri { get; set; default = "about:dial"; }
        public override void get_contents (Midori.View view, string uri) {
            var browser = Midori.Browser.get_for_widget (view);
            Midori.SpeedDial dial;
            browser.get ("speed-dial", out dial);
            if (dial == null)
                return;
            try {
                this.load_html (view, dial.get_html (), uri);
            } catch (Error error) {
                this.load_html (view, error.message, uri);
            }
        }
    }

    private class Geolocation : Page {
        public override string uri { get; set; default = "about:geolocation"; }
        public override void get_contents (Midori.View view, string uri) {
            this.load_html (view, """<html>
                <body>
                    <a href="http://dev.w3.org/geo/api/spec-source.html" id="method"></a>
                    <span id="locationInfo"><noscript>No Geolocation without Javascript</noscript></span>
                    <script>
                        function displayLocation (position) {
                            var geouri = 'geo:' + position.coords.latitude + ',' + position.coords.longitude + ',' + position.coords.altitude + ',u=' + position.coords.accuracy;
                            document.getElementById('locationInfo').innerHTML = '<a href="' + geouri + '">' + geouri + '</a><br><code>'
                                + ' timestamp: ' + position.timestamp
                                + ' latitude: ' + position.coords.latitude
                                + ' longitude: ' + position.coords.longitude
                                + ' altitude: ' + position.coords.altitude + '<br>'
                                + ' accuracy: ' + position.coords.accuracy
                                + ' altitudeAccuracy: ' + position.coords.altitudeAccuracy
                                + ' heading: ' + position.coords.heading
                                + ' speed: ' + position.coords.speed
                                + '</code>';
                            }
                            function handleError (error) {
                                var errorMessage = '<b>' + ['Unknown error', 'Permission denied', 'Position failed', 'Timed out'][error.code] + '</b>';
                                if (error.code == 3) document.getElementById('locationInfo').innerHTML += (' ' + errorMessage);
                                else document.getElementById('locationInfo').innerHTML = errorMessage;
                            }
                            if (navigator.geolocation) {
                                var options = { enableHighAccuracy: true, timeout: 60000, maximumAge: "Infinite" };
                                if (navigator.geolocation.watchPosition) {
                                    document.getElementById('method').innerHTML = '<code>geolocation.watchPosition</code>:';
                                    navigator.geolocation.watchPosition(displayLocation, handleError, options);
                                } else {
                                    document.getElementById('method').innerHTML = '<code>geolocation.getCurrentPosition</code>:';
                                    navigator.geolocation.getCurrentPosition(displayLocation, handleError);
                                }
                            } else
                                document.getElementById('locationInfo').innerHTML = 'Geolocation unavailable';
                    </script>
                </body>
            </html>""", uri);
        }
    }

    private class Redirects : Page {
        public override string uri { get; set; }
        private string property;
        public Redirects (string alias, string property) {
            this.uri = alias;
            this.property = property;
        }
        public override void get_contents (Midori.View view, string uri) {
            string new_uri = uri;
            view.settings.get (property, out new_uri);
            if (uri == "about:search")
                new_uri = Midori.URI.for_search (new_uri, "");
            view.set_uri (new_uri);
        }
    }

    private class Manager : Midori.Extension {
        private GLib.HashTable<string, Page>? about_pages;

        private void register (Page page) {
            this.about_pages.insert (page.uri, page);
        }

        private bool about_content (Midori.View view, string uri) {
            unowned Page? page = this.about_pages.get (uri);
            if (page != null) {
                page.get_contents (view, uri);
                return true;
            }

            return false;
        }

        private void tab_added (Midori.Browser browser, Midori.View view) {
            view.about_content.connect (this.about_content);
        }

        private void tab_removed (Midori.Browser browser, Midori.View view) {
            view.about_content.disconnect (this.about_content);
        }

        private void browser_added (Midori.Browser browser) {
            foreach (Midori.View tab in browser.get_tabs ()) {
                this.tab_added (browser, tab);
            }
            browser.add_tab.connect (this.tab_added);
            browser.remove_tab.connect (this.tab_removed);
        }

        private void browser_removed (Midori.Browser browser) {
            foreach (Midori.View tab in browser.get_tabs ()) {
                this.tab_removed (browser, tab);
            }
            browser.add_tab.disconnect (this.tab_added);
            browser.remove_tab.disconnect (this.tab_removed);
        }

        public void activated (Midori.App app) {
            this.about_pages = new GLib.HashTable<string, Page> (GLib.str_hash, GLib.str_equal);
            register (new Widgets ());
            register (new Version ("about:", about_pages));
            register (new Version ("about:version", about_pages));
            register (new Private ());
            register (new Paths ());
            register (new Geolocation ());
            register (new Redirects ("about:new", "tabhome"));
            register (new Redirects ("about:home", "homepage"));
            register (new Redirects ("about:search", "location-entry-search"));
            register (new Dial ());

            foreach (Midori.Browser browser in app.get_browsers ()) {
                this.browser_added (browser);
            }
            app.add_browser.connect (this.browser_added);
        }

        public void deactivated () {
            Midori.App app = this.get_app ();
            foreach (Midori.Browser browser in app.get_browsers ()) {
                this.browser_removed (browser);
            }
            app.add_browser.disconnect (this.browser_added);

            this.about_pages = null;
        }

        internal Manager () {
            GLib.Object (name: "About pages",
                         description: "Internal about: handler",
                         version: "0.1",
                         authors: "André Stösel <andre@stoesel.de>");

            this.activate.connect (this.activated);
            this.deactivate.connect (this.deactivated);
        }
    }
}

public Midori.Extension extension_init () {
    return new About.Manager ();
}