1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
#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
#include <tqvaluelist.h>
#include <tqfileinfo.h>
#include <tqfile.h>
#include <tqtextstream.h>
#include <tqregexp.h>
#include <tqtextcodec.h>
#include <kdebug.h>
#include <kurl.h>
#include <kglobal.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include <kinstance.h>
#include "kio_help.h"
#include <libxslt/xsltutils.h>
#include <libxslt/transform.h>
#include "xslt.h"
using namespace KIO;
TQString HelpProtocol::langLookup(const TQString& fname)
{
TQStringList search;
// assemble the local search paths
const TQStringList localDoc = KGlobal::dirs()->resourceDirs("html") + KGlobal::dirs()->resourceDirs("html-bundle");
TQStringList langs = KGlobal::locale()->languageList();
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
for (TQStringList::Iterator it = langs.begin(); it != langs.end(); ++it)
if ( *it == "en_US" )
*it = "en";
// look up the different languages
int ldCount = localDoc.count();
for (int id=0; id < ldCount; id++)
{
TQStringList::ConstIterator lang;
for (lang = langs.begin(); lang != langs.end(); ++lang)
search.append(TQString("%1%2/%3").arg(localDoc[id], *lang, fname));
}
// try to locate the file
TQStringList::Iterator it;
for (it = search.begin(); it != search.end(); ++it)
{
kdDebug( 7119 ) << "Looking for help in: " << *it << endl;
TQFileInfo info(*it);
if (info.exists() && info.isFile() && info.isReadable())
return *it;
if ( ( *it ).right( 5 ) == ".html" )
{
TQString file = (*it).left((*it).tqfindRev('/')) + "/index.docbook";
kdDebug( 7119 ) << "Looking for help in: " << file << endl;
info.setFile(file);
if (info.exists() && info.isFile() && info.isReadable())
return *it;
}
}
return TQString::null;
}
TQString HelpProtocol::lookupFile(const TQString &fname,
const TQString &query, bool &redirect)
{
redirect = false;
TQString path, result;
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
{
tqunicodeError( i18n("There is no documentation available for %1." ).arg(path) );
finished();
return TQString::null;
}
} else
kdDebug( 7119 ) << "result " << result << endl;
return result;
}
void HelpProtocol::tqunicodeError( const TQString &t )
{
data(fromUnicode( TQString(
"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=%1\"></head>\n"
"%2</html>" ).arg( TQTextCodec::codecForLocale()->name() ).arg( t ) ) );
}
HelpProtocol *slave = 0;
HelpProtocol::HelpProtocol( bool ghelp, const TQCString &pool, const TQCString &app )
: 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;
TQString doc;
doc = url.path();
if ( !mGhelp ) {
if (doc.at(0) != '/')
doc = doc.prepend('/');
if (doc.at(doc.length() - 1) == '/')
doc += "index.html";
}
infoMessage(i18n("Looking up correct file"));
if ( !mGhelp ) {
doc = lookupFile(doc, url.query(), redirect);
if (redirect)
{
finished();
return;
}
}
if (doc.isEmpty())
{
error( KIO::ERR_DOES_NOT_EXIST, url.url() );
return;
}
mimeType("text/html");
KURL target;
target.setPath(doc);
if (url.hasHTMLRef())
target.setHTMLRef(url.htmlRef());
kdDebug( 7119 ) << "target " << target.url() << endl;
TQString file = target.path();
if ( mGhelp ) {
if ( file.right( 4 ) != ".xml" ) {
get_file( target );
return;
}
} else {
TQString docbook_file = file.left(file.tqfindRev('/')) + "/index.docbook";
if (!KStandardDirs::exists(file)) {
file = docbook_file;
} else {
TQFileInfo fi(file);
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 ) {
TQString xsl = "customization/kde-nochunk.xsl";
mParsed = transform(file, locate("dtd", xsl));
kdDebug( 7119 ) << "parsed " << mParsed.length() << endl;
if (mParsed.isEmpty()) {
tqunicodeError( i18n( "The requested help file could not be parsed:<br>%1" ).arg( file ) );
} else {
int pos1 = mParsed.tqfind( "charset=" );
if ( pos1 > 0 ) {
int pos2 = mParsed.tqfind( '"', pos1 );
if ( pos2 > 0 ) {
mParsed.replace( pos1, pos2 - pos1, "charset=UTF-8" );
}
}
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" ) );
TQString cache = file.left( file.length() - 7 );
saveToCache( mParsed, locateLocal( "cache",
"kio_help" + cache +
"cache.bz2" ) );
}
} else infoMessage( i18n( "Using cached version" ) );
kdDebug( 7119 ) << "parsed " << mParsed.length() << endl;
if (mParsed.isEmpty()) {
tqunicodeError( i18n( "The requested help file could not be parsed:<br>%1" ).arg( file ) );
} else {
TQString query = url.query(), anchor;
// if we have a query, look if it contains an anchor
if (!query.isEmpty())
if (query.left(8) == "?anchor=") {
anchor = query.mid(8).lower();
KURL redirURL(url);
redirURL.setQuery(TQString::null);
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 ) {
index = mParsed.tqfind( TQRegExp( "<a name=" ), index);
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() ==
TQString( "<a name=\"%1\">" ).arg( anchor ) )
{
index = mParsed.tqfindRev( "<FILENAME filename=", index ) +
strlen( "<FILENAME filename=\"" );
TQString filename=mParsed.mid( index, 2000 );
filename = filename.left( filename.tqfind( '\"' ) );
TQString path = target.path();
path = path.left( path.tqfindRev( '/' ) + 1) + filename;
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"));
TQString filename = url.path().mid(url.path().tqfindRev('/') + 1);
int index = mParsed.tqfind(TQString("<FILENAME filename=\"%1\"").arg(filename));
if (index == -1) {
if ( filename == "index.html" ) {
data( fromUnicode( mParsed ) );
return;
}
tqunicodeError( i18n("Could not find filename %1 in %2.").arg(filename).arg( url.url() ) );
return;
}
TQString filedata = splitOut(mParsed, index);
replaceCharsetHeader( filedata );
data( fromUnicode( filedata ) );
data( TQByteArray() );
}
void HelpProtocol::mimetype( const KURL &)
{
mimeType("text/html");
finished();
}
// Copied from kio_file to avoid redirects
#define MAX_IPC_SIZE (1024*32)
void HelpProtocol::get_file( const KURL& url )
{
kdDebug( 7119 ) << "get_file " << url.url() << endl;
TQCString _path( TQFile::encodeName(url.path()));
struct stat buff;
if ( ::stat( _path.data(), &buff ) == -1 ) {
if ( errno == EACCES )
error( KIO::ERR_ACCESS_DENIED, url.path() );
else
error( KIO::ERR_DOES_NOT_EXIST, url.path() );
return;
}
if ( S_ISDIR( buff.st_mode ) ) {
error( KIO::ERR_IS_DIRECTORY, url.path() );
return;
}
if ( S_ISFIFO( buff.st_mode ) || S_ISSOCK ( buff.st_mode ) ) {
error( KIO::ERR_CANNOT_OPEN_FOR_READING, url.path() );
return;
}
int fd = open( _path.data(), O_RDONLY);
if ( fd < 0 ) {
error( KIO::ERR_CANNOT_OPEN_FOR_READING, url.path() );
return;
}
totalSize( buff.st_size );
int processed_size = 0;
char buffer[ MAX_IPC_SIZE ];
TQByteArray array;
while( 1 )
{
int n = ::read( fd, buffer, MAX_IPC_SIZE );
if (n == -1)
{
if (errno == EINTR)
continue;
error( KIO::ERR_COULD_NOT_READ, url.path());
close(fd);
return;
}
if (n == 0)
break; // Finished
array.setRawData(buffer, n);
data( array );
array.resetRawData(buffer, n);
processed_size += n;
processedSize( processed_size );
}
data( TQByteArray() );
close( fd );
processedSize( buff.st_size );
finished();
}
|