Date:2009-12-27 23:37:18 (10 years 7 months ago)
Author:Tommi Mäkitalo
Commit:ffdd227df73e44a9e40489b666e1d5d783924ab1
Message:add support for newer zim libraries fix crash when link clicked is not found

Files: Makefile.am (1 diff)
configure.in (2 diffs)
src/Makefile.am (1 diff)
src/main_window.cc (2 diffs)
src/main_window.hh (1 diff)
src/search_dialog.cc (2 diffs)
src/search_dialog.hh (1 diff)
src/vido.cc (5 diffs)

Change Details

Makefile.am
11SUBDIRS = src
2
3EXTRA_DIST = AUTHORS TODO README configure
configure.in
11AC_INIT(configure.in)
22AM_INIT_AUTOMAKE(vido, 0.1)
3AC_CONFIG_HEADER(config.h)
3AC_CONFIG_HEADER(src/config.h)
44
55AC_ISC_POSIX
66AC_PROG_CC
...... 
1616PKG_CHECK_MODULES([GTKMM], [gtkmm-2.4 >= 2.8.0])
1717PKG_CHECK_MODULES([GTKHTML], [libgtkhtml-3.14 >= 3.28.0])
1818
19AC_CHECK_HEADER([zim/qunicode.h],
20    [AC_DEFINE([HAVE_ZIM_QUNICODE_H], [1], [Defined if header <zim/qunicode.h> is found])])
21
1922AC_OUTPUT(Makefile src/Makefile )
src/Makefile.am
11bin_PROGRAMS = vido
22
3vido_SOURCES = main_window.cc search_dialog.cc vido.cc vido.cc_bkp
3vido_SOURCES = main_window.cc search_dialog.cc vido.cc
44
55noinst_HEADERS = main_window.hh search_dialog.hh
66
src/main_window.cc
2525
2626#include "config.h"
2727#include "main_window.hh"
28#include "vido.hh"
29#include <cxxtools/log.h>
2830
29
30extern void show_random();
31extern void search_window(main_window *window);
31log_define("vido.main_window");
3232
3333main_window::main_window()
3434{
35
36    this->signal_key_press_event().connect(sigc::mem_fun(*this,&main_window::on_my_key_press_event));
37
38    this->signal_key_release_event().connect(sigc::mem_fun(*this,&main_window::on_my_key_release_event));
35    signal_key_press_event().connect(sigc::mem_fun(*this,&main_window::on_my_key_press_event));
36    signal_key_release_event().connect(sigc::mem_fun(*this,&main_window::on_my_key_release_event));
3937}
4038
41 main_window::~main_window()
42{ }
43
4439bool main_window::actions(int key)
4540{
41    static int key_q = 113;
42    static int key_r = 114;
43    static int key_s = 115;
4644
47// // 115 == s
48// // 114 == r
49
50    printf("actions key:%i q:%i",int(key), 113);
51    if (key == int(113))
45    if (key == key_q)
5246    {
53        printf("quit requested");
54        this->quit();
55
47        log_debug("quit requested");
48        quit();
5649    }
57    else if(key == int(114))
50    else if(key == key_r)
5851    {
59// gtk_html_load_from_string(GTK_HTML(html_wg), "<html><body>me</body></html>", -1);
60
52        log_debug("random");
6153        show_random();
62        printf("random");
6354    }
64    else if(key == int(115))
55    else if(key == key_s)
6556    {
57        log_debug("search");
6658        search_window(this);
67        printf("search");
6859    }
6960
7061    return true;
...... 
7364
7465bool main_window::on_my_key_press_event(GdkEventKey *Key)
7566{
76// printf("ctrl set value nows: %i\n", *ctrl_state);
77    if (Key->keyval == gdk_keyval_from_name("Control_L")){
67    if (Key->keyval == gdk_keyval_from_name("Control_L"))
68    {
7869        ctrl_state = 1;
79// printf("ctrl set value nows: %i\n", ctrl_state);
80    }else{
81      if (ctrl_state == 1){
82// printf("key num: %i", Key->keyval);
83          this->actions(Key->keyval);
84// printf("\n Key Presssed is %s with ctrl held\n",gdk_keyval_name(Key->keyval));
70    }
71    else
72    {
73      if (ctrl_state == 1)
74      {
75          actions(Key->keyval);
8576      }
8677    }
87    printf("\n Key Presssed is %s\n",gdk_keyval_name(Key->keyval));
78
79    log_debug("Key Presssed is " << gdk_keyval_name(Key->keyval));
8880    return true;
8981}
9082
9183bool main_window::on_my_key_release_event(GdkEventKey *Key)
9284{
93// printf("ctrl set value nows: %i\n", *ctrl_state);
94    if (Key->keyval == gdk_keyval_from_name("Control_L")){
85    if (Key->keyval == gdk_keyval_from_name("Control_L"))
86    {
9587        ctrl_state = 0;
96// printf("ctrl set value nows: %i\n", ctrl_state);
9788    }
98// printf("\n Key released is %s\n",gdk_keyval_name(Key->keyval));
89
9990    return true;
10091}
10192
10293bool main_window::quit()
10394{
10495    hide();
105    return 0;
96    return false;
10697}
src/main_window.hh
1818 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
1919 ***************************************************************************/
2020
21#ifndef MAIN_WINDOW_HH
22#define MAIN_WINDOW_HH
2123
22#include <gtkmm.h>
23#include <iostream>
24#include <stdio.h>
25#include <gdkmm/gc.h>
26#include <gdk/gdkkeysyms.h>
2724#include <gtkmm/window.h>
2825
2926class main_window : public Gtk::Window
3027{
3128    public:
3229      main_window();
33      ~main_window();
34      bool actions(int);
35      int ctrl_state;
3630
3731    private:
32      bool actions(int);
33      int ctrl_state;
3834      bool on_my_key_press_event(GdkEventKey *event);
3935      bool on_my_key_release_event(GdkEventKey *event);
4036      bool quit();
4137};
4238
39#endif // MAIN_WINDOW_HH
src/search_dialog.cc
2020
2121#include "config.h"
2222#include "search_dialog.hh"
23#include "vido.hh"
2324#include <gtkmm.h>
2425
25extern void set_html(const gchar *url);
26extern void set_search(const gchar *url);
2726
28search_dialog::search_dialog(main_window* parent, Glib::ustring input="")
27search_dialog::search_dialog(main_window* parent, const Glib::ustring& input)
2928{
3029    Gtk::Dialog dialog("Search", parent, true);
3130
...... 
3938    dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
4039    dialog.show_all();
4140    int result = dialog.run();
42    if (result == Gtk::RESPONSE_OK) {
41    if (result == Gtk::RESPONSE_OK)
42    {
4343             set_search(entry.get_text().c_str());
44    } else {
45
4644    }
4745
4846}
49
50 search_dialog::~search_dialog()
51{ }
src/search_dialog.hh
1818 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
1919 ***************************************************************************/
2020
21#include <gtkmm/dialog.h>
22// #include <gtkmm.h>
23#include <gtkmm/window.h>
24#include <main_window.hh>
21#ifndef SEARCH_DIALOG_HH
22#define SEARCH_DIALOG_HH
2523
26extern main_window parent;
24#include <gtkmm/dialog.h>
2725
28class search_dialog : public Gtk::Dialog{
26class main_window;
2927
28class search_dialog : public Gtk::Dialog
29{
3030    public:
31      search_dialog(main_window* parent, Glib::ustring);
32      ~search_dialog();
31      search_dialog(main_window* parent, const Glib::ustring&);
32};
3333
34};
34#endif // SEARCH_DIALOG_HH
src/vido.cc
2121
2222#include <gtkmm.h>
2323#include <zim/file.h>
24#include <zim/fileiterator.h>
2524#include <zim/search.h>
26#include <zim/articlesearch.h>
2725#include <iostream>
2826#include <string>
2927
30#include <fstream>
31
32// #include "main_window.hh"
28#include "config.h"
29#include "vido.hh"
30#include "main_window.hh"
3331#include "search_dialog.hh"
3432
33#include <cxxtools/loginit.h>
34
35log_define("vido.main")
36
3537extern "C" {
3638   #include "gtkhtml/gtkhtml.h"
3739 }
3840
39using namespace Gtk;
40Glib::RefPtr<Gtk::TextBuffer> buffer;
41unsigned int seed = static_cast<unsigned int>(time(0));
42zim::Search::Results result;
43std::string res;
4441std::string content;
45std::string file;
46zim::File f;
47Window *window;
42std::string fileName;
4843
49Widget *html;
44Gtk::Widget *html;
5045GtkWidget *html_wg;
5146
52static zim::File zimFile;
53static bool initialized = false;
54
5547// // // misc functions
5648
57const zim::File get_file()
49const zim::File& get_file()
5850{
59  if (!initialized){
60    std::cout<<"file not initialized :"<<file<<".\n";
61    zim::File zimFile(file);
62    initialized = true;
63  }else{
64    std::cout<<"file initialized :"<<file<<".\n";
51  static zim::File zimFile;
52  if (!zimFile.good())
53  {
54    log_debug("file not initialized:" << fileName);
55    zimFile = zim::File(fileName);
56    log_debug("number of articles: " << zimFile.getCountArticles());
6557  }
66  std::cout<<"returning file"<<".\n";
58
59  log_debug("returning file.");
6760  return zimFile;
6861}
6962
7063// // copy article html from while loop into global variable
71void set_article(std::string txt)
64void set_article(const std::string& txt)
7265{
7366    content = txt;
7467}
...... 
7669// // // externally called functions
7770
7871// // display random article
79void show_random(){
72void show_random()
73{
8074
81    std::cout<<"random called"<<".\n";
75    log_debug("random called.");
8276    zim::File m = get_file();
83    std::cout<<"random got file"<<".\n";
8477
85    while(true){
86        std::cout<<"random running loop"<<".\n";
78    static unsigned int seed = static_cast<unsigned int>(time(0));
79
80    zim::Article article;
81    do
82    {
83        log_debug("random running loop");
8784        zim::size_type idx = static_cast<zim::size_type>(static_cast<double>(m.getCountArticles()) * rand_r(&seed) / RAND_MAX);
8885
89        zim::Article article = m.getArticle(idx);
86        log_debug("random index " << idx << " number of articles: " << m.getCountArticles());
87
88        article = m.getArticle(idx);
89
90    } while(article.isRedirect());
91
92    content = article.getPage();
93    log_debug("article size=" << content.size());
9094
91        std::string res = article.getPage(false, 10);
92        if (res.size() > 0){
93            set_article(res);
94            break;
95        }
96    }
9795    gtk_html_flush(GTK_HTML(html_wg));
9896    gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1);
9997}
10098
10199// // display search dialog
102void search_window(main_window *window_x){
103
100void search_window(main_window *window_x)
101{
104102    search_dialog(window_x, " ");
105
106103}
107104
108105// // // meta functions
109106
110107// // set displayed html to given url
111108// // FIXME: returns several articles and displays on one page ... why?
112void set_html(const gchar *url){
109void set_html(const gchar *url)
110{
111    // TODO unescape url
113112
114113// // create ZIM file accessor
115114    zim::File m = get_file();
116115
117116// // convert url to string
118    std::string term;
119    term.assign( url );
117    std::string term(url);
120118
121119// // replace '+' signs with spaces in url
122120    std::replace(term.begin(), term.end(), '+', ' ');
...... 
124122// // find and declare namespace
125123    size_t found;
126124    size_t found2;
127    char *ns;
125    char ns;
128126
129127    found=term.find("/");
130    if (found!=std::string::npos){
131        if (found != (int) 0){
132            ns = &term[0];
133            term = term.substr(found + 1, strlen(url));
134        }else{
135            term = term.substr(1, strlen(url));
136            ns = &term[0];
128    if (found != std::string::npos)
129    {
130        if (found)
131        {
132            ns = term[0];
133            term.erase(found + 1);
134        }
135        else
136        {
137            term.erase(1);
138            ns = term[0];
137139            found2 = term.find("/");
138            term = term.substr(found2 + 1, strlen(url));
140            term.erase(found2 + 1);
139141        }
140    }else{
141        ns = "A";
142    }
143    else
144    {
145        ns = 'A';
142146    }
143147
144148// // try to retrieve article
145    try {
146        zim::File::const_iterator iterator = m.find(ns[0], zim::QUnicodeString(term));
147        zim::Article article = m.getArticle(iterator.getIndex());
149    try
150    {
151#if HAVE_ZIM_QUNICODE_H
152        zim::Article article = m.getArticle(ns, zim::QUnicodeString(term));
153#else
154        zim::Article article = m.getArticle(ns, term);
155#endif
148156
149// // get redirect article if needed
150        if (article.isRedirect())
157        if (article.good()) // check if article is really found
151158        {
152            article = article.getRedirectArticle();
153        }
154
155159// // get content html
156        content = article.getData().data();
157
158// // get article length
159        int length = article.getArticleSize();
160
161// // limit html output to article length
162        std::string str2 = content.substr (0,length);
160            content = article.getPage();
163161
164162// // emtpy gtkhtml widget
165        gtk_html_flush(GTK_HTML(html_wg));
163            gtk_html_flush(GTK_HTML(html_wg));
166164
167165// // load new data into gtkhtml widget
168        gtk_html_load_from_string(GTK_HTML(html_wg), str2.c_str(), -1);
169        std::cout<<"term:"<<term<<".\n";
170        std::cout<<"url:"<<url<<".\n";
171    }catch(...){
166            gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1);
167            log_debug("term=" << term << " url=" << url);
168        }
169        else
170        {
171            std::cerr << "article \"" << url << "\" not found" << std::endl;
172        }
173    }
174    catch (const std::exception& e)
175    {
176        std::cerr << e.what() << std::endl;
172177    }
173178}
174179
175180
176181// // test functions
177void set_search(const gchar *url){
178// std::cout<<"url"<<url<<".";
182void set_search(const gchar *url)
183{
179184    zim::File z = get_file();
180185
181186    zim::Search::Results result;
182    result.clear();
183187    zim::Search search(z);
184    std::string term;
185    term.assign( url );
188    std::string term(url);
186189    std::replace(term.begin(), term.end(), '+', ' ');
187190    search.search(result, term);
188    if(result.size() == 0) {
189
191    if(result.size() == 0)
192    {
190193    }
191    else if (result.size() == 1){
192
194    else if (result.size() == 1)
195    {
193196        zim::Article article = z.getArticle(result[0].getArticle().getIndex());
194197
195        std::string content = article.getPage(false, 10);
198        content = article.getPage(false, 10);
196199
197200        gtk_html_flush(GTK_HTML(html_wg));
198201        gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1);
199    }else{
202    }
203    else
204    {
200205        std::string res;
201        for (unsigned i = 0; i < result.size(); ++i) {
206        for (unsigned i = 0; i < result.size(); ++i)
207        {
208#if HAVE_ZIM_QUNICODE_H
202209          res += "<li><a href=" + result[i].getArticle().getUrl().toXML() + ">" + result[i].getArticle().getTitle().toXML() + "</a></li>";
210#else
211          res += "<li><a href=" + result[i].getArticle().getUrl() + ">" + result[i].getArticle().getTitle() + "</a></li>";
212#endif
203213        }
204214
205215        gtk_html_flush(GTK_HTML(html_wg));
206216        gtk_html_load_from_string(GTK_HTML(html_wg), res.c_str(), -1);
207217
208218    }
209// char* ns = "A";
210
211// try {
212// zim::File::const_iterator iterator = z.find(ns[0], zim::QUnicodeString(term));
213// zim::Article article = z.getArticle(iterator.getIndex());
214// if (article.isRedirect())
215// {
216// article = article.getRedirectArticle();
217// }
218// content = article.getData().data();
219// gtk_html_flush(GTK_HTML(html_wg));
220// gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1);
221// }catch(...){
222// }
223
224    std::cout<<url<<".\n";
225}
226
227void articles2file(){
228
229    std::ofstream myfile;
230    myfile.open ("articles.txt");
231    zim::File f = get_file();
232    for (zim::File::const_iterator it = f.begin(); it != f.end(); ++it)
233        {
234            myfile << it->getUrl() << '\n';
235        }
236    myfile.close();
237
238    }
239
240219
220    log_debug(url);
221}
241222
242223// // // window response functions
243224
...... 
255236// // main function
256237int main(int argc, char **argv)
257238{
239  try
240  {
241    log_init();
242
258243    if (argc == 2)
259244    {
260      file = argv[1];
245      fileName = argv[1];
261246
262      std::cout<<"vido is here file:"<<file<<".\n";
247      log_debug("vido is here file: " << fileName);
263248
264      Main kit(argc, argv);
249      Gtk::Main kit(argc, argv);
265250
266251      main_window window;
267252      window.set_title("Vido");
...... 
276261      gtk_html_set_caret_mode(GTK_HTML(html_wg),true);
277262
278263
279      ScrolledWindow scrolled_window;
264      Gtk::ScrolledWindow scrolled_window;
280265
281266      scrolled_window.add(*html);
282      scrolled_window.set_policy(POLICY_NEVER, POLICY_AUTOMATIC);
267      scrolled_window.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
283268
284269      window.add(scrolled_window);
285270      window.show_all();
286271
287272      show_random();
288  // articles2file();
289  // set_berlin();
290      Main::run(window);
291    }else{
292      std::cout<<"You provided too many or too few arguments("<< argc <<").\n";
293      std::cout<<"Vido is to be used as follows:"<<".\n";
294      std::cout<<"vido [PATH TO ZIM FILE]"<<".\n";
273      Gtk::Main::run(window);
274    }
275    else
276    {
277      std::cout << "usage: " << argv[0] << " [PATH TO ZIM FILE]" << std::endl;
295278    }
296    return 0;
279
280  }
281  catch (const std::exception& e)
282  {
283    std::cerr << e.what() << std::endl;
284    return -1;
285  }
297286
298287}

Archive Download the corresponding diff file

Branches:
development
master



interactive