Root/
Source at commit ffdd227df73e44a9e40489b666e1d5d783924ab1 created 14 years 3 months ago. By Tommi Mäkitalo, add support for newer zim libraries fix crash when link clicked is not found | |
---|---|
1 | /*************************************************************************** |
2 | * Copyright (C) 2009 by Mirko Lindner,,, * |
3 | * vegyraupe@mira * |
4 | * * |
5 | * This program is free software; you can redistribute it and/or modify * |
6 | * it under the terms of the GNU General Public License as published by * |
7 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * (at your option) any later version. * |
9 | * * |
10 | * This program is distributed in the hope that it will be useful, * |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * GNU General Public License for more details. * |
14 | * * |
15 | * You should have received a copy of the GNU General Public License * |
16 | * along with this program; if not, write to the * |
17 | * Free Software Foundation, Inc., * |
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | ***************************************************************************/ |
20 | |
21 | |
22 | #include <gtkmm.h> |
23 | #include <zim/file.h> |
24 | #include <zim/search.h> |
25 | #include <iostream> |
26 | #include <string> |
27 | |
28 | #include "config.h" |
29 | #include "vido.hh" |
30 | #include "main_window.hh" |
31 | #include "search_dialog.hh" |
32 | |
33 | #include <cxxtools/loginit.h> |
34 | |
35 | log_define("vido.main") |
36 | |
37 | extern "C" { |
38 | #include "gtkhtml/gtkhtml.h" |
39 | } |
40 | |
41 | std::string content; |
42 | std::string fileName; |
43 | |
44 | Gtk::Widget *html; |
45 | GtkWidget *html_wg; |
46 | |
47 | // // // misc functions |
48 | |
49 | const zim::File& get_file() |
50 | { |
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()); |
57 | } |
58 | |
59 | log_debug("returning file."); |
60 | return zimFile; |
61 | } |
62 | |
63 | // // copy article html from while loop into global variable |
64 | void set_article(const std::string& txt) |
65 | { |
66 | content = txt; |
67 | } |
68 | |
69 | // // // externally called functions |
70 | |
71 | // // display random article |
72 | void show_random() |
73 | { |
74 | |
75 | log_debug("random called."); |
76 | zim::File m = get_file(); |
77 | |
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"); |
84 | zim::size_type idx = static_cast<zim::size_type>(static_cast<double>(m.getCountArticles()) * rand_r(&seed) / RAND_MAX); |
85 | |
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()); |
94 | |
95 | gtk_html_flush(GTK_HTML(html_wg)); |
96 | gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1); |
97 | } |
98 | |
99 | // // display search dialog |
100 | void search_window(main_window *window_x) |
101 | { |
102 | search_dialog(window_x, " "); |
103 | } |
104 | |
105 | // // // meta functions |
106 | |
107 | // // set displayed html to given url |
108 | // // FIXME: returns several articles and displays on one page ... why? |
109 | void set_html(const gchar *url) |
110 | { |
111 | // TODO unescape url |
112 | |
113 | // // create ZIM file accessor |
114 | zim::File m = get_file(); |
115 | |
116 | // // convert url to string |
117 | std::string term(url); |
118 | |
119 | // // replace '+' signs with spaces in url |
120 | std::replace(term.begin(), term.end(), '+', ' '); |
121 | |
122 | // // find and declare namespace |
123 | size_t found; |
124 | size_t found2; |
125 | char ns; |
126 | |
127 | found=term.find("/"); |
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]; |
139 | found2 = term.find("/"); |
140 | term.erase(found2 + 1); |
141 | } |
142 | } |
143 | else |
144 | { |
145 | ns = 'A'; |
146 | } |
147 | |
148 | // // try to retrieve article |
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 |
156 | |
157 | if (article.good()) // check if article is really found |
158 | { |
159 | // // get content html |
160 | content = article.getPage(); |
161 | |
162 | // // emtpy gtkhtml widget |
163 | gtk_html_flush(GTK_HTML(html_wg)); |
164 | |
165 | // // load new data into gtkhtml widget |
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; |
177 | } |
178 | } |
179 | |
180 | |
181 | // // test functions |
182 | void set_search(const gchar *url) |
183 | { |
184 | zim::File z = get_file(); |
185 | |
186 | zim::Search::Results result; |
187 | zim::Search search(z); |
188 | std::string term(url); |
189 | std::replace(term.begin(), term.end(), '+', ' '); |
190 | search.search(result, term); |
191 | if(result.size() == 0) |
192 | { |
193 | } |
194 | else if (result.size() == 1) |
195 | { |
196 | zim::Article article = z.getArticle(result[0].getArticle().getIndex()); |
197 | |
198 | content = article.getPage(false, 10); |
199 | |
200 | gtk_html_flush(GTK_HTML(html_wg)); |
201 | gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1); |
202 | } |
203 | else |
204 | { |
205 | std::string res; |
206 | for (unsigned i = 0; i < result.size(); ++i) |
207 | { |
208 | #if HAVE_ZIM_QUNICODE_H |
209 | 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 |
213 | } |
214 | |
215 | gtk_html_flush(GTK_HTML(html_wg)); |
216 | gtk_html_load_from_string(GTK_HTML(html_wg), res.c_str(), -1); |
217 | |
218 | } |
219 | |
220 | log_debug(url); |
221 | } |
222 | |
223 | // // // window response functions |
224 | |
225 | // // open requested link |
226 | // FIXME show error window when dead-link requested |
227 | bool on_link_clicked(GtkHTML *html, const gchar *url) |
228 | { |
229 | |
230 | set_html(url); |
231 | // set_berlin(url); |
232 | return true; |
233 | } |
234 | |
235 | |
236 | // // main function |
237 | int main(int argc, char **argv) |
238 | { |
239 | try |
240 | { |
241 | log_init(); |
242 | |
243 | if (argc == 2) |
244 | { |
245 | fileName = argv[1]; |
246 | |
247 | log_debug("vido is here file: " << fileName); |
248 | |
249 | Gtk::Main kit(argc, argv); |
250 | |
251 | main_window window; |
252 | window.set_title("Vido"); |
253 | // window.set_border_width(0); |
254 | window.set_default_size(320, 240); |
255 | // window.set_resizable(0); |
256 | |
257 | html_wg = gtk_html_new(); |
258 | |
259 | html = Glib::wrap(html_wg); |
260 | g_signal_connect( G_OBJECT( html_wg ), "link_clicked", G_CALLBACK( on_link_clicked ), NULL ); |
261 | gtk_html_set_caret_mode(GTK_HTML(html_wg),true); |
262 | |
263 | |
264 | Gtk::ScrolledWindow scrolled_window; |
265 | |
266 | scrolled_window.add(*html); |
267 | scrolled_window.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC); |
268 | |
269 | window.add(scrolled_window); |
270 | window.show_all(); |
271 | |
272 | show_random(); |
273 | Gtk::Main::run(window); |
274 | } |
275 | else |
276 | { |
277 | std::cout << "usage: " << argv[0] << " [PATH TO ZIM FILE]" << std::endl; |
278 | } |
279 | |
280 | } |
281 | catch (const std::exception& e) |
282 | { |
283 | std::cerr << e.what() << std::endl; |
284 | return -1; |
285 | } |
286 | |
287 | } |
288 |
Branches:
development
master