1#include "xslt_help.h"
2#include "xslt.h"
3
4#include <libxslt/xsltconfig.h>
5#include <libxslt/xsltInternals.h>
6#include <libxslt/transform.h>
7#include <libxslt/xsltutils.h>
8#include <libxml/xmlIO.h>
9#include <libxml/parserInternals.h>
10#include <libxml/catalog.h>
11#include <kdebug.h>
12#include <kstandarddirs.h>
13#include <QtCore/QDate>
14#include <QtCore/QDir>
15#include <QtCore/QRegExp>
16#include <kcomponentdata.h>
17#include <klocale.h>
18#include <assert.h>
19#include <kfilterbase.h>
20#include <kfilterdev.h>
21#include <QtCore/QTextCodec>
22#include <stdlib.h>
23#include <config.h>
24#include <stdarg.h>
25#include <kcharsets.h>
26#include <kurl.h>
27
28
29static bool readCache( const QString &filename,
30 const QString &cache, QString &output)
31{
32 kDebug( 7119 ) << filename << " " << cache;
33 KGlobal::dirs()->addResourceType("dtd", "data", "ksgmltools2/");
34 if ( !compareTimeStamps( filename, cache ) )
35 return false;
36 if ( !compareTimeStamps( KStandardDirs::locate( "dtd", "customization/kde-chunk.xsl"), cache ) )
37 return false;
38
39 kDebug( 7119 ) << "create filter";
40 QIODevice *fd = ::getBZip2device(cache);
41 if ( !fd )
42 return false;
43
44 if (!fd->open(QIODevice::ReadOnly))
45 {
46 delete fd;
47 QFile::remove(cache);
48 return false;
49 }
50
51 kDebug( 7119 ) << "reading";
52
53 char buffer[32000];
54 int n;
55 QByteArray text;
56 // Also end loop in case of error, when -1 is returned
57 while ( ( n = fd->read(buffer, 31900) ) > 0)
58 {
59 buffer[n] = 0;
60 text += buffer;
61 }
62 kDebug( 7119 ) << "read " << text.length();
63 fd->close();
64
65 output = QString::fromUtf8( text );
66 delete fd;
67
68 if (n == -1)
69 return false;
70
71 kDebug( 7119 ) << "finished ";
72
73 return true;
74}
75
76QString lookForCache( const QString &filename )
77{
78 kDebug() << "lookForCache " << filename;
79 assert( filename.endsWith( QLatin1String(".docbook") ) );
80 assert( QDir::isAbsolutePath(filename));
81 QString cache = filename.left( filename.length() - 7 );
82 QString output;
83 if ( readCache( filename, cache + "cache.bz2", output) )
84 return output;
85#ifdef Q_WS_WIN
86 QFileInfo fi(filename);
87 // make sure filenames do not contain the base path, otherwise
88 // accessing user data from another location invalids cached files.
89 // Accessing user data under a different path is possible
90 // when using usb sticks - this may affect unix/mac systems also
91 cache = '/' + fi.absolutePath().remove(KStandardDirs::installPath("html"),Qt::CaseInsensitive).replace('/','_') + '_' + fi.baseName() + '.';
92#endif
93 if ( readCache( filename,
94 KStandardDirs::locateLocal( "cache",
95 "kio_help" + cache +
96 "cache.bz2" ), output ) )
97 return output;
98
99 return QString();
100}
101
102bool compareTimeStamps( const QString &older, const QString &newer )
103{
104 QFileInfo _older( older );
105 QFileInfo _newer( newer );
106 assert( _older.exists() );
107 if ( !_newer.exists() )
108 return false;
109 return ( _newer.lastModified() > _older.lastModified() );
110}
111