24 #include <QtCore/QSet>
27 #include <Soprano/Model>
28 #include <Soprano/QueryResultIterator>
29 #include <Soprano/Vocabulary/NAO>
30 #include <Soprano/Vocabulary/RDF>
31 #include <Soprano/Vocabulary/NRL>
37 using namespace Soprano::Vocabulary;
38 using namespace Nepomuk2::Vocabulary;
47 SyncResource createSyncResource(
const QUrl& uri,
bool inference =
false) {
51 QString query = QString::fromLatin1(
"select distinct ?p ?o where { %1 ?p ?o. }")
52 .arg( Soprano::Node::resourceToN3( uri ) );
54 Soprano::Query::QueryLanguage lang;
56 lang = Soprano::Query::QueryLanguageSparql;
58 lang = Soprano::Query::QueryLanguageSparqlNoInference;
61 Soprano::QueryResultIterator it = model->executeQuery( query, lang );
63 Soprano::Node
object = it[1];
64 syncRes.prop.insertMulti( it[0].uri(),
object );
72 TablePrinter(
int columns ) {
76 for(
int i = 0; i<columns; i++ ) {
77 m_data.append( QStringList() );
78 m_dataMaxLength.append( 0 );
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();
88 void print( QTextStream& stream ) {
89 int rows = m_data[0].size();
91 for(
int i=0; i<rows; i++ ) {
93 stream << QString( 8, QChar::fromAscii(
' ') );
94 for(
int c=0; c<m_columns; c++ ) {
95 stream << m_data[c][i];
97 int numSpaces = m_dataMaxLength[c] - m_data[c][i].length();
101 if( c != m_columns-1 )
102 stream << QString( numSpaces, QChar::fromAscii(
' ') );
111 QList<QStringList> m_data;
112 QList<int> m_dataMaxLength;
115 QList<QUrl> sortProperties(
const QList<QUrl>& properties) {
118 foreach(
const QUrl& propUri, properties ) {
119 groupedProperties.insert(
Nepomuk2::Uri(propUri).prefix(), propUri );
123 QList<QUrl> sortedKeys;
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();
140 QString nodeToN3(
const Soprano::Node& node) {
142 if( node.isResource() ) {
143 if( node.uri().scheme() == QLatin1String(
"nepomuk") || node.uri().scheme() == QLatin1String(
"file") )
144 str = node.uri().toString();
149 str = node.literal().toString();
154 QString printGraphData(
const QUrl& graphUri) {
155 SyncResource res = createSyncResource(graphUri);
159 bool discardable =
false;
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() );
165 Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
167 app = it[0].literal().toString();
171 if( res.prop.contains(NAO::created()) ) {
172 created = res.prop.value(NAO::created()).literal().toDateTime();
175 if( res.prop.contains(RDF::type(), Soprano::Node(NRL::DiscardableInstanceBase())) ) {
179 QString str =
"| " + app +
" | " + created.toLocalTime().toString( Qt::ISODate );
186 void print(QTextStream& stream,
const SyncResource& syncRes,
187 bool printGraphInfo,
bool printPlainText) {
189 stream <<
"\n" << Soprano::Node::resourceToN3( syncRes.uri ) <<
"\n";
191 TablePrinter tablePrinter( printGraphInfo ? 3: 2 );
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() )
204 QString
object = nodeToN3( node );
206 tablePrinter.insert( 0, propery );
207 tablePrinter.insert( 1,
object );
209 if( printGraphInfo ) {
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),
215 Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
217 tablePrinter.insert( 2, printGraphData( it[0].uri() ) );
219 tablePrinter.insert( 2,
"---");
224 tablePrinter.print( stream );
231 m_printGraphs =
false;
233 m_printPlainText =
false;
239 ::print( stream, createSyncResource(uri, m_inference), m_printGraphs, m_printPlainText );
244 m_printGraphs = status;
249 m_inference = status;
254 m_printPlainText = status;
void setGraphs(bool status)
static ResourceManager * instance()
void print(QTextStream &stream, const QUrl &uri)
void setInference(bool status)
void setPlainText(bool status)
Soprano::Model * mainModel()
Retrieve the main data storage model.