GnuCash  5.6-150-g038405b370+
gnc-plugin-manager.c
1 /*
2  * gnc-plugin-manager.c --
3  *
4  * Copyright (C) 2003 Jan Arne Petersen
5  * Author: Jan Arne Petersen <jpetersen@uni-bonn.de>
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 
25 #include <config.h>
26 
27 #include <gtk/gtk.h>
28 
29 #include "gnc-plugin-manager.h"
30 #include "gnc-engine.h"
31 #include "gnc-hooks.h"
32 
33 static QofLogModule log_module = GNC_MOD_GUI;
34 
35 static void gnc_plugin_manager_dispose (GObject *object);
36 static void gnc_plugin_manager_finalize (GObject *object);
37 static void gnc_plugin_manager_shutdown (gpointer dummy, gpointer dummy2);
38 
39 /* typedefs & structures */
41 {
42  GObject gobject;
43 
44  GList *plugins;
45  GHashTable *plugins_table;
46 } GncPluginManagerPrivate;
47 
48 G_DEFINE_TYPE(GncPluginManager, gnc_plugin_manager, G_TYPE_OBJECT)
49 
50 enum
51 {
52  PLUGIN_ADDED,
53  PLUGIN_REMOVED,
54  LAST_SIGNAL
55 };
56 
57 static guint signals[LAST_SIGNAL] = { 0 };
58 static GncPluginManager *singleton = NULL;
59 
60 GncPluginManager *
62 {
63  if (singleton == NULL)
64  {
65  singleton = g_object_new (GNC_TYPE_PLUGIN_MANAGER,
66  NULL);
67  gnc_hook_add_dangler (HOOK_SHUTDOWN,
68  gnc_plugin_manager_shutdown, NULL, NULL);
69  }
70 
71  return singleton;
72 }
73 
74 void
75 gnc_plugin_manager_add_plugin (GncPluginManager *manager,
76  GncPlugin *plugin)
77 {
78  gint index;
79 
80  ENTER (" ");
81  g_return_if_fail (GNC_IS_PLUGIN_MANAGER (manager));
82  g_return_if_fail (GNC_IS_PLUGIN (plugin));
83 
84  index = g_list_index (manager->plugins, plugin);
85 
86  if (index >= 0)
87  return;
88 
89  manager->plugins = g_list_append (manager->plugins, plugin);
90  g_hash_table_insert (manager->plugins_table,
91  g_strdup( GNC_PLUGIN_GET_CLASS(plugin)->plugin_name ),
92  plugin);
93 
94  g_signal_emit (G_OBJECT (manager), signals[PLUGIN_ADDED], 0, plugin);
95  LEAVE ("added %s to GncPluginManager", gnc_plugin_get_name(plugin));
96 }
97 
98 void
99 gnc_plugin_manager_remove_plugin (GncPluginManager *manager,
100  GncPlugin *plugin)
101 {
102  gint index;
103 
104  ENTER (" ");
105  g_return_if_fail (GNC_IS_PLUGIN_MANAGER (manager));
106  g_return_if_fail (GNC_IS_PLUGIN (plugin));
107 
108  index = g_list_index (manager->plugins, plugin);
109 
110  if (index < 0)
111  return;
112 
113  manager->plugins = g_list_remove (manager->plugins, plugin);
114  g_hash_table_remove (manager->plugins_table,
115  GNC_PLUGIN_GET_CLASS(plugin)->plugin_name);
116 
117  g_signal_emit (G_OBJECT (manager), signals[PLUGIN_REMOVED], 0, plugin);
118 
119  LEAVE ("removed %s from GncPluginManager",
120  gnc_plugin_get_name(plugin));
121  g_object_unref (plugin);
122 }
123 
124 GList *
125 gnc_plugin_manager_get_plugins (GncPluginManager *manager)
126 {
127  g_return_val_if_fail (GNC_IS_PLUGIN_MANAGER (manager), NULL);
128 
129  return g_list_copy (manager->plugins);
130 }
131 
132 GncPlugin *
133 gnc_plugin_manager_get_plugin (GncPluginManager *manager,
134  const gchar *name)
135 {
136  g_return_val_if_fail (GNC_IS_PLUGIN_MANAGER (manager), NULL);
137  g_return_val_if_fail (name != NULL, NULL);
138 
139  return GNC_PLUGIN (g_hash_table_lookup (manager->plugins_table, name));
140 }
141 
142 static void
143 gnc_plugin_manager_class_init (GncPluginManagerClass *klass)
144 {
145  GObjectClass *object_class = G_OBJECT_CLASS (klass);
146 
147  object_class->dispose = gnc_plugin_manager_dispose;
148  object_class->finalize = gnc_plugin_manager_finalize;
149 
150  signals[PLUGIN_ADDED] = g_signal_new ("plugin-added",
151  G_OBJECT_CLASS_TYPE (klass),
152  G_SIGNAL_RUN_FIRST,
153  0,
154  NULL, NULL,
155  g_cclosure_marshal_VOID__OBJECT,
156  G_TYPE_NONE,
157  1,
158  GNC_TYPE_PLUGIN);
159  signals[PLUGIN_REMOVED] = g_signal_new ("plugin-removed",
160  G_OBJECT_CLASS_TYPE (klass),
161  G_SIGNAL_RUN_FIRST,
162  0,
163  NULL, NULL,
164  g_cclosure_marshal_VOID__OBJECT,
165  G_TYPE_NONE,
166  1,
167  GNC_TYPE_PLUGIN);
168 }
169 
170 static void
171 gnc_plugin_manager_init (GncPluginManager *manager)
172 {
173  manager->plugins_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
174 }
175 
176 static void
177 gnc_plugin_manager_dispose (GObject *object)
178 {
179  GncPluginManager *manager = GNC_PLUGIN_MANAGER (object);
180 
181  g_return_if_fail (GNC_IS_PLUGIN_MANAGER (manager));
182 
183  if (manager->plugins_table)
184  {
185  g_hash_table_destroy (manager->plugins_table);
186  manager->plugins_table = NULL;
187 
188  g_list_foreach (manager->plugins, (GFunc)g_object_unref, NULL);
189  g_list_free (manager->plugins);
190  manager->plugins = NULL;
191  }
192 
193  G_OBJECT_CLASS (gnc_plugin_manager_parent_class)->dispose (object);
194 }
195 
196 static void
197 gnc_plugin_manager_finalize (GObject *object)
198 {
199  g_return_if_fail (GNC_IS_PLUGIN_MANAGER (object));
200 
201  G_OBJECT_CLASS (gnc_plugin_manager_parent_class)->finalize (object);
202 }
203 
204 static void
205 gnc_plugin_manager_shutdown (gpointer dummy, gpointer dummy2)
206 {
207  if (singleton != NULL)
208  {
209  g_object_unref(singleton);
210  singleton = NULL;
211  }
212 }
Plugin management functions for the GnuCash UI.
void gnc_plugin_manager_add_plugin(GncPluginManager *manager, GncPlugin *plugin)
Add a plugin to the list maintained by the plugin manager.
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
void gnc_plugin_manager_remove_plugin(GncPluginManager *manager, GncPlugin *plugin)
Remove a plugin from the list maintained by the plugin manager.
GncPluginManager * gnc_plugin_manager_get(void)
Retrieve a pointer to the plugin manager.
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.
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
GList * gnc_plugin_manager_get_plugins(GncPluginManager *manager)
Get a list of all plugins being held by the plugin manager.
GncPlugin * gnc_plugin_manager_get_plugin(GncPluginManager *manager, const gchar *name)
Find a plugin by name from the list of plugins being held by the plugin manager.