summaryrefslogtreecommitdiff
path: root/src/contacts-shell-search-provider.vala
blob: 2940db46175bea5819c347fd0f5e7b19e63d966a (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
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
/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
using Gee;

[DBus (name = "org.gnome.Shell.SearchProvider2")]
public class Contacts.SearchProvider : Object {
  SearchProviderApp app;
  Store store;
  Gee.HashMap<string, Contact> contacts_map;
  private uint next_id;

  public SearchProvider (SearchProviderApp app) {
    this.app = app;
    ensure_eds_accounts ();
    store = new Store ();
    contacts_map = new Gee.HashMap<string, Contact> ();
    next_id = 0;

    store.changed.connect ( (c) => {
	contacts_map.set(c.get_data<string> ("search-id"), c);
      });
    store.added.connect ( (c) => {
	var id = next_id++.to_string ();
	c.set_data ("search-id", id);
	contacts_map.set(id, c);
      });
    store.removed.connect ( (c) => {
	contacts_map.unset(c.get_data<string> ("search-id"));
      });
  }

  private static int compare_contacts (Contact a, Contact b) {
    int a_prio = a.is_main ? 0 : -1;
    int b_prio = b.is_main ? 0 : -1;

    if (a_prio > b_prio)
      return -1;
    if (a_prio < b_prio)
      return 1;

    if (is_set (a.display_name) && is_set (b.display_name))
      return a.display_name.collate (b.display_name);

    // Sort empty names last
    if (is_set (a.display_name))
      return -1;
    if (is_set (b.display_name))
      return 1;

    return 0;
  }

  private async string[] do_search (string[] terms) {
    app.hold ();
    string[] normalized_terms =
    Utils.canonicalize_for_search (string.joinv(" ", terms)).split(" ");

    var matches = new ArrayList<Contact> ();
    foreach (var c in store.get_contacts ()) {
      if (c.is_hidden)
	continue;

      if (c.contains_strings (normalized_terms))
	matches.add (c);
    }

    matches.sort((CompareDataFunc<Contact>) compare_contacts);

    var results = new string[matches.size];
    for (int i = 0; i < matches.size; i++)
      results[i] = matches[i].get_data ("search-id");
    app.release ();
    return results;
  }

  public async string[] GetInitialResultSet (string[] terms) {
    return yield do_search (terms);
  }

  public async string[] GetSubsearchResultSet (string[] previous_results,
					       string[] new_terms) {
    return yield do_search (new_terms);
  }

  private async HashTable<string, Variant>[] get_metas (owned string[] ids) {
    app.hold ();
    var results = new ArrayList<HashTable> ();
    foreach (var id in ids) {
      var contact = contacts_map.get (id);

      if (contact == null)
        continue;

      var meta = new HashTable<string, Variant> (str_hash, str_equal);
      meta.insert ("id", new Variant.string (id));

      meta.insert ("name", new Variant.string (contact.display_name));

      if (contact.serializable_avatar_icon != null)
        meta.insert ("gicon", new Variant.string (contact.serializable_avatar_icon.to_string ()));
      else if (contact.avatar_icon_data != null)
        meta.insert ("icon-data", contact.avatar_icon_data);
      else
        meta.insert ("gicon", new Variant.string (new ThemedIcon ("avatar-default").to_string ()));
      results.add (meta);
    }
    app.release ();
    return results.to_array ();
  }

  public async HashTable<string, Variant>[] GetResultMetas (string[] ids) {
    return yield get_metas (ids);
  }

  public void ActivateResult (string search_id, string[] terms, uint32 timestamp) {
    app.hold ();

    var contact = contacts_map.get (search_id);

    if (contact == null) {
      app.release ();
      return;
    }

    string id = contact.individual.id;
    try {
      if (!Process.spawn_command_line_async ("gnome-contacts -i " + id))
        stderr.printf ("Failed to launch contact with id '%s'\n", id);
    } catch (SpawnError e) {
      stderr.printf ("Failed to launch contact with id '%s'\n", id);
    }

    app.release ();
  }

  public void LaunchSearch (string[] terms, uint32 timestamp) {
    app.hold ();

    debug ("LaunchSearch (%s)", string.joinv (", ", terms));

    try {
      string[] args = {};
      args += "gnome-contacts";
      args += "--search";
      args += string.joinv (" ", terms);
      if (!Process.spawn_async (null, args, null, SpawnFlags.SEARCH_PATH, null, null))
	stderr.printf ("Failed to launch Contacts for search\n");
    } catch (SpawnError error) {
      stderr.printf ("Failed to launch Contacts for search\n");
    }

    app.release ();
  }
}

public class Contacts.SearchProviderApp : GLib.Application {
  public SearchProviderApp () {
    Object (application_id: "org.gnome.Contacts.SearchProvider",
            flags: ApplicationFlags.IS_SERVICE,
            inactivity_timeout: 10000);
  }

  public override bool dbus_register (GLib.DBusConnection connection, string object_path) {
    try {
      connection.register_object (object_path, new SearchProvider (this));
    } catch (IOError error) {
      stderr.printf ("Could not register service: %s", error.message);
      quit ();
    }
    return true;
  }

  public override void startup () {
    if (Environment.get_variable ("CONTACTS_SEARCH_PROVIDER_PERSIST") != null)
      hold ();
    base.startup ();
  }
}

int main () {
  return new Contacts.SearchProviderApp ().run ();
}