GnuCash 2.4.99
gnc-entry-quickfill.c
00001 /********************************************************************\
00002  * gnc-entry-quickfill.c -- Create an entry description quick-fill  *
00003  * Copyright (C) 2010 Christian Stimming <christian@cstimming.de>   *
00004  *                                                                  *
00005  * This program is free software; you can redistribute it and/or    *
00006  * modify it under the terms of the GNU General Public License as   *
00007  * published by the Free Software Foundation; either version 2 of   *
00008  * the License, or (at your option) any later version.              *
00009  *                                                                  *
00010  * This program is distributed in the hope that it will be useful,  *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00013  * GNU General Public License for more details.                     *
00014  *                                                                  *
00015  * You should have received a copy of the GNU General Public License*
00016  * along with this program; if not, contact:                        *
00017  *                                                                  *
00018  * Free Software Foundation           Voice:  +1-617-542-5942       *
00019  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
00020  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
00021  *                                                                  *
00022 \********************************************************************/
00023 
00024 #include "config.h"
00025 #include "gnc-entry-quickfill.h"
00026 #include "engine/gnc-event.h"
00027 #include "engine/gncEntry.h"
00028 
00029 /* This static indicates the debugging module that this .o belongs to. */
00030 static QofLogModule log_module = GNC_MOD_REGISTER;
00031 
00032 typedef struct
00033 {
00034     QuickFill *qf;
00035     QuickFillSort qf_sort;
00036     QofBook *book;
00037     gint  listener;
00038     gboolean using_invoices;
00039 } EntryQF;
00040 
00041 static void
00042 listen_for_gncentry_events(QofInstance *entity,  QofEventId event_type,
00043                            gpointer user_data, gpointer event_data)
00044 {
00045     EntryQF *qfb = user_data;
00046     QuickFill *qf = qfb->qf;
00047     const char *desc;
00048 
00049     /* We only listen for GncEntry events */
00050     if (!GNC_IS_ENTRY (entity))
00051         return;
00052 
00053     /* We listen for MODIFY (if the description was changed into
00054      * something non-empty, so we add the string to the quickfill) and
00055      * DESTROY (to remove the description from the quickfill). */
00056     if (0 == (event_type & (QOF_EVENT_MODIFY | QOF_EVENT_DESTROY)))
00057         return;
00058 
00059     /*     g_warning("entity %p, entity type %s, event type %s, user data %p, ecent data %p", */
00060     /*               entity, entity->e_type, qofeventid_to_string(event_type), user_data, event_data); */
00061 
00062     desc = gncEntryGetDescription(GNC_ENTRY(entity));
00063     if (event_type & QOF_EVENT_MODIFY)
00064     {
00065         /* If the description was changed into something non-empty, so
00066          * we add the string to the quickfill */
00067         if (!desc || strlen(desc) == 0)
00068             return;
00069 
00070         /* Add the new string to the quickfill */
00071         gnc_quickfill_insert (qf, desc, QUICKFILL_LIFO);
00072     }
00073     else if (event_type & QOF_EVENT_DESTROY)
00074     {
00075         if (!desc || strlen(desc) == 0)
00076             return;
00077 
00078         /* Remove the description from the quickfill */
00079         gnc_quickfill_insert (qf, desc, QUICKFILL_LIFO);
00080     }
00081 }
00082 
00083 static void
00084 shared_quickfill_destroy (QofBook *book, gpointer key, gpointer user_data)
00085 {
00086     EntryQF *qfb = user_data;
00087     gnc_quickfill_destroy (qfb->qf);
00088     qof_event_unregister_handler (qfb->listener);
00089     g_free (qfb);
00090 }
00091 
00092 static void entry_cb(gpointer data, gpointer user_data)
00093 {
00094     const GncEntry* entry = data;
00095     EntryQF *s = (EntryQF *) user_data;
00096     if (s->using_invoices == (gncEntryGetInvAccount(entry) != NULL))
00097     {
00098         gnc_quickfill_insert (s->qf,
00099                               gncEntryGetDescription(entry),
00100                               s->qf_sort);
00101     }
00102 }
00103 
00106 static QofQuery *new_query_for_entrys(QofBook *book)
00107 {
00108     GSList *primary_sort_params = NULL;
00109     QofQuery *query = qof_query_create_for (GNC_ID_ENTRY);
00110     g_assert(book);
00111     qof_query_set_book (query, book);
00112 
00113     /* Set the sort order: By DATE_ENTERED, increasing, and returning
00114      * only one single resulting item. */
00115     primary_sort_params = qof_query_build_param_list(ENTRY_DATE_ENTERED, NULL);
00116     qof_query_set_sort_order (query, primary_sort_params, NULL, NULL);
00117     qof_query_set_sort_increasing (query, TRUE, TRUE, TRUE);
00118 
00119     return query;
00120 }
00121 
00122 static EntryQF* build_shared_quickfill (QofBook *book, const char * key, gboolean use_invoices)
00123 {
00124     EntryQF *result;
00125     QofQuery *query = new_query_for_entrys(book);
00126     GList *entries = qof_query_run(query);
00127 
00128     /*     g_warning("Found %d GncEntry items", g_list_length (entries)); */
00129 
00130     result = g_new0(EntryQF, 1);
00131 
00132     result->using_invoices = use_invoices;
00133     result->qf = gnc_quickfill_new();
00134     result->qf_sort = QUICKFILL_LIFO;
00135     result->book = book;
00136 
00137     g_list_foreach (entries, entry_cb, result);
00138 
00139     qof_query_destroy(query);
00140 
00141     result->listener =
00142         qof_event_register_handler (listen_for_gncentry_events,
00143                                     result);
00144 
00145     qof_book_set_data_fin (book, key, result, shared_quickfill_destroy);
00146 
00147     return result;
00148 }
00149 
00150 QuickFill * gnc_get_shared_entry_desc_quickfill (QofBook *book,
00151         const char * key, gboolean use_invoices)
00152 {
00153     EntryQF *qfb;
00154 
00155     g_assert(book);
00156     g_assert(key);
00157 
00158     qfb = qof_book_get_data (book, key);
00159 
00160     if (!qfb)
00161     {
00162         qfb = build_shared_quickfill(book, key, use_invoices);
00163     }
00164 
00165     g_assert(use_invoices == qfb->using_invoices);
00166     return qfb->qf;
00167 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines