~ubuntu-branches/ubuntu/trusty/gnome-contacts/trusty

« back to all changes in this revision

Viewing changes to src/contacts-shell-search-provider.vala

  • Committer: Package Import Robot
  • Author(s): Michael Biebl, Jeremy Bicha, Michael Biebl
  • Date: 2013-09-19 18:23:06 UTC
  • mfrom: (1.3.10) (0.3.4 experimental)
  • mto: This revision was merged to the branch mainline in revision 40.
  • Revision ID: package-import@ubuntu.com-20130919182306-rcatwotzg94pr884
Tags: 3.8.3-1
[ Jeremy Bicha ]
* debian/control.in:
  - Drop alternate build-depends on valac-0.18 since it's no longer
    in Debian

[ Michael Biebl ]
* New upstream release.
* Loosen Build-Depends on libgnome-desktop-3-dev, we do not strictly require
  version (>= 3.6.0) which is not yet available in unstable.
* Bump Standards-Version to 3.9.4. No further changes.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using Gee;
 
2
 
 
3
[DBus (name = "org.gnome.Shell.SearchProvider")]
 
4
public class Contacts.SearchProvider : Object {
 
5
  SearchProviderApp app;
 
6
  Store store;
 
7
  Gee.HashMap<string, Contact> contacts_map;
 
8
  private uint next_id;
 
9
 
 
10
  public SearchProvider (SearchProviderApp app) {
 
11
    this.app = app;
 
12
    ensure_eds_accounts ();
 
13
    store = new Store ();
 
14
    contacts_map = new Gee.HashMap<string, Contact> ();
 
15
    next_id = 0;
 
16
 
 
17
    store.changed.connect ( (c) => {
 
18
      contacts_map.set(c.get_data<string> ("search-id"), c);
 
19
    });
 
20
    store.added.connect ( (c) => {
 
21
      var id = next_id++.to_string ();
 
22
      c.set_data ("search-id", id);
 
23
      contacts_map.set(id, c);
 
24
    });
 
25
    store.removed.connect ( (c) => {
 
26
      contacts_map.unset(c.get_data<string> ("search-id"));
 
27
    });
 
28
  }
 
29
 
 
30
  private static int compare_contacts (Contact a, Contact b) {
 
31
    int a_prio = a.is_main ? 0 : -1;
 
32
    int b_prio = b.is_main ? 0 : -1;
 
33
 
 
34
    if (a_prio > b_prio)
 
35
        return -1;
 
36
    if (a_prio < b_prio)
 
37
        return 1;
 
38
 
 
39
    if (is_set (a.display_name) && is_set (b.display_name))
 
40
      return a.display_name.collate (b.display_name);
 
41
 
 
42
    // Sort empty names last
 
43
    if (is_set (a.display_name))
 
44
      return -1;
 
45
    if (is_set (b.display_name))
 
46
      return 1;
 
47
 
 
48
    return 0;
 
49
  }
 
50
 
 
51
  private string[] do_search (string[] terms) {
 
52
    app.hold ();
 
53
    string[] normalized_terms =
 
54
        Utils.canonicalize_for_search (string.joinv(" ", terms)).split(" ");
 
55
 
 
56
    var matches = new ArrayList<Contact> ();
 
57
    foreach (var c in store.get_contacts ()) {
 
58
      if (c.is_hidden)
 
59
          continue;
 
60
 
 
61
      if (c.contains_strings (normalized_terms))
 
62
          matches.add (c);
 
63
    }
 
64
 
 
65
    matches.sort((CompareDataFunc<Contact>) compare_contacts);
 
66
 
 
67
    var results = new string[matches.size];
 
68
    for (int i = 0; i < matches.size; i++)
 
69
        results[i] = matches[i].get_data ("search-id");
 
70
    app.release ();
 
71
    return results;
 
72
  }
 
73
 
 
74
  public string[] GetInitialResultSet (string[] terms) {
 
75
    return do_search (terms);
 
76
  }
 
77
 
 
78
  public string[] GetSubsearchResultSet (string[] previous_results,
 
79
                                         string[] new_terms) {
 
80
    return do_search (new_terms);
 
81
  }
 
82
 
 
83
  public HashTable<string, Variant>[] GetResultMetas (string[] ids) {
 
84
    app.hold ();
 
85
    var results = new ArrayList<HashTable> ();
 
86
    foreach (var id in ids) {
 
87
      var contact = contacts_map.get (id);
 
88
 
 
89
      if (contact == null)
 
90
        continue;
 
91
 
 
92
      var meta = new HashTable<string, Variant> (str_hash, str_equal);
 
93
      meta.insert ("id", new Variant.string (id));
 
94
 
 
95
      meta.insert ("name", new Variant.string (contact.display_name));
 
96
 
 
97
      if (contact.serializable_avatar_icon != null)
 
98
        meta.insert ("gicon", new Variant.string (contact.serializable_avatar_icon.to_string ()));
 
99
      else if (contact.avatar_icon_data != null)
 
100
        meta.insert ("icon-data", contact.avatar_icon_data);
 
101
      else
 
102
        meta.insert ("gicon", new Variant.string (new ThemedIcon ("avatar-default").to_string ()));
 
103
      results.add (meta);
 
104
    }
 
105
    app.release ();
 
106
    return results.to_array ();
 
107
  }
 
108
 
 
109
  public void ActivateResult (string search_id) {
 
110
    app.hold ();
 
111
 
 
112
    var contact = contacts_map.get (search_id);
 
113
 
 
114
    if (contact == null) {
 
115
      app.release ();
 
116
      return;
 
117
    }
 
118
 
 
119
    string id = contact.individual.id;
 
120
    try {
 
121
      if (!Process.spawn_command_line_async ("gnome-contacts -i " + id))
 
122
        stderr.printf ("Failed to launch contact with id '%s'\n", id);
 
123
    } catch (SpawnError e) {
 
124
      stderr.printf ("Failed to launch contact with id '%s'\n", id);
 
125
    }
 
126
 
 
127
    app.release ();
 
128
  }
 
129
}
 
130
 
 
131
public class Contacts.SearchProviderApp : GLib.Application {
 
132
  public SearchProviderApp () {
 
133
    Object (application_id: "org.gnome.Contacts.SearchProvider",
 
134
            flags: ApplicationFlags.IS_SERVICE,
 
135
            inactivity_timeout: 10000);
 
136
  }
 
137
 
 
138
  public override bool dbus_register (GLib.DBusConnection connection, string object_path) {
 
139
    try {
 
140
      connection.register_object (object_path, new SearchProvider (this));
 
141
    } catch (IOError error) {
 
142
      stderr.printf ("Could not register service: %s", error.message);
 
143
      quit ();
 
144
    }
 
145
    return true;
 
146
  }
 
147
 
 
148
  public override void startup () {
 
149
    if (Environment.get_variable ("CONTACTS_SEARCH_PROVIDER_PERSIST") != null)
 
150
      hold ();
 
151
    base.startup ();
 
152
  }
 
153
}
 
154
 
 
155
int main () {
 
156
  return new Contacts.SearchProviderApp ().run ();
 
157
}