Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / runtime / nepomuk / strigibackend / sopranoindexmanager.cpp
blobc7d8a5b7e06b51b2474f0cdc6529514abafee3cc
1 /*
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>
41 namespace {
42 ::Soprano::Client::DBusClient* s_sopranoClient = 0;
45 class Strigi::Soprano::IndexManager::Private
47 public:
48 Private()
49 : repository( 0 ),
50 protectionModel( 0 ),
51 index( 0 ),
52 indexModel( 0 ),
53 writer( 0 ),
54 reader( 0 ) {
57 ::Soprano::Model* repository;
58 ::Soprano::Util::MutexModel* protectionModel;
59 ::Soprano::Index::CLuceneIndex* index;
60 ::Soprano::Index::IndexFilterModel* indexModel;
61 IndexWriter* writer;
62 IndexReader* reader;
66 extern "C" {
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() );
79 else {
80 return 0;
83 else {
84 const ::Soprano::Backend* backend = ::Soprano::discoverBackendByName( "sesame2" );
85 if ( !backend ) {
86 qDebug() << "(Strigi::Soprano::IndexManager) could not find Sesame2 backend. Falling back to redland. NO BACKEND CHANGE SUPPORT YET!";
87 backend = ::Soprano::discoverBackendByName( "redland" );
89 if ( !backend ) {
90 qDebug() << "(Strigi::Soprano::IndexManager) could not find a backend.";
91 return 0;
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();
99 return 0;
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 );
108 if ( model ) {
109 return new Strigi::Soprano::IndexManager( model, QString( "%1/index" ).arg( storageDir.path() ) );
111 else {
112 return 0;
117 STRIGI_EXPORT void deleteIndexManager( Strigi::IndexManager* m )
119 delete m;
123 Strigi::Soprano::IndexManager::IndexManager( ::Soprano::Model* model, const QString& path )
125 d = new Private;
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 );
132 else {
133 d->protectionModel = new ::Soprano::Util::MutexModel( ::Soprano::Util::MutexModel::ReadWriteMultiThreading, model );
138 Strigi::Soprano::IndexManager::~IndexManager()
140 qDebug() << "Cleaning up SopranoIndexManager";
141 delete d->reader;
142 delete d->writer;
143 delete d->indexModel;
144 delete d->index;
145 delete d->protectionModel;
146 delete d->repository;
147 delete d;
151 Strigi::IndexReader* Strigi::Soprano::IndexManager::indexReader()
153 if ( !d->reader ) {
154 qDebug() << "(Soprano::IndexManager) creating IndexReader";
155 if ( d->indexModel )
156 d->reader = new Strigi::Soprano::IndexReader( d->indexModel );
157 else
158 d->reader = new Strigi::Soprano::IndexReader( d->protectionModel );
161 return d->reader;
165 Strigi::IndexWriter* Strigi::Soprano::IndexManager::indexWriter()
167 if ( !d->writer ) {
168 qDebug() << "(Soprano::IndexManager) creating IndexWriter";
169 if ( d->indexModel )
170 d->writer = new Strigi::Soprano::IndexWriter( d->indexModel );
171 else
172 d->writer = new Strigi::Soprano::IndexWriter( d->protectionModel );
175 return d->writer;