summaryrefslogtreecommitdiff
path: root/src/contacts-shell-search-provider.vala
blob: b0e86b4dff3540b9679e678b1425b42e9b18984d (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
/*
 * Copyright (C) 2011 Alexander Larsson <alexl@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using Gee;
using Folks;

[DBus (name = "org.gnome.Shell.SearchProvider2")]
public class Contacts.SearchProvider : Object {
  private SearchProviderApp app;
  private IndividualAggregator aggregator;
  private SimpleQuery query;
  private Variant serialized_fallback_icon;

  public SearchProvider (SearchProviderApp app) {
    // Do this first, since this will be the slowest (and is async anyway)
    this.aggregator = IndividualAggregator.dup ();
    this.aggregator.prepare.begin ();

    this.app = app;
    this.serialized_fallback_icon = new ThemedIcon.from_names ({"avatar-default-symbolic"}).serialize ();;

    var matched_fields = Query.MATCH_FIELDS_NAMES;
    foreach (var field in Query.MATCH_FIELDS_ADDRESSES)
      matched_fields += field;
    this.query = new SimpleQuery ("", matched_fields);

    if (!ensure_eds_accounts (false))
      this.app.quit ();
  }

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

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

  private async string[] do_search (string[] terms) throws Error {
    this.app.hold ();

    // Make the query and search view
    query.query_string = string.joinv(" ", terms);
    var search_view = new SearchView (aggregator, query);
    try {
      yield search_view.prepare ();
    } catch (Error e) {
      error ("Couldn't load SearchView: %s", e.message);
    }
    var results = new string[search_view.individuals.size];
    var i = 0;
    foreach (var individual in search_view.individuals) {
      results[i] = individual.id;
      i++;
    }

    this.app.release ();
    return results;
  }

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

  private async HashTable<string, Variant>[] get_metas (owned string[] ids) throws Error {
    this.app.hold ();

    var results = new ArrayList<HashTable> ();
    foreach (var id in ids) {
      Individual indiv = null;
      try {
        indiv = yield aggregator.look_up_individual (id);
      } catch (Error e) {
        continue;
      }
      if (indiv == null)
        continue;

      var meta = new HashTable<string, Variant> (str_hash, str_equal);
      meta["id"] = new Variant.string (id);
      meta["name"] = new Variant.string (indiv.display_name);
      meta["icon"] = (indiv.avatar != null)? indiv.avatar.serialize () : serialized_fallback_icon;

      // Make a description based the first email address/phone nr/... we can find
      var description = new StringBuilder ();

      var email = Utils.get_first<EmailFieldDetails> (indiv.email_addresses);
      if (email != null && email.value != null && email.value != "")
        description.append (email.value);

      var phone = Utils.get_first<PhoneFieldDetails> (indiv.phone_numbers);
      if (phone != null && phone.value != null && phone.value != "") {
        if (description.len > 0)
          description.append (" / ");
        description.append (phone.value);
      }

      meta["description"] = description.str;

      results.add (meta);
    }
    this.app.release ();
    return results.to_array ();
  }

  public void ActivateResult (string id, string[] terms, uint32 timestamp) throws Error {
    this.app.hold ();

    try {
      Process.spawn_command_line_async ("gnome-contacts -i " + id);
    } catch (SpawnError e) {
      stderr.printf ("Failed to launch contact with id '%s': %s\n.", id, e.message);
    }
    this.app.release ();
  }

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

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

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

    this.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 ();
}