File indexing completed on 2024-05-05 03:56:06

0001 #include "xslt_help.h"
0002 
0003 #include <docbookxslt.h>
0004 
0005 #include <libxml/catalog.h>
0006 #include <libxml/parserInternals.h>
0007 #include <libxml/xmlIO.h>
0008 #include <libxslt/transform.h>
0009 #include <libxslt/xsltInternals.h>
0010 #include <libxslt/xsltconfig.h>
0011 #include <libxslt/xsltutils.h>
0012 
0013 #include <QDateTime>
0014 #include <QDir>
0015 #include <QStandardPaths>
0016 
0017 #include <KCompressionDevice>
0018 #include <QDebug>
0019 
0020 static bool readCache(const QString &filename, const QString &cache, QString &output)
0021 {
0022     // qDebug() << filename << cache;
0023     if (!compareTimeStamps(filename, cache)) {
0024         return false;
0025     }
0026     if (!compareTimeStamps(KDocTools::locateFileInDtdResource(QStringLiteral("customization/kde-chunk.xsl")), cache)) {
0027         return false;
0028     }
0029 
0030     // qDebug() << "create filter";
0031     KCompressionDevice fd(cache);
0032 
0033     if (!fd.open(QIODevice::ReadOnly)) {
0034         QFile::remove(cache);
0035         return false;
0036     }
0037 
0038     // qDebug() << "reading";
0039 
0040     char buffer[32000];
0041     int n;
0042     QByteArray text;
0043     // Also end loop in case of error, when -1 is returned
0044     while ((n = fd.read(buffer, 31900)) > 0) {
0045         buffer[n] = 0;
0046         text += buffer;
0047     }
0048     // qDebug() << "read " << text.length();
0049     fd.close();
0050 
0051     output = QString::fromUtf8(text);
0052 
0053     if (n == -1) {
0054         return false;
0055     }
0056 
0057     // qDebug() << "finished ";
0058 
0059     return true;
0060 }
0061 
0062 QString lookForCache(const QString &filename)
0063 {
0064     // qDebug() << "lookForCache" << filename;
0065     Q_ASSERT(filename.endsWith(QLatin1String(".docbook")));
0066     Q_ASSERT(QDir::isAbsolutePath(filename));
0067     QString cache = filename.left(filename.length() - 7);
0068     QString output;
0069     if (readCache(filename, cache + QLatin1String("cache.bz2"), output)) {
0070         return output;
0071     }
0072 #ifdef Q_OS_WIN
0073     QFileInfo fi(filename);
0074     // make sure filenames do not contain the base path, otherwise
0075     // accessing user data from another location invalids cached files.
0076     // Accessing user data under a different path is possible
0077     // when using usb sticks - this may affect unix/mac systems also
0078     const QString installPath = KDocTools::documentationDirs().last();
0079     cache = QLatin1Char('/') + fi.absolutePath().remove(installPath, Qt::CaseInsensitive).replace(QLatin1Char('/'), QLatin1Char('_')) + QLatin1Char('_')
0080         + fi.baseName() + QLatin1Char('.');
0081 #endif
0082     if (readCache(filename,
0083                   QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1String("/kio_help") + cache + QLatin1String("cache.bz2"),
0084                   output)) {
0085         return output;
0086     }
0087 
0088     return QString();
0089 }
0090 
0091 bool compareTimeStamps(const QString &older, const QString &newer)
0092 {
0093     QFileInfo _older(older);
0094     QFileInfo _newer(newer);
0095     Q_ASSERT(_older.exists());
0096     if (!_newer.exists()) {
0097         return false;
0098     }
0099     return (_newer.lastModified() > _older.lastModified());
0100 }