]> git.llucax.com Git - personal/website.git/blob - source/blog/posts/2012/07/02-querying-n900-address-book.rst
Move eventxx submodule to the repo subdir
[personal/website.git] / source / blog / posts / 2012 / 07 / 02-querying-n900-address-book.rst
1 Title: Querying N900 address book
2 Tags: en, python, maemo, n900, osso, addressbook, libebook, evolution, mobile, hacking, c, binding
3
4 Since there is not a lot of information on how to hack Maemo's address book
5 to find some contacts with a mobile phone number, I'll share my findings.
6
7 Since setting up an environment to cross-compile for ARM is a big hassle,
8 I decided to write this small test program in Python, (ab)using the wonderful
9 ctypes module to avoid compiling at all.
10
11 Here is a very small script to use the (sadly proprietary) OSSO Addressbook
12 library::
13
14    # This function get all the names in the address book with mobile phone numbers
15    # and print them. The code is Python but is as similar as C as possible.
16    def get_all_mobiles():
17
18        osso_ctx = osso_initialize("test_abook", "0.1", FALSE)
19        osso_abook_init(argc, argv, hash(osso_ctx))
20
21        roster = osso_abook_aggregator_get_default(NULL)
22        osso_abook_waitable_run(roster, g_main_context_default(), NULL)
23        contacts = osso_abook_aggregator_list_master_contacts(roster)
24
25        for contact in glist(contacts):
26            name = osso_abook_contact_get_display_name(contact)
27            # Somehow hackish way to get the EVC_TEL attributes
28            field = e_contact_field_id("mobile-phone")
29            attrs = e_contact_get_attributes(contact, field)
30            mobiles = []
31            for attr in glist(attrs):
32                types = e_vcard_attribute_get_param(attr, "TYPE")
33                for t in glist(types):
34                    type = ctypes.c_char_p(t).value
35                    # Remove this condition to get all phone numbers
36                    # (not just mobile phones)
37                    if type == "CELL":
38                        mobiles.append(e_vcard_attribute_get_value(attr))
39            if mobiles:
40                print name, mobiles
41
42
43    # Python
44
45    import sys
46    import ctypes
47    # be sure to import gtk before calling osso_abook_init()
48    import gtk
49    import osso
50
51    osso_initialize = osso.Context
52
53    # Dynamic libraries bindings
54    glib = ctypes.CDLL('libglib-2.0.so.0')
55    g_main_context_default = glib.g_main_context_default
56    def glist(addr):
57        class _GList(ctypes.Structure):
58            _fields_ = [('data', ctypes.c_void_p),
59                        ('next', ctypes.c_void_p)]
60        l = addr
61        while l:
62            l = _GList.from_address(l)
63            yield l.data
64            l = l.next
65
66    osso_abook = ctypes.CDLL('libosso-abook-1.0.so.0')
67    osso_abook_init = osso_abook.osso_abook_init
68    osso_abook_aggregator_get_default = osso_abook.osso_abook_aggregator_get_default
69    osso_abook_waitable_run = osso_abook.osso_abook_waitable_run
70    osso_abook_aggregator_list_master_contacts = osso_abook.osso_abook_aggregator_list_master_contacts
71    osso_abook_contact_get_display_name = osso_abook.osso_abook_contact_get_display_name
72    osso_abook_contact_get_display_name.restype = ctypes.c_char_p
73
74    ebook = ctypes.CDLL('libebook-1.2.so.5')
75    e_contact_field_id = ebook.e_contact_field_id
76    e_contact_get_attributes = ebook.e_contact_get_attributes
77    e_vcard_attribute_get_value = ebook.e_vcard_attribute_get_value
78    e_vcard_attribute_get_value.restype = ctypes.c_char_p
79    e_vcard_attribute_get_param = ebook.e_vcard_attribute_get_param
80
81    # argc/argv adaption
82    argv_type = ctypes.c_char_p * len(sys.argv)
83    argv = ctypes.byref(argv_type(*sys.argv))
84    argc = ctypes.byref(ctypes.c_int(len(sys.argv)))
85
86    # C-ish aliases
87    NULL = None
88    FALSE = False
89
90    # Run the test
91    get_all_mobiles()
92
93
94 Here are some useful links I used as reference:
95
96 * http://wiki.maemo.org/PyMaemo/Accessing_APIs_without_Python_bindings
97 * http://wiki.maemo.org/Documentation/Maemo_5_Developer_Guide/Using_Generic_Platform_Components/Using_Address_Book_API
98 * http://maemo.org/api_refs/5.0/5.0-final/libebook/
99 * http://maemo.org/api_refs/5.0/5.0-final/libosso-abook/
100 * https://garage.maemo.org/plugins/scmsvn/viewcvs.php/releases/evolution-data-server/1.4.2.1-20091104/addressbook/libebook-dbus/?root=eds
101 * http://www.developer.nokia.com/Community/Discussion/showthread.php?200914-How-to-print-all-contact-names-in-N900-addressbook-Please-Help
102
103
104 .. vim: set et sw=3 sts=3 :