GnuCash  5.6-150-g038405b370+
gnc-plugin-page-sx-list.cpp
1 /********************************************************************\
2  * gnc-plugin-page-sx-list.cpp : scheduled transaction plugin *
3  * *
4  * Copyright (C) 2006 Joshua Sled <jsled@asynchronous.org> *
5  * Copyright (C) 2011 Robert Fewell *
6  * *
7  * This program is free software; you can redistribute it and/or *
8  * modify it under the terms of version 2 and/or version 3 of the *
9  * GNU General Public License as published by the Free Software *
10  * Foundation. *
11  * *
12  * As a special exception, permission is granted to link the binary *
13  * module resultant from this code with the OpenSSL project's *
14  * "OpenSSL" library (or modified versions of it that use the same *
15  * license as the "OpenSSL" library), and distribute the linked *
16  * executable. You must obey the GNU General Public License in all *
17  * respects for all of the code used other than "OpenSSL". If you *
18  * modify this file, you may extend this exception to your version *
19  * of the file, but you are not obligated to do so. If you do not *
20  * wish to do so, delete this exception statement from your version *
21  * of this file. *
22  * *
23  * This program is distributed in the hope that it will be useful, *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
26  * GNU General Public License for more details. *
27  * *
28  * You should have received a copy of the GNU General Public License*
29  * along with this program; if not, contact: *
30  * *
31  * Free Software Foundation Voice: +1-617-542-5942 *
32  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
33  * Boston, MA 02110-1301, USA gnu@gnu.org *
34 \********************************************************************/
35 
44 #include <config.h>
45 
46 #include <gtk/gtk.h>
47 #include <glib.h>
48 #include <glib/gi18n.h>
49 
50 #include <gnc-gobject-utils.h>
51 #include "SX-book.h"
52 #include "Split.h"
53 #include "Transaction.h"
54 #include "dialog-sx-editor.h"
55 #include "dialog-utils.h"
56 #include "gnc-commodity.h"
57 #include "gnc-component-manager.h"
58 #include "gnc-date.h"
59 #include "gnc-dense-cal.h"
60 #include "gnc-engine.h"
61 #include "gnc-event.h"
62 #include "gnc-glib-utils.h"
63 #include "gnc-icons.h"
64 #include "gnc-main-window.h"
65 #include "gnc-plugin.h"
66 #include "gnc-plugin-page-sx-list.h"
67 #include "gnc-prefs.h"
68 #include "gnc-session.h"
69 #include "gnc-sx-instance-dense-cal-adapter.h"
70 #include "gnc-sx-instance-model.h"
71 #include "gnc-sx-list-tree-model-adapter.h"
72 #include "gnc-tree-view-sx-list.h"
73 #include "gnc-ui-util.h"
74 #include "gnc-ui.h"
75 #include "gnc-window.h"
76 
77 #undef G_LOG_DOMAIN
78 #define G_LOG_DOMAIN "gnc.gui.plugin-page.sx-list"
79 
80 G_GNUC_UNUSED static QofLogModule log_module = GNC_MOD_GUI_SX;
81 
82 #define PLUGIN_PAGE_SX_LIST_CM_CLASS "plugin-page-sx-list"
83 #define STATE_SECTION "SX Transaction List"
84 #define GNC_PREF_DIVIDER_POS "divider-position"
85 #define GNC_PREF_NUM_OF_MONTHS "number-of-months"
86 
88 {
89  gboolean disposed;
90 
91  GtkWidget* widget;
92  gint gnc_component_id;
93 
94  GncSxInstanceDenseCalAdapter *dense_cal_model;
95  GncDenseCal* gdcal;
96 
97  GncSxInstanceModel* instances;
98  GtkTreeView* tree_view;
99  GList *selected_list;
100 
102 
103 G_DEFINE_TYPE_WITH_PRIVATE(GncPluginPageSxList, gnc_plugin_page_sx_list, GNC_TYPE_PLUGIN_PAGE)
104 
105 #define GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(o) \
106  ((GncPluginPageSxListPrivate*)gnc_plugin_page_sx_list_get_instance_private ((GncPluginPageSxList*)o))
107 
108 /************************************************************
109  * Prototypes *
110  ************************************************************/
111 /* Plugin Actions */
112 static void gnc_plugin_page_sx_list_dispose (GObject *object);
113 static void gnc_plugin_page_sx_list_finalize (GObject *object);
114 
115 static GtkWidget *gnc_plugin_page_sx_list_create_widget (GncPluginPage *plugin_page);
116 static void gnc_plugin_page_sx_list_destroy_widget (GncPluginPage *plugin_page);
117 static void gnc_plugin_page_sx_list_save_page (GncPluginPage *plugin_page, GKeyFile *file, const gchar *group);
118 static GncPluginPage *gnc_plugin_page_sx_list_recreate_page (GtkWidget *window, GKeyFile *file, const gchar *group);
119 
120 static void gppsl_row_activated_cb (GtkTreeView *tree_view, GtkTreePath *path,
121  GtkTreeViewColumn *column, gpointer user_data);
122 
123 static void gnc_plugin_page_sx_list_cmd_new (GSimpleAction *simple, GVariant *paramter, gpointer user_data);
124 static void gnc_plugin_page_sx_list_cmd_edit (GSimpleAction *simple, GVariant *paramter, gpointer user_data);
125 static void gnc_plugin_page_sx_list_cmd_delete (GSimpleAction *simple, GVariant *paramter, gpointer user_data);
126 static void gnc_plugin_page_sx_list_cmd_refresh (GSimpleAction *simple, GVariant *paramter, gpointer user_data);
127 static void gnc_plugin_page_sx_list_cmd_save_layout (GSimpleAction *simple, GVariant *paramter, gpointer user_data);
128 static void gnc_plugin_page_sx_list_cmd_edit_tax_options (GSimpleAction *simple, GVariant *paramter, gpointer user_data);
129 
130 /* Command callbacks */
131 static GActionEntry gnc_plugin_page_sx_list_actions [] =
132 {
133  { "SxListAction", nullptr, nullptr, nullptr, nullptr },
134  { "SxListNewAction", gnc_plugin_page_sx_list_cmd_new, nullptr, nullptr, nullptr },
135  { "SxListEditAction", gnc_plugin_page_sx_list_cmd_edit, nullptr, nullptr, nullptr },
136  { "SxListDeleteAction", gnc_plugin_page_sx_list_cmd_delete, nullptr, nullptr, nullptr },
137  { "ViewRefreshAction", gnc_plugin_page_sx_list_cmd_refresh, nullptr, nullptr, nullptr },
138  { "ViewSaveLayoutAction", gnc_plugin_page_sx_list_cmd_save_layout, nullptr, nullptr, nullptr },
139  { "EditTaxOptionsAction", gnc_plugin_page_sx_list_cmd_edit_tax_options, nullptr, nullptr, nullptr },
140 };
142 static guint gnc_plugin_page_sx_list_n_actions = G_N_ELEMENTS(gnc_plugin_page_sx_list_actions);
143 
145 static const gchar *gnc_plugin_load_ui_items [] =
146 {
147  "FilePlaceholder3",
148  "EditPlaceholder3",
149  "EditPlaceholder5",
150  "ViewPlaceholder4",
151  "SchedulePlaceholder0",
152  nullptr,
153 };
154 
155 
158 {
159  GncPluginPageSxList *plugin_page;
160  const GList *object = gnc_gobject_tracking_get_list (GNC_PLUGIN_PAGE_SX_LIST_NAME);
161  if (object && GNC_IS_PLUGIN_PAGE_SX_LIST(object->data))
162  plugin_page = GNC_PLUGIN_PAGE_SX_LIST(object->data);
163  else
164  {
165  plugin_page = GNC_PLUGIN_PAGE_SX_LIST(g_object_new (GNC_TYPE_PLUGIN_PAGE_SX_LIST, nullptr));
166  }
167  return GNC_PLUGIN_PAGE(plugin_page);
168 }
169 
170 
175 static gboolean
176 gnc_plugin_page_sx_list_focus_widget (GncPluginPage *sx_plugin_page)
177 {
178  if (GNC_IS_PLUGIN_PAGE_SX_LIST(sx_plugin_page))
179  {
180  GncPluginPageSxListPrivate *priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(sx_plugin_page);
181  GtkTreeView *tree_view = priv->tree_view;
182 
183  /* Disable the Transaction Menu */
184  GAction *action = gnc_main_window_find_action (GNC_MAIN_WINDOW(sx_plugin_page->window), "TransactionAction");
185  g_simple_action_set_enabled (G_SIMPLE_ACTION(action), false);
186  /* Enable the Schedule Menu */
187  action = gnc_main_window_find_action (GNC_MAIN_WINDOW(sx_plugin_page->window), "ScheduledAction");
188  g_simple_action_set_enabled (G_SIMPLE_ACTION(action), true);
189  /* Disable the FilePrintAction */
190  action = gnc_main_window_find_action (GNC_MAIN_WINDOW(sx_plugin_page->window), "FilePrintAction");
191  g_simple_action_set_enabled (G_SIMPLE_ACTION(action), false);
192 
193  gnc_main_window_update_menu_and_toolbar (GNC_MAIN_WINDOW(sx_plugin_page->window),
194  sx_plugin_page,
195  gnc_plugin_load_ui_items);
196 
197  if (GTK_IS_TREE_VIEW(tree_view))
198  {
199  if (!gtk_widget_is_focus (GTK_WIDGET(tree_view)))
200  gtk_widget_grab_focus (GTK_WIDGET(tree_view));
201  }
202  }
203  return false;
204 }
205 
206 static void
207 gnc_plugin_page_sx_list_class_init (GncPluginPageSxListClass *klass)
208 {
209  GObjectClass *object_class = G_OBJECT_CLASS(klass);
210  GncPluginPageClass *gnc_plugin_class = GNC_PLUGIN_PAGE_CLASS(klass);
211 
212  object_class->dispose = gnc_plugin_page_sx_list_dispose;
213  object_class->finalize = gnc_plugin_page_sx_list_finalize;
214 
215  gnc_plugin_class->tab_icon = GNC_ICON_ACCOUNT;
216  gnc_plugin_class->plugin_name = GNC_PLUGIN_PAGE_SX_LIST_NAME;
217  gnc_plugin_class->create_widget = gnc_plugin_page_sx_list_create_widget;
218  gnc_plugin_class->destroy_widget = gnc_plugin_page_sx_list_destroy_widget;
219  gnc_plugin_class->save_page = gnc_plugin_page_sx_list_save_page;
220  gnc_plugin_class->recreate_page = gnc_plugin_page_sx_list_recreate_page;
221  gnc_plugin_class->focus_page_function = gnc_plugin_page_sx_list_focus_widget;
222 }
223 
224 
225 static void
226 gnc_plugin_page_sx_list_init (GncPluginPageSxList *plugin_page)
227 {
228  GSimpleActionGroup *simple_action_group;
229  GncPluginPage *parent;
230 
231  /* Init parent declared variables */
232  parent = GNC_PLUGIN_PAGE(plugin_page);
233  g_object_set(G_OBJECT(plugin_page),
234  "page-name", _("Scheduled Transactions"),
235  "ui-description", "gnc-plugin-page-sx-list.ui",
236  nullptr);
237 
238  gnc_plugin_page_add_book (parent, gnc_get_current_book());
239  simple_action_group = gnc_plugin_page_create_action_group (parent, "GncPluginPageSxListActions");
240  g_action_map_add_action_entries (G_ACTION_MAP(simple_action_group),
241  gnc_plugin_page_sx_list_actions,
242  gnc_plugin_page_sx_list_n_actions,
243  plugin_page);
244 }
245 
246 
247 static void
248 gnc_plugin_page_sx_list_dispose (GObject *object)
249 {
250  GncPluginPageSxList *page;
252 
253  page = GNC_PLUGIN_PAGE_SX_LIST(object);
254  g_return_if_fail (GNC_IS_PLUGIN_PAGE_SX_LIST(page));
255  priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(page);
256  g_return_if_fail (priv != nullptr);
257 
258  g_return_if_fail (!priv->disposed);
259  priv->disposed = true;
260 
261  g_object_unref (G_OBJECT(priv->dense_cal_model));
262  priv->dense_cal_model = nullptr;
263  g_object_unref (GTK_WIDGET(priv->gdcal));
264  priv->gdcal = nullptr;
265  g_object_unref (G_OBJECT(priv->instances));
266  priv->instances = nullptr;
267 
268  G_OBJECT_CLASS(gnc_plugin_page_sx_list_parent_class)->dispose (object);
269 }
270 
271 
272 static void
273 gnc_plugin_page_sx_list_finalize (GObject *object)
274 {
275  GncPluginPageSxList *page;
277 
278  page = GNC_PLUGIN_PAGE_SX_LIST(object);
279  g_return_if_fail (GNC_IS_PLUGIN_PAGE_SX_LIST(page));
280  priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(page);
281  g_return_if_fail (priv != nullptr);
282 
283  G_OBJECT_CLASS(gnc_plugin_page_sx_list_parent_class)->finalize (object);
284 }
285 
286 
287 /* Virtual Functions */
288 static void
289 gnc_plugin_page_sx_list_refresh_cb (GHashTable *changes, gpointer user_data)
290 {
291  auto page = GNC_PLUGIN_PAGE_SX_LIST(user_data);
293 
294  g_return_if_fail (GNC_IS_PLUGIN_PAGE_SX_LIST(page));
295 
296  /* We're only looking for forced updates here. */
297  if (changes)
298  return;
299 
300  priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(page);
301  gtk_widget_queue_draw (priv->widget);
302 }
303 
304 
305 static void
306 gnc_plugin_page_sx_list_close_cb (gpointer user_data)
307 {
308  GncPluginPage *plugin_page = GNC_PLUGIN_PAGE(user_data);
309  gnc_main_window_close_page (plugin_page);
310 }
311 
312 
313 static void
314 gppsl_selection_changed_cb (GtkTreeSelection *selection, gpointer user_data)
315 {
316  GncPluginPage *page;
317  GAction *edit_action, *delete_action;
318  gboolean selection_state = true;
319 
320  page = GNC_PLUGIN_PAGE(user_data);
321  edit_action = gnc_plugin_page_get_action (page, "SxListEditAction");
322  delete_action = gnc_plugin_page_get_action (page, "SxListDeleteAction");
323  selection_state = gtk_tree_selection_count_selected_rows (selection) == 0
324  ? false
325  : true;
326  g_simple_action_set_enabled (G_SIMPLE_ACTION(edit_action), selection_state);
327  g_simple_action_set_enabled (G_SIMPLE_ACTION(delete_action), selection_state);
328 }
329 
330 
331 static void
332 gppsl_update_selected_list (GncPluginPageSxList *page, gboolean reset, SchedXaction *sx)
333 {
334  GncPluginPageSxListPrivate *priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(page);
335 
336  if (reset && priv->selected_list)
337  {
338  g_list_free (priv->selected_list);
339  priv->selected_list = nullptr;
340  }
341  if (sx)
342  priv->selected_list = g_list_prepend (priv->selected_list, sx);
343 }
344 
345 
346 static void
347 gppsl_model_populated_cb (GtkTreeModel *tree_model, GncPluginPageSxList *page)
348 {
349  GncPluginPageSxListPrivate *priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(page);
350  GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(priv->tree_view));
351  gboolean found = false;
352 
353  if (priv->selected_list)
354  {
355  // walk the list to see if we can reselect the sx
356  for (GList *list = priv->selected_list; list != nullptr; list = list->next)
357  {
358  SchedXaction *sx = GNC_SCHEDXACTION(list->data);
359  GtkTreePath *path = gtk_tree_path_new_first ();
360 
361  // loop through the model trying to find selected sx's
362  while (gnc_tree_view_path_is_valid (GNC_TREE_VIEW(priv->tree_view), path))
363  {
364  SchedXaction *sx_tmp = gnc_tree_view_sx_list_get_sx_from_path (
365  GNC_TREE_VIEW_SX_LIST(priv->tree_view), path);
366  if (sx_tmp == sx)
367  {
368  found = true;
369  break;
370  }
371  gtk_tree_path_next (path);
372  }
373  if (found)
374  gtk_tree_selection_select_path (selection, path);
375 
376  gtk_tree_path_free (path);
377  }
378  }
379  // this could be on load or if sx is deleted
380  if (!found)
381  {
382  GtkTreePath *path = gtk_tree_path_new_first ();
383  gtk_tree_selection_select_path (selection, path);
384  gtk_tree_path_free (path);
385  }
386 }
387 
388 
389 static void
390 treeview_popup (GtkTreeView *treeview, GdkEvent *event, GncPluginPageSxList *page)
391 {
392  GncPluginPageSxListPrivate *priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(page);
393  GtkWidget *menu, *menuitem;
394  gchar *full_action_name;
395  const gchar *group_name = gnc_plugin_page_get_simple_action_group_name (GNC_PLUGIN_PAGE(page));
396 
397  menu = gtk_menu_new();
398 
399  menuitem = gtk_menu_item_new_with_mnemonic (_("_New Schedule"));
400  full_action_name = g_strconcat (group_name, ".SxListNewAction", nullptr);
401  gtk_actionable_set_action_name (GTK_ACTIONABLE(menuitem), full_action_name);
402  g_free (full_action_name);
403  gtk_menu_shell_append (GTK_MENU_SHELL(menu), menuitem);
404 
405  menuitem = gtk_menu_item_new_with_mnemonic (_("_Edit Schedule"));
406  full_action_name = g_strconcat (group_name, ".SxListEditAction", nullptr);
407  gtk_actionable_set_action_name (GTK_ACTIONABLE(menuitem), full_action_name);
408  g_free (full_action_name);
409  gtk_menu_shell_append (GTK_MENU_SHELL(menu), menuitem);
410 
411  menuitem = gtk_menu_item_new_with_mnemonic (_("_Delete Schedule"));
412  full_action_name = g_strconcat (group_name, ".SxListDeleteAction", nullptr);
413  gtk_actionable_set_action_name (GTK_ACTIONABLE(menuitem), full_action_name);
414  g_free (full_action_name);
415  gtk_menu_shell_append (GTK_MENU_SHELL(menu), menuitem);
416 
417  gtk_menu_attach_to_widget (GTK_MENU(menu), GTK_WIDGET(priv->tree_view), nullptr);
418  gtk_widget_show_all (menu);
419  gtk_menu_popup_at_pointer (GTK_MENU(menu), event);
420 }
421 
422 static gboolean
423 treeview_button_press (GtkTreeView *treeview, GdkEvent *event,
424  GncPluginPageSxList *page)
425 {
426  GncPluginPageSxListPrivate *priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(page);
427  GtkTreeView *tree_view = GTK_TREE_VIEW(priv->tree_view);
428 
429  if (event->type == GDK_BUTTON_PRESS)
430  {
431  GdkEventButton *event_button = (GdkEventButton*)event;
432  if (event_button->button == GDK_BUTTON_SECONDARY)
433  {
434  GtkTreePath *path = nullptr;
435  if (gtk_tree_view_get_path_at_pos (priv->tree_view, event_button->x, event_button->y,
436  &path, nullptr, nullptr, nullptr))
437  {
438  GtkTreeSelection *selection = gtk_tree_view_get_selection (priv->tree_view);
439 
440  if (!gtk_tree_selection_path_is_selected (selection, path))
441  {
442  gtk_tree_selection_unselect_all (selection);
443  gtk_tree_selection_select_path (selection, path);
444  }
445  }
446  gtk_tree_path_free (path);
447 
448  treeview_popup (tree_view, event, page);
449  return true;
450  }
451  }
452  return false;
453 }
454 
455 static gboolean
456 treeview_popup_menu (GtkTreeView *treeview, GncPluginPageSxList *page)
457 {
458  treeview_popup (treeview, nullptr, page);
459  return true;
460 }
461 
462 static GtkWidget *
463 gnc_plugin_page_sx_list_create_widget (GncPluginPage *plugin_page)
464 {
465  GncPluginPageSxList *page;
467  GtkWidget *widget;
468  GtkWidget *vbox;
469  GtkWidget *label;
470  GtkWidget *swin;
471  GtkWindow *window;
472 
473  page = GNC_PLUGIN_PAGE_SX_LIST(plugin_page);
474  priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(page);
475  if (priv->widget != nullptr)
476  return priv->widget;
477 
478  window = GTK_WINDOW(gnc_plugin_page_get_window (GNC_PLUGIN_PAGE(page)));
479 
480  /* Create Vpaned widget for top level */
481  widget = gtk_paned_new (GTK_ORIENTATION_VERTICAL);
482  priv->widget = widget;
483  gtk_widget_show (priv->widget);
484 
485  // Set the name for this widget so it can be easily manipulated with css
486  gtk_widget_set_name (GTK_WIDGET(priv->widget), "gnc-id-sx-page");
487 
488  /* Add vbox and label */
489  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
490  gtk_box_set_homogeneous (GTK_BOX(vbox), false);
491  gtk_paned_pack1 (GTK_PANED(widget), vbox, true, false);
492 
493  label = gtk_label_new (_("Transactions"));
494  gnc_widget_style_context_add_class (GTK_WIDGET(label), "gnc-class-strong");
495  gtk_widget_set_margin_start (GTK_WIDGET(label), 6);
496  gnc_label_set_alignment (label, 0.0, 0);
497  gtk_widget_show (label);
498  gtk_box_pack_start (GTK_BOX(vbox), label, false, false, 0);
499  gtk_widget_show (vbox);
500 
501  /* Create scrolled window for top area */
502  swin = gtk_scrolled_window_new (nullptr, nullptr);
503  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(swin),
504  GTK_POLICY_AUTOMATIC,
505  GTK_POLICY_AUTOMATIC);
506  gtk_box_pack_start (GTK_BOX(vbox), swin, true, true, 5);
507  gtk_widget_show (swin);
508 
509  /* Set the paned position from the preferences, default 160 */
510  gtk_paned_set_position (GTK_PANED(priv->widget),
511  gnc_prefs_get_float (GNC_PREFS_GROUP_SXED,
512  GNC_PREF_DIVIDER_POS));
513 
514  {
515  GDate end;
516  g_date_clear (&end, 1);
517  gnc_gdate_set_today (&end);
518  g_date_add_years (&end, 1);
519  priv->instances = GNC_SX_INSTANCE_MODEL(gnc_sx_get_instances (&end, true));
520  }
521 
522  {
523  GAction *edit_action, *delete_action;
524  edit_action = gnc_plugin_page_get_action (GNC_PLUGIN_PAGE(page), "SxListEditAction");
525  delete_action = gnc_plugin_page_get_action (GNC_PLUGIN_PAGE(page), "SxListDeleteAction");
526  g_simple_action_set_enabled (G_SIMPLE_ACTION(edit_action), false);
527  g_simple_action_set_enabled (G_SIMPLE_ACTION(delete_action), false);
528  }
529 
530  {
531  GtkTreeSelection *selection;
532  GtkTreePath *path = gtk_tree_path_new_first ();
533 
534  priv->tree_view = GTK_TREE_VIEW(gnc_tree_view_sx_list_new (priv->instances));
535  g_object_set (G_OBJECT(priv->tree_view),
536  "state-section", STATE_SECTION,
537  "show-column-menu", true,
538  nullptr);
539  gtk_container_add (GTK_CONTAINER(swin), GTK_WIDGET(priv->tree_view));
540 
541  selection = gtk_tree_view_get_selection (priv->tree_view);
542  gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
543 
544  g_signal_connect (G_OBJECT(selection), "changed",
545  (GCallback)gppsl_selection_changed_cb, (gpointer)page);
546  g_signal_connect (G_OBJECT(priv->tree_view), "row-activated",
547  (GCallback)gppsl_row_activated_cb, (gpointer)page);
548  g_signal_connect (G_OBJECT(gtk_tree_view_get_model (GTK_TREE_VIEW(priv->tree_view))),
549  "model-populated", (GCallback)gppsl_model_populated_cb, (gpointer)page);
550 
551  gtk_tree_selection_select_path (selection, path);
552  gtk_tree_path_free (path);
553  }
554 
555  g_signal_connect (G_OBJECT(priv->tree_view), "button-press-event",
556  G_CALLBACK(treeview_button_press), page);
557  g_signal_connect (G_OBJECT(priv->tree_view), "popup-menu",
558  G_CALLBACK(treeview_popup_menu), page);
559 
560  /* Add vbox and label */
561  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
562  gtk_box_set_homogeneous (GTK_BOX(vbox), false);
563  gtk_paned_pack2 (GTK_PANED(widget), vbox, true, false);
564 
565  label = gtk_label_new (_("Upcoming Transactions"));
566  gnc_widget_style_context_add_class (GTK_WIDGET(label), "gnc-class-strong");
567  gtk_widget_set_margin_start (GTK_WIDGET(label), 6);
568  gnc_label_set_alignment (label, 0.0, 0);
569  gtk_widget_show (label);
570 
571  gtk_box_pack_start (GTK_BOX(vbox), label, false, false, 0);
572  gtk_widget_show (vbox);
573 
574  /* Create scrolled window for bottom area */
575  swin = gtk_scrolled_window_new (nullptr, nullptr);
576  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(swin),
577  GTK_POLICY_AUTOMATIC,
578  GTK_POLICY_AUTOMATIC);
579  gtk_box_pack_start (GTK_BOX(vbox), swin, true, true, 5);
580  gtk_widget_show (swin);
581 
582  {
583  priv->dense_cal_model = gnc_sx_instance_dense_cal_adapter_new (GNC_SX_INSTANCE_MODEL(priv->instances));
584  priv->gdcal = GNC_DENSE_CAL(gnc_dense_cal_new_with_model (window, GNC_DENSE_CAL_MODEL(priv->dense_cal_model)));
585  g_object_ref_sink (priv->gdcal);
586 
587  /* Set number of months from preference, default 12 */
588  gchar *num_months = gnc_prefs_get_string (GNC_PREFS_GROUP_SXED, GNC_PREF_NUM_OF_MONTHS);
589  gnc_dense_cal_set_num_months (priv->gdcal, atoi (num_months));
590  g_free (num_months);
591 
592  gtk_container_add (GTK_CONTAINER(swin), GTK_WIDGET(priv->gdcal));
593  }
594 
595  priv->gnc_component_id = gnc_register_gui_component ("plugin-page-sx-list",
596  gnc_plugin_page_sx_list_refresh_cb,
597  gnc_plugin_page_sx_list_close_cb,
598  page);
599  gnc_gui_component_set_session (priv->gnc_component_id,
600  gnc_get_current_session ());
601 
602  g_signal_connect (G_OBJECT(plugin_page), "inserted",
603  G_CALLBACK(gnc_plugin_page_inserted_cb),
604  nullptr);
605 
606  return priv->widget;
607 }
608 
609 
610 static void
611 gnc_plugin_page_sx_list_destroy_widget (GncPluginPage *plugin_page)
612 {
613  GncPluginPageSxList *page;
615 
616  page = GNC_PLUGIN_PAGE_SX_LIST(plugin_page);
617  priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(page);
618 
619  // Remove the page_changed signal callback
620  gnc_plugin_page_disconnect_page_changed (GNC_PLUGIN_PAGE(plugin_page));
621 
622  // Remove the page focus idle function if present
623  g_idle_remove_by_data (plugin_page);
624 
625  if (priv->widget)
626  {
627  g_object_unref (G_OBJECT(priv->widget));
628  priv->widget = nullptr;
629  }
630 
631  if (priv->selected_list)
632  g_list_free (priv->selected_list);
633 
634  if (priv->gnc_component_id)
635  {
636  gnc_unregister_gui_component (priv->gnc_component_id);
637  priv->gnc_component_id = 0;
638  }
639 }
640 
641 
649 static void
650 gnc_plugin_page_sx_list_save_page (GncPluginPage *plugin_page,
651  GKeyFile *key_file,
652  const gchar *group_name)
653 {
654  GncPluginPageSxList *page;
656 
657  g_return_if_fail (GNC_IS_PLUGIN_PAGE_SX_LIST(plugin_page));
658  g_return_if_fail (key_file != nullptr);
659  g_return_if_fail (group_name != nullptr);
660 
661  page = GNC_PLUGIN_PAGE_SX_LIST(plugin_page);
662  priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(page);
663 
664  g_key_file_set_integer (key_file, group_name, "dense_cal_num_months",
665  gnc_dense_cal_get_num_months (priv->gdcal));
666 
667  g_key_file_set_integer (key_file, group_name, "paned_position",
668  gtk_paned_get_position (GTK_PANED(priv->widget)));
669 }
670 
671 
680 static GncPluginPage *
681 gnc_plugin_page_sx_list_recreate_page (GtkWidget *window,
682  GKeyFile *key_file,
683  const gchar *group_name)
684 {
685  GncPluginPageSxList *page;
687 
688  g_return_val_if_fail (key_file, nullptr);
689  g_return_val_if_fail (group_name, nullptr);
690 
691  /* Create the new page. */
692  page = GNC_PLUGIN_PAGE_SX_LIST(gnc_plugin_page_sx_list_new ());
693  priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(page);
694 
695  /* Install it now so we can them manipulate the created widget */
696  gnc_main_window_open_page (GNC_MAIN_WINDOW(window), GNC_PLUGIN_PAGE(page));
697 
698  {
699  GError *err = nullptr;
700  gint num_months = g_key_file_get_integer (key_file, group_name, "dense_cal_num_months", &err);
701  if (err == nullptr)
702  gnc_dense_cal_set_num_months (priv->gdcal, num_months);
703  else
704  g_error_free (err);
705  }
706 
707  {
708  GError *err = nullptr;
709  gint paned_position = g_key_file_get_integer (key_file, group_name,
710  "paned_position", &err);
711  if (err == nullptr)
712  gtk_paned_set_position (GTK_PANED(priv->widget), paned_position);
713  else
714  g_error_free (err);
715  }
716 
717  return GNC_PLUGIN_PAGE(page);
718 }
719 
720 
721 static void
722 gnc_plugin_page_sx_list_cmd_new (GSimpleAction *simple,
723  GVariant *parameter,
724  gpointer user_data)
725 {
726  auto plugin_page = GNC_PLUGIN_PAGE_SX_LIST(user_data);
727  GtkWindow *window = GTK_WINDOW(gnc_plugin_page_get_window (GNC_PLUGIN_PAGE(plugin_page)));
728  SchedXaction *new_sx;
729  gboolean new_sx_flag = true;
730 
731  new_sx = xaccSchedXactionMalloc (gnc_get_current_book());
732  {
733  GDate now;
734  Recurrence *r = g_new0 (Recurrence, 1);
735  GList *schedule;
736 
737  g_date_clear (&now, 1);
738  gnc_gdate_set_today (&now);
739  recurrenceSet (r, 1, PERIOD_MONTH, &now, WEEKEND_ADJ_NONE);
740  schedule = gnc_sx_get_schedule (new_sx);
741  schedule = g_list_append (schedule, r);
742  gnc_sx_set_schedule (new_sx, schedule);
743  }
744  gnc_ui_scheduled_xaction_editor_dialog_create (window, new_sx, new_sx_flag);
745  gppsl_update_selected_list (plugin_page, true, new_sx);
746 }
747 
748 static void
749 gnc_plugin_page_sx_list_cmd_refresh (GSimpleAction *simple,
750  GVariant *parameter,
751  gpointer user_data)
752 {
753  auto plugin_page = GNC_PLUGIN_PAGE_SX_LIST(user_data);
755 
756  g_return_if_fail (GNC_IS_PLUGIN_PAGE_SX_LIST(plugin_page));
757 
758  priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(plugin_page);
759  gtk_widget_queue_draw (priv->widget);
760 }
761 
762 static void
763 gnc_plugin_page_sx_list_cmd_save_layout (GSimpleAction *simple,
764  GVariant *parameter,
765  gpointer user_data)
766 {
767  auto plugin_page = GNC_PLUGIN_PAGE_SX_LIST(user_data);
769  gchar *num_of_months;
770  gint paned_position;
771 
772  g_return_if_fail (GNC_IS_PLUGIN_PAGE_SX_LIST(plugin_page));
773 
774  priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(plugin_page);
775 
776  num_of_months = g_strdup_printf ("%d", gnc_dense_cal_get_num_months (priv->gdcal));
777  paned_position = gtk_paned_get_position (GTK_PANED(priv->widget));
778 
779  gnc_prefs_set_float (GNC_PREFS_GROUP_SXED, GNC_PREF_DIVIDER_POS,
780  paned_position);
781 
782  gnc_prefs_set_string (GNC_PREFS_GROUP_SXED, GNC_PREF_NUM_OF_MONTHS,
783  num_of_months);
784 
785  g_free (num_of_months);
786 }
787 
788 static void
789 _edit_sx(gpointer data, gpointer user_data)
790 {
791  gnc_ui_scheduled_xaction_editor_dialog_create (GTK_WINDOW(user_data),
792  GNC_SCHEDXACTION(data), false);
793 }
794 
795 static SchedXaction*
796 _argument_reorder_fn (GtkTreePath* list_path_data, GncTreeViewSxList* user_tree_view)
797 {
798  return gnc_tree_view_sx_list_get_sx_from_path (user_tree_view, list_path_data);
799 }
800 
801 
802 static void
803 gnc_plugin_page_sx_list_cmd_edit (GSimpleAction *simple,
804  GVariant *parameter,
805  gpointer user_data)
806 {
807  auto plugin_page = GNC_PLUGIN_PAGE_SX_LIST(user_data);
808  GncPluginPageSxListPrivate *priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(plugin_page);
809  GtkWindow *window = GTK_WINDOW(gnc_plugin_page_get_window (GNC_PLUGIN_PAGE(plugin_page)));
810  GtkTreeSelection *selection;
811  GList *selected_paths, *to_edit;
812  GtkTreeModel *model;
813 
814  selection = gtk_tree_view_get_selection (priv->tree_view);
815  selected_paths = gtk_tree_selection_get_selected_rows (selection, &model);
816  if (!gnc_list_length_cmp (selected_paths, 0))
817  {
818  g_warning ("no selection edit.");
819  return;
820  }
821 
822  to_edit = gnc_g_list_map (selected_paths,
823  (GncGMapFunc)_argument_reorder_fn,
824  priv->tree_view);
825 
826  gppsl_update_selected_list (plugin_page, true, nullptr);
827  for (GList *list = to_edit; list != nullptr; list = list->next)
828  {
829  DEBUG ("to-edit [%s]\n", xaccSchedXactionGetName (GNC_SCHEDXACTION(list->data)));
830  gppsl_update_selected_list (plugin_page, false, GNC_SCHEDXACTION(list->data));
831  }
832 
833  g_list_foreach (to_edit, (GFunc)_edit_sx, window);
834  g_list_free (to_edit);
835  g_list_foreach (selected_paths, (GFunc)gtk_tree_path_free, nullptr);
836  g_list_free (selected_paths);
837 }
838 
839 
840 static void
841 gnc_plugin_page_sx_list_cmd_edit_tax_options (GSimpleAction *simple,
842  GVariant *parameter,
843  gpointer user_data)
844 {
845  auto plugin_page = GNC_PLUGIN_PAGE_SX_LIST(user_data);
846  GtkWidget *window = GTK_WIDGET(gnc_plugin_page_get_window (GNC_PLUGIN_PAGE(plugin_page)));
847 
848  ENTER ("(action %p, page %p)", simple, plugin_page);
849  gnc_tax_info_dialog (window, nullptr);
850  LEAVE (" ");
851 }
852 
853 
854 static void
855 gppsl_row_activated_cb (GtkTreeView *tree_view,
856  GtkTreePath *path,
857  GtkTreeViewColumn *column,
858  gpointer user_data)
859 {
860  GncPluginPageSxList *page = GNC_PLUGIN_PAGE_SX_LIST(user_data);
861  GncPluginPageSxListPrivate *priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(page);
862  GtkWindow *window = GTK_WINDOW(gnc_plugin_page_get_window (GNC_PLUGIN_PAGE(page)));
863 
864  SchedXaction *sx = gnc_tree_view_sx_list_get_sx_from_path (
865  GNC_TREE_VIEW_SX_LIST(priv->tree_view), path);
866  gnc_ui_scheduled_xaction_editor_dialog_create (window, sx, false);
867  gppsl_update_selected_list (page, true, sx);
868 }
869 
870 
871 static void
872 _destroy_sx(gpointer data, gpointer user_data)
873 {
874  SchedXactions *sxes;
875  auto sx = GNC_SCHEDXACTION(data);
876  QofBook *book;
877  book = gnc_get_current_book ();
878  sxes = gnc_book_get_schedxactions (book);
879 
880  DEBUG("deleting sx [%s]", xaccSchedXactionGetName (sx));
881 
882  gnc_sxes_del_sx (sxes, sx);
883  gnc_sx_begin_edit (sx);
885 }
886 
887 
888 static void
889 _destroy_sx_names (gpointer data, gpointer user_data)
890 {
891  auto sx = GNC_SCHEDXACTION(data);
892  GList **to_delete_names = (GList**)user_data;
893  *to_delete_names = g_list_append (*to_delete_names, xaccSchedXactionGetName (sx));
894 }
895 
896 
897 static void
898 gnc_plugin_page_sx_list_cmd_delete (GSimpleAction *simple,
899  GVariant *parameter,
900  gpointer user_data)
901 {
902  auto plugin_page = GNC_PLUGIN_PAGE_SX_LIST(user_data);
903  GncPluginPageSxListPrivate *priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(plugin_page);
904  GtkTreeSelection *selection = gtk_tree_view_get_selection (priv->tree_view);
905  GList *selected_paths, *to_delete = nullptr, *to_delete_names = nullptr;
906  GtkTreeModel *model;
907  GtkWindow *window;
908  gchar *message = nullptr;
909  gint length;
910  gchar *text_list_of_scheduled_transaction_names = nullptr;
911 
912  selected_paths = gtk_tree_selection_get_selected_rows (selection, &model);
913  if (!gnc_list_length_cmp (selected_paths, 0))
914  {
915  g_warning ("no selection for delete.");
916  return;
917  }
918 
919  to_delete = gnc_g_list_map (selected_paths,
920  (GncGMapFunc)_argument_reorder_fn,
921  priv->tree_view);
922 
923  g_list_foreach (to_delete, (GFunc)_destroy_sx_names, &to_delete_names);
924 
925  window = GTK_WINDOW(gnc_plugin_page_get_window (GNC_PLUGIN_PAGE(plugin_page)));
926 
927  length = g_list_length (to_delete);
928 
929  text_list_of_scheduled_transaction_names = gnc_g_list_stringjoin (to_delete_names, "\n");
930 
931  /* Translators: This is a ngettext(3) message, question followed by list of scheduled transactions to be deleted */
932  message = g_strdup_printf ("%s\n\n%s",
933  ngettext ("Do you really want to delete this scheduled transaction?",
934  "Do you really want to delete these scheduled transactions?",
935  length),
936  text_list_of_scheduled_transaction_names);
937 
938  g_free (text_list_of_scheduled_transaction_names);
939  g_list_free (to_delete_names);
940 
941  if (gnc_verify_dialog (window, false, "%s", message))
942  {
943  gppsl_update_selected_list (plugin_page, true, nullptr);
944 
945  g_list_foreach (to_delete, (GFunc)_destroy_sx, nullptr);
946  }
947 
948  g_free (message);
949  g_list_free (to_delete);
950  g_list_foreach (selected_paths, (GFunc)gtk_tree_path_free, nullptr);
951  g_list_free (selected_paths);
952 }
953 
GtkWidget * gnc_plugin_page_get_window(GncPluginPage *page)
Retrieve a pointer to the GncMainWindow (GtkWindow) containing this page.
void gnc_sx_set_schedule(SchedXaction *sx, GList *schedule)
GList * gnc_g_list_map(GList *list, GncGMapFunc fn, gpointer user_data)
const gchar * tab_icon
The relative name of the icon that should be shown on the tab for this page.
gboolean(* focus_page_function)(GncPluginPage *plugin_page)
This function performs specific actions to set the focus on a specific widget.
GList * gnc_sx_get_schedule(const SchedXaction *sx)
void gnc_main_window_update_menu_and_toolbar(GncMainWindow *window, GncPluginPage *page, const gchar **ui_updates)
Update the main window menu with the placeholders listed in ui_updates and load the page specific too...
The instance data structure for a content plugin.
gchar * gnc_prefs_get_string(const gchar *group, const gchar *pref_name)
Get a string value from the preferences backend.
const GList * gnc_gobject_tracking_get_list(const gchar *name)
Get a list of all known objects of a specified type.
Date and Time handling routines.
gchar * gnc_g_list_stringjoin(GList *list_of_strings, const gchar *sep)
Return a string joining a GList whose elements are gchar* strings.
utility functions for the GnuCash UI
GncTreeView implementation for Scheduled Transaction List.
GncPluginPage *(* recreate_page)(GtkWidget *window, GKeyFile *file, const gchar *group)
Create a new page based on the information saved during a previous instantiation of gnucash...
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
Functions that are supported by all types of windows.
void gnc_gdate_set_today(GDate *gd)
Set a GDate to the current day.
Definition: gnc-date.cpp:1236
GtkWidget * window
The window that contains the display widget for this plugin.
API for Transactions and Splits (journal entries)
GSimpleActionGroup * gnc_plugin_page_create_action_group(GncPluginPage *page, const gchar *group_name)
Create the GSimpleActionGroup object associated with this page.
gboolean gnc_prefs_set_string(const gchar *group, const gchar *pref_name, const gchar *value)
Store a string into the preferences backend.
Definition: gnc-prefs.c:319
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
void gnc_main_window_open_page(GncMainWindow *window, GncPluginPage *page)
Display a data plugin page in a window.
Functions for adding content to a window.
void(* destroy_widget)(GncPluginPage *plugin_page)
Function called to destroy the display widget for a particular type of plugin.
The class data structure for a content plugin.
Gobject helper routines.
GAction * gnc_main_window_find_action(GncMainWindow *window, const gchar *action_name)
Find the GAction in the main window.
void gnc_plugin_page_disconnect_page_changed(GncPluginPage *page)
Disconnect the page_changed_id signal callback.
Anchor Scheduled Transaction info in a book.
const gchar * plugin_name
The textual name of this plugin.
gboolean gnc_prefs_set_float(const gchar *group, const gchar *pref_name, gdouble value)
Store a float value into the preferences backend.
Definition: gnc-prefs.c:308
GtkWidget *(* create_widget)(GncPluginPage *plugin_page)
Function called to create the display widget for a particular type of plugin.
const gchar * gnc_plugin_page_get_simple_action_group_name(GncPluginPage *page)
Retrieve the simple action group name associated with this plugin page.
Additional event handling code.
All type declarations for the whole Gnucash engine.
void(* save_page)(GncPluginPage *page, GKeyFile *file, const gchar *group)
Save enough information about this page so that it can be recreated next time the user starts gnucash...
GLib helper routines.
Generic api to store and retrieve preferences.
GncSxInstanceModel * gnc_sx_get_instances(const GDate *range_end, gboolean include_disabled)
Allocates a new SxInstanceModel and fills it with generated instances for all scheduled transactions ...
void gnc_plugin_page_inserted_cb(GncPluginPage *page, gpointer user_data)
Set up the page_changed callback for when the current page is changed.
void gnc_main_window_close_page(GncPluginPage *page)
Remove a data plugin page from a window and display the previous page.
Functions for adding plugins to a GnuCash window.
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
#define GNC_TYPE_PLUGIN_PAGE_SX_LIST
Functions providing a list of scheduled transactions as a plugin page.
GncPluginPage * gnc_plugin_page_sx_list_new(void)
void gnc_plugin_page_add_book(GncPluginPage *page, QofBook *book)
Add a book reference to the specified page.
SchedXaction * xaccSchedXactionMalloc(QofBook *book)
Creates and initializes a scheduled transaction.
gint gnc_list_length_cmp(const GList *list, size_t len)
Scans the GList elements the minimum number of iterations required to test it against a specified siz...
API for Transactions and Splits (journal entries)
GAction * gnc_plugin_page_get_action(GncPluginPage *page, const gchar *name)
Retrieve a GAction object associated with this page.
void xaccSchedXactionDestroy(SchedXaction *sx)
Cleans up and frees a SchedXaction and its associated data.
Commodity handling public routines.
gdouble gnc_prefs_get_float(const gchar *group, const gchar *pref_name)
Get an float value from the preferences backend.