00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "kio_help.h"
00024
00025 #include <config.h>
00026
00027 #ifdef HAVE_SYS_TYPES_H
00028 # include <sys/types.h>
00029 #endif
00030 #ifdef HAVE_SYS_STAT_H
00031 # include <sys/stat.h>
00032 #endif
00033
00034 #include <errno.h>
00035 #include <fcntl.h>
00036 #ifdef HAVE_STDIO_H
00037 # include <stdio.h>
00038 #endif
00039 #ifdef HAVE_STDLIB_H
00040 # include <stdlib.h>
00041 #endif
00042
00043 #include <QtCore/QFileInfo>
00044 #include <QtCore/QFile>
00045 #include <QtCore/QTextStream>
00046 #include <QtCore/QRegExp>
00047 #include <QtCore/QTextCodec>
00048 #include <QtGui/QTextDocument>
00049
00050 #include <kdebug.h>
00051 #include <kde_file.h>
00052 #include <kurl.h>
00053 #include <kglobal.h>
00054 #include <klocale.h>
00055 #include <kstandarddirs.h>
00056 #include <kcomponentdata.h>
00057
00058 #include <libxslt/xsltutils.h>
00059 #include <libxslt/transform.h>
00060 #include "xslt.h"
00061
00062 using namespace KIO;
00063
00064 QString HelpProtocol::langLookup(const QString &fname)
00065 {
00066 QStringList search;
00067
00068
00069 const QStringList localDoc = KGlobal::dirs()->resourceDirs("html");
00070
00071 QStringList langs = KGlobal::locale()->languageList();
00072 langs.append( "en" );
00073 langs.removeAll( "C" );
00074
00075
00076
00077 for (QStringList::Iterator it = langs.begin(); it != langs.end(); ++it)
00078 if ( *it == "en_US" )
00079 *it = "en";
00080
00081
00082 int ldCount = localDoc.count();
00083 for (int id=0; id < ldCount; id++)
00084 {
00085 QStringList::ConstIterator lang;
00086 for (lang = langs.begin(); lang != langs.end(); ++lang)
00087 search.append(QString("%1%2/%3").arg(localDoc[id], *lang, fname));
00088 }
00089
00090
00091 for (QStringList::Iterator it = search.begin(); it != search.end(); ++it)
00092 {
00093 kDebug( 7119 ) << "Looking for help in: " << *it;
00094
00095 QFileInfo info(*it);
00096 if (info.exists() && info.isFile() && info.isReadable())
00097 return *it;
00098
00099 if ( ( *it ).endsWith( ".html" ) )
00100 {
00101 QString file = (*it).left((*it).lastIndexOf('/')) + "/index.docbook";
00102 kDebug( 7119 ) << "Looking for help in: " << file;
00103 info.setFile(file);
00104 if (info.exists() && info.isFile() && info.isReadable())
00105 return *it;
00106 }
00107 }
00108
00109
00110 return QString();
00111 }
00112
00113
00114 QString HelpProtocol::lookupFile(const QString &fname,
00115 const QString &query, bool &redirect)
00116 {
00117 redirect = false;
00118
00119 const QString path = fname;
00120
00121 QString result = langLookup(path);
00122 if (result.isEmpty())
00123 {
00124 result = langLookup(path+"/index.html");
00125 if (!result.isEmpty())
00126 {
00127 KUrl red( "help:/" );
00128 red.setPath( path + "/index.html" );
00129 red.setQuery( query );
00130 redirection(red);
00131 kDebug( 7119 ) << "redirect to " << red.url();
00132 redirect = true;
00133 }
00134 else
00135 {
00136 unicodeError( i18n("There is no documentation available for %1." , Qt::escape(path)) );
00137 return QString();
00138 }
00139 } else
00140 kDebug( 7119 ) << "result " << result;
00141
00142 return result;
00143 }
00144
00145
00146 void HelpProtocol::unicodeError( const QString &t )
00147 {
00148 data(fromUnicode( QString(
00149 "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=%1\"></head>\n"
00150 "%2</html>" ).arg( QString( QTextCodec::codecForLocale()->name() ), t ) ) );
00151 }
00152
00153 HelpProtocol *slave = 0;
00154
00155 HelpProtocol::HelpProtocol( bool ghelp, const QByteArray &pool, const QByteArray &app )
00156 : SlaveBase( ghelp ? "ghelp" : "help", pool, app ), mGhelp( ghelp )
00157 {
00158 slave = this;
00159 }
00160
00161 void HelpProtocol::get( const KUrl& url )
00162 {
00163 kDebug( 7119 ) << "get: path=" << url.path()
00164 << " query=" << url.query() << endl;
00165
00166 bool redirect;
00167 QString doc;
00168 doc = url.path();
00169
00170 if ( !mGhelp ) {
00171 if (doc.at(0) != '/')
00172 doc = doc.prepend(QLatin1Char('/'));
00173
00174 if (doc.at(doc.length() - 1) == '/')
00175 doc += "index.html";
00176 }
00177
00178 infoMessage(i18n("Looking up correct file"));
00179
00180 if ( !mGhelp ) {
00181 doc = lookupFile(doc, url.query(), redirect);
00182
00183 if (redirect)
00184 {
00185 finished();
00186 return;
00187 }
00188 }
00189
00190 if (doc.isEmpty())
00191 {
00192 error( KIO::ERR_DOES_NOT_EXIST, url.url() );
00193 return;
00194 }
00195
00196 mimeType("text/html");
00197 KUrl target;
00198 target.setPath(doc);
00199 if (url.hasHTMLRef())
00200 target.setHTMLRef(url.htmlRef());
00201
00202 kDebug( 7119 ) << "target " << target.url();
00203
00204 QString file = target.path();
00205
00206 if ( mGhelp ) {
00207 if ( !file.endsWith( QLatin1String( ".xml" ) ) ) {
00208 get_file( target );
00209 return;
00210 }
00211 } else {
00212 QString docbook_file = file.left(file.lastIndexOf('/')) + "/index.docbook";
00213 if (!KStandardDirs::exists(file)) {
00214 file = docbook_file;
00215 } else {
00216 QFileInfo fi(file);
00217 if (fi.isDir()) {
00218 file = file + "/index.docbook";
00219 } else {
00220 if ( !file.endsWith( QLatin1String( ".html" ) ) || !compareTimeStamps( file, docbook_file ) ) {
00221 get_file( target );
00222 return;
00223 } else
00224 file = docbook_file;
00225 }
00226 }
00227 }
00228
00229 infoMessage(i18n("Preparing document"));
00230
00231 if ( mGhelp ) {
00232 QString xsl = "customization/kde-nochunk.xsl";
00233 mParsed = transform(file, KStandardDirs::locate("dtd", xsl));
00234
00235 kDebug( 7119 ) << "parsed " << mParsed.length();
00236
00237 if (mParsed.isEmpty()) {
00238 unicodeError( i18n( "The requested help file could not be parsed:<br />%1" , file ) );
00239 } else {
00240 int pos1 = mParsed.indexOf( "charset=" );
00241 if ( pos1 > 0 ) {
00242 int pos2 = mParsed.indexOf( '"', pos1 );
00243 if ( pos2 > 0 ) {
00244 mParsed.replace( pos1, pos2 - pos1, "charset=UTF-8" );
00245 }
00246 }
00247 data( mParsed.toUtf8() );
00248 }
00249 } else {
00250
00251 kDebug( 7119 ) << "look for cache for " << file;
00252
00253 mParsed = lookForCache( file );
00254
00255 kDebug( 7119 ) << "cached parsed " << mParsed.length();
00256
00257 if ( mParsed.isEmpty() ) {
00258 mParsed = transform(file, KStandardDirs::locate("dtd", "customization/kde-chunk.xsl"));
00259 if ( !mParsed.isEmpty() ) {
00260 infoMessage( i18n( "Saving to cache" ) );
00261 #ifdef Q_WS_WIN
00262 QFileInfo fi(file);
00263
00264
00265
00266
00267 QString cache = '/' + fi.absolutePath().remove(KStandardDirs::installPath("html"),Qt::CaseInsensitive).replace('/','_') + '_' + fi.baseName() + '.';
00268 #else
00269 QString cache = file.left( file.length() - 7 );
00270 #endif
00271 saveToCache( mParsed, KStandardDirs::locateLocal( "cache",
00272 "kio_help" + cache +
00273 "cache.bz2" ) );
00274 }
00275 } else infoMessage( i18n( "Using cached version" ) );
00276
00277 kDebug( 7119 ) << "parsed " << mParsed.length();
00278
00279 if (mParsed.isEmpty()) {
00280 unicodeError( i18n( "The requested help file could not be parsed:<br />%1" , file ) );
00281 } else {
00282 QString query = url.query(), anchor;
00283
00284
00285 if (!query.isEmpty())
00286 if (query.startsWith("?anchor=")) {
00287 anchor = query.mid(8).toLower();
00288
00289 KUrl redirURL(url);
00290
00291 redirURL.setQuery(QString());
00292 redirURL.setHTMLRef(anchor);
00293 redirection(redirURL);
00294 finished();
00295 return;
00296 }
00297 if (anchor.isEmpty() && url.hasHTMLRef())
00298 anchor = url.htmlRef();
00299
00300 kDebug( 7119 ) << "anchor: " << anchor;
00301
00302 if ( !anchor.isEmpty() )
00303 {
00304 int index = 0;
00305 while ( true ) {
00306 index = mParsed.indexOf( QRegExp( "<a name=" ), index);
00307 if ( index == -1 ) {
00308 kDebug( 7119 ) << "no anchor\n";
00309 break;
00310 }
00311
00312 if ( mParsed.mid( index, 11 + anchor.length() ).toLower() ==
00313 QString( "<a name=\"%1\">" ).arg( anchor ) )
00314 {
00315 index = mParsed.lastIndexOf( "<FILENAME filename=", index ) +
00316 strlen( "<FILENAME filename=\"" );
00317 QString filename=mParsed.mid( index, 2000 );
00318 filename = filename.left( filename.indexOf( '\"' ) );
00319 QString path = target.path();
00320 path = path.left( path.lastIndexOf( '/' ) + 1) + filename;
00321 kDebug( 7119 ) << "anchor found in " << path;
00322 target.setPath( path );
00323 break;
00324 }
00325 index++;
00326 }
00327 }
00328 emitFile( target );
00329 }
00330 }
00331
00332 finished();
00333 }
00334
00335 void HelpProtocol::emitFile( const KUrl& url )
00336 {
00337 infoMessage(i18n("Looking up section"));
00338
00339 QString filename = url.path().mid(url.path().lastIndexOf('/') + 1);
00340
00341 int index = mParsed.indexOf(QString("<FILENAME filename=\"%1\"").arg(filename));
00342 if (index == -1) {
00343 if ( filename == "index.html" ) {
00344 data( fromUnicode( mParsed ) );
00345 return;
00346 }
00347
00348 unicodeError( i18n("Could not find filename %1 in %2.", filename, url.url() ) );
00349 return;
00350 }
00351
00352 QString filedata = splitOut(mParsed, index);
00353 replaceCharsetHeader( filedata );
00354
00355 data( fromUnicode( filedata ) );
00356 data( QByteArray() );
00357 }
00358
00359 void HelpProtocol::mimetype( const KUrl &)
00360 {
00361 mimeType("text/html");
00362 finished();
00363 }
00364
00365
00366
00367 #define MAX_IPC_SIZE (1024*32)
00368
00369 void HelpProtocol::get_file( const KUrl& url )
00370 {
00371 kDebug( 7119 ) << "get_file " << url.url();
00372
00373 #ifdef Q_WS_WIN
00374 QFile f( url.path() );
00375 if ( !f.exists() ) {
00376 error( KIO::ERR_DOES_NOT_EXIST, url.url() );
00377 return;
00378 }
00379 if ( !f.open(QIODevice::ReadOnly) ) {
00380 error( KIO::ERR_CANNOT_OPEN_FOR_READING, url.path() );
00381 return;
00382 }
00383 int processed_size = 0;
00384 totalSize( f.size() );
00385
00386 QByteArray array;
00387 array.resize(MAX_IPC_SIZE);
00388
00389 while( 1 )
00390 {
00391 qint64 n = f.read(array.data(),array.size());
00392 if (n == -1) {
00393 error( KIO::ERR_COULD_NOT_READ, url.path());
00394 f.close();
00395 return;
00396 }
00397 if (n == 0)
00398 break;
00399
00400 data( array );
00401
00402 processed_size += n;
00403 processedSize( processed_size );
00404 }
00405
00406 data( QByteArray() );
00407 f.close();
00408
00409 processedSize( f.size() );
00410 finished();
00411 #else
00412 QByteArray _path( QFile::encodeName(url.path()));
00413 KDE_struct_stat buff;
00414 if ( KDE_stat( _path.data(), &buff ) == -1 ) {
00415 if ( errno == EACCES )
00416 error( KIO::ERR_ACCESS_DENIED, url.url() );
00417 else
00418 error( KIO::ERR_DOES_NOT_EXIST, url.url() );
00419 return;
00420 }
00421
00422 if ( S_ISDIR( buff.st_mode ) ) {
00423 error( KIO::ERR_IS_DIRECTORY, url.path() );
00424 return;
00425 }
00426 if ( S_ISFIFO( buff.st_mode ) || S_ISSOCK ( buff.st_mode ) ) {
00427 error( KIO::ERR_CANNOT_OPEN_FOR_READING, url.path() );
00428 return;
00429 }
00430
00431 int fd = KDE_open( _path.data(), O_RDONLY);
00432 if ( fd < 0 ) {
00433 error( KIO::ERR_CANNOT_OPEN_FOR_READING, url.path() );
00434 return;
00435 }
00436
00437 totalSize( buff.st_size );
00438 int processed_size = 0;
00439
00440 char buffer[ MAX_IPC_SIZE ];
00441 QByteArray array;
00442
00443 while( 1 )
00444 {
00445 int n = ::read( fd, buffer, MAX_IPC_SIZE );
00446 if (n == -1)
00447 {
00448 if (errno == EINTR)
00449 continue;
00450 error( KIO::ERR_COULD_NOT_READ, url.path());
00451 ::close(fd);
00452 return;
00453 }
00454 if (n == 0)
00455 break;
00456
00457 array = array.fromRawData(buffer, n);
00458 data( array );
00459 array = array.fromRawData(buffer, n);
00460
00461 processed_size += n;
00462 processedSize( processed_size );
00463 }
00464
00465 data( QByteArray() );
00466
00467 ::close( fd );
00468
00469 processedSize( buff.st_size );
00470
00471 finished();
00472 #endif
00473 }