Root/
Source at commit 68c4971a2eea0a0102fe78fb7b75e68698358996 created 13 years 11 months ago. By Mirko Lindner, initial upload | |
---|---|
1 | /*************************************************************************** |
2 | * Copyright (C) 2010 by Mirko Lindner * |
3 | * mirko@sharism.cc * |
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 | // #include <iostream> |
22 | #include <QtGui> |
23 | #include <QMessageBox> |
24 | #include <QTextBrowser> |
25 | #include <string> |
26 | #include <sstream> |
27 | // #include <vector> |
28 | #include "qmain.h" |
29 | #include <zim/file.h> |
30 | #include <zim/search.h> |
31 | #include <zim/fileimpl.h> |
32 | |
33 | #include <cxxtools/log.h> |
34 | |
35 | log_define("qvido.htmlarea"); |
36 | |
37 | /////////////////////////////////////////// |
38 | // Functions related to htmlarea |
39 | void HtmlArea::linking( const QUrl &txt ){ |
40 | QString str = txt.toString(); |
41 | |
42 | // // check if url is an internal anchor if yes jump if not end request and query zimlib |
43 | if (str.startsWith("#") == 1){ |
44 | this->scrollToAnchor(str); |
45 | }else{ |
46 | this->setSource(this->source()); |
47 | this->setNewContent(1, str); |
48 | } |
49 | } |
50 | |
51 | void HtmlArea::setNewContent( int method, QString str = "" ){ |
52 | zim::Article article; |
53 | |
54 | if (method == 0){ |
55 | article = this->getRandomArticle(); |
56 | }else if( method == 1){ |
57 | article = this->getArticleFromUrl(str); |
58 | }else if( method == 2){ |
59 | // first search |
60 | zim::Search::Results result = this->searchArticleFromTitle(str); |
61 | // if result = 1 display article |
62 | if (result.size() == 1){ |
63 | //create ZIM file accessor |
64 | zim::File file = this->get_file(); |
65 | article = file.getArticle(result[0].getArticle().getIndex()); |
66 | } |
67 | // if result > 1 display options return at end |
68 | else if (result.size() > 1){ |
69 | QString res; |
70 | for (unsigned i = 0; i < result.size(); ++i) |
71 | { |
72 | res.append("<li><a href='" + QString::fromStdString(result[i].getArticle().getUrl()) + "'>" + QString::fromStdString(result[i].getArticle().getTitle()) + "</a></li>"); |
73 | } |
74 | this->setText(res); |
75 | return; |
76 | } |
77 | |
78 | } |
79 | |
80 | //check if article is valid, if display if not show error |
81 | if (article.good()){ |
82 | //TODO build history, set Title |
83 | this->setText(QString::fromStdString(article.getPage())); |
84 | }else{ |
85 | //construct error text |
86 | QString errText = "The article you requested ("; |
87 | errText.append(str); |
88 | errText.append(") could not be found."); |
89 | |
90 | //construct and display error box |
91 | QMessageBox::information(qobject_cast<QMain *>(this->parentWidget()), "Error", |
92 | errText, |
93 | QMessageBox::Close); |
94 | } |
95 | |
96 | } |
97 | |
98 | // // slots |
99 | // // TODO ...is there any way to connect setNewContent to Actions directly using default values? |
100 | void HtmlArea::getRandom(){ |
101 | log_debug("getRandom called"); |
102 | this->setNewContent( 0 ); |
103 | } |
104 | |
105 | void HtmlArea::searchArticle(QString term){ |
106 | log_debug("searchArticle called"); |
107 | this->setNewContent( 2 , term); |
108 | } |
109 | |
110 | // |
111 | //////////////////////////////////////////// |
112 | //////////////////////////////////////////// |
113 | // Zimlib functions |
114 | |
115 | const zim::File& HtmlArea::get_file() |
116 | { |
117 | static zim::File zimFile; |
118 | |
119 | if (!zimFile.good()) |
120 | { |
121 | zimFile = zim::File(qobject_cast<QMain *>(this->parentWidget())->fileLocation); |
122 | log_debug("number of articles: " << zimFile.getCountArticles()); |
123 | } |
124 | |
125 | return zimFile; |
126 | } |
127 | |
128 | //get random article |
129 | //TODO get new article if article returned is current article |
130 | zim::Article HtmlArea::getRandomArticle(){ |
131 | |
132 | //get file |
133 | zim::File file = this->get_file(); |
134 | |
135 | //create empty article |
136 | zim::Article article; |
137 | |
138 | // do |
139 | // { |
140 | // generate random number |
141 | unsigned int seed = static_cast<unsigned int>(time(0)); |
142 | zim::size_type idx = static_cast<zim::size_type>(static_cast<double>(file.getCountArticles()) * rand_r(&seed) / RAND_MAX); |
143 | |
144 | //retrieve article |
145 | article = file.getArticle(idx); |
146 | |
147 | //loop in case article is redirect |
148 | do |
149 | { |
150 | article = article.getRedirectArticle(); |
151 | }while(article.isRedirect()); |
152 | |
153 | // }while(article.getUrl() == current_url); |
154 | |
155 | return article; |
156 | } |
157 | |
158 | zim::Article HtmlArea::getArticleFromUrl(QString url) |
159 | { |
160 | // TODO unescape url |
161 | |
162 | //create ZIM file accessor |
163 | zim::File file = this->get_file(); |
164 | |
165 | //create empty article |
166 | zim::Article article; |
167 | |
168 | // remove '+' signs |
169 | QString term = url.replace( '+', ' '); |
170 | |
171 | //set default namespace |
172 | char ns = 'A'; |
173 | |
174 | //check for different namespace |
175 | if (term.contains("/")) |
176 | { |
177 | int index = term.indexOf("/"); |
178 | if ( index == 1) |
179 | { |
180 | ns = term[0].toLatin1(); |
181 | term.remove(0, 2); |
182 | } |
183 | //TODO what was this for? |
184 | /* |
185 | else |
186 | { |
187 | term.erase(1); |
188 | ns = term[0]; |
189 | found2 = term.find("/"); |
190 | term.erase(0, term.indexOf("/")); |
191 | }*/ |
192 | } |
193 | |
194 | // // // try to retrieve article |
195 | try |
196 | { |
197 | article = file.getArticle(ns, term.toStdString()); |
198 | return article; |
199 | } |
200 | catch (const std::exception& e) |
201 | { |
202 | std::cerr << e.what() << std::endl; |
203 | } |
204 | } |
205 | |
206 | zim::Search::Results HtmlArea::searchArticleFromTitle(QString phrase) |
207 | { |
208 | char ns; |
209 | ns = 'A'; |
210 | |
211 | zim::File file = get_file(); |
212 | |
213 | zim::Article article; |
214 | |
215 | zim::Search::Results result; |
216 | zim::Search search(file); |
217 | search.setSearchLimit(25); |
218 | search.find(result, ns, phrase.toStdString()); |
219 | |
220 | return result; |
221 | } |
222 | |
223 | // zim::Article HtmlArea::getArticleFromTitle(QString *phrase) |
224 | // { |
225 | // // historyCall = false; |
226 | // // screenblock(1); |
227 | // |
228 | // if( result.size() == 0) |
229 | // { |
230 | // // show_message("Error", "The article you requested (" + term + ") was not found."); |
231 | // // screenblock(0); |
232 | // return article; |
233 | // }else{ |
234 | // |
235 | // if (result.size() == 1){ |
236 | // log_debug("one article in result"); |
237 | // |
238 | // article = z.getArticle(result[0].getArticle().getIndex()); |
239 | // |
240 | // //loop in case article is redirect |
241 | // do |
242 | // { |
243 | // article = article.getRedirectArticle(); |
244 | // }while(article.isRedirect()); |
245 | // |
246 | // return article; |
247 | // } |
248 | // else |
249 | // { |
250 | // log_debug("more than one article in result"); |
251 | // for (unsigned i = 0; i < result.size(); ++i) |
252 | // { |
253 | // res += "<li><a href='" + result[i].getArticle().getUrl() + "'>" + result[i].getArticle().getUrl() + "</a></li>"; |
254 | // } |
255 | // |
256 | // |
257 | // } |
258 | // |
259 | // // fill_gtkhtml(res, url, title); |
260 | // } |
261 | // } |
262 |
Branches:
master