~ubuntu-branches/ubuntu/quantal/kde-runtime/quantal

« back to all changes in this revision

Viewing changes to nepomuk/kioslaves/nepomuk/resourcepagegenerator.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2012-06-03 21:50:00 UTC
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: package-import@ubuntu.com-20120603215000-vn7oarsq0ynrydj5
Tags: upstream-4.8.80
Import upstream version 4.8.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
   Copyright 2009 Sebastian Trueg <trueg@kde.org>
 
2
   Copyright 2009-2012 Sebastian Trueg <trueg@kde.org>
3
3
 
4
4
   This program is free software; you can redistribute it and/or
5
5
   modify it under the terms of the GNU General Public License as
31
31
#include <kdeversion.h>
32
32
#include <KUrl>
33
33
 
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>
40
42
 
41
43
#include <Soprano/Model>
42
44
#include <Soprano/Node>
46
48
#include <Soprano/Vocabulary/NAO>
47
49
 
48
50
 
 
51
using namespace Nepomuk2::Vocabulary;
 
52
 
49
53
namespace {
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";
53
58
 
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 );
56
61
 
57
62
        newUrl.removeEncodedQueryItem( s_noFollow );
60
65
        }
61
66
 
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 );
65
70
        }
66
71
 
 
72
        newUrl.removeEncodedQueryItem( s_showNonUserVisibible );
 
73
        if ( flags & Nepomuk2::ResourcePageGenerator::ShowNonUserVisible ) {
 
74
            newUrl.addEncodedQueryItem( s_showNonUserVisibible, s_true );
 
75
        }
 
76
 
67
77
        return newUrl;
68
78
    }
69
79
}
70
80
 
71
81
 
72
 
Nepomuk::ResourcePageGenerator::ResourcePageGenerator( const Nepomuk::Resource& res )
 
82
Nepomuk2::ResourcePageGenerator::ResourcePageGenerator( const Nepomuk2::Resource& res )
73
83
    : m_resource( res )
74
84
{
75
85
}
76
86
 
77
87
 
78
 
Nepomuk::ResourcePageGenerator::~ResourcePageGenerator()
 
88
Nepomuk2::ResourcePageGenerator::~ResourcePageGenerator()
79
89
{
80
90
}
81
91
 
82
92
 
83
 
void Nepomuk::ResourcePageGenerator::setFlagsFromUrl( const KUrl& url )
 
93
void Nepomuk2::ResourcePageGenerator::setFlagsFromUrl( const KUrl& url )
84
94
{
85
95
    m_flags = NoFlags;
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;
88
100
}
89
101
 
90
102
 
91
 
KUrl Nepomuk::ResourcePageGenerator::url() const
 
103
KUrl Nepomuk2::ResourcePageGenerator::url() const
92
104
{
93
105
    return configureUrl( m_resource.resourceUri(), m_flags );
94
106
}
95
107
 
96
108
 
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
99
111
{
100
112
    bool exists = m_resource.exists();
101
113
 
186
198
    os << "<h1>" << label << "</h1>"
187
199
       << "Type: " << ( exists ? typesToHtml( m_resource.types() ) : i18n( "Resource does not exist" ) );
188
200
 
 
201
    if(m_resource.isFile() && m_resource.hasType(Nepomuk2::Vocabulary::NFO::Image())) {
 
202
        os << "<img src=\"" << m_resource.resourceUri().toString() << "\" />";
 
203
    }
 
204
    else if(m_resource.hasProperty(Nepomuk2::Vocabulary::NFO::depiction())) {
 
205
        os << "<img src=\"" << m_resource.property(Nepomuk2::Vocabulary::NFO::depiction()).toUrlList().first().toString() << "\" />";
 
206
    }
 
207
 
189
208
    os << "<h2>" << i18n("Relations:") << "</h2><div id=\"relations\"><table>";
190
209
 
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 );
201
 
                else
202
 
                    os << s.object().toString();
 
219
                os << formatLiteral(p, s.object().literal());
203
220
            }
204
221
            else {
205
222
                //
209
226
                //
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() ) )
241
258
               << "</td>"
242
259
               << "<td width=16px></td>"
243
260
               << "<td><i>" << entityLabel( p ) << "</i></td></tr>";
257
274
}
258
275
 
259
276
 
260
 
QString Nepomuk::ResourcePageGenerator::resourceLabel( const Resource& res ) const
 
277
QString Nepomuk2::ResourcePageGenerator::resourceLabel( const Resource& res ) const
261
278
{
262
279
    if ( m_flags & ShowUris )
263
280
        return KUrl( res.resourceUri() ).prettyUrl();
266
283
}
267
284
 
268
285
 
269
 
QString Nepomuk::ResourcePageGenerator::entityLabel( const Nepomuk::Types::Entity& e ) const
 
286
QString Nepomuk2::ResourcePageGenerator::entityLabel( const Nepomuk2::Types::Entity& e ) const
270
287
{
271
288
    if ( m_flags & ShowUris )
272
289
        return KUrl( e.uri() ).prettyUrl();
275
292
}
276
293
 
277
294
 
278
 
QString Nepomuk::ResourcePageGenerator::typesToHtml( const QList<QUrl>& types ) const
 
295
QString Nepomuk2::ResourcePageGenerator::typesToHtml( const QList<QUrl>& types ) const
279
296
{
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 );
283
300
    }
284
301
 
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];
289
306
        bool use = true;
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] ));
306
326
    }
307
327
 
308
328
    return typeStrings.join( ", " );
309
329
}
310
330
 
311
331
 
312
 
QString Nepomuk::ResourcePageGenerator::encodeUrl( const QUrl& url ) const
 
332
QString Nepomuk2::ResourcePageGenerator::encodeUrl( const QUrl& url ) const
313
333
{
314
334
    return QString::fromAscii( configureUrl( url, m_flags ).toEncoded() );
315
335
}
316
336
 
317
337
 
318
 
QString Nepomuk::ResourcePageGenerator::createConfigureBoxHtml() const
 
338
QString Nepomuk2::ResourcePageGenerator::createConfigureBoxHtml() const
319
339
{
320
340
    QString html
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" ));
324
346
 
325
347
    return html;
326
348
}
 
349
 
 
350
QString Nepomuk2::ResourcePageGenerator::formatLiteral(const Nepomuk2::Types::Property &p, const Soprano::LiteralValue &value) const
 
351
{
 
352
    return Nepomuk2::Utils::formatPropertyValue(p, Nepomuk2::Variant(value.variant()));
 
353
}
 
354
 
 
355
QString Nepomuk2::ResourcePageGenerator::formatResource(const Nepomuk2::Types::Property &p, const QUrl &uri_) const
 
356
{
 
357
    //
 
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.
 
361
    //
 
362
    KUrl uri(uri_);
 
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() ) );
 
370
    }
 
371
    return QString( "<a href=\"%1\">%2</a>" )
 
372
            .arg( encodeUrl( uri ),
 
373
                  label );
 
374
}