1
0
mirror of git://projects.qi-hardware.com/vido.git synced 2024-11-01 09:16:18 +02:00

make links clickable

clicking on links now returns a new page --- but with several articles ...


Signed-off-by: Mirko Lindner <mirko@qi-hardware.com>
This commit is contained in:
Mirko Lindner 2009-10-30 09:04:39 +01:00
parent a138d4b770
commit 8e8144a4bb

View File

@ -28,6 +28,8 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <fstream>
extern "C" { extern "C" {
#include "gtkhtml/gtkhtml.h" #include "gtkhtml/gtkhtml.h"
} }
@ -44,11 +46,17 @@ zim::File f;
Widget *html; Widget *html;
GtkWidget *html_wg; GtkWidget *html_wg;
// // // misc functions
// // copy article html from while loop into global variable
void set_article(std::string txt) void set_article(std::string txt)
{ {
content = txt; content = txt;
} }
// // // externally called functions
// // display random article
void show_random(){ void show_random(){
while(true){ while(true){
@ -58,73 +66,128 @@ void show_random(){
zim::Article article = m.getArticle(idx); zim::Article article = m.getArticle(idx);
std::string res = article.getPage(false, 10); std::string res = article.getPage(false, 10);
std::cout<<"Diese Zeile läuft so lange bis "<<res.size()<<"."; // std::cout<<"Diese Zeile läuft so lange bis "<<res.size()<<".";
if (res.size() > 0){ if (res.size() > 0){
set_article(res); set_article(res);
break; break;
} }
} }
std::cout<<"nach while "<<content.size()<<"."; // std::cout<<"nach while "<<content.size()<<".";
gtk_html_flush(GTK_HTML(html_wg)); gtk_html_flush(GTK_HTML(html_wg));
gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1); gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1);
// gtk_html_set_editable(GTK_HTML(html_wg),false);
// gtk_html_allow_selection(GTK_HTML(html_wg),true);
// gtk_html_set_magic_links(GTK_HTML(html_wg),true);
// gtk_html_set_caret_mode(GTK_HTML(html_wg),true);
} }
// // // meta functions
// // set displayed html to given url
// // FIXME: returns several articles and displays on one page ... why?
void set_html(const gchar *url){ void set_html(const gchar *url){
// std::cout<<"url"<<url<<".";
// // create ZIM file accessor
zim::File m(file); zim::File m(file);
zim::Search::Results result; // // convert url to string
result.clear();
zim::Search search(m);
std::string term; std::string term;
term.assign( url ); term.assign( url );
search.search(result, term);
std::cout<<"url"<<result.size()<<"."; // // replace '+' signs with spaces in url
if (!result.empty()){ std::replace(term.begin(), term.end(), '+', ' ');
zim::Article article = m.getArticle(result[0].getArticle().getIndex());
// // declare namespace
std::string content = article.getPage(false, 10); char* ns = "A";
// // try to retrieve article
try {
zim::File::const_iterator iterator = m.find(ns[0], zim::QUnicodeString(term));
zim::Article article = m.getArticle(iterator.getIndex());
// // get redirect article if needed
if (article.isRedirect())
{
article = article.getRedirectArticle();
}
// // get content html
content = article.getData().data();
// // emtpy gtkhtml widget
gtk_html_flush(GTK_HTML(html_wg)); 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); gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1);
}catch(...){
} }
} }
void set_berlin(){
// // test functions
void set_berlin(const gchar *url){
// std::cout<<"url"<<url<<"."; // std::cout<<"url"<<url<<".";
zim::File m(file); zim::File z(file);
zim::Search::Results result; // zim::Search::Results result;
result.clear(); // result.clear();
zim::Search search(m); // zim::Search search(m);
std::string term = "Geographische+Koordinaten"; std::string term;
// term.assign( url ); term.assign( url );
search.search(result, term); // search.search(result, term);
if (result.size() > 0){ // if (result.size() > 0){
zim::Article article = m.getArticle(result[0].getArticle().getIndex()); // zim::Article article = m.getArticle(result[0].getArticle().getIndex());
//
std::string content = article.getPage(false, 10); // std::string content = article.getPage(false, 10);
//
gtk_html_flush(GTK_HTML(html_wg)); // gtk_html_flush(GTK_HTML(html_wg));
gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1); // gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1);
// }
std::replace(term.begin(), term.end(), '+', ' ');
char* ns = "A";
try {
zim::File::const_iterator iterator = z.find(ns[0], zim::QUnicodeString(term));
zim::Article article = z.getArticle(iterator.getIndex());
if (article.isRedirect())
{
article = article.getRedirectArticle();
}
content = article.getData().data();
gtk_html_flush(GTK_HTML(html_wg));
gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1);
}catch(...){
} }
// std::cout<<"url"<<<<"."; // std::cout<<content<<".\n";
} }
void articles2file(){
std::ofstream myfile;
myfile.open ("articles.txt");
zim::File f(file);
for (zim::File::const_iterator it = f.begin(); it != f.end(); ++it)
{
myfile << it->getUrl() << '\n';
}
myfile.close();
}
// // // window response functions
// // open requested link
// FIXME show error window when dead-link requested
bool on_link_clicked(GtkHTML *html, const gchar *url) bool on_link_clicked(GtkHTML *html, const gchar *url)
{ {
set_html(url); set_html(url);
// set_berlin(); // set_berlin(url);
return true; return true;
} }
// // main function
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
@ -142,6 +205,8 @@ int main(int argc, char **argv)
html = Glib::wrap(html_wg); html = Glib::wrap(html_wg);
g_signal_connect( G_OBJECT( html_wg ), "link_clicked", G_CALLBACK( on_link_clicked ), NULL ); 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);
ScrolledWindow scrolled_window; ScrolledWindow scrolled_window;
@ -152,6 +217,7 @@ int main(int argc, char **argv)
window.show_all(); window.show_all();
show_random(); show_random();
// set_berlin(); // set_berlin();
Main::run(window); Main::run(window);
return 0; return 0;