~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
/*
 Copyright (C) 2012-2013 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 Midori {
    public class ViewCompletion : Completion {
        GLib.Object app;
        GLib.Object browsers;

        public ViewCompletion () {
            GLib.Object (description: _("Open tabs"));
        }

        public override void prepare (GLib.Object app) {
            this.app = app;
            app.get ("browsers", out browsers);
        }

        public override bool can_complete (string text) {
            return browsers != null;
        }

        public override bool can_action (string action) {
            return action == "complete:more/views";
        }

        public override async List<Suggestion>? complete (string text, string? action, Cancellable cancellable) {
            return_val_if_fail (browsers != null, null);

            unowned List<GLib.Object> browsers_list = Katze.array_peek_items (browsers);
            var suggestions = new List<Suggestion> ();
            uint n = 0;
            string key = text.casefold ();
            foreach (var browser in browsers_list) {
                /* FIXME multiple windows */
                GLib.Object current_browser;
                app.get ("browser", out current_browser);
                if (browser != current_browser)
                    continue;

                GLib.Object items;
                browser.get ("proxy-items", out items);
                unowned List<GLib.Object> items_list = Katze.array_peek_items (items);

                foreach (var item in items_list) {
                    string? uri, title;
                    item.get ("uri", out uri);
                    item.get ("name", out title);

                    /* Omit speed dial and blank pages */
                    if (uri == "about:dial" || uri == "about:blank")
                        continue;

                    if (uri == null) {
                        warning ("item.uri != null");
                        continue;
                    }
                    if (title == null) {
                        warning ("item.name != null");
                        continue;
                    }
                    if (!(key in uri.casefold () || key in title.casefold ()))
                        continue;

                    Gdk.Pixbuf? icon = Midori.Paths.get_icon (uri, null);
                    /* FIXME: Theming? Win32? */
                    string background = "gray";
                    var suggestion = new Suggestion (uri, title, true, background, icon, this.position);
                    suggestions.append (suggestion);

                    n++;
                    if (n == 3 && action == null) {
                        suggestion = new Suggestion ("complete:more/views", _("More open tabs…"), false, background);
                        suggestion.action = true;
                        suggestions.append (suggestion);
                        break;
                    }

                    uint src = Idle.add (complete.callback);
                    yield;
                    Source.remove (src);
                }
            }

            if (cancellable.is_cancelled ())
                return null;

            return suggestions;
        }
    }
}