From c4b1f2b48298f187cb18255220c191fc404e1203 Mon Sep 17 00:00:00 2001 From: Mirko Lindner Date: Fri, 22 Jan 2010 17:33:35 +0100 Subject: [PATCH] adapt search to new zimlib and make anchor jump work Signed-off-by: Mirko Lindner --- src/vido.cc | 63 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/src/vido.cc b/src/vido.cc index 37e86b8..5cdf8ec 100644 --- a/src/vido.cc +++ b/src/vido.cc @@ -24,6 +24,7 @@ #include #include #include +// #include #include "config.h" #include "message_dialog.hh" @@ -56,6 +57,7 @@ const zim::File& get_file() log_debug("file not initialized:" << fileName); zimFile = zim::File(fileName); log_debug("number of articles: " << zimFile.getCountArticles()); + } log_debug("returning file."); @@ -67,6 +69,13 @@ void show_message(std::string title, std::string txt) message_dialog(window, title, txt); } +// void page_move(int val){ +// Gtk::Adjustment *vertical = scrolled_window.get_vadjustment(); +// double page_size = vertical->get_page_size(); +// double value = vertical->get_value(); +// vertical->set_value(value + (val * page_size)); +// } + // // copy article html from while loop into global variable void set_article(const std::string& txt) { @@ -82,7 +91,7 @@ void show_random() log_debug("random called."); zim::File m = get_file(); - static unsigned int seed = static_cast(time(0)); + unsigned int seed = static_cast(time(0)); zim::size_type idx = static_cast(static_cast(m.getCountArticles()) * rand_r(&seed) / RAND_MAX); zim::Article article; @@ -98,9 +107,12 @@ void show_random() content = article.getPage(); log_debug("article size=" << content.size()); - + log_debug("article title=" << article.getTitle()); + log_debug("article namespace=" << article.getNamespace()); + gtk_html_flush(GTK_HTML(html_wg)); gtk_html_load_from_string(GTK_HTML(html_wg), res.c_str(), -1); + log_debug("html \n" << res.c_str()); } // // display search dialog @@ -177,6 +189,7 @@ void set_html(const gchar *url) else { std::cerr << "article \"" << url << "\" not found" << std::endl; + show_message("Error", "The article you requested (" + term + ") was not found."); } } catch (const std::exception& e) @@ -189,19 +202,20 @@ void set_html(const gchar *url) // // test functions void set_search(const gchar *url) { + char ns; + ns = 'A'; zim::File z = get_file(); zim::Search::Results result; zim::Search search(z); std::string term(url); - std::replace(term.begin(), term.end(), '+', ' '); - search.search(result, term); - if(result.size() == 0) + search.setSearchLimit(25); + search.find(result, ns, term); + if( result.size() == 0) { show_message("Error", "The article you requested (" + term + ") was not found."); - } - else if (result.size() == 1) - { + + }else if (result.size() == 1){ zim::Article article = z.getArticle(result[0].getArticle().getIndex()); content = article.getPage(false, 10); @@ -215,15 +229,14 @@ void set_search(const gchar *url) for (unsigned i = 0; i < result.size(); ++i) { #if HAVE_ZIM_QUNICODE_H - res += "
  • " + result[i].getArticle().getTitle().toXML() + "
  • "; + res += "
  • " + result[i].getArticle().getLongUrl().toXML() + "
  • "; #else - res += "
  • " + result[i].getArticle().getTitle() + "
  • "; + res += "
  • " + result[i].getArticle().getUrl() + "
  • "; #endif } gtk_html_flush(GTK_HTML(html_wg)); gtk_html_load_from_string(GTK_HTML(html_wg), res.c_str(), -1); -// window.resize(220, 140); } @@ -236,9 +249,25 @@ void set_search(const gchar *url) // FIXME show error window when dead-link requested bool on_link_clicked(GtkHTML *html, const gchar *url) { - - set_html(url); -// window.resize(220, 140); + std::string term(url); + size_t found; + found=term.find("#"); + if (found != std::string::npos){ + + if (found == 0){ + term.erase(0, 1); + bool success = gtk_html_jump_to_anchor(GTK_HTML(html_wg), const_cast(term.c_str())); + log_debug("jump to " << term << "worked: " << success); + }else{ + term = term.substr(0, found); + //TODO make "set_html" func accept anchor name + set_html(const_cast(term.c_str())); + } + + }else{ + set_html(url); + } + return true; } @@ -269,10 +298,8 @@ int main(int argc, char **argv) html = Glib::wrap(html_wg); g_signal_connect( G_OBJECT( html_wg ), "link_clicked", G_CALLBACK( on_link_clicked ), NULL ); gtk_html_set_caret_mode(GTK_HTML(html_wg),true); - - - Gtk::ScrolledWindow scrolled_window; - + + static Gtk::ScrolledWindow scrolled_window; scrolled_window.add(*html); scrolled_window.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);