• 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
  • tools
  • nepomukshow
resourceprinter.cpp
Go to the documentation of this file.
1 /*
2  * <one line to give the library's name and an idea of what it does.>
3  * Copyright (C) 2012 Vishesh Handa <me@vhanda.in>
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) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  */
20 
21 #include "resourceprinter.h"
22 #include "uri.h"
23 
24 #include <QtCore/QSet>
25 #include <QDateTime>
26 
27 #include <Soprano/Model>
28 #include <Soprano/QueryResultIterator>
29 #include <Soprano/Vocabulary/NAO>
30 #include <Soprano/Vocabulary/RDF>
31 #include <Soprano/Vocabulary/NRL>
32 
33 #include "resourcemanager.h"
34 #include "nie.h"
35 #include "nmo.h"
36 
37 using namespace Soprano::Vocabulary;
38 using namespace Nepomuk2::Vocabulary;
39 
40 namespace {
41 
42  struct SyncResource {
43  QUrl uri;
44  QMultiHash<QUrl, Soprano::Node> prop;
45  };
46 
47  SyncResource createSyncResource(const QUrl& uri, bool inference = false) {
48  SyncResource syncRes;
49  syncRes.uri = uri;
50 
51  QString query = QString::fromLatin1("select distinct ?p ?o where { %1 ?p ?o. }")
52  .arg( Soprano::Node::resourceToN3( uri ) );
53 
54  Soprano::Query::QueryLanguage lang;
55  if( inference )
56  lang = Soprano::Query::QueryLanguageSparql;
57  else
58  lang = Soprano::Query::QueryLanguageSparqlNoInference;
59 
60  Soprano::Model* model = Nepomuk2::ResourceManager::instance()->mainModel();
61  Soprano::QueryResultIterator it = model->executeQuery( query, lang );
62  while( it.next() ) {
63  Soprano::Node object = it[1];
64  syncRes.prop.insertMulti( it[0].uri(), object );
65  }
66 
67  return syncRes;
68  }
69 
70  class TablePrinter {
71  public:
72  TablePrinter( int columns ) {
73  m_columns = columns;
74 
75  // Isn't there a better way?
76  for( int i = 0; i<columns; i++ ) {
77  m_data.append( QStringList() );
78  m_dataMaxLength.append( 0 );
79  }
80  }
81 
82  void insert( int column, const QString& data ) {
83  m_data[ column ] << data;
84  if( data.length() > m_dataMaxLength[column] )
85  m_dataMaxLength[column] = data.length();
86  }
87 
88  void print( QTextStream& stream ) {
89  int rows = m_data[0].size();
90 
91  for( int i=0; i<rows; i++ ) {
92  // For the initial indentation - Make it configurable
93  stream << QString( 8, QChar::fromAscii(' ') );
94  for( int c=0; c<m_columns; c++ ) {
95  stream << m_data[c][i];
96 
97  int numSpaces = m_dataMaxLength[c] - m_data[c][i].length();
98  numSpaces += 2;
99 
100  // Do not print spaces for the last column
101  if( c != m_columns-1 )
102  stream << QString( numSpaces, QChar::fromAscii(' ') );
103  }
104  stream << "\n";
105  }
106  }
107 
108  private:
109  int m_columns;
110 
111  QList<QStringList> m_data;
112  QList<int> m_dataMaxLength;
113  };
114 
115  QList<QUrl> sortProperties(const QList<QUrl>& properties) {
116  // Group the keys based on prefix
117  QMultiHash<QString, QUrl> groupedProperties;
118  foreach( const QUrl& propUri, properties ) {
119  groupedProperties.insert( Nepomuk2::Uri(propUri).prefix(), propUri );
120  }
121 
122  // Print on the basis of a custom sort
123  QList<QUrl> sortedKeys;
124 
125  // Custom Sort
126  sortedKeys += groupedProperties.values("rdf");
127  sortedKeys += groupedProperties.values("rdfs");
128  sortedKeys += groupedProperties.values("nrl");
129  sortedKeys += groupedProperties.values("nao");
130  sortedKeys += groupedProperties.values("nie");
131  sortedKeys += groupedProperties.values("nfo");
132  sortedKeys += groupedProperties.values("nmm");
133  sortedKeys += groupedProperties.values("kext");
134  sortedKeys += properties.toSet().subtract( sortedKeys.toSet() ).toList(); // Remaining
135 
136  return sortedKeys;
137  }
138 
139 
140  QString nodeToN3(const Soprano::Node& node) {
141  QString str;
142  if( node.isResource() ) {
143  if( node.uri().scheme() == QLatin1String("nepomuk") || node.uri().scheme() == QLatin1String("file") )
144  str = node.uri().toString();
145  else
146  str = Nepomuk2::Uri( node.uri() ).toN3();
147  }
148  else {
149  str = node.literal().toString();
150  }
151  return str;
152  }
153 
154  QString printGraphData(const QUrl& graphUri) {
155  SyncResource res = createSyncResource(graphUri);
156 
157  QString app;
158  QDateTime created;
159  bool discardable = false;
160 
161  if( res.prop.contains(NAO::maintainedBy()) ) {
162  QString query = QString::fromLatin1("select ?i where { %1 nao:identifier ?i. }")
163  .arg( res.prop.value(NAO::maintainedBy()).toN3() );
164  Soprano::Model* model = Nepomuk2::ResourceManager::instance()->mainModel();
165  Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
166  if( it.next() ) {
167  app = it[0].literal().toString();
168  }
169  }
170 
171  if( res.prop.contains(NAO::created()) ) {
172  created = res.prop.value(NAO::created()).literal().toDateTime();
173  }
174 
175  if( res.prop.contains(RDF::type(), Soprano::Node(NRL::DiscardableInstanceBase())) ) {
176  discardable = true;
177  }
178 
179  QString str = "| " + app + " | " + created.toLocalTime().toString( Qt::ISODate );
180  if( discardable )
181  str += " | D";
182 
183  return str;
184  }
185 
186  void print(QTextStream& stream, const SyncResource& syncRes,
187  bool printGraphInfo, bool printPlainText) {
188  // Print the uri
189  stream << "\n" << Soprano::Node::resourceToN3( syncRes.uri ) << "\n";
190 
191  TablePrinter tablePrinter( printGraphInfo ? 3: 2 );
192 
193  // Print on the basis of a custom sort
194  QList<QUrl> sortedKeys = sortProperties( syncRes.prop.uniqueKeys() );
195  foreach( const QUrl& propUri, sortedKeys ) {
196  QList<Soprano::Node> values = syncRes.prop.values( propUri );
197  foreach(const Soprano::Node& node, values) {
198  if( !printPlainText ) {
199  if( propUri == NIE::plainTextContent() || propUri == NMO::plainTextMessageContent() )
200  continue;
201  }
202 
203  QString propery = Nepomuk2::Uri( propUri ).toN3();
204  QString object = nodeToN3( node );
205 
206  tablePrinter.insert( 0, propery );
207  tablePrinter.insert( 1, object );
208 
209  if( printGraphInfo ) {
210  Soprano::Model* model = Nepomuk2::ResourceManager::instance()->mainModel();
211  QString query = QString::fromLatin1("select ?g where { graph ?g { %1 %2 %3. } }")
212  .arg( Soprano::Node::resourceToN3(syncRes.uri),
213  Soprano::Node::resourceToN3(propUri),
214  node.toN3() );
215  Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
216  if( it.next() )
217  tablePrinter.insert( 2, printGraphData( it[0].uri() ) );
218  else
219  tablePrinter.insert( 2, "---");
220  }
221  }
222  }
223 
224  tablePrinter.print( stream );
225  }
226 
227 }
228 
229 Nepomuk2::ResourcePrinter::ResourcePrinter()
230 {
231  m_printGraphs = false;
232  m_inference = false;
233  m_printPlainText = false;
234 }
235 
236 
237 void Nepomuk2::ResourcePrinter::print(QTextStream& stream, const QUrl& uri)
238 {
239  ::print( stream, createSyncResource(uri, m_inference), m_printGraphs, m_printPlainText );
240 }
241 
242 void Nepomuk2::ResourcePrinter::setGraphs(bool status)
243 {
244  m_printGraphs = status;
245 }
246 
247 void Nepomuk2::ResourcePrinter::setInference(bool status)
248 {
249  m_inference = status;
250 }
251 
252 void Nepomuk2::ResourcePrinter::setPlainText(bool status)
253 {
254  m_printPlainText = status;
255 }
QMultiHash
Nepomuk2::Uri::toN3
QString toN3()
Definition: uri.cpp:106
uri.h
Nepomuk2::ResourcePrinter::ResourcePrinter
ResourcePrinter()
Definition: resourceprinter.cpp:229
Nepomuk2::ResourcePrinter::setGraphs
void setGraphs(bool status)
Definition: resourceprinter.cpp:242
Nepomuk2::Uri
Definition: uri.h:28
Nepomuk2::ResourceManager::instance
static ResourceManager * instance()
Definition: resourcemanager.cpp:270
resourcemanager.h
Nepomuk2::ResourcePrinter::print
void print(QTextStream &stream, const QUrl &uri)
Definition: resourceprinter.cpp:237
resourceprinter.h
Nepomuk2::ResourcePrinter::setInference
void setInference(bool status)
Definition: resourceprinter.cpp:247
Nepomuk2::ResourcePrinter::setPlainText
void setPlainText(bool status)
Definition: resourceprinter.cpp:252
Nepomuk2::ResourceManager::mainModel
Soprano::Model * mainModel()
Retrieve the main data storage model.
Definition: resourcemanager.cpp:363
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:08 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