• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

NepomukDaemons

searchcore.cpp

Go to the documentation of this file.
00001 /*
00002    This file is part of the Nepomuk KDE project.
00003    Copyright (C) 2007 Sebastian Trueg <trueg@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License version 2 as published by the Free Software Foundation.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "searchcore.h"
00021 #include "searchthread.h"
00022 #include "qurlhash.h"
00023 
00024 #include <QtCore/QEventLoop>
00025 #include <QtCore/QPointer>
00026 
00027 #include <KDebug>
00028 
00029 class Nepomuk::Search::SearchCore::Private
00030 {
00031 public:
00032     Private()
00033         : cutOffScore( 0.25 ),
00034           active( false ),
00035           canceled( false ) {
00036     }
00037 
00038     double cutOffScore;
00039     QHash<QUrl, Nepomuk::Search::Result> lastResults;
00040     SearchThread* searchThread;
00041 
00042     bool active;
00043     bool canceled;
00044 
00045     QPointer<QEventLoop> eventLoop;
00046 };
00047 
00048 
00049 Nepomuk::Search::SearchCore::SearchCore( QObject* parent )
00050     : QObject( parent ),
00051       d( new Private() )
00052 {
00053     d->searchThread = new SearchThread( this );
00054     connect( d->searchThread, SIGNAL( newResult( const Nepomuk::Search::Result& ) ),
00055              this, SLOT( slotNewResult( const Nepomuk::Search::Result& ) ) );
00056     connect( d->searchThread, SIGNAL( finished() ),
00057              this, SLOT( slotFinished() ) );
00058 }
00059 
00060 
00061 Nepomuk::Search::SearchCore::~SearchCore()
00062 {
00063     d->searchThread->cancel();
00064     delete d;
00065 }
00066 
00067 
00068 bool Nepomuk::Search::SearchCore::isActive() const
00069 {
00070     return d->active;
00071 }
00072 
00073 
00074 void Nepomuk::Search::SearchCore::query( const Query& query )
00075 {
00076     d->lastResults.clear();
00077     d->canceled = false;
00078     d->active = true;
00079     d->searchThread->query( query, cutOffScore() );
00080 }
00081 
00082 
00083 void Nepomuk::Search::SearchCore::cancel()
00084 {
00085     d->canceled = true;
00086     d->active = false;
00087     d->searchThread->cancel();
00088 }
00089 
00090 
00091 void Nepomuk::Search::SearchCore::setCutOffScore( double score )
00092 {
00093     d->cutOffScore = qMin( 1.0, qMax( score, 0.0 ) );
00094 }
00095 
00096 
00097 double Nepomuk::Search::SearchCore::cutOffScore() const
00098 {
00099     return d->cutOffScore;
00100 }
00101 
00102 
00103 QList<Nepomuk::Search::Result> Nepomuk::Search::SearchCore::lastResults() const
00104 {
00105     return d->lastResults.values();
00106 }
00107 
00108 
00109 void Nepomuk::Search::SearchCore::slotNewResult( const Nepomuk::Search::Result& result )
00110 {
00111     if ( !d->canceled ) {
00112         QHash<QUrl, Result>::iterator it = d->lastResults.find( result.resourceUri() );
00113         if ( it == d->lastResults.end() ) {
00114             d->lastResults.insert( result.resourceUri(), result );
00115             emit newResult( result );
00116         }
00117         else {
00118             // FIXME: normalizing scores anyone??
00119             it.value().setScore( it.value().score() + result.score() );
00120             emit scoreChanged( it.value() );
00121         }
00122     }
00123 }
00124 
00125 
00126 void Nepomuk::Search::SearchCore::slotFinished()
00127 {
00128     kDebug();
00129     d->active = false;
00130     if ( d->eventLoop ) {
00131         d->eventLoop->exit();
00132     }
00133     emit finished();
00134 }
00135 
00136 
00137 QList<Nepomuk::Search::Result> Nepomuk::Search::SearchCore::blockingQuery( const Query& q )
00138 {
00139     kDebug();
00140 
00141     // cancel previous search
00142     if( d->eventLoop != 0 ) {
00143         kDebug() << "Killing previous search";
00144         QEventLoop* loop = d->eventLoop;
00145         d->eventLoop = 0;
00146         d->searchThread->cancel();
00147         loop->exit();
00148     }
00149 
00150     QEventLoop loop;
00151     d->eventLoop = &loop;
00152     query( q );
00153     loop.exec();
00154     d->eventLoop = 0;
00155     kDebug() << "done";
00156     return lastResults();
00157 }
00158 
00159 #include "searchcore.moc"

NepomukDaemons

Skip menu "NepomukDaemons"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KCMShell
  • KNotify
  • KStyles
  • Nepomuk Daemons
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal