summaryrefslogtreecommitdiff
path: root/midori/midori-historycompletion.vala
blob: 27926b03b30c619f6b59606b81540ae1046ff135 (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
/*
 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 HistoryCompletion : Completion {
        public HistoryDatabase? database = null;

        public HistoryCompletion () {
            GLib.Object (description: _("Bookmarks and History"));
        }

        public override void prepare (GLib.Object app) {
            try {
                database = new HistoryDatabase (app);
            }
            catch (Error error) {
                warning (error.message);
            }
        }

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

        public override bool can_action (string action) {
            return false;
        }

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

            List<HistoryItem> items = yield database.list_by_count_with_bookmarks (text, max_items, cancellable);
            if (items == null)
                return null;

            var suggestions = new List<Suggestion> ();
            foreach (var item in items) {
                if (item is Midori.HistoryWebsite) {
                    var website = item as Midori.HistoryWebsite;
                    suggestions.append (new Suggestion (website.uri, website.title,
                        true, null, yield Midori.URI.get_icon_fallback (website.uri, null, cancellable), this.position));
                }
                else if (item is Midori.HistorySearch) {
                    var search = item as Midori.HistorySearch;
                    suggestions.append (new Suggestion (search.uri, search.title + "\n" + search.uri,
                        false, "gray", yield Midori.URI.get_icon_fallback (search.uri, null, cancellable), this.position));
                }
                else
                    warn_if_reached ();
            }

            if (cancellable.is_cancelled ())
                return null;

            return suggestions;
        }
    }
}