2010-12-14 15:25:52 -06:00
|
|
|
/********************************************************************\
|
|
|
|
* gnc-entry-quickfill.c -- Create an entry description quick-fill *
|
|
|
|
* Copyright (C) 2010 Christian Stimming <christian@cstimming.de> *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 2 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License*
|
|
|
|
* along with this program; if not, contact: *
|
|
|
|
* *
|
|
|
|
* Free Software Foundation Voice: +1-617-542-5942 *
|
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
|
|
|
* *
|
|
|
|
\********************************************************************/
|
|
|
|
|
2017-10-26 04:14:21 -05:00
|
|
|
#include <config.h>
|
2010-12-14 15:25:52 -06:00
|
|
|
#include "gnc-entry-quickfill.h"
|
2017-08-10 06:56:00 -05:00
|
|
|
#include "gnc-event.h"
|
|
|
|
#include "gncEntry.h"
|
2010-12-17 15:31:22 -06:00
|
|
|
|
|
|
|
/* This static indicates the debugging module that this .o belongs to. */
|
2012-05-26 18:47:34 -05:00
|
|
|
G_GNUC_UNUSED static QofLogModule log_module = GNC_MOD_REGISTER;
|
2010-12-14 15:25:52 -06:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
QuickFill *qf;
|
|
|
|
QuickFillSort qf_sort;
|
|
|
|
QofBook *book;
|
2010-12-17 15:31:22 -06:00
|
|
|
gint listener;
|
2010-12-27 15:07:58 -06:00
|
|
|
gboolean using_invoices;
|
2010-12-14 15:25:52 -06:00
|
|
|
} EntryQF;
|
|
|
|
|
2010-12-17 15:31:22 -06:00
|
|
|
static void
|
|
|
|
listen_for_gncentry_events(QofInstance *entity, QofEventId event_type,
|
|
|
|
gpointer user_data, gpointer event_data)
|
|
|
|
{
|
|
|
|
EntryQF *qfb = user_data;
|
|
|
|
QuickFill *qf = qfb->qf;
|
|
|
|
const char *desc;
|
|
|
|
|
|
|
|
/* We only listen for GncEntry events */
|
|
|
|
if (!GNC_IS_ENTRY (entity))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* We listen for MODIFY (if the description was changed into
|
|
|
|
* something non-empty, so we add the string to the quickfill) and
|
|
|
|
* DESTROY (to remove the description from the quickfill). */
|
|
|
|
if (0 == (event_type & (QOF_EVENT_MODIFY | QOF_EVENT_DESTROY)))
|
|
|
|
return;
|
|
|
|
|
2011-01-24 08:10:09 -06:00
|
|
|
/* g_warning("entity %p, entity type %s, event type %s, user data %p, ecent data %p", */
|
|
|
|
/* entity, entity->e_type, qofeventid_to_string(event_type), user_data, event_data); */
|
2010-12-17 15:31:22 -06:00
|
|
|
|
|
|
|
desc = gncEntryGetDescription(GNC_ENTRY(entity));
|
|
|
|
if (event_type & QOF_EVENT_MODIFY)
|
|
|
|
{
|
|
|
|
/* If the description was changed into something non-empty, so
|
|
|
|
* we add the string to the quickfill */
|
|
|
|
if (!desc || strlen(desc) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Add the new string to the quickfill */
|
|
|
|
gnc_quickfill_insert (qf, desc, QUICKFILL_LIFO);
|
|
|
|
}
|
|
|
|
else if (event_type & QOF_EVENT_DESTROY)
|
|
|
|
{
|
|
|
|
if (!desc || strlen(desc) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Remove the description from the quickfill */
|
|
|
|
gnc_quickfill_insert (qf, desc, QUICKFILL_LIFO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-14 15:25:52 -06:00
|
|
|
static void
|
|
|
|
shared_quickfill_destroy (QofBook *book, gpointer key, gpointer user_data)
|
|
|
|
{
|
|
|
|
EntryQF *qfb = user_data;
|
|
|
|
gnc_quickfill_destroy (qfb->qf);
|
2010-12-17 15:31:22 -06:00
|
|
|
qof_event_unregister_handler (qfb->listener);
|
2010-12-14 15:25:52 -06:00
|
|
|
g_free (qfb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void entry_cb(gpointer data, gpointer user_data)
|
|
|
|
{
|
|
|
|
const GncEntry* entry = data;
|
|
|
|
EntryQF *s = (EntryQF *) user_data;
|
2010-12-27 15:07:58 -06:00
|
|
|
if (s->using_invoices == (gncEntryGetInvAccount(entry) != NULL))
|
|
|
|
{
|
|
|
|
gnc_quickfill_insert (s->qf,
|
|
|
|
gncEntryGetDescription(entry),
|
|
|
|
s->qf_sort);
|
|
|
|
}
|
2010-12-14 15:25:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Creates a new query that searches for all GncEntry items in the
|
|
|
|
* current book. */
|
|
|
|
static QofQuery *new_query_for_entrys(QofBook *book)
|
|
|
|
{
|
2011-01-10 14:12:39 -06:00
|
|
|
GSList *primary_sort_params = NULL;
|
2010-12-14 15:25:52 -06:00
|
|
|
QofQuery *query = qof_query_create_for (GNC_ID_ENTRY);
|
|
|
|
g_assert(book);
|
|
|
|
qof_query_set_book (query, book);
|
2011-01-10 14:12:39 -06:00
|
|
|
|
|
|
|
/* Set the sort order: By DATE_ENTERED, increasing, and returning
|
|
|
|
* only one single resulting item. */
|
|
|
|
primary_sort_params = qof_query_build_param_list(ENTRY_DATE_ENTERED, NULL);
|
|
|
|
qof_query_set_sort_order (query, primary_sort_params, NULL, NULL);
|
|
|
|
qof_query_set_sort_increasing (query, TRUE, TRUE, TRUE);
|
|
|
|
|
2010-12-14 15:25:52 -06:00
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2010-12-27 15:07:58 -06:00
|
|
|
static EntryQF* build_shared_quickfill (QofBook *book, const char * key, gboolean use_invoices)
|
2010-12-14 15:25:52 -06:00
|
|
|
{
|
|
|
|
EntryQF *result;
|
|
|
|
QofQuery *query = new_query_for_entrys(book);
|
|
|
|
GList *entries = qof_query_run(query);
|
|
|
|
|
2011-01-24 08:10:09 -06:00
|
|
|
/* g_warning("Found %d GncEntry items", g_list_length (entries)); */
|
2010-12-14 15:25:52 -06:00
|
|
|
|
|
|
|
result = g_new0(EntryQF, 1);
|
|
|
|
|
2010-12-27 15:07:58 -06:00
|
|
|
result->using_invoices = use_invoices;
|
2010-12-14 15:25:52 -06:00
|
|
|
result->qf = gnc_quickfill_new();
|
|
|
|
result->qf_sort = QUICKFILL_LIFO;
|
|
|
|
result->book = book;
|
|
|
|
|
|
|
|
g_list_foreach (entries, entry_cb, result);
|
|
|
|
|
|
|
|
qof_query_destroy(query);
|
|
|
|
|
2010-12-17 15:31:22 -06:00
|
|
|
result->listener =
|
|
|
|
qof_event_register_handler (listen_for_gncentry_events,
|
|
|
|
result);
|
|
|
|
|
2010-12-14 15:25:52 -06:00
|
|
|
qof_book_set_data_fin (book, key, result, shared_quickfill_destroy);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QuickFill * gnc_get_shared_entry_desc_quickfill (QofBook *book,
|
2010-12-27 15:07:58 -06:00
|
|
|
const char * key, gboolean use_invoices)
|
2010-12-14 15:25:52 -06:00
|
|
|
{
|
|
|
|
EntryQF *qfb;
|
|
|
|
|
|
|
|
g_assert(book);
|
|
|
|
g_assert(key);
|
|
|
|
|
|
|
|
qfb = qof_book_get_data (book, key);
|
|
|
|
|
2011-02-10 15:48:58 -06:00
|
|
|
if (!qfb)
|
2010-12-27 15:07:58 -06:00
|
|
|
{
|
2011-02-10 15:48:58 -06:00
|
|
|
qfb = build_shared_quickfill(book, key, use_invoices);
|
2010-12-27 15:07:58 -06:00
|
|
|
}
|
2010-12-14 15:25:52 -06:00
|
|
|
|
2011-02-10 15:48:58 -06:00
|
|
|
g_assert(use_invoices == qfb->using_invoices);
|
2010-12-14 15:25:52 -06:00
|
|
|
return qfb->qf;
|
|
|
|
}
|