summaryrefslogtreecommitdiff
path: root/midori/midori-searchcompletion.vala
diff options
context:
space:
mode:
authorChristian Dywan <christian@twotoasts.de>2012-10-05 00:34:35 +0200
committerChristian Dywan <christian@twotoasts.de>2012-10-05 00:34:35 +0200
commit371f4a030c7e87a9d5ed02b567f93a950860916e (patch)
treee09749e354e3c5491256bb6afe64818baa2dea36 /midori/midori-searchcompletion.vala
parent79d8d641db7811c0215c37f01f38e24e65b446f6 (diff)
downloadmidori-371f4a030c7e87a9d5ed02b567f93a950860916e.tar.gz
Introduce Completion API with search and history classes
The new API makes completion independent from the database and uses pluggable, asynchronous backends. No new functionality.
Diffstat (limited to 'midori/midori-searchcompletion.vala')
-rw-r--r--midori/midori-searchcompletion.vala77
1 files changed, 77 insertions, 0 deletions
diff --git a/midori/midori-searchcompletion.vala b/midori/midori-searchcompletion.vala
new file mode 100644
index 00000000..0a5c44ed
--- /dev/null
+++ b/midori/midori-searchcompletion.vala
@@ -0,0 +1,77 @@
+/*
+ Copyright (C) 2012 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 Katze {
+ extern static unowned List<GLib.Object> array_peek_items (GLib.Object array);
+}
+
+namespace Midori {
+ extern static Gdk.Pixbuf? search_action_get_icon (GLib.Object item,
+ Gtk.Widget? widget, out string[] icon_name, bool in_entry);
+
+ public class SearchCompletion : Completion {
+ GLib.Object search_engines;
+
+ public SearchCompletion () {
+ GLib.Object (description: "Search engines");
+ }
+
+ public override void prepare (GLib.Object app) {
+ app.get ("search-engines", out search_engines);
+ }
+
+ public override bool can_complete (string text) {
+ return search_engines != null;
+ }
+
+ public override bool can_action (string action) {
+ return action == "about:search";
+ }
+
+ public override async List<Suggestion>? complete (string text, string? action, Cancellable cancellable) {
+ return_val_if_fail (search_engines != null, null);
+
+ unowned List<GLib.Object> items = Katze.array_peek_items (search_engines);
+ var suggestions = new List<Suggestion> ();
+ uint n = 0;
+ foreach (var item in items) {
+ string uri, title, desc;
+ item.get ("uri", out uri);
+ item.get ("name", out title);
+ item.get ("text", out desc);
+ string search_uri = URI.for_search (uri, text);
+ string search_title = _("Search with %s").printf (title);
+ var icon = Midori.search_action_get_icon (item, null, null, false);
+ string search_desc = search_title + "\n" + desc ?? uri;
+ /* FIXME: Theming? Win32? */
+ string background = "gray";
+ var suggestion = new Suggestion (search_uri, search_desc, false, background, icon);
+ suggestions.append (suggestion);
+
+ n++;
+ if (n == 3 && action == null) {
+ suggestion = new Suggestion ("about:search", _("Search with…"), 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;
+ }
+ }
+}