delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / nepomuk / services / queryservice / searchcore.cpp
blob8cbc87fbeba6da11ffafdb54940358b2e8e10f27
1 /*
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"
22 #include "qurlhash.h"
24 #include <QtCore/QEventLoop>
25 #include <QtCore/QPointer>
27 #include <KDebug>
29 class Nepomuk::Search::SearchCore::Private
31 public:
32 Private()
33 : cutOffScore( 0.25 ),
34 active( false ),
35 canceled( false ) {
38 double cutOffScore;
39 QHash<QUrl, Nepomuk::Search::Result> lastResults;
40 SearchThread* searchThread;
42 bool active;
43 bool canceled;
45 QPointer<QEventLoop> eventLoop;
49 Nepomuk::Search::SearchCore::SearchCore( QObject* parent )
50 : QObject( parent ),
51 d( new Private() )
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();
64 delete d;
68 bool Nepomuk::Search::SearchCore::isActive() const
70 return d->active;
74 void Nepomuk::Search::SearchCore::query( const Query& query )
76 d->lastResults.clear();
77 d->canceled = false;
78 d->active = true;
79 d->searchThread->query( query, cutOffScore() );
83 void Nepomuk::Search::SearchCore::cancel()
85 d->canceled = true;
86 d->active = false;
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 );
117 else {
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()
128 kDebug();
129 d->active = false;
130 if ( d->eventLoop ) {
131 d->eventLoop->exit();
133 emit finished();
137 QList<Nepomuk::Search::Result> Nepomuk::Search::SearchCore::blockingQuery( const Query& q )
139 kDebug();
141 // cancel previous search
142 if( d->eventLoop != 0 ) {
143 kDebug() << "Killing previous search";
144 QEventLoop* loop = d->eventLoop;
145 d->eventLoop = 0;
146 d->searchThread->cancel();
147 loop->exit();
150 QEventLoop loop;
151 d->eventLoop = &loop;
152 query( q );
153 loop.exec();
154 d->eventLoop = 0;
155 kDebug() << "done";
156 return lastResults();
159 #include "searchcore.moc"