File: utils.cpp

package info (click to toggle)
kde4libs 4%3A4.14.2-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 82,316 kB
  • sloc: cpp: 761,810; xml: 12,344; ansic: 6,295; java: 4,060; perl: 2,938; yacc: 2,507; python: 1,207; sh: 1,179; ruby: 337; lex: 278; makefile: 29
file content (217 lines) | stat: -rw-r--r-- 7,898 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
   This file is part of the Nepomuk KDE project.
   Copyright (C) 2010 Sebastian Trueg <trueg@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) version 3, or any
   later version accepted by the membership of KDE e.V. (or its
   successor approved by the membership of KDE e.V.), which shall
   act as a proxy defined in Section 6 of version 3 of the license.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "utils.h"

#include "variant.h"
#include "resourcemanager.h"
#include "resource.h"

#include "filequery.h"
#include "comparisonterm.h"
#include "andterm.h"
#include "resourceterm.h"
#include "resourcetypeterm.h"
#include "optionalterm.h"

#include "nie.h"
#include "nfo.h"
#include "nuao.h"
#include "ndo.h"

#include <kglobal.h>
#include <klocale.h>
#include <kmimetype.h>

#include <Soprano/Model>
#include <Soprano/QueryResultIterator>
#include <Soprano/NodeIterator>


QString Nepomuk::Utils::formatPropertyValue( const Nepomuk::Types::Property& property,
                                             const Nepomuk::Variant& value,
                                             const QList<Nepomuk::Resource>& resources,
                                             PropertyFormatFlags flags )
{
    // first handle lists
    if( value.isList() ) {
        QList<Variant> values = value.toVariantList();
        QStringList valueStrings;
        Q_FOREACH( const Variant& v, values ) {
            valueStrings << formatPropertyValue( property, v, resources, flags );
        }
        return valueStrings.join( QLatin1String(", ") );
    }

    //
    // We handle the one special case of referrer URLs of downloads
    // TODO: put stuff like this in a generic rule-based framework
    //
    if( property == Nepomuk::Vocabulary::NDO::copiedFrom() &&
        !resources.isEmpty() ) {
        Nepomuk::Query::Query query(
            Nepomuk::Query::AndTerm(
                Nepomuk::Query::ResourceTypeTerm(
                    Nepomuk::Vocabulary::NDO::DownloadEvent()
                    ),
                Nepomuk::Query::ComparisonTerm(
                    Nepomuk::Vocabulary::NUAO::involves(),
                    Nepomuk::Query::ResourceTerm(resources.first())
                    )
                )
            );
        query.setLimit(1);

        QList<Soprano::Node> results =
            Nepomuk::ResourceManager::instance()->mainModel()->executeQuery(
                query.toSparqlQuery(),
                Soprano::Query::QueryLanguageSparql).iterateBindings(0).allNodes();
        if( !results.isEmpty() ) {
            Nepomuk::Resource dlRes(results.first().uri());
            KUrl url;
            QString label;
            if( dlRes.hasProperty(Nepomuk::Vocabulary::NDO::referrer()) ) {
                Nepomuk::Resource referrerWebPage = dlRes.property(Nepomuk::Vocabulary::NDO::referrer()).toResource();
                url = referrerWebPage.property(Nepomuk::Vocabulary::NIE::url()).toUrl();
                KUrl referrerDomain(url);
                referrerDomain.setPath(QString());
                referrerDomain.setQuery(QString());
                label = referrerDomain.prettyUrl();
            }
            else {
                Nepomuk::Resource res(value.toResource());
                url = res.resourceUri();
                label = res.genericLabel();
            }

            if( flags & WithKioLinks ) {
                return QString::fromLatin1("<a href=\"%1\">%2</a>")
                    .arg(url.url(), label);
            }
            else {
                return label;
            }
        }
    }

    // do not use else here since the above code might fall through

    QString valueString;
    if (value.isDateTime()) {
        valueString = KGlobal::locale()->formatDateTime(value.toDateTime().toLocalTime(), KLocale::FancyLongDate);
    }

    else if(value.isDouble()) {
        valueString = KGlobal::locale()->formatNumber(value.toDouble());
    }

    else if(value.isInt() && property == Vocabulary::NFO::duration() ) {
        QTime time = QTime().addSecs( value.toInt() );
        valueString = KGlobal::locale()->formatTime( time, true, true );
    }

    else if(value.isResource() &&
            value.toResource().exists()) {
        valueString = value.toResource().genericLabel();
    }

    else if(property == Vocabulary::NIE::contentSize()) {
        valueString = KGlobal::locale()->formatByteSize(value.toDouble());
    }

    else if(property == Vocabulary::NIE::mimeType()) {
        KMimeType::Ptr mimeType = KMimeType::mimeType(value.toString());
        valueString = (mimeType ? mimeType->comment() : value.toString());
    }

    else {
        valueString = value.toString();
    }

    if( flags & WithKioLinks ) {
        // for all property/value pairs we create a default query
        Nepomuk::Query::FileQuery query( Nepomuk::Query::Term::fromProperty(property, value) );
        return QString::fromLatin1("<a href=\"%1\">%2</a>")
            .arg(query.toSearchUrl(property.label() + QLatin1String(": '") + valueString + '\'').url(),
                 valueString);
    }
    else {
        return valueString;
    }
}


