I've got the following snippet of code that (in the looking glass) gives me a list of contacts I have (managed by gnome-contacts):
// METHOD 1:
const ContactDisplay = imports.ui.contactDisplay;
let csp = new ContactDisplay.ContactSearchProvider();
let contacts = csp.getInitialResultSet(['']);
contacts.length // 120 contacts
// METHOD 2:
const ContactSystem = Shell.ContactSystem;
let cs = ContactSystem.get_default();
let contacts2 = cs.initial_search(['']);
contacts2.length // 120 contacts
This two pieces of code are (as far as I know) equivalent, as ContactSearchProvider
's getInitialResultSet
basically calls the initial_search
method of ContactSystem.get_default()
.
However, when I put these same bits of code into a gnome shell extension (say the enable
method), and I global.log(contacts.length)
or global.log(contacts2.length)
, I always get 0
.
Hence for some reason this code works in looking-glass but not in an extension. Why is this? Are the contacts not loaded at the point the extension is executed? Is the contact search provider somehow not connected up to me? How can I work around it?
I asked this on the gnome-shell mailing list -- it turns out that the extensions are loaded quite early on in the piece, before the a user's contacts have properly been loaded.
Adding a 5-second delay to the code snippet that retrieves the contact list (to give the system a chance to load all the contacts) works a charm.