PyMaemo/Accessing APIs without Python bindings/More examples

(Searching contacts by phone number: use <source>)
 
(3 intermediate revisions not shown)
Line 3: Line 3:
This page contains some other example code using ctypes. Note that they are not complete, and assume some code already shown on the [[../|main article]].
This page contains some other example code using ctypes. Note that they are not complete, and assume some code already shown on the [[../|main article]].
-
== Searching contacts by phone number ==
+
== Searching for contacts by phone number ==
-
Searching contacts by phone number can be done using the [http://maemo.org/api_refs/5.0/5.0-final/libosso-abook/libosso-abook-osso-abook-util.html#osso-abook-query-phone-number osso_abook_query_phone_number()] and [http://maemo.org/api_refs/5.0/5.0-final/libebook/EBook.html#e-book-get-contacts e_book_get_contacts()] functions:
+
Searching for contacts by phone number can be done using the [http://maemo.org/api_refs/5.0/5.0-final/libosso-abook/OssoABookAggregator.html#osso-abook-aggregator-find-contacts-for-phone-number osso_abook_aggregator_find_contacts_for_phone_number()] function:
 +
<source lang="python">
 +
def aggregator_ready_cb(aggregator, error, user_data):
 +
    contacts = osso_abook.osso_abook_aggregator_find_contacts_for_phone_number(aggregator, phone_number, 1)
 +
    if contacts:
 +
        print "Contacts with phone number %s:" % phone_number
 +
        for i in glist(contacts):
 +
            contact_get_display_name = osso_abook.osso_abook_contact_get_display_name
 +
            contact_get_display_name.restype = ctypes.c_char_p
 +
            print contact_get_display_name(i)
 +
        glib.g_list_free(contacts)
 +
    else:
 +
        print "No contacts found with phone number %s." % phone_number
-
ebook = ctypes.CDLL('libebook-1.2.so.5')
+
    gtk.main_quit()
-
+
-
err = ctypes.c_void_p()
+
-
c_book = ebook.e_book_new_default_addressbook(ctypes.byref(err))
+
-
if not ebook.e_book_open(c_book, 1, 0):
+
-
    print >>sys.stderr, "Couldn't load addressbook."
+
-
    sys.exit(1)
+
-
c_query = osso_abook.osso_abook_query_phone_number("12345678", 0)
+
-
+
-
contacts = ctypes.c_void_p()
+
-
if not ebook.e_book_get_contacts(c_book, c_query, ctypes.byref(contacts), err):
+
-
    print >>sys.stderr, "Couldn't get query results."
+
-
    sys.exit(1)
+
-
The "contacts" is a GList which can be iterated over as explained [[../#Accessing Items in a GList|here]].
+
aggregator = osso_abook.osso_abook_aggregator_get_default(0)
 +
wait_cb_type = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p)
 +
# Keep prototype object reference so it is not destroyed
 +
cb_proto = wait_cb_type(aggregator_ready_cb)
 +
osso_abook.osso_abook_waitable_call_when_ready(aggregator, cb_proto, 0, 0)
 +
gtk.main()
 +
</source>
[[Category:Python]]
[[Category:Python]]

Latest revision as of 12:15, 22 June 2010

[edit] More examples

This page contains some other example code using ctypes. Note that they are not complete, and assume some code already shown on the main article.

[edit] Searching for contacts by phone number

Searching for contacts by phone number can be done using the osso_abook_aggregator_find_contacts_for_phone_number() function:

def aggregator_ready_cb(aggregator, error, user_data):
    contacts = osso_abook.osso_abook_aggregator_find_contacts_for_phone_number(aggregator, phone_number, 1)
    if contacts:
        print "Contacts with phone number %s:" % phone_number
        for i in glist(contacts):
            contact_get_display_name = osso_abook.osso_abook_contact_get_display_name
            contact_get_display_name.restype = ctypes.c_char_p
            print contact_get_display_name(i)
        glib.g_list_free(contacts)
    else:
        print "No contacts found with phone number %s." % phone_number
 
    gtk.main_quit()
 
aggregator = osso_abook.osso_abook_aggregator_get_default(0)
wait_cb_type = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p)
# Keep prototype object reference so it is not destroyed
cb_proto = wait_cb_type(aggregator_ready_cb)
osso_abook.osso_abook_waitable_call_when_ready(aggregator, cb_proto, 0, 0)
 
gtk.main()