Added settings dialog, fixed a simple leak in gst_pitch_setup_algorithm()
[tunertool] / src / settings.c
1 /* vim: set sts=2 sw=2 et: */
2 /* 
3  * Copyright (C) 2008 Jari Tenhunen <jari.tenhunen@iki.fi>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gtk/gtk.h>
26 #if HILDON == 1
27 #include <hildon/hildon-caption.h>
28 #include <hildon/hildon-defines.h>
29 #include <hildon/hildon-number-editor.h>
30 #endif
31
32 #include "settings.h"
33
34 static GConfClient * client = NULL;
35
36 static GConfClient *
37 get_client ()
38 {
39   if (!client)
40     client = gconf_client_get_default ();
41
42   return client;
43
44 }
45
46 gboolean
47 settings_get_display_keepalive (gboolean default_value)
48 {
49   GConfValue * value = NULL;
50   GError * err = NULL;
51   gboolean val = default_value;
52
53   value = gconf_client_get (get_client (), GCONF_KEY_DISPLAY_KEEPALIVE, &err);
54   if (err) {
55     g_error_free (err);
56   }
57
58   if (value) {
59     if (value->type == GCONF_VALUE_BOOL)
60       val = gconf_value_get_bool (value);
61
62     gconf_value_free (value);
63   }
64
65   return val;
66 }
67
68 gboolean
69 settings_set_display_keepalive (gboolean val)
70 {
71   return gconf_client_set_bool (get_client (), GCONF_KEY_DISPLAY_KEEPALIVE, val, NULL);
72 }
73
74 gint
75 settings_get_algorithm (gint default_value)
76 {
77   GConfValue * value = NULL;
78   GError * err = NULL;
79   gint val = default_value;
80
81   value = gconf_client_get (get_client (), GCONF_KEY_ALGORITHM, &err);
82   if (err) {
83     g_error_free (err);
84   }
85
86   if (value) {
87     if (value->type == GCONF_VALUE_INT)
88       val = gconf_value_get_int (value);
89
90     gconf_value_free (value);
91   }
92
93   return val;
94 }
95
96 gboolean
97 settings_set_algorithm (gint val)
98 {
99   return gconf_client_set_int (get_client (), GCONF_KEY_ALGORITHM, val, NULL);
100 }
101
102 gint
103 settings_get_calibration (gint default_value)
104 {
105   GError * err = NULL;
106   gint val;
107
108   val = gconf_client_get_int (get_client (), GCONF_KEY_CALIBRATION, &err);
109   if (err) {
110     val = default_value;
111     g_error_free (err);
112   }
113   if (val == 0)
114     val = default_value;
115
116   return val;
117 }
118
119 gboolean
120 settings_set_calibration (gint value)
121 {
122   return gconf_client_set_int (get_client (), GCONF_KEY_CALIBRATION, value, NULL);
123 }
124
125 gboolean
126 settings_init (GConfClientNotifyFunc func, gpointer user_data)
127 {
128   gconf_client_add_dir (get_client (), GCONF_ROOT, GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
129   gconf_client_notify_add (get_client (), GCONF_ROOT, func, user_data, NULL, NULL);
130
131   return TRUE;
132 }
133
134 #if HILDON == 1
135 static void
136 fix_hildon_number_editor (GtkWidget * widget, gpointer data)
137 {
138   if (GTK_IS_EDITABLE (widget)) {
139     gtk_editable_set_editable (GTK_EDITABLE (widget), FALSE);
140     g_object_set (G_OBJECT (widget), "can-focus", FALSE, NULL);
141   }
142 }
143
144 GtkWidget *
145 calibration_editor_new (gint min, gint max)
146 {
147   GtkWidget *control;
148  
149   control = hildon_number_editor_new (min, max);
150   /* we don't want that ugly cursor there */
151   gtk_container_forall (GTK_CONTAINER (control),
152       (GtkCallback) fix_hildon_number_editor, NULL);
153
154   return control;
155 }
156
157 void
158 settings_dialog_show (GtkWindow * parent)
159 {
160   GtkWidget *dialog;
161   GtkWidget *vbox;
162   GtkWidget *caption;
163   GtkSizeGroup *group;
164   GtkWidget *editor = NULL;
165   GtkWidget *control;
166   GtkComboBox *combo;
167   gint res;
168
169   dialog = gtk_dialog_new_with_buttons("Settings",
170       parent,
171       GTK_DIALOG_MODAL | 
172       GTK_DIALOG_DESTROY_WITH_PARENT | 
173       GTK_DIALOG_NO_SEPARATOR,
174       "OK", GTK_RESPONSE_OK,
175       "Cancel",
176       GTK_RESPONSE_CANCEL,
177       NULL, NULL);
178
179   g_signal_connect (G_OBJECT (dialog), "delete_event", G_CALLBACK (gtk_widget_destroy), NULL);
180
181   vbox = gtk_vbox_new (FALSE, HILDON_MARGIN_DEFAULT);
182   gtk_container_set_border_width (GTK_CONTAINER (vbox), HILDON_MARGIN_DEFAULT);
183   gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), vbox);
184
185   group = GTK_SIZE_GROUP (gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL));
186
187   combo = GTK_COMBO_BOX (gtk_combo_box_new_text ());
188   gtk_combo_box_append_text (combo, "Simple FFT");
189   gtk_combo_box_append_text (combo, "Harmonic Product Spectrum");
190   gtk_combo_box_set_active (combo, settings_get_algorithm (DEFAULT_ALGORITHM));
191   caption = hildon_caption_new (group, "Pitch detection algorithm:",
192       GTK_WIDGET (combo), NULL, HILDON_CAPTION_OPTIONAL);
193   gtk_box_pack_start (GTK_BOX (vbox), caption, FALSE, FALSE, 0);
194 #if 0
195   editor = calibration_editor_new (CALIB_MIN, CALIB_MAX);
196   hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (editor), CALIB_DEFAULT);
197   caption = hildon_caption_new (group, "Calibration:",
198       editor, NULL, HILDON_CAPTION_OPTIONAL);
199   gtk_box_pack_start (GTK_BOX (vbox), caption, FALSE, FALSE, 0);
200 #endif
201
202   control = gtk_check_button_new ();
203   caption = hildon_caption_new (group, "Keep display on:",
204       control, NULL, HILDON_CAPTION_OPTIONAL);
205   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (control), 
206       settings_get_display_keepalive (DEFAULT_DISPLAY_KEEPALIVE));
207   gtk_box_pack_start (GTK_BOX (vbox), caption, FALSE, FALSE, 0);
208
209   gtk_widget_show_all (dialog);
210   res = gtk_dialog_run (GTK_DIALOG (dialog));
211
212   if (res == GTK_RESPONSE_OK) {
213     /* save settings */
214     g_debug ("algorithm: %d", gtk_combo_box_get_active (combo));
215     settings_set_algorithm (gtk_combo_box_get_active (combo));
216     if (editor) {
217       g_debug ("calib: %d", hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (editor)));
218       settings_set_calibration (hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (editor)));
219     }
220     g_debug ("keepalive: %d", gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (control)));
221     settings_set_display_keepalive (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (control)));
222   }
223
224   gtk_widget_destroy (dialog);
225 }
226 #endif
227