• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • sources
  • kde-4.12
  • kdelibs
  • nepomuk-core
  • services
  • storage
graphretriever.cpp
Go to the documentation of this file.
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>
4 
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.
9 
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.
14 
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.
19 */
20 
21 #include "graphretriever.h"
22 
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>
30 
31 #include <Soprano/Model>
32 #include <Soprano/Global>
33 #include <Soprano/Parser>
34 #include <Soprano/PluginManager>
35 #include <Soprano/StatementIterator>
36 
37 #include <KDebug>
38 #include <KLocale>
39 #include <kio/job.h>
40 
41 
42 class Nepomuk2::GraphRetriever::Private
43 {
44 public:
45  Private( Nepomuk2::GraphRetriever* qq );
46 
47  void get( const QUrl& url );
48 
49  Nepomuk2::GraphRetriever* q;
50 
51  QUrl url;
52  QHash<int, QByteArray> m_data;
53  unsigned int m_idleCount;
54  unsigned int m_timeoutThreshold;
55 };
56 
57 
58 Nepomuk2::GraphRetriever::Private::Private( Nepomuk2::GraphRetriever* qq )
59  : q(qq),
60  m_idleCount( 0 )
61 {
62 }
63 
64 
65 void Nepomuk2::GraphRetriever::Private::get( const QUrl& url )
66 {
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" );
73 
74  connect( job, SIGNAL(result(KJob*)),
75  q, SLOT(httpRequestFinished(KJob*)));
76 }
77 
78 
79 Nepomuk2::GraphRetriever::GraphRetriever( QObject* parent )
80  : KJob( parent ),
81  d( new Private(this) )
82 {
83 }
84 
85 
86 Nepomuk2::GraphRetriever::~GraphRetriever()
87 {
88  delete d;
89 }
90 
91 
92 void Nepomuk2::GraphRetriever::setUrl( const QUrl& url )
93 {
94  d->url = url;
95 }
96 
97 
98 QUrl Nepomuk2::GraphRetriever::url() const
99 {
100  return d->url;
101 }
102 
103 
104 void Nepomuk2::GraphRetriever::start()
105 {
106  d->get( d->url );
107 }
108 
109 
110 Soprano::Model* Nepomuk2::GraphRetriever::model() const
111 {
112  Soprano::Model* result = Soprano::createModel();
113  Soprano::StatementIterator it = statements();
114  while ( it.next() ) {
115  result->addStatement( *it );
116  }
117  return result;
118 }
119 
120 
121 Soprano::StatementIterator Nepomuk2::GraphRetriever::statements() const
122 {
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];
128  }
129  else {
130  serialization = Soprano::SerializationRdfXml;
131  data = d->m_data[( int )Soprano::SerializationRdfXml];
132  }
133 
134  QTextStream stream( data );
135  if ( const Soprano::Parser* parser =
136  Soprano::PluginManager::instance()->discoverParserForSerialization( serialization ) ) {
137  return parser->parseStream( stream, d->url, serialization );
138  }
139  else {
140  return Soprano::StatementIterator();
141  }
142 }
143 
144 
145 void Nepomuk2::GraphRetriever::httpRequestFinished( KJob* job )
146 {
147  KIO::StoredTransferJob* tj = static_cast<KIO::StoredTransferJob*>( job );
148 
149  // reset idle counter every time a request is finished
150  d->m_idleCount = 0;
151 
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;
157  }
158  if ( serialization != Soprano::SerializationUser )
159  d->m_data[( int )serialization] = tj->data();
160 
161  emitResult();
162 }
163 
164 
165 Nepomuk2::GraphRetriever* Nepomuk2::GraphRetriever::retrieve( const QUrl& uri )
166 {
167  GraphRetriever* gr = new GraphRetriever();
168  gr->setUrl( uri );
169  gr->start();
170  return gr;
171 }
172 
173 #include "graphretriever.moc"
Nepomuk2::GraphRetriever::url
QUrl url() const
Definition: graphretriever.cpp:98
Nepomuk2::GraphRetriever::GraphRetriever
GraphRetriever(QObject *parent=0)
Custom and default constructor.
Definition: graphretriever.cpp:79
Nepomuk2::GraphRetriever
Utility class for retrieving RDF graphs from Web locations.
Definition: graphretriever.h:54
Nepomuk2::GraphRetriever::model
Soprano::Model * model() const
Returns a new Soprano model constructed from the retrieved graph fragments.
Definition: graphretriever.cpp:110
graphretriever.h
Nepomuk2::GraphRetriever::~GraphRetriever
~GraphRetriever()
Default destructor.
Definition: graphretriever.cpp:86
QHash
QObject
Nepomuk2::GraphRetriever::start
void start()
Adds a get request for the given URL.
Definition: graphretriever.cpp:104
Nepomuk2::GraphRetriever::setUrl
void setUrl(const QUrl &url)
Definition: graphretriever.cpp:92
KJob
Nepomuk2::GraphRetriever::statements
Soprano::StatementIterator statements() const
Definition: graphretriever.cpp:121
Nepomuk2::GraphRetriever::retrieve
static GraphRetriever * retrieve(const QUrl &uri)
Definition: graphretriever.cpp:165
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk-Core

Skip menu "Nepomuk-Core"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal