~ubuntu-branches/ubuntu/natty/kdebase-runtime/natty-proposed

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-11-24 11:07:10 UTC
  • mto: (0.8.7 upstream)
  • mto: This revision was merged to the branch mainline in revision 129.
  • Revision ID: james.westby@ubuntu.com-20101124110710-6dbsyw0yh21qvn82
Tags: upstream-4.5.80
ImportĀ upstreamĀ versionĀ 4.5.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
*/
20
20
 
21
21
#include "resourcepagegenerator.h"
22
 
#include "nie.h"
23
22
 
24
23
#include <QtCore/QByteArray>
25
24
#include <QtCore/QTextStream>
37
36
#include <nepomuk/variant.h>
38
37
#include <nepomuk/class.h>
39
38
#include <nepomuk/property.h>
 
39
#include <Nepomuk/Vocabulary/NIE>
40
40
 
41
41
#include <Soprano/Model>
42
42
#include <Soprano/Node>
47
47
 
48
48
 
49
49
namespace {
50
 
    QString typesToHtml( const QList<QUrl>& types )
51
 
    {
52
 
        QList<Nepomuk::Types::Class> typeClasses;
53
 
        foreach( const QUrl& type, types ) {
54
 
            typeClasses << Nepomuk::Types::Class( type );
55
 
        }
56
 
 
57
 
        // remove all types that are supertypes of others in the list
58
 
        QList<Nepomuk::Types::Class> normalizedTypes;
59
 
        for ( int i = 0; i < typeClasses.count(); ++i ) {
60
 
            Nepomuk::Types::Class& type = typeClasses[i];
61
 
            bool use = true;
62
 
            for ( int j = 0; j < typeClasses.count(); ++j ) {
63
 
                if ( type != typeClasses[j] &&
64
 
                     typeClasses[j].isSubClassOf( type ) ) {
65
 
                    use = false;
66
 
                    break;
67
 
                }
68
 
            }
69
 
            if ( use ) {
70
 
                normalizedTypes << type;
71
 
            }
72
 
        }
73
 
 
74
 
        // extract the labels
75
 
        QStringList typeStrings;
76
 
        for ( int i = 0; i < normalizedTypes.count(); ++i ) {
77
 
            typeStrings << normalizedTypes[i].label();
78
 
        }
79
 
 
80
 
        return typeStrings.join( ", " );
81
 
    }
82
 
 
83
 
    QString encodeUrl( const QUrl& u ) {
84
 
        QUrl url( u );
 
50
    const char* s_noFollow = "noFollow";
 
51
    const char* s_showUri = "showUris";
 
52
    const char* s_true = "true";
 
53
 
 
54
    KUrl configureUrl( const KUrl& url, Nepomuk::ResourcePageGenerator::Flags flags ) {
 
55
        KUrl newUrl( url );
 
56
 
 
57
        newUrl.removeEncodedQueryItem( s_noFollow );
85
58
        if ( url.scheme() == QLatin1String( "nepomuk" ) ) {
86
 
            url.setEncodedQuery( "noFollow=true" );
87
 
        }
88
 
        return QString::fromAscii( url.toEncoded() );
 
59
            newUrl.addEncodedQueryItem( s_noFollow, s_true );
 
60
        }
 
61
 
 
62
        newUrl.removeEncodedQueryItem( s_showUri );
 
63
        if ( flags & Nepomuk::ResourcePageGenerator::ShowUris ) {
 
64
            newUrl.addEncodedQueryItem( s_showUri, s_true );
 
65
        }
 
66
 
 
67
        return newUrl;
89
68
    }
90
69
}
91
70
 
101
80
}
102
81
 
103
82
 
 
83
void Nepomuk::ResourcePageGenerator::setFlagsFromUrl( const KUrl& url )
 
84
{
 
85
    m_flags = NoFlags;
 
86
    if ( url.encodedQueryItemValue( s_showUri ) == s_true )
 
87
        m_flags |= ShowUris;
 
88
}
 
89
 
 
90
 
 
91
KUrl Nepomuk::ResourcePageGenerator::url() const
 
92
{
 
93
    return configureUrl( m_resource.resourceUri(), m_flags );
 
94
}
 
95
 
 
96
 
