applet: print strerror and exit if execlp() fails
[espeaktime] / src / applet.c
index d98db82..9659081 100644 (file)
@@ -1,9 +1,13 @@
 #include <unistd.h>
+#include <errno.h>
 #include <gtk/gtk.h>
 #include <hildon/hildon.h>
 #include <hildon-cp-plugin/hildon-cp-plugin-interface.h>
+#include <gconf/gconf.h>
+#include <gconf/gconf-client.h>
 
 #define ESPEAK_BIN "espeak"
+#define GCONF_KEY "/apps/Maemo/espeaktime"
 
 /* TODO: read these from disk */
 
@@ -71,16 +75,59 @@ static const struct effect {
 static const int num_effects = sizeof(effects) / sizeof(effects[0]);
 
 struct espeaktime_settings {
-       const gchar *voice;
-       const gchar *effect;
-       const gchar *text;
+       gchar *voice;
+       gchar *effect;
+       gchar *text;
        gint amplitude;
        gint pitch;
        gint speed;
        gboolean ignore_silent;
 };
 
-static void do_test(struct espeaktime_settings *cfg)
+static void cfg_read(GConfClient *client, struct espeaktime_settings *cfg)
+{
+       cfg->voice = gconf_client_get_string(client, GCONF_KEY "/voice", NULL);
+       cfg->effect = gconf_client_get_string(client, GCONF_KEY "/effect", NULL);
+       cfg->text = gconf_client_get_string(client, GCONF_KEY "/text", NULL);
+       cfg->amplitude = gconf_client_get_int(client, GCONF_KEY "/amplitude", NULL);
+       cfg->pitch = gconf_client_get_int(client, GCONF_KEY "/pitch", NULL);
+       cfg->speed = gconf_client_get_int(client, GCONF_KEY "/speed", NULL);
+       cfg->ignore_silent = gconf_client_get_bool(client, GCONF_KEY "/ignore_silent", NULL);
+       if (!cfg->voice)
+               cfg->voice = g_strdup("en-us");
+       if (!cfg->effect)
+               cfg->effect = g_strdup("");
+       if (!cfg->text)
+               cfg->text = g_strdup("%H:%M");
+       if (!cfg->amplitude)
+               cfg->amplitude = 100;
+       if (!cfg->pitch)
+               cfg->pitch = 50;
+       if (!cfg->speed)
+               cfg->speed = 170;
+       /* TODO: default ignore_silent to TRUE */
+}
+
+static void cfg_write(GConfClient *client, struct espeaktime_settings *cfg)
+{
+       gconf_client_add_dir(client, GCONF_KEY, GCONF_CLIENT_PRELOAD_NONE, NULL);
+       gconf_client_set_string(client, GCONF_KEY "/voice", cfg->voice, NULL);
+       gconf_client_set_string(client, GCONF_KEY "/effect", cfg->effect, NULL);
+       gconf_client_set_string(client, GCONF_KEY "/text", cfg->text, NULL);
+       gconf_client_set_int(client, GCONF_KEY "/amplitude", cfg->amplitude, NULL);
+       gconf_client_set_int(client, GCONF_KEY "/pitch", cfg->pitch, NULL);
+       gconf_client_set_int(client, GCONF_KEY "/speed", cfg->speed, NULL);
+       gconf_client_set_bool(client, GCONF_KEY "/ignore_silent", cfg->ignore_silent, NULL);
+}
+
+static void cfg_free(struct espeaktime_settings *cfg)
+{
+       g_free(cfg->voice);
+       g_free(cfg->effect);
+       g_free(cfg->text);
+}
+
+static void cfg_speak(struct espeaktime_settings *cfg, gboolean test_mode)
 {
        gchar astr[16], pstr[16], sstr[16];
        gchar vstr[64];
@@ -112,7 +159,8 @@ static void do_test(struct espeaktime_settings *cfg)
        res = execlp(ESPEAK_BIN, ESPEAK_BIN,
                "-a", astr, "-p", pstr, "-s", sstr, "-v", vstr,
                text, NULL);
-       g_print("execlp: %d\n", res);
+       g_print("execlp: %d (%s)\n", res, g_strerror(errno));
+       exit(res);
 }
 
 
@@ -136,6 +184,9 @@ osso_return_t execute(osso_context_t *osso, gpointer data, gboolean user_activat
                .ignore_silent = TRUE,
        };
 
+       GConfClient *client = gconf_client_get_default();
+       cfg_read(client, &cfg);
+
        GtkWidget *dialog;
        dialog = gtk_dialog_new_with_buttons(
                "eSpeakTime Settings",
@@ -211,26 +262,34 @@ osso_return_t execute(osso_context_t *osso, gpointer data, gboolean user_activat
        while (1) {
                int result = gtk_dialog_run(GTK_DIALOG(dialog));
 
+               g_free(cfg.voice);
+               g_free(cfg.effect);
+               g_free(cfg.text);
+               cfg.voice = g_strdup(voices[hildon_picker_button_get_active(voice)].id);
+               cfg.effect = g_strdup(effects[hildon_picker_button_get_active(effect)].id);
+               cfg.text = g_strdup(gtk_entry_get_text(GTK_ENTRY(text)));
+               cfg.amplitude = gtk_adjustment_get_value(amplitude_adj);
+               cfg.pitch = gtk_adjustment_get_value(pitch_adj);
+               cfg.speed = gtk_adjustment_get_value(speed_adj);
+               cfg.ignore_silent = hildon_check_button_get_active(ignore_silent);
+
                switch (result) {
                case 1:
                        g_print("Test button\n");
-                       cfg.voice = voices[hildon_picker_button_get_active(voice)].id;
-                       cfg.effect = effects[hildon_picker_button_get_active(effect)].id;
-                       cfg.text = gtk_entry_get_text(GTK_ENTRY(text));
-                       cfg.amplitude = gtk_adjustment_get_value(amplitude_adj);
-                       cfg.pitch = gtk_adjustment_get_value(pitch_adj);
-                       cfg.speed = gtk_adjustment_get_value(speed_adj);
-                       cfg.ignore_silent = hildon_check_button_get_active(ignore_silent);
-                       do_test(&cfg);
+                       cfg_speak(&cfg, TRUE);
                        continue;
                case GTK_RESPONSE_OK:
                        g_print("Save\n");
+                       cfg_write(client, &cfg);
                        break;
                }
                break;
        }
        gtk_widget_destroy(GTK_WIDGET(dialog));
 
+       cfg_free(&cfg);
+       g_object_unref(G_OBJECT(client));
+
        return OSSO_OK;
 }