File: searchengine.cpp

package info (click to toggle)
khelpcenter 4:18.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,720 kB
  • sloc: cpp: 5,955; xml: 249; perl: 86; makefile: 5; sh: 3
file content (363 lines) | stat: -rw-r--r-- 9,562 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363

#include "searchengine.h"

#include "docmetainfo.h"
#include "view.h"
#include "searchhandler.h"
#include "khc_debug.h"
#include "grantleeformatter.h"

#include <prefs.h>

namespace KHC
{

SearchTraverser::SearchTraverser( SearchEngine *engine, int level ) :
  mMaxLevel( 999 ), mEngine( engine), mLevel( level ), mResultsPtr( &mResults )
{
#if 0
  khcDebug() << "SearchTraverser():" << mLevel << this;
#endif
}

SearchTraverser::~SearchTraverser()
{
#if 0
    khcDebug() << "~SearchTraverser():" << mLevel << this;
#endif
}

void SearchTraverser::process( DocEntry * )
{
  khcDebug() << "SearchTraverser::process()";
}

void SearchTraverser::startProcess( DocEntry *entry )
{
//  khcDebug() << "SearchTraverser::startProcess():" << entry->name()
//    << "SEARCH:" << entry->search();

  if ( !mEngine->canSearch( entry ) || !entry->searchEnabled() ) {
    mNotifyee->endProcess( entry, this );
    return;
  }

//  khcDebug() << "SearchTraverser::startProcess():" << entry->identifier();

  SearchHandler *handler = mEngine->handler( entry->documentType() );

  if ( !handler ) {
    QString txt;
    if ( entry->documentType().isEmpty() ) {
      txt = i18n("Error: No document type specified.");
    } else {
      txt = i18n("Error: No search handler for document type '%1'.",
          entry->documentType() );
    }
    showSearchError( handler, entry, txt );
    return;
  }

  connectHandler( handler );

  handler->search( entry, mEngine->words(), mEngine->maxResults(),
    mEngine->operation() );

//  khcDebug() << "SearchTraverser::startProcess() done: " << entry->name();
}

void SearchTraverser::connectHandler( SearchHandler *handler )
{
  QMap<SearchHandler *,int>::Iterator it;
  it = mConnectCount.find( handler );
  int count = 0;
  if ( it != mConnectCount.end() ) count = *it;
  if ( count == 0 ) {
    connect( handler, SIGNAL( searchError( SearchHandler *, DocEntry *, const QString & ) ),
      SLOT( showSearchError( SearchHandler *, DocEntry *, const QString & ) ) );
    connect( handler, SIGNAL( searchFinished( SearchHandler *, DocEntry *, const QString & ) ),
      SLOT( showSearchResult( SearchHandler *, DocEntry *, const QString & ) ) );
  }
  mConnectCount[ handler ] = ++count;
}

void SearchTraverser::disconnectHandler( SearchHandler *handler )
{
  QMap<SearchHandler *,int>::Iterator it;
  it = mConnectCount.find( handler );
  if ( it == mConnectCount.end() ) {
    khcWarning() << "SearchTraverser::disconnectHandler() handler not connected.";
  } else {
    int count = *it;
    --count;
    if ( count == 0 ) {
      disconnect( handler, SIGNAL( searchError( SearchHandler *, DocEntry *, const QString & ) ),
        this, SLOT( showSearchError( SearchHandler *, DocEntry *, const QString & ) ) );
      disconnect( handler, SIGNAL( searchFinished( SearchHandler *, DocEntry *, const QString & ) ),
        this, SLOT( showSearchResult( SearchHandler *, DocEntry *, const QString & ) ) );
    }
    mConnectCount[ handler ] = count;
  }
}

DocEntryTraverser *SearchTraverser::createChild( DocEntry *parentEntry )
{
//  khcDebug() << "SearchTraverser::createChild() level " << mLevel;

  if ( mLevel >= mMaxLevel ) {
    ++mLevel;
    return this;
  } else {
    SearchTraverser *t = new SearchTraverser( mEngine, mLevel + 1 );
    t->mResultsPtr = mResultsPtr;
    t->setParentEntry( parentEntry );
    return t;
  }
}

DocEntryTraverser *SearchTraverser::parentTraverser()
{
//  khcDebug() << "SearchTraverser::parentTraverser(): level: " << mLevel;

  if ( mLevel > mMaxLevel ) {
    return this;
  } else {
    return mParent;
  }
}

void SearchTraverser::deleteTraverser()
{
//  khcDebug() << "SearchTraverser::deleteTraverser()";

  if ( mLevel > mMaxLevel ) {
    --mLevel;
  } else {
    delete this;
  }
}

void SearchTraverser::showSearchError( SearchHandler *handler, DocEntry *entry, const QString &error )
{
//  khcDebug() << "SearchTraverser::showSearchError():" << entry->name();

  mResultsPtr->append( qMakePair( entry, error ) );

  mEngine->logError( entry, error );

  disconnectHandler( handler );

  mNotifyee->endProcess( entry, this );
}

void SearchTraverser::showSearchResult( SearchHandler *handler, DocEntry *entry, const QString &result )
{
//  khcDebug() << "SearchTraverser::showSearchResult():" << entry->name();

  mResultsPtr->append( qMakePair( entry, result ) );

  disconnectHandler( handler );

  mNotifyee->endProcess( entry, this );
}

void SearchTraverser::finishTraversal()
{
//  khcDebug() << "SearchTraverser::finishTraversal()";

  mEngine->view()->beginSearchResult();
  mEngine->view()->writeSearchResult( mEngine->grantleeFormatter()->formatSearchResults( mWords, mResults ) );
  mEngine->view()->endSearchResult();

  mEngine->finishSearch();
}

void SearchTraverser::setWords( const QString &words )
{
  mWords = words;
}


SearchEngine::SearchEngine( View *destination )
  : QObject(),
    mSearchRunning( false ), mView( destination ),
    mRootTraverser( 0 )
{
}

SearchEngine::~SearchEngine()
{
  delete mRootTraverser;
}

bool SearchEngine::initSearchHandlers()
{
  const QStringList resourceDirs = QStandardPaths::locateAll(QStandardPaths::DataLocation, "searchhandlers/", QStandardPaths::LocateDirectory );
  QStringList resources;
  foreach(const QString& dir, resourceDirs) {
      QDir d(dir);
    foreach ( const QString& entry, d.entryList( QStringList( "*.desktop" ), QDir::Files ) ) {
      resources += dir + entry;
    }
  }

  QStringList::ConstIterator it;
  for( it = resources.constBegin(); it != resources.constEnd(); ++it ) {
    QString filename = *it;
    khcDebug() << "SearchEngine::initSearchHandlers(): " << filename;
    QSharedPointer<SearchHandler> handler( SearchHandler::initFromFile( filename ) );
    if ( !handler ) {
      khcWarning() << "Unable to initialize SearchHandler from" << filename;
    } else {
      QStringList documentTypes = handler->documentTypes();
      QStringList::ConstIterator it;
      for( it = documentTypes.constBegin(); it != documentTypes.constEnd(); ++it ) {
        // only register "handler" for this content type if there is no
        // handler set already; since we read more specific directories
        // first, then those will override whatever is in more global
        // ones
        if ( !mHandlers.contains( *it ) ) {
          mHandlers.insert( *it, handler );
        }
      }
    }
  }

  if ( mHandlers.isEmpty() ) {
    QString txt = i18n("No valid search handler found.");
    khcWarning() << txt;
//    KMessageBox::sorry( mView->widget(), txt );
    return false;
  }

  return true;
}

bool SearchEngine::search( const QString & words, const QString & method, int matches,
                           const QStringList & scope )
{
  if ( mSearchRunning ) return false;

  // These should be removed
  mWords = words;
  mMethod = method;
  mMatches = matches;
  mScope = scope;

  // Saner variables to store search parameters:
  mWordList = words.split(' ');
  mMaxResults = matches;
  if ( method == "or" ) mOperation = Or;
  else mOperation = And;

  if ( !mView ) {
    return false;
  }

  QString txt = i18n("Search Results for '%1':", words.toHtmlEscaped() );

  mStderr = "<b>" + txt + "</b>\n";

  if ( mRootTraverser ) {
    khcDebug() << "SearchEngine::search(): mRootTraverser not null.";
    return false;
  }
  mRootTraverser = new SearchTraverser( this, 0 );
  mRootTraverser->setWords( words );
  DocMetaInfo::self()->startTraverseEntries( mRootTraverser );

  return true;
}

QString SearchEngine::substituteSearchQuery( const QString &query,
  const QString &identifier, const QStringList &words, int maxResults,
  Operation operation, const QString &lang, const QString& binary )
{
  QString result = query;
  result.replace( QLatin1String("%i"), identifier );
  result.replace( QLatin1String("%w"), words.join( "+" ) );
  result.replace( QLatin1String("%m"), QString::number( maxResults ) );
  QString o = QLatin1String(operation == Or ? "or" : "and");
  result.replace( QLatin1String("%o"), o );
  result.replace( QLatin1String("%d"), Prefs::indexDirectory() );
  result.replace( QLatin1String("%l"), lang );
  result.replace( QLatin1String("%b"), binary );

  return result;
}

GrantleeFormatter *SearchEngine::grantleeFormatter() const
{
  return mView->grantleeFormatter();
}

View *SearchEngine::view() const
{
  return mView;
}

void SearchEngine::finishSearch()
{
  delete mRootTraverser;
  mRootTraverser = 0;
  mSearchRunning = false;

  emit searchFinished();
}

QString SearchEngine::errorLog() const
{
  return mStderr;
}

void SearchEngine::logError( DocEntry *entry, const QString &msg )
{
  mStderr += entry->identifier() + QLatin1String(": ") + msg;
}

bool SearchEngine::isRunning() const
{
  return mSearchRunning;
}

SearchHandler *SearchEngine::handler( const QString &documentType ) const
{
  QSharedPointer<SearchHandler> ptr = mHandlers.value( documentType );
  return ptr ? ptr.data() : 0;
}

QStringList SearchEngine::words() const
{
  return mWordList;
}

int SearchEngine::maxResults() const
{
  return mMaxResults;
}

SearchEngine::Operation SearchEngine::operation() const
{
  return mOperation;
}

bool SearchEngine::canSearch( DocEntry *entry )
{
  return entry->docExists() && !entry->documentType().isEmpty() &&
    handler( entry->documentType() );
}

bool SearchEngine::needsIndex( DocEntry *entry )
{
  if ( !canSearch( entry ) ) return false;

  SearchHandler *h = handler( entry->documentType() );
  if ( !h || h->indexCommand( entry->identifier() ).isEmpty() ) return false;

  return true;
}

}


// vim:ts=2:sw=2:et