summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-reader/CHM.cpp
Side-by-side diff
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 @@
#include "CHM.h"
#include "chm_lib.h"
#include <qstring.h>
#include <qstringlist.h>
#include <stdio.h>
#include <qimage.h>
#include <qpixmap.h>
+#ifdef USEQPE
+#include <qpe/global.h>
+#endif
#ifndef __STATIC
extern "C"
{
CExpander* newcodec() { return new CHM; }
}
#endif
static int _print_ui_index(struct chmFile *h, struct chmUnitInfo *ui,
void *context)
{
CHM *chm = (CHM *)context;
QString temp = "<tr>";
char buff[1024];
sprintf( buff,"<td align=right>%8d\n</td><td><a href=\"%s\">%s</a></td></tr>",(int)ui->length, ui->path, ui->path);
temp += buff;
chm->addContent(temp);
return CHM_ENUMERATOR_CONTINUE;
}
static int _get_hhc (struct chmFile *h, struct chmUnitInfo *ui,
void *context)
{
CHM *chm = (CHM *)context;
QString PathName = ui->path;
if (PathName.find(".hhc") > -1) {
chm->setPath(PathName);
}
return CHM_ENUMERATOR_CONTINUE;
}
CHM::CHM() {
chmFile = NULL;
chmPath = "";
chmHHCPath = "";
chmBuffer = "";
bufpos = 0;
}
CHM::~CHM() {
if (chmFile != NULL)
chm_close(chmFile);
}
-void CHM::suspend() {
+void CHM::suspend() {
#ifdef USEQPE
bSuspended = true;
//suspos = gztell(file);
chm_close(chmFile);
chmFile = NULL;
- sustime = time(NULL);
+ sustime = time(NULL);
#endif
}
-void CHM::unsuspend() {
+void CHM::unsuspend() {
#ifdef USEQPE
if (bSuspended)
{
bSuspended = false;
int delay = time(NULL) - sustime;
- if (delay < 10)
- sleep(10-delay);
+ if (delay < 10)
+ {
+ Global::statusMessage("Stalling");
+ sleep(10-delay);
+ }
chmFile = chm_open(fname);
for (int i = 0; chmFile == NULL && i < 5; i++) {
+ Global::statusMessage("Stalling");
sleep(5);
chmFile = chm_open(fname);
}
if (chmFile == NULL) {
QMessageBox::warning(NULL, PROGNAME, "Couldn't reopen file");
exit(0);
}
//suspos = gzseek(file, suspos, SEEK_SET);
- }
+ }
#endif
}
void CHM::addContent(QString content) {
chmBuffer += content;
}
void CHM::FillHomeContent() {
+unsuspend();
if (chmHHCPath != "") {
const char *ext;
char buffer[65536];
unsigned int swath, offset;
QString tmp="";
QString HTML="";
/* try to find the file */
const char *filename = (const char *)chmHHCPath;
if (chm_resolve_object(chmFile, filename, &m_ui) != CHM_RESOLVE_SUCCESS)
{
chmBuffer = "HELP";
fsize = chmBuffer.length();
return;
}
m_homestart = m_ui.start;
m_homeend = m_homestart + m_ui.length;
swath = 65536;
offset = 0;
fsize = m_ui.length;
while (offset < m_ui.length)
{
if ((m_ui.length - offset) < 65536)
swath = m_ui.length - offset;
else
@@ -141,126 +149,129 @@ void CHM::FillHomeContent() {
Local = Local.left(Local.length() - 2);
if (Name != "") {
HTML += "<br>\r\n<a href=";
HTML += Local;
HTML += ">";
HTML += Name;
HTML += "</a>\r\n";
Name = "";
}
}
}
HTML +="</h3>";
HTML +="</BODY>\r\n";
HTML +="</HTML>\r\n";
chmHomeBuffer = HTML;
chmBuffer = HTML;
fsize = chmBuffer.length();
m_currentstart = m_ui.start;
m_currentend = m_currentstart+chmBuffer.length();
}
}
bool CHM::FillBuffer()
{
+unsuspend();
bool bRetVal = false;
char buffer[65536];
int swath, offset;
chmBuffer = "";
swath = 65536;
offset = 0;
fsize = m_ui.length;
while (offset < m_ui.length)
{
if ((m_ui.length - offset) < 65536)
swath = m_ui.length - offset;
else
swath = 65536;
swath = (int)chm_retrieve_object(chmFile, &m_ui, (unsigned char *) buffer, offset, swath);
chmBuffer += buffer;
offset += swath;
}
//There seems to be a ton of gobbledygook at the end that is reminiscent of the hhc file - remove it
QString temp = chmBuffer.lower();
int lpos = temp.find("</html");
if (lpos > -1) {
chmBuffer.truncate(lpos);
chmBuffer = chmBuffer+"</HTML>";
}
fsize = chmBuffer.length();
bufpos = 0;
bRetVal = true;
m_currentstart = m_ui.start;
m_currentend = m_currentstart+chmBuffer.length();
return bRetVal;
}
bool CHM::FillContent() {
+unsuspend();
bool bRetVal = false;
if (chmPath != "") {
/* try to find the file */
const char *filename = (const char *)chmPath;
if (chm_resolve_object(chmFile, filename, &m_ui) != CHM_RESOLVE_SUCCESS)
{
fsize = chmBuffer.length();
return bRetVal;
}
char buffer[65536];
int swath, offset;
chmBuffer = "";
swath = 65536;
offset = 0;
fsize = m_ui.length;
while (offset < m_ui.length)
{
if ((m_ui.length - offset) < 65536)
swath = m_ui.length - offset;
else
swath = 65536;
swath = (int)chm_retrieve_object(chmFile, &m_ui, (unsigned char *) buffer, offset, swath);
chmBuffer += buffer;
offset += swath;
}
//There seems to be a ton of gobbledygook at the end that is reminiscent of the hhc file - remove it
QString temp = chmBuffer.lower();
int lpos = temp.find("</html");
if (lpos > -1) {
chmBuffer.truncate(lpos);
chmBuffer = chmBuffer+"</HTML>";
}
fsize = chmBuffer.length();
bufpos = 0;
m_currentstart = m_ui.start;
m_currentend = m_currentstart+chmBuffer.length();
bRetVal = true;
}
return bRetVal;
}
-bool CHM::getFile(const QString& href) {
+bool CHM::getFile(const QString& _href, const QString& ) {
+ QString href = "/" + _href;
qDebug("Got:%s", (const char*)href);
bool bRetVal = false;
QString temp = chmPath;
chmPath = href;
if (FillContent()) {
qDebug("Got it");
bRetVal = true;
} else {
qDebug("Missed");
chmPath = temp;
FillContent();
}
return bRetVal;
}
QImage *CHM::getPicture(const QString& href) {
QImage *img = NULL;
QString PicRef = "/"+href;
struct chmUnitInfo ui;
const char *ext;
/* try to find the file */
const char *filename = (const char *)PicRef;
if (chm_resolve_object(chmFile, filename, &ui) != CHM_RESOLVE_SUCCESS)
{
@@ -298,53 +309,53 @@ QImage *CHM::getPicture(const QString& href) {
int CHM::OpenFile(const char *src) {
if (chmFile != NULL) chm_close(chmFile);
struct stat _stat;
stat(src,&_stat);
fsize = _stat.st_size;
chmFile = chm_open(src);
if (chmFile != NULL) {
chm_enumerate(chmFile,
CHM_ENUMERATE_ALL,
_get_hhc,
(void *)this);
if ( chmPath != "") {
setHomePath(chmPath);
FillHomeContent();
}
}
m_homepos = locate();
qDebug("Home:%u", m_homepos);
return (chmFile==NULL);
}
int CHM::getch() {
if ( (bufpos+1) >= chmBuffer.length() )
return EOF;
-#ifdef _WINDOWS
- QChar letter = chmBuffer.at(bufpos++);
-#else
- QChar letter = chmBuffer[bufpos++];
-#endif
+#ifdef _WINDOWS
+ QChar letter = chmBuffer.at(bufpos++);
+#else
+ QChar letter = chmBuffer[bufpos++];
+#endif
return (int)(char)letter;
}
void CHM::getch(tchar& ch, CStyle& sty)
{
int ich = getch();
ch = (ich == EOF) ? UEOF : ich;
}
void CHM::start2endSection()
{
m_currentstart = m_ui.start;
m_currentend = m_currentstart+chmBuffer.length();
}
unsigned int CHM::locate() {
return m_currentstart+bufpos;
}
void CHM::locate(unsigned int n) {
if (n == 0) n = m_homepos;
if (n >= m_homestart && n < m_homeend)
{
FillHomeContent();