2 This file is part of the Nepomuk KDE project.
3 Copyright (C) 2007 Sebastian Trueg <trueg@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "searchcore.h"
21 #include "searchthread.h"
24 #include <QtCore/QEventLoop>
25 #include <QtCore/QPointer>
29 class Nepomuk::Search::SearchCore::Private
33 : cutOffScore( 0.25 ),
39 QHash
<QUrl
, Nepomuk::Search::Result
> lastResults
;
40 SearchThread
* searchThread
;
45 QPointer
<QEventLoop
> eventLoop
;
49 Nepomuk::Search::SearchCore::SearchCore( QObject
* parent
)
53 d
->searchThread
= new SearchThread( this );
54 connect( d
->searchThread
, SIGNAL( newResult( const Nepomuk::Search::Result
& ) ),
55 this, SLOT( slotNewResult( const Nepomuk::Search::Result
& ) ) );
56 connect( d
->searchThread
, SIGNAL( finished() ),
57 this, SLOT( slotFinished() ) );
61 Nepomuk::Search::SearchCore::~SearchCore()
63 d
->searchThread
->cancel();
68 bool Nepomuk::Search::SearchCore::isActive() const
74 void Nepomuk::Search::SearchCore::query( const Query
& query
)
76 d
->lastResults
.clear();
79 d
->searchThread
->query( query
, cutOffScore() );
83 void Nepomuk::Search::SearchCore::cancel()
87 d
->searchThread
->cancel();
91 void Nepomuk::Search::SearchCore::setCutOffScore( double score
)
93 d
->cutOffScore
= qMin( 1.0, qMax( score
, 0.0 ) );
97 double Nepomuk::Search::SearchCore::cutOffScore() const
99 return d
->cutOffScore
;
103 QList
<Nepomuk::Search::Result
> Nepomuk::Search::SearchCore::lastResults() const
105 return d
->lastResults
.values();
109 void Nepomuk::Search::SearchCore::slotNewResult( const Nepomuk::Search::Result
& result
)
111 if ( !d
->canceled
) {
112 QHash
<QUrl
, Result
>::iterator it
= d
->lastResults
.find( result
.resourceUri() );
113 if ( it
== d
->lastResults
.end() ) {
114 d
->lastResults
.insert( result
.resourceUri(), result
);
115 emit
newResult( result
);
118 // FIXME: normalizing scores anyone??
119 it
.value().setScore( it
.value().score() + result
.score() );
120 emit
scoreChanged( it
.value() );
126 void Nepomuk::Search::SearchCore::slotFinished()
130 if ( d
->eventLoop
) {
131 d
->eventLoop
->exit();
137 QList
<Nepomuk::Search::Result
> Nepomuk::Search::SearchCore::blockingQuery( const Query
& q
)
141 // cancel previous search
142 if( d
->eventLoop
!= 0 ) {
143 kDebug() << "Killing previous search";
144 QEventLoop
* loop
= d
->eventLoop
;
146 d
->searchThread
->cancel();
151 d
->eventLoop
= &loop
;
156 return lastResults();
159 #include "searchcore.moc"