GnuCash c935c2f+
Loading...
Searching...
No Matches
gnc-entry-quickfill.c
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. */
30G_GNUC_UNUSED static QofLogModule log_module = GNC_MOD_REGISTER;
31
32typedef struct
33{
34 QuickFill *qf;
35 QuickFillSort qf_sort;
36 QofBook *book;
37 gint listener;
38 gboolean using_invoices;
39} EntryQF;
40
41static void
42listen_for_gncentry_events(QofInstance *entity, QofEventId event_type,
43 gpointer user_data, gpointer event_data)
44{
45 EntryQF *qfb = user_data;
46 QuickFill *qf = qfb->qf;
47 const char *desc;
48
49 /* We only listen for GncEntry events */
50 if (!GNC_IS_ENTRY (entity))
51 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 if (0 == (event_type & (QOF_EVENT_MODIFY | QOF_EVENT_DESTROY)))
57 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 desc = gncEntryGetDescription(GNC_ENTRY(entity));
63 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 if (!desc || strlen(desc) == 0)
68 return;
69
70 /* Add the new string to the quickfill */
71 gnc_quickfill_insert (qf, desc, QUICKFILL_LIFO);
72 }
73 else if (event_type & QOF_EVENT_DESTROY)
74 {
75 if (!desc || strlen(desc) == 0)
76 return;
77
78 /* Remove the description from the quickfill */
79 gnc_quickfill_insert (qf, desc, QUICKFILL_LIFO);
80 }
81}
82
83static void
84shared_quickfill_destroy (QofBook *book, gpointer key, gpointer user_data)
85{
86 EntryQF *qfb = user_data;
87 gnc_quickfill_destroy (qfb->qf);
88 qof_event_unregister_handler (qfb->listener);
89 g_free (qfb);
90}
91
92static void entry_cb(gpointer data, gpointer user_data)
93{
94 const GncEntry* entry = data;
95 EntryQF *s = (EntryQF *) user_data;
96 if (s->using_invoices == (gncEntryGetInvAccount(entry) != NULL))
97 {
99 gncEntryGetDescription(entry),
100 s->qf_sort);
101 }
102}
103
106static QofQuery *new_query_for_entrys(QofBook *book)
107{
108 GSList *primary_sort_params = NULL;
109 QofQuery *query = qof_query_create_for (GNC_ID_ENTRY);
110 g_assert(book);
111 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 primary_sort_params = qof_query_build_param_list(ENTRY_DATE_ENTERED, NULL);
116 qof_query_set_sort_order (query, primary_sort_params, NULL, NULL);
117 qof_query_set_sort_increasing (query, TRUE, TRUE, TRUE);
118
119 return query;
120}
121
122static EntryQF* build_shared_quickfill (QofBook *book, const char * key, gboolean use_invoices)
123{
124 EntryQF *result;
125 QofQuery *query = new_query_for_entrys(book);
126 GList *entries = qof_query_run(query);
127
128 /* g_warning("Found %d GncEntry items", g_list_length (entries)); */
129
130 result = g_new0(EntryQF, 1);
131
132 result->using_invoices = use_invoices;
133 result->qf = gnc_quickfill_new();
134 result->qf_sort = QUICKFILL_LIFO;
135 result->book = book;
136
137 g_list_foreach (entries, entry_cb, result);
138
139 qof_query_destroy(query);
140
141 result->listener =
142 qof_event_register_handler (listen_for_gncentry_events,
143 result);
144
145 qof_book_set_data_fin (book, key, result, shared_quickfill_destroy);
146
147 return result;
148}
149
151 const char * key, gboolean use_invoices)
152{
153 EntryQF *qfb;
154
155 g_assert(book);
156 g_assert(key);
157
158 qfb = qof_book_get_data (book, key);
159
160 if (!qfb)
161 {
162 qfb = build_shared_quickfill(book, key, use_invoices);
163 }
164
165 g_assert(use_invoices == qfb->using_invoices);
166 return qfb->qf;
167}
Additional event handling code.
Business Entry Interface.
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_sort_increasing(QofQuery *q, gboolean prim_inc, gboolean sec_inc, gboolean tert_inc)
When a query is run, the results are sorted before being returned.
void qof_query_set_book(QofQuery *query, QofBook *book)
Set the book to be searched.
void qof_query_set_sort_order(QofQuery *q, QofQueryParamList *params1, QofQueryParamList *params2, QofQueryParamList *params3)
When a query is run, the results are sorted before being returned.
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.
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_entry_desc_quickfill(QofBook *book, const char *key, gboolean use_invoices)
Similar to the Account Names account name quickfill, we create a cached quickfill with the descriptio...
QofBook reference.
Definition qofbook-p.hpp:47
A Query.
Definition qofquery.cpp:75