1
0
mirror of git://projects.qi-hardware.com/vido.git synced 2024-12-22 14:23:19 +02:00

fix history

Signed-off-by: Mirko Lindner <mirko@sharism.cc>
This commit is contained in:
Mirko Lindner 2010-02-21 20:23:03 +01:00
parent 024e25a62f
commit 93a9bc198d

View File

@ -52,7 +52,7 @@ Gtk::ScrolledWindow *scrolled_window2;
Gtk::Widget *html;
GtkWidget *html_wg;
std::vector < std::pair< std::string, std::string > > history;
unsigned int position = 0;
int position;
bool historyCall = false;
// variable to keep url of currently displayed article
std::string current_url = "";
@ -65,12 +65,12 @@ const zim::File& get_file()
if (!zimFile.good())
{
log_debug("file not initialized:" << fileName);
// log_debug("file not initialized:" << fileName);
zimFile = zim::File(fileName);
log_debug("number of articles: " << zimFile.getCountArticles());
// log_debug("number of articles: " << zimFile.getCountArticles());
}
log_debug("returning file.");
// log_debug("returning file.");
return zimFile;
}
@ -116,29 +116,41 @@ void show_help()
// fill gtkhtml widget with new content
void fill_gtkhtml(std::string& html_str, std::string url, std::string title){
log_debug("fill gtkhtml called with " << url << " and " << title);
log_debug("fill gtkhtml called with " << position << " " << url << " and " << title);
std::string ccontent;
ccontent = "<a name=\"top\"></a>" + html_str;
gtk_html_flush(GTK_HTML(html_wg));
gtk_html_load_from_string(GTK_HTML(html_wg), ccontent.c_str(), -1);
current_url = url;
if ((url != "") && title != ""){
if (position == 0){
log_debug("adding " << title << " to history");
if ((!historyCall)){
if(position != 0){
std::vector < std::pair< std::string, std::string > >::iterator iterator = history.begin();
log_debug("history size: " << history.size());
for (int d=0; d < history.size(); d++){
log_debug("history entry: " << d << " " << history[d].first);
}
for (int m=history.size()+position; m <= history.size(); m++){
log_debug("erasing history: number: "<< m << "title: " << history[m].first);
history.erase(iterator+m);
}
}
log_debug("adding " << title << " to history \n" << "position: " << position);
history.push_back( std::make_pair( url, title ) );
if (history.size() == 11){
history.erase(history.begin());
}
log_debug("history size after stuff: " << history.size());
}else{
if (!historyCall){
log_debug("clearing history");
std::vector < std::pair< std::string, std::string > >::iterator iterator = history.begin();
for (int m=0; m < position; m++){
history.erase(iterator+m);
}
}else{
// if (!historyCall){
// log_debug("clearing history");
// std::vector < std::pair< std::string, std::string > >::iterator iterator = history.begin();
// for (int m=0; m < position; m++){
// history.erase(iterator+m);
// }
// }else{
historyCall = false;
}
// }
}
}
@ -157,10 +169,13 @@ void show_history()
screenblock(1);
std::string res, url, title;
res += "<ul style=\"list-style-type:none;\">";
for(unsigned i=history.size()-1; i > 0; --i)
log_debug("history size: " << history.size());
for (int d=0; d < history.size(); d++){
log_debug("history entry: " << d << " " << history[d].first);
}
for(int i=history.size()-1; i >= 0; i--)
{
log_debug("entry: " << i);
#if HAVE_ZIM_QUNICODE_H
res += "<li><a href=" + history[i].first + ">" + history[i].second + "</a></li>";
#else
@ -187,14 +202,18 @@ void history_jump(int jumper){
log_debug("new_pos = " << new_pos);
log_debug("history size = " << history.size());
if ((new_pos > 0) && (new_pos <= history.size())){
// position = new_rel_pos;
historyCall = true;
std::string url = history[new_pos-1].first;
log_debug("url is = " << url);
// const char* uri = url.c_str();
// getArticleFromUrl(const_cast<char*>(url.c_str()), 1);
const char* uri = url.c_str();
getArticleFromUrl(uri, 0);
getArticleFromUrl(uri, new_rel_pos);
position = new_rel_pos;
}else{
log_debug("jump not possible");
window2->connect_all();
}
}
@ -214,7 +233,7 @@ void show_random()
// window2->disconnect_all();
screenblock(1);
log_debug("random called. window all disconnected");
// log_debug("random called. window all disconnected");
zim::File m = get_file();
zim::Article article;
do
@ -234,12 +253,12 @@ void show_random()
std::string res = article.getPage();
log_debug("article size=" << res.size());
log_debug("article title=" << article.getTitle());
log_debug("article namespace=" << article.getNamespace());
position = 0;
// log_debug("article size=" << res.size());
// log_debug("article title=" << article.getTitle());
// log_debug("article namespace=" << article.getNamespace());
// position = 0;
fill_gtkhtml(res, article.getUrl(), article.getTitle());
log_debug("random called. window all connected");
// log_debug("random called. window all connected");
}
@ -264,15 +283,16 @@ void GoToTop()
// // set displayed html to given url
// // FIXME: returns several articles and displays on one page ... why?
void getArticleFromUrl(const gchar *url, int pos = 0)
void getArticleFromUrl(const gchar *url, int pos)
{
// TODO unescape url
// // create ZIM file accessor
zim::File m = get_file();
// // convert url to string
std::string term(url);
std::string term;
term.assign(url);
// log_debug("str url" << term << "\n");
std::string content;
// // replace '+' signs with spaces in url
std::replace(term.begin(), term.end(), '+', ' ');
@ -315,9 +335,7 @@ void getArticleFromUrl(const gchar *url, int pos = 0)
if (article.good()) // check if article is really found
{
content = article.getPage();
if (pos == 0){
position = 0;
}
position = pos;
fill_gtkhtml(content, article.getUrl(), article.getTitle());
}
else
@ -336,6 +354,7 @@ void getArticleFromUrl(const gchar *url, int pos = 0)
// // test functions
void getArticleFromTitle(const gchar *phrase)
{
historyCall = false;
screenblock(1);
char ns;
ns = 'A';
@ -367,6 +386,7 @@ void getArticleFromTitle(const gchar *phrase)
res = article.getPage(false, 10);
url = article.getUrl();
title = article.getTitle();
log_debug("set article from title position = 0");
position = 0;
}
else
@ -408,11 +428,11 @@ bool on_link_clicked(GtkHTML *html, const gchar *url)
term = term.substr(0, found);
//TODO make "set_html" func accept anchor name
const char* uri = term.c_str();
getArticleFromUrl(uri, 0);
getArticleFromUrl(uri, position);
}
}else{
getArticleFromUrl(url,0);
getArticleFromUrl(url,position);
}
return true;
@ -460,7 +480,7 @@ int main(int argc, char **argv)
window.add(scrolled_window);
window.show_all();
position = 0;
show_random();
gtk_html_edit_make_cursor_visible(GTK_HTML(html_wg));
Gtk::Main::run(window);