OSDN Git Service

removed warning when no dict found; initial preferences fix
authoriev <iev@land.ru>
Tue, 24 Sep 2013 22:09:04 +0000 (02:09 +0400)
committeriev <iev@land.ru>
Tue, 24 Sep 2013 22:09:04 +0000 (02:09 +0400)
src/.prefs.c.swp [new file with mode: 0644]
src/audio.c~ [new file with mode: 0644]
src/dicts.c
src/ebook.c
src/prefs.c

diff --git a/src/.prefs.c.swp b/src/.prefs.c.swp
new file mode 100644 (file)
index 0000000..35928a8
Binary files /dev/null and b/src/.prefs.c.swp differ
diff --git a/src/audio.c~ b/src/audio.c~
new file mode 100644 (file)
index 0000000..73c3d53
--- /dev/null
@@ -0,0 +1,222 @@
+
+#include "defs.h"
+
+#include "eb123.h"
+#include "ebgstsrc.h"
+#include "history.h"
+#include "mainwnd.h"
+#include "render.h"
+#include "audio.h"
+
+G_DEFINE_TYPE(Audio, audio, GTK_TYPE_FRAME);
+
+EB_Error_Code audio_save_wave(RESULT *res, gchar *file)
+{
+    char binary_data[EB_SIZE_PAGE];
+    EB_Error_Code error_code;
+    EB_Position end_position;
+    ssize_t read_len;
+    size_t write_len;
+    FILE *fp;
+
+    end_position.page = res->pos.page + (res->size / EB_SIZE_PAGE);
+    end_position.offset = res->pos.offset + (res->size % EB_SIZE_PAGE);
+    if (EB_SIZE_PAGE <= end_position.offset)
+    {
+        end_position.offset -= EB_SIZE_PAGE;
+        end_position.page++;
+    }
+    error_code = eb_set_binary_wave(res->binfo->book, &res->pos, &end_position);
+    if (error_code != EB_SUCCESS)
+    {
+        LOG(LOG_CRITICAL, "Failed to set binary wave: %s", eb_error_message(error_code));
+        return error_code;
+    }
+
+    fp = fopen(file, "wb");
+    if(!fp)
+    {
+        LOG(LOG_CRITICAL, "Failed to open file : %s", file);
+        return EB_ERR_BAD_FILE_NAME;
+    }
+
+    for(;;)
+    {
+        error_code = eb_read_binary(res->binfo->book, EB_SIZE_PAGE, binary_data, &read_len);
+        if(error_code != EB_SUCCESS || read_len == 0)
+        {
+            fclose(fp);
+            return error_code;
+        }
+
+        // If there are extra data (32 bytes) before fmt chunk,remove them.
+        if((strncmp("fmt ", &binary_data[44], 4) == 0) && (strncmp("fmt ", &binary_data[12], 4) != 0))
+        {
+            LOG(LOG_CRITICAL, "Warning: extra header found in WAVE data.");
+            write_len = fwrite(binary_data, 12, 1, fp);
+            write_len = fwrite(&binary_data[44], read_len - 44, 1, fp);
+        }
+        else
+            write_len = fwrite(binary_data, read_len, 1, fp);
+    }
+    (void)write_len;
+    return EB_SUCCESS;
+}
+
+void audio_save_as(GtkWidget *w, gpointer data)
+{       
+    static gchar *path = NULL;
+    path = app_browse_disk(_("Save Audio As"), mainwnd_get_wnd(), GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_SAVE_AS, path);
+    if(path)
+    {
+        RESULT *res = (RESULT*)data;
+        if(audio_save_wave(res, path) != EB_SUCCESS)
+            LOG(LOG_WARNING, _("Failed to save audio"));
+    }
+
+}
+
+gboolean audio_context_menu(GtkWidget *w, GdkEventButton *event, gpointer user_data)
+{
+    if(event->button != 3) return FALSE;
+    GtkWidget *menu = gtk_menu_new(), *item;
+    item = gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE_AS, NULL);
+    g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(audio_save_as), user_data);
+    gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
+    gtk_widget_show_all(menu);
+    gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
+    return TRUE;
+}
+
+#ifdef ENABLE_GSTREAMER
+
+gboolean audio_bus_cb(GstBus *bus, GstMessage *msg, gpointer data)
+{
+    GstElement *pipeline = GST_ELEMENT(data);
+    switch (GST_MESSAGE_TYPE (msg))
+    {
+       case GST_MESSAGE_EOS:
+       {
+           gst_element_set_state(pipeline, GST_STATE_NULL);
+           gst_object_unref(GST_OBJECT (pipeline));
+           break;
+       }
+
+       case GST_MESSAGE_ERROR:
+       {
+           gchar  *debug;
+           GError *error;
+
+           gst_message_parse_error(msg, &error, &debug);
+           g_free(debug);
+
+           LOG(LOG_INFO, "%s\n", error->message);
+           g_error_free(error);
+
+           break;
+       }
+        default:
+           break;
+    }
+
+    return TRUE;
+}
+
+static void audio_pad_added_cb(GstElement *dec, GstPad *pad, gpointer data)
+{
+    GstElement *sink = (GstElement*)data;
+    GstPad *sinkpad = gst_element_get_pad(sink, "sink");
+
+    gst_pad_link(pad, sinkpad);
+    gst_object_unref(sinkpad);
+}
+
+void audio_play(GtkWidget *w, gpointer data)
+{
+    GstElement *src, *dec, *sink, *pipeline = gst_pipeline_new ("pipeline");
+    GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE (pipeline));
+    RESULT *res = (RESULT*)data;
+
+    gst_bus_add_watch(bus, audio_bus_cb, (gpointer)pipeline);
+    gst_object_unref(bus);
+
+    src = g_object_new(EB_GST_TYPE_SRC, NULL);
+    EB_GST_SRC(src)->res = res;
+    dec = gst_element_factory_make("wavparse", "decoder");
+    sink = gst_element_factory_make("alsasink", "sink");
+
+    gst_bin_add_many(GST_BIN(pipeline), src, dec, sink, NULL);
+    gst_element_link(src, dec);
+    g_signal_connect(dec, "pad-added", G_CALLBACK(audio_pad_added_cb), sink);
+
+    gst_element_set_state(pipeline, GST_STATE_PLAYING);
+}
+
+#endif
+
+static void audio_set_property(GObject *object, guint param_id, const GValue *value, GParamSpec *pspec)
+{
+    Audio *self = AUDIO(object);
+    RESULT *res;
+    switch(param_id)
+    {
+        case 1:
+            res = g_value_get_pointer(value);
+           g_signal_connect(G_OBJECT(self->save_btn), "clicked", G_CALLBACK(audio_save_as), res);
+#ifdef ENABLE_GSTREAMER
+           if(self->play_btn)
+               g_signal_connect(G_OBJECT(self->play_btn), "clicked", G_CALLBACK(audio_play), res);
+#endif
+            break;
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
+            break;
+    }
+}
+
+static void audio_class_init(AudioClass *klass)
+{
+    GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
+    gobject_class->set_property = audio_set_property;
+    g_object_class_install_property(gobject_class, 1, g_param_spec_pointer("link", _("Link"), _("Link"), G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
+}
+
+static void audio_init(Audio *self)
+{
+    self->play_btn = NULL;
+    GtkWidget *img;
+    GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
+    gtk_container_add(GTK_CONTAINER(self), hbox);
+#ifdef ENABLE_GSTREAMER
+    self->play_btn = gtk_button_new();
+    img = gtk_image_new_from_stock(GTK_STOCK_MEDIA_PLAY, GTK_ICON_SIZE_SMALL_TOOLBAR);
+    gtk_button_set_image(GTK_BUTTON(self->play_btn), img);
+    gtk_box_pack_start(GTK_BOX(hbox), self->play_btn, FALSE, TRUE, 0);
+#else
+    GtkWidget *label = gtk_label_new(_("Audio"));
+    gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
+#endif
+    self->save_btn = gtk_button_new();
+    img = gtk_image_new_from_stock(GTK_STOCK_SAVE_AS, GTK_ICON_SIZE_SMALL_TOOLBAR);
+    gtk_container_add(GTK_CONTAINER(self->save_btn), img);
+    gtk_box_pack_start(GTK_BOX(hbox), self->save_btn, FALSE, TRUE, 0);
+}
+
+void audio_render(Audio *self, RenderTextCtx *ctx)
+{
+    GtkTextIter iter1, iter2;
+    if(render_get_last_mark(ctx, "wave", &iter1, &iter2))
+    {
+        gtk_text_buffer_delete(ctx->buf, &iter1, &iter2);
+        gtk_text_buffer_get_end_iter(ctx->buf, &iter1);
+        gtk_text_buffer_insert(ctx->buf, &iter1, "\n", -1);
+        gtk_text_buffer_get_end_iter(ctx->buf, &iter1);
+        GtkTextChildAnchor *anchor = gtk_text_buffer_create_child_anchor(ctx->buf, &iter1);
+        gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(ctx->view), GTK_WIDGET(self), anchor);
+        gtk_text_buffer_insert(ctx->buf, &iter1, "\n", -1);
+        g_object_set_data_full(G_OBJECT(self), "audio", ctx->link, result_free);
+       gtk_widget_show_all(GTK_WIDGET(self));
+       gtk_widget_realize(GTK_WIDGET(self));
+    }
+}
+
index a894d37..a23f8af 100644 (file)
@@ -18,13 +18,13 @@ static void dicts_set_property(GObject *object, guint param_id, const GValue *va
     Dicts *dicts = DICTS(object);
     switch(param_id)
     {
-       case 1:
-           dicts->builder = BUILDER(g_value_get_pointer(value));
-           dicts->store = GTK_TREE_STORE(gtk_builder_get_object(GTK_BUILDER(dicts->builder), "dicts_store"));
-           break;
-       default:
-           G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
-           break;
+        case 1:
+            dicts->builder = BUILDER(g_value_get_pointer(value));
+            dicts->store = GTK_TREE_STORE(gtk_builder_get_object(GTK_BUILDER(dicts->builder), "dicts_store"));
+            break;
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
+            break;
     }
 }
 
@@ -69,8 +69,8 @@ static void dicts_item_move(Dicts *self, gboolean up)
     selection = gtk_tree_view_get_selection(tree);
     if(!gtk_tree_selection_get_selected(selection, NULL, &iter))
     {
-       LOG(LOG_WARNING, _("No dictionary selected"));
-       return;
+        LOG(LOG_WARNING, _("No dictionary selected"));
+        return;
     }
     path = gtk_tree_model_get_path(GTK_TREE_MODEL(self->store), &iter);
     path_orig = gtk_tree_path_copy(path);
@@ -78,46 +78,46 @@ static void dicts_item_move(Dicts *self, gboolean up)
     up ? gtk_tree_path_prev(path) : gtk_tree_path_next(path);
     if(gtk_tree_path_compare(path, path_orig) && gtk_tree_model_get_iter(GTK_TREE_MODEL(self->store), &iter2, path)) 
     {
-       gtk_tree_store_swap(self->store, &iter, &iter2);
-       gtk_tree_selection_select_iter(selection, &iter);
+        gtk_tree_store_swap(self->store, &iter, &iter2);
+        gtk_tree_selection_select_iter(selection, &iter);
     }
     else if(gtk_tree_path_get_depth(path) > 1)
     {
-       gtk_tree_path_up(path);
-       gtk_tree_path_free(path_orig);
-       path_orig = gtk_tree_path_copy(path);
-       up ? gtk_tree_path_prev(path) : gtk_tree_path_next(path);
-       if(gtk_tree_path_compare(path, path_orig))
-       {
-           GtkTreeIter parent;
-           if(gtk_tree_model_get_iter(GTK_TREE_MODEL(self->store), &parent, path))
-           {
-               if(up)
-                   gtk_tree_store_append(self->store, &iter2, &parent);
-               else
-                   gtk_tree_store_prepend(self->store, &iter2, &parent);
-
-               gchar *title;
-               gboolean editable;
-               BOOK_INFO *binfo;
-               gtk_tree_model_get(GTK_TREE_MODEL(self->store), 
-                       &iter, 
-                       DICT_ALIAS, &title,
-                       DICT_BINFO, &binfo,
-                       DICT_EDITABLE, &editable,
-                       -1);
-               gtk_tree_store_set(self->store, &iter2,
-                       DICT_ALIAS, title,
-                       DICT_BINFO, binfo,
-                       DICT_EDITABLE, editable,
-                       -1);
-               g_free(title);
-
-               gtk_tree_store_remove(self->store, &iter);
-               gtk_tree_view_expand_row(tree, path, TRUE);
-               gtk_tree_selection_select_iter(gtk_tree_view_get_selection(tree), &iter2);
-           }
-       }
+        gtk_tree_path_up(path);
+        gtk_tree_path_free(path_orig);
+        path_orig = gtk_tree_path_copy(path);
+        up ? gtk_tree_path_prev(path) : gtk_tree_path_next(path);
+        if(gtk_tree_path_compare(path, path_orig))
+        {
+            GtkTreeIter parent;
+            if(gtk_tree_model_get_iter(GTK_TREE_MODEL(self->store), &parent, path))
+            {
+                if(up)
+                    gtk_tree_store_append(self->store, &iter2, &parent);
+                else
+                    gtk_tree_store_prepend(self->store, &iter2, &parent);
+
+                gchar *title;
+                gboolean editable;
+                BOOK_INFO *binfo;
+                gtk_tree_model_get(GTK_TREE_MODEL(self->store), 
+                        &iter, 
+                        DICT_ALIAS, &title,
+                        DICT_BINFO, &binfo,
+                        DICT_EDITABLE, &editable,
+                        -1);
+                gtk_tree_store_set(self->store, &iter2,
+                        DICT_ALIAS, title,
+                        DICT_BINFO, binfo,
+                        DICT_EDITABLE, editable,
+                        -1);
+                g_free(title);
+
+                gtk_tree_store_remove(self->store, &iter);
+                gtk_tree_view_expand_row(tree, path, TRUE);
+                gtk_tree_selection_select_iter(gtk_tree_view_get_selection(tree), &iter2);
+            }
+        }
     }
     gtk_tree_path_free(path);
     gtk_tree_path_free(path_orig);
@@ -126,11 +126,12 @@ static void dicts_item_move(Dicts *self, gboolean up)
 void dicts_scroll_down(Dicts *self)
 {
     while(gtk_events_pending())
-       gtk_main_iteration();
+        gtk_main_iteration();
     GtkTreeView *tree = GTK_TREE_VIEW(gtk_builder_get_object(GTK_BUILDER(self->builder), "dicts_tree"));
     GtkTreeSelection *sel = gtk_tree_view_get_selection(tree);
     GtkTreeIter iter;
-    gtk_tree_selection_get_selected(sel, NULL, &iter);
+    if(!gtk_tree_selection_get_selected(sel, NULL, &iter))
+        return;
     GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(self->store), &iter);
     gtk_tree_view_scroll_to_cell(tree, path, NULL, True, .5, .5);
     gtk_tree_path_free(path);
@@ -166,11 +167,11 @@ void dicts_info_update_search_cb(GtkWidget *w, gpointer data)
     const gchar *name = NULL;
     for(i = 0; (name = ebook_index_to_method_name(i)); i++)
     {
-       if(!g_strcmp0(gtk_label_get_text(GTK_LABEL(w)), name))
-       {
-           (book ? ebook_has_method(book, i) : FALSE) ? gtk_widget_show(w) : gtk_widget_hide(w);
-           break;
-       }
+        if(!g_strcmp0(gtk_label_get_text(GTK_LABEL(w)), name))
+        {
+            (book ? ebook_has_method(book, i) : FALSE) ? gtk_widget_show(w) : gtk_widget_hide(w);
+            break;
+        }
     }
 }
 
