• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

NepomukDaemons

util.cpp

Go to the documentation of this file.
00001 /*
00002    Copyright (C) 2007-2008 Sebastian Trueg <trueg@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU General Public License as
00006    published by the Free Software Foundation; either version 2 of
00007    the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this library; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "util.h"
00021 
00022 #include <strigi/variant.h>
00023 #include <strigi/fieldtypes.h>
00024 
00025 #include <QtCore/QUrl>
00026 #include <QtCore/QFile>
00027 #include <QtCore/QFileInfo>
00028 #include <QtCore/QUuid>
00029 #include <QtCore/QDebug>
00030 
00031 #include <Soprano/Index/CLuceneIndex>
00032 #include <Soprano/Model>
00033 #include <Soprano/Vocabulary/RDF>
00034 
00035 
00036 #define STRIGI_NS "http://www.strigi.org/data#"
00037 
00038 QUrl Strigi::Soprano::Util::fieldUri( const std::string& s )
00039 {
00040     QString qKey = QString::fromUtf8( s.c_str() );
00041     QUrl url;
00042 
00043     // very naive test for proper URI
00044     if ( qKey.contains( ":/" ) ) {
00045         url = qKey;
00046     }
00047     else {
00048         url = STRIGI_NS + qKey;
00049     }
00050 
00051     // just to be sure
00052     if ( url.isRelative() ) {
00053         url.setScheme( "http" );
00054     }
00055 
00056     return url;
00057 }
00058 
00059 
00060 QUrl Strigi::Soprano::Util::fileUrl( const std::string& filename )
00061 {
00062     QUrl url = QUrl::fromLocalFile( QFileInfo( QString::fromUtf8( filename.c_str() ) ).absoluteFilePath() );
00063     url.setScheme( "file" );
00064     return url;
00065 }
00066 
00067 
00068 std::string Strigi::Soprano::Util::fieldName( const QUrl& uri )
00069 {
00070     QString s = uri.toString();
00071     if ( s.startsWith( STRIGI_NS ) ) {
00072         s = s.mid( strlen( STRIGI_NS ) );
00073     }
00074     return s.toUtf8().data();
00075 }
00076 
00077 
00078 TString Strigi::Soprano::Util::convertSearchField( const std::string& field )
00079 {
00080     if ( QString::fromUtf8( field.c_str() ) == ::Soprano::Index::CLuceneIndex::defaultSearchField() ) {
00081         return TString::fromUtf8( field.c_str() );
00082     }
00083     else if ( QString( field.c_str() ) == ::Soprano::Vocabulary::RDF::type().toString() ) {
00084         // see sopranoindexwriter:addValue for details in this conversion
00085         static TString strigiType( "http://strigi.sourceforge.net/fields#rdf-string-type" );
00086         return strigiType;
00087     }
00088     else {
00089         return fieldUri( field ).toString();
00090     }
00091 }
00092 
00093 
00094 QUrl Strigi::Soprano::Util::uniqueUri( const QString& ns, ::Soprano::Model* model )
00095 {
00096     QUrl uri;
00097     do {
00098         QString uid = QUuid::createUuid().toString();
00099         uri = ( ns + uid.mid( 1, uid.length()-2 ) );
00100     } while ( model->containsAnyStatement( ::Soprano::Statement( uri, ::Soprano::Node(), ::Soprano::Node() ) ) );
00101     return uri;
00102 }
00103 
00104 
00105 Strigi::Variant Strigi::Soprano::Util::nodeToVariant( const ::Soprano::Node& node )
00106 {
00107     if ( node.isLiteral() ) {
00108         switch( node.literal().type() ) {
00109         case QVariant::Int:
00110         case QVariant::UInt:
00111         case QVariant::LongLong:  // FIXME: no perfect conversion :(
00112         case QVariant::ULongLong:
00113             return Strigi::Variant( node.literal().toInt() );
00114 
00115         case QVariant::Bool:
00116             return Strigi::Variant( node.literal().toBool() );
00117 
00118         default:
00119             return Strigi::Variant( node.literal().toString().toUtf8().data() );
00120         }
00121     }
00122     else {
00123         qWarning() << "(Soprano::Util::nodeToVariant) cannot convert non-literal node to variant.";
00124         return Strigi::Variant();
00125     }
00126 }

NepomukDaemons

Skip menu "NepomukDaemons"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KCMShell
  • KNotify
  • KStyles
  • Nepomuk Daemons
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal