Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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