2 Copyright (c) 2008 Sebastian Trueg <trueg@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
20 #include "folderconnection.h"
21 #include "queryservice.h"
24 #include <Soprano/Model>
29 Nepomuk::Search::Folder::Folder( const Query
& query
, QObject
* parent
)
32 m_initialListingDone( false ),
33 m_storageChanged( false )
35 m_updateTimer
.setSingleShot( true );
36 m_updateTimer
.setInterval( 2000 );
38 m_searchCore
= new SearchCore( this );
40 connect( m_searchCore
, SIGNAL( newResult( const Nepomuk::Search::Result
& ) ),
41 this, SLOT( slotSearchNewResult( const Nepomuk::Search::Result
& ) ) );
42 connect( m_searchCore
, SIGNAL( scoreChanged( const Nepomuk::Search::Result
& ) ),
43 this, SLOT( slotSearchScoreChanged( const Nepomuk::Search::Result
& ) ) );
44 connect( m_searchCore
, SIGNAL( finished() ),
45 this, SLOT( slotSearchFinished() ) );
46 connect( QueryService::instance()->mainModel(), SIGNAL( statementsAdded() ),
47 this, SLOT( slotStorageChanged() ) );
48 connect( QueryService::instance()->mainModel(), SIGNAL( statementsRemoved() ),
49 this, SLOT( slotStorageChanged() ) );
50 connect( &m_updateTimer
, SIGNAL( timeout() ),
51 this, SLOT( slotUpdateTimeout() ) );
55 Nepomuk::Search::Folder::~Folder()
60 void Nepomuk::Search::Folder::update()
62 if ( !m_searchCore
->isActive() ) {
63 // run the search and forward signals to all connections that requested it
64 m_searchCore
->query( m_query
);
69 QList
<Nepomuk::Search::Result
> Nepomuk::Search::Folder::entries() const
71 return m_results
.values();
75 bool Nepomuk::Search::Folder::initialListingDone() const
77 return m_initialListingDone
;
81 void Nepomuk::Search::Folder::slotSearchNewResult( const Nepomuk::Search::Result
& result
)
83 if ( m_initialListingDone
) {
84 m_newResults
.insert( result
.resourceUri(), result
);
85 if ( !m_results
.contains( result
.resourceUri() ) ) {
86 emit
newEntries( QList
<Result
>() << result
);
90 m_results
.insert( result
.resourceUri(), result
);
91 emit
newEntries( QList
<Result
>() << result
);
96 void Nepomuk::Search::Folder::slotSearchScoreChanged( const Nepomuk::Search::Result
& )
98 if ( m_initialListingDone
) {
99 #warning FIXME: handle scoreChanged
107 void Nepomuk::Search::Folder::slotSearchFinished()
109 if ( m_initialListingDone
) {
110 // inform about removed items
111 foreach( const Result
& result
, m_results
) {
112 if ( !m_newResults
.contains( result
.resourceUri() ) ) {
113 emit
entriesRemoved( QList
<QUrl
>() << result
.resourceUri() );
118 m_results
= m_newResults
;
119 m_newResults
.clear();
122 m_initialListingDone
= true;
123 emit
finishedListing();
126 // make sure we do not update again right away
127 m_updateTimer
.start();
131 void Nepomuk::Search::Folder::slotStorageChanged()
133 if ( !m_updateTimer
.isActive() && !m_searchCore
->isActive() ) {
137 m_storageChanged
= true;
142 // if there was a change in the nepomuk store we update
143 void Nepomuk::Search::Folder::slotUpdateTimeout()
145 if ( m_storageChanged
&& !m_searchCore
->isActive() ) {
146 m_storageChanged
= false;
152 void Nepomuk::Search::Folder::addConnection( FolderConnection
* conn
)
154 Q_ASSERT( conn
!= 0 );
155 Q_ASSERT( !m_connections
.contains( conn
) );
157 m_connections
.append( conn
);
161 void Nepomuk::Search::Folder::removeConnection( FolderConnection
* conn
)
163 Q_ASSERT( conn
!= 0 );
164 Q_ASSERT( m_connections
.contains( conn
) );
166 m_connections
.removeAll( conn
);
168 if ( m_connections
.isEmpty() ) {
169 kDebug() << "Folder unused. Deleting.";
175 QList
<Nepomuk::Search::FolderConnection
*> Nepomuk::Search::Folder::openConnections() const
177 return m_connections
;
180 #include "folder.moc"