add support for newer zim libraries

fix crash when link clicked is not found
This commit is contained in:
Tommi Mäkitalo 2009-12-27 23:37:18 +01:00
parent a3a1b3a3b1
commit ffdd227df7
8 changed files with 172 additions and 199 deletions

View File

@ -1,3 +1 @@
SUBDIRS = src
EXTRA_DIST = AUTHORS TODO README configure

View File

@ -1,6 +1,6 @@
AC_INIT(configure.in)
AM_INIT_AUTOMAKE(vido, 0.1)
AC_CONFIG_HEADER(config.h)
AC_CONFIG_HEADER(src/config.h)
AC_ISC_POSIX
AC_PROG_CC
@ -16,4 +16,7 @@ AC_LANG_CPLUSPLUS
PKG_CHECK_MODULES([GTKMM], [gtkmm-2.4 >= 2.8.0])
PKG_CHECK_MODULES([GTKHTML], [libgtkhtml-3.14 >= 3.28.0])
AC_CHECK_HEADER([zim/qunicode.h],
[AC_DEFINE([HAVE_ZIM_QUNICODE_H], [1], [Defined if header <zim/qunicode.h> is found])])
AC_OUTPUT(Makefile src/Makefile )

View File

@ -1,6 +1,6 @@
bin_PROGRAMS = vido
vido_SOURCES = main_window.cc search_dialog.cc vido.cc vido.cc_bkp
vido_SOURCES = main_window.cc search_dialog.cc vido.cc
noinst_HEADERS = main_window.hh search_dialog.hh

View File

@ -25,46 +25,37 @@
#include "config.h"
#include "main_window.hh"
#include "vido.hh"
#include <cxxtools/log.h>
extern void show_random();
extern void search_window(main_window *window);
log_define("vido.main_window");
main_window::main_window()
{
this->signal_key_press_event().connect(sigc::mem_fun(*this,&main_window::on_my_key_press_event));
this->signal_key_release_event().connect(sigc::mem_fun(*this,&main_window::on_my_key_release_event));
signal_key_press_event().connect(sigc::mem_fun(*this,&main_window::on_my_key_press_event));
signal_key_release_event().connect(sigc::mem_fun(*this,&main_window::on_my_key_release_event));
}
main_window::~main_window()
{ }
bool main_window::actions(int key)
{
static int key_q = 113;
static int key_r = 114;
static int key_s = 115;
// // 115 == s
// // 114 == r
printf("actions key:%i q:%i",int(key), 113);
if (key == int(113))
if (key == key_q)
{
printf("quit requested");
this->quit();
log_debug("quit requested");
quit();
}
else if(key == int(114))
else if(key == key_r)
{
// gtk_html_load_from_string(GTK_HTML(html_wg), "<html><body>me</body></html>", -1);
log_debug("random");
show_random();
printf("random");
}
else if(key == int(115))
else if(key == key_s)
{
log_debug("search");
search_window(this);
printf("search");
}
return true;
@ -73,34 +64,34 @@ bool main_window::actions(int key)
bool main_window::on_my_key_press_event(GdkEventKey *Key)
{
// printf("ctrl set value nows: %i\n", *ctrl_state);
if (Key->keyval == gdk_keyval_from_name("Control_L")){
if (Key->keyval == gdk_keyval_from_name("Control_L"))
{
ctrl_state = 1;
// printf("ctrl set value nows: %i\n", ctrl_state);
}else{
if (ctrl_state == 1){
// printf("key num: %i", Key->keyval);
this->actions(Key->keyval);
// printf("\n Key Presssed is %s with ctrl held\n",gdk_keyval_name(Key->keyval));
}
else
{
if (ctrl_state == 1)
{
actions(Key->keyval);
}
}
printf("\n Key Presssed is %s\n",gdk_keyval_name(Key->keyval));
log_debug("Key Presssed is " << gdk_keyval_name(Key->keyval));
return true;
}
bool main_window::on_my_key_release_event(GdkEventKey *Key)
{
// printf("ctrl set value nows: %i\n", *ctrl_state);
if (Key->keyval == gdk_keyval_from_name("Control_L")){
if (Key->keyval == gdk_keyval_from_name("Control_L"))
{
ctrl_state = 0;
// printf("ctrl set value nows: %i\n", ctrl_state);
}
// printf("\n Key released is %s\n",gdk_keyval_name(Key->keyval));
return true;
}
bool main_window::quit()
{
hide();
return 0;
return false;
}

View File

@ -18,25 +18,22 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef MAIN_WINDOW_HH
#define MAIN_WINDOW_HH
#include <gtkmm.h>
#include <iostream>
#include <stdio.h>
#include <gdkmm/gc.h>
#include <gdk/gdkkeysyms.h>
#include <gtkmm/window.h>
class main_window : public Gtk::Window
{
public:
main_window();
~main_window();
bool actions(int);
int ctrl_state;
private:
bool actions(int);
int ctrl_state;
bool on_my_key_press_event(GdkEventKey *event);
bool on_my_key_release_event(GdkEventKey *event);
bool quit();
};
#endif // MAIN_WINDOW_HH

View File

@ -20,12 +20,11 @@
#include "config.h"
#include "search_dialog.hh"
#include "vido.hh"
#include <gtkmm.h>
extern void set_html(const gchar *url);
extern void set_search(const gchar *url);
search_dialog::search_dialog(main_window* parent, Glib::ustring input="")
search_dialog::search_dialog(main_window* parent, const Glib::ustring& input)
{
Gtk::Dialog dialog("Search", parent, true);
@ -39,13 +38,9 @@ search_dialog::search_dialog(main_window* parent, Glib::ustring input="")
dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
dialog.show_all();
int result = dialog.run();
if (result == Gtk::RESPONSE_OK) {
if (result == Gtk::RESPONSE_OK)
{
set_search(entry.get_text().c_str());
} else {
}
}
search_dialog::~search_dialog()
{ }

View File

@ -18,17 +18,17 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef SEARCH_DIALOG_HH
#define SEARCH_DIALOG_HH
#include <gtkmm/dialog.h>
// #include <gtkmm.h>
#include <gtkmm/window.h>
#include <main_window.hh>
extern main_window parent;
class search_dialog : public Gtk::Dialog{
class main_window;
class search_dialog : public Gtk::Dialog
{
public:
search_dialog(main_window* parent, Glib::ustring);
~search_dialog();
search_dialog(main_window* parent, const Glib::ustring&);
};
};
#endif // SEARCH_DIALOG_HH

View File

@ -21,54 +21,47 @@
#include <gtkmm.h>
#include <zim/file.h>
#include <zim/fileiterator.h>
#include <zim/search.h>
#include <zim/articlesearch.h>
#include <iostream>
#include <string>
#include <fstream>
// #include "main_window.hh"
#include "config.h"
#include "vido.hh"
#include "main_window.hh"
#include "search_dialog.hh"
#include <cxxtools/loginit.h>
log_define("vido.main")
extern "C" {
#include "gtkhtml/gtkhtml.h"
}
using namespace Gtk;
Glib::RefPtr<Gtk::TextBuffer> buffer;
unsigned int seed = static_cast<unsigned int>(time(0));
zim::Search::Results result;
std::string res;
std::string content;
std::string file;
zim::File f;
Window *window;
std::string fileName;
Widget *html;
Gtk::Widget *html;
GtkWidget *html_wg;
static zim::File zimFile;
static bool initialized = false;
// // // misc functions
const zim::File get_file()
const zim::File& get_file()
{
if (!initialized){
std::cout<<"file not initialized :"<<file<<".\n";
zim::File zimFile(file);
initialized = true;
}else{
std::cout<<"file initialized :"<<file<<".\n";
static zim::File zimFile;
if (!zimFile.good())
{
log_debug("file not initialized:" << fileName);
zimFile = zim::File(fileName);
log_debug("number of articles: " << zimFile.getCountArticles());
}
std::cout<<"returning file"<<".\n";
log_debug("returning file.");
return zimFile;
}
// // copy article html from while loop into global variable
void set_article(std::string txt)
void set_article(const std::string& txt)
{
content = txt;
}
@ -76,47 +69,52 @@ void set_article(std::string txt)
// // // externally called functions
// // display random article
void show_random(){
void show_random()
{
std::cout<<"random called"<<".\n";
log_debug("random called.");
zim::File m = get_file();
std::cout<<"random got file"<<".\n";
while(true){
std::cout<<"random running loop"<<".\n";
static unsigned int seed = static_cast<unsigned int>(time(0));
zim::Article article;
do
{
log_debug("random running loop");
zim::size_type idx = static_cast<zim::size_type>(static_cast<double>(m.getCountArticles()) * rand_r(&seed) / RAND_MAX);
zim::Article article = m.getArticle(idx);
log_debug("random index " << idx << " number of articles: " << m.getCountArticles());
article = m.getArticle(idx);
} while(article.isRedirect());
content = article.getPage();
log_debug("article size=" << content.size());
std::string res = article.getPage(false, 10);
if (res.size() > 0){
set_article(res);
break;
}
}
gtk_html_flush(GTK_HTML(html_wg));
gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1);
}
// // display search dialog
void search_window(main_window *window_x){
void search_window(main_window *window_x)
{
search_dialog(window_x, " ");
}
// // // 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)
{
// TODO unescape url
// // create ZIM file accessor
zim::File m = get_file();
// // convert url to string
std::string term;
term.assign( url );
std::string term(url);
// // replace '+' signs with spaces in url
std::replace(term.begin(), term.end(), '+', ' ');
@ -124,121 +122,104 @@ void set_html(const gchar *url){
// // find and declare namespace
size_t found;
size_t found2;
char *ns;
char ns;
found=term.find("/");
if (found!=std::string::npos){
if (found != (int) 0){
ns = &term[0];
term = term.substr(found + 1, strlen(url));
}else{
term = term.substr(1, strlen(url));
ns = &term[0];
found2 = term.find("/");
term = term.substr(found2 + 1, strlen(url));
if (found != std::string::npos)
{
if (found)
{
ns = term[0];
term.erase(found + 1);
}
}else{
ns = "A";
else
{
term.erase(1);
ns = term[0];
found2 = term.find("/");
term.erase(found2 + 1);
}
}
else
{
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());
try
{
#if HAVE_ZIM_QUNICODE_H
zim::Article article = m.getArticle(ns, zim::QUnicodeString(term));
#else
zim::Article article = m.getArticle(ns, term);
#endif
// // get redirect article if needed
if (article.isRedirect())
if (article.good()) // check if article is really found
{
article = article.getRedirectArticle();
}
// // get content html
content = article.getData().data();
// // get article length
int length = article.getArticleSize();
// // limit html output to article length
std::string str2 = content.substr (0,length);
content = article.getPage();
// // 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), str2.c_str(), -1);
std::cout<<"term:"<<term<<".\n";
std::cout<<"url:"<<url<<".\n";
}catch(...){
gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1);
log_debug("term=" << term << " url=" << url);
}
else
{
std::cerr << "article \"" << url << "\" not found" << std::endl;
}
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
// // test functions
void set_search(const gchar *url){
// std::cout<<"url"<<url<<".";
void set_search(const gchar *url)
{
zim::File z = get_file();
zim::Search::Results result;
result.clear();
zim::Search search(z);
std::string term;
term.assign( url );
std::string term(url);
std::replace(term.begin(), term.end(), '+', ' ');
search.search(result, term);
if(result.size() == 0) {
if(result.size() == 0)
{
}
else if (result.size() == 1){
else if (result.size() == 1)
{
zim::Article article = z.getArticle(result[0].getArticle().getIndex());
std::string content = article.getPage(false, 10);
content = article.getPage(false, 10);
gtk_html_flush(GTK_HTML(html_wg));
gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1);
}else{
}
else
{
std::string res;
for (unsigned i = 0; i < result.size(); ++i) {
for (unsigned i = 0; i < result.size(); ++i)
{
#if HAVE_ZIM_QUNICODE_H
res += "<li><a href=" + result[i].getArticle().getUrl().toXML() + ">" + result[i].getArticle().getTitle().toXML() + "</a></li>";
#else
res += "<li><a href=" + result[i].getArticle().getUrl() + ">" + result[i].getArticle().getTitle() + "</a></li>";
#endif
}
gtk_html_flush(GTK_HTML(html_wg));
gtk_html_load_from_string(GTK_HTML(html_wg), res.c_str(), -1);
}
// 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<<".\n";
log_debug(url);
}
void articles2file(){
std::ofstream myfile;
myfile.open ("articles.txt");
zim::File f = get_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
@ -255,13 +236,17 @@ bool on_link_clicked(GtkHTML *html, const gchar *url)
// // main function
int main(int argc, char **argv)
{
try
{
log_init();
if (argc == 2)
{
file = argv[1];
fileName = argv[1];
std::cout<<"vido is here file:"<<file<<".\n";
log_debug("vido is here file: " << fileName);
Main kit(argc, argv);
Gtk::Main kit(argc, argv);
main_window window;
window.set_title("Vido");
@ -276,23 +261,27 @@ int main(int argc, char **argv)
gtk_html_set_caret_mode(GTK_HTML(html_wg),true);
ScrolledWindow scrolled_window;
Gtk::ScrolledWindow scrolled_window;
scrolled_window.add(*html);
scrolled_window.set_policy(POLICY_NEVER, POLICY_AUTOMATIC);
scrolled_window.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
window.add(scrolled_window);
window.show_all();
show_random();
// articles2file();
// set_berlin();
Main::run(window);
}else{
std::cout<<"You provided too many or too few arguments("<< argc <<").\n";
std::cout<<"Vido is to be used as follows:"<<".\n";
std::cout<<"vido [PATH TO ZIM FILE]"<<".\n";
Gtk::Main::run(window);
}
return 0;
else
{
std::cout << "usage: " << argv[0] << " [PATH TO ZIM FILE]" << std::endl;
}
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
return -1;
}
}