Nepomuk::Resource Nepomuk::Utils::createCopyEvent( const KUrl& srcUrl, const KUrl& destUrl, const QDateTime& startTime, const KUrl& referrer )
{
    //
    // Remember where a file was downloaded from the semantic way:
    // We have two file resources:
    //   one for the source file (which in most cases is a remote file)
    //   and one for the destination file (which will be or is already indexed)
    // the latter is marked as being copied from the former
    // and then there is the download event which links to the referrer.
    //

    QUrl srcType;
    QUrl destType;
    if(srcUrl.isLocalFile()) {
        srcType = Nepomuk::Vocabulary::NFO::FileDataObject();
    }
    else {
        srcType = Nepomuk::Vocabulary::NFO::RemoteDataObject();
    }
    if(destUrl.isLocalFile()) {
        destType = Nepomuk::Vocabulary::NFO::FileDataObject();
    }
    else {
        destType = Nepomuk::Vocabulary::NFO::RemoteDataObject();
    }

    // source and dest resources
    Nepomuk::Resource srcFileRes(srcUrl, srcType);
    Nepomuk::Resource destFileRes(destUrl, destType);
    srcFileRes.setProperty(Nepomuk::Vocabulary::NIE::url(), srcUrl);
    destFileRes.setProperty(Nepomuk::Vocabulary::NIE::url(), destUrl);

    // relate src and dest
    destFileRes.setProperty(Nepomuk::Vocabulary::NDO::copiedFrom(), srcFileRes);

    // details in the download event
    Nepomuk::Resource downloadEventRes(QUrl(), Nepomuk::Vocabulary::NDO::DownloadEvent());
    downloadEventRes.addProperty(Nepomuk::Vocabulary::NUAO::involves(), destFileRes);
    downloadEventRes.addProperty(Nepomuk::Vocabulary::NUAO::start(), startTime);

    // set the referrer
    if(referrer.isValid()) {
        // TODO: we could at this point index the referrer site via strigi
        Nepomuk::Resource referrerRes(referrer, Nepomuk::Vocabulary::NFO::Website());
        downloadEventRes.addProperty(Nepomuk::Vocabulary::NDO::referrer(), referrerRes);
    }

    return downloadEventRes;
}


void Nepomuk::Utils::finishCopyEvent( Resource& /*eventResource*/, const QDateTime& /*endTime*/ )
{
    // FIXME: NUAO doesn't have end() yet.
    // eventResource.setProperty(Nepomuk::Vocabulary::NUAO::end(), endTime);
}