Getting list of contacts with Gnome-Shell JS interface

匆匆过客 提交于 2020-01-23 17:19:28

问题


I've just started fiddling with writing gnome-shell extensions, and would like to know how to get a list of contacts a user has.

I've tracked down some likely files: gnome-shell/js/ui/contactDisplay.js and gnome-shell/src/shell_contact_system.c.

Now I notice in shell_contact_system.c the following function:

/**
 * shell_contact_system_get_all:
 * @self: A #ShellContactSystem
 *
 * Returns: (transfer none): All individuals
 */
GeeMap *
shell_contact_system_get_all (ShellContactSystem *self)
{
  GeeMap *individuals;

  g_return_val_if_fail (SHELL_IS_CONTACT_SYSTEM (self), NULL);

  individuals = folks_individual_aggregator_get_individuals (self->priv->aggregator);

  return individuals;
}

This suggests that inthe javascript interface I can use function get_all (my belief is reinforced by the fact that the .c file also has a shell_contact_system_get_individual and contactDisplay.js demonstrates the use of Shell.ContactSystem.get_default().get_individual), so I try:

contactSys = Shell.ContactSystem.get_default();
// get contacts
contacts = contactSys.get_all();

It works! (I'm trying it out in Gnome-shell's "looking glass" javascript interpreter).

It appears to be a 'Gobject' so I have no idea what to do with this object to get the names of my contacts out. I notice (from the C code) that this object is a GeeMap *, so looking at the documentation for Gee.Map, I see I can do the following:

contacts.size   // returns 31, which is the number of contacts I have !
contacts.values // is a Gee.Collection
contacts.keys   // is a Gee.Set

I then try to look at contacts.values and contacts.key, noting the to_array() method these have:

contacts.values.to_array()
contacts.keys.to_array()

However when I do this I get an empty array back out: contacts.xxx.to_array().size is 0, yet contacts.size is 31 ?? !!

The same occurs when I attempt to use a contacts.map_iterator() to iterate through the map; it.get_value() and it.get_key() appear to be null.

So, my question is: How can I access the individual elements of contacts?

I do note that the comment for shell_contact_system_get_all says Returns: (transfer none): ... whilst shell_contact_system_get_individual says Returns: (transfer full): ..., and the latter function works (provided I know the ID of the individual I want to look up, which is my problem), whereas the former doesn't. Could this be something to do with it?

Also, as a workaround, I can currently make use of the contactSearchProvider class to achieve this (used by the overview when you intall gnome-contacts where you can type the name of a contact and they'll appear in the overview). I basically search for '' which should return me a list of every contact. It seems roundabout though - I feel I should just be able to use the get_all() method to get all the contacts without having to search on an empty string...

    const ContactDisplay = imports.ui.contactDisplay;
    cds = new ContactDisplay.contactSearchProvider;
    res = cds.getInitialResultSet(['']); 
    // woohoo! res.length is 31, and res[0], res[1], ... are all not null!

Any help would be greatly appreciated (by the way, is there any sort of documentation about for this? I feel extremely inadequate when I stare at the source and try to deduce all the documentation from it!)


回答1:


I believe this is a bug and that you already found where it is: the annotation on the C method should say Returns: (transfer full): in order to give back results to the bindings.

The next logical step would be to report the bug and the patch to their bugzilla, but I need to note that code was removed in gnome 3.6 so I'm skeptical it will be ever fixed.

The contact search code now lives in the gnome-contact app at:

http://git.gnome.org/browse/gnome-contacts



来源:https://stackoverflow.com/questions/10151433/getting-list-of-contacts-with-gnome-shell-js-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!