@@ -178,7 +179,7 @@ static BOOK_INFO* dicts_load_binfo(const gchar *path, gint subbook_no)
 {
     BOOK_INFO *binfo = (BOOK_INFO*)g_new0(BOOK_INFO, 1);
     if(!binfo)
-       return NULL;
+        return NULL;
     binfo->path = g_strdup(path);
     binfo->subbook_no = subbook_no;
     ebook_load(binfo);
@@ -198,75 +199,75 @@ static void dicts_search_recursive(Dicts *self, gchar *path, gint depth)
 
     if(!(dir = g_dir_open(path, 0, NULL)))
     {
-       LOG(LOG_CRITICAL, "Failed to open directory %s", path);
-       return;
+        LOG(LOG_CRITICAL, "Failed to open directory %s", path);
+        return;
     }
     if(!depth)
     {
-       // remove trailing separators, if needed
-       while(strlen(path) > 1)
-       {
-           if(!g_strcmp0(&(path[strlen(path) - 1]), G_DIR_SEPARATOR_S))
-               path[strlen(path) - 1] = 0;
-           else
-               break;
-       }
-       dirname = g_strrstr(path, G_DIR_SEPARATOR_S);
-       dirname = &(dirname[1]);
-       if(strlen(dirname))
-           dirname = g_strdup(dirname);
-       else
-           dirname = g_strdup(_("Results"));
-       group_exists = FALSE;
+        // remove trailing separators, if needed
+        while(strlen(path) > 1)
+        {
+            if(!g_strcmp0(&(path[strlen(path) - 1]), G_DIR_SEPARATOR_S))
+                path[strlen(path) - 1] = 0;
+            else
+                break;
+        }
+        dirname = g_strrstr(path, G_DIR_SEPARATOR_S);
+        dirname = &(dirname[1]);
+        if(strlen(dirname))
+            dirname = g_strdup(dirname);
+        else
+            dirname = g_strdup(_("Results"));
+        group_exists = FALSE;
     }
     GtkTreeView *dicts_tree = GTK_TREE_VIEW(gtk_builder_get_object(GTK_BUILDER(self->builder), "dicts_tree"));
     GtkTreeStore *dicts_store = GTK_TREE_STORE(gtk_builder_get_object(GTK_BUILDER(self->builder), "dicts_store"));
     while((name = g_dir_read_name(dir)))
     {
-       sprintf(fullpath, "%s%s%s", path, G_DIR_SEPARATOR_S, name);
-       if(g_file_test(fullpath, G_FILE_TEST_IS_DIR))
-       {
-           if(depth < builder_get_int(self->builder, "dicts_search_depth"))
-               dicts_search_recursive(self, fullpath, depth + 1);
-       }
-       if(g_file_test(fullpath, G_FILE_TEST_IS_REGULAR))
-       {
-           if(!strcasecmp(name, "catalog") || !strcasecmp(name, "catalogs"))
-           {
-               gint i;
-               BOOK_INFO *binfo;
-               GtkTreePath *tree_path;
-               for(i = 0;; i++)
-               {
-                   binfo = dicts_load_binfo(path, i);
-                   if(!binfo->book)
-                   {
-                       ebook_free(binfo);
-                       break;
-                   }
-                   if(!group_exists)
-                   {
-                       gtk_tree_store_append(dicts_store, &iter, NULL);
-                       gtk_tree_store_set(dicts_store, &iter, DICT_ALIAS, dirname, DICT_EDITABLE, TRUE, -1);
-                       GtkTreeSelection *selection = gtk_tree_view_get_selection(dicts_tree);
-                       gtk_tree_selection_select_iter(selection, &iter);
-                       gtk_widget_grab_focus(GTK_WIDGET(dicts_tree));
-                       group_exists = TRUE;
-                   }
-                   gtk_tree_store_append(dicts_store, &iter1, &iter);
-                   gtk_tree_store_set(dicts_store, &iter1, DICT_ALIAS, binfo->title, DICT_BINFO, binfo, DICT_EDITABLE, TRUE, -1);
-                   tree_path = gtk_tree_model_get_path(GTK_TREE_MODEL(dicts_store), &iter);
-                   gtk_tree_view_expand_row(dicts_tree, tree_path, TRUE);
-                   gtk_tree_path_free(tree_path);
-               }
-           }
-       }
+        sprintf(fullpath, "%s%s%s", path, G_DIR_SEPARATOR_S, name);
+        if(g_file_test(fullpath, G_FILE_TEST_IS_DIR))
+        {
+            if(depth < builder_get_int(self->builder, "dicts_search_depth"))
+                dicts_search_recursive(self, fullpath, depth + 1);
+        }
+        if(g_file_test(fullpath, G_FILE_TEST_IS_REGULAR))
+        {
+            if(!strcasecmp(name, "catalog") || !strcasecmp(name, "catalogs"))
+            {
+                gint i;
+                BOOK_INFO *binfo;
+                GtkTreePath *tree_path;
+                for(i = 0;; i++)
+                {
+                    binfo = dicts_load_binfo(path, i);
+                    if(!binfo->book)
+                    {
+                        ebook_free(binfo);
+                        break;
+                    }
+                    if(!group_exists)
+                    {
+                        gtk_tree_store_append(dicts_store, &iter, NULL);
+                        gtk_tree_store_set(dicts_store, &iter, DICT_ALIAS, dirname, DICT_EDITABLE, TRUE, -1);
+                        GtkTreeSelection *selection = gtk_tree_view_get_selection(dicts_tree);
+                        gtk_tree_selection_select_iter(selection, &iter);
+                        gtk_widget_grab_focus(GTK_WIDGET(dicts_tree));
+                        group_exists = TRUE;
+                    }
+                    gtk_tree_store_append(dicts_store, &iter1, &iter);
+                    gtk_tree_store_set(dicts_store, &iter1, DICT_ALIAS, binfo->title, DICT_BINFO, binfo, DICT_EDITABLE, TRUE, -1);
+                    tree_path = gtk_tree_model_get_path(GTK_TREE_MODEL(dicts_store), &iter);
+                    gtk_tree_view_expand_row(dicts_tree, tree_path, TRUE);
+                    gtk_tree_path_free(tree_path);
+                }
+            }
+        }
     }
     if(!depth)
     {
-       g_free(dirname);
-       dirname = NULL;
-       group_exists = FALSE;
+        g_free(dirname);
+        dirname = NULL;
+        group_exists = FALSE;
     }
     g_dir_close(dir);
 }
