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 "qhelpsearchengine.h"
|
---|
44 | #include "qhelpsearchquerywidget.h"
|
---|
45 | #include "qhelpsearchresultwidget.h"
|
---|
46 |
|
---|
47 | #if defined(QT_CLUCENE_SUPPORT)
|
---|
48 | # include "qhelpsearchindexreader_clucene_p.h"
|
---|
49 | # include "qhelpsearchindexwriter_clucene_p.h"
|
---|
50 | #else
|
---|
51 | # include "qhelpsearchindexreader_default_p.h"
|
---|
52 | # include "qhelpsearchindexwriter_default_p.h"
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | #include <QtCore/QDir>
|
---|
56 | #include <QtCore/QFile>
|
---|
57 | #include <QtCore/QFileInfo>
|
---|
58 | #include <QtCore/QVariant>
|
---|
59 | #include <QtCore/QThread>
|
---|
60 | #include <QtCore/QPointer>
|
---|
61 |
|
---|
62 | QT_BEGIN_NAMESPACE
|
---|
63 |
|
---|
64 | #if defined(QT_CLUCENE_SUPPORT)
|
---|
65 | using namespace qt::fulltextsearch::clucene;
|
---|
66 | #else
|
---|
67 | using namespace qt::fulltextsearch::std;
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | class QHelpSearchEnginePrivate : public QObject
|
---|
71 | {
|
---|
72 | Q_OBJECT
|
---|
73 |
|
---|
74 | signals:
|
---|
75 | void indexingStarted();
|
---|
76 | void indexingFinished();
|
---|
77 |
|
---|
78 | void searchingStarted();
|
---|
79 | void searchingFinished(int hits);
|
---|
80 |
|
---|
81 | private:
|
---|
82 | QHelpSearchEnginePrivate(QHelpEngineCore *helpEngine)
|
---|
83 | : queryWidget(0)
|
---|
84 | , resultWidget(0)
|
---|
85 | , helpEngine(helpEngine)
|
---|
86 | {
|
---|
87 | hitList.clear();
|
---|
88 | indexReader = 0;
|
---|
89 | indexWriter = 0;
|
---|
90 | }
|
---|
91 |
|
---|
92 | ~QHelpSearchEnginePrivate()
|
---|
93 | {
|
---|
94 | hitList.clear();
|
---|
95 | delete indexReader;
|
---|
96 | delete indexWriter;
|
---|
97 | }
|
---|
98 |
|
---|
99 | int hitsCount() const
|
---|
100 | {
|
---|
101 | int count = 0;
|
---|
102 | if (indexReader)
|
---|
103 | count = indexReader->hitsCount();
|
---|
104 |
|
---|
105 | return count;
|
---|
106 | }
|
---|
107 |
|
---|
108 | QList<QHelpSearchEngine::SearchHit> hits(int start, int end) const
|
---|
109 | {
|
---|
110 | QList<QHelpSearchEngine::SearchHit> returnValue;
|
---|
111 | if (indexReader) {
|
---|
112 | for (int i = start; i < end && i < hitsCount(); ++i)
|
---|
113 | returnValue.append(indexReader->hit(i));
|
---|
114 | }
|
---|
115 | return returnValue;
|
---|
116 | }
|
---|
117 |
|
---|
118 | void updateIndex(bool reindex = false)
|
---|
119 | {
|
---|
120 | if (helpEngine.isNull())
|
---|
121 | return;
|
---|
122 |
|
---|
123 | if (!QFile::exists(QFileInfo(helpEngine->collectionFile()).path()))
|
---|
124 | return;
|
---|
125 |
|
---|
126 | if (!indexWriter) {
|
---|
127 | indexWriter = new QHelpSearchIndexWriter();
|
---|
128 |
|
---|
129 | connect(indexWriter, SIGNAL(indexingStarted()), this, SIGNAL(indexingStarted()));
|
---|
130 | connect(indexWriter, SIGNAL(indexingFinished()), this, SIGNAL(indexingFinished()));
|
---|
131 | connect(indexWriter, SIGNAL(indexingFinished()), this, SLOT(optimizeIndex()));
|
---|
132 | }
|
---|
133 |
|
---|
134 | if (indexWriter) {
|
---|
135 | indexWriter->cancelIndexing();
|
---|
136 | indexWriter->updateIndex(helpEngine->collectionFile(),
|
---|
137 | indexFilesFolder(), reindex);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | void cancelIndexing()
|
---|
142 | {
|
---|
143 | if (indexWriter)
|
---|
144 | indexWriter->cancelIndexing();
|
---|
145 | }
|
---|
146 |
|
---|
147 | void search(const QList<QHelpSearchQuery> &queryList)
|
---|
148 | {
|
---|
149 | if (helpEngine.isNull())
|
---|
150 | return;
|
---|
151 |
|
---|
152 | if (!QFile::exists(QFileInfo(helpEngine->collectionFile()).path()))
|
---|
153 | return;
|
---|
154 |
|
---|
155 | if (!indexReader) {
|
---|
156 | indexReader = new QHelpSearchIndexReader();
|
---|
157 |
|
---|
158 | connect(indexReader, SIGNAL(searchingStarted()), this, SIGNAL(searchingStarted()));
|
---|
159 | connect(indexReader, SIGNAL(searchingFinished(int)), this, SIGNAL(searchingFinished(int)));
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (indexReader) {
|
---|
163 | m_queryList = queryList;
|
---|
164 | indexReader->cancelSearching();
|
---|
165 | indexReader->search(helpEngine->collectionFile(), indexFilesFolder(), queryList);
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | void cancelSearching()
|
---|
170 | {
|
---|
171 | if (indexReader)
|
---|
172 | indexReader->cancelSearching();
|
---|
173 | }
|
---|
174 |
|
---|
175 | QString indexFilesFolder() const
|
---|
176 | {
|
---|
177 | QString indexFilesFolder = QLatin1String(".fulltextsearch");
|
---|
178 | if (helpEngine && !helpEngine->collectionFile().isEmpty()) {
|
---|
179 | QFileInfo fi(helpEngine->collectionFile());
|
---|
180 | indexFilesFolder = fi.absolutePath() + QDir::separator()
|
---|
181 | + QLatin1Char('.')
|
---|
182 | + fi.fileName().left(fi.fileName().lastIndexOf(QLatin1String(".qhc")));
|
---|
183 | }
|
---|
184 | return indexFilesFolder;
|
---|
185 | }
|
---|
186 |
|
---|
187 | private slots:
|
---|
188 | void optimizeIndex()
|
---|
189 | {
|
---|
190 | #if defined(QT_CLUCENE_SUPPORT)
|
---|
191 | if (indexWriter && !helpEngine.isNull()) {
|
---|
192 | indexWriter->optimizeIndex();
|
---|
193 | }
|
---|
194 | #endif
|
---|
195 | }
|
---|
196 |
|
---|
197 | private:
|
---|
198 | friend class QHelpSearchEngine;
|
---|
199 |
|
---|
200 | QHelpSearchQueryWidget *queryWidget;
|
---|
201 | QHelpSearchResultWidget *resultWidget;
|
---|
202 |
|
---|
203 | QHelpSearchIndexReader *indexReader;
|
---|
204 | QHelpSearchIndexWriter *indexWriter;
|
---|
205 |
|
---|
206 | QPointer<QHelpEngineCore> helpEngine;
|
---|
207 | QList<QHelpSearchEngine::SearchHit> hitList;
|
---|
208 |
|
---|
209 | QList<QHelpSearchQuery> m_queryList;
|
---|
210 | };
|
---|
211 |
|
---|
212 | #include "qhelpsearchengine.moc"
|
---|
213 |
|
---|
214 |
|
---|
215 | /*!
|
---|
216 | \class QHelpSearchQuery
|
---|
217 | \since 4.4
|
---|
218 | \inmodule QtHelp
|
---|
219 | \brief The QHelpSearchQuery class contains the field name and the associated
|
---|
220 | search term
|
---|
221 |
|
---|
222 | The QHelpSearchQuery class contains the field name and the associated search
|
---|
223 | term. Depending on the field the search term might get split up into seperate
|
---|
224 | terms to be parsed differently by the search engine.
|
---|
225 |
|
---|
226 | \sa QHelpSearchQueryWidget
|
---|
227 | */
|
---|
228 |
|
---|
229 | /*!
|
---|
230 | \fn QHelpSearchQuery::QHelpSearchQuery()
|
---|
231 |
|
---|
232 | Constructs a new empty QHelpSearchQuery.
|
---|
233 | */
|
---|
234 |
|
---|
235 | /*!
|
---|
236 | \fn QHelpSearchQuery::QHelpSearchQuery(FieldName field, const QStringList &wordList)
|
---|
237 |
|
---|
238 | Constructs a new QHelpSearchQuery and initializes it with the given \a field and \a wordList.
|
---|
239 | */
|
---|
240 |
|
---|
241 | /*!
|
---|
242 | \enum QHelpSearchQuery::FieldName
|
---|
243 | This enum type specifies the field names that are handled by the search engine.
|
---|
244 |
|
---|
245 | \value DEFAULT the default field provided by the search widget, several terms should be
|
---|
246 | splitted and stored in the wordlist except search terms enclosed in quotes.
|
---|
247 | \value FUZZY a field only provided in use with clucene. Terms should be split in seperate
|
---|
248 | words and passed to the search engine.
|
---|
249 | \value WITHOUT a field only provided in use with clucene. Terms should be split in seperate
|
---|
250 | words and passed to the search engine.
|
---|
251 | \value PHRASE a field only provided in use with clucene. Terms should not be split in seperate
|
---|
252 | words.
|
---|
253 | \value ALL a field only provided in use with clucene. Terms should be split in seperate
|
---|
254 | words and passed to the search engine
|
---|
255 | \value ATLEAST a field only provided in use with clucene. Terms should be split in seperate
|
---|
256 | words and passed to the search engine
|
---|
257 | */
|
---|
258 |
|
---|
259 | /*!
|
---|
260 | \class QHelpSearchEngine
|
---|
261 | \since 4.4
|
---|
262 | \inmodule QtHelp
|
---|
263 | \brief The QHelpSearchEngine class provides access to widgets reusable
|
---|
264 | to integrate fulltext search as well as to index and search documentation.
|
---|
265 |
|
---|
266 | Before the search engine can be used, one has to instantiate at least a
|
---|
267 | QHelpEngineCore object that needs to be passed to the search engines constructor.
|
---|
|
---|