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