Hiroyuki Ikezoe
ikezo****@users*****
Sun Dec 3 18:46:41 JST 2006
Index: kazehakase/module/search/kz-search-hyper-estraier.c diff -u kazehakase/module/search/kz-search-hyper-estraier.c:1.1 kazehakase/module/search/kz-search-hyper-estraier.c:1.2 --- kazehakase/module/search/kz-search-hyper-estraier.c:1.1 Sun Dec 3 16:12:15 2006 +++ kazehakase/module/search/kz-search-hyper-estraier.c Sun Dec 3 18:46:41 2006 @@ -20,6 +20,7 @@ #include <ctype.h> #include <glib/gi18n.h> +#include <estraier.h> #include "kazehakase.h" #include "utils/utils.h" @@ -93,7 +94,6 @@ static void make_index (KzSearch *search); static gboolean exist_index_dir (KzSearch *search); -static gchar *estsearch_get_version (void); static gboolean _update_index (gpointer data); static KzSearchHyperEstraier *the_kz_search_hyper_estraier = NULL; @@ -211,108 +211,56 @@ return KZ_SEARCH(g_object_new(KZ_TYPE_SEARCH_HYPER_ESTRAIER, NULL)); } -static gboolean -execute_search_command(const gchar *search_text, gint *standard_output) +static gchar * +create_search_result_html (const gchar *text) { - gboolean ret; - const gchar *estsearch_com = "estcmd search -vx "; - gchar *command; - gint argc; - gchar **argv = NULL; - GSpawnFlags flags; - GPid pid; - gint err; - gchar **split = NULL; - gchar *join = NULL; - gint max_results = 20, num_summary = 128, half_of_summary; - gchar *except_word; - gchar **except_keywords = NULL; - - KZ_CONF_GET("History", "num_summary", num_summary, INT); - KZ_CONF_GET("History", "max_results", max_results, INT); - half_of_summary = num_summary / 2; - - split = g_strsplit(search_text, " ", -1); - if (split) + ESTDB *db; + ESTCOND *cond; + gchar *dbname, *dirname; + int ecode, *results, n_results, i, ret; + gchar *except_word, *tmp; + gchar **texts; + GString *html, *phrase; + gint num_summary = 128, max_results = 20, half_of_summary; + size_t len; + + dbname = g_strdup_printf("%s%s", g_get_home_dir(), HISTORY_INDEX); + db = est_db_open(dbname, ESTDBREADER, &ecode); + g_free(dbname); + if (!db) { - join = g_strjoinv(" AND ", split); - g_strfreev(split); + g_warning("error: %s", est_err_msg(ecode)); + return NULL; } + cond = est_cond_new(); + texts = g_strsplit(text, " ", -1); + tmp = g_strjoinv(" AND ", texts); + g_strfreev(texts); + phrase = g_string_new(tmp); + except_word = KZ_CONF_GET_STR("History", "except_keyword"); if (except_word && *except_word) { - except_keywords = g_strsplit(except_word, ",", -1); + texts = g_strsplit(except_word, ",", -1); g_free(except_word); - except_word = g_strjoinv(" ANDNOT ", except_keywords); - g_strfreev(except_keywords); - - command = g_strdup_printf("%s -max %d -sn %d %d %d %s%s %s ANDNOT %s", - estsearch_com, - max_results, - num_summary, - half_of_summary, - half_of_summary, - g_get_home_dir(), - HISTORY_INDEX" ", - join, - except_word); - g_free(except_word); - } - else - { - command = g_strdup_printf("%s -max %d -sn %d %d %d %s%s %s", - estsearch_com, - max_results, - num_summary, - half_of_summary, - half_of_summary, - g_get_home_dir(), - HISTORY_INDEX" ", - join); + i = 0; + while (texts[i]) + { + phrase = g_string_append(phrase, " ANDNOT "); + phrase = g_string_append(phrase, texts[i]); + } + g_strfreev(texts); } + est_cond_set_phrase(cond, phrase->str); + g_string_free(phrase, TRUE); - if (join) - g_free(join); - - g_shell_parse_argv(command, - &argc, - &argv, - NULL); - - flags = G_SPAWN_SEARCH_PATH; - ret = g_spawn_async_with_pipes(NULL, - argv, - NULL, - flags, - NULL, - NULL, - &pid, - NULL, - standard_output, - &err, - NULL); - g_strfreev(argv); - g_free(command); - - return ret; -} - + KZ_CONF_GET("History", "num_summary", num_summary, INT); + KZ_CONF_GET("History", "max_results", max_results, INT); + half_of_summary = num_summary / 2; -static gchar * -create_search_result_html (gint out, const gchar *text) -{ - GIOChannel *io; - gchar *line; - gsize length; - gchar *title = NULL, *uri = NULL, *date = NULL, *desc = NULL; - gchar *cache_link = NULL; - gchar *estversion = estsearch_get_version(); - GString *html; + results = est_db_search(db, cond, &n_results, NULL); - io = g_io_channel_unix_new(out); - g_io_channel_set_encoding(io, NULL, NULL); - html = g_string_sized_new(0); g_string_append(html, DTD"\n"); @@ -323,69 +271,63 @@ g_string_append_printf(html, "<h1>Search results for %s</h1>", text); - while (g_io_channel_read_line(io, &line, &length, NULL, NULL) == G_IO_STATUS_NORMAL) + dirname = g_strconcat(g_get_home_dir(), HISTORY_DIR, NULL); + len = strlen(dirname); + g_free(dirname); + + for (i = 0; i < n_results, i < max_results; i++) { - if (g_str_has_prefix(line, "<document")) + ESTDOC *doc; + const CBLIST *texts; + int j; + const gchar *title, *uri, *date, *desc = NULL; + const gchar *cache_link, *thumb_uri, *thumb_filename; + + doc = est_db_get_doc(db, results[i], 0); + if (!doc) + continue; + + cache_link = est_doc_attr(doc, "@uri"); + uri = create_uri_from_filename(cache_link + + strlen("file://") + len); + thumb_filename = egg_pixbuf_get_thumb_filename(uri, + EGG_PIXBUF_THUMB_LARGE); + thumb_uri = g_strdup_printf("history-search:?image=%s", + thumb_filename); + title = est_doc_attr(doc, "@title"); + date = est_doc_attr(doc, "@mdate"); + + texts = est_doc_texts(doc); +#warning FIXME! Use num_summary etc. + for (j = 0; j < cblistnum(texts); j++) { - size_t len; - gchar *dirname = g_strconcat(g_get_home_dir(), - HISTORY_DIR, - NULL); - len = strlen(dirname); - - cache_link = xml_get_attr(line, "uri"); - uri = create_uri_from_filename(cache_link + - strlen("file://") + - len); - g_free(dirname); + desc = cblistval(texts, j, NULL); } - else if (g_str_has_prefix(line, "</document>")) - { - gchar *thumb_filename, *thumb_uri; - thumb_filename = egg_pixbuf_get_thumb_filename(uri, - EGG_PIXBUF_THUMB_LARGE); - thumb_uri = g_strdup_printf("history-search:?image=%s", - thumb_filename); - g_string_append_printf(html, - CONTENT, - uri, - title, - thumb_uri, /* thumbnail */ - desc, - uri, - cache_link, - date); - - g_free(desc); - g_free(title); - g_free(uri); - g_free(date); - g_free(cache_link); - g_free(thumb_filename); - g_free(thumb_uri); - } - else if (g_str_has_prefix(line, "<attribute name=\"@title\"")) - { - title = xml_get_attr(line, "value"); - } - else if (g_str_has_prefix(line, "<attribute name=\"@mdate\"")) - { - date = xml_get_attr(line, "value"); - } - else if (g_str_has_prefix(line, "<snippet")) - { - gchar *summary = xml_get_content(line); - desc = remove_tag(summary, g_strlen(summary)); - g_free(summary); - } - g_free(line); + + g_string_append_printf(html, + CONTENT, + uri, + title, + thumb_uri, /* thumbnail */ + desc, + uri, + cache_link, + date); + est_doc_delete(doc); } - g_io_channel_unref(io); - g_string_append_printf(html, FOOTER, HYPER_ESTRAIERRAIER_URI, estversion); + free(results); + + est_cond_delete(cond); + + ret = est_db_close(db, &ecode); + if (!ret) + { + g_warning("error: %s", est_err_msg(ecode)); + } + + g_string_append_printf(html, FOOTER, HYPER_ESTRAIERRAIER_URI, _EST_VERSION); g_string_append(html, "</body></html>"); - if (estversion) - g_free(estversion); return g_string_free(html, FALSE); } @@ -393,15 +335,9 @@ gchar * get_search_result_html (KzSearch *search, const gchar *text) { - gint out; - if (!text) return NULL; - if (!exists_search_cmd) return NULL; - if (!execute_search_command(text, &out)) - return NULL; - - return create_search_result_html(out, text); + return create_search_result_html(text); } static gboolean @@ -460,6 +396,7 @@ return FALSE; } + gboolean register_document (KzSearch *search, const gchar *filename) { @@ -550,129 +487,11 @@ return pid; } -static gchar* -estsearch_get_version (void) -{ - gchar *version, *estcmd_output; - gchar **splited_hyper_estraiercmd_output = NULL; - const gchar *estversion = "estcmd version"; - gint argc; - gchar **argv = NULL; - GSpawnFlags flags; - GPid pid; - gint out, err; - gboolean ret; - GIOChannel *io; - gsize length; - - if (!exists_search_cmd) return NULL; - - g_shell_parse_argv(estversion, - &argc, - &argv, - NULL); - - flags = G_SPAWN_SEARCH_PATH; - ret = g_spawn_async_with_pipes(NULL, - argv, - NULL, - flags, - NULL, - NULL, - &pid, - NULL, - &out, - &err, - NULL); - g_strfreev(argv); - if (!ret) return NULL; - - io = g_io_channel_unix_new(out); - g_io_channel_set_encoding(io, NULL, NULL); - g_io_channel_read_line(io, &estcmd_output, &length, NULL, NULL); - g_io_channel_shutdown(io, TRUE, NULL); - g_io_channel_unref(io); - - splited_hyper_estraiercmd_output = g_strsplit(estcmd_output, " ", -1); - version = g_strdup(splited_hyper_estraiercmd_output[2]); - - g_free(estcmd_output); - g_strfreev(splited_hyper_estraiercmd_output); - - return version; -} - -static KzBookmark * -create_search_result_bookmark (gint out, const gchar *text) -{ - GIOChannel *io; - gchar *line; - gsize length; - gchar *title = NULL, *uri = NULL, *desc = NULL; - KzBookmark *result; - - io = g_io_channel_unix_new(out); - g_io_channel_set_encoding(io, NULL, NULL); - - result = kz_bookmark_pure_folder_new(); - - while (g_io_channel_read_line(io, &line, &length, NULL, NULL) == G_IO_STATUS_NORMAL) - { - if (g_str_has_prefix(line, "</document>")) - { - KzBookmark *child; - child = kz_bookmark_new_with_attrs(title, uri, desc); - kz_bookmark_append(result, child); - g_object_unref(child); - g_free(desc); - g_free(title); - g_free(uri); - } - else if (g_str_has_prefix(line, "<uri>")) - { - gchar *dirname, *orig_uri; - gchar *link; - size_t len; - link = xml_get_attr(line, "uri"); - dirname = g_strconcat(g_get_home_dir(), - HISTORY_DIR, - NULL); - len = strlen(dirname); - orig_uri = create_uri_from_filename(link + strlen("file://") + len); - uri = url_decode(orig_uri); - g_free(orig_uri); - g_free(dirname); - g_free(link); - } - else if (g_str_has_prefix(line, "<title>")) - { - title = xml_get_content(line); - } - else if (g_str_has_prefix(line, "<summary")) - { - gchar *summary = xml_get_content(line); - desc = remove_tag(summary, g_strlen(summary)); - g_free(summary); - } - g_free(line); - } - g_io_channel_unref(io); - - return result; -} - static KzBookmark * get_search_result_bookmark (KzSearch *search, const gchar *text) { - gint out; - - if (!text) return NULL; - if (!exists_search_cmd) return NULL; - - if (!execute_search_command(text, &out)) - return NULL; - - return create_search_result_bookmark(out, text); + /* not implemented yet */ + return NULL; } static void