1 /*
2  * gnc-plugin-register2.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 #include <glib/gi18n.h>
29 #include <string.h>
30 
31 #include "gnc-component-manager.h"
32 #include "gnc-plugin-register2.h"
33 #include "gnc-plugin-page-register2.h"
34 #include "gnc-prefs.h"
35 
36 static void gnc_plugin_register2_class_init (GncPluginRegister2Class *klass);
37 static void gnc_plugin_register2_init (GncPluginRegister2 *plugin);
38 static void gnc_plugin_register2_finalize (GObject *object);
39 
40 static void gnc_plugin_register2_add_to_window (GncPlugin *plugin, GncMainWindow *window, GQuark type);
41 static void gnc_plugin_register2_remove_from_window (GncPlugin *plugin, GncMainWindow *window, GQuark type);
42 
43 /* Command callbacks */
44 static void gnc_plugin_register2_cmd_general_ledger (GtkAction *action, GncMainWindowActionData *data);
45 
46 #define PLUGIN_ACTIONS_NAME "gnc-plugin-register2-actions"
47 
48 #ifdef REGISTER2_ENABLED
49 #define PLUGIN_UI_FILENAME  "gnc-plugin-register2-ui.xml"
50 #else
51 #define PLUGIN_UI_FILENAME  "gnc-plugin-register22-ui.xml"
52 #endif
53 static GtkActionEntry gnc_plugin_actions [] =
54 {
55 #ifdef REGISTER2_ENABLED
56     {
57         "ToolsGeneralJournal2Action", NULL, N_("_General Journal"), NULL,
58         N_("Open a general journal window"),
59         G_CALLBACK (gnc_plugin_register2_cmd_general_ledger)
60     },
61 #endif
62 
63     /* Extensions Menu */
64     { "Register2TestAction", NULL, N_("_Register2"), NULL, NULL, NULL },
65     {
66         "Register2TestGLAction", NULL, N_("Register2 Open GL Account"), NULL,
67         N_("Register2 Open GL Account"),
68         G_CALLBACK (gnc_plugin_register2_cmd_general_ledger)
69     },
70 };
71 static guint gnc_plugin_n_actions = G_N_ELEMENTS (gnc_plugin_actions);
72 
73 typedef struct GncPluginRegister2Private
74 {
75     gpointer dummy;
76 } GncPluginRegister2Private;
77 
78 G_DEFINE_TYPE_WITH_PRIVATE(GncPluginRegister2, gnc_plugin_register2, GNC_TYPE_PLUGIN)
79 
80 #define GNC_PLUGIN_REGISTER2_GET_PRIVATE(o)  \
81    ((GncPluginRegister2Private*)g_type_instance_get_private((GTypeInstance*)o, GNC_TYPE_PLUGIN_REGISTER2))
82 
83 static GObjectClass *parent_class = NULL;
84 static QofLogModule log_module = GNC_MOD_GUI;
85 
86 /************************************************************
87  *                     Other Functions                      *
88  ************************************************************/
89 
90 /** This function is called whenever an entry in the general register
91  *  preferences group is changed.  It does nothing more than kick off a
92  *  gui refresh which should be delivered to any open register page.
93  *  The register pages will then reread their preferences and
94  *  update the screen.
95  *
96  *  @prefs Unused.
97  *
98  *  @pref Unused.
99  *
100  *  @user_data Unused.
101  */
102 static void
gnc_plugin_register2_pref_changed(gpointer prefs,gchar * pref,gpointer user_data)103 gnc_plugin_register2_pref_changed (gpointer prefs, gchar *pref,
104                                    gpointer user_data)
105 {
106     ENTER("");
107     gnc_gui_refresh_all ();
108     LEAVE("");
109 }
110 
111 /************************************************************
112  *                  Object Implementation                   *
113  ************************************************************/
114 
115 GncPlugin *
gnc_plugin_register2_new(void)116 gnc_plugin_register2_new (void)
117 {
118     GncPluginRegister2 *plugin;
119 
120     /* Reference the register page plugin to ensure it exists in
121      * the gtk type system. */
122     GNC_TYPE_PLUGIN_PAGE_REGISTER2;
123 
124     plugin = g_object_new (GNC_TYPE_PLUGIN_REGISTER2,
125                            NULL);
126 
127     return GNC_PLUGIN (plugin);
128 }
129 
130 static void
gnc_plugin_register2_class_init(GncPluginRegister2Class * klass)131 gnc_plugin_register2_class_init (GncPluginRegister2Class *klass)
132 {
133     GObjectClass *object_class = G_OBJECT_CLASS (klass);
134     GncPluginClass *plugin_class = GNC_PLUGIN_CLASS (klass);
135 
136     parent_class = g_type_class_peek_parent (klass);
137 
138     object_class->finalize = gnc_plugin_register2_finalize;
139 
140     /* plugin info */
141     plugin_class->plugin_name  = GNC_PLUGIN_REGISTER2_NAME;
142 
143     /* function overrides */
144     plugin_class->add_to_window = gnc_plugin_register2_add_to_window;
145     plugin_class->remove_from_window =
146         gnc_plugin_register2_remove_from_window;
147 
148     /* widget addition/removal */
149     plugin_class->actions_name = PLUGIN_ACTIONS_NAME;
150     plugin_class->actions      = gnc_plugin_actions;
151     plugin_class->n_actions    = gnc_plugin_n_actions;
152     plugin_class->ui_filename  = PLUGIN_UI_FILENAME;
153 }
154 
155 static void
gnc_plugin_register2_init(GncPluginRegister2 * plugin)156 gnc_plugin_register2_init (GncPluginRegister2 *plugin)
157 {
158 }
159 
160 static void
gnc_plugin_register2_finalize(GObject * object)161 gnc_plugin_register2_finalize (GObject *object)
162 {
163     GncPluginRegister2 *plugin;
164 
165     plugin = GNC_PLUGIN_REGISTER2 (object);
166     g_return_if_fail (GNC_IS_PLUGIN_REGISTER2 (plugin));
167 
168     G_OBJECT_CLASS (parent_class)->finalize (object);
169 }
170 
171 /************************************************************
172  *              Plugin Function Implementation              *
173  ************************************************************/
174 
175 /** Initialize the registeru for a window.  This function is
176  *  called as part of the initialization of a window, after all the
177  *  plugin menu items have been added to the menu structure.  Its job
178  *  is to correctly initialize the register.  It does this by
179  *  installing a function that listens for preference changes. Each
180  *  time a preference changes, it kicks off a gui refresh.
181  *
182  *  @param plugin A pointer to the gnc-plugin object responsible for
183  *  adding/removing the register.
184  *
185  *  @param window A pointer to the gnc-main-window that is being initialized.
186  *
187  *  @param type Unused
188  */
189 static void
gnc_plugin_register2_add_to_window(GncPlugin * plugin,GncMainWindow * window,GQuark type)190 gnc_plugin_register2_add_to_window (GncPlugin *plugin,
191                                    GncMainWindow *window,
192                                    GQuark type)
193 {
194     gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL_REGISTER, NULL,
195                            gnc_plugin_register2_pref_changed, window);
196 }
197 
198 
199 /** Finalize the register for this window.  This function is
200  *  called as part of the destruction of a window.
201  *
202  *  @param plugin A pointer to the gnc-plugin object responsible for
203  *  adding/removing the register.  It stops listening for
204  *  changes in the register preferences.
205  *
206  *  @param window A pointer the gnc-main-window that is being destroyed.
207  *
208  *  @param type Unused
209  */
210 static void
gnc_plugin_register2_remove_from_window(GncPlugin * plugin,GncMainWindow * window,GQuark type)211 gnc_plugin_register2_remove_from_window (GncPlugin *plugin,
212                                         GncMainWindow *window,
213                                         GQuark type)
214 {
215     gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL_REGISTER, NULL,
216                                  gnc_plugin_register2_pref_changed, window);
217 }
218 
219 /************************************************************
220  *                    Command Callbacks                     *
221  ************************************************************/
222 
223 static void
gnc_plugin_register2_cmd_general_ledger(GtkAction * action,GncMainWindowActionData * data)224 gnc_plugin_register2_cmd_general_ledger (GtkAction *action,
225                                         GncMainWindowActionData *data)
226 {
227     GncPluginPage *page;
228 
229     g_return_if_fail (data != NULL);
230 
231     page = gnc_plugin_page_register2_new_gl ();
232     gnc_main_window_open_page (data->window, page);
233 }
234