Branch data Line data Source code
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. */
31 : : G_GNUC_UNUSED static QofLogModule log_module = GNC_MOD_REGISTER;
32 : :
33 : : typedef 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 : :
43 : : static void
44 : 0 : listen_for_gncaddress_events(QofInstance *entity, QofEventId event_type,
45 : : gpointer user_data, gpointer event_data)
46 : : {
47 : 0 : AddressQF *qfb = user_data;
48 : : const char *addr2, *addr3, *addr4;
49 : :
50 : : /* We only listen for GncAddress events */
51 : 0 : if (!GNC_IS_ADDRESS (entity))
52 : 0 : 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 : 0 : if (0 == (event_type & (QOF_EVENT_MODIFY | QOF_EVENT_DESTROY)))
58 : 0 : 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 : 0 : addr2 = gncAddressGetAddr2(GNC_ADDRESS(entity));
64 : 0 : addr3 = gncAddressGetAddr3(GNC_ADDRESS(entity));
65 : 0 : addr4 = gncAddressGetAddr4(GNC_ADDRESS(entity));
66 : 0 : 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 : 0 : if (addr2 && strlen(addr2) > 0)
71 : : {
72 : : /* Add the new string to the quickfill */
73 : 0 : gnc_quickfill_insert (qfb->qf_addr2, addr2, QUICKFILL_LIFO);
74 : : }
75 : 0 : if (addr3 && strlen(addr3) > 0)
76 : : {
77 : : /* Add the new string to the quickfill */
78 : 0 : gnc_quickfill_insert (qfb->qf_addr3, addr3, QUICKFILL_LIFO);
79 : : }
80 : 0 : if (addr4 && strlen(addr4) > 0)
81 : : {
82 : : /* Add the new string to the quickfill */
83 : 0 : gnc_quickfill_insert (qfb->qf_addr4, addr4, QUICKFILL_LIFO);
84 : : }
85 : : }
86 : 0 : else if (event_type & QOF_EVENT_DESTROY)
87 : : {
88 : 0 : if (addr2 && strlen(addr2) > 0)
89 : : {
90 : : /* Remove the description from the quickfill */
91 : 0 : gnc_quickfill_insert (qfb->qf_addr2, addr2, QUICKFILL_LIFO);
92 : : }
93 : 0 : if (addr3 && strlen(addr3) > 0)
94 : : {
95 : : /* Remove the description from the quickfill */
96 : 0 : gnc_quickfill_insert (qfb->qf_addr3, addr3, QUICKFILL_LIFO);
97 : : }
98 : 0 : if (addr4 && strlen(addr4) > 0)
99 : : {
100 : : /* Remove the description from the quickfill */
101 : 0 : gnc_quickfill_insert (qfb->qf_addr4, addr4, QUICKFILL_LIFO);
102 : : }
103 : : }
104 : : }
105 : :
106 : : static void
107 : 0 : shared_quickfill_destroy (QofBook *book, gpointer key, gpointer user_data)
108 : : {
109 : 0 : AddressQF *qfb = user_data;
110 : 0 : gnc_quickfill_destroy (qfb->qf_addr2);
111 : 0 : gnc_quickfill_destroy (qfb->qf_addr3);
112 : 0 : gnc_quickfill_destroy (qfb->qf_addr4);
113 : 0 : qof_event_unregister_handler (qfb->listener);
114 : 0 : g_free (qfb);
115 : 0 : }
116 : :
117 : 0 : static void address_cb(gpointer data, gpointer user_data)
118 : : {
119 : 0 : const GncAddress* address = data;
120 : 0 : AddressQF *s = (AddressQF *) user_data;
121 : :
122 : 0 : gnc_quickfill_insert (s->qf_addr2,
123 : : gncAddressGetAddr2(address),
124 : : s->qf_sort);
125 : :
126 : 0 : gnc_quickfill_insert (s->qf_addr3,
127 : : gncAddressGetAddr3(address),
128 : : s->qf_sort);
129 : :
130 : 0 : gnc_quickfill_insert (s->qf_addr4,
131 : : gncAddressGetAddr4(address),
132 : : s->qf_sort);
133 : 0 : }
134 : :
135 : : /** Creates a new query that searches for all GncAddress items in the
136 : : * current book. */
137 : 0 : static QofQuery *new_query_for_address(QofBook *book)
138 : : {
139 : 0 : QofQuery *query = qof_query_create_for (GNC_ID_ADDRESS);
140 : 0 : g_assert(book);
141 : 0 : qof_query_set_book (query, book);
142 : :
143 : : /* No particular sort order here. */
144 : :
145 : 0 : return query;
146 : : }
147 : :
148 : 0 : static AddressQF* build_shared_quickfill (QofBook *book, const char * key)
149 : : {
150 : : AddressQF *result;
151 : 0 : QofQuery *query = new_query_for_address(book);
152 : 0 : GList *entries = qof_query_run(query);
153 : :
154 : : /* g_warning("Found %d GncAddress items", g_list_length (entries)); */
155 : :
156 : 0 : result = g_new0(AddressQF, 1);
157 : :
158 : 0 : result->qf_addr2 = gnc_quickfill_new();
159 : 0 : result->qf_addr3 = gnc_quickfill_new();
160 : 0 : result->qf_addr4 = gnc_quickfill_new();
161 : 0 : result->qf_sort = QUICKFILL_ALPHA;
162 : 0 : result->book = book;
163 : :
164 : 0 : g_list_foreach (entries, address_cb, result);
165 : :
166 : 0 : qof_query_destroy(query);
167 : :
168 : 0 : result->listener =
169 : 0 : qof_event_register_handler (listen_for_gncaddress_events,
170 : : result);
171 : :
172 : 0 : qof_book_set_data_fin (book, key, result, shared_quickfill_destroy);
173 : :
174 : 0 : return result;
175 : : }
176 : :
177 : 0 : QuickFill * gnc_get_shared_address_addr2_quickfill (QofBook *book, const char * key)
178 : : {
179 : : AddressQF *qfb;
180 : :
181 : 0 : g_assert(book);
182 : 0 : g_assert(key);
183 : :
184 : 0 : qfb = qof_book_get_data (book, key);
185 : :
186 : 0 : if (!qfb)
187 : : {
188 : 0 : qfb = build_shared_quickfill(book, key);
189 : : }
190 : :
191 : 0 : return qfb->qf_addr2;
192 : : }
193 : :
194 : 0 : QuickFill * gnc_get_shared_address_addr3_quickfill (QofBook *book, const char * key)
195 : : {
196 : : AddressQF *qfb;
197 : :
198 : 0 : g_assert(book);
199 : 0 : g_assert(key);
200 : :
201 : 0 : qfb = qof_book_get_data (book, key);
202 : :
203 : 0 : if (!qfb)
204 : : {
205 : 0 : qfb = build_shared_quickfill(book, key);
206 : : }
207 : :
208 : 0 : return qfb->qf_addr3;
209 : : }
210 : :
211 : 0 : QuickFill * gnc_get_shared_address_addr4_quickfill (QofBook *book, const char * key)
212 : : {
213 : : AddressQF *qfb;
214 : :
215 : 0 : g_assert(book);
216 : 0 : g_assert(key);
217 : :
218 : 0 : qfb = qof_book_get_data (book, key);
219 : :
220 : 0 : if (!qfb)
221 : : {
222 : 0 : qfb = build_shared_quickfill(book, key);
223 : : }
224 : :
225 : 0 : return qfb->qf_addr4;
226 : : }
|