-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgnc_tree_view_account_ctypes.py
More file actions
323 lines (198 loc) · 9.13 KB
/
Copy pathgnc_tree_view_account_ctypes.py
File metadata and controls
323 lines (198 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import sys
import types
#import gobject
#import gtk
import ctypes
import pdb
import gc
import sw_app_utils
import gnome_utils_ctypes
import glib_ctypes
#import qof_ctypes
import gnucash
import gnucash.gnucash_core_c
from gnucash.gnucash_core_c import string_to_guid as gnucash_core_string_to_guid
import swighelpers
def string_to_new_guid (cls, str):
#pdb.set_trace()
guid = cls()
retval = gnucash_core_string_to_guid(str,guid.instance)
if retval:
return guid
else:
raise Exception("Unable to convert string to GUID")
gnucash.GUID.string_to_guid = classmethod(string_to_new_guid)
from pygobjectcapi import PyGObjectCAPI
#from pygobjectcapi_gi import PyGObjectCAPI
# call like this:
# Cgobject = PyGObjectCAPI()
# Cgobject.to_object(memory_address)
# to get memory address from a gobject:
# address = hash(obj)
#pdb.set_trace()
Cgobject = PyGObjectCAPI()
NUM_ACCOUNT_TYPES = gnucash.gnucash_core_c.NUM_ACCOUNT_TYPES
class AccountOpaque(ctypes.Structure):
pass
gboolean = ctypes.c_int
class AccountViewInfo(ctypes.Structure):
pass
AccountViewInfo._fields_ = [
('include_type', gboolean*NUM_ACCOUNT_TYPES),
('show_hidden', gboolean),
]
AccountViewInfoPtr = ctypes.POINTER(AccountViewInfo)
# great - GncTreeView or GncTreeViewAccount are not defined at the load point
# so we have to use the following to create new GncTreeViewAccount objects
# but we do not get access to the additional functions the class defines
# only get access to the functions we have mapped for the parent classe(s)
# looks as though we need to add functions to the python side
# unfortunately just using gobject.new does not work - gnc_tree_view_account defines
# specific constructor functions which do things just calling gobject.new does not
def new_with_root (root, show_root):
# but they are defined at this point
#pdb.set_trace()
#tmptreeview = GObject.new(GObject.type_from_name('GncTreeView'))
#tmptreeviewaccount = GObject.new(GObject.type_from_name('GncTreeViewAccount'))
#print("trying 1")
#print(tmptreeviewaccount)
#print("trying 2")
#newaccview = GObject.new(GObject.type_from_name('GncTreeViewAccount'))
account_ptr = ctypes.cast( root.instance.__int__(), ctypes.POINTER( AccountOpaque ) )
newaccview_ptr = gnome_utils_ctypes.libgnc_gnomeutils.gnc_tree_view_account_new_with_root(account_ptr, show_root)
# call like this:
newaccview = Cgobject.to_object(newaccview_ptr)
newaccview.do_get_headers_visible = types.MethodType(do_get_headers_visible, newaccview)
newaccview.do_set_headers_visible = types.MethodType(do_set_headers_visible, newaccview)
newaccview.do_get_selection = types.MethodType(do_get_selection, newaccview)
newaccview.do_set_selection = types.MethodType(do_set_selection, newaccview)
newaccview.do_get_view_info = types.MethodType(get_view_info, newaccview)
newaccview.do_set_view_info = types.MethodType(set_view_info, newaccview)
newaccview.do_get_cursor_account = types.MethodType(get_cursor_account, newaccview)
newaccview.do_select_subaccounts = types.MethodType(select_subaccounts, newaccview)
newaccview.do_set_selected_accounts = types.MethodType(set_selected_accounts, newaccview)
newaccview.do_get_selected_accounts = types.MethodType(get_selected_accounts, newaccview)
return newaccview
def new (show_root):
curbook = sw_app_utils.get_current_book()
rootacc = curbook.get_root_account()
return new_with_root(rootacc,show_root)
def do_get_headers_visible (self):
return self.get_headers_visible()
def do_set_headers_visible (self, make_visible):
self.set_headers_visible(make_visible)
def do_get_selection (self):
return self.get_selection()
def do_set_selection (self, slctn):
self.set_get_selection(slctn)
def get_view_info (self):
#pdb.set_trace()
vwinfo = AccountViewInfo()
print("0x%x"%ctypes.addressof(vwinfo), file=sys.stderr)
print("0x%x"%ctypes.addressof(vwinfo.include_type), file=sys.stderr)
vwptr = ctypes.pointer(vwinfo)
print("0x%x"%ctypes.addressof(vwptr), file=sys.stderr)
print("0x%x"%ctypes.addressof(vwptr.contents), file=sys.stderr)
trvwptr = hash(self)
print("tree view",self, file=sys.stderr)
print("tree view 0x%x"%trvwptr, file=sys.stderr)
gnome_utils_ctypes.libgnc_gnomeutils.gnc_tree_view_account_get_view_info(trvwptr, ctypes.cast( vwptr, ctypes.c_void_p ) )
return vwinfo
def set_view_info (self, vwinfo):
#pdb.set_trace()
#print("set_view_info 1",self)
#gc.collect()
print("0x%x"%ctypes.addressof(vwinfo), file=sys.stderr)
print("0x%x"%ctypes.addressof(vwinfo.include_type), file=sys.stderr)
vwptr = ctypes.pointer(vwinfo)
print("0x%x"%ctypes.addressof(vwptr), file=sys.stderr)
print("0x%x"%ctypes.addressof(vwptr.contents), file=sys.stderr)
trvwptr = hash(self)
gnome_utils_ctypes.libgnc_gnomeutils.gnc_tree_view_account_set_view_info(trvwptr, ctypes.cast( vwptr, ctypes.c_void_p ) )
#print("set_view_info 2")
#gc.collect()
def get_cursor_account (self):
pdb.set_trace()
print("get_cursor_account")
trvwptr = hash(self)
account_ptr = gnome_utils_ctypes.libgnc_gnomeutils.gnc_tree_view_account_get_cursor_account(trvwptr)
if account_ptr == None:
return None
## bugger - we now hit the big problem - how to convert raw C pointers to swig instances
## ah - got a way - stupid but what can we do
## get the account guid and look it up
#curbook = sw_app_utils.get_current_book()
#qof_ptr = ctypes.cast( account_ptr, ctypes.POINTER( qof_ctypes.QofInstanceOpaque ) )
#acc_guid = qof_ctypes.libgnc_qof.qof_instance_get_guid(qof_ptr)
#acc_guid_str = "".join([ "%02x"%x for x in acc_guid.contents.data ])
#acc_guid = gnucash.GUID.string_to_guid(acc_guid_str)
#account = gnucash.GUID.AccountLookup(acc_guid,curbook)
accinst = swighelpers.int_to_swig(account_ptr,"_p_Account")
account = gnucash.Account(instance=accinst)
return account
def select_subaccounts (self, account):
#pdb.set_trace()
print("select_subaccounts")
print(self)
print(account)
account_ptr = ctypes.cast( account.instance.__int__(), ctypes.POINTER( AccountOpaque ) )
trvwptr = hash(self)
gnome_utils_ctypes.libgnc_gnomeutils.gnc_tree_view_account_select_subaccounts(trvwptr, account_ptr)
def set_selected_accounts (self, account_list, show_last):
pdb.set_trace()
#print("set_selected_accounts 1",self)
#gc.collect()
if len(account_list) > 0:
# construct a g_list from list of accounts
glst_ptr = None
for elm in account_list:
acc_ptr = ctypes.cast( elm.instance.__int__(), ctypes.POINTER( AccountOpaque ) )
glst_ptr = glib_ctypes.libglib.g_list_prepend(glst_ptr,acc_ptr)
glst_ptr = glib_ctypes.libglib.g_list_reverse(glst_ptr)
print("0x%x"%ctypes.addressof(glst_ptr), file=sys.stderr)
#print("0x%x"%ctypes.addressof(vwptr.contents), file=sys.stderr)
#pdb.set_trace()
trvwptr = hash(self)
gnome_utils_ctypes.libgnc_gnomeutils.gnc_tree_view_account_set_selected_accounts(trvwptr, glst_ptr, show_last)
#print("set_selected_accounts 2")
#gc.collect()
def get_selected_accounts (self):
pdb.set_trace()
#print("get_selected_accounts 1",self)
#gc.collect()
trvwptr = hash(self)
print("tree view",self, file=sys.stderr)
print("tree view 0x%x"%trvwptr, file=sys.stderr)
glst_ptr = gnome_utils_ctypes.libgnc_gnomeutils.gnc_tree_view_account_get_selected_accounts(trvwptr)
print(glst_ptr)
glst_ptr = ctypes.cast( glst_ptr, ctypes.POINTER( glib_ctypes.GListRaw ) )
print(glst_ptr)
glst_len = glib_ctypes.libglib.g_list_length(glst_ptr)
print(glst_ptr)
print(glst_len)
curbook = sw_app_utils.get_current_book()
# we need a much better way of doing this
# need to be able to hook into the SWIG base functions using the
# ctypes integer pointer value
# - indexing through the list is slow as we scan the pointers each time
acctype = swighelpers.get_swig_type("_p_Account")
acc_lst = []
glst_ptr_base = glst_ptr
for irng in range(glst_len):
#gelm1 = glib_ctypes.libglib.g_list_nth_data(glst_ptr_base,irng)
gelm = glst_ptr.contents.data
glst_ptr = glib_ctypes.g_list_next(glst_ptr)
account_ptr = gelm
print("account_ptr 0x%x"%account_ptr, file=sys.stderr)
accinst = swighelpers.int_to_swig(account_ptr,acctype);
#accinst = swighelpers.int_to_swig(account_ptr,"_p_Account")
#qof_ptr = ctypes.cast( account_ptr, ctypes.POINTER( qof_ctypes.QofInstanceOpaque ) )
#acc_guid = qof_ctypes.libgnc_qof.qof_instance_get_guid(qof_ptr)
#acc_guid_str = "".join([ "%02x"%x for x in acc_guid.contents.data ])
#acc_guid = gnucash.GUID.string_to_guid(acc_guid_str)
#account = gnucash.GUID.AccountLookup(acc_guid,curbook)
account = gnucash.Account(instance=accinst)
acc_lst.append(account)
#pdb.set_trace()
glib_ctypes.libglib.g_list_free(glst_ptr_base)
return acc_lst