GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-plugin-budget.c
1 /* Copyright (C) 2005 Chris Shoemaker <[email protected]>
2  *
3  * gnc-plugin-budget.c --
4  * (based on gnc-plugin-account-tree.c)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, contact:
18  *
19  * Free Software Foundation Voice: +1-617-542-5942
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
21  * Boston, MA 02110-1301, USA [email protected]
22  */
23 
24 #include "config.h"
25 
26 #include <gtk/gtk.h>
27 #include <glib/gi18n.h>
28 #include <string.h>
29 
30 #include "gnc-plugin-budget.h"
31 #include "gnc-plugin-page-budget.h"
32 #include "gnc-tree-model-budget.h"
33 
34 #include "qof.h"
35 #include "gnc-ui-util.h"
36 #include "gnc-ui.h"
37 #include "gnc-component-manager.h"
38 
39 #define PLUGIN_ACTIONS_NAME "gnc-plugin-budget-actions"
40 #define PLUGIN_UI_FILENAME "gnc-plugin-budget-ui.xml"
41 
42 static QofLogModule log_module = GNC_MOD_GUI;
43 
44 static void gnc_plugin_budget_class_init (GncPluginBudgetClass *klass);
45 static void gnc_plugin_budget_init (GncPluginBudget *plugin);
46 static void gnc_plugin_budget_finalize (GObject *object);
47 
48 /* Command Callbacks */
49 static void gnc_plugin_budget_cmd_new_budget (GtkAction *action,
51 static void gnc_plugin_budget_cmd_open_budget (GtkAction *action,
53 static void gnc_plugin_budget_cmd_copy_budget (GtkAction *action,
55 
56 static GtkActionEntry gnc_plugin_actions [] =
57 {
58  {
59  "NewBudgetAction", NULL, N_("New Budget"), NULL,
60  N_("Create a new Budget"),
61  G_CALLBACK (gnc_plugin_budget_cmd_new_budget)
62  },
63 
64  {
65  "OpenBudgetAction", NULL, N_("Open Budget"), NULL,
66  N_("Open an existing Budget"),
67  G_CALLBACK (gnc_plugin_budget_cmd_open_budget)
68  },
69 
70  {
71  "CopyBudgetAction", NULL, N_("Copy Budget"), NULL,
72  N_("Copy an existing Budget"),
73  G_CALLBACK (gnc_plugin_budget_cmd_copy_budget)
74  },
75 };
76 static guint gnc_plugin_n_actions = G_N_ELEMENTS (gnc_plugin_actions);
77 
78 typedef struct GncPluginBudgetPrivate
79 {
80  gpointer dummy;
82 
83 #define GNC_PLUGIN_BUDGET_GET_PRIVATE(o) \
84  (G_TYPE_INSTANCE_GET_PRIVATE ((o), GNC_TYPE_PLUGIN_BUDGET, GncPluginBudgetPrivate))
85 
86 static GObjectClass *parent_class = NULL;
87 
88 GType
89 gnc_plugin_budget_get_type (void)
90 {
91  static GType gnc_plugin_budget_type = 0;
92 
93  if (!gnc_plugin_budget_type)
94  {
95  static const GTypeInfo our_info =
96  {
97  sizeof (GncPluginBudgetClass),
98  NULL, /* base_init */
99  NULL, /* base_finalize */
100  (GClassInitFunc) gnc_plugin_budget_class_init,
101  NULL, /* class_finalize */
102  NULL, /* class_data */
103  sizeof (GncPluginBudget),
104  0, /* n_preallocs */
105  (GInstanceInitFunc) gnc_plugin_budget_init
106  };
107 
108  gnc_plugin_budget_type = g_type_register_static(
109  GNC_TYPE_PLUGIN, "GncPluginBudget", &our_info, 0);
110  }
111 
112  return gnc_plugin_budget_type;
113 }
114 
115 GncPlugin * gnc_plugin_budget_new (void)
116 {
117  GncPluginBudget *plugin;
118  ENTER(" ");
119 
120  /* Reference the budget page plugin to ensure it exists in the gtk
121  * type system. */
122  GNC_TYPE_PLUGIN_PAGE_BUDGET;
123 
124  plugin = g_object_new (GNC_TYPE_PLUGIN_BUDGET, NULL);
125  LEAVE(" ");
126  return GNC_PLUGIN (plugin);
127 }
128 
129 static void
130 gnc_plugin_budget_class_init (GncPluginBudgetClass *klass)
131 {
132  GObjectClass *object_class = G_OBJECT_CLASS (klass);
133  GncPluginClass *plugin_class = GNC_PLUGIN_CLASS (klass);
134 
135  ENTER (" ");
136  parent_class = g_type_class_peek_parent (klass);
137  object_class->finalize = gnc_plugin_budget_finalize;
138 
139  plugin_class->plugin_name = GNC_PLUGIN_BUDGET_NAME;
140  plugin_class->actions_name = PLUGIN_ACTIONS_NAME;
141  plugin_class->actions = gnc_plugin_actions;
142  plugin_class->n_actions = gnc_plugin_n_actions;
143  plugin_class->ui_filename = PLUGIN_UI_FILENAME;
144 
145  g_type_class_add_private(klass, sizeof(GncPluginBudgetPrivate));
146  LEAVE (" ");
147 }
148 
149 static void
150 gnc_plugin_budget_init(GncPluginBudget *plugin)
151 {
152 }
153 
154 static void
155 gnc_plugin_budget_finalize(GObject *object)
156 {
157  g_return_if_fail(GNC_IS_PLUGIN_BUDGET (object));
158 
159  ENTER(" ");
160  (parent_class->finalize)(object);
161  LEAVE(" ");
162 
163 }
164 
165 /************************************************************
166  * Command Callbacks *
167  ************************************************************/
168 
169 /* Make a new budget; put it in a page; open the page. */
170 static void
171 gnc_plugin_budget_cmd_new_budget (GtkAction *action,
173 {
174  GncBudget *budget;
175  GncPluginPage *page;
176 
177  g_return_if_fail (data != NULL);
178 
179  budget = gnc_budget_new(gnc_get_current_book());
180  page = gnc_plugin_page_budget_new(budget);
181  gnc_main_window_open_page (data->window, page);
182 }
183 
184 /* If only one budget exists, open it; otherwise user selects one to open */
185 static void
186 gnc_plugin_budget_cmd_open_budget (GtkAction *action,
188 {
189  guint count;
190  QofBook *book;
191  GncBudget *bgt = NULL;
192  QofCollection *col;
193  g_return_if_fail (data != NULL);
194 
195  book = gnc_get_current_book();
196  col = qof_book_get_collection(book, GNC_ID_BUDGET);
197  count = qof_collection_count(col);
198  if (count > 0)
199  {
200  if (count == 1)
201  {
202  bgt = gnc_budget_get_default(book);
203  }
204  else
205  {
206  bgt = gnc_budget_gui_select_budget(book);
207  }
208 
209  if (bgt) gnc_main_window_open_page(
210  data->window, gnc_plugin_page_budget_new(bgt));
211  }
212  else /* if no budgets exist yet, just open a new budget */
213  {
214  gnc_plugin_budget_cmd_new_budget(action, data);
215  }
216 }
217 
218 /* If only one budget exists, create a copy of it; otherwise user selects one to copy */
219 static void
220 gnc_plugin_budget_cmd_copy_budget (GtkAction *action,
222 {
223  guint count;
224  QofBook *book;
225  GncBudget *bgt = NULL;
226  QofCollection *col;
227  g_return_if_fail (data != NULL);
228 
229  book = gnc_get_current_book();
230  col = qof_book_get_collection(book, GNC_ID_BUDGET);
231  count = qof_collection_count(col);
232  if (count > 0)
233  {
234  if (count == 1)
235  {
236  bgt = gnc_budget_get_default(book);
237  }
238  else
239  {
240  bgt = gnc_budget_gui_select_budget(book);
241  }
242 
243  if (bgt)
244  {
245  GncBudget* copy;
246  gchar* name;
247 
248  copy = gnc_budget_clone(bgt);
249  name = g_strdup_printf("Copy of %s", gnc_budget_get_name(bgt));
250  gnc_budget_set_name(copy, name);
251  g_free(name);
252 
254  data->window, gnc_plugin_page_budget_new(copy));
255  }
256  }
257  else /* if no budgets exist yet, just open a new budget */
258  {
259  gnc_plugin_budget_cmd_new_budget(action, data);
260  }
261 }
262 
263 /************************************************************
264  * Other Functions *
265  ************************************************************/
266 
267 static void
268 row_activated_cb(GtkTreeView *tv, GtkTreePath *path, GtkTreeViewColumn *column,
269  gpointer data)
270 {
271  gtk_dialog_response(GTK_DIALOG(data), GTK_RESPONSE_OK);
272 }
273 
274 GncBudget *
275 gnc_budget_gui_select_budget(QofBook *book)
276 {
277  GncBudget *bgt;
278  GtkDialog *dlg;
279  GtkTreeView *tv;
280  GtkTreeIter iter;
281  GtkTreeSelection *sel;
282  GtkTreeModel *tm;
283  gint response;
284  gboolean ok;
285 
286  dlg = GTK_DIALOG(gtk_dialog_new_with_buttons(
287  _("Select a Budget"), NULL, GTK_DIALOG_MODAL,
288  GTK_STOCK_OK, GTK_RESPONSE_OK,
289  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL));
290 
291  tv = GTK_TREE_VIEW(gtk_tree_view_new());
292  sel = gtk_tree_view_get_selection(tv);
293  gtk_tree_selection_set_mode(sel, GTK_SELECTION_BROWSE);
294  g_signal_connect(tv, "row-activated", G_CALLBACK(row_activated_cb), dlg);
295  tm = gnc_tree_model_budget_new(book);
296  gnc_tree_view_budget_set_model(tv, tm);
297  g_object_unref(tm);
298  gtk_container_add(GTK_CONTAINER(dlg->vbox), GTK_WIDGET(tv));
299  gtk_widget_show_all(GTK_WIDGET(dlg));
300 
301  bgt = NULL;
302  response = gtk_dialog_run(dlg);
303  switch (response)
304  {
305  case GTK_RESPONSE_OK:
306  ok = gtk_tree_selection_get_selected(sel, &tm, &iter);
307  if (ok)
308  {
309  bgt = gnc_tree_model_budget_get_budget(tm, &iter);
310  }
311  break;
312  default:
313  break;
314  }
315 
316  gtk_widget_destroy(GTK_WIDGET(dlg));
317  return bgt;
318 }
319 
utility functions for the GnuCash UI
GncBudget * gnc_budget_new(QofBook *book)
Definition: gnc-budget.c:289
GncPluginPage * gnc_plugin_page_budget_new(GncBudget *budget)
#define ENTER(format, args...)
Definition: qoflog.h:261
const gchar * ui_filename
Definition: gnc-plugin.h:137
void gnc_main_window_open_page(GncMainWindow *window, GncPluginPage *page)
GtkActionEntry * actions
Definition: gnc-plugin.h:122
GncBudget * gnc_budget_clone(const GncBudget *old_b)
Definition: gnc-budget.c:340
void gnc_budget_set_name(GncBudget *budget, const gchar *name)
Definition: gnc-budget.c:371
const gchar * actions_name
Definition: gnc-plugin.h:119
const gchar * plugin_name
Definition: gnc-plugin.h:112
#define PLUGIN_ACTIONS_NAME
#define LEAVE(format, args...)
Definition: qoflog.h:271
QofCollection * qof_book_get_collection(const QofBook *, QofIdType)
guint qof_collection_count(const QofCollection *col)
provides some utilities for working with the list of budgets in a book.
#define PLUGIN_UI_FILENAME
const gchar * QofLogModule
Definition: qofid.h:89