@@ -278,8 +279,8 @@ void dicts_browse_btn_clicked_cb(GtkWidget *widget, gpointer data)
     path = app_browse_disk("Select directory", GTK_WINDOW(gtk_widget_get_toplevel(widget)), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_STOCK_OPEN, path);
     if(path)
     {
-       builder_set_str(mw->builder, "dicts_search_path", path);
-       builder_grab_focus(mw->builder, "dicts_search_path");
+        builder_set_str(mw->builder, "dicts_search_path", path);
+        builder_grab_focus(mw->builder, "dicts_search_path");
     }
 }
 
@@ -289,7 +290,7 @@ void dicts_search_btn_clicked_cb(GtkWidget *widget, gpointer data)
     Dicts *self = mw->dicts;
     GtkEntry *path = GTK_ENTRY(gtk_builder_get_object(GTK_BUILDER(mw->builder), "dicts_search_path"));
     if(!gtk_entry_get_text_length(path))
-       return;
+        return;
     gchar *dirname = (gchar*)gtk_entry_get_text(path);
     dirname = g_strdup(dirname);
     dicts_search_recursive(self, dirname, 0);
@@ -325,15 +326,15 @@ static void dicts_selection_changed_cb(GtkTreeSelection *selection, gpointer dat
 
     if(gtk_tree_selection_get_selected(selection, &store, &iter))
     {
-       path = gtk_tree_model_get_path(store, &iter);
-       dicts_selection_depth = gtk_tree_path_get_depth(path);
-       n = gtk_tree_path_get_depth(path);
-       gtk_tree_path_free(path);
-       if(n == 2)
-           gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DICT_BINFO, &binfo, -1);
+        path = gtk_tree_model_get_path(store, &iter);
+        dicts_selection_depth = gtk_tree_path_get_depth(path);
+        n = gtk_tree_path_get_depth(path);
+        gtk_tree_path_free(path);
+        if(n == 2)
+            gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DICT_BINFO, &binfo, -1);
     }
     else
