Line data Source code
1 : /********************************************************************\
2 : * gnc-entry-quickfill.c -- Create an entry description quick-fill *
3 : * Copyright (C) 2010 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-entry-quickfill.h"
26 : #include "gnc-event.h"
27 : #include "gncEntry.h"
28 :
29 : /* This static indicates the debugging module that this .o belongs to. */
30 : G_GNUC_UNUSED static QofLogModule log_module = GNC_MOD_REGISTER;
31 :
32 : typedef struct
33 : {
34 : QuickFill *qf;
35 : QuickFillSort qf_sort;
36 : QofBook *book;
37 : gint listener;
38 : gboolean using_invoices;
39 : } EntryQF;
40 :
41 : static void
42 0 : listen_for_gncentry_events(QofInstance *entity, QofEventId event_type,
43 : gpointer user_data, gpointer event_data)
44 : {
45 0 : EntryQF *qfb = user_data;
46 0 : QuickFill *qf = qfb->qf;
47 : const char *desc;
48 :
49 : /* We only listen for GncEntry events */
50 0 : if (!GNC_IS_ENTRY (entity))
51 0 : return;
52 :
53 : /* We listen for MODIFY (if the description was changed into
54 : * something non-empty, so we add the string to the quickfill) and
55 : * DESTROY (to remove the description from the quickfill). */
56 0 : if (0 == (event_type & (QOF_EVENT_MODIFY | QOF_EVENT_DESTROY)))
57 0 : return;
58 :
59 : /* g_warning("entity %p, entity type %s, event type %s, user data %p, ecent data %p", */
60 : /* entity, entity->e_type, qofeventid_to_string(event_type), user_data, event_data); */
61 :
62 0 : desc = gncEntryGetDescription(GNC_ENTRY(entity));
63 0 : if (event_type & QOF_EVENT_MODIFY)
64 : {
65 : /* If the description was changed into something non-empty, so
66 : * we add the string to the quickfill */
67 0 : if (!desc || strlen(desc) == 0)
68 0 : return;
69 :
70 : /* Add the new string to the quickfill */
71 0 : gnc_quickfill_insert (qf, desc, QUICKFILL_LIFO);
72 : }
73 0 : else if (event_type & QOF_EVENT_DESTROY)
74 : {
75 0 : if (!desc || strlen(desc) == 0)
76 0 : return;
77 :
78 : /* Remove the description from the quickfill */
79 0 : gnc_quickfill_insert (qf, desc, QUICKFILL_LIFO);
80 : }
81 : }
82 :
83 : static void
84 0 : shared_quickfill_destroy (QofBook *book, gpointer key, gpointer user_data)
85 : {
86 0 : EntryQF *qfb = user_data;
87 0 : gnc_quickfill_destroy (qfb->qf);
88 0 : qof_event_unregister_handler (qfb->listener);
89 0 : g_free (qfb);
90 0 : }
91 :
92 0 : static void entry_cb(gpointer data, gpointer user_data)
93 : {
94 0 : const GncEntry* entry = data;
95 0 : EntryQF *s = (EntryQF *) user_data;
96 0 : if (s->using_invoices == (gncEntryGetInvAccount(entry) != NULL))
97 : {
98 0 : gnc_quickfill_insert (s->qf,
99 : gncEntryGetDescription(entry),
100 : s->qf_sort);
101 : }
102 0 : }
103 :
104 : /** Creates a new query that searches for all GncEntry items in the
105 : * current book. */
106 0 : static QofQuery *new_query_for_entrys(QofBook *book)
107 : {
108 0 : GSList *primary_sort_params = NULL;
109 0 : QofQuery *query = qof_query_create_for (GNC_ID_ENTRY);
110 0 : g_assert(book);
111 0 : qof_query_set_book (query, book);
112 :
113 : /* Set the sort order: By DATE_ENTERED, increasing, and returning
114 : * only one single resulting item. */
115 0 : primary_sort_params = qof_query_build_param_list(ENTRY_DATE_ENTERED, NULL);
116 0 : qof_query_set_sort_order (query, primary_sort_params, NULL, NULL);
117 0 : qof_query_set_sort_increasing (query, TRUE, TRUE, TRUE);
118 :
119 0 : return query;
120 : }
121 :
122 0 : static EntryQF* build_shared_quickfill (QofBook *book, const char * key, gboolean use_invoices)
123 : {
124 : EntryQF *result;
125 0 : QofQuery *query = new_query_for_entrys(book);
126 0 : GList *entries = qof_query_run(query);
127 :
128 : /* g_warning("Found %d GncEntry items", g_list_length (entries)); */
129 :
130 0 : result = g_new0(EntryQF, 1);
131 :
132 0 : result->using_invoices = use_invoices;
133 0 : result->qf = gnc_quickfill_new();
134 0 : result->qf_sort = QUICKFILL_LIFO;
135 0 : result->book = book;
136 :
137 0 : g_list_foreach (entries, entry_cb, result);
138 :
139 0 : qof_query_destroy(query);
140 :
141 0 : result->listener =
142 0 : qof_event_register_handler (listen_for_gncentry_events,
143 : result);
144 :
145 0 : qof_book_set_data_fin (book, key, result, shared_quickfill_destroy);
146 :
147 0 : return result;
148 : }
149 :
150 0 : QuickFill * gnc_get_shared_entry_desc_quickfill (QofBook *book,
151 : const char * key, gboolean use_invoices)
152 : {
153 : EntryQF *qfb;
154 :
155 0 : g_assert(book);
156 0 : g_assert(key);
157 :
158 0 : qfb = qof_book_get_data (book, key);
159 :
160 0 : if (!qfb)
161 : {
162 0 : qfb = build_shared_quickfill(book, key, use_invoices);
163 : }
164 :
165 0 : g_assert(use_invoices == qfb->using_invoices);
166 0 : return qfb->qf;
167 : }
|