NepomukDaemons
graphretriever.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "graphretriever.h"
00022
00023 #include <QtCore/QByteArray>
00024 #include <QtCore/QEventLoop>
00025 #include <QtCore/QHash>
00026 #include <QtCore/QPair>
00027 #include <QtCore/QString>
00028 #include <QtCore/QTextStream>
00029 #include <QtCore/QUrl>
00030
00031 #include <Soprano/Model>
00032 #include <Soprano/Global>
00033 #include <Soprano/Parser>
00034 #include <Soprano/PluginManager>
00035 #include <Soprano/StatementIterator>
00036
00037 #include <KDebug>
00038 #include <KLocale>
00039 #include <kio/job.h>
00040
00041
00042 class Nepomuk::GraphRetriever::Private
00043 {
00044 public:
00045 Private( Nepomuk::GraphRetriever* qq );
00046
00047 void get( const QUrl& url );
00048
00049 Nepomuk::GraphRetriever* q;
00050
00051 QUrl url;
00052 QHash<int, QByteArray> m_data;
00053 unsigned int m_idleCount;
00054 unsigned int m_timeoutThreshold;
00055 };
00056
00057
00058 Nepomuk::GraphRetriever::Private::Private( Nepomuk::GraphRetriever* qq )
00059 : q(qq),
00060 m_idleCount( 0 )
00061 {
00062 }
00063
00064
00065 void Nepomuk::GraphRetriever::Private::get( const QUrl& url )
00066 {
00067 KIO::StoredTransferJob* job = KIO::storedGet( url, KIO::Reload, KIO::HideProgressInfo );
00068 job->addMetaData( "accept",
00069 QString( "%1;q=0.2, %2" )
00070 .arg( Soprano::serializationMimeType( Soprano::SerializationRdfXml ) )
00071 .arg( Soprano::serializationMimeType( Soprano::SerializationTrig ) ) );
00072 job->addMetaData( "Charsets", "utf-8" );
00073
00074 connect( job, SIGNAL(result(KJob*)),
00075 q, SLOT(httpRequestFinished(KJob*)));
00076 }
00077
00078
00079 Nepomuk::GraphRetriever::GraphRetriever( QObject* parent )
00080 : KJob( parent ),
00081 d( new Private(this) )
00082 {
00083 }
00084
00085
00086 Nepomuk::GraphRetriever::~GraphRetriever()
00087 {
00088 delete d;
00089 }
00090
00091
00092 void Nepomuk::GraphRetriever::setUrl( const QUrl& url )
00093 {
00094 d->url = url;
00095 }
00096
00097
00098 QUrl Nepomuk::GraphRetriever::url() const
00099 {
00100 return d->url;
00101 }
00102
00103
00104 void Nepomuk::GraphRetriever::start()
00105 {
00106 d->get( d->url );
00107 }
00108
00109
00110 Soprano::Model* Nepomuk::GraphRetriever::model() const
00111 {
00112 Soprano::Model* result = Soprano::createModel();
00113 Soprano::StatementIterator it = statements();
00114 while ( it.next() ) {
00115 result->addStatement( *it );
00116 }
00117 return result;
00118 }
00119
00120
00121 Soprano::StatementIterator Nepomuk::GraphRetriever::statements() const
00122 {
00123 QByteArray data;
00124 Soprano::RdfSerialization serialization = Soprano::SerializationRdfXml;
00125 if ( d->m_data.contains( ( int )Soprano::SerializationTrig ) ) {
00126 serialization = Soprano::SerializationTrig;
00127 data = d->m_data[( int )Soprano::SerializationTrig];
00128 }
00129 else {
00130 serialization = Soprano::SerializationRdfXml;
00131 data = d->m_data[( int )Soprano::SerializationRdfXml];
00132 }
00133
00134 QTextStream stream( data );
00135 if ( const Soprano::Parser* parser =
00136 Soprano::PluginManager::instance()->discoverParserForSerialization( serialization ) ) {
00137 return parser->parseStream( stream, d->url, serialization );
00138 }
00139 else {
00140 return Soprano::StatementIterator();
00141 }
00142 }
00143
00144
00145 void Nepomuk::GraphRetriever::httpRequestFinished( KJob* job )
00146 {
00147 KIO::StoredTransferJob* tj = static_cast<KIO::StoredTransferJob*>( job );
00148
00149
00150 d->m_idleCount = 0;
00151
00152 QString mimetype = tj->mimetype();
00153 Soprano::RdfSerialization serialization = Soprano::mimeTypeToSerialization( mimetype );
00154 if ( serialization == Soprano::SerializationUser &&
00155 mimetype.contains( "xml", Qt::CaseInsensitive ) ) {
00156 serialization = Soprano::SerializationRdfXml;
00157 }
00158 if ( serialization != Soprano::SerializationUser )
00159 d->m_data[( int )serialization] = tj->data();
00160
00161 emitResult();
00162 }
00163
00164
00165 Nepomuk::GraphRetriever* Nepomuk::GraphRetriever::retrieve( const QUrl& uri )
00166 {
00167 GraphRetriever* gr = new GraphRetriever();
00168 gr->setUrl( uri );
00169 gr->start();
00170 return gr;
00171 }
00172
00173 #include "graphretriever.moc"