/
gnc-plugin-budget.c
319 lines (270 loc) · 9.04 KB
/
gnc-plugin-budget.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* Copyright (C) 2005 Chris Shoemaker <c.shoemaker@cox.net>
*
* gnc-plugin-budget.c --
* (based on gnc-plugin-account-tree.c)
*
* 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
*/
#include "config.h"
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <string.h>
#include "gnc-plugin-budget.h"
#include "gnc-plugin-page-budget.h"
#include "gnc-tree-model-budget.h"
#include "qof.h"
#include "gnc-ui-util.h"
#include "gnc-ui.h"
#include "gnc-component-manager.h"
#define PLUGIN_ACTIONS_NAME "gnc-plugin-budget-actions"
#define PLUGIN_UI_FILENAME "gnc-plugin-budget-ui.xml"
static QofLogModule log_module = GNC_MOD_GUI;
static void gnc_plugin_budget_class_init (GncPluginBudgetClass *klass);
static void gnc_plugin_budget_init (GncPluginBudget *plugin);
static void gnc_plugin_budget_finalize (GObject *object);
/* Command Callbacks */
static void gnc_plugin_budget_cmd_new_budget (GtkAction *action,
GncMainWindowActionData *data);
static void gnc_plugin_budget_cmd_open_budget (GtkAction *action,
GncMainWindowActionData *data);
static void gnc_plugin_budget_cmd_copy_budget (GtkAction *action,
GncMainWindowActionData *data);
static GtkActionEntry gnc_plugin_actions [] =
{
{
"NewBudgetAction", NULL, N_("New Budget"), NULL,
N_("Create a new Budget"),
G_CALLBACK (gnc_plugin_budget_cmd_new_budget)
},
{
"OpenBudgetAction", NULL, N_("Open Budget"), NULL,
N_("Open an existing Budget"),
G_CALLBACK (gnc_plugin_budget_cmd_open_budget)
},
{
"CopyBudgetAction", NULL, N_("Copy Budget"), NULL,
N_("Copy an existing Budget"),
G_CALLBACK (gnc_plugin_budget_cmd_copy_budget)
},
};
static guint gnc_plugin_n_actions = G_N_ELEMENTS (gnc_plugin_actions);
typedef struct GncPluginBudgetPrivate
{
gpointer dummy;
} GncPluginBudgetPrivate;
#define GNC_PLUGIN_BUDGET_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), GNC_TYPE_PLUGIN_BUDGET, GncPluginBudgetPrivate))
static GObjectClass *parent_class = NULL;
GType
gnc_plugin_budget_get_type (void)
{
static GType gnc_plugin_budget_type = 0;
if (!gnc_plugin_budget_type)
{
static const GTypeInfo our_info =
{
sizeof (GncPluginBudgetClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) gnc_plugin_budget_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GncPluginBudget),
0, /* n_preallocs */
(GInstanceInitFunc) gnc_plugin_budget_init
};
gnc_plugin_budget_type = g_type_register_static(
GNC_TYPE_PLUGIN, "GncPluginBudget", &our_info, 0);
}
return gnc_plugin_budget_type;
}
GncPlugin * gnc_plugin_budget_new (void)
{
GncPluginBudget *plugin;
ENTER(" ");
/* Reference the budget page plugin to ensure it exists in the gtk
* type system. */
GNC_TYPE_PLUGIN_PAGE_BUDGET;
plugin = g_object_new (GNC_TYPE_PLUGIN_BUDGET, NULL);
LEAVE(" ");
return GNC_PLUGIN (plugin);
}
static void
gnc_plugin_budget_class_init (GncPluginBudgetClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GncPluginClass *plugin_class = GNC_PLUGIN_CLASS (klass);
ENTER (" ");
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = gnc_plugin_budget_finalize;
plugin_class->plugin_name = GNC_PLUGIN_BUDGET_NAME;
plugin_class->actions_name = PLUGIN_ACTIONS_NAME;
plugin_class->actions = gnc_plugin_actions;
plugin_class->n_actions = gnc_plugin_n_actions;
plugin_class->ui_filename = PLUGIN_UI_FILENAME;
g_type_class_add_private(klass, sizeof(GncPluginBudgetPrivate));
LEAVE (" ");
}
static void
gnc_plugin_budget_init(GncPluginBudget *plugin)
{
}
static void
gnc_plugin_budget_finalize(GObject *object)
{
g_return_if_fail(GNC_IS_PLUGIN_BUDGET (object));
ENTER(" ");
(parent_class->finalize)(object);
LEAVE(" ");
}
/************************************************************
* Command Callbacks *
************************************************************/
/* Make a new budget; put it in a page; open the page. */
static void
gnc_plugin_budget_cmd_new_budget (GtkAction *action,
GncMainWindowActionData *data)
{
GncBudget *budget;
GncPluginPage *page;
g_return_if_fail (data != NULL);
budget = gnc_budget_new(gnc_get_current_book());
page = gnc_plugin_page_budget_new(budget);
gnc_main_window_open_page (data->window, page);
}
/* If only one budget exists, open it; otherwise user selects one to open */
static void
gnc_plugin_budget_cmd_open_budget (GtkAction *action,
GncMainWindowActionData *data)
{
guint count;
QofBook *book;
GncBudget *bgt = NULL;
QofCollection *col;
g_return_if_fail (data != NULL);
book = gnc_get_current_book();
col = qof_book_get_collection(book, GNC_ID_BUDGET);
count = qof_collection_count(col);
if (count > 0)
{
if (count == 1)
{
bgt = gnc_budget_get_default(book);
}
else
{
bgt = gnc_budget_gui_select_budget(book);
}
if (bgt) gnc_main_window_open_page(
data->window, gnc_plugin_page_budget_new(bgt));
}
else /* if no budgets exist yet, just open a new budget */
{
gnc_plugin_budget_cmd_new_budget(action, data);
}
}
/* If only one budget exists, create a copy of it; otherwise user selects one to copy */
static void
gnc_plugin_budget_cmd_copy_budget (GtkAction *action,
GncMainWindowActionData *data)
{
guint count;
QofBook *book;
GncBudget *bgt = NULL;
QofCollection *col;
g_return_if_fail (data != NULL);
book = gnc_get_current_book();
col = qof_book_get_collection(book, GNC_ID_BUDGET);
count = qof_collection_count(col);
if (count > 0)
{
if (count == 1)
{
bgt = gnc_budget_get_default(book);
}
else
{
bgt = gnc_budget_gui_select_budget(book);
}
if (bgt)
{
GncBudget* copy;
gchar* name;
copy = gnc_budget_clone(bgt);
name = g_strdup_printf("Copy of %s", gnc_budget_get_name(bgt));
gnc_budget_set_name(copy, name);
g_free(name);
gnc_main_window_open_page(
data->window, gnc_plugin_page_budget_new(copy));
}
}
else /* if no budgets exist yet, just open a new budget */
{
gnc_plugin_budget_cmd_new_budget(action, data);
}
}
/************************************************************
* Other Functions *
************************************************************/
static void
row_activated_cb(GtkTreeView *tv, GtkTreePath *path, GtkTreeViewColumn *column,
gpointer data)
{
gtk_dialog_response(GTK_DIALOG(data), GTK_RESPONSE_OK);
}
GncBudget *
gnc_budget_gui_select_budget(QofBook *book)
{
GncBudget *bgt;
GtkDialog *dlg;
GtkTreeView *tv;
GtkTreeIter iter;
GtkTreeSelection *sel;
GtkTreeModel *tm;
gint response;
gboolean ok;
dlg = GTK_DIALOG(gtk_dialog_new_with_buttons(
_("Select a Budget"), NULL, GTK_DIALOG_MODAL,
_("_OK"), GTK_RESPONSE_OK,
_("_Cancel"), GTK_RESPONSE_CANCEL, NULL));
tv = GTK_TREE_VIEW(gtk_tree_view_new());
sel = gtk_tree_view_get_selection(tv);
gtk_tree_selection_set_mode(sel, GTK_SELECTION_BROWSE);
g_signal_connect(tv, "row-activated", G_CALLBACK(row_activated_cb), dlg);
tm = gnc_tree_model_budget_new(book);
gnc_tree_view_budget_set_model(tv, tm);
g_object_unref(tm);
gtk_container_add(GTK_CONTAINER (gtk_dialog_get_content_area (dlg)), GTK_WIDGET(tv));
gtk_widget_show_all(GTK_WIDGET(dlg));
bgt = NULL;
response = gtk_dialog_run(dlg);
switch (response)
{
case GTK_RESPONSE_OK:
ok = gtk_tree_selection_get_selected(sel, &tm, &iter);
if (ok)
{
bgt = gnc_tree_model_budget_get_budget(tm, &iter);
}
break;
default:
break;
}
gtk_widget_destroy(GTK_WIDGET(dlg));
return bgt;
}