tools/assistant/lib/qhelp_global.h
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    43 #define QHELP_GLOBAL_H
    43 #define QHELP_GLOBAL_H
    44 
    44 
    45 #include <QtCore/qglobal.h>
    45 #include <QtCore/qglobal.h>
    46 #include <QtCore/QString>
    46 #include <QtCore/QString>
    47 #include <QtCore/QObject>
    47 #include <QtCore/QObject>
    48 #include <QtCore/QRegExp>
       
    49 #include <QtCore/QMutexLocker>
       
    50 #include <QtGui/QTextDocument>
       
    51 
    48 
    52 QT_BEGIN_HEADER
    49 QT_BEGIN_HEADER
    53 
    50 
    54 QT_BEGIN_NAMESPACE
    51 QT_BEGIN_NAMESPACE
    55 
    52 
    63 #   define QHELP_EXPORT Q_DECL_IMPORT
    60 #   define QHELP_EXPORT Q_DECL_IMPORT
    64 #endif
    61 #endif
    65 
    62 
    66 class QHelpGlobal {
    63 class QHelpGlobal {
    67 public:
    64 public:
    68     static QString uniquifyConnectionName(const QString &name, void *pointer)
    65     static QString uniquifyConnectionName(const QString &name, void *pointer);
    69     {
    66     static QString documentTitle(const QString &content);
    70         static int counter = 0;
    67     static QString codecFromData(const QByteArray &data);
    71         static QMutex mutex;
       
    72 
    68 
    73         QMutexLocker locker(&mutex);
    69 private:
    74         if (++counter > 1000)
    70     static QString codecFromHtmlData(const QByteArray &data);
    75             counter = 0;
    71     static QString codecFromXmlData(const QByteArray &data);
    76         
       
    77         return QString::fromLatin1("%1-%2-%3")
       
    78             .arg(name).arg(long(pointer)).arg(counter);
       
    79     };
       
    80 
       
    81     static QString documentTitle(const QString &content)
       
    82     {
       
    83         QString title = QObject::tr("Untitled");
       
    84         if (!content.isEmpty()) {
       
    85             int start = content.indexOf(QLatin1String("<title>"), 0, Qt::CaseInsensitive) + 7;
       
    86             int end = content.indexOf(QLatin1String("</title>"), 0, Qt::CaseInsensitive);
       
    87             if ((end - start) > 0) {
       
    88                 title = content.mid(start, end - start);
       
    89                 if (Qt::mightBeRichText(title) || title.contains(QLatin1Char('&'))) {
       
    90                     QTextDocument doc;
       
    91                     doc.setHtml(title);
       
    92                     title = doc.toPlainText();
       
    93                 }
       
    94             }
       
    95         }
       
    96         return title;
       
    97     };
       
    98 
       
    99     static QString charsetFromData(const QByteArray &data)
       
   100     {
       
   101         QString content = QString::fromUtf8(data.constData(), data.size());
       
   102         int start =
       
   103             content.indexOf(QLatin1String("<meta"), 0, Qt::CaseInsensitive);
       
   104         if (start > 0) {
       
   105             int end;
       
   106             QRegExp r(QLatin1String("charset=([^\"\\s]+)"));
       
   107             while (start != -1) {
       
   108                 end = content.indexOf(QLatin1Char('>'), start) + 1;
       
   109                 const QString &meta = content.mid(start, end - start).toLower();
       
   110                 if (r.indexIn(meta) != -1)
       
   111                     return r.cap(1);
       
   112                 start = content.indexOf(QLatin1String("<meta"), end,
       
   113                     Qt::CaseInsensitive);
       
   114             }
       
   115         }
       
   116         return QLatin1String("utf-8");
       
   117     }
       
   118 };
    72 };
   119 
    73 
   120 QT_END_NAMESPACE
    74 QT_END_NAMESPACE
   121 
    75 
   122 QT_END_HEADER
    76 QT_END_HEADER