tools/assistant/lib/qhelp_global.cpp
changeset 30 5dc02b23752f
parent 18 2f34d5167611
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
    37 **
    37 **
    38 ** $QT_END_LICENSE$
    38 ** $QT_END_LICENSE$
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
       
    42 #include <QtCore/QCoreApplication>
    42 #include <QtCore/QRegExp>
    43 #include <QtCore/QRegExp>
    43 #include <QtCore/QMutexLocker>
    44 #include <QtCore/QMutexLocker>
    44 #include <QtGui/QTextDocument>
    45 #include <QtGui/QTextDocument>
    45 
    46 
    46 #include "qhelp_global.h"
    47 #include "qhelp_global.h"
    53     QMutexLocker locker(&mutex);
    54     QMutexLocker locker(&mutex);
    54     if (++counter > 1000)
    55     if (++counter > 1000)
    55         counter = 0;
    56         counter = 0;
    56 
    57 
    57     return QString::fromLatin1("%1-%2-%3").
    58     return QString::fromLatin1("%1-%2-%3").
    58         arg(name).arg(long(pointer)).arg(counter);
    59         arg(name).arg(quintptr(pointer)).arg(counter);
    59 }
    60 }
    60 
    61 
    61 QString QHelpGlobal::documentTitle(const QString &content)
    62 QString QHelpGlobal::documentTitle(const QString &content)
    62 {
    63 {
    63     QString title = QObject::tr("Untitled");
    64     QString title = QCoreApplication::translate("QHelp", "Untitled");
    64     if (!content.isEmpty()) {
    65     if (!content.isEmpty()) {
    65         int start = content.indexOf(QLatin1String("<title>"), 0, Qt::CaseInsensitive) + 7;
    66         int start = content.indexOf(QLatin1String("<title>"), 0, Qt::CaseInsensitive) + 7;
    66         int end = content.indexOf(QLatin1String("</title>"), 0, Qt::CaseInsensitive);
    67         int end = content.indexOf(QLatin1String("</title>"), 0, Qt::CaseInsensitive);
    67         if ((end - start) > 0) {
    68         if ((end - start) > 0) {
    68             title = content.mid(start, end - start);
    69             title = content.mid(start, end - start);
    84     return codec.isEmpty() ? QLatin1String("utf-8") : codec;
    85     return codec.isEmpty() ? QLatin1String("utf-8") : codec;
    85 }
    86 }
    86 
    87 
    87 QString QHelpGlobal::codecFromHtmlData(const QByteArray &data)
    88 QString QHelpGlobal::codecFromHtmlData(const QByteArray &data)
    88 {
    89 {
    89     QString content = QString::fromUtf8(data.constData(), data.size());
    90     QString head = QString::fromUtf8(data.constData(), qMin(1000, data.size()));
    90     int start = content.indexOf(QLatin1String("<meta"), 0, Qt::CaseInsensitive);
    91     int start = head.indexOf(QLatin1String("<meta"), 0, Qt::CaseInsensitive);
    91     if (start > 0) {
    92     if (start > 0) {
    92         int end;
       
    93         QRegExp r(QLatin1String("charset=([^\"\\s]+)"));
    93         QRegExp r(QLatin1String("charset=([^\"\\s]+)"));
    94         while (start != -1) {
    94         while (start != -1) {
    95             end = content.indexOf(QLatin1Char('>'), start) + 1;
    95             const int end = head.indexOf(QLatin1Char('>'), start) + 1;
    96             const QString &meta = content.mid(start, end - start).toLower();
    96             if (end <= start)
       
    97                 break;
       
    98             const QString &meta = head.mid(start, end - start).toLower();
    97             if (r.indexIn(meta) != -1)
    99             if (r.indexIn(meta) != -1)
    98                 return r.cap(1);
   100                 return r.cap(1);
    99             start = content.indexOf(QLatin1String("<meta"), end,
   101             start = head.indexOf(QLatin1String("<meta"), end,
   100                                     Qt::CaseInsensitive);
   102                                     Qt::CaseInsensitive);
   101         }
   103         }
   102     }
   104     }
   103     return QString();
   105     return QString();
   104 }
   106 }
   105 
   107 
   106 QString QHelpGlobal::codecFromXmlData(const QByteArray &data)
   108 QString QHelpGlobal::codecFromXmlData(const QByteArray &data)
   107 {
   109 {
   108     QString content = QString::fromUtf8(data.constData(), data.size());
   110     QString head = QString::fromUtf8(data.constData(), qMin(1000, data.size()));
   109     const QRegExp encodingExp(QLatin1String("^\\s*<\\?xml version="
   111     const QRegExp encodingExp(QLatin1String("^\\s*<\\?xml version="
   110         "\"\\d\\.\\d\" encoding=\"([^\"]+)\"\\?>.*"));
   112         "\"\\d\\.\\d\" encoding=\"([^\"]+)\"\\?>.*"));
   111     return encodingExp.exactMatch(content) ? encodingExp.cap(1) : QString();
   113     return encodingExp.exactMatch(head) ? encodingExp.cap(1) : QString();
   112 }
   114 }