104
97
// TODO: create an html template rather than having it hardcoded here
105
98
QByteArray Nepomuk::ResourcePageGenerator::generatePage() const
106
99
{
185
178
        "#relations { "
186
179
        "  padding: 0 20 0 20; }"
187
180
        "</style></head>"
188
 
       << "<body><div id=\"body_wrapper\"><div id=\"body\"><div class=\"content\"><div id=\"main\"><div class=\"clearer\">&nbsp;</div>";
 
181
       << "<body>"
 
182
       << createConfigureBoxHtml()
 
183
       << "<div id=\"body_wrapper\"><div id=\"body\"><div class=\"content\"><div id=\"main\"><div class=\"clearer\">&nbsp;</div>";
189
184
 
190
185
    os << "<h1>" << label << "</h1>"
191
186
       << "Type: " << ( exists ? typesToHtml( m_resource.types() ) : i18n( "Resource does not exist" ) );
198
193
        Soprano::Statement s = it.current();
199
194
        if ( s.predicate().uri() != Soprano::Vocabulary::RDF::type() ) {
200
195
            Nepomuk::Types::Property p( s.predicate().uri() );
201
 
            os << "<tr><td align=right><i>" << ( p.label().isEmpty() ? p.name() : p.label() ) << "</i></td><td width=16px></td><td>";
 
196
            os << "<tr><td align=right><i>" << entityLabel( p ) << "</i></td><td width=16px></td><td>";
202
197
            if ( s.object().isLiteral() ) {
203
198
                if ( s.object().literal().isDateTime() )
204
199
                    os << KGlobal::locale()->formatDateTime( s.object().literal().toDateTime(), KLocale::FancyShortDate );
217
212
                    Resource resource( uri );
218
213
                    uri = resource.resourceUri();
219
214
                    label = QString::fromLatin1( "%1 (%2)" )
220
 
                            .arg( resource.genericLabel(),
 
215
                            .arg( resourceLabel( resource ),
221
216
                                  typesToHtml( resource.types() ) );
222
217
                }
223
218
                os << QString( "<a href=\"%1\">%2</a>" )
240
235
            os << "<td align=right>"
241
236
               << QString( "<a href=\"%1\">%2</a> (%3)" )
242
237
                .arg( encodeUrl( s.subject().uri() ),
243
 
                      resource.genericLabel(),
 
238
                      resourceLabel( resource ),
244
239
                      typesToHtml( resource.types() ) )
245
240
               << "</td>"
246
241
               << "<td width=16px></td>"
247
 
               << "<td><i>" << ( p.label().isEmpty() ? p.name() : p.label() ) << "</i></td></tr>";
 
242
               << "<td><i>" << entityLabel( p ) << "</i></td></tr>";
248
243
        }
249
244
    }
250
245
    os << "</table></div>";
259
254
 
260
255
    return output;
261
256
}
 
257
 
 
258
 
 
259
QString Nepomuk::ResourcePageGenerator::resourceLabel( const Resource& res ) const
 
260
{
 
261
    if ( m_flags & ShowUris )
 
262
        return KUrl( res.resourceUri() ).prettyUrl();
 
263
    else
 
264
        return res.genericLabel();
 
265
}
 
266
 
 
267
 
 
268
QString Nepomuk::ResourcePageGenerator::entityLabel( const Nepomuk::Types::Entity& e ) const
 
269
{
 
270
    if ( m_flags & ShowUris )
 
271
        return KUrl( e.uri() ).prettyUrl();
 
272
    else
 
273
        return e.label();
 
274
}
 
275
 
 
276
 
 
277
QString Nepomuk::ResourcePageGenerator::typesToHtml( const QList<QUrl>& types ) const
 
278
{
 
279
    QList<Nepomuk::Types::Class> typeClasses;
 
280
    foreach( const QUrl& type, types ) {
 
281
        typeClasses << Nepomuk::Types::Class( type );
 
282
    }
 
283
 
 
284
    // remove all types that are supertypes of others in the list
 
285
    QList<Nepomuk::Types::Class> normalizedTypes;
 
286
    for ( int i = 0; i < typeClasses.count(); ++i ) {
 
287
        Nepomuk::Types::Class& type = typeClasses[i];
 
288
        bool use = true;
 
289
        for ( int j = 0; j < typeClasses.count(); ++j ) {
 
290
            if ( type != typeClasses[j] &&
 
291
                 typeClasses[j].isSubClassOf( type ) ) {
 
292
                use = false;
 
293
                break;
 
294
            }
 
295
        }
 
296
        if ( use ) {
 
297
            normalizedTypes << type;
 
298
        }
 
299
    }
 
300
 
 
301
    // extract the labels
 
302
    QStringList typeStrings;
 
303
    for ( int i = 0; i < normalizedTypes.count(); ++i ) {
 
304
        typeStrings << entityLabel( normalizedTypes[i] );
 
305
    }
 
306
 
 
307
    return typeStrings.join( ", " );
 
308
}
 
309
 
 
310
 
 
311
QString Nepomuk::ResourcePageGenerator::encodeUrl( const QUrl& url ) const
 
312
{
 
313
    return QString::fromAscii( configureUrl( url, m_flags ).toEncoded() );
 
314
}
 
315
 
 
316
 
 
317
QString Nepomuk::ResourcePageGenerator::createConfigureBoxHtml() const
 
318
{
 
319
    QString html
 
320
        = QString::fromLatin1( "<div style=\"position:fixed; right:10px; top:10px; text-align:right;\"><a href=\"%1\">%2</a></div>" )
 
321
        .arg( configureUrl( url(), m_flags^ShowUris ).url(),
 
322
              m_flags&ShowUris ? i18n( "Hide URIs" ) : i18n( "Show URIs" ) );
 
323
 
 
324
    return html;
 
325
}