2

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?

2

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.

  • thedailywtf.com/Articles/Too-Much-Sleep.aspx Sleep for synchronization is a very poor solution. – deadalnix Feb 2 '13 at 6:31
  • sure, but can you suggest an alternative in this case? the relevant libraries do not provide signals indicating that contacts have loaded, so it's not like I can connect to the signal with a callback. – mathematical.coffee Feb 3 '13 at 13:10
  • I have to say I never touched that part of the API, so I'm not sure. The hacky way would be finding some side effect of contact initialization and rely on that. For instance by monkey patching a function that is called by the initalization. – deadalnix Feb 3 '13 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.