GnuCash c935c2f+
Loading...
Searching...
No Matches
window-autoclear.c
1/********************************************************************\
2 * window-autoclear.c -- the autoclear window *
3 * Copyright (C) 2010 Cristian KLEIN *
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#include <config.h>
24
25#include <gtk/gtk.h>
26#include <glib/gi18n.h>
27
28#include "dialog-utils.h"
29#include "gnc-amount-edit.h"
30#include "gnc-event.h"
31#include "gnc-gnome-utils.h"
32#include "gnc-main-window.h"
34#include "gnc-ui.h"
35#include "gnc-autoclear.h"
36#include "window-autoclear.h"
37
38#define WINDOW_AUTOCLEAR_CM_CLASS "window-autoclear"
39
40__attribute__((unused)) static QofLogModule log_module = GNC_MOD_GUI;
41
43struct _AutoClearWindow
44{
45 Account *account; /* The account that we are auto-clearing */
46
47 gint component_id; /* id of component */
48
49 GtkWidget *window; /* The auto-clear window */
50 GNCAmountEdit *end_value;/* The ending value */
51 GtkWidget *ok_button;
52 GtkWidget *cancel_button;
53 GtkWidget *show_cleared_splits_button;
54 GtkLabel *status_label;
55};
56
58void gnc_autoclear_window_ok_cb (GtkWidget *widget,
59 AutoClearWindow *data);
60void gnc_autoclear_window_cancel_cb (GtkWidget *widget,
61 AutoClearWindow *data);
62
63/********************************************************************\
64 * gnc_ui_autoclear_window_raise *
65 * shows and raises an auto-clear window *
66 * *
67 * Args: autoClearData - the auto-clear window structure *
68\********************************************************************/
69void
70gnc_ui_autoclear_window_raise(AutoClearWindow * autoClearData)
71{
72 if (autoClearData == NULL)
73 return;
74
75 if (autoClearData->window == NULL)
76 return;
77
78 gtk_window_present(GTK_WINDOW(autoClearData->window));
79}
80
81static char *
82gnc_autoclear_make_window_name(Account *account)
83{
84 char *fullname;
85 char *title;
86
87 fullname = gnc_account_get_full_name(account);
88 title = g_strconcat(fullname, " - ", _("Auto-clear"), NULL);
89
90 g_free(fullname);
91
92 return title;
93}
94
95static void
96show_cleared_splits (GList *splits)
97{
98 GNCLedgerDisplay *ledger;
99 GncPluginPage *page;
100 Query *book_query, *guid_query;
101
102 book_query = qof_query_create_for (GNC_ID_SPLIT);
103 guid_query = qof_query_create_for (GNC_ID_SPLIT);
104 qof_query_set_book (book_query, gnc_get_current_book ());
105
106 for (GList *iter = splits; iter; iter = iter->next)
107 {
108 GncGUID guid = iter->data ? *xaccSplitGetGUID (iter->data) : *guid_null() ;
109 xaccQueryAddGUIDMatch (guid_query, &guid, GNC_ID_SPLIT, QOF_QUERY_OR);
110 }
111 book_query = qof_query_merge (book_query, guid_query, QOF_QUERY_AND);
112 ledger = gnc_ledger_display_query (book_query, SEARCH_LEDGER, REG_STYLE_JOURNAL);
115 main_window_update_page_name (page, _("Cleared Transactions"));
116 gnc_main_window_open_page (NULL, page);
117 qof_query_destroy (book_query);
118 qof_query_destroy (guid_query);
119}
120
121void
122gnc_autoclear_window_ok_cb (GtkWidget *widget,
123 AutoClearWindow *data)
124{
125 GList *toclear_list = NULL;
126 gnc_numeric toclear_value = gnc_numeric_error (GNC_ERROR_ARG);
127 GError* error = NULL;
128
129 g_return_if_fail (widget && data);
130
131 /* test for valid value */
132 if (gnc_amount_edit_evaluate (GNC_AMOUNT_EDIT(data->end_value), &error))
133 {
134 toclear_value = gnc_amount_edit_get_amount(data->end_value);
135
136 if (gnc_reverse_balance(data->account))
137 toclear_value = gnc_numeric_neg (toclear_value);
138
139 toclear_value = gnc_numeric_convert
140 (toclear_value, xaccAccountGetCommoditySCU(data->account), GNC_HOW_RND_ROUND);
141
142#define MAX_AUTOCLEAR_SECONDS 5
143
144 toclear_list = gnc_account_get_autoclear_splits
145 (data->account, toclear_value, INT64_MAX, &error,
146 MAX_AUTOCLEAR_SECONDS);
147 }
148
149 if (error && error->message)
150 {
151 GtkWidget *entry = gnc_amount_edit_gtk_entry (GNC_AMOUNT_EDIT(data->end_value));
152 gtk_label_set_text (data->status_label, error->message);
153 if (gnc_numeric_check (toclear_value) == 0)
154 gnc_amount_edit_set_amount (data->end_value, toclear_value);
155 gtk_widget_grab_focus (GTK_WIDGET(entry));
156 gnc_amount_edit_select_region (GNC_AMOUNT_EDIT(data->end_value), 0, -1);
157 g_error_free (error);
158 }
159 else
160 {
161 xaccAccountBeginEdit (data->account);
162 for (GList *node = toclear_list; node; node = node->next)
163 xaccSplitSetReconcile (node->data, CREC);
164 xaccAccountCommitEdit (data->account);
165
166 if (gtk_toggle_button_get_active
167 (GTK_TOGGLE_BUTTON (data->show_cleared_splits_button)))
168 show_cleared_splits (toclear_list);
169
170 g_list_free (toclear_list);
171
172 /* Close window */
173 gtk_widget_destroy (data->window);
174 g_free (data);
175 }
176}
177
178void
179gnc_autoclear_window_cancel_cb (GtkWidget *widget,
180 AutoClearWindow *data)
181{
182 /* Close window */
183 gtk_widget_destroy(data->window);
184 g_free(data);
185}
186
187static void clear_status_label_cb (GtkEditable *editable, AutoClearWindow *data)
188{
189 gtk_label_set_text (data->status_label, "");
190}
191
192
193/********************************************************************\
194 * autoClearWindow *
195 * opens up the window to auto-clear an account *
196 * *
197 * Args: parent - the parent of this window *
198 * account - the account to auto-clear *
199 * Return: autoClearData - the instance of this AutoClearWindow *
200\********************************************************************/
201AutoClearWindow *
202autoClearWindow (GtkWidget *parent, Account *account)
203{
204 GtkBox *box;
205 GtkWidget *label;
206 GtkBuilder *builder;
207 AutoClearWindow *data;
208 char *title;
209 gnc_numeric after;
210 GNCPrintAmountInfo print_info;
211 gnc_commodity *currency;
212
213 data = g_new0 (AutoClearWindow, 1);
214 data->account = account;
215
216 /* Create the dialog box */
217 builder = gtk_builder_new();
218 gnc_builder_add_from_file (builder, "window-autoclear.glade", "auto_clear_start_dialog");
219 data->window = GTK_WIDGET(gtk_builder_get_object (builder, "auto_clear_start_dialog"));
220 title = gnc_autoclear_make_window_name (account);
221 gtk_window_set_title(GTK_WINDOW(data->window), title);
222 g_free (title);
223
224 // Set the name for this dialog so it can be easily manipulated with css
225 gtk_widget_set_name (GTK_WIDGET(data->window), "gnc-id-auto-clear");
226
227 data->show_cleared_splits_button =
228 GTK_WIDGET (gtk_builder_get_object (builder, "show_cleared_splits_button"));
229
230 /* Add amount edit box */
231 data->end_value = GNC_AMOUNT_EDIT(gnc_amount_edit_new());
232
233 currency = xaccAccountGetCommodity (account);
234 print_info = gnc_commodity_print_info (currency, FALSE);
235 gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT(data->end_value), print_info);
236 gnc_amount_edit_set_fraction (GNC_AMOUNT_EDIT(data->end_value),
237 gnc_commodity_get_fraction (currency));
238
239 g_signal_connect(GTK_WIDGET(data->end_value), "activate",
240 G_CALLBACK(gnc_autoclear_window_ok_cb), data);
241
242 box = GTK_BOX(gtk_builder_get_object (builder, "end_value_box"));
243 gtk_box_pack_start(box, GTK_WIDGET(data->end_value), TRUE, TRUE, 0);
244
245 label = GTK_WIDGET(gtk_builder_get_object (builder, "end_label"));
246 gnc_amount_edit_make_mnemonic_target (GNC_AMOUNT_EDIT(data->end_value), label);
247
248 /* pre-fill with current balance */
249 after = xaccAccountGetClearedBalance (data->account);
250 if (gnc_reverse_balance(data->account))
251 after = gnc_numeric_neg(after);
252 gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT (data->end_value), after);
253 gtk_widget_grab_focus(GTK_WIDGET(data->end_value));
254 gnc_amount_edit_select_region (GNC_AMOUNT_EDIT (data->end_value), 0, -1);
255
256 data->status_label = GTK_LABEL(gtk_builder_get_object (builder, "status_label"));
257
258 g_signal_connect (GTK_WIDGET(data->end_value), "changed",
259 G_CALLBACK(clear_status_label_cb), data);
260
261 if (parent != NULL)
262 gtk_window_set_transient_for (GTK_WINDOW (data->window), GTK_WINDOW (parent));
263
264 gtk_builder_connect_signals(builder, data);
265 g_object_unref(G_OBJECT(builder));
266
267 return data;
268}
269
Additional event handling code.
Gnome specific utility functions.
Functions for adding content to a window.
Functions providing a register page for the GnuCash UI.
void xaccAccountCommitEdit(Account *acc)
ThexaccAccountCommitEdit() subroutine is the second phase of a two-phase-commit wrapper for account u...
Definition Account.cpp:1516
void xaccAccountBeginEdit(Account *acc)
The xaccAccountBeginEdit() subroutine is the first phase of a two-phase-commit wrapper for account up...
Definition Account.cpp:1475
gchar * gnc_account_get_full_name(const Account *account)
The gnc_account_get_full_name routine returns the fully qualified name of the account using the given...
Definition Account.cpp:3305
int xaccAccountGetCommoditySCU(const Account *acc)
Return the SCU for the account.
Definition Account.cpp:2745
gnc_numeric xaccAccountGetClearedBalance(const Account *acc)
Get the current balance of the account, only including cleared transactions.
Definition Account.cpp:3474
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account's commodity
Definition Account.cpp:3408
int gnc_commodity_get_fraction(const gnc_commodity *cm)
Retrieve the fraction for the specified commodity.
const GncGUID * guid_null(void)
Returns a GncGUID which is guaranteed to never reference any entity.
Definition guid.cpp:165
void main_window_update_page_name(GncPluginPage *page, const gchar *name_in)
Update the name of the page in the main window.
void gnc_main_window_open_page(GncMainWindow *window, GncPluginPage *page)
Display a data plugin page in a window.
void gnc_ledger_display_refresh(GNCLedgerDisplay *ld)
redisplay/redraw only the indicated window.
GNCLedgerDisplay * gnc_ledger_display_query(Query *query, SplitRegisterType type, SplitRegisterStyle style)
display a general ledger for an arbitrary query
gnc_numeric gnc_numeric_error(GNCNumericErrorCode error_code)
Create a gnc_numeric object that signals the error condition noted by error_code, rather than a numbe...
gnc_numeric gnc_numeric_neg(gnc_numeric a)
Returns a newly created gnc_numeric that is the negative of the given gnc_numeric value.
@ GNC_ERROR_ARG
Argument is not a valid number.
QofQuery * qof_query_merge(QofQuery *q1, QofQuery *q2, QofQueryOp op)
Combine two queries together using the Boolean set (logical) operator 'op'.
void qof_query_set_book(QofQuery *query, QofBook *book)
Set the book to be searched.
void qof_query_destroy(QofQuery *query)
Frees the resources associate with a Query object.
GncPluginPage * gnc_plugin_page_register_new_ledger(GNCLedgerDisplay *ledger)
Create a new "register" plugin page, given a pointer to an already created ledger.
void xaccSplitSetReconcile(Split *split, char recn)
Set the reconcile flag.
#define xaccSplitGetGUID(X)
Definition Split.h:579
#define CREC
The Split has been cleared
Definition Split.h:73
STRUCTS.
The type used to store guids in C.
Definition guid.h:75
The instance data structure for a content plugin.
A Query.
Definition qofquery.cpp:75