Active Topics

 


Reply
Thread Tools
Posts: 246 | Thanked: 2,574 times | Joined on Jan 2010 @ Egypt, Cairo
#1
So I've been trying to access contacts using libosso-abook-1.0 and python

I've been following this tutorial PyMaemo/Accessing APIs Without Python Bindings
and I'm only trying to get the number of contacts, or get GList of them. I just want anything that shows it works (except for the example in the tutorial because it simple works and I need ways other than a widget).

Code:
import ctypes
import osso
import sys
import gtk
from pygobject import *

###### Initialization, got that from the tutorial #####

osso_ctx = osso.Context("test_abook", "0.1")
osso_abook = ctypes.CDLL('libosso-abook-1.0.so.0')
argv_type = ctypes.c_char_p * len(sys.argv)
argv = argv_type(*sys.argv)
argc = ctypes.c_int(len(sys.argv))
osso_abook.osso_abook_init(ctypes.byref(argc), ctypes.byref(argv), hash(osso_ctx))

##################################################


############### Get default aggregator ################

agg = osso_abook.osso_abook_aggregator_get_default(None)
print agg   ### Prints a memory address ### 

##################################################

############ Try to get count ##################

count = osso_abook.osso_abook_aggregator_get_master_contact_count(agg)

print count ### Prints 0 ###

############################################


########## Try to get glists of master and roster ########

masterList = osso_abook.osso_abook_aggregator_list_master_contacts(agg)
print masterList  ### Prints 0 ###
rosterList = osso_abook.osso_abook_aggregator_list_roster_contacts(agg)
print rosterList   ### Prints 0 ###

###########################################
So in that code, why do I receive a zero in any function that I try to call, except for getting the default aggregator? I've seen codes in C , that type cast the the default aggregator into OssoABookAggregator, because that function returns OssoABookRoster *

Code:
roster = (OssoABookAggregator *) osso_abook_aggregator_get_default (NULL);
Is that why my code is not working? If so, I don't know how to do that in python and ctypes and I would appreciate if someone points me out to how to do that.

Attached is the pygobject class
Attached Files
File Type: zip pygobject.py.zip (527 Bytes, 91 views)
 
Posts: 86 | Thanked: 362 times | Joined on Dec 2007 @ Paris / France
#2
Have got the same problem in C code.
osso_abook_aggregator_get_master_contact_count always gives me 0, osso_abook_aggregator_list_master_contacts returns an empty list, etc.

So maybe your problem is not caused by python or ctype.

Could you give links to C code you've seen ? (I can't find any working C sample for getting a list of contacts)

Edit: working C code example here http://pastebin.com/BfvS2wC0. Should be easy to do the same with ctype.

Last edited by eber42; 2010-04-10 at 16:39. Reason: Adding link to working C program
 
Posts: 56 | Thanked: 31 times | Joined on Jul 2008 @ Austria
#3
Code:
#!/usr/bin/env python

import ctypes
import sys
import gtk # needed, otherwise abook won't work.
import glib
import osso

def from_GList(native, B_free_list, B_unref_items):
	"""
		given a native C GList, returns the items of the list.
	"""
	class _GList(ctypes.Structure):
		_fields_ = [("data", ctypes.c_void_p),
		            ("next", ctypes.c_void_p)]
	try:
		l = native
		while l:
			l = _GList.from_address(l)
			yield l.data
			l = l.next
	finally:
		l = native if B_unref_items else None
		while l:
			l = _GList.from_address(l)
			c_gobject.g_object_unref(l.data)
			l = l.next
		if B_free_list:
			c_glib.g_list_free(native)

# ctypes wrapper for pygobject_new(), based on code snippet from
# http://faq.pygtk.org/index.py?req=sh...=faq23.041.htp
class _PyGObject_Functions(ctypes.Structure):
    _fields_ = [
        ('register_class',
            ctypes.PYFUNCTYPE(ctypes.c_void_p, ctypes.c_char_p,
            ctypes.c_int, ctypes.py_object, ctypes.py_object)),
        ('register_wrapper',
            ctypes.PYFUNCTYPE(ctypes.c_void_p, ctypes.py_object)),
        ('register_sinkfunc',
            ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
        ('lookupclass',
            ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_int)),
        ('newgobj',
            ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
    ]

class PyGObjectCAPI(object):
    def __init__(self):
        import gobject
        py_obj = ctypes.py_object(gobject._PyGObject_API)
        addr = ctypes.pythonapi.PyCObject_AsVoidPtr(py_obj)
        self._api = _PyGObject_Functions.from_address(addr)
 
    def pygobject_new(self, addr):
        return self._api.newgobj(addr)

c_pyapi = PyGObjectCAPI()


context = osso.Context("test_abook", "0.1")

c_gobject = ctypes.CDLL("libgobject-2.0.so.0")
c_glib = ctypes.CDLL("libglib-2.0.so.0")
book = ctypes.CDLL("libosso-abook-1.0.so.0")
argv_type = ctypes.c_char_p * len(sys.argv)
argv = argv_type(*sys.argv)
argc = ctypes.c_int(len(sys.argv))
book.osso_abook_init(ctypes.byref(argc), ctypes.byref(argv), hash(context))
c_None = ctypes.c_void_p()
roster = book.osso_abook_aggregator_new(c_None, c_None) # TODO GError
book.osso_abook_roster_start(roster)
book.osso_abook_waitable_run(roster, c_glib.g_main_context_default(), None) # TODO GError
if not book.osso_abook_waitable_is_ready(roster, c_None): # TODO GError
	print("whoops")
	sys.exit(1)
for i in range(2):
	contacts = map(c_pyapi.pygobject_new, from_GList(book.osso_abook_aggregator_list_master_contacts(roster), True, False))
	book.osso_abook_contact_get_display_name.restype = ctypes.c_char_p
	for contact in contacts:
		#print(contact)
		#print(dir(contact))
		print(contact.props.display_name)
		# note do not free resulting string.
		#print(book.osso_abook_contact_get_display_name(contact))
 
Reply


 
Forum Jump


All times are GMT. The time now is 05:36.