• 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
  • libnepomukcore
  • misc
utils.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Nepomuk KDE project.
3  Copyright (C) 2010 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 Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) version 3, or any
9  later version accepted by the membership of KDE e.V. (or its
10  successor approved by the membership of KDE e.V.), which shall
11  act as a proxy defined in Section 6 of version 3 of the license.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "utils.h"
23 
24 #include "variant.h"
25 #include "resourcemanager.h"
26 #include "resource.h"
27 
28 #include "filequery.h"
29 #include "comparisonterm.h"
30 #include "andterm.h"
31 #include "resourceterm.h"
32 #include "resourcetypeterm.h"
33 #include "optionalterm.h"
34 #include "literal.h"
35 
36 #include "nie.h"
37 #include "nfo.h"
38 #include "nuao.h"
39 #include "ndo.h"
40 
41 #include <kglobal.h>
42 #include <klocale.h>
43 #include <kmimetype.h>
44 
45 #include <Soprano/Model>
46 #include <Soprano/QueryResultIterator>
47 #include <Soprano/NodeIterator>
48 
49 
50 QString Nepomuk2::Utils::formatPropertyValue( const Nepomuk2::Types::Property& property,
51  const Nepomuk2::Variant& value,
52  const QList<Nepomuk2::Resource>& resources,
53  PropertyFormatFlags flags )
54 {
55  // first handle lists
56  if( value.isList() ) {
57  QList<Variant> values = value.toVariantList();
58  QStringList valueStrings;
59  Q_FOREACH( const Variant& v, values ) {
60  valueStrings << formatPropertyValue( property, v, resources, flags );
61  }
62  return valueStrings.join( QLatin1String(", ") );
63  }
64 
65  //
66  // We handle the one special case of referrer URLs of downloads
67  // TODO: put stuff like this in a generic rule-based framework
68  //
69  if( property == Nepomuk2::Vocabulary::NDO::copiedFrom() &&
70  !resources.isEmpty() ) {
71  Nepomuk2::Query::Query query(
72  Nepomuk2::Query::AndTerm(
73  Nepomuk2::Query::ResourceTypeTerm(
74  Nepomuk2::Vocabulary::NDO::DownloadEvent()
75  ),
76  Nepomuk2::Query::ComparisonTerm(
77  Nepomuk2::Vocabulary::NUAO::involves(),
78  Nepomuk2::Query::ResourceTerm(resources.first())
79  )
80  )
81  );
82  query.setLimit(1);
83 
84  QList<Soprano::Node> results =
85  Nepomuk2::ResourceManager::instance()->mainModel()->executeQuery(
86  query.toSparqlQuery(),
87  Soprano::Query::QueryLanguageSparql).iterateBindings(0).allNodes();
88  if( !results.isEmpty() ) {
89  Nepomuk2::Resource dlRes(results.first().uri());
90  KUrl url;
91  QString label;
92  if( dlRes.hasProperty(Nepomuk2::Vocabulary::NDO::referrer()) ) {
93  Nepomuk2::Resource referrerWebPage = dlRes.property(Nepomuk2::Vocabulary::NDO::referrer()).toResource();
94  url = referrerWebPage.property(Nepomuk2::Vocabulary::NIE::url()).toUrl();
95  KUrl referrerDomain(url);
96  referrerDomain.setPath(QString());
97  referrerDomain.setQuery(QString());
98  label = referrerDomain.prettyUrl();
99  }
100  else {
101  Nepomuk2::Resource res(value.toResource());
102  url = res.uri();
103  label = res.genericLabel();
104  }
105 
106  if( flags & WithKioLinks ) {
107  return QString::fromLatin1("<a href=\"%1\">%2</a>")
108  .arg(url.url(), label);
109  }
110  else {
111  return label;
112  }
113  }
114  }
115 
116  // do not use else here since the above code might fall through
117  QString valueString;
118  KUrl additionalResourceUri;
119 
120  if(value.isResource() && value.toResource().exists()) {
121  valueString = value.toResource().genericLabel();
122  additionalResourceUri = value.toUrl();
123  }
124 
125  else if(property == Vocabulary::NIE::contentSize()) {
126  valueString = KGlobal::locale()->formatByteSize(value.toDouble());
127  }
128 
129  else if(property == Vocabulary::NIE::mimeType()) {
130  KMimeType::Ptr mimeType = KMimeType::mimeType(value.toString());
131  valueString = (mimeType ? mimeType->comment() : value.toString());
132  }
133 
134  else if(property == Vocabulary::NFO::duration() ) {
135  QTime time = QTime().addSecs( value.toInt() );
136  valueString = KGlobal::locale()->formatTime( time, true, true );
137  }
138 
139  if( valueString.isEmpty() ) {
140  Types::Literal literalRange = property.literalRangeType();
141  QVariant::Type dataType = literalRange.dataType();
142  if( !literalRange.isValid() )
143  dataType = value.variant().type();
144 
145  switch( dataType ) {
146  case QVariant::DateTime:
147  valueString = KGlobal::locale()->formatDateTime(value.toDateTime(), KLocale::FancyLongDate);
148  break;
149  case QVariant::Date:
150  valueString = KGlobal::locale()->formatDate(value.toDate(), KLocale::FancyLongDate);
151  break;
152  case QVariant::Int:
153  valueString = KGlobal::locale()->formatLong(value.toInt());
154  break;
155  case QVariant::Double:
156  valueString = KGlobal::locale()->formatNumber(value.toDouble());
157  break;
158  default:
159  valueString = value.toString();
160  break;
161  }
162  }
163 
164  if( flags & WithKioLinks ) {
165  // for all property/value pairs we create a default query
166  Nepomuk2::Query::FileQuery query( Nepomuk2::Query::Term::fromProperty(property, value) );
167  KUrl searchUrl = query.toSearchUrl(property.label() + QLatin1String(": '") + valueString + '\'');
168  if( !additionalResourceUri.isEmpty() ) {
169  searchUrl.addQueryItem( QLatin1String("resource"), additionalResourceUri.url() );
170  }
171 
172  return QString::fromLatin1("<a href=\"%1\">%2</a>").arg( searchUrl.url(), valueString);
173  }
174  else {
175  return valueString;
176  }
177 }
178 
179 
180 Nepomuk2::Resource Nepomuk2::Utils::createCopyEvent( const KUrl& srcUrl, const KUrl& destUrl, const QDateTime& startTime, const KUrl& referrer )
181 {
182  //
183  // Remember where a file was downloaded from the semantic way:
184  // We have two file resources:
185  // one for the source file (which in most cases is a remote file)
186  // and one for the destination file (which will be or is already indexed)
187  // the latter is marked as being copied from the former
188  // and then there is the download event which links to the referrer.
189  //
190 
191  QUrl srcType;
192  QUrl destType;
193  if(srcUrl.isLocalFile()) {
194  srcType = Nepomuk2::Vocabulary::NFO::FileDataObject();
195  }
196  else {
197  srcType = Nepomuk2::Vocabulary::NFO::RemoteDataObject();
198  }
199  if(destUrl.isLocalFile()) {
200  destType = Nepomuk2::Vocabulary::NFO::FileDataObject();
201  }
202  else {
203  destType = Nepomuk2::Vocabulary::NFO::RemoteDataObject();
204  }
205 
206  // source and dest resources
207  Nepomuk2::Resource srcFileRes(srcUrl, srcType);
208  Nepomuk2::Resource destFileRes(destUrl, destType);
209  srcFileRes.setProperty(Nepomuk2::Vocabulary::NIE::url(), srcUrl);
210  destFileRes.setProperty(Nepomuk2::Vocabulary::NIE::url(), destUrl);
211 
212  // relate src and dest
213  destFileRes.setProperty(Nepomuk2::Vocabulary::NDO::copiedFrom(), srcFileRes);
214 
215  // details in the download event
216  Nepomuk2::Resource downloadEventRes(QUrl(), Nepomuk2::Vocabulary::NDO::DownloadEvent());
217  downloadEventRes.addProperty(Nepomuk2::Vocabulary::NUAO::involves(), destFileRes);
218  downloadEventRes.addProperty(Nepomuk2::Vocabulary::NUAO::start(), startTime);
219 
220  // set the referrer
221  if(referrer.isValid()) {
222  // TODO: we could at this point index the referrer site via strigi
223  Nepomuk2::Resource referrerRes(referrer, Nepomuk2::Vocabulary::NFO::Website());
224  downloadEventRes.addProperty(Nepomuk2::Vocabulary::NDO::referrer(), referrerRes);
225  }
226 
227  return downloadEventRes;
228 }
229 
230 
231 void Nepomuk2::Utils::finishCopyEvent( Resource& /*eventResource*/, const QDateTime& /*endTime*/ )
232 {
233  // FIXME: NUAO doesn't have end() yet.
234  // eventResource.setProperty(Nepomuk2::Vocabulary::NUAO::end(), endTime);
235 }
andterm.h
Nepomuk2::Query::Query::toSparqlQuery
QString toSparqlQuery(SparqlFlags flags=NoFlags) const
Convert the query into a SPARQL query which can be used with the Nepomuk query service or directly in...
Definition: query.cpp:409
Nepomuk2::Types::Literal::isValid
bool isValid() const
Is this a valid Literal, i.e.
Definition: literal.cpp:124
Nepomuk2::Query::Query::toSearchUrl
KUrl toSearchUrl(SparqlFlags flags=NoFlags) const
Convert the query into a URL which can be listed using KIO::DirLister.
Definition: query.cpp:544
Nepomuk2::Resource::setProperty
void setProperty(const QUrl &uri, const Variant &value)
Set a property of the resource.
Definition: resource.cpp:285
Nepomuk2::Query::AndTerm
Match resource that match all sub terms.
Definition: andterm.h:43
Nepomuk2::Variant::toUrl
QUrl toUrl() const
Convert into a QUrl value.
Definition: variant.cpp:893
resourceterm.h
Nepomuk2::Resource::exists
bool exists() const
Definition: resource.cpp:322
Nepomuk2::Query::ResourceTypeTerm
Matching resources by type.
Definition: resourcetypeterm.h:48
Nepomuk2::Query::Query::setLimit
void setLimit(int)
Set the maximum number of results this query should yield.
Definition: query.cpp:317
Nepomuk2::Utils::formatPropertyValue
QString formatPropertyValue(const Nepomuk2::Types::Property &property, const Nepomuk2::Variant &value, const QList< Nepomuk2::Resource > &resources=QList< Nepomuk2::Resource >(), PropertyFormatFlags flags=NoPropertyFormatFlags)
Format a property to be displayed to the user.
Definition: utils.cpp:50
Nepomuk2::Variant::toDate
QDate toDate() const
Convert into a QDate value.
Definition: variant.cpp:860
utils.h
Nepomuk2::Variant::variant
QVariant variant() const
Definition: variant.cpp:1454
Nepomuk2::Variant::isList
bool isList() const
This methods does not handle all list types.
Definition: variant.cpp:1291
Nepomuk2::Utils::createCopyEvent
Nepomuk2::Resource createCopyEvent(const KUrl &src, const KUrl &dest, const QDateTime &startTime=QDateTime(), const KUrl &referrer=KUrl())
Save a copy event in Nepomuk stating that src has been copied to dest with an optional download start...
Definition: utils.cpp:180
Nepomuk2::Variant::toDouble
double toDouble() const
Convert into a double value.
Definition: variant.cpp:815
Nepomuk2::Utils::WithKioLinks
Include html links to Nepomuk resources, files, and Nepomuk queries.
Definition: utils.h:57
Nepomuk2::Variant::toVariantList
QList< Variant > toVariantList() const
Convert a Variant to a list of Variants.
Definition: variant.cpp:1173
Nepomuk2::Variant
The Nepomuk Variant extends over QVariant by introducing direct support for Resource embedding...
Definition: variant.h:65
variant.h
Nepomuk2::Types::Literal::dataType
QVariant::Type dataType() const
The type converted to a QVariant::Type.
Definition: literal.cpp:118
Nepomuk2::Variant::toResource
Resource toResource() const
Convert into a Resource value.
Definition: variant.cpp:908
Nepomuk2::Resource::property
Variant property(const QUrl &uri) const
Retrieve the value of property uri.
Definition: resource.cpp:264
Nepomuk2::Query::FileQuery
A Nepomuk desktop query specialized for file searches.
Definition: filequery.h:44
Nepomuk2::Query::ComparisonTerm
A term matching the value of a property.
Definition: comparisonterm.h:70
Nepomuk2::Variant::toInt
int toInt() const
Convert into an int value.
Definition: variant.cpp:755
Nepomuk2::Query::Query
A Nepomuk desktop query.
Definition: query.h:76
resource.h
Nepomuk2::Types::Property
A property is a resource of type rdf:Property which relates a domain with a range.
Definition: libnepomukcore/types/property.h:52
Nepomuk2::Types::Literal
Defines a literal type based on XML Schema.
Definition: literal.h:41
Nepomuk2::ResourceManager::instance
static ResourceManager * instance()
Definition: resourcemanager.cpp:270
resourcemanager.h
Nepomuk2::Resource::genericLabel
QString genericLabel() const
Tries very hard to find a suitable human-readable label for this resource.
Definition: resource.cpp:341
resourcetypeterm.h
Nepomuk2::Variant::toDateTime
QDateTime toDateTime() const
Convert into a QDateTime value.
Definition: variant.cpp:882
comparisonterm.h
Nepomuk2::Types::Entity::label
QString label(const QString &language=KGlobal::locale() ->language())
Retrieve the label of the entity (rdfs:label)
Definition: entity.cpp:187
Nepomuk2::Variant::toString
QString toString() const
The toString() method is a little more powerful than other toXXX methods since it actually converts a...
Definition: variant.cpp:827
Nepomuk2::Utils::finishCopyEvent
void finishCopyEvent(Nepomuk2::Resource &eventResource, const QDateTime &endTime)
Save the end time of a copy event created via createCopyEvent().
Definition: utils.cpp:231
Nepomuk2::Resource
Resource is the central object type in Nepomuk.
Definition: resource.h:93
Nepomuk2::Variant::isResource
bool isResource() const
Definition: variant.cpp:674
start
void start()
Definition: ontologydownloadjob.cpp:13
optionalterm.h
Nepomuk2::Resource::uri
QUrl uri() const
The URI of the resource, uniquely identifying it.
Definition: resource.cpp:166
literal.h
Nepomuk2::ResourceManager::mainModel
Soprano::Model * mainModel()
Retrieve the main data storage model.
Definition: resourcemanager.cpp:363
Nepomuk2::Resource::addProperty
void addProperty(const QUrl &uri, const Variant &value)
Add a property value to the existing values.
Definition: resource.cpp:276
filequery.h
Nepomuk2::Query::Term::fromProperty
static Term fromProperty(const Types::Property &property, const Variant &variant)
Create a term using a Types::Property and a Variant.
Definition: term.cpp:369
Nepomuk2::Query::ResourceTerm
Matches exactly one resource.
Definition: resourceterm.h:52
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:09 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