Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / runtime / nepomuk / strigibackend / util.cpp
blob46abe5a0466c4e68f7cbea1f18c6bffdbfa6442d
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 "util.h"
25 #include <strigi/variant.h>
26 #include <strigi/fieldtypes.h>
28 #include <QtCore/QUrl>
29 #include <QtCore/QFile>
30 #include <QtCore/QFileInfo>
31 #include <QtCore/QUuid>
32 #include <QtCore/QDebug>
34 #include <Soprano/Index/CLuceneIndex>
35 #include <Soprano/Model>
36 #include <Soprano/Vocabulary/RDF>
39 #define STRIGI_NS "http://www.strigi.org/data#"
41 QUrl Strigi::Soprano::Util::fieldUri( const std::string& s )
43 QString qKey = QString::fromUtf8( s.c_str() );
44 QUrl url;
46 // very naive test for proper URI
47 if ( qKey.contains( ":/" ) ) {
48 url = qKey;
50 else {
51 url = STRIGI_NS + qKey;
54 // just to be sure
55 if ( url.isRelative() ) {
56 url.setScheme( "http" );
59 return url;
63 QUrl Strigi::Soprano::Util::fileUrl( const std::string& filename )
65 QUrl url = QUrl::fromLocalFile( QFileInfo( QString::fromUtf8( filename.c_str() ) ).absoluteFilePath() );
66 url.setScheme( "file" );
67 return url;
71 std::string Strigi::Soprano::Util::fieldName( const QUrl& uri )
73 QString s = uri.toString();
74 if ( s.startsWith( STRIGI_NS ) ) {
75 s = s.mid( strlen( STRIGI_NS ) );
77 return s.toUtf8().data();
81 TString Strigi::Soprano::Util::convertSearchField( const std::string& field )
83 if ( QString::fromUtf8( field.c_str() ) == ::Soprano::Index::CLuceneIndex::defaultSearchField() ) {
84 return TString::fromUtf8( field.c_str() );
86 else if ( QString( field.c_str() ) == ::Soprano::Vocabulary::RDF::type().toString() ) {
87 // see sopranoindexwriter:addValue for details in this conversion
88 return TString( "http://strigi.sourceforge.net/fields#rdf-string-type" );
90 else {
91 return fieldUri( field ).toString();
96 QUrl Strigi::Soprano::Util::uniqueUri( const QString& ns, ::Soprano::Model* model )
98 QUrl uri;
99 do {
100 QString uid = QUuid::createUuid().toString();
101 uri = ( ns + uid.mid( 1, uid.length()-2 ) );
102 } while ( model->containsAnyStatement( ::Soprano::Statement( uri, ::Soprano::Node(), ::Soprano::Node() ) ) );
103 return uri;
107 Strigi::Variant Strigi::Soprano::Util::nodeToVariant( const ::Soprano::Node& node )
109 if ( node.isLiteral() ) {
110 switch( node.literal().type() ) {
111 case QVariant::Int:
112 case QVariant::UInt:
113 case QVariant::LongLong: // FIXME: no perfect conversion :(
114 case QVariant::ULongLong:
115 return Strigi::Variant( node.literal().toInt() );
117 case QVariant::Bool:
118 return Strigi::Variant( node.literal().toBool() );
120 default:
121 return Strigi::Variant( node.literal().toString().toUtf8().data() );
124 else {
125 qWarning() << "(Soprano::Util::nodeToVariant) cannot convert non-literal node to variant.";
126 return Strigi::Variant();