-       dicts_selection_depth = -1;
+        dicts_selection_depth = -1;
     builder_set_str(mw->builder, "dicts_book_title", binfo ? (binfo->title ? binfo->title : "N/A") : "");
     builder_set_str(mw->builder, "dicts_book_path", binfo ? binfo->path : "");
     builder_set_int(mw->builder, "dicts_subbook_no", binfo ? binfo->subbook_no : -1);
@@ -356,21 +357,21 @@ void dicts_btn_clicked_cb(GtkWidget *widget, gpointer data)
     static gboolean preparewnd = True;
     if(preparewnd)
     {
-       GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
-       gtk_tree_view_insert_column_with_attributes(tree, -1, _("Name"), renderer, "text", DICT_ALIAS, "editable", DICT_EDITABLE, NULL);
-       g_signal_connect(G_OBJECT(renderer), "edited", G_CALLBACK(dicts_cell_edited_cb), self);
-       gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE);
-       g_signal_connect(G_OBJECT(select), "changed", G_CALLBACK(dicts_selection_changed_cb), data);
-       gint i;
-       const gchar *name;
-       for(i = 0; (name = ebook_index_to_method_name(i)); i++)
-           gtk_box_pack_start(box, gtk_label_new(name), False, False, 2);
-
-       GtkTreeDragDestIface *iface = GTK_TREE_DRAG_DEST_GET_IFACE(self->store);
-       iface->row_drop_possible = dicts_row_drop_possible;
-       g_signal_connect(G_OBJECT(tree), "drag_data_received", G_CALLBACK(dicts_drag_data_received_cb), self);
-       gtk_tree_view_set_reorderable(tree, TRUE);
-       preparewnd = False;
+        GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
+        gtk_tree_view_insert_column_with_attributes(tree, -1, _("Name"), renderer, "text", DICT_ALIAS, "editable", DICT_EDITABLE, NULL);
+        g_signal_connect(G_OBJECT(renderer), "edited", G_CALLBACK(dicts_cell_edited_cb), self);
+        gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE);
+        g_signal_connect(G_OBJECT(select), "changed", G_CALLBACK(dicts_selection_changed_cb), data);
+        gint i;
+        const gchar *name;
+        for(i = 0; (name = ebook_index_to_method_name(i)); i++)
+            gtk_box_pack_start(box, gtk_label_new(name), False, False, 2);
+
+        GtkTreeDragDestIface *iface = GTK_TREE_DRAG_DEST_GET_IFACE(self->store);
+        iface->row_drop_possible = dicts_row_drop_possible;
+        g_signal_connect(G_OBJECT(tree), "drag_data_received", G_CALLBACK(dicts_drag_data_received_cb), self);
+        gtk_tree_view_set_reorderable(tree, TRUE);
+        preparewnd = False;
     }
     if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(self->store), &iter1))
     {   
@@ -392,29 +393,29 @@ gboolean dicts_save_item(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *it
     static xmlNodePtr group;
     if(n == 1)
     {
-       xmlNodePtr node = (xmlNodePtr)data;
-       gtk_tree_model_get(model, iter, DICT_ALIAS, &title, -1);
-       group = xmlAddChild(node, xmlNewNode(NULL, (xmlChar*)"group"));
-       xmlNewProp(group, (xmlChar*)"name", title);
-       g_free(title);
+        xmlNodePtr node = (xmlNodePtr)data;
+        gtk_tree_model_get(model, iter, DICT_ALIAS, &title, -1);
+        group = xmlAddChild(node, xmlNewNode(NULL, (xmlChar*)"group"));
+        xmlNewProp(group, (xmlChar*)"name", title);
+        g_free(title);
     }
     else if(n == 2)
     {
-       gchar buf[16];
-       xmlNodePtr node = xmlAddChild(group, xmlNewNode(NULL, (xmlChar*)"dict"));
-       BOOK_INFO *binfo;
-       GtkTreeStore *dicts_store = GTK_TREE_STORE(model);
-       gtk_tree_model_get(GTK_TREE_MODEL(dicts_store), iter, DICT_ALIAS, &title, DICT_BINFO, &binfo, -1);
-       xmlNewProp(node, (xmlChar*)"name", (xmlChar*)title);
-       xmlNewProp(node, (xmlChar*)"path", (xmlChar*)binfo->path);
-       sprintf(buf, "%d", binfo->subbook_no);
-       xmlNewProp(node, (xmlChar*)"subbook", (xmlChar*)buf);
-       xmlNewProp(node, (xmlChar*)"appendix_path", (xmlChar*)(binfo->appendix_path ? binfo->appendix_path : " "));
-       sprintf(buf, "%d", binfo->appendix_subbook_no);
-       xmlNewProp(node, (xmlChar*)"appendix_subbook", (xmlChar*)buf);
-       sprintf(buf, "%d", binfo->active ? 1 : 0);
-       xmlNewProp(node, (xmlChar*)"active", (xmlChar*)buf);
-       g_free(title);
+        gchar buf[16];
+        xmlNodePtr node = xmlAddChild(group, xmlNewNode(NULL, (xmlChar*)"dict"));
+        BOOK_INFO *binfo;
+        GtkTreeStore *dicts_store = GTK_TREE_STORE(model);
+        gtk_tree_model_get(GTK_TREE_MODEL(dicts_store), iter, DICT_ALIAS, &title, DICT_BINFO, &binfo, -1);
+        xmlNewProp(node, (xmlChar*)"name", (xmlChar*)title);
+        xmlNewProp(node, (xmlChar*)"path", (xmlChar*)binfo->path);
+        sprintf(buf, "%d", binfo->subbook_no);
+        xmlNewProp(node, (xmlChar*)"subbook", (xmlChar*)buf);
+        xmlNewProp(node, (xmlChar*)"appendix_path", (xmlChar*)(binfo->appendix_path ? binfo->appendix_path : " "));
+        sprintf(buf, "%d", binfo->appendix_subbook_no);
+        xmlNewProp(node, (xmlChar*)"appendix_subbook", (xmlChar*)buf);
+        sprintf(buf, "%d", binfo->active ? 1 : 0);
+        xmlNewProp(node, (xmlChar*)"active", (xmlChar*)buf);
+        g_free(title);
     }
     return FALSE;
 }
@@ -443,25 +444,25 @@ BOOK_INFO* dicts_load_dict(gchar **data)
     BOOK_INFO *binfo;
     for(i = 1; i < g_strv_length(data); i++)
     {
-       if(!g_strcmp0(data[i - 1], "active"))
-           active = atoi(data[i]);
-       else if(!g_strcmp0(data[i - 1], "path") ? strlen(data[i]): FALSE)
-           book_path = data[i];
-       else if(!g_strcmp0(data[i - 1], "appendix_path") ? strlen(data[i]) : FALSE)
-           appendix_path = data[i];
-       else if(!g_strcmp0(data[i - 1], "subbook"))
-           subbook_no = atoi(data[i]);
-       else if(!g_strcmp0(data[i - 1], "appendix_subbook"))
-           appendix_subbook_no = atoi(data[i]);
+        if(!g_strcmp0(data[i - 1], "active"))
+            active = atoi(data[i]);
+        else if(!g_strcmp0(data[i - 1], "path") ? strlen(data[i]): FALSE)
+            book_path = data[i];
+        else if(!g_strcmp0(data[i - 1], "appendix_path") ? strlen(data[i]) : FALSE)
+            appendix_path = data[i];
+        else if(!g_strcmp0(data[i - 1], "subbook"))
+            subbook_no = atoi(data[i]);
+        else if(!g_strcmp0(data[i - 1], "appendix_subbook"))
+            appendix_subbook_no = atoi(data[i]);
     }
     if(!book_path)
-       return NULL;
+        return NULL;
     binfo = dicts_load_binfo(book_path, subbook_no);
     if(!binfo)
-       return NULL;
+        return NULL;
     binfo->active = active;
     if(appendix_path)
-       binfo->appendix_path = g_strdup(appendix_path);
+        binfo->appendix_path = g_strdup(appendix_path);
     binfo->appendix_subbook_no = appendix_subbook_no;
     return binfo;
 }
