Integrate adding files with the file manager
[anjuta-git-plugin.git] / plugins / valgrind / preferences.c
blob2e5d36ed990873ac2f55ba9a266baacfd7ea02da
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * preferences.c
5 * Copyright (C) Massimo Cora' 2006 <maxcvs@gmail.com>
6 *
7 * preferences.c is free software.
8 *
9 * You may redistribute it and/or modify it under the terms of the
10 * GNU General Public License, as published by the Free Software
11 * Foundation; either version 2, or (at your option) any later version.
13 * plugin.h is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with plugin.h. See the file "COPYING". If not,
20 * write to: The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
25 #include "vggeneralprefs.h"
26 #include "vgmemcheckprefs.h"
27 #include "vgcachegrindprefs.h"
28 #include "vghelgrindprefs.h"
29 #include "preferences.h"
31 #include <string.h>
32 #include <gconf/gconf-client.h>
33 #include <libgnomevfs/gnome-vfs.h>
34 #include <libanjuta/anjuta-debug.h>
36 #define EDITOR_KEY "/apps/anjuta/valgrind/editor"
37 #define NUM_LINES_KEY "/apps/anjuta/valgrind/num-lines"
38 #define EXE_PATH "/apps/anjuta/valgrind/exe-path"
40 #define VALGRIND_DEFAULT_BIN "/usr/bin/valgrind"
42 enum {
43 PAGE_GENERAL = 0,
44 PAGE_MEMCHECK = 1,
45 PAGE_ADDRCHECK = PAGE_MEMCHECK,
46 PAGE_CACHEGRIND = 2,
47 PAGE_HELGRIND = 3,
50 struct _ValgrindPluginPrefsPriv {
51 GtkWidget * pages[4]; /* must be used only to retrieve gconf parameters.
52 * Do NOT return these as widgets that can be destroyed */
56 static void valgrind_plugin_prefs_class_init(ValgrindPluginPrefsClass *klass);
57 static void valgrind_plugin_prefs_init(ValgrindPluginPrefs *sp);
58 static void valgrind_plugin_prefs_finalize(GObject *object);
60 static GObjectClass *parent_class = NULL;
62 static gboolean
63 spin_changed (GtkSpinButton *spin, GdkEvent *event, const char *key)
65 GConfClient *gconf;
66 gint num;
68 gconf = gconf_client_get_default ();
70 num = gtk_spin_button_get_value_as_int (spin);
71 gconf_client_set_int (gconf, key, num, NULL);
73 g_object_unref (gconf);
75 return FALSE;
79 /*-----------------------------------------------------------------------------
80 * Callback for valgrind exe file path selection
83 static void
84 on_exe_path_entry_changed (GtkFileChooser *chooser, const char *key)
86 GConfClient *gconf;
87 gchar *str;
89 gconf = gconf_client_get_default ();
91 str = gtk_file_chooser_get_filename (chooser);
93 DEBUG_PRINT ("str is %s key is %s", str, key);
95 gconf_client_set_string (gconf, key, str ? str : "", NULL);
96 g_free (str);
98 g_object_unref (gconf);
102 /*-----------------------------------------------------------------------------
103 * build and returns a widget containing the general prefs of alleyoop/valgrind
106 static GtkWidget *
107 build_general_prefs ()
109 GConfClient *gconf;
110 GtkWidget *vbox, *hbox, *label, *main_label, *gen_page, *widget;
111 GtkSpinButton *numlines;
112 GError *err = NULL;
113 gint num;
114 gchar *str_file;
116 gconf = gconf_client_get_default ();
118 vbox = gtk_vbox_new (FALSE, 6);
120 hbox = gtk_hbox_new (FALSE, 6);
122 main_label = gtk_label_new ("");
123 gtk_label_set_markup (GTK_LABEL (main_label), _("<b>Valgrind general preferences</b>"));
125 gtk_box_pack_start (GTK_BOX (hbox), main_label, FALSE, FALSE, 0);
126 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
128 hbox = gtk_hbox_new (FALSE, 6);
129 label = gtk_label_new (_("Valgrind binary file path:"));
130 gtk_widget_show (label);
131 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
133 str_file = gconf_client_get_string (gconf, EXE_PATH, &err);
135 if (str_file == NULL || err != NULL || strlen (str_file) <= 0) {
136 str_file = g_strdup (VALGRIND_DEFAULT_BIN);
139 if (!g_path_is_absolute(str_file))
140 DEBUG_PRINT("Not absolute");
142 widget =
143 gtk_file_chooser_button_new (_("Choose Valgrind Binary File Path..."),
144 GTK_FILE_CHOOSER_ACTION_OPEN);
146 if ( gtk_file_chooser_select_filename (GTK_FILE_CHOOSER (widget), str_file) == FALSE )
147 DEBUG_PRINT ("error: could not select file uri with gtk_file_chooser_select_filename ()");
149 g_free (str_file);
151 /* grab every change in file selection */
152 g_signal_connect (widget, "selection-changed", G_CALLBACK (on_exe_path_entry_changed), EXE_PATH);
154 gtk_widget_show (widget);
155 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
157 gtk_widget_show (hbox);
158 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
160 hbox = gtk_hbox_new (FALSE, 6);
162 label = gtk_label_new (_("Preview"));
163 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
165 num = gconf_client_get_int (gconf, NUM_LINES_KEY, NULL);
166 numlines = GTK_SPIN_BUTTON (gtk_spin_button_new_with_range (0, (gdouble) INT_MAX, 1));
167 gtk_spin_button_set_digits (numlines, 0);
168 gtk_spin_button_set_numeric (numlines, TRUE);
169 gtk_spin_button_set_value (numlines, (gdouble) num);
171 g_signal_connect (numlines, "focus-out-event", G_CALLBACK (spin_changed), NUM_LINES_KEY);
172 gtk_widget_show (GTK_WIDGET (numlines));
173 gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (numlines), FALSE, FALSE, 0);
175 label = gtk_label_new (_("lines above and below the target line."));
176 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
177 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
179 /* create a fresh new general prefs widget and add it to the vbox */
180 gen_page = g_object_new (VG_TYPE_GENERAL_PREFS, NULL);
181 gtk_box_pack_start (GTK_BOX (vbox), gen_page, FALSE, FALSE, 0);
183 gtk_widget_show_all (vbox);
184 return vbox;
187 GPtrArray *
188 valgrind_plugin_prefs_create_argv (ValgrindPluginPrefs *valprefs, const char *tool)
190 GPtrArray *argv;
191 int page;
192 ValgrindPluginPrefsPriv *priv;
193 GConfClient *gconf;
194 gchar *str_file;
196 g_return_val_if_fail (valprefs != NULL, NULL);
198 priv = valprefs->priv;
200 argv = g_ptr_array_new ();
202 gconf = gconf_client_get_default ();
203 str_file = gconf_client_get_string (gconf, EXE_PATH, NULL);
205 g_ptr_array_add (argv, str_file);
207 if (tool != NULL) {
208 if (!strcmp (tool, "memcheck")) {
209 g_ptr_array_add (argv, "--tool=memcheck");
210 page = PAGE_MEMCHECK;
211 } else if (!strcmp (tool, "addrcheck")) {
212 g_ptr_array_add (argv, "--tool=addrcheck");
213 page = PAGE_ADDRCHECK;
214 } else if (!strcmp (tool, "cachegrind")) {
215 g_ptr_array_add (argv, "--tool=cachegrind");
216 page = PAGE_CACHEGRIND;
217 } else if (!strcmp (tool, "helgrind")) {
218 g_ptr_array_add (argv, "--tool=helgrind");
219 page = PAGE_HELGRIND;
220 } else {
221 g_assert_not_reached ();
223 } else {
224 /* default tool */
225 g_ptr_array_add (argv, "--tool=memcheck");
226 page = PAGE_MEMCHECK;
229 /* next, apply the general prefs */
230 vg_tool_prefs_get_argv (VG_TOOL_PREFS (priv->pages[PAGE_GENERAL]), tool, argv);
232 /* finally, apply the current view's prefs */
233 vg_tool_prefs_get_argv (VG_TOOL_PREFS (priv->pages[page]), tool, argv);
235 return argv;
238 GtkWidget *
239 valgrind_plugin_prefs_get_anj_prefs (void)
241 return build_general_prefs ();
244 GtkWidget *
245 valgrind_plugin_prefs_get_general_widget (void)
247 return g_object_new (VG_TYPE_GENERAL_PREFS, NULL);
250 GtkWidget *
251 valgrind_plugin_prefs_get_memcheck_widget (void)
253 return g_object_new (VG_TYPE_MEMCHECK_PREFS, NULL);
256 GtkWidget *
257 valgrind_plugin_prefs_get_cachegrind_widget (void)
259 return g_object_new (VG_TYPE_CACHEGRIND_PREFS, NULL);
262 GtkWidget *
263 valgrind_plugin_prefs_get_helgrind_widget (void)
265 return g_object_new (VG_TYPE_HELGRIND_PREFS, NULL);
269 GType
270 valgrind_plugin_prefs_get_type(void)
272 static GType type = 0;
274 if(type == 0) {
275 static const GTypeInfo our_info = {
276 sizeof (ValgrindPluginPrefsClass),
277 NULL,
278 NULL,
279 (GClassInitFunc)valgrind_plugin_prefs_class_init,
280 NULL,
281 NULL,
282 sizeof (ValgrindPluginPrefs),
284 (GInstanceInitFunc)valgrind_plugin_prefs_init,
287 type = g_type_register_static(G_TYPE_OBJECT,
288 "ValgrindPluginPrefs", &our_info, 0);
291 return type;
294 static void
295 valgrind_plugin_prefs_class_init(ValgrindPluginPrefsClass *klass)
297 GObjectClass *object_class = G_OBJECT_CLASS(klass);
299 parent_class = g_type_class_peek_parent(klass);
300 object_class->finalize = valgrind_plugin_prefs_finalize;
303 static void
304 valgrind_plugin_prefs_init(ValgrindPluginPrefs *obj)
306 ValgrindPluginPrefsPriv *priv;
307 obj->priv = g_new0(ValgrindPluginPrefsPriv, 1);
309 priv = obj->priv;
311 /* build our own widgets. These will be used only by VgActions to retrieve
312 * the configs */
313 priv->pages[PAGE_GENERAL] = g_object_new (VG_TYPE_GENERAL_PREFS, NULL);
314 priv->pages[PAGE_MEMCHECK] = g_object_new (VG_TYPE_MEMCHECK_PREFS, NULL);
315 priv->pages[PAGE_CACHEGRIND] = g_object_new (VG_TYPE_CACHEGRIND_PREFS, NULL);
316 priv->pages[PAGE_HELGRIND] = g_object_new (VG_TYPE_HELGRIND_PREFS, NULL);
319 static void
320 valgrind_plugin_prefs_finalize(GObject *object)
322 ValgrindPluginPrefs *cobj;
323 cobj = VALGRIND_PLUGINPREFS(object);
325 /* FIXME: Free private members, etc. */
326 g_free(cobj->priv);
327 G_OBJECT_CLASS(parent_class)->finalize(object);
330 ValgrindPluginPrefs *
331 valgrind_plugin_prefs_new(void)
333 ValgrindPluginPrefs *obj;
335 obj = VALGRIND_PLUGINPREFS(g_object_new(VALGRIND_TYPE_PLUGINPREFS, NULL));
337 return obj;