GnuCash  5.6-150-g038405b370+
gnc-plugin.c
Go to the documentation of this file.
1 /*
2  * gnc-plugin.c --
3  *
4  * Copyright (C) 2003 Jan Arne Petersen <jpetersen@uni-bonn.de>
5  * Copyright (C) 2003,2005 David Hampton <hampton@employees.org>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, contact:
19  *
20  * Free Software Foundation Voice: +1-617-542-5942
21  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
22  * Boston, MA 02110-1301, USA gnu@gnu.org
23  */
24 
35 #include <config.h>
36 
37 #include <gtk/gtk.h>
38 #include <glib/gi18n.h>
39 
40 #include "gnc-plugin.h"
41 #include "gnc-engine.h"
42 #include "gnc-filepath-utils.h"
43 #include "gnc-gnome-utils.h"
44 #include "gnc-gobject-utils.h"
45 #include "gnc-gtk-utils.h"
46 
48 static QofLogModule log_module = GNC_MOD_GUI;
49 
50 static void gnc_plugin_constructed (GObject *object);
51 static void gnc_plugin_finalize (GObject *object);
52 
53 
56 typedef struct GncPluginPrivate
57 {
58  gpointer dummy;
60 
61 G_DEFINE_TYPE_WITH_CODE(GncPlugin, gnc_plugin, G_TYPE_OBJECT,
62  G_ADD_PRIVATE(GncPlugin))
63 
64 #define GNC_PLUGIN_GET_PRIVATE(o) \
65  ((GncPluginPrivate*)gnc_plugin_get_instance_private((GncPlugin*)o))
66 
74 static void
75 gnc_plugin_class_init (GncPluginClass *klass)
76 {
77  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
78 
79  gobject_class->constructed = gnc_plugin_constructed;
80  gobject_class->finalize = gnc_plugin_finalize;
81 }
82 
83 
90 static void
91 gnc_plugin_init (GncPlugin *plugin)
92 {
93 }
94 
101 static void
102 gnc_plugin_constructed (GObject *obj)
103 {
105 
106  G_OBJECT_CLASS (gnc_plugin_parent_class)->constructed (obj);
107 }
108 
109 
117 static void
118 gnc_plugin_finalize (GObject *object)
119 {
120  g_return_if_fail (GNC_IS_PLUGIN (object));
121 
123  G_OBJECT_CLASS (gnc_plugin_parent_class)->finalize (object);
124 }
125 
126 
132 void
133 gnc_plugin_add_to_window (GncPlugin *plugin,
134  GncMainWindow *window,
135  GQuark type)
136 {
137  GncPluginClass *klass;
138 
139  g_return_if_fail (GNC_IS_PLUGIN (plugin));
140  klass = GNC_PLUGIN_GET_CLASS (plugin);
141  ENTER (": plugin %s(%p), window %p", gnc_plugin_get_name(plugin),
142  plugin, window);
143 
144  /*
145  * Update window with additional UI items
146  */
147  if (klass->actions_name)
148  {
149  DEBUG ("%s: %d actions to merge with gui from %s",
150  klass->actions_name, klass->n_actions, klass->ui_filename);
151  gnc_main_window_merge_actions (window, klass->actions_name,
152  klass->actions, klass->n_actions,
153  klass->ui_updates,
154  klass->ui_filename, plugin);
155  }
156 
157  /*
158  * Do plugin specific actions.
159  */
160  if (GNC_PLUGIN_GET_CLASS (plugin)->add_to_window)
161  {
162  DEBUG ("Calling child class function %p", GNC_PLUGIN_GET_CLASS (plugin)->add_to_window);
163  GNC_PLUGIN_GET_CLASS (plugin)->add_to_window (plugin, window, type);
164  }
165  LEAVE ("");
166 }
167 
168 
169 /* Remove the specified plugin from the specified window. This
170  * function will call the plugin to perform any plugin specific
171  * actions and remove the page's user interface from the window.
172  *
173  * See gnc-plugin.h for documentation on the function arguments. */
174 void
175 gnc_plugin_remove_from_window (GncPlugin *plugin,
176  GncMainWindow *window,
177  GQuark type)
178 {
179  GncPluginClass *klass;
180 
181  g_return_if_fail (GNC_IS_PLUGIN (plugin));
182  klass = GNC_PLUGIN_GET_CLASS (plugin);
183  ENTER (": plugin %s(%p), window %p", gnc_plugin_get_name(plugin),
184  plugin, window);
185 
186  /*
187  * Do plugin specific actions.
188  */
189  if (GNC_PLUGIN_GET_CLASS (plugin)->remove_from_window)
190  {
191  DEBUG ("Calling child class function %p",
192  GNC_PLUGIN_GET_CLASS (plugin)->remove_from_window);
193  GNC_PLUGIN_GET_CLASS (plugin)->remove_from_window (plugin, window, type);
194  }
195 
196  /*
197  * Update window to remove UI items
198  */
199  if (klass->actions_name && !window->just_plugin_prefs)
200  {
201  DEBUG ("%s: %d actions to unmerge",
202  klass->actions_name, (klass->n_actions));
203  gnc_main_window_unmerge_actions (window, klass->actions_name);
204  }
205  LEAVE ("");
206 }
207 
208 
211 const gchar *
212 gnc_plugin_get_name (GncPlugin *plugin)
213 {
214  g_return_val_if_fail (GNC_IS_PLUGIN (plugin), NULL);
215  return (GNC_PLUGIN_GET_CLASS(plugin)->plugin_name);
216 }
217 
218 
219 /************************************************************
220  * Utility Functions *
221  ************************************************************/
222 
223 
228 void
229 gnc_plugin_init_short_names (GtkWidget *toolbar,
230  GncToolBarShortNames *toolbar_labels)
231 {
232  g_return_if_fail (toolbar != NULL);
233  g_return_if_fail (toolbar_labels != NULL);
234 
235  for (gint i = 0; (toolbar_labels[i].action_name); i++)
236  {
237  GtkWidget *tool_item = gnc_find_toolbar_item (toolbar, toolbar_labels[i].action_name);
238 
239  if (!tool_item)
240  continue;
241 
242  gtk_tool_button_set_label (GTK_TOOL_BUTTON(tool_item), _(toolbar_labels[i].short_label));
243  gtk_tool_button_set_use_underline (GTK_TOOL_BUTTON(tool_item), TRUE);
244  }
245 }
246 
247 
248 /* Update the sensitivity of an action */
249 void
250 gnc_plugin_set_actions_enabled (GActionMap *action_map,
251  const gchar **action_names, gboolean enable)
252 {
253  g_return_if_fail (action_map != NULL);
254 
255  for (gint i = 0; action_names[i]; i++)
256  {
257  GAction *action = g_action_map_lookup_action (G_ACTION_MAP(action_map),
258  action_names[i]);
259  if (action)
260  g_simple_action_set_enabled (G_SIMPLE_ACTION(action), enable);
261  else
262  PERR("No such action with name '%s' in action group %p)",
263  action_names[i], action_map);
264  }
265 }
266 
267 void
269  GMenuModel *menubar_model,
270  GtkWidget *statusbar)
271 {
272  GList *menu_item_list;
273 
274  g_return_if_fail (G_IS_MENU_MODEL(menubar_model));
275  g_return_if_fail (GTK_IS_STATUSBAR(statusbar));
276 
277  menu_item_list = gnc_menu_get_items (menubar);
278 
279  for (GList *node = menu_item_list; node; node = node->next)
280  {
281  GtkWidget *menu_item = node->data;
282 
284  }
285  g_object_set_data (G_OBJECT(statusbar), "menu-model", menubar_model);
286  g_list_free (menu_item_list);
287 }
288 
289 static void
290 for_each_tool_action (GtkWidget *widget, gpointer user_data)
291 {
292  GtkWidget *statusbar = user_data;
293 
294  if (GTK_IS_ACTIONABLE(widget))
296 }
297 
298 void
299 gnc_plugin_add_toolbar_tooltip_callbacks (GtkWidget *toolbar, GtkWidget *statusbar)
300 {
301  g_return_if_fail (GTK_IS_TOOLBAR(toolbar));
302  g_return_if_fail (GTK_IS_STATUSBAR(statusbar));
303 
304  gtk_container_foreach (GTK_CONTAINER(toolbar), for_each_tool_action, statusbar);
305 }
306 
void gnc_tool_item_setup_tooltip_to_statusbar_callback(GtkWidget *tool_item, GtkWidget *statusbar)
Setup the callbacks for tool bar items so the tooltip can be displayed in the status bar...
gtk helper routines.
void gnc_gobject_tracking_forget(GObject *object)
Tell gnucash to remember this object in the database.
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
void gnc_plugin_add_toolbar_tooltip_callbacks(GtkWidget *toolbar, GtkWidget *statusbar)
This function adds the tooltip callbacks to make the tooltips appear in the status bar...
Definition: gnc-plugin.c:299
void gnc_plugin_add_to_window(GncPlugin *plugin, GncMainWindow *window, GQuark type)
Add the specified plugin from the specified window.
Definition: gnc-plugin.c:133
void gnc_main_window_merge_actions(GncMainWindow *window, const gchar *group_name, GActionEntry *actions, guint n_actions, const gchar **ui_updates, const gchar *ui_filename, gpointer user_data)
Add a set of actions to the specified window.
void gnc_main_window_unmerge_actions(GncMainWindow *window, const gchar *group_name)
Remove a set of actions from the specified window.
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
void gnc_plugin_add_menu_tooltip_callbacks(GtkWidget *menubar, GMenuModel *menubar_model, GtkWidget *statusbar)
This function adds the tooltip callbacks to make the tooltips appear in the status bar...
Definition: gnc-plugin.c:268
void gnc_gobject_tracking_remember(GObject *object)
Tell gnucash to remember this object in the database.
GList * gnc_menu_get_items(GtkWidget *menu)
Return a list of GtkMenuItems.
Gobject helper routines.
G_DEFINE_TYPE_WITH_CODE(GncPlugin, gnc_plugin, G_TYPE_OBJECT, G_ADD_PRIVATE(GncPlugin))
Initialize the class for the new gnucash plugin object.
Definition: gnc-plugin.c:61
void gnc_plugin_set_actions_enabled(GActionMap *action_map, const gchar **action_names, gboolean enable)
This function sets the sensitivity of a GAction in a specific group.
Definition: gnc-plugin.c:250
Gnome specific utility functions.
void gnc_plugin_remove_from_window(GncPlugin *plugin, GncMainWindow *window, GQuark type)
Remove the specified plugin from the specified window.
Definition: gnc-plugin.c:175
GtkWidget * gnc_find_toolbar_item(GtkWidget *toolbar, const gchar *action_name)
Search the toolbar for the tool item based on the action name.
const gchar * gnc_plugin_get_name(GncPlugin *plugin)
Retrieve the textual name of a plugin.
Definition: gnc-plugin.c:212
All type declarations for the whole Gnucash engine.
void gnc_plugin_init_short_names(GtkWidget *toolbar, GncToolBarShortNames *toolbar_labels)
Add "short" labels to existing actions.
Definition: gnc-plugin.c:229
A structure for defining alternate action names for use in the toolbar.
Functions for adding plugins to a GnuCash window.
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
The instance data structure for a main window object.
File path resolution utility functions.
const char * action_name
The name of the action.
void gnc_menu_item_setup_tooltip_to_statusbar_callback(GtkWidget *menu_item, GtkWidget *statusbar)
Setup the callbacks for menu bar items so the tooltip can be displayed in the status bar...
The instance private data for a menu-only plugin.
Definition: gnc-plugin.c:56