You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
405 lines
11 KiB
405 lines
11 KiB
15 years ago
|
#include <config.h>
|
||
|
|
||
|
#ifdef HAVE_SYS_TYPES_H
|
||
|
# include <sys/types.h>
|
||
|
#endif
|
||
|
#ifdef HAVE_SYS_STAT_H
|
||
|
# include <sys/stat.h>
|
||
|
#endif
|
||
|
|
||
|
#include <errno.h>
|
||
|
#include <fcntl.h>
|
||
|
#ifdef HAVE_STDIO_H
|
||
|
# include <stdio.h>
|
||
|
#endif
|
||
|
#ifdef HAVE_STDLIB_H
|
||
|
# include <stdlib.h>
|
||
|
#endif
|
||
|
|
||
15 years ago
|
#include <tqvaluelist.h>
|
||
|
#include <tqfileinfo.h>
|
||
|
#include <tqfile.h>
|
||
13 years ago
|
#include <tqtextstream.h>
|
||
15 years ago
|
#include <tqregexp.h>
|
||
13 years ago
|
#include <tqtextcodec.h>
|
||
15 years ago
|
|
||
|
#include <kdebug.h>
|
||
|
#include <kurl.h>
|
||
|
#include <kglobal.h>
|
||
|
#include <klocale.h>
|
||
|
#include <kstandarddirs.h>
|
||
|
#include <kinstance.h>
|
||
|
|
||
12 years ago
|
#include "tdeio_help.h"
|
||
15 years ago
|
#include <libxslt/xsltutils.h>
|
||
|
#include <libxslt/transform.h>
|
||
|
#include "xslt.h"
|
||
|
|
||
12 years ago
|
using namespace TDEIO;
|
||
15 years ago
|
|
||
15 years ago
|
TQString HelpProtocol::langLookup(const TQString& fname)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQStringList search;
|
||
15 years ago
|
|
||
|
// assemble the local search paths
|
||
12 years ago
|
const TQStringList localDoc = TDEGlobal::dirs()->resourceDirs("html") + TDEGlobal::dirs()->resourceDirs("html-bundle");
|
||
15 years ago
|
|
||
12 years ago
|
TQStringList langs = TDEGlobal::locale()->languageList();
|
||
15 years ago
|
langs.append( "en" );
|
||
|
langs.remove( "C" );
|
||
|
|
||
|
// this is kind of compat hack as we install our docs in en/ but the
|
||
|
// default language is en_US
|
||
15 years ago
|
for (TQStringList::Iterator it = langs.begin(); it != langs.end(); ++it)
|
||
15 years ago
|
if ( *it == "en_US" )
|
||
|
*it = "en";
|
||
|
|
||
|
// look up the different languages
|
||
|
int ldCount = localDoc.count();
|
||
|
for (int id=0; id < ldCount; id++)
|
||
|
{
|
||
15 years ago
|
TQStringList::ConstIterator lang;
|
||
15 years ago
|
for (lang = langs.begin(); lang != langs.end(); ++lang)
|
||
15 years ago
|
search.append(TQString("%1%2/%3").arg(localDoc[id], *lang, fname));
|
||
15 years ago
|
}
|
||
|
|
||
|
// try to locate the file
|
||
15 years ago
|
TQStringList::Iterator it;
|
||
15 years ago
|
for (it = search.begin(); it != search.end(); ++it)
|
||
|
{
|
||
|
kdDebug( 7119 ) << "Looking for help in: " << *it << endl;
|
||
|
|
||
15 years ago
|
TQFileInfo info(*it);
|
||
15 years ago
|
if (info.exists() && info.isFile() && info.isReadable())
|
||
|
return *it;
|
||
|
|
||
|
if ( ( *it ).right( 5 ) == ".html" )
|
||
|
{
|
||
13 years ago
|
TQString file = (*it).left((*it).findRev('/')) + "/index.docbook";
|
||
15 years ago
|
kdDebug( 7119 ) << "Looking for help in: " << file << endl;
|
||
|
info.setFile(file);
|
||
|
if (info.exists() && info.isFile() && info.isReadable())
|
||
|
return *it;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
15 years ago
|
TQString HelpProtocol::lookupFile(const TQString &fname,
|
||
|
const TQString &query, bool &redirect)
|
||
15 years ago
|
{
|
||
|
redirect = false;
|
||
|
|
||
15 years ago
|
TQString path, result;
|
||
15 years ago
|
|
||
|
path = fname;
|
||
|
|
||
|
result = langLookup(path);
|
||
|
if (result.isEmpty())
|
||
|
{
|
||
|
result = langLookup(path+"/index.html");
|
||
|
if (!result.isEmpty())
|
||
|
{
|
||
|
KURL red( "help:/" );
|
||
|
red.setPath( path + "/index.html" );
|
||
|
red.setQuery( query );
|
||
|
redirection(red);
|
||
|
kdDebug( 7119 ) << "redirect to " << red.url() << endl;
|
||
|
redirect = true;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
13 years ago
|
unicodeError( i18n("There is no documentation available for %1." ).arg(path) );
|
||
15 years ago
|
finished();
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
} else
|
||
|
kdDebug( 7119 ) << "result " << result << endl;
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
|
||
13 years ago
|
void HelpProtocol::unicodeError( const TQString &t )
|
||
15 years ago
|
{
|
||
15 years ago
|
data(fromUnicode( TQString(
|
||
15 years ago
|
"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=%1\"></head>\n"
|
||
15 years ago
|
"%2</html>" ).arg( TQTextCodec::codecForLocale()->name() ).arg( t ) ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
HelpProtocol *slave = 0;
|
||
|
|
||
15 years ago
|
HelpProtocol::HelpProtocol( bool ghelp, const TQCString &pool, const TQCString &app )
|
||
15 years ago
|
: SlaveBase( ghelp ? "ghelp" : "help", pool, app ), mGhelp( ghelp )
|
||
|
{
|
||
|
slave = this;
|
||
|
}
|
||
|
|
||
|
void HelpProtocol::get( const KURL& url )
|
||
|
{
|
||
|
kdDebug( 7119 ) << "get: path=" << url.path()
|
||
|
<< " query=" << url.query() << endl;
|
||
|
|
||
|
bool redirect;
|
||
15 years ago
|
TQString doc;
|
||
15 years ago
|
doc = url.path();
|
||
|
|
||
|
if ( !mGhelp ) {
|
||
13 years ago
|
if (doc.at(0) != '/')
|
||
15 years ago
|
doc = doc.prepend('/');
|
||
|
|
||
13 years ago
|
if (doc.at(doc.length() - 1) == '/')
|
||
15 years ago
|
doc += "index.html";
|
||
|
}
|
||
|
|
||
|
infoMessage(i18n("Looking up correct file"));
|
||
|
|
||
|
if ( !mGhelp ) {
|
||
|
doc = lookupFile(doc, url.query(), redirect);
|
||
|
|
||
|
if (redirect)
|
||
|
{
|
||
|
finished();
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (doc.isEmpty())
|
||
|
{
|
||
12 years ago
|
error( TDEIO::ERR_DOES_NOT_EXIST, url.url() );
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
mimeType("text/html");
|
||
|
KURL target;
|
||
|
target.setPath(doc);
|
||
|
if (url.hasHTMLRef())
|
||
|
target.setHTMLRef(url.htmlRef());
|
||
|
|
||
|
kdDebug( 7119 ) << "target " << target.url() << endl;
|
||
|
|
||
15 years ago
|
TQString file = target.path();
|
||
15 years ago
|
|
||
|
if ( mGhelp ) {
|
||
|
if ( file.right( 4 ) != ".xml" ) {
|
||
|
get_file( target );
|
||
|
return;
|
||
|
}
|
||
|
} else {
|
||
13 years ago
|
TQString docbook_file = file.left(file.findRev('/')) + "/index.docbook";
|
||
15 years ago
|
if (!KStandardDirs::exists(file)) {
|
||
|
file = docbook_file;
|
||
|
} else {
|
||
15 years ago
|
TQFileInfo fi(file);
|
||
15 years ago
|
if (fi.isDir()) {
|
||
|
file = file + "/index.docbook";
|
||
|
} else {
|
||
|
if ( file.right( 5 ) != ".html" || !compareTimeStamps( file, docbook_file ) ) {
|
||
|
get_file( target );
|
||
|
return;
|
||
|
} else
|
||
|
file = docbook_file;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
infoMessage(i18n("Preparing document"));
|
||
|
|
||
|
if ( mGhelp ) {
|
||
15 years ago
|
TQString xsl = "customization/kde-nochunk.xsl";
|
||
15 years ago
|
mParsed = transform(file, locate("dtd", xsl));
|
||
|
|
||
|
kdDebug( 7119 ) << "parsed " << mParsed.length() << endl;
|
||
|
|
||
|
if (mParsed.isEmpty()) {
|
||
13 years ago
|
unicodeError( i18n( "The requested help file could not be parsed:<br>%1" ).arg( file ) );
|
||
15 years ago
|
} else {
|
||
13 years ago
|
int pos1 = mParsed.find( "charset=" );
|
||
15 years ago
|
if ( pos1 > 0 ) {
|
||
13 years ago
|
int pos2 = mParsed.find( '"', pos1 );
|
||
15 years ago
|
if ( pos2 > 0 ) {
|
||
14 years ago
|
mParsed.replace( pos1, pos2 - pos1, "charset=UTF-8" );
|
||
15 years ago
|
}
|
||
|
}
|
||
|
data( mParsed.utf8() );
|
||
|
}
|
||
|
} else {
|
||
|
|
||
|
kdDebug( 7119 ) << "look for cache for " << file << endl;
|
||
|
|
||
|
mParsed = lookForCache( file );
|
||
|
|
||
|
kdDebug( 7119 ) << "cached parsed " << mParsed.length() << endl;
|
||
|
|
||
|
if ( mParsed.isEmpty() ) {
|
||
|
mParsed = transform(file, locate("dtd", "customization/kde-chunk.xsl"));
|
||
|
if ( !mParsed.isEmpty() ) {
|
||
|
infoMessage( i18n( "Saving to cache" ) );
|
||
15 years ago
|
TQString cache = file.left( file.length() - 7 );
|
||
15 years ago
|
saveToCache( mParsed, locateLocal( "cache",
|
||
12 years ago
|
"tdeio_help" + cache +
|
||
15 years ago
|
"cache.bz2" ) );
|
||
|
}
|
||
|
} else infoMessage( i18n( "Using cached version" ) );
|
||
|
|
||
|
kdDebug( 7119 ) << "parsed " << mParsed.length() << endl;
|
||
|
|
||
|
if (mParsed.isEmpty()) {
|
||
13 years ago
|
unicodeError( i18n( "The requested help file could not be parsed:<br>%1" ).arg( file ) );
|
||
15 years ago
|
} else {
|
||
15 years ago
|
TQString query = url.query(), anchor;
|
||
15 years ago
|
|
||
14 years ago
|
// if we have a query, look if it contains an anchor
|
||
15 years ago
|
if (!query.isEmpty())
|
||
|
if (query.left(8) == "?anchor=") {
|
||
|
anchor = query.mid(8).lower();
|
||
|
|
||
|
KURL redirURL(url);
|
||
|
|
||
15 years ago
|
redirURL.setQuery(TQString::null);
|
||
15 years ago
|
redirURL.setHTMLRef(anchor);
|
||
|
redirection(redirURL);
|
||
|
finished();
|
||
|
return;
|
||
|
}
|
||
|
if (anchor.isEmpty() && url.hasHTMLRef())
|
||
|
anchor = url.htmlRef();
|
||
|
|
||
|
kdDebug( 7119 ) << "anchor: " << anchor << endl;
|
||
|
|
||
|
if ( !anchor.isEmpty() )
|
||
|
{
|
||
|
int index = 0;
|
||
|
while ( true ) {
|
||
13 years ago
|
index = mParsed.find( TQRegExp( "<a name=" ), index);
|
||
15 years ago
|
if ( index == -1 ) {
|
||
|
kdDebug( 7119 ) << "no anchor\n";
|
||
|
break; // use whatever is the target, most likely index.html
|
||
|
}
|
||
|
|
||
|
if ( mParsed.mid( index, 11 + anchor.length() ).lower() ==
|
||
15 years ago
|
TQString( "<a name=\"%1\">" ).arg( anchor ) )
|
||
15 years ago
|
{
|
||
13 years ago
|
index = mParsed.findRev( "<FILENAME filename=", index ) +
|
||
15 years ago
|
strlen( "<FILENAME filename=\"" );
|
||
15 years ago
|
TQString filename=mParsed.mid( index, 2000 );
|
||
13 years ago
|
filename = filename.left( filename.find( '\"' ) );
|
||
15 years ago
|
TQString path = target.path();
|
||
13 years ago
|
path = path.left( path.findRev( '/' ) + 1) + filename;
|
||
15 years ago
|
kdDebug( 7119 ) << "anchor found in " << path <<endl;
|
||
|
target.setPath( path );
|
||
|
break;
|
||
|
}
|
||
|
index++;
|
||
|
}
|
||
|
}
|
||
|
emitFile( target );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
finished();
|
||
|
}
|
||
|
|
||
|
void HelpProtocol::emitFile( const KURL& url )
|
||
|
{
|
||
|
infoMessage(i18n("Looking up section"));
|
||
|
|
||
13 years ago
|
TQString filename = url.path().mid(url.path().findRev('/') + 1);
|
||
15 years ago
|
|
||
13 years ago
|
int index = mParsed.find(TQString("<FILENAME filename=\"%1\"").arg(filename));
|
||
15 years ago
|
if (index == -1) {
|
||
|
if ( filename == "index.html" ) {
|
||
|
data( fromUnicode( mParsed ) );
|
||
|
return;
|
||
|
}
|
||
|
|
||
13 years ago
|
unicodeError( i18n("Could not find filename %1 in %2.").arg(filename).arg( url.url() ) );
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString filedata = splitOut(mParsed, index);
|
||
14 years ago
|
replaceCharsetHeader( filedata );
|
||
15 years ago
|
|
||
|
data( fromUnicode( filedata ) );
|
||
15 years ago
|
data( TQByteArray() );
|
||
15 years ago
|
}
|
||
|
|
||
|
void HelpProtocol::mimetype( const KURL &)
|
||
|
{
|
||
|
mimeType("text/html");
|
||
|
finished();
|
||
|
}
|
||
|
|
||
12 years ago
|
// Copied from tdeio_file to avoid redirects
|
||
15 years ago
|
|
||
|
#define MAX_IPC_SIZE (1024*32)
|
||
|
|
||
|
void HelpProtocol::get_file( const KURL& url )
|
||
|
{
|
||
|
kdDebug( 7119 ) << "get_file " << url.url() << endl;
|
||
|
|
||
15 years ago
|
TQCString _path( TQFile::encodeName(url.path()));
|
||
15 years ago
|
struct stat buff;
|
||
|
if ( ::stat( _path.data(), &buff ) == -1 ) {
|
||
|
if ( errno == EACCES )
|
||
12 years ago
|
error( TDEIO::ERR_ACCESS_DENIED, url.path() );
|
||
15 years ago
|
else
|
||
12 years ago
|
error( TDEIO::ERR_DOES_NOT_EXIST, url.path() );
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
if ( S_ISDIR( buff.st_mode ) ) {
|
||
12 years ago
|
error( TDEIO::ERR_IS_DIRECTORY, url.path() );
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
if ( S_ISFIFO( buff.st_mode ) || S_ISSOCK ( buff.st_mode ) ) {
|
||
12 years ago
|
error( TDEIO::ERR_CANNOT_OPEN_FOR_READING, url.path() );
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
int fd = open( _path.data(), O_RDONLY);
|
||
|
if ( fd < 0 ) {
|
||
12 years ago
|
error( TDEIO::ERR_CANNOT_OPEN_FOR_READING, url.path() );
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
totalSize( buff.st_size );
|
||
|
int processed_size = 0;
|
||
|
|
||
|
char buffer[ MAX_IPC_SIZE ];
|
||
15 years ago
|
TQByteArray array;
|
||
15 years ago
|
|
||
|
while( 1 )
|
||
|
{
|
||
|
int n = ::read( fd, buffer, MAX_IPC_SIZE );
|
||
|
if (n == -1)
|
||
|
{
|
||
|
if (errno == EINTR)
|
||
|
continue;
|
||
12 years ago
|
error( TDEIO::ERR_COULD_NOT_READ, url.path());
|
||
15 years ago
|
close(fd);
|
||
|
return;
|
||
|
}
|
||
|
if (n == 0)
|
||
|
break; // Finished
|
||
|
|
||
|
array.setRawData(buffer, n);
|
||
|
data( array );
|
||
|
array.resetRawData(buffer, n);
|
||
|
|
||
|
processed_size += n;
|
||
|
processedSize( processed_size );
|
||
|
}
|
||
|
|
||
15 years ago
|
data( TQByteArray() );
|
||
15 years ago
|
|
||
|
close( fd );
|
||
|
|
||
|
processedSize( buff.st_size );
|
||
|
|
||
|
finished();
|
||
|
}
|