1 | #include "xslt_help.h" |
2 | |
3 | #include <docbookxslt.h> |
4 | |
5 | #include <libxml/catalog.h> |
6 | #include <libxml/parserInternals.h> |
7 | #include <libxml/xmlIO.h> |
8 | #include <libxslt/transform.h> |
9 | #include <libxslt/xsltInternals.h> |
10 | #include <libxslt/xsltconfig.h> |
11 | #include <libxslt/xsltutils.h> |
12 | |
13 | #include <QDateTime> |
14 | #include <QDir> |
15 | #include <QStandardPaths> |
16 | |
17 | #include <KCompressionDevice> |
18 | #include <QDebug> |
19 | |
20 | static bool readCache(const QString &filename, const QString &cache, QString &output) |
21 | { |
22 | // qDebug() << filename << cache; |
23 | if (!compareTimeStamps(older: filename, newer: cache)) { |
24 | return false; |
25 | } |
26 | if (!compareTimeStamps(older: KDocTools::locateFileInDtdResource(QStringLiteral("customization/kde-chunk.xsl" )), newer: cache)) { |
27 | return false; |
28 | } |
29 | |
30 | // qDebug() << "create filter"; |
31 | KCompressionDevice fd(cache); |
32 | |
33 | if (!fd.open(mode: QIODevice::ReadOnly)) { |
34 | QFile::remove(fileName: cache); |
35 | return false; |
36 | } |
37 | |
38 | // qDebug() << "reading"; |
39 | |
40 | char buffer[32000]; |
41 | int n; |
42 | QByteArray text; |
43 | // Also end loop in case of error, when -1 is returned |
44 | while ((n = fd.read(data: buffer, maxlen: 31900)) > 0) { |
45 | buffer[n] = 0; |
46 | text += buffer; |
47 | } |
48 | // qDebug() << "read " << text.length(); |
49 | fd.close(); |
50 | |
51 | output = QString::fromUtf8(ba: text); |
52 | |
53 | if (n == -1) { |
54 | return false; |
55 | } |
56 | |
57 | // qDebug() << "finished "; |
58 | |
59 | return true; |
60 | } |
61 | |
62 | QString lookForCache(const QString &filename) |
63 | { |
64 | // qDebug() << "lookForCache" << filename; |
65 | Q_ASSERT(filename.endsWith(QLatin1String(".docbook" ))); |
66 | Q_ASSERT(QDir::isAbsolutePath(filename)); |
67 | QString cache = filename.left(n: filename.length() - 7); |
68 | QString output; |
69 | if (readCache(filename, cache: cache + QLatin1String("cache.bz2" ), output)) { |
70 | return output; |
71 | } |
72 | #ifdef Q_OS_WIN |
73 | QFileInfo fi(filename); |
74 | // make sure filenames do not contain the base path, otherwise |
75 | // accessing user data from another location invalids cached files. |
76 | // Accessing user data under a different path is possible |
77 | // when using usb sticks - this may affect unix/mac systems also |
78 | const QString installPath = KDocTools::documentationDirs().last(); |
79 | cache = QLatin1Char('/') + fi.absolutePath().remove(installPath, Qt::CaseInsensitive).replace(QLatin1Char('/'), QLatin1Char('_')) + QLatin1Char('_') |
80 | + fi.baseName() + QLatin1Char('.'); |
81 | #endif |
82 | if (readCache(filename, |
83 | cache: QStandardPaths::writableLocation(type: QStandardPaths::GenericCacheLocation) + QLatin1String("/kio_help" ) + cache + QLatin1String("cache.bz2" ), |
84 | output)) { |
85 | return output; |
86 | } |
87 | |
88 | return QString(); |
89 | } |
90 | |
91 | bool compareTimeStamps(const QString &older, const QString &newer) |
92 | { |
93 | QFileInfo _older(older); |
94 | QFileInfo _newer(newer); |
95 | Q_ASSERT(_older.exists()); |
96 | if (!_newer.exists()) { |
97 | return false; |
98 | } |
99 | return (_newer.lastModified() > _older.lastModified()); |
100 | } |
101 | |