2 $Id: sourceheader 511311 2006-02-19 14:51:05Z trueg $
4 This file is part of the Strigi project.
5 Copyright (C) 2007 Sebastian Trueg <trueg@kde.org>
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of
10 the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this library; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
23 #include "sopranoindexmanager.h"
24 #include "sopranoindexwriter.h"
25 #include "sopranoindexreader.h"
27 #include <strigi/strigiconfig.h>
29 #include <Soprano/Soprano>
30 #include <Soprano/Client/DBusClient>
31 #include <Soprano/Index/IndexFilterModel>
32 #include <Soprano/Index/CLuceneIndex>
33 #include <Soprano/Util/MutexModel>
34 #include <Soprano/Client/DBusModel>
36 #include <QtCore/QDir>
37 #include <QtCore/QDebug>
38 #include <QtCore/QString>
42 ::Soprano::Client::DBusClient
* s_sopranoClient
= 0;
45 class Strigi::Soprano::IndexManager::Private
57 ::Soprano::Model
* repository
;
58 ::Soprano::Util::MutexModel
* protectionModel
;
59 ::Soprano::Index::CLuceneIndex
* index
;
60 ::Soprano::Index::IndexFilterModel
* indexModel
;
67 // we do not use REGISTER_STRIGI_INDEXMANAGER as we do have to perform some additional checks
68 STRIGI_EXPORT
Strigi::IndexManager
* createIndexManager( const char* dir
)
70 if ( !s_sopranoClient
) {
71 s_sopranoClient
= new ::Soprano::Client::DBusClient( "org.kde.NepomukServer" );
74 if ( s_sopranoClient
->isValid() ) {
75 qDebug() << "(Strigi::Soprano::IndexManager) found Soprano server.";
76 if ( ::Soprano::Model
* model
= s_sopranoClient
->createModel( "main" ) ) {
77 return new Strigi::Soprano::IndexManager( model
, QString() );
84 const ::Soprano::Backend
* backend
= ::Soprano::discoverBackendByName( "sesame2" );
86 qDebug() << "(Strigi::Soprano::IndexManager) could not find Sesame2 backend. Falling back to redland. NO BACKEND CHANGE SUPPORT YET!";
87 backend
= ::Soprano::discoverBackendByName( "redland" );
90 qDebug() << "(Strigi::Soprano::IndexManager) could not find a backend.";
94 QDir
storageDir( dir
);
95 storageDir
.makeAbsolute();
96 if ( !storageDir
.exists() ) {
97 if ( !QDir( "/" ).mkpath( storageDir
.path() ) ) {
98 qDebug() << "Failed to create storage dir " << storageDir
.path();
102 storageDir
.mkdir( "index" );
104 QList
< ::Soprano::BackendSetting
> settings
;
105 settings
.append( ::Soprano::BackendSetting( ::Soprano::BackendOptionStorageDir
, storageDir
.path() ) );
107 ::Soprano::Model
* model
= backend
->createModel( settings
);
109 return new Strigi::Soprano::IndexManager( model
, QString( "%1/index" ).arg( storageDir
.path() ) );
117 STRIGI_EXPORT
void deleteIndexManager( Strigi::IndexManager
* m
)
123 Strigi::Soprano::IndexManager::IndexManager( ::Soprano::Model
* model
, const QString
& path
)
126 d
->repository
= model
;
127 if ( !path
.isEmpty() ) {
128 d
->index
= new ::Soprano::Index::CLuceneIndex();
129 d
->index
->open( path
, true );
130 d
->indexModel
= new ::Soprano::Index::IndexFilterModel( d
->index
, model
);
133 d
->protectionModel
= new ::Soprano::Util::MutexModel( ::Soprano::Util::MutexModel::ReadWriteMultiThreading
, model
);
138 Strigi::Soprano::IndexManager::~IndexManager()
140 qDebug() << "Cleaning up SopranoIndexManager";
143 delete d
->indexModel
;
145 delete d
->protectionModel
;
146 delete d
->repository
;
151 Strigi::IndexReader
* Strigi::Soprano::IndexManager::indexReader()
154 qDebug() << "(Soprano::IndexManager) creating IndexReader";
156 d
->reader
= new Strigi::Soprano::IndexReader( d
->indexModel
);
158 d
->reader
= new Strigi::Soprano::IndexReader( d
->protectionModel
);
165 Strigi::IndexWriter
* Strigi::Soprano::IndexManager::indexWriter()
168 qDebug() << "(Soprano::IndexManager) creating IndexWriter";
170 d
->writer
= new Strigi::Soprano::IndexWriter( d
->indexModel
);
172 d
->writer
= new Strigi::Soprano::IndexWriter( d
->protectionModel
);