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

NepomukDaemons

dbusoperators.cpp

Go to the documentation of this file.
00001 /*
00002    Copyright (c) 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 Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "dbusoperators.h"
00020 
00021 #include <QtDBus/QDBusMetaType>
00022 
00023 
00024 Q_DECLARE_METATYPE(Nepomuk::Search::Result)
00025 Q_DECLARE_METATYPE(Nepomuk::Search::Term)
00026 Q_DECLARE_METATYPE(Nepomuk::Search::Query)
00027 Q_DECLARE_METATYPE(Soprano::Node)
00028 Q_DECLARE_METATYPE(QList<int>)
00029 Q_DECLARE_METATYPE(QList<Nepomuk::Search::Result>)
00030 
00031 
00032 void Nepomuk::Search::registerDBusTypes()
00033 {
00034     qDBusRegisterMetaType<Nepomuk::Search::Result>();
00035     qDBusRegisterMetaType<QList<Nepomuk::Search::Result> >();
00036     qDBusRegisterMetaType<Nepomuk::Search::Term>();
00037     qDBusRegisterMetaType<Nepomuk::Search::Query>();
00038     qDBusRegisterMetaType<Soprano::Node>();
00039 }
00040 
00041 
00042 QDBusArgument& operator<<( QDBusArgument& arg, const Nepomuk::Search::Result& result )
00043 {
00044     //
00045     // Signature: (sda{s(isss)})
00046     //
00047 
00048     arg.beginStructure();
00049 
00050     arg << QString::fromAscii( result.resourceUri().toEncoded() ) << result.score();
00051 
00052     arg.beginMap( QVariant::String, qMetaTypeId<Soprano::Node>() );
00053 
00054     QHash<QUrl, Soprano::Node> rp = result.requestProperties();
00055     for ( QHash<QUrl, Soprano::Node>::const_iterator it = rp.constBegin(); it != rp.constEnd(); ++it ) {
00056         arg.beginMapEntry();
00057         arg << QString::fromAscii( it.key().toEncoded() ) << it.value();
00058         arg.endMapEntry();
00059     }
00060 
00061     arg.endMap();
00062 
00063     arg.endStructure();
00064 
00065     return arg;
00066 }
00067 
00068 
00069 const QDBusArgument& operator>>( const QDBusArgument& arg, Nepomuk::Search::Result& result )
00070 {
00071     //
00072     // Signature: (sda{s(isss)})
00073     //
00074 
00075     arg.beginStructure();
00076     QString uri;
00077     double score = 0.0;
00078 
00079     arg >> uri >> score;
00080     result = Nepomuk::Search::Result( QUrl::fromEncoded( uri.toAscii() ), score );
00081 
00082     arg.beginMap();
00083     while ( !arg.atEnd() ) {
00084         QString rs;
00085         Soprano::Node node;
00086         arg.beginMapEntry();
00087         arg >> rs >> node;
00088         arg.endMapEntry();
00089         result.addRequestProperty( QUrl::fromEncoded( rs.toAscii() ), node );
00090     }
00091     arg.endMap();
00092 
00093     arg.endStructure();
00094 
00095     return arg;
00096 }
00097 
00098 
00099 QDBusArgument& operator<<( QDBusArgument& arg, const Nepomuk::Search::Term& term )
00100 {
00101     //
00102     // Signature: (ii(isss)sss)
00103     // i      -> type
00104     // i      -> comparator type
00105     // (isss) -> Soprano::LiteralValue encoded as a Soprano::Node for simplicity
00106     // s      -> resource
00107     // s      -> field
00108     // s      -> property
00109     //
00110 
00111     arg.beginStructure();
00112     arg << ( int )term.type()
00113         << ( int )term.comparator()
00114         << Soprano::Node( term.value() )
00115         << QString::fromAscii( term.resource().toEncoded() )
00116         << term.field()
00117         << QString::fromAscii( term.property().toEncoded() );
00118     arg.endStructure();
00119 
00120     return arg;
00121 }
00122 
00123 
00124 const QDBusArgument& operator>>( const QDBusArgument& arg, Nepomuk::Search::Term& term )
00125 {
00126     //
00127     // Signature: (ii(isss)sss)
00128     // i      -> type
00129     // i      -> comparator type
00130     // (isss) -> Soprano::LiteralValue encoded as a Soprano::Node for simplicity
00131     // s      -> resource
00132     // s      -> field
00133     // s      -> property
00134     //
00135 
00136     arg.beginStructure();
00137     int type = Nepomuk::Search::Term::InvalidTerm;
00138     int comparator = Nepomuk::Search::Term::Equal;
00139     Soprano::Node valueNode;
00140     QString resource, field, property;
00141     arg >> type
00142         >> comparator
00143         >> valueNode
00144         >> resource
00145         >> field
00146         >> property;
00147     term.setType( Nepomuk::Search::Term::Type( type ) );
00148     term.setComparator( Nepomuk::Search::Term::Comparator( comparator ) );
00149     if ( valueNode.isLiteral() )
00150         term.setValue( valueNode.literal() );
00151     if ( !resource.isEmpty() )
00152         term.setResource( QUrl::fromEncoded( resource.toAscii() ) );
00153     if ( !field.isEmpty() )
00154         term.setField( field );
00155     if ( !property.isEmpty() )
00156         term.setProperty( QUrl::fromEncoded( property.toAscii() ) );
00157     arg.endStructure();
00158 
00159     return arg;
00160 }
00161 
00162 
00163 // streaming a Query object is a bit tricky as it is a set of nested Term objects
00164 // DBus does not allow arbitrary nesting of objects, thus we use a little trick:
00165 // We store all used Term objects in a list and use integer indices pointing into
00166 // this list to describe the nesting within the Term objects. This also means that
00167 // a Term's subTerm list is replaced with a list of indices
00168 namespace {
00172     void buildTermRelations( QList<Nepomuk::Search::Term>& terms, QHash<int, QList<int> >& termRelations ) {
00173         QList<Nepomuk::Search::Term> subTerms = terms.last().subTerms();
00174         int termIndex = terms.count()-1;
00175         for ( int i = 0; i < subTerms.count(); ++i ) {
00176             terms.append( subTerms[i] );
00177             termRelations[termIndex].append( terms.count()-1 );
00178             buildTermRelations( terms, termRelations );
00179         }
00180     }
00181 }
00182 
00183 QDBusArgument& operator<<( QDBusArgument& arg, const Nepomuk::Search::Query& query )
00184 {
00185     //
00186     // Signature: (isa(ii(isss)sss)a{iai}ia{sb})
00187     // i      -> type
00188     // s      -> sparql query
00189     // a(ii(isss)sss)    -> array of terms (first is root term)
00190     // a{iai} -> hash of term relations
00191     // i      -> limit
00192     // a{sb}  -> request properties
00193     //
00194 
00195     arg.beginStructure();
00196 
00197     arg << ( int )query.type() << query.sparqlQuery();
00198 
00199     QList<Nepomuk::Search::Term> terms;
00200     QHash<int, QList<int> > termRelations;
00201     if ( query.type() == Nepomuk::Search::Query::PlainQuery ) {
00202         terms.append( query.term() );
00203         buildTermRelations( terms, termRelations );
00204     }
00205     arg << terms;
00206 
00207     arg.beginMap( QVariant::Int, qMetaTypeId<QList<int> >() );
00208     for( QHash<int, QList<int> >::const_iterator it = termRelations.constBegin();
00209          it != termRelations.constEnd(); ++it ) {
00210         arg.beginMapEntry();
00211         arg << it.key() << it.value();
00212         arg.endMapEntry();
00213     }
00214     arg.endMap();
00215     arg << query.limit();
00216 
00217     arg.beginMap( QVariant::String, QVariant::Bool );
00218     QList<Nepomuk::Search::Query::RequestProperty> requestProperties = query.requestProperties();
00219     foreach( const Nepomuk::Search::Query::RequestProperty& rp, requestProperties ) {
00220         arg.beginMapEntry();
00221         arg << QString::fromAscii( rp.first.toEncoded() ) << rp.second;
00222         arg.endMapEntry();
00223     }
00224     arg.endMap();
00225 
00226     arg.endStructure();
00227 
00228     return arg;
00229 }
00230 
00231 
00232 namespace {
00233     Nepomuk::Search::Term rebuildTermFromTermList( const QList<Nepomuk::Search::Term>& terms,
00234                                                    const QHash<int, QList<int> >& termRelations,
00235                                                    int index = 0 ) {
00236         Nepomuk::Search::Term root = terms[index];
00237         foreach( int i, termRelations[index] ) {
00238             root.addSubTerm( rebuildTermFromTermList( terms, termRelations, i ) );
00239         }
00240         return root;
00241     }
00242 }
00243 
00244 const QDBusArgument& operator>>( const QDBusArgument& arg, Nepomuk::Search::Query& query )
00245 {
00246     //
00247     // Signature: (isa(ii(isss)sss)a{iai}ia{sb})
00248     // i      -> type
00249     // s      -> sparql query
00250     // a(ii(isss)sss)    -> array of terms (first is root term)
00251     // a{iai} -> hash of term relations
00252     // i      -> limit
00253     // a{sb}  -> request properties
00254     //
00255 
00256     arg.beginStructure();
00257 
00258     int type = Nepomuk::Search::Query::InvalidQuery;
00259     QString sparqlQuery;
00260     QList<Nepomuk::Search::Term> terms;
00261     QHash<int, QList<int> > termRelations;
00262     int limit = 0;
00263 
00264     arg >> type
00265         >> sparqlQuery
00266         >> terms;
00267 
00268     arg.beginMap();
00269     while ( !arg.atEnd() ) {
00270         int termIndex = 0;
00271         QList<int> indices;
00272         arg.beginMapEntry();
00273         arg >> termIndex >> indices;
00274         arg.endMapEntry();
00275         termRelations.insert( termIndex, indices );
00276     }
00277     arg.endMap();
00278 
00279     arg >> limit;
00280 
00281     arg.beginMap();
00282     while ( !arg.atEnd() ) {
00283         QString prop;
00284         bool optional = true;
00285         arg.beginMapEntry();
00286         arg >> prop >> optional;
00287         arg.endMapEntry();
00288         query.addRequestProperty( QUrl::fromEncoded( prop.toAscii() ), optional );
00289     }
00290     arg.endMap();
00291 
00292     arg.endStructure();
00293 
00294     if ( Nepomuk::Search::Query::Type( type ) == Nepomuk::Search::Query::PlainQuery ) {
00295         query.setTerm( rebuildTermFromTermList( terms, termRelations ) );
00296     }
00297     else {
00298         query.setSparqlQuery( sparqlQuery );
00299     }
00300     query.setLimit( limit );
00301 
00302     return arg;
00303 }
00304 
00305 
00306 QDBusArgument& operator<<( QDBusArgument& arg, const Soprano::Node& node )
00307 {
00308     arg.beginStructure();
00309     arg << ( int )node.type();
00310     if ( node.type() == Soprano::Node::ResourceNode ) {
00311         arg << QString::fromAscii( node.uri().toEncoded() );
00312     }
00313     else {
00314         arg << node.toString();
00315     }
00316     arg << node.language() << node.dataType().toString();
00317     arg.endStructure();
00318     return arg;
00319 }
00320 
00321 
00322 const QDBusArgument& operator>>( const QDBusArgument& arg, Soprano::Node& node )
00323 {
00324     //
00325     // Signature: (isss)
00326     //
00327     arg.beginStructure();
00328     int type;
00329     QString value, language, dataTypeUri;
00330     arg >> type >> value >> language >> dataTypeUri;
00331     if ( type == Soprano::Node::LiteralNode ) {
00332         node = Soprano::Node( Soprano::LiteralValue::fromString( value, dataTypeUri ), language );
00333     }
00334     else if ( type == Soprano::Node::ResourceNode ) {
00335         node = Soprano::Node( QUrl::fromEncoded( value.toAscii() ) );
00336     }
00337     else if ( type == Soprano::Node::BlankNode ) {
00338         node = Soprano::Node( value );
00339     }
00340     else {
00341         node = Soprano::Node();
00342     }
00343     arg.endStructure();
00344     return arg;
00345 }

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