1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the Qt Assistant of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QtCore/QCoreApplication>
|
---|
43 | #include <QtCore/QRegExp>
|
---|
44 | #include <QtCore/QMutexLocker>
|
---|
45 | #include <QtGui/QTextDocument>
|
---|
46 |
|
---|
47 | #include "qhelp_global.h"
|
---|
48 |
|
---|
49 | QString QHelpGlobal::uniquifyConnectionName(const QString &name, void *pointer)
|
---|
50 | {
|
---|
51 | static int counter = 0;
|
---|
52 | static QMutex mutex;
|
---|
53 |
|
---|
54 | QMutexLocker locker(&mutex);
|
---|
55 | if (++counter > 1000)
|
---|
56 | counter = 0;
|
---|
57 |
|
---|
58 | return QString::fromLatin1("%1-%2-%3").
|
---|
59 | arg(name).arg(quintptr(pointer)).arg(counter);
|
---|
60 | }
|
---|
61 |
|
---|
62 | QString QHelpGlobal::documentTitle(const QString &content)
|
---|
63 | {
|
---|
64 | QString title = QCoreApplication::translate("QHelp", "Untitled");
|
---|
65 | if (!content.isEmpty()) {
|
---|
66 | int start = content.indexOf(QLatin1String("<title>"), 0, Qt::CaseInsensitive) + 7;
|
---|
67 | int end = content.indexOf(QLatin1String("</title>"), 0, Qt::CaseInsensitive);
|
---|
68 | if ((end - start) > 0) {
|
---|
69 | title = content.mid(start, end - start);
|
---|
70 | if (Qt::mightBeRichText(title) || title.contains(QLatin1Char('&'))) {
|
---|
71 | QTextDocument doc;
|
---|
72 | doc.setHtml(title);
|
---|
73 | title = doc.toPlainText();
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | return title;
|
---|
78 | }
|
---|
79 |
|
---|
80 | QString QHelpGlobal::codecFromData(const QByteArray &data)
|
---|
81 | {
|
---|
82 | QString codec = codecFromXmlData(data);
|
---|
83 | if (codec.isEmpty())
|
---|
84 | codec = codecFromHtmlData(data);
|
---|
85 | return codec.isEmpty() ? QLatin1String("utf-8") : codec;
|
---|
86 | }
|
---|
87 |
|
---|
88 | QString QHelpGlobal::codecFromHtmlData(const QByteArray &data)
|
---|
89 | {
|
---|
90 | QString head = QString::fromUtf8(data.constData(), qMin(1000, data.size()));
|
---|
91 | int start = head.indexOf(QLatin1String("<meta"), 0, Qt::CaseInsensitive);
|
---|
92 | if (start > 0) {
|
---|
93 | QRegExp r(QLatin1String("charset=([^\"\\s]+)"));
|
---|
94 | while (start != -1) {
|
---|
95 | const int end = head.indexOf(QLatin1Char('>'), start) + 1;
|
---|
96 | if (end <= start)
|
---|
97 | break;
|
---|
98 | const QString &meta = head.mid(start, end - start).toLower();
|
---|
99 | if (r.indexIn(meta) != -1)
|
---|
100 | return r.cap(1);
|
---|
101 | start = head.indexOf(QLatin1String("<meta"), end,
|
---|
102 | Qt::CaseInsensitive);
|
---|
103 | }
|
---|
104 | }
|
---|
105 | return QString();
|
---|
106 | }
|
---|
107 |
|
---|
108 | QString QHelpGlobal::codecFromXmlData(const QByteArray &data)
|
---|
109 | {
|
---|
110 | QString head = QString::fromUtf8(data.constData(), qMin(1000, data.size()));
|
---|
111 | const QRegExp encodingExp(QLatin1String("^\\s*<\\?xml version="
|
---|
112 | "\"\\d\\.\\d\" encoding=\"([^\"]+)\"\\?>.*"));
|
---|
113 | return encodingExp.exactMatch(head) ? encodingExp.cap(1) : QString();
|
---|
114 | }
|
---|