Root/
Source at commit f6d6448cfb6aadf808524d49d5f72ca7b26ad531 created 14 years 2 months ago. By Mirko Lindner, make vido work again :) | |
---|---|
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 "message_dialog.hh" |
30 | #include "vido.hh" |
31 | #include "main_window.hh" |
32 | #include "search_dialog.hh" |
33 | |
34 | #include <cxxtools/loginit.h> |
35 | |
36 | log_define("vido.main") |
37 | |
38 | extern "C" { |
39 | #include "gtkhtml/gtkhtml.h" |
40 | } |
41 | |
42 | std::string content; |
43 | std::string fileName; |
44 | |
45 | main_window *window; |
46 | Gtk::Widget *html; |
47 | GtkWidget *html_wg; |
48 | |
49 | // // // misc functions |
50 | |
51 | const zim::File& get_file() |
52 | { |
53 | static zim::File zimFile; |
54 | if (!zimFile.good()) |
55 | { |
56 | log_debug("file not initialized:" << fileName); |
57 | zimFile = zim::File(fileName); |
58 | log_debug("number of articles: " << zimFile.getCountArticles()); |
59 | } |
60 | |
61 | log_debug("returning file."); |
62 | return zimFile; |
63 | } |
64 | |
65 | void show_message(std::string title, std::string txt) |
66 | { |
67 | message_dialog(window, title, txt); |
68 | } |
69 | |
70 | // // copy article html from while loop into global variable |
71 | void set_article(const std::string& txt) |
72 | { |
73 | content = txt; |
74 | } |
75 | |
76 | // // // externally called functions |
77 | |
78 | // // display random article |
79 | void show_random() |
80 | { |
81 | |
82 | log_debug("random called."); |
83 | zim::File m = get_file(); |
84 | |
85 | static unsigned int seed = static_cast<unsigned int>(time(0)); |
86 | zim::size_type idx = static_cast<zim::size_type>(static_cast<double>(m.getCountArticles()) * rand_r(&seed) / RAND_MAX); |
87 | zim::Article article; |
88 | |
89 | article = m.getArticle(idx); |
90 | |
91 | //loop in case article is redirect |
92 | do |
93 | { |
94 | article = article.getRedirectArticle(); |
95 | }while(article.isRedirect()); |
96 | |
97 | std::string res = article.getPage(); |
98 | |
99 | content = article.getPage(); |
100 | log_debug("article size=" << content.size()); |
101 | |
102 | gtk_html_flush(GTK_HTML(html_wg)); |
103 | gtk_html_load_from_string(GTK_HTML(html_wg), res.c_str(), -1); |
104 | } |
105 | |
106 | // // display search dialog |
107 | void search_window(main_window *window_x) |
108 | { |
109 | search_dialog(window_x, " "); |
110 | } |
111 | |
112 | // // // meta functions |
113 | |
114 | // // set displayed html to given url |
115 | // // FIXME: returns several articles and displays on one page ... why? |
116 | void set_html(const gchar *url) |
117 | { |
118 | // TODO unescape url |
119 | |
120 | // // create ZIM file accessor |
121 | zim::File m = get_file(); |
122 | |
123 | // // convert url to string |
124 | std::string term(url); |
125 | |
126 | // // replace '+' signs with spaces in url |
127 | std::replace(term.begin(), term.end(), '+', ' '); |
128 | |
129 | // // find and declare namespace |
130 | size_t found; |
131 | size_t found2; |
132 | char ns; |
133 | |
134 | found=term.find("/"); |
135 | if (found != std::string::npos) |
136 | { |
137 | if (found) |
138 | { |
139 | ns = term[0]; |
140 | term.erase(found + 1); |
141 | } |
142 | else |
143 | { |
144 | term.erase(1); |
145 | ns = term[0]; |
146 | found2 = term.find("/"); |
147 | term.erase(found2 + 1); |
148 | } |
149 | } |
150 | else |
151 | { |
152 | ns = 'A'; |
153 | } |
154 | |
155 | // // try to retrieve article |
156 | try |
157 | { |
158 | #if HAVE_ZIM_QUNICODE_H |
159 | zim::Article article = m.getArticle(ns, zim::QUnicodeString(term)); |
160 | #else |
161 | zim::Article article = m.getArticle(ns, term); |
162 | #endif |
163 | |
164 | if (article.good()) // check if article is really found |
165 | { |
166 | // // get content html |
167 | content = article.getPage(); |
168 | |
169 | // // emtpy gtkhtml widget |
170 | gtk_html_flush(GTK_HTML(html_wg)); |
171 | |
172 | // // load new data into gtkhtml widget |
173 | gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1); |
174 | // window.resize(220, 140); |
175 | log_debug("term=" << term << " url=" << url); |
176 | } |
177 | else |
178 | { |
179 | std::cerr << "article \"" << url << "\" not found" << std::endl; |
180 | } |
181 | } |
182 | catch (const std::exception& e) |
183 | { |
184 | std::cerr << e.what() << std::endl; |
185 | } |
186 | } |
187 | |
188 | |
189 | // // test functions |
190 | void set_search(const gchar *url) |
191 | { |
192 | zim::File z = get_file(); |
193 | |
194 | zim::Search::Results result; |
195 | zim::Search search(z); |
196 | std::string term(url); |
197 | std::replace(term.begin(), term.end(), '+', ' '); |
198 | search.search(result, term); |
199 | if(result.size() == 0) |
200 | { |
201 | show_message("Error", "The article you requested (" + term + ") was not found."); |
202 | } |
203 | else if (result.size() == 1) |
204 | { |
205 | zim::Article article = z.getArticle(result[0].getArticle().getIndex()); |
206 | |
207 | content = article.getPage(false, 10); |
208 | |
209 | gtk_html_flush(GTK_HTML(html_wg)); |
210 | gtk_html_load_from_string(GTK_HTML(html_wg), content.c_str(), -1); |
211 | } |
212 | else |
213 | { |
214 | std::string res; |
215 | for (unsigned i = 0; i < result.size(); ++i) |
216 | { |
217 | #if HAVE_ZIM_QUNICODE_H |
218 | res += "<li><a href=" + result[i].getArticle().getUrl().toXML() + ">" + result[i].getArticle().getTitle().toXML() + "</a></li>"; |
219 | #else |
220 | res += "<li><a href=" + result[i].getArticle().getUrl() + ">" + result[i].getArticle().getTitle() + "</a></li>"; |
221 | #endif |
222 | } |
223 | |
224 | gtk_html_flush(GTK_HTML(html_wg)); |
225 | gtk_html_load_from_string(GTK_HTML(html_wg), res.c_str(), -1); |
226 | // window.resize(220, 140); |
227 | |
228 | } |
229 | |
230 | log_debug(url); |
231 | } |
232 | |
233 | // // // window response functions |
234 | |
235 | // // open requested link |
236 | // FIXME show error window when dead-link requested |
237 | bool on_link_clicked(GtkHTML *html, const gchar *url) |
238 | { |
239 | |
240 | set_html(url); |
241 | // window.resize(220, 140); |
242 | return true; |
243 | } |
244 | |
245 | |
246 | // // main function |
247 | int main(int argc, char **argv) |
248 | { |
249 | try |
250 | { |
251 | log_init(); |
252 | |
253 | if (argc == 2) |
254 | { |
255 | fileName = argv[1]; |
256 | |
257 | log_debug("vido is here file: " << fileName); |
258 | |
259 | Gtk::Main kit(argc, argv); |
260 | |
261 | main_window window; |
262 | window.set_title("Vido"); |
263 | // window.set_border_width(0); |
264 | window.set_default_size(220, 240); |
265 | // window.set_resizable(0); |
266 | |
267 | html_wg = gtk_html_new(); |
268 | |
269 | html = Glib::wrap(html_wg); |
270 | g_signal_connect( G_OBJECT( html_wg ), "link_clicked", G_CALLBACK( on_link_clicked ), NULL ); |
271 | gtk_html_set_caret_mode(GTK_HTML(html_wg),true); |
272 | |
273 | |
274 | Gtk::ScrolledWindow scrolled_window; |
275 | |
276 | scrolled_window.add(*html); |
277 | scrolled_window.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC); |
278 | |
279 | window.add(scrolled_window); |
280 | window.show_all(); |
281 | |
282 | show_random(); |
283 | Gtk::Main::run(window); |
284 | } |
285 | else |
286 | { |
287 | std::cout << "usage: " << argv[0] << " [PATH TO ZIM FILE]" << std::endl; |
288 | } |
289 | |
290 | } |
291 | catch (const std::exception& e) |
292 | { |
293 | std::cerr << e.what() << std::endl; |
294 | return -1; |
295 | } |
296 | |
297 | } |
298 |
Branches:
development
master