• Skip to content
  • Skip to link menu
KDE 4.2 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 #include <Soprano/Vocabulary/RDFS>
00035 #include <Soprano/Vocabulary/Xesam>
00036 #include <Soprano/Vocabulary/NRL>
00037 #include <Soprano/Vocabulary/XMLSchema>
00038 
00039 
00040 #define STRIGI_NS "http://www.strigi.org/data#"
00041 
00042 QUrl Strigi::Soprano::Util::fieldUri( const std::string& s )
00043 {
00044     QString qKey = QString::fromUtf8( s.c_str() );
00045     QUrl url;
00046 
00047     // very naive test for proper URI
00048     if ( qKey.contains( ":/" ) ) {
00049         url = qKey;
00050     }
00051     else {
00052         url = STRIGI_NS + qKey;
00053     }
00054 
00055     // just to be sure
00056     if ( url.isRelative() ) {
00057         url.setScheme( "http" );
00058     }
00059 
00060     return url;
00061 }
00062 
00063 
00064 QUrl Strigi::Soprano::Util::fileUrl( const std::string& filename )
00065 {
00066     QUrl url = QUrl::fromLocalFile( QFileInfo( QString::fromUtf8( filename.c_str() ) ).absoluteFilePath() );
00067     url.setScheme( "file" );
00068     return url;
00069 }
00070 
00071 
00072 std::string Strigi::Soprano::Util::fieldName( const QUrl& uri )
00073 {
00074     QString s = uri.toString();
00075     if ( s.startsWith( STRIGI_NS ) ) {
00076         s = s.mid( strlen( STRIGI_NS ) );
00077     }
00078     return s.toUtf8().data();
00079 }
00080 
00081 
00082 TString Strigi::Soprano::Util::convertSearchField( const std::string& field )
00083 {
00084     if ( QString::fromUtf8( field.c_str() ) == ::Soprano::Index::CLuceneIndex::defaultSearchField() ) {
00085         return TString::fromUtf8( field.c_str() );
00086     }
00087     else {
00088         return fieldUri( field ).toString();
00089     }
00090 }
00091 
00092 
00093 QUrl Strigi::Soprano::Util::uniqueUri( const QString& ns, ::Soprano::Model* model )
00094 {
00095     QUrl uri;
00096     do {
00097         QString uid = QUuid::createUuid().toString();
00098         uri = ( ns + uid.mid( 1, uid.length()-2 ) );
00099     } while ( model->containsAnyStatement( ::Soprano::Statement( uri, ::Soprano::Node(), ::Soprano::Node() ) ) );
00100     return uri;
00101 }
00102 
00103 
00104 Strigi::Variant Strigi::Soprano::Util::nodeToVariant( const ::Soprano::Node& node )
00105 {
00106     if ( node.isLiteral() ) {
00107         switch( node.literal().type() ) {
00108         case QVariant::Int:
00109         case QVariant::UInt:
00110         case QVariant::LongLong:  // FIXME: no perfect conversion :(
00111         case QVariant::ULongLong:
00112             return Strigi::Variant( node.literal().toInt() );
00113 
00114         case QVariant::Bool:
00115             return Strigi::Variant( node.literal().toBool() );
00116 
00117         default:
00118             return Strigi::Variant( node.literal().toString().toUtf8().data() );
00119         }
00120     }
00121     else {
00122         qWarning() << "(Soprano::Util::nodeToVariant) cannot convert non-literal node to variant.";
00123         return Strigi::Variant();
00124     }
00125 }
00126 
00127 
00128 void Strigi::Soprano::Util::storeStrigiMiniOntology( ::Soprano::Model* model )
00129 {
00130     // we use some nice URI here although we still have the STRIGI_NS for backwards comp
00131     // at some point (if parentUrl will not be moved to xesam) we should move to a proper onto
00132 
00133     QUrl graph( "http://nepomuk.kde.org/ontologies/2008/07/24/strigi/metadata" );
00134     ::Soprano::Statement parentUrlProp( fieldUri( FieldRegister::parentLocationFieldName ),
00135                                         ::Soprano::Vocabulary::RDF::type(),
00136                                         ::Soprano::Vocabulary::RDF::Property(),
00137                                         graph );
00138     ::Soprano::Statement parentUrlRange( parentUrlProp.subject(),
00139                                          ::Soprano::Vocabulary::RDFS::range(),
00140                                          ::Soprano::Vocabulary::RDFS::Resource(),
00141                                          graph );
00142     ::Soprano::Statement oldParentUrlRange( parentUrlProp.subject(),
00143                                             ::Soprano::Vocabulary::RDFS::range(),
00144                                             ::Soprano::Vocabulary::XMLSchema::string(),
00145                                             graph );
00146     ::Soprano::Statement parentUrlDomain( parentUrlProp.subject(),
00147                                           ::Soprano::Vocabulary::RDFS::domain(),
00148                                           ::Soprano::Vocabulary::Xesam::File(),
00149                                           graph );
00150     ::Soprano::Statement metaDataType( graph,
00151                                        ::Soprano::Vocabulary::RDF::type(),
00152                                        ::Soprano::Vocabulary::NRL::Ontology(),
00153                                        graph );
00154 
00155     if ( !model->containsStatement( parentUrlProp ) ) {
00156         model->addStatement( parentUrlProp );
00157     }
00158     if ( !model->containsStatement( parentUrlRange ) ) {
00159         model->removeStatement( oldParentUrlRange );
00160         model->addStatement( parentUrlRange );
00161     }
00162     if ( !model->containsStatement( parentUrlDomain ) ) {
00163         model->addStatement( parentUrlDomain );
00164     }
00165     if ( !model->containsStatement( metaDataType ) ) {
00166         model->addStatement( metaDataType );
00167     }
00168 }
00169 
00170 
00171 QUrl Strigi::Ontology::indexGraphFor()
00172 {
00173     return QUrl::fromEncoded( "http://www.strigi.org/fields#indexGraphFor", QUrl::StrictMode );
00174 }

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.7
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