31
31
#include <kdeversion.h>
34
#include <nepomuk/resource.h>
35
#include <nepomuk/resourcemanager.h>
36
#include <nepomuk/variant.h>
37
#include <nepomuk/class.h>
38
#include <nepomuk/property.h>
39
#include <Nepomuk/Vocabulary/NIE>
34
#include <nepomuk2/file.h>
35
#include <nepomuk2/resourcemanager.h>
36
#include <nepomuk2/variant.h>
37
#include <nepomuk2/class.h>
38
#include <nepomuk2/property.h>
39
#include <Nepomuk2/Vocabulary/NIE>
40
#include <Nepomuk2/Vocabulary/NFO>
41
#include <nepomuk2/utils.h>
41
43
#include <Soprano/Model>
42
44
#include <Soprano/Node>
46
48
#include <Soprano/Vocabulary/NAO>
51
using namespace Nepomuk2::Vocabulary;
50
54
const char* s_noFollow = "noFollow";
51
55
const char* s_showUri = "showUris";
56
const char* s_showNonUserVisibible = "showNonUserVisible";
52
57
const char* s_true = "true";
54
KUrl configureUrl( const KUrl& url, Nepomuk::ResourcePageGenerator::Flags flags ) {
59
KUrl configureUrl( const KUrl& url, Nepomuk2::ResourcePageGenerator::Flags flags ) {
55
60
KUrl newUrl( url );
57
62
newUrl.removeEncodedQueryItem( s_noFollow );
62
67
newUrl.removeEncodedQueryItem( s_showUri );
63
if ( flags & Nepomuk::ResourcePageGenerator::ShowUris ) {
68
if ( flags & Nepomuk2::ResourcePageGenerator::ShowUris ) {
64
69
newUrl.addEncodedQueryItem( s_showUri, s_true );
72
newUrl.removeEncodedQueryItem( s_showNonUserVisibible );
73
if ( flags & Nepomuk2::ResourcePageGenerator::ShowNonUserVisible ) {
74
newUrl.addEncodedQueryItem( s_showNonUserVisibible, s_true );
72
Nepomuk::ResourcePageGenerator::ResourcePageGenerator( const Nepomuk::Resource& res )
82
Nepomuk2::ResourcePageGenerator::ResourcePageGenerator( const Nepomuk2::Resource& res )
73
83
: m_resource( res )
78
Nepomuk::ResourcePageGenerator::~ResourcePageGenerator()
88
Nepomuk2::ResourcePageGenerator::~ResourcePageGenerator()
83
void Nepomuk::ResourcePageGenerator::setFlagsFromUrl( const KUrl& url )
93
void Nepomuk2::ResourcePageGenerator::setFlagsFromUrl( const KUrl& url )
86
96
if ( url.encodedQueryItemValue( s_showUri ) == s_true )
87
97
m_flags |= ShowUris;
98
if ( url.encodedQueryItemValue( s_showNonUserVisibible ) == s_true )
99
m_flags |= ShowNonUserVisible;
91
KUrl Nepomuk::ResourcePageGenerator::url() const
103
KUrl Nepomuk2::ResourcePageGenerator::url() const
93
105
return configureUrl( m_resource.resourceUri(), m_flags );
97
109
// TODO: create an html template rather than having it hardcoded here
98
QByteArray Nepomuk::ResourcePageGenerator::generatePage() const
110
QByteArray Nepomuk2::ResourcePageGenerator::generatePage() const
100
112
bool exists = m_resource.exists();
186
198
os << "<h1>" << label << "</h1>"
187
199
<< "Type: " << ( exists ? typesToHtml( m_resource.types() ) : i18n( "Resource does not exist" ) );
201
if(m_resource.isFile() && m_resource.hasType(Nepomuk2::Vocabulary::NFO::Image())) {
202
os << "<img src=\"" << m_resource.resourceUri().toString() << "\" />";
204
else if(m_resource.hasProperty(Nepomuk2::Vocabulary::NFO::depiction())) {
205
os << "<img src=\"" << m_resource.property(Nepomuk2::Vocabulary::NFO::depiction()).toUrlList().first().toString() << "\" />";
189
208
os << "<h2>" << i18n("Relations:") << "</h2><div id=\"relations\"><table>";
191
210
// query relations
192
211
Soprano::StatementIterator it = ResourceManager::instance()->mainModel()->listStatements( m_resource.resourceUri(), Soprano::Node(), Soprano::Node() );
193
212
while ( it.next() ) {
194
213
Soprano::Statement s = it.current();
195
if ( s.predicate().uri() != Soprano::Vocabulary::RDF::type() ) {
196
Nepomuk::Types::Property p( s.predicate().uri() );
214
Nepomuk2::Types::Property p( s.predicate().uri() );
215
if ( p != Soprano::Vocabulary::RDF::type() &&
216
((m_flags & ShowNonUserVisible) || p.userVisible())) {
197
217
os << "<tr><td align=right><i>" << entityLabel( p ) << "</i></td><td width=16px></td><td>";
198
218
if ( s.object().isLiteral() ) {
199
if ( s.object().literal().isDateTime() )
200
os << KGlobal::locale()->formatDateTime( s.object().literal().toDateTime(), KLocale::FancyShortDate );
202
os << s.object().toString();
219
os << formatLiteral(p, s.object().literal());
210
227
KUrl uri = s.object().uri();
211
228
QString label = uri.fileName();
212
if ( s.predicate() != Nepomuk::Vocabulary::NIE::url() ) {
229
if ( s.predicate() != Nepomuk2::Vocabulary::NIE::url() ) {
213
230
Resource resource( uri );
214
231
uri = resource.resourceUri();
215
232
label = QString::fromLatin1( "%1 (%2)" )
230
247
Soprano::StatementIterator itb = ResourceManager::instance()->mainModel()->listStatements( Soprano::Node(), Soprano::Node(), m_resource.resourceUri() );
231
248
while ( itb.next() ) {
232
249
Soprano::Statement s = itb.current();
233
if ( s.predicate().uri() != Soprano::Vocabulary::RDF::type() ) {
234
Resource resource( s.subject().uri() );
235
Nepomuk::Types::Property p( s.predicate().uri() );
250
Resource resource( s.subject().uri() );
251
Nepomuk2::Types::Property p( s.predicate().uri() );
252
if((m_flags & ShowNonUserVisible) || p.userVisible()) {
236
253
os << "<td align=right>"
237
254
<< QString( "<a href=\"%1\">%2</a> (%3)" )
238
.arg( encodeUrl( s.subject().uri() ),
239
resourceLabel( resource ),
240
typesToHtml( resource.types() ) )
255
.arg( encodeUrl( s.subject().uri() ),
256
resourceLabel( resource ),
257
typesToHtml( resource.types() ) )
242
259
<< "<td width=16px></td>"
243
260
<< "<td><i>" << entityLabel( p ) << "</i></td></tr>";
278
QString Nepomuk::ResourcePageGenerator::typesToHtml( const QList<QUrl>& types ) const
295
QString Nepomuk2::ResourcePageGenerator::typesToHtml( const QList<QUrl>& types ) const
280
QList<Nepomuk::Types::Class> typeClasses;
297
QList<Nepomuk2::Types::Class> typeClasses;
281
298
foreach( const QUrl& type, types ) {
282
typeClasses << Nepomuk::Types::Class( type );
299
typeClasses << Nepomuk2::Types::Class( type );
285
302
// remove all types that are supertypes of others in the list
286
QList<Nepomuk::Types::Class> normalizedTypes;
303
QList<Nepomuk2::Types::Class> normalizedTypes;
287
304
for ( int i = 0; i < typeClasses.count(); ++i ) {
288
Nepomuk::Types::Class& type = typeClasses[i];
305
Nepomuk2::Types::Class& type = typeClasses[i];
290
307
for ( int j = 0; j < typeClasses.count(); ++j ) {
291
308
if ( type != typeClasses[j] &&
302
319
// extract the labels
303
320
QStringList typeStrings;
304
321
for ( int i = 0; i < normalizedTypes.count(); ++i ) {
305
typeStrings << entityLabel( normalizedTypes[i] );
322
QUrl url("nepomuk:");
323
url.addQueryItem(QLatin1String("resource"), normalizedTypes[i].uri().toString());
324
url.addQueryItem(QLatin1String("noFollow"), QLatin1String("true"));
325
typeStrings << QString::fromLatin1("<a href=\"nepomuk:?resource=%1&noFollow=true\">%2</a>").arg(QUrl::toPercentEncoding(normalizedTypes[i].uri().toString(), "", "/:"), entityLabel( normalizedTypes[i] ));
308
328
return typeStrings.join( ", " );
312
QString Nepomuk::ResourcePageGenerator::encodeUrl( const QUrl& url ) const
332
QString Nepomuk2::ResourcePageGenerator::encodeUrl( const QUrl& url ) const
314
334
return QString::fromAscii( configureUrl( url, m_flags ).toEncoded() );
318
QString Nepomuk::ResourcePageGenerator::createConfigureBoxHtml() const
338
QString Nepomuk2::ResourcePageGenerator::createConfigureBoxHtml() const
321
= QString::fromLatin1( "<div style=\"position:fixed; right:10px; top:10px; text-align:right;\"><a href=\"%1\">%2</a></div>" )
341
= QString::fromLatin1( "<div style=\"position:fixed; right:10px; top:10px; text-align:right;\"><a href=\"%1\">%2</a><br/><a href=\"%3\">%4</a></div>" )
322
342
.arg( configureUrl( url(), m_flags^ShowUris ).url(),
323
m_flags&ShowUris ? i18n( "Hide URIs" ) : i18n( "Show URIs" ) );
343
m_flags&ShowUris ? i18n( "Hide URIs" ) : i18n( "Show URIs" ),
344
configureUrl( url(), m_flags^ShowNonUserVisible ).url(),
345
m_flags&ShowNonUserVisible ? i18n( "Hide non-user visible properties" ) : i18n( "Show non-user visible properties" ));
350
QString Nepomuk2::ResourcePageGenerator::formatLiteral(const Nepomuk2::Types::Property &p, const Soprano::LiteralValue &value) const
352
return Nepomuk2::Utils::formatPropertyValue(p, Nepomuk2::Variant(value.variant()));
355
QString Nepomuk2::ResourcePageGenerator::formatResource(const Nepomuk2::Types::Property &p, const QUrl &uri_) const
358
// nie:url is a special case for which we should never use Resource
359
// since Resource does in turn use nie:url to resolve resource URIs.
360
// Thus, we would get back to m_resource.
363
QString label = uri.fileName();
364
if ( p != NIE::url() ) {
365
Resource resource( uri );
366
uri = resource.resourceUri();
367
label = QString::fromLatin1( "%1 (%2)" )
368
.arg( resourceLabel( resource ),
369
typesToHtml( resource.types() ) );
371
return QString( "<a href=\"%1\">%2</a>" )
372
.arg( encodeUrl( uri ),