• 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
  • services
  • storage
  • backup
resourcelistgenerator.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 2013 Vishesh Handa <me@vhanda.in>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License or (at your option) version 3 or any later version
9  * accepted by the membership of KDE e.V. (or its successor approved
10  * by the membership of KDE e.V.), which shall act as a proxy
11  * defined in Section 14 of version 3 of the license.
12  *
13  * This program 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
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #include "resourcelistgenerator.h"
24 
25 #include <QtCore/QTimer>
26 #include <QtCore/QFile>
27 
28 #include <KDebug>
29 
30 #include <Soprano/QueryResultIterator>
31 
32 using namespace Nepomuk2;
33 using namespace Nepomuk2::Backup;
34 
35 ResourceListGenerator::ResourceListGenerator(Soprano::Model* model, const QString& outputFile, QObject* parent)
36  : KJob(parent)
37  , m_model(model)
38  , m_outputFile(outputFile)
39  , m_filter(Filter_None)
40  , m_resourceCount(0)
41 {
42 }
43 
44 void ResourceListGenerator::start()
45 {
46  QTimer::singleShot( 0, this, SLOT(doJob()) );
47 }
48 
49 void ResourceListGenerator::setFilter(ResourceListGenerator::Filter filter)
50 {
51  m_filter = filter;
52 }
53 
54 namespace {
55  QUrl nepomukGraph(Soprano::Model* model) {
56  QString query = QString::fromLatin1("select ?r where { ?r a nrl:Graph ; nao:maintainedBy ?app ."
57  " ?app nao:identifier %1 . } LIMIT 1")
58  .arg( Soprano::Node::literalToN3(QLatin1String("nepomuk")) );
59 
60  Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
61  if( it.next() )
62  return it[0].uri();
63 
64  return QUrl();
65  }
66 }
67 
68 void ResourceListGenerator::doJob()
69 {
70  // FIXME: Emit some kind of %?
71 
72  QFile file( m_outputFile );
73  if( !file.open( QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text ) ) {
74  setError(1);
75  setErrorText( QString::fromLatin1("Could not open file %1").arg( m_outputFile ) );
76  emitResult();
77  return;
78  }
79 
80  QTextStream out( &file );
81 
82  if( m_filter == Filter_None ) {
83  QUrl ng = nepomukGraph(m_model);
84  QString query = QString::fromLatin1("select distinct ?r where { graph ?g { ?r ?p ?o }"
85  " ?g a nrl:InstanceBase ."
86  " FILTER( ?g!=%1 ) ."
87  " FILTER NOT EXISTS { ?g a nrl:DiscardableInstanceBase . } }")
88  .arg( Soprano::Node::resourceToN3(ng) );
89 
90  kDebug() << "Fetching URI list";
91  Soprano::QueryResultIterator rit = m_model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
92 
93  while( rit.next() ) {
94  out << rit[0].uri().toString() << "\n";
95  m_resourceCount++;
96  }
97  }
98  else if( m_filter == Filter_FilesAndTags ) {
99  QString query = QString::fromLatin1("select distinct ?r where { ?r a nao:Tag . ?f nao:hasTag ?r ."
100  "?f a nfo:FileDataObject . }");
101  Soprano::QueryResultIterator it = m_model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
102 
103  while( it.next() ) {
104  out << it[0].uri().toString() << "\n";
105  m_resourceCount++;
106  }
107 
108  // file count
109  QString countQuery = QString::fromLatin1("select count(distinct ?r) where { ?r a nfo:FileDataObject ;"
110  " nie:url ?url ; ?p ?t. "
111  "FILTER(?p in (nao:hasTag, nao:numericRating, nao:description)). }");
112  Soprano::QueryResultIterator iter = m_model->executeQuery( countQuery, Soprano::Query::QueryLanguageSparqlNoInference );
113  int approxCount = 0;
114  if( iter.next() )
115  approxCount = iter[0].literal().toInt() + m_resourceCount;
116 
117  query = QString::fromLatin1("select distinct ?r where { ?r a nfo:FileDataObject ;"
118  " nie:url ?url ; ?p ?t ."
119  " FILTER(?p in (nao:hasTag, nao:numericRating, nao:description)) ."
120  " FILTER(REGEX(STR(?url), '^file:')) . }");
121  it = m_model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
122 
123  while( it.next() ) {
124  out << it[0].uri().toString() << "\n";
125  m_resourceCount++;
126 
127  if( m_resourceCount < approxCount )
128  emitPercent( m_resourceCount, approxCount );
129  }
130  }
131 
132  emitPercent( m_resourceCount, m_resourceCount );
133  emitResult();
134 }
Nepomuk2::Backup::ResourceListGenerator::ResourceListGenerator
ResourceListGenerator(Soprano::Model *model, const QString &outputFile, QObject *parent=0)
Definition: resourcelistgenerator.cpp:35
resourcelistgenerator.h
QObject
Nepomuk2::Backup::ResourceListGenerator::Filter_FilesAndTags
Definition: resourcelistgenerator.h:49
Nepomuk2::Backup::ResourceListGenerator::setFilter
void setFilter(Filter filter)
Definition: resourcelistgenerator.cpp:49
Nepomuk2::Backup::ResourceListGenerator::Filter_None
Definition: resourcelistgenerator.h:48
Nepomuk2::Backup::ResourceListGenerator::Filter
Filter
Definition: resourcelistgenerator.h:47
Nepomuk2::Backup::ResourceListGenerator::start
virtual void start()
Definition: resourcelistgenerator.cpp:44
KJob
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