summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-reader/CHM.cpp
Unidiff
Diffstat (limited to 'noncore/apps/opie-reader/CHM.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/opie-reader/CHM.cpp35
1 files changed, 23 insertions, 12 deletions
diff --git a/noncore/apps/opie-reader/CHM.cpp b/noncore/apps/opie-reader/CHM.cpp
index 88d53bf..ace5abc 100644
--- a/noncore/apps/opie-reader/CHM.cpp
+++ b/noncore/apps/opie-reader/CHM.cpp
@@ -1,112 +1,120 @@
1#include "CHM.h" 1#include "CHM.h"
2#include "chm_lib.h" 2#include "chm_lib.h"
3#include <qstring.h> 3#include <qstring.h>
4#include <qstringlist.h> 4#include <qstringlist.h>
5#include <stdio.h> 5#include <stdio.h>
6#include <qimage.h> 6#include <qimage.h>
7#include <qpixmap.h> 7#include <qpixmap.h>
8#ifdef USEQPE
9#include <qpe/global.h>
10#endif
8 11
9#ifndef __STATIC 12#ifndef __STATIC
10extern "C" 13extern "C"
11{ 14{
12 CExpander* newcodec() { return new CHM; } 15 CExpander* newcodec() { return new CHM; }
13} 16}
14#endif 17#endif
15 18
16static int _print_ui_index(struct chmFile *h, struct chmUnitInfo *ui, 19static int _print_ui_index(struct chmFile *h, struct chmUnitInfo *ui,
17 void *context) 20 void *context)
18{ 21{
19 CHM *chm = (CHM *)context; 22 CHM *chm = (CHM *)context;
20 QString temp = "<tr>"; 23 QString temp = "<tr>";
21 char buff[1024]; 24 char buff[1024];
22 sprintf( buff,"<td align=right>%8d\n</td><td><a href=\"%s\">%s</a></td></tr>",(int)ui->length, ui->path, ui->path); 25 sprintf( buff,"<td align=right>%8d\n</td><td><a href=\"%s\">%s</a></td></tr>",(int)ui->length, ui->path, ui->path);
23 temp += buff; 26 temp += buff;
24 chm->addContent(temp); 27 chm->addContent(temp);
25 return CHM_ENUMERATOR_CONTINUE; 28 return CHM_ENUMERATOR_CONTINUE;
26} 29}
27 30
28static int _get_hhc (struct chmFile *h, struct chmUnitInfo *ui, 31static int _get_hhc (struct chmFile *h, struct chmUnitInfo *ui,
29 void *context) 32 void *context)
30{ 33{
31 CHM *chm = (CHM *)context; 34 CHM *chm = (CHM *)context;
32 QString PathName = ui->path; 35 QString PathName = ui->path;
33 if (PathName.find(".hhc") > -1) { 36 if (PathName.find(".hhc") > -1) {
34 chm->setPath(PathName); 37 chm->setPath(PathName);
35 } 38 }
36 return CHM_ENUMERATOR_CONTINUE; 39 return CHM_ENUMERATOR_CONTINUE;
37} 40}
38 41
39CHM::CHM() { 42CHM::CHM() {
40 chmFile = NULL; 43 chmFile = NULL;
41 chmPath = ""; 44 chmPath = "";
42 chmHHCPath = ""; 45 chmHHCPath = "";
43 chmBuffer = ""; 46 chmBuffer = "";
44 bufpos = 0; 47 bufpos = 0;
45} 48}
46 49
47CHM::~CHM() { 50CHM::~CHM() {
48 if (chmFile != NULL) 51 if (chmFile != NULL)
49 chm_close(chmFile); 52 chm_close(chmFile);
50} 53}
51 54
52void CHM::suspend() { 55void CHM::suspend() {
53#ifdef USEQPE 56#ifdef USEQPE
54 bSuspended = true; 57 bSuspended = true;
55 //suspos = gztell(file); 58 //suspos = gztell(file);
56 chm_close(chmFile); 59 chm_close(chmFile);
57 chmFile = NULL; 60 chmFile = NULL;
58 sustime = time(NULL); 61 sustime = time(NULL);
59#endif 62#endif
60} 63}
61 64
62void CHM::unsuspend() { 65void CHM::unsuspend() {
63#ifdef USEQPE 66#ifdef USEQPE
64 if (bSuspended) 67 if (bSuspended)
65 { 68 {
66 bSuspended = false; 69 bSuspended = false;
67 int delay = time(NULL) - sustime; 70 int delay = time(NULL) - sustime;
68 if (delay < 10) 71 if (delay < 10)
69 sleep(10-delay); 72 {
73 Global::statusMessage("Stalling");
74 sleep(10-delay);
75 }
70 chmFile = chm_open(fname); 76 chmFile = chm_open(fname);
71 for (int i = 0; chmFile == NULL && i < 5; i++) { 77 for (int i = 0; chmFile == NULL && i < 5; i++) {
78 Global::statusMessage("Stalling");
72 sleep(5); 79 sleep(5);
73 chmFile = chm_open(fname); 80 chmFile = chm_open(fname);
74 } 81 }
75 if (chmFile == NULL) { 82 if (chmFile == NULL) {
76 QMessageBox::warning(NULL, PROGNAME, "Couldn't reopen file"); 83 QMessageBox::warning(NULL, PROGNAME, "Couldn't reopen file");
77 exit(0); 84 exit(0);
78 } 85 }
79 //suspos = gzseek(file, suspos, SEEK_SET); 86 //suspos = gzseek(file, suspos, SEEK_SET);
80 } 87 }
81#endif 88#endif
82} 89}
83 90
84void CHM::addContent(QString content) { 91void CHM::addContent(QString content) {
85 chmBuffer += content; 92 chmBuffer += content;
86} 93}
87 94
88void CHM::FillHomeContent() { 95void CHM::FillHomeContent() {
96unsuspend();
89 if (chmHHCPath != "") { 97 if (chmHHCPath != "") {
90 const char *ext; 98 const char *ext;
91 char buffer[65536]; 99 char buffer[65536];
92 unsigned int swath, offset; 100 unsigned int swath, offset;
93 QString tmp=""; 101 QString tmp="";
94 QString HTML=""; 102 QString HTML="";
95 /* try to find the file */ 103 /* try to find the file */
96 const char *filename = (const char *)chmHHCPath; 104 const char *filename = (const char *)chmHHCPath;
97 if (chm_resolve_object(chmFile, filename, &m_ui) != CHM_RESOLVE_SUCCESS) 105 if (chm_resolve_object(chmFile, filename, &m_ui) != CHM_RESOLVE_SUCCESS)
98 { 106 {
99 chmBuffer = "HELP"; 107 chmBuffer = "HELP";
100 fsize = chmBuffer.length(); 108 fsize = chmBuffer.length();
101 return; 109 return;
102 } 110 }
103 m_homestart = m_ui.start; 111 m_homestart = m_ui.start;
104 m_homeend = m_homestart + m_ui.length; 112 m_homeend = m_homestart + m_ui.length;
105 swath = 65536; 113 swath = 65536;
106 offset = 0; 114 offset = 0;
107 fsize = m_ui.length; 115 fsize = m_ui.length;
108 while (offset < m_ui.length) 116 while (offset < m_ui.length)
109 { 117 {
110 if ((m_ui.length - offset) < 65536) 118 if ((m_ui.length - offset) < 65536)
111 swath = m_ui.length - offset; 119 swath = m_ui.length - offset;
112 else 120 else
@@ -141,126 +149,129 @@ void CHM::FillHomeContent() {
141 Local = Local.left(Local.length() - 2); 149 Local = Local.left(Local.length() - 2);
142 if (Name != "") { 150 if (Name != "") {
143 HTML += "<br>\r\n<a href="; 151 HTML += "<br>\r\n<a href=";
144 HTML += Local; 152 HTML += Local;
145 HTML += ">"; 153 HTML += ">";
146 HTML += Name; 154 HTML += Name;
147 HTML += "</a>\r\n"; 155 HTML += "</a>\r\n";
148 Name = ""; 156 Name = "";
149 } 157 }
150 } 158 }
151 } 159 }
152 HTML +="</h3>"; 160 HTML +="</h3>";
153 HTML +="</BODY>\r\n"; 161 HTML +="</BODY>\r\n";
154 HTML +="</HTML>\r\n"; 162 HTML +="</HTML>\r\n";
155 chmHomeBuffer = HTML; 163 chmHomeBuffer = HTML;
156 chmBuffer = HTML; 164 chmBuffer = HTML;
157 fsize = chmBuffer.length(); 165 fsize = chmBuffer.length();
158 m_currentstart = m_ui.start; 166 m_currentstart = m_ui.start;
159 m_currentend = m_currentstart+chmBuffer.length(); 167 m_currentend = m_currentstart+chmBuffer.length();
160 } 168 }
161} 169}
162 170
163bool CHM::FillBuffer() 171bool CHM::FillBuffer()
164{ 172{
173unsuspend();
165 bool bRetVal = false; 174 bool bRetVal = false;
166 char buffer[65536]; 175 char buffer[65536];
167 int swath, offset; 176 int swath, offset;
168 chmBuffer = ""; 177 chmBuffer = "";
169 swath = 65536; 178 swath = 65536;
170 offset = 0; 179 offset = 0;
171 fsize = m_ui.length; 180 fsize = m_ui.length;
172 while (offset < m_ui.length) 181 while (offset < m_ui.length)
173 { 182 {
174 if ((m_ui.length - offset) < 65536) 183 if ((m_ui.length - offset) < 65536)
175 swath = m_ui.length - offset; 184 swath = m_ui.length - offset;
176 else 185 else
177 swath = 65536; 186 swath = 65536;
178 swath = (int)chm_retrieve_object(chmFile, &m_ui, (unsigned char *) buffer, offset, swath); 187 swath = (int)chm_retrieve_object(chmFile, &m_ui, (unsigned char *) buffer, offset, swath);
179 chmBuffer += buffer; 188 chmBuffer += buffer;
180 offset += swath; 189 offset += swath;
181 } 190 }
182 //There seems to be a ton of gobbledygook at the end that is reminiscent of the hhc file - remove it 191 //There seems to be a ton of gobbledygook at the end that is reminiscent of the hhc file - remove it
183 QString temp = chmBuffer.lower(); 192 QString temp = chmBuffer.lower();
184 int lpos = temp.find("</html"); 193 int lpos = temp.find("</html");
185 if (lpos > -1) { 194 if (lpos > -1) {
186 chmBuffer.truncate(lpos); 195 chmBuffer.truncate(lpos);
187 chmBuffer = chmBuffer+"</HTML>"; 196 chmBuffer = chmBuffer+"</HTML>";
188 } 197 }
189 fsize = chmBuffer.length(); 198 fsize = chmBuffer.length();
190 199
191 bufpos = 0; 200 bufpos = 0;
192 bRetVal = true; 201 bRetVal = true;
193 m_currentstart = m_ui.start; 202 m_currentstart = m_ui.start;
194 m_currentend = m_currentstart+chmBuffer.length(); 203 m_currentend = m_currentstart+chmBuffer.length();
195 return bRetVal; 204 return bRetVal;
196} 205}
197 206
198bool CHM::FillContent() { 207bool CHM::FillContent() {
208unsuspend();
199 bool bRetVal = false; 209 bool bRetVal = false;
200 if (chmPath != "") { 210 if (chmPath != "") {
201 /* try to find the file */ 211 /* try to find the file */
202 const char *filename = (const char *)chmPath; 212 const char *filename = (const char *)chmPath;
203 if (chm_resolve_object(chmFile, filename, &m_ui) != CHM_RESOLVE_SUCCESS) 213 if (chm_resolve_object(chmFile, filename, &m_ui) != CHM_RESOLVE_SUCCESS)
204 { 214 {
205 fsize = chmBuffer.length(); 215 fsize = chmBuffer.length();
206 return bRetVal; 216 return bRetVal;
207 } 217 }
208 218
209 char buffer[65536]; 219 char buffer[65536];
210 int swath, offset; 220 int swath, offset;
211 chmBuffer = ""; 221 chmBuffer = "";
212 swath = 65536; 222 swath = 65536;
213 offset = 0; 223 offset = 0;
214 fsize = m_ui.length; 224 fsize = m_ui.length;
215 while (offset < m_ui.length) 225 while (offset < m_ui.length)
216 { 226 {
217 if ((m_ui.length - offset) < 65536) 227 if ((m_ui.length - offset) < 65536)
218 swath = m_ui.length - offset; 228 swath = m_ui.length - offset;
219 else 229 else
220 swath = 65536; 230 swath = 65536;
221 swath = (int)chm_retrieve_object(chmFile, &m_ui, (unsigned char *) buffer, offset, swath); 231 swath = (int)chm_retrieve_object(chmFile, &m_ui, (unsigned char *) buffer, offset, swath);
222 chmBuffer += buffer; 232 chmBuffer += buffer;
223 offset += swath; 233 offset += swath;
224 } 234 }
225 //There seems to be a ton of gobbledygook at the end that is reminiscent of the hhc file - remove it 235 //There seems to be a ton of gobbledygook at the end that is reminiscent of the hhc file - remove it
226 QString temp = chmBuffer.lower(); 236 QString temp = chmBuffer.lower();
227 int lpos = temp.find("</html"); 237 int lpos = temp.find("</html");
228 if (lpos > -1) { 238 if (lpos > -1) {
229 chmBuffer.truncate(lpos); 239 chmBuffer.truncate(lpos);
230 chmBuffer = chmBuffer+"</HTML>"; 240 chmBuffer = chmBuffer+"</HTML>";
231 } 241 }
232 fsize = chmBuffer.length(); 242 fsize = chmBuffer.length();
233 243
234 bufpos = 0; 244 bufpos = 0;
235 m_currentstart = m_ui.start; 245 m_currentstart = m_ui.start;
236 m_currentend = m_currentstart+chmBuffer.length(); 246 m_currentend = m_currentstart+chmBuffer.length();
237 bRetVal = true; 247 bRetVal = true;
238 } 248 }
239 return bRetVal; 249 return bRetVal;
240} 250}
241 251
242bool CHM::getFile(const QString& href) { 252bool CHM::getFile(const QString& _href, const QString& ) {
253 QString href = "/" + _href;
243 qDebug("Got:%s", (const char*)href); 254 qDebug("Got:%s", (const char*)href);
244 bool bRetVal = false; 255 bool bRetVal = false;
245 QString temp = chmPath; 256 QString temp = chmPath;
246 chmPath = href; 257 chmPath = href;
247 if (FillContent()) { 258 if (FillContent()) {
248 qDebug("Got it"); 259 qDebug("Got it");
249 bRetVal = true; 260 bRetVal = true;
250 } else { 261 } else {
251 qDebug("Missed"); 262 qDebug("Missed");
252 chmPath = temp; 263 chmPath = temp;
253 FillContent(); 264 FillContent();
254 } 265 }
255 return bRetVal; 266 return bRetVal;
256} 267}
257 268
258QImage *CHM::getPicture(const QString& href) { 269QImage *CHM::getPicture(const QString& href) {
259 QImage *img = NULL; 270 QImage *img = NULL;
260 QString PicRef = "/"+href; 271 QString PicRef = "/"+href;
261 struct chmUnitInfo ui; 272 struct chmUnitInfo ui;
262 const char *ext; 273 const char *ext;
263 /* try to find the file */ 274 /* try to find the file */
264 const char *filename = (const char *)PicRef; 275 const char *filename = (const char *)PicRef;
265 if (chm_resolve_object(chmFile, filename, &ui) != CHM_RESOLVE_SUCCESS) 276 if (chm_resolve_object(chmFile, filename, &ui) != CHM_RESOLVE_SUCCESS)
266 { 277 {
@@ -298,53 +309,53 @@ QImage *CHM::getPicture(const QString& href) {
298int CHM::OpenFile(const char *src) { 309int CHM::OpenFile(const char *src) {
299 if (chmFile != NULL) chm_close(chmFile); 310 if (chmFile != NULL) chm_close(chmFile);
300 struct stat _stat; 311 struct stat _stat;
301 stat(src,&_stat); 312 stat(src,&_stat);
302 fsize = _stat.st_size; 313 fsize = _stat.st_size;
303 chmFile = chm_open(src); 314 chmFile = chm_open(src);
304 if (chmFile != NULL) { 315 if (chmFile != NULL) {
305 chm_enumerate(chmFile, 316 chm_enumerate(chmFile,
306 CHM_ENUMERATE_ALL, 317 CHM_ENUMERATE_ALL,
307 _get_hhc, 318 _get_hhc,
308 (void *)this); 319 (void *)this);
309 if ( chmPath != "") { 320 if ( chmPath != "") {
310 setHomePath(chmPath); 321 setHomePath(chmPath);
311 FillHomeContent(); 322 FillHomeContent();
312 } 323 }
313 } 324 }
314 m_homepos = locate(); 325 m_homepos = locate();
315 qDebug("Home:%u", m_homepos); 326 qDebug("Home:%u", m_homepos);
316 return (chmFile==NULL); 327 return (chmFile==NULL);
317} 328}
318 329
319int CHM::getch() { 330int CHM::getch() {
320 if ( (bufpos+1) >= chmBuffer.length() ) 331 if ( (bufpos+1) >= chmBuffer.length() )
321 return EOF; 332 return EOF;
322#ifdef _WINDOWS 333#ifdef _WINDOWS
323 QChar letter = chmBuffer.at(bufpos++); 334 QChar letter = chmBuffer.at(bufpos++);
324#else 335#else
325 QChar letter = chmBuffer[bufpos++]; 336 QChar letter = chmBuffer[bufpos++];
326#endif 337#endif
327 return (int)(char)letter; 338 return (int)(char)letter;
328} 339}
329 340
330void CHM::getch(tchar& ch, CStyle& sty) 341void CHM::getch(tchar& ch, CStyle& sty)
331{ 342{
332 int ich = getch(); 343 int ich = getch();
333 ch = (ich == EOF) ? UEOF : ich; 344 ch = (ich == EOF) ? UEOF : ich;
334} 345}
335 346
336void CHM::start2endSection() 347void CHM::start2endSection()
337{ 348{
338 m_currentstart = m_ui.start; 349 m_currentstart = m_ui.start;
339 m_currentend = m_currentstart+chmBuffer.length(); 350 m_currentend = m_currentstart+chmBuffer.length();
340} 351}
341 352
342unsigned int CHM::locate() { 353unsigned int CHM::locate() {
343 return m_currentstart+bufpos; 354 return m_currentstart+bufpos;
344} 355}
345 356
346void CHM::locate(unsigned int n) { 357void CHM::locate(unsigned int n) {
347 if (n == 0) n = m_homepos; 358 if (n == 0) n = m_homepos;
348 if (n >= m_homestart && n < m_homeend) 359 if (n >= m_homestart && n < m_homeend)
349 { 360 {
350 FillHomeContent(); 361 FillHomeContent();