delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / nepomuk / services / ontologyloader / graphretriever.cpp
blobbef75de8934c640039fcfc7ec2b96a4405e58106
1 /* This file is part of the KDE semantic clipboard
2 Copyright (C) 2008 Tobias Wolf <twolf@access.unizh.ch>
3 Copyright (C) 2008 Sebastian Trueg <trueg@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 #include "graphretriever.h"
23 #include <QtCore/QByteArray>
24 #include <QtCore/QEventLoop>
25 #include <QtCore/QHash>
26 #include <QtCore/QPair>
27 #include <QtCore/QString>
28 #include <QtCore/QTextStream>
29 #include <QtCore/QUrl>
31 #include <Soprano/Model>
32 #include <Soprano/Global>
33 #include <Soprano/Parser>
34 #include <Soprano/PluginManager>
35 #include <Soprano/StatementIterator>
37 #include <KDebug>
38 #include <KLocale>
39 #include <kio/job.h>
42 class Nepomuk::GraphRetriever::Private
44 public:
45 Private( Nepomuk::GraphRetriever* qq );
47 void get( const QUrl& url );
49 Nepomuk::GraphRetriever* q;
51 QUrl url;
52 QHash<int, QByteArray> m_data;
53 unsigned int m_idleCount;
54 unsigned int m_timeoutThreshold;
58 Nepomuk::GraphRetriever::Private::Private( Nepomuk::GraphRetriever* qq )
59 : q(qq),
60 m_idleCount( 0 )
65 void Nepomuk::GraphRetriever::Private::get( const QUrl& url )
67 KIO::StoredTransferJob* job = KIO::storedGet( url, KIO::Reload, KIO::HideProgressInfo );
68 job->addMetaData( "accept",
69 QString( "%1;q=0.2, %2" )
70 .arg( Soprano::serializationMimeType( Soprano::SerializationRdfXml ) )
71 .arg( Soprano::serializationMimeType( Soprano::SerializationTrig ) ) );
72 job->addMetaData( "Charsets", "utf-8" );
74 connect( job, SIGNAL(result(KJob*)),
75 q, SLOT(httpRequestFinished(KJob*)));
79 Nepomuk::GraphRetriever::GraphRetriever( QObject* parent )
80 : KJob( parent ),
81 d( new Private(this) )
86 Nepomuk::GraphRetriever::~GraphRetriever()
88 delete d;
92 void Nepomuk::GraphRetriever::setUrl( const QUrl& url )
94 d->url = url;
98 QUrl Nepomuk::GraphRetriever::url() const
100 return d->url;
104 void Nepomuk::GraphRetriever::start()
106 d->get( d->url );
110 Soprano::Model* Nepomuk::GraphRetriever::model() const
112 Soprano::Model* result = Soprano::createModel();
113 Soprano::StatementIterator it = statements();
114 while ( it.next() ) {
115 result->addStatement( *it );
117 return result;
121 Soprano::StatementIterator Nepomuk::GraphRetriever::statements() const
123 QByteArray data;
124 Soprano::RdfSerialization serialization = Soprano::SerializationRdfXml;
125 if ( d->m_data.contains( ( int )Soprano::SerializationTrig ) ) {
126 serialization = Soprano::SerializationTrig;
127 data = d->m_data[( int )Soprano::SerializationTrig];
129 else {
130 serialization = Soprano::SerializationRdfXml;
131 data = d->m_data[( int )Soprano::SerializationRdfXml];
134 QTextStream stream( data );
135 if ( const Soprano::Parser* parser =
136 Soprano::PluginManager::instance()->discoverParserForSerialization( serialization ) ) {
137 return parser->parseStream( stream, d->url, serialization );
139 else {
140 return Soprano::StatementIterator();
145 void Nepomuk::GraphRetriever::httpRequestFinished( KJob* job )
147 KIO::StoredTransferJob* tj = static_cast<KIO::StoredTransferJob*>( job );
149 // reset idle counter every time a request is finished
150 d->m_idleCount = 0;
152 QString mimetype = tj->mimetype();
153 Soprano::RdfSerialization serialization = Soprano::mimeTypeToSerialization( mimetype );
154 if ( serialization == Soprano::SerializationUser &&
155 mimetype.contains( "xml", Qt::CaseInsensitive ) ) {
156 serialization = Soprano::SerializationRdfXml;
158 if ( serialization != Soprano::SerializationUser )
159 d->m_data[( int )serialization] = tj->data();
161 emitResult();
165 Nepomuk::GraphRetriever* Nepomuk::GraphRetriever::retrieve( const QUrl& uri )
167 GraphRetriever* gr = new GraphRetriever();
168 gr->setUrl( uri );
169 gr->start();
170 return gr;
173 #include "graphretriever.moc"