@@ -474,23 +475,23 @@ void dicts_startElement(void *ctx, const xmlChar *name, const xmlChar **atts)
     Dicts *dicts = DICTS(cb->_private);
     if(!g_strcmp0((gchar*)name, "Dictionaries"))
     {
-       dicts->active_group = atoi((gchar*)atts[1]);
+        dicts->active_group = atoi((gchar*)atts[1]);
     }
     GtkTreeStore *dicts_store = GTK_TREE_STORE(gtk_builder_get_object(GTK_BUILDER(dicts->builder), "dicts_store"));
     if(!g_strcmp0((gchar*)name, "group"))
     {
-       gtk_tree_store_append(dicts_store, &iter, NULL);
-       gtk_tree_store_set(dicts_store, &iter, DICT_ALIAS, g_strdup((gchar*)atts[1]), DICT_EDITABLE, TRUE, -1);
+        gtk_tree_store_append(dicts_store, &iter, NULL);
+        gtk_tree_store_set(dicts_store, &iter, DICT_ALIAS, g_strdup((gchar*)atts[1]), DICT_EDITABLE, TRUE, -1);
     }
     else if(!g_strcmp0((gchar*)name, "dict"))
     {
-       BOOK_INFO *binfo = dicts_load_dict((gchar**)atts);
-       //if(binfo)
-       {
-           GtkTreeIter child;
-           gtk_tree_store_append(dicts_store, &child, &iter);
-           gtk_tree_store_set(dicts_store, &child, DICT_ALIAS, atts[1], DICT_BINFO, binfo, DICT_EDITABLE, TRUE, -1);
-       }
+        BOOK_INFO *binfo = dicts_load_dict((gchar**)atts);
+        //if(binfo)
+        {
+            GtkTreeIter child;
+            gtk_tree_store_append(dicts_store, &child, &iter);
+            gtk_tree_store_set(dicts_store, &child, DICT_ALIAS, atts[1], DICT_BINFO, binfo, DICT_EDITABLE, TRUE, -1);
+        }
     }
 }
 
