add more spacing
[personal-kdebase.git] / runtime / nepomuk / strigibackend / util.cpp
blobd24fa352794aadcd37bdbd54ba0447a75b40b3f6
1 /*
2 Copyright (C) 2007-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 General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
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 General Public License
15 along with this library; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "util.h"
22 #include <strigi/variant.h>
23 #include <strigi/fieldtypes.h>
25 #include <QtCore/QUrl>
26 #include <QtCore/QFile>
27 #include <QtCore/QFileInfo>
28 #include <QtCore/QUuid>
29 #include <QtCore/QDebug>
31 #include <Soprano/Index/CLuceneIndex>
32 #include <Soprano/Model>
33 #include <Soprano/Vocabulary/RDF>
34 #include <Soprano/Vocabulary/RDFS>
35 #include <Soprano/Vocabulary/Xesam>
36 #include <Soprano/Vocabulary/NRL>
37 #include <Soprano/Vocabulary/XMLSchema>
40 #define STRIGI_NS "http://www.strigi.org/data#"
42 QUrl Strigi::Soprano::Util::fieldUri( const std::string& s )
44 QString qKey = QString::fromUtf8( s.c_str() );
45 QUrl url;
47 // very naive test for proper URI
48 if ( qKey.contains( ":/" ) ) {
49 url = qKey;
51 else {
52 url = STRIGI_NS + qKey;
55 // just to be sure
56 if ( url.isRelative() ) {
57 url.setScheme( "http" );
60 return url;
64 QUrl Strigi::Soprano::Util::fileUrl( const std::string& filename )
66 QUrl url = QUrl::fromLocalFile( QFileInfo( QString::fromUtf8( filename.c_str() ) ).absoluteFilePath() );
67 url.setScheme( "file" );
68 return url;
72 std::string Strigi::Soprano::Util::fieldName( const QUrl& uri )
74 QString s = uri.toString();
75 if ( s.startsWith( STRIGI_NS ) ) {
76 s = s.mid( strlen( STRIGI_NS ) );
78 return s.toUtf8().data();
82 TString Strigi::Soprano::Util::convertSearchField( const std::string& field )
84 if ( QString::fromUtf8( field.c_str() ) == ::Soprano::Index::CLuceneIndex::defaultSearchField() ) {
85 return TString::fromUtf8( field.c_str() );
87 else {
88 return fieldUri( field ).toString();
93 QUrl Strigi::Soprano::Util::uniqueUri( const QString& ns, ::Soprano::Model* model )
95 QUrl uri;
96 do {
97 QString uid = QUuid::createUuid().toString();
98 uri = ( ns + uid.mid( 1, uid.length()-2 ) );
99 } while ( model->containsAnyStatement( ::Soprano::Statement( uri, ::Soprano::Node(), ::Soprano::Node() ) ) );
100 return uri;
104 Strigi::Variant Strigi::Soprano::Util::nodeToVariant( const ::Soprano::Node& node )
106 if ( node.isLiteral() ) {
107 switch( node.literal().type() ) {
108 case QVariant::Int:
109 case QVariant::UInt:
110 case QVariant::LongLong: // FIXME: no perfect conversion :(
111 case QVariant::ULongLong:
112 return Strigi::Variant( node.literal().toInt() );
114 case QVariant::Bool:
115 return Strigi::Variant( node.literal().toBool() );
117 default:
118 return Strigi::Variant( node.literal().toString().toUtf8().data() );
121 else {
122 qWarning() << "(Soprano::Util::nodeToVariant) cannot convert non-literal node to variant.";
123 return Strigi::Variant();
128 void Strigi::Soprano::Util::storeStrigiMiniOntology( ::Soprano::Model* model )
130 // we use some nice URI here although we still have the STRIGI_NS for backwards comp
131 // at some point (if parentUrl will not be moved to xesam) we should move to a proper onto
133 QUrl graph( "http://nepomuk.kde.org/ontologies/2008/07/24/strigi/metadata" );
134 ::Soprano::Statement parentUrlProp( fieldUri( FieldRegister::parentLocationFieldName ),
135 ::Soprano::Vocabulary::RDF::type(),
136 ::Soprano::Vocabulary::RDF::Property(),
137 graph );
138 ::Soprano::Statement parentUrlRange( parentUrlProp.subject(),
139 ::Soprano::Vocabulary::RDFS::range(),
140 ::Soprano::Vocabulary::RDFS::Resource(),
141 graph );
142 ::Soprano::Statement oldParentUrlRange( parentUrlProp.subject(),
143 ::Soprano::Vocabulary::RDFS::range(),
144 ::Soprano::Vocabulary::XMLSchema::string(),
145 graph );
146 ::Soprano::Statement parentUrlDomain( parentUrlProp.subject(),
147 ::Soprano::Vocabulary::RDFS::domain(),
148 ::Soprano::Vocabulary::Xesam::File(),
149 graph );
150 ::Soprano::Statement metaDataType( graph,
151 ::Soprano::Vocabulary::RDF::type(),
152 ::Soprano::Vocabulary::NRL::Ontology(),
153 graph );
155 if ( !model->containsStatement( parentUrlProp ) ) {
156 model->addStatement( parentUrlProp );
158 if ( !model->containsStatement( parentUrlRange ) ) {
159 model->removeStatement( oldParentUrlRange );
160 model->addStatement( parentUrlRange );
162 if ( !model->containsStatement( parentUrlDomain ) ) {
163 model->addStatement( parentUrlDomain );
165 if ( !model->containsStatement( metaDataType ) ) {
166 model->addStatement( metaDataType );
171 QUrl Strigi::Ontology::indexGraphFor()
173 return QUrl::fromEncoded( "http://www.strigi.org/fields#indexGraphFor", QUrl::StrictMode );