1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the Qt Assistant of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** 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 are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qhelpenginecore.h"
|
---|
43 | #include "qhelpsearchindexreader_default_p.h"
|
---|
44 |
|
---|
45 | #include <QtCore/QDir>
|
---|
46 | #include <QtCore/QUrl>
|
---|
47 | #include <QtCore/QFile>
|
---|
48 | #include <QtCore/QVariant>
|
---|
49 | #include <QtCore/QFileInfo>
|
---|
50 | #include <QtCore/QDataStream>
|
---|
51 | #include <QtCore/QTextStream>
|
---|
52 |
|
---|
53 | QT_BEGIN_NAMESPACE
|
---|
54 |
|
---|
55 | namespace qt {
|
---|
56 | namespace fulltextsearch {
|
---|
57 | namespace std {
|
---|
58 |
|
---|
59 | namespace {
|
---|
60 | QStringList split( const QString &str )
|
---|
61 | {
|
---|
62 | QStringList lst;
|
---|
63 | int j = 0;
|
---|
64 | int i = str.indexOf(QLatin1Char('*'), j );
|
---|
65 |
|
---|
66 | if (str.startsWith(QLatin1String("*")))
|
---|
67 | lst << QLatin1String("*");
|
---|
68 |
|
---|
69 | while ( i != -1 ) {
|
---|
70 | if ( i > j && i <= (int)str.length() ) {
|
---|
71 | lst << str.mid( j, i - j );
|
---|
72 | lst << QLatin1String("*");
|
---|
73 | }
|
---|
74 | j = i + 1;
|
---|
75 | i = str.indexOf(QLatin1Char('*'), j );
|
---|
76 | }
|
---|
77 |
|
---|
78 | int l = str.length() - 1;
|
---|
79 | if ( str.mid( j, l - j + 1 ).length() > 0 )
|
---|
80 | lst << str.mid( j, l - j + 1 );
|
---|
81 |
|
---|
82 | return lst;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | Reader::Reader()
|
---|
88 | : indexPath(QString())
|
---|
89 | , indexFile(QString())
|
---|
90 | , documentFile(QString())
|
---|
91 | {
|
---|
92 | termList.clear();
|
---|
93 | indexTable.clear();
|
---|
94 | searchIndexTable.clear();
|
---|
95 | }
|
---|
96 |
|
---|
97 | Reader::~Reader()
|
---|
98 | {
|
---|
99 | reset();
|
---|
100 | searchIndexTable.clear();
|
---|
101 | }
|
---|
102 |
|
---|
103 | bool Reader::readIndex()
|
---|
104 | {
|
---|
105 | if (indexTable.contains(indexFile))
|
---|
106 | return true;
|
---|
107 |
|
---|
108 | QFile idxFile(indexFile);
|
---|
109 | if (!idxFile.open(QFile::ReadOnly))
|
---|
110 | return false;
|
---|
111 |
|
---|
112 | QString key;
|
---|
113 | int numOfDocs;
|
---|
114 | EntryTable entryTable;
|
---|
115 | QVector<Document> docs;
|
---|
116 | QDataStream dictStream(&idxFile);
|
---|
117 | while (!dictStream.atEnd()) {
|
---|
118 | dictStream >> key;
|
---|
119 | dictStream >> numOfDocs;
|
---|
120 | docs.resize(numOfDocs);
|
---|
121 | dictStream >> docs;
|
---|
122 | entryTable.insert(key, new Entry(docs));
|
---|
123 | }
|
---|
124 | idxFile.close();
|
---|
125 |
|
---|
126 | if (entryTable.isEmpty())
|
---|
127 | return false;
|
---|
128 |
|
---|
129 | QFile docFile(documentFile);
|
---|
130 | if (!docFile.open(QFile::ReadOnly))
|
---|
131 | return false;
|
---|
132 |
|
---|
133 | QString title, url;
|
---|
134 | DocumentList documentList;
|
---|
135 | QDataStream docStream(&docFile);
|
---|
136 | while (!docStream.atEnd()) {
|
---|
137 | docStream >> title;
|
---|
138 | docStream >> url;
|
---|
139 | documentList.append(QStringList(title) << url);
|
---|
140 | }
|
---|
141 | docFile.close();
|
---|
142 |
|
---|
143 | if (documentList.isEmpty()) {
|
---|
144 | cleanupIndex(entryTable);
|
---|
145 | return false;
|
---|
146 | }
|
---|
147 |
|
---|
148 | indexTable.insert(indexFile, Index(entryTable, documentList));
|
---|
149 | return true;
|
---|
150 | }
|
---|
151 |
|
---|
152 | bool Reader::initCheck() const
|
---|
153 | {
|
---|
154 | return !searchIndexTable.isEmpty();
|
---|
155 | }
|
---|
156 |
|
---|
157 | void Reader::setIndexPath(const QString &path)
|
---|
158 | {
|
---|
159 | indexPath = path;
|
---|
160 | }
|
---|
161 |
|
---|
162 | void Reader::filterFilesForAttributes(const QStringList &attributes)
|
---|
163 | {
|
---|
164 | searchIndexTable.clear();
|
---|
165 | for(IndexTable::ConstIterator it = indexTable.begin(); it != indexTable.end(); ++it) {
|
---|
166 | const QString fileName = it.key();
|
---|
167 | bool containsAll = true;
|
---|
168 | QStringList split = fileName.split(QLatin1String("@"));
|
---|
169 | foreach (const QString attribute, attributes) {
|
---|
170 | if (!split.contains(attribute, Qt::CaseInsensitive)) {
|
---|
171 | containsAll = false;
|
---|
172 | break;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | if (containsAll)
|
---|
177 | searchIndexTable.insert(fileName, it.value());
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | void Reader::setIndexFile(const QString &namespaceName, const QString &attributes)
|
---|
182 | {
|
---|
183 | QString extention = namespaceName + QLatin1String("@") + attributes;
|
---|
184 | indexFile = indexPath + QLatin1String("/indexdb40.") + extention;
|
---|
185 | documentFile = indexPath + QLatin1String("/indexdoc40.") + extention;
|
---|
186 | }
|
---|
187 |
|
---|
188 | bool Reader::splitSearchTerm(const QString &searchTerm, QStringList *terms,
|
---|
189 | QStringList *termSeq, QStringList *seqWords)
|
---|
190 | {
|
---|
191 | QString term = searchTerm;
|
---|
192 |
|
---|
193 | term = term.simplified();
|
---|
194 | term = term.replace(QLatin1String("\'"), QLatin1String("\""));
|
---|
195 | term = term.replace(QLatin1String("`"), QLatin1String("\""));
|
---|
196 | term = term.replace(QLatin1String("-"), QLatin1String(" "));
|
---|
197 | term = term.replace(QRegExp(QLatin1String("\\s[\\S]?\\s")), QLatin1String(" "));
|
---|
198 |
|
---|
199 | *terms = term.split(QLatin1Char(' '));
|
---|
200 | QStringList::iterator it = terms->begin();
|
---|
201 | for (; it != terms->end(); ++it) {
|
---|
202 | (*it) = (*it).simplified();
|
---|
203 | (*it) = (*it).toLower();
|
---|
204 | (*it) = (*it).replace(QLatin1String("\""), QLatin1String(""));
|
---|
205 | }
|
---|
206 |
|
---|
207 | if (term.contains(QLatin1Char('\"'))) {
|
---|
208 | if ((term.count(QLatin1Char('\"')))%2 == 0) {
|
---|
209 | int beg = 0;
|
---|
210 | int end = 0;
|
---|
211 | QString s;
|
---|
212 | beg = term.indexOf(QLatin1Char('\"'), beg);
|
---|
213 | while (beg != -1) {
|
---|
214 | beg++;
|
---|
215 | end = term.indexOf(QLatin1Char('\"'), beg);
|
---|
216 | s = term.mid(beg, end - beg);
|
---|
217 | s = s.toLower();
|
---|
218 | s = s.simplified();
|
---|
219 | if (s.contains(QLatin1Char('*'))) {
|
---|
220 | qWarning("Full Text Search, using a wildcard within phrases is not allowed.");
|
---|
221 | return false;
|
---|
222 | }
|
---|
223 | *seqWords += s.split(QLatin1Char(' '));
|
---|
224 | *termSeq << s;
|
---|
225 | beg = term.indexOf(QLatin1Char('\"'), end + 1);
|
---|
226 | }
|
---|
227 | } else {
|
---|
228 | qWarning("Full Text Search, the closing quotation mark is missing.");
|
---|
229 | return false;
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | return true;
|
---|
234 | }
|
---|
235 |
|
---|
236 | void Reader::searchInIndex(const QStringList &terms)
|
---|
237 | {
|
---|
238 | foreach (const QString term, terms) {
|
---|
239 | QVector<Document> documents;
|
---|
240 |
|
---|
241 | for(IndexTable::ConstIterator it = searchIndexTable.begin();
|
---|
242 | it != searchIndexTable.end(); ++it) {
|
---|
243 | EntryTable entryTable = it.value().first;
|
---|
244 | DocumentList documentList = it.value().second;
|
---|
245 |
|
---|
246 | if (term.contains(QLatin1Char('*')))
|
---|
247 | documents = setupDummyTerm(getWildcardTerms(term, entryTable), entryTable);
|
---|
248 | else if (entryTable.value(term))
|
---|
249 | documents = entryTable.value(term)->documents;
|
---|
250 | else
|
---|
251 | continue;
|
---|
252 |
|
---|
253 | if (!documents.isEmpty()) {
|
---|
254 | DocumentInfo info;
|
---|
255 | QString title, url;
|
---|
256 | QVector<DocumentInfo> documentsInfo;
|
---|
257 | foreach(const Document doc, documents) {
|
---|
258 | info.docNumber = doc.docNumber;
|
---|
|
---|