@@ -501,7 +502,7 @@ gint dicts_load(Dicts *self)
     const gchar *userdir = prefs_get_userdir();
     sprintf(filename, "%s%s%s", userdir, G_DIR_SEPARATOR_S, FILENAME_DICTS);
     if(!g_file_test(filename, G_FILE_TEST_IS_REGULAR))
-       return 0;
+        return 0;
     memset(&cb, 0, sizeof(xmlSAXHandler));
     cb.startElement = &dicts_startElement;
     cb._private = self;
@@ -514,9 +515,9 @@ gboolean dicts_get_nth(Dicts *self, gint n, GtkTreeIter *iter)
 {
     GtkTreeIter iter1;
     if(!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(self->store), &iter1, NULL, n))
-       return FALSE;
+        return FALSE;
     if(!gtk_tree_model_iter_children(GTK_TREE_MODEL(self->store), iter, &iter1))
-       return FALSE;
+        return FALSE;
     return TRUE;
 }
 
@@ -532,21 +533,21 @@ void dicts_delete_btn_clicked_cb(GtkWidget *widget, gpointer data)
     GtkTreeView *dicts_tree = GTK_TREE_VIEW(gtk_builder_get_object(GTK_BUILDER(dicts->builder), "dicts_tree"));
     GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dicts_tree));
     if(!gtk_tree_selection_get_selected(selection, NULL, &iter))
