wavs saved as wavs, pumped up version
[maemo-recorder] / src / settings.c
1 /* vim: set sts=4 sw=4 et: */
2 /*
3  * settings.c
4  * GConf settings
5  * 
6  * Copyright (C) 2006 Nokia Corporation
7  *
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16  * for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include <gtk/gtk.h>
25 #include <glib/gi18n-lib.h>
26 #include <locale.h>
27 #include <hildon-widgets/hildon-caption.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <osso-log.h>
31 #include <hildon-fm/hildon-widgets/hildon-file-chooser-dialog.h>
32
33 #include "maemo-recorder.h"
34 #include "settings.h"
35
36 #define GCONF_PATH          "/apps/maemo-recorder/settings"
37 #define KEY_DEFAULT_DIR     GCONF_PATH "/default_dir"
38 #define KEY_DEFAULT_FILTER  GCONF_PATH "/default_filter"
39
40 typedef struct _SettingsPriv SettingsPriv;
41
42 struct _SettingsPriv
43 {
44     GConfClient *gconf;
45     gchar *default_dir;
46     gint default_filter;
47 };
48
49 static SettingsPriv *settings_priv;
50
51 typedef struct _SettingsData SettingsData;
52
53 struct _SettingsData
54 {
55     AppData *appdata;
56     GtkWidget *dir_entry;
57     GtkWidget *window;
58     GtkWidget *format_combobox;
59 };
60
61 static SettingsData *settings_data;
62
63 static gboolean load_settings(void);
64
65 static void
66 reset_defaults()
67 {
68     gchar *str;
69
70     str = g_strdup_printf("/home/user/MyDocs/.sounds");
71     gconf_client_set_string(settings_priv->gconf, KEY_DEFAULT_DIR, str, NULL);
72     g_free(settings_priv->default_dir);
73     settings_priv->default_dir = str;
74
75     gconf_client_set_int(settings_priv->gconf,
76                          KEY_DEFAULT_FILTER, FORMAT_ILBC, NULL);
77     settings_priv->default_filter = FORMAT_ILBC;
78 }
79
80 gint
81 get_default_filter()
82 {
83     load_settings();
84     return settings_priv->default_filter;
85 }
86
87 void
88 set_default_filter(gint filter)
89 {
90     gconf_client_set_int(settings_priv->gconf,
91                          KEY_DEFAULT_FILTER, filter, NULL);
92     settings_priv->default_filter = filter;
93
94 }
95
96 gchar *
97 get_default_dir()
98 {
99     load_settings();
100     return settings_priv->default_dir;
101 }
102
103 void
104 set_default_dir(gchar * selected)
105 {
106     gconf_client_set_string(settings_priv->gconf,
107                             KEY_DEFAULT_DIR, selected, NULL);
108 }
109
110 static void
111 cb_format_changed(gpointer data)
112 {
113     gint active;
114
115     active =
116         gtk_combo_box_get_active(GTK_COMBO_BOX
117                                  (settings_data->format_combobox));
118     switch (active)
119     {
120         case 0:
121             settings_priv->default_filter = FORMAT_WAV;
122             break;
123         case 1:
124             settings_priv->default_filter = FORMAT_PCMA;
125             break;
126         case 2:
127             settings_priv->default_filter = FORMAT_ILBC;
128             break;
129         default:
130             break;
131     }
132 }
133
134 static void
135 cb_chdir(gpointer data)
136 {
137     GtkWidget *dialog = NULL;
138
139     g_assert(settings_data);
140
141     /* create dialog */
142     dialog =
143         GTK_WIDGET(hildon_file_chooser_dialog_new
144                    (GTK_WINDOW(settings_data->window),
145                     GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER));
146
147     /* show it */
148     gtk_widget_show_all(dialog);
149
150     if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
151     {
152         g_free(settings_priv->default_dir);
153         settings_priv->default_dir =
154             g_strdup(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)));
155         gtk_label_set_text(GTK_LABEL(settings_data->dir_entry),
156                            settings_priv->default_dir);
157     }
158
159     gtk_widget_destroy(dialog);
160
161 }
162
163 static gboolean
164 load_settings()
165 {
166     g_free(settings_priv->default_dir);
167     settings_priv->default_dir = gconf_client_get_string(settings_priv->gconf,
168                                                          KEY_DEFAULT_DIR,
169                                                          NULL);
170
171     settings_priv->default_filter = gconf_client_get_int(settings_priv->gconf,
172                                                          KEY_DEFAULT_FILTER,
173                                                          NULL);
174
175     if (settings_priv->default_dir && settings_priv->default_filter)
176         return TRUE;
177     else
178         return FALSE;
179
180 }
181
182 GConfClient *
183 settings_init(gint argc, gchar * argv[])
184 {
185     GError *err = NULL;
186
187     settings_priv = g_new0(SettingsPriv, 1);
188     settings_data = g_new0(SettingsData, 1);
189
190     gconf_init(argc, argv, &err);
191
192     if (err)
193     {
194         ULOG_ERR("Unable to initialize gconf");
195         reset_defaults();
196         return NULL;
197     }
198
199     settings_priv->gconf = gconf_client_get_default();
200
201     if (!settings_priv->gconf)
202     {
203         ULOG_ERR("Unable to initialize gconf");
204         reset_defaults();
205         return NULL;
206     }
207
208     gconf_client_add_dir(settings_priv->gconf, GCONF_PATH,
209                          GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
210
211     if (!load_settings())
212     {
213         ULOG_ERR("Unable to load all settings");
214         reset_defaults();
215         return settings_priv->gconf;
216     }
217
218     return settings_priv->gconf;
219 }
220
221 void
222 settings_edit(GtkWidget * widget, AppData * data)
223 {
224     GtkWidget *vbox;
225     GtkWidget *btn_chdir;
226     GtkWidget *alignment;
227     GtkWidget *caption;
228     GtkSizeGroup *group;
229     gint res;
230
231     load_settings();
232
233     settings_data->appdata = data;
234
235     settings_data->window = gtk_dialog_new_with_buttons(_("Settings"),
236                                                         GTK_WINDOW(data->
237                                                                    mainView),
238                                                         GTK_DIALOG_MODAL |
239                                                         GTK_DIALOG_DESTROY_WITH_PARENT
240                                                         |
241                                                         GTK_DIALOG_NO_SEPARATOR,
242                                                         _("OK"),
243                                                         GTK_RESPONSE_OK,
244                                                         _("Cancel"),
245                                                         GTK_RESPONSE_CANCEL,
246                                                         NULL, NULL);
247
248     g_signal_connect(G_OBJECT(settings_data->window),
249                      "delete_event", G_CALLBACK(gtk_widget_destroy), NULL);
250
251     group = GTK_SIZE_GROUP(gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL));
252     vbox = gtk_vbox_new(FALSE, HILDON_MARGIN_DEFAULT);
253     gtk_container_set_border_width(GTK_CONTAINER(vbox),
254                                    HILDON_MARGIN_DEFAULT);
255     gtk_container_add(GTK_CONTAINER(GTK_DIALOG(settings_data->window)->vbox),
256                       vbox);
257
258     /* with the format combo on top and directory thing at the bottom it looks cleaner */
259
260     /* format combo */
261     settings_data->format_combobox = gtk_combo_box_new_text();
262
263     gtk_combo_box_append_text(GTK_COMBO_BOX(settings_data->format_combobox),
264                               FORMAT_NAME_WAV);
265     gtk_combo_box_append_text(GTK_COMBO_BOX(settings_data->format_combobox),
266                               FORMAT_NAME_PCMA);
267     gtk_combo_box_append_text(GTK_COMBO_BOX(settings_data->format_combobox),
268                               FORMAT_NAME_ILBC);
269
270     switch (settings_priv->default_filter)
271     {
272         case FORMAT_WAV:
273             gtk_combo_box_set_active(GTK_COMBO_BOX
274                                      (settings_data->format_combobox), 0);
275             break;
276
277         case FORMAT_PCMA:
278             gtk_combo_box_set_active(GTK_COMBO_BOX
279                                      (settings_data->format_combobox), 1);
280             break;
281
282         case FORMAT_ILBC:
283         default:
284             gtk_combo_box_set_active(GTK_COMBO_BOX
285                                      (settings_data->format_combobox), 2);
286             break;
287     }
288
289     g_signal_connect(G_OBJECT(settings_data->format_combobox), "changed",
290                      G_CALLBACK(cb_format_changed), settings_data);
291
292     caption = hildon_caption_new(group, _("Default recording format:"),
293                                  settings_data->format_combobox, NULL,
294                                  HILDON_CAPTION_OPTIONAL);
295
296     gtk_box_pack_start(GTK_BOX(vbox), caption, FALSE, FALSE, 0);
297
298     /* save directory */
299     /* GtkEntry looks worse because the text get ellipsized */
300     settings_data->dir_entry = gtk_label_new(get_default_dir());
301
302     /*
303     gtk_editable_set_editable(GTK_EDITABLE(settings_data->dir_entry), FALSE);
304
305     gtk_entry_set_text(GTK_ENTRY(settings_data->dir_entry), get_default_dir());
306     */
307
308     /* In maemo world it seems to "folder", not "directory" =( */
309     caption = hildon_caption_new(group, _("Default save folder:"),
310                                  settings_data->dir_entry, NULL,
311                                  HILDON_CAPTION_OPTIONAL);
312
313     gtk_box_pack_start(GTK_BOX(vbox), caption, FALSE, FALSE, 0);
314
315     /* browse button */
316     btn_chdir = gtk_button_new_with_label(_("Browse"));
317     alignment = gtk_alignment_new(1.0, 0.5, 0.0, 0.0);
318
319     gtk_container_add(GTK_CONTAINER(alignment), btn_chdir);
320     gtk_box_pack_start(GTK_BOX(vbox), alignment, FALSE, FALSE, 0);
321
322     g_signal_connect(G_OBJECT(btn_chdir), "clicked",
323                      G_CALLBACK(cb_chdir), settings_data);
324
325     /*
326     gtk_widget_set_size_request(btn_chdir, 40, 40);
327     */
328
329     gtk_widget_show_all(settings_data->window);
330     res = gtk_dialog_run(GTK_DIALOG(settings_data->window));
331
332     if (res == GTK_RESPONSE_OK)
333     {
334         set_default_dir(settings_priv->default_dir);
335         set_default_filter(settings_priv->default_filter);
336         ULOG_DEBUG("ok");
337     }
338     else
339     {
340         ULOG_DEBUG("cancel");
341     }
342
343     gtk_widget_destroy(settings_data->window);
344 }