source: trunk/tools/assistant/lib/qhelpsearchengine.cpp@ 109

Last change on this file since 109 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 13.7 KB
Line 
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
62QT_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
70class QHelpSearchEnginePrivate : public QObject
71{
72 Q_OBJECT
73
74signals:
75 void indexingStarted();
76 void indexingFinished();
77
78 void searchingStarted();
79 void searchingFinished(int hits);
80
81private:
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
187private slots:
188 void optimizeIndex()
189 {