-       return;
+        return;
 
     GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(dicts->store), &iter);
     b = (gtk_tree_path_get_depth(path) == 1);
     while(b ? gtk_tree_model_iter_children(GTK_TREE_MODEL(dicts->store), &iter2, &iter) : TRUE)
     {
-       GtkTreeIter *iter1 = b ? &iter2 : &iter;
-       gtk_tree_model_get(GTK_TREE_MODEL(dicts->store), iter1, DICT_ALIAS, &title, DICT_BINFO, &binfo, -1);
-       gtk_tree_store_remove(dicts->store, iter1);
-       ebook_free(binfo);
-       g_free(title);
-       if(!b) break;
+        GtkTreeIter *iter1 = b ? &iter2 : &iter;
+        gtk_tree_model_get(GTK_TREE_MODEL(dicts->store), iter1, DICT_ALIAS, &title, DICT_BINFO, &binfo, -1);
+        gtk_tree_store_remove(dicts->store, iter1);
+        ebook_free(binfo);
+        g_free(title);
+        if(!b) break;
     }
     if(b)
-       gtk_tree_store_remove(GTK_TREE_STORE(dicts->store), &iter);
+        gtk_tree_store_remove(GTK_TREE_STORE(dicts->store), &iter);
     gtk_tree_path_free(path);
 }
 
@@ -554,7 +555,7 @@ gboolean dicts_tree_key_release_event_cb(GtkWidget *w, GdkEventKey *evt, gpointe
 {
     if(evt->keyval == GDK_DELETE)
     {
-       dicts_delete_btn_clicked_cb(w, data);
+        dicts_delete_btn_clicked_cb(w, data);
     }
     return TRUE;
 }
index c34ce86..b5a97c2 100644 (file)
@@ -39,9 +39,9 @@ static const SEARCH_METHOD methods[] =
 const gchar* ebook_index_to_method_name(gint index)
 {
     if(index < 0)
-       return NULL;
+        return NULL;
     if(index >= SZ(methods))
-       return NULL;
+        return NULL;
     SEARCH_METHOD m = methods[index];
     return m.name;
 }
@@ -49,9 +49,9 @@ const gchar* ebook_index_to_method_name(gint index)
 gboolean ebook_has_method(EB_Book *book, gint index)
 {
     if(index < 0)
-       return False;
+        return False;
     if(index >= SZ(methods))
-       return False;
+        return False;
     return methods[index].have_method(book);
 }
 
@@ -62,7 +62,7 @@ gboolean ebook_load(BOOK_INFO *binfo)
     EB_Error_Code error_code;
     EB_Subbook_Code sublist[EB_MAX_SUBBOOKS];
     if(!binfo)
-       return False;
+        return False;
     if(!binfo->path)
         return False;
 
@@ -75,7 +75,7 @@ gboolean ebook_load(BOOK_INFO *binfo)
     if(error_code != EB_SUCCESS)
     {
         g_free(binfo->book);
-       binfo->book = NULL;
+        binfo->book = NULL;
         LOG(LOG_CRITICAL, "Failed to bind the book %s: %s", binfo->path, eb_error_message(error_code));
         return False;
     }
@@ -83,21 +83,21 @@ gboolean ebook_load(BOOK_INFO *binfo)
     if(error_code != EB_SUCCESS)
     {
         g_free(binfo->book);
-       binfo->book = NULL;
+        binfo->book = NULL;
         LOG(LOG_CRITICAL, "Failed to get a subbook list: %s", eb_error_message(error_code));
         return False;
     }
     if(binfo->subbook_no >= subcount)
     {
         g_free(binfo->book);
-       binfo->book = NULL;
+        binfo->book = NULL;
         return False;
     }
     error_code = eb_subbook_directory2(binfo->book, sublist[binfo->subbook_no], buff);
     if (error_code != EB_SUCCESS)
     {
         g_free(binfo->book);
-       binfo->book = NULL;
+        binfo->book = NULL;
         LOG(LOG_CRITICAL, "Failed to get the directory: %s", eb_error_message(error_code));
         return False;;
     }
@@ -106,7 +106,7 @@ gboolean ebook_load(BOOK_INFO *binfo)
     if(error_code != EB_SUCCESS)
     {
         g_free(binfo->book);
-       binfo->book = NULL;
+        binfo->book = NULL;
         LOG(LOG_CRITICAL, "Failed to get the title: %s", eb_error_message(error_code));
         return False;
     }
@@ -114,14 +114,14 @@ gboolean ebook_load(BOOK_INFO *binfo)
     if(!binfo->title)
     {
         g_free(binfo->book);
-       binfo->book = NULL;
+        binfo->book = NULL;
         return False;
     }
     error_code = eb_set_subbook(binfo->book, binfo->subbook_no);
     if(error_code != EB_SUCCESS)
     {
         g_free(binfo->book);
-       binfo->book = NULL;
+        binfo->book = NULL;
         LOG(LOG_CRITICAL, "Failed to get the title: %s", eb_error_message(error_code));
         return False;
     }
@@ -320,8 +320,8 @@ static void ebook_search_results_filter(gpointer data, gpointer user_data)
     RESULT **res = (RESULT**)user_data;
     if(result_compare((RESULT*)data, *res))
     {
-       result_free(*res);
-       *res = NULL;
+        result_free(*res);
+        *res = NULL;
     }
 }
 
@@ -342,32 +342,32 @@ static gint ebook_search_results_save(BOOK_INFO *binfo, const gchar *word, gint
     }
     for(i = 0; (i < maxhits) && (i < hits_count); i++)
     {
-       res = (RESULT*)g_new0(RESULT, 1);
-       if(!res)
-       {
-           LOG(LOG_ERROR, "No memory");
-           break;
-       }
-       error_code = eb_seek_text(binfo->book, &(hits[i].heading));
-       if(error_code != EB_SUCCESS)
-       {
-           g_free(res);
-           continue;
-       }
-       error_code = eb_read_heading(binfo->book, binfo->appendix, &hooksets.heading, NULL, MAX_BUFSIZE, heading, &len);
-       if(error_code != EB_SUCCESS)
-       {
-           g_free(res);
-           break;
-       }
-       heading[len] = '\0';
-
-       res->heading = iconv_convert(ENC_EUC_JP, ENC_UTF8, heading);
-       res->binfo = binfo;
-       res->pos = hits[i].text;
-       g_sequence_foreach(ebook.results, ebook_search_results_filter, &res);
-       if(res)
-           g_sequence_append(ebook.results, res);
+        res = (RESULT*)g_new0(RESULT, 1);
+        if(!res)
+        {
+            LOG(LOG_ERROR, "No memory");
+            break;
+        }
+        error_code = eb_seek_text(binfo->book, &(hits[i].heading));
+        if(error_code != EB_SUCCESS)
+        {
+            g_free(res);
+            continue;
+        }
+        error_code = eb_read_heading(binfo->book, binfo->appendix, &hooksets.heading, NULL, MAX_BUFSIZE, heading, &len);
+        if(error_code != EB_SUCCESS)
+        {
+            g_free(res);
+            break;
+        }
+        heading[len] = '\0';
+
+        res->heading = iconv_convert(ENC_EUC_JP, ENC_UTF8, heading);
+        res->binfo = binfo;
+        res->pos = hits[i].text;
+        g_sequence_foreach(ebook.results, ebook_search_results_filter, &res);
+        if(res)
+            g_sequence_append(ebook.results, res);
     }
     g_free(hits);
     return error_code;
@@ -384,20 +384,20 @@ EB_Error_Code ebook_search_keyword(EB_Book *book, const char *input_word)
 gboolean ebook_search_book(const gchar *word, gint method, GSequence *results, gint maxhits, BOOK_INFO *binfo)
 {
     if(!binfo)
-       return False;
+        return False;
     if(!binfo->book)
-       return False;
+        return False;
     if(!methods[method].have_method(binfo->book))
-       return False;
+        return False;
     gchar *euc_str = NULL;
     if(word)
     {
-       euc_str = iconv_convert(ENC_UTF8, ENC_EUC_JP, word);
-       if(!validate_euc(euc_str, False))
-       {
-           g_free(euc_str);
-           return False;
-       }
+        euc_str = iconv_convert(ENC_UTF8, ENC_EUC_JP, word);
+        if(!validate_euc(euc_str, False))
+        {
+            g_free(euc_str);
+            return False;
+        }
     }
     if(results)
     {
@@ -406,28 +406,28 @@ gboolean ebook_search_book(const gchar *word, gint method, GSequence *results, g
     }
     if((method == SEARCH_METHOD_MENU) || (method == SEARCH_METHOD_COPYRIGHT))
     {
-       EB_Position pos;
-       EB_Error_Code err;
-       gboolean menu = (method == SEARCH_METHOD_MENU);
-       if(menu)
-           err = eb_menu(binfo->book, &pos);
-       else
-           err = eb_copyright(binfo->book, &pos);
-       if(err == EB_SUCCESS)
-       {
-           RESULT *res = (RESULT *)calloc(sizeof(RESULT), 1);
-           res->heading = g_strdup_printf("%s : %s", menu ? _("menu") : _("copyright"), binfo->title);
-           res->binfo = binfo;
-           res->pos = pos;
-           g_sequence_append(ebook.results, res);
-       }
+        EB_Position pos;
+        EB_Error_Code err;
+        gboolean menu = (method == SEARCH_METHOD_MENU);
+        if(menu)
+            err = eb_menu(binfo->book, &pos);
+        else
+            err = eb_copyright(binfo->book, &pos);
+        if(err == EB_SUCCESS)
+        {
+            RESULT *res = (RESULT *)calloc(sizeof(RESULT), 1);
+            res->heading = g_strdup_printf("%s : %s", menu ? _("menu") : _("copyright"), binfo->title);
+            res->binfo = binfo;
+            res->pos = pos;
+            g_sequence_append(ebook.results, res);
+        }
     }
     else
     {
-       if(methods[method].search(binfo->book, euc_str) == EB_SUCCESS)
-       {
-           ebook_search_results_save(binfo, euc_str, maxhits);
-       }
+        if(methods[method].search(binfo->book, euc_str) == EB_SUCCESS)
+        {
+            ebook_search_results_save(binfo, euc_str, maxhits);
+        }
     }
     g_free(euc_str);
     return True;
@@ -448,15 +448,15 @@ gboolean ebook_search(const gchar *word, gint method, GSequence *results, gint m
     gboolean ret = False;
     do {
         gtk_tree_model_get(store, &iter, DICT_BINFO, &binfo, -1);
-       if(!binfo)
-           continue;
+        if(!binfo)
+            continue;
         if(!binfo->active)
             continue;
         ret = ret | ebook_search_book(word, method, NULL, maxhits, binfo);
-       if(!max_per_dict)
-           maxhits -= g_sequence_get_length(ebook.results);
-       if(maxhits <= 0)
-           break;
+        if(!max_per_dict)
+            maxhits -= g_sequence_get_length(ebook.results);
+        if(maxhits <= 0)
+            break;
     } while (gtk_tree_model_iter_next(store, &iter));
     ebook.results = NULL;
     return ret;
index d7ec56d..4df45c1 100644 (file)
@@ -94,6 +94,15 @@ static void prefs_init(Prefs *self)
     //gst_element_class_add_pad_template(gstelement_class, gst_static_pad_template_get(&prefs_template));
 
     g_datalist_init(&(self->data));
+    int i;
+    for(i = 0; i < SZ(prefs_list); i++)
+    {
+        const pref1 *p1 = &(prefs_list[i]);
+        if(p1->type == G_TYPE_INT)
+            prefs_set_int(self, p1->name, p1->val.i);
+        else if(p1->type == G_TYPE_STRING)
+            prefs_set_str(self, p1->name, p1->val.s);
+    }
 
     gchar *home_dir = getenv("HOME");
     gchar *userdir = g_strdup_printf("%s%s.%s", home_dir, G_DIR_SEPARATOR_S, PACKAGE_NAME);
@@ -376,12 +385,6 @@ void prefs_set_int(Prefs *self, const gchar *name, gint value)
 const gchar* prefs_get_str(Prefs *self, const gchar *name)
 {
     gpointer ptr = g_datalist_get_data(&self->data, name);
-    if(!ptr)
-    {
-        const pref1 *p1 = prefs_get1(self, name);
-        if(p1)
-            ptr = p1->val.s;
-    }
     return (const gchar*)ptr;
 }