GnuCash c935c2f+
Loading...
Searching...
No Matches
gnc-addr-quickfill.c
1/********************************************************************\
2 * gnc-addr-quickfill.c -- Create an address line quick-fill *
3 * Copyright (C) 2011 Christian Stimming <christian@cstimming.de> *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation; either version 2 of *
8 * the License, or (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License*
16 * along with this program; if not, contact: *
17 * *
18 * Free Software Foundation Voice: +1-617-542-5942 *
19 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20 * Boston, MA 02110-1301, USA gnu@gnu.org *
21 * *
22\********************************************************************/
23
24#include <config.h>
25#include "gnc-addr-quickfill.h"
26#include "gnc-event.h"
27#include "gnc-engine.h"
28#include "gncAddress.h"
29
30/* This static indicates the debugging module that this .o belongs to. */
31G_GNUC_UNUSED static QofLogModule log_module = GNC_MOD_REGISTER;
32
33typedef struct
34{
35 QuickFill *qf_addr2;
36 QuickFill *qf_addr3;
37 QuickFill *qf_addr4;
38 QuickFillSort qf_sort;
39 QofBook *book;
40 gint listener;
41} AddressQF;
42
43static void
44listen_for_gncaddress_events(QofInstance *entity, QofEventId event_type,
45 gpointer user_data, gpointer event_data)
46{
47 AddressQF *qfb = user_data;
48 const char *addr2, *addr3, *addr4;
49
50 /* We only listen for GncAddress events */
51 if (!GNC_IS_ADDRESS (entity))
52 return;
53
54 /* We listen for MODIFY (if the description was changed into
55 * something non-empty, so we add the string to the quickfill) and
56 * DESTROY (to remove the description from the quickfill). */
57 if (0 == (event_type & (QOF_EVENT_MODIFY | QOF_EVENT_DESTROY)))
58 return;
59
60 /* g_warning("entity %p, entity type %s, event type %s, user data %p, ecent data %p", */
61 /* entity, entity->e_type, qofeventid_to_string(event_type), user_data, event_data); */
62
63 addr2 = gncAddressGetAddr2(GNC_ADDRESS(entity));
64 addr3 = gncAddressGetAddr3(GNC_ADDRESS(entity));
65 addr4 = gncAddressGetAddr4(GNC_ADDRESS(entity));
66 if (event_type & QOF_EVENT_MODIFY)
67 {
68 /* If the description was changed into something non-empty, so
69 * we add the string to the quickfill */
70 if (addr2 && strlen(addr2) > 0)
71 {
72 /* Add the new string to the quickfill */
73 gnc_quickfill_insert (qfb->qf_addr2, addr2, QUICKFILL_LIFO);
74 }
75 if (addr3 && strlen(addr3) > 0)
76 {
77 /* Add the new string to the quickfill */
78 gnc_quickfill_insert (qfb->qf_addr3, addr3, QUICKFILL_LIFO);
79 }
80 if (addr4 && strlen(addr4) > 0)
81 {
82 /* Add the new string to the quickfill */
83 gnc_quickfill_insert (qfb->qf_addr4, addr4, QUICKFILL_LIFO);
84 }
85 }
86 else if (event_type & QOF_EVENT_DESTROY)
87 {
88 if (addr2 && strlen(addr2) > 0)
89 {
90 /* Remove the description from the quickfill */
91 gnc_quickfill_insert (qfb->qf_addr2, addr2, QUICKFILL_LIFO);
92 }
93 if (addr3 && strlen(addr3) > 0)
94 {
95 /* Remove the description from the quickfill */
96 gnc_quickfill_insert (qfb->qf_addr3, addr3, QUICKFILL_LIFO);
97 }
98 if (addr4 && strlen(addr4) > 0)
99 {
100 /* Remove the description from the quickfill */
101 gnc_quickfill_insert (qfb->qf_addr4, addr4, QUICKFILL_LIFO);
102 }
103 }
104}
105
106static void
107shared_quickfill_destroy (QofBook *book, gpointer key, gpointer user_data)
108{
109 AddressQF *qfb = user_data;
110 gnc_quickfill_destroy (qfb->qf_addr2);
111 gnc_quickfill_destroy (qfb->qf_addr3);
112 gnc_quickfill_destroy (qfb->qf_addr4);
113 qof_event_unregister_handler (qfb->listener);
114 g_free (qfb);
115}
116
117static void address_cb(gpointer data, gpointer user_data)
118{
119 const GncAddress* address = data;
120 AddressQF *s = (AddressQF *) user_data;
121
122 gnc_quickfill_insert (s->qf_addr2,
123 gncAddressGetAddr2(address),
124 s->qf_sort);
125
126 gnc_quickfill_insert (s->qf_addr3,
127 gncAddressGetAddr3(address),
128 s->qf_sort);
129
130 gnc_quickfill_insert (s->qf_addr4,
131 gncAddressGetAddr4(address),
132 s->qf_sort);
133}
134
137static QofQuery *new_query_for_address(QofBook *book)
138{
139 QofQuery *query = qof_query_create_for (GNC_ID_ADDRESS);
140 g_assert(book);
141 qof_query_set_book (query, book);
142
143 /* No particular sort order here. */
144
145 return query;
146}
147
148static AddressQF* build_shared_quickfill (QofBook *book, const char * key)
149{
150 AddressQF *result;
151 QofQuery *query = new_query_for_address(book);
152 GList *entries = qof_query_run(query);
153
154 /* g_warning("Found %d GncAddress items", g_list_length (entries)); */
155
156 result = g_new0(AddressQF, 1);
157
158 result->qf_addr2 = gnc_quickfill_new();
159 result->qf_addr3 = gnc_quickfill_new();
160 result->qf_addr4 = gnc_quickfill_new();
161 result->qf_sort = QUICKFILL_ALPHA;
162 result->book = book;
163
164 g_list_foreach (entries, address_cb, result);
165
166 qof_query_destroy(query);
167
168 result->listener =
169 qof_event_register_handler (listen_for_gncaddress_events,
170 result);
171
172 qof_book_set_data_fin (book, key, result, shared_quickfill_destroy);
173
174 return result;
175}
176
177QuickFill * gnc_get_shared_address_addr2_quickfill (QofBook *book, const char * key)
178{
179 AddressQF *qfb;
180
181 g_assert(book);
182 g_assert(key);
183
184 qfb = qof_book_get_data (book, key);
185
186 if (!qfb)
187 {
188 qfb = build_shared_quickfill(book, key);
189 }
190
191 return qfb->qf_addr2;
192}
193
194QuickFill * gnc_get_shared_address_addr3_quickfill (QofBook *book, const char * key)
195{
196 AddressQF *qfb;
197
198 g_assert(book);
199 g_assert(key);
200
201 qfb = qof_book_get_data (book, key);
202
203 if (!qfb)
204 {
205 qfb = build_shared_quickfill(book, key);
206 }
207
208 return qfb->qf_addr3;
209}
210
211QuickFill * gnc_get_shared_address_addr4_quickfill (QofBook *book, const char * key)
212{
213 AddressQF *qfb;
214
215 g_assert(book);
216 g_assert(key);
217
218 qfb = qof_book_get_data (book, key);
219
220 if (!qfb)
221 {
222 qfb = build_shared_quickfill(book, key);
223 }
224
225 return qfb->qf_addr4;
226}
All type declarations for the whole Gnucash engine.
Additional event handling code.
an Address object
void qof_event_unregister_handler(gint handler_id)
Unregister an event handler.
Definition qofevent.cpp:103
gint qof_event_register_handler(QofEventHandler handler, gpointer user_data)
Register a handler for events.
Definition qofevent.cpp:73
gint QofEventId
Define the type of events allowed.
Definition qofevent.h:45
void qof_query_set_book(QofQuery *query, QofBook *book)
Set the book to be searched.
void qof_query_destroy(QofQuery *query)
Frees the resources associate with a Query object.
GList * qof_query_run(QofQuery *query)
Perform the query, return the results.
QuickFill * gnc_get_shared_address_addr3_quickfill(QofBook *book, const char *key)
Create/fetch a quickfill GncAddress description strings on the Addr3 part.
QuickFill * gnc_get_shared_address_addr2_quickfill(QofBook *book, const char *key)
Similar to the Account Names account name quickfill, we create a cached quickfill with the address li...
void gnc_quickfill_insert(QuickFill *qf, const char *text, QuickFillSort sort)
Add the string "text" to the collection of searchable strings.
Definition QuickFill.c:229
QuickFill * gnc_get_shared_address_addr4_quickfill(QofBook *book, const char *key)
Create/fetch a quickfill GncAddress description strings on the Addr4 part.
QofBook reference.
Definition qofbook-p.hpp:47
A Query.
Definition qofquery.cpp:75