From 556e12ff0dcf111e43fe0d6bb20c1d9c9f3d0e04 Mon Sep 17 00:00:00 2001 From: Mirko Lindner Date: Sat, 23 Jan 2010 17:58:51 +0100 Subject: [PATCH] refactoring and function rename Signed-off-by: Mirko Lindner --- src/search_dialog.cc | 2 +- src/vido.cc | 74 +++++++++++++++++++++++--------------------- src/vido.hh | 4 +-- 3 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/search_dialog.cc b/src/search_dialog.cc index 21fe65c..be3f712 100644 --- a/src/search_dialog.cc +++ b/src/search_dialog.cc @@ -40,7 +40,7 @@ search_dialog::search_dialog(main_window* parent, const Glib::ustring& input) int result = dialog.run(); if (result == Gtk::RESPONSE_OK) { - set_search(entry.get_text().c_str()); + getArticleFromTitle(entry.get_text().c_str()); } } diff --git a/src/vido.cc b/src/vido.cc index 5cdf8ec..e6f8299 100644 --- a/src/vido.cc +++ b/src/vido.cc @@ -48,7 +48,7 @@ Gtk::Widget *html; GtkWidget *html_wg; // // // misc functions - +// get zimFile, if not already opened, open file const zim::File& get_file() { static zim::File zimFile; @@ -64,18 +64,21 @@ const zim::File& get_file() return zimFile; } +// display message in gtk window 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)); -// } +// fill gtkhtml widget with new content +//TODO prepend "top" anchor on html +void fill_gtkhtml(std::string& html){ + log_debug("fill gtkhtml called"); + gtk_html_flush(GTK_HTML(html_wg)); + gtk_html_load_from_string(GTK_HTML(html_wg), html.c_str(), -1); +} +//UNUSED // // copy article html from while loop into global variable void set_article(const std::string& txt) { @@ -110,9 +113,10 @@ void show_random() 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()); + fill_gtkhtml(res); +// 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 @@ -125,7 +129,7 @@ void search_window(main_window *window_x) // // set displayed html to given url // // FIXME: returns several articles and displays on one page ... why? -void set_html(const gchar *url) +void getArticleFromUrl(const gchar *url) { // TODO unescape url @@ -175,16 +179,9 @@ void set_html(const gchar *url) if (article.good()) // check if article is really found { -// // get content html content = article.getPage(); -// // emtpy gtkhtml widget - gtk_html_flush(GTK_HTML(html_wg)); - -// // load new data into gtkhtml widget - gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1); -// window.resize(220, 140); - log_debug("term=" << term << " url=" << url); + fill_gtkhtml(content); } else { @@ -200,7 +197,7 @@ void set_html(const gchar *url) // // test functions -void set_search(const gchar *url) +void getArticleFromTitle(const gchar *url) { char ns; ns = 'A'; @@ -209,23 +206,32 @@ void set_search(const gchar *url) zim::Search::Results result; zim::Search search(z); std::string term(url); + std::string res; 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){ + log_debug("one article in result"); + zim::Article article = z.getArticle(result[0].getArticle().getIndex()); - content = article.getPage(false, 10); + //loop in case article is redirect + do + { + article = article.getRedirectArticle(); + }while(article.isRedirect()); - gtk_html_flush(GTK_HTML(html_wg)); - gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1); - } - else - { - std::string res; + res = article.getPage(false, 10); + + } + else + { + log_debug("more than one article in result"); for (unsigned i = 0; i < result.size(); ++i) { #if HAVE_ZIM_QUNICODE_H @@ -234,13 +240,11 @@ void set_search(const gchar *url) 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); + } + + fill_gtkhtml(res); } - - log_debug(url); } // // // window response functions @@ -261,11 +265,11 @@ bool on_link_clicked(GtkHTML *html, const gchar *url) }else{ term = term.substr(0, found); //TODO make "set_html" func accept anchor name - set_html(const_cast(term.c_str())); + getArticleFromUrl(const_cast(term.c_str())); } }else{ - set_html(url); + getArticleFromUrl(url); } return true; diff --git a/src/vido.hh b/src/vido.hh index 22fcf86..3c2cca7 100644 --- a/src/vido.hh +++ b/src/vido.hh @@ -27,7 +27,7 @@ class main_window; extern void show_random(); extern void search_window(main_window *window); -extern void set_html(const gchar *url); -extern void set_search(const gchar *url); +extern void getArticleFromUrl(const gchar *url); +extern void getArticleFromTitle(const gchar *url); #endif // VIDO_HH