• 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
  • fileindexer
basicindexingqueue.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 "basicindexingqueue.h"
22 #include "fileindexerconfig.h"
23 #include "util.h"
24 #include "indexer/simpleindexer.h"
25 
26 #include "resourcemanager.h"
27 
28 #include <Soprano/Model>
29 #include <Soprano/QueryResultIterator>
30 
31 #include <KDebug>
32 #include <KMimeType>
33 #include <QtCore/QDateTime>
34 
35 namespace Nepomuk2 {
36 
37 BasicIndexingQueue::BasicIndexingQueue(QObject* parent): IndexingQueue(parent)
38 {
39 
40 }
41 
42 void BasicIndexingQueue::clear()
43 {
44  m_currentUrl.clear();
45  m_currentFlags = NoUpdateFlags;
46  m_paths.clear();
47 }
48 
49 void BasicIndexingQueue::clear(const QString& path)
50 {
51  QMutableVectorIterator< QPair<QString, UpdateDirFlags> > it( m_paths );
52  while( it.hasNext() ) {
53  it.next();
54  if( it.value().first.startsWith( path ) )
55  it.remove();
56  }
57 }
58 
59 QUrl BasicIndexingQueue::currentUrl() const
60 {
61  return m_currentUrl;
62 }
63 
64 UpdateDirFlags BasicIndexingQueue::currentFlags() const
65 {
66  return m_currentFlags;
67 }
68 
69 
70 bool BasicIndexingQueue::isEmpty()
71 {
72  return m_paths.isEmpty();
73 }
74 
75 void BasicIndexingQueue::enqueue(const QString& path)
76 {
77  UpdateDirFlags flags;
78  flags |= UpdateRecursive;
79 
80  enqueue( path, flags );
81 }
82 
83 void BasicIndexingQueue::enqueue(const QString& path, UpdateDirFlags flags)
84 {
85  kDebug() << path;
86  bool wasEmpty = m_paths.empty();
87  m_paths.push( qMakePair( path, flags ) );
88  callForNextIteration();
89 
90  if( wasEmpty )
91  emit startedIndexing();
92 }
93 
94 void BasicIndexingQueue::processNextIteration()
95 {
96  bool processingFile = false;
97 
98  if( !m_paths.isEmpty() ) {
99  QPair< QString, UpdateDirFlags > pair = m_paths.pop();
100  processingFile = process( pair.first, pair.second );
101  }
102 
103  if( !processingFile )
104  finishIteration();
105 }
106 
107 
108 bool BasicIndexingQueue::process(const QString& path, UpdateDirFlags flags)
109 {
110  bool startedIndexing = false;
111 
112  QUrl url = QUrl::fromLocalFile( path );
113  QString mimetype = KMimeType::findByUrl( url )->name();
114 
115  bool forced = flags & ForceUpdate;
116  bool recursive = flags & UpdateRecursive;
117  bool indexingRequired = shouldIndex( path, mimetype );
118 
119  QFileInfo info( path );
120  if( info.isDir() ) {
121  if( forced || indexingRequired ) {
122  m_currentUrl = url;
123  m_currentFlags = flags;
124  m_currentMimeType = mimetype;
125 
126  startedIndexing = true;
127  index( path );
128  }
129 
130  // We don't want to follow system links
131  if( recursive && !info.isSymLink() && shouldIndexContents(path) ) {
132  QDir::Filters dirFilter = QDir::NoDotAndDotDot|QDir::Readable|QDir::Files|QDir::Dirs;
133 
134  QDirIterator it( path, dirFilter );
135  while( it.hasNext() ) {
136  m_paths.push( qMakePair(it.next(), flags) );
137  }
138  }
139  }
140  else if( info.isFile() && (forced || indexingRequired) ) {
141  m_currentUrl = url;
142  m_currentFlags = flags;
143  m_currentMimeType = mimetype;
144 
145  startedIndexing = true;
146  index( path );
147  }
148 
149  return startedIndexing;
150 }
151 
152 bool BasicIndexingQueue::shouldIndex(const QString& path, const QString& mimetype)
153 {
154  bool shouldIndexFile = FileIndexerConfig::self()->shouldFileBeIndexed( path );
155  if( !shouldIndexFile )
156  return false;
157 
158  bool shouldIndexType = FileIndexerConfig::self()->shouldMimeTypeBeIndexed( mimetype );
159  if( !shouldIndexType )
160  return false;
161 
162  QFileInfo fileInfo( path );
163  if( !fileInfo.exists() )
164  return false;
165 
166  bool needToIndex = false;
167 
168  Soprano::Model* model = ResourceManager::instance()->mainModel();
169 
170  // Optimization: We don't care about the mtime of directories. If it has been indexed once
171  // then it doesn't need to indexed again - ever
172  if( fileInfo.isDir() ) {
173  QString query = QString::fromLatin1("ask where { ?r nie:url %1 . }")
174  .arg( Soprano::Node::resourceToN3( QUrl::fromLocalFile(path) ) );
175 
176  needToIndex = !model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference ).boolValue();
177  }
178  else {
179  // check if this file is new by checking its mtime
180  QString query = QString::fromLatin1("ask where { ?r nie:url %1 ; nie:lastModified ?dt . FILTER(?dt=%2) .}")
181  .arg( Soprano::Node::resourceToN3( QUrl::fromLocalFile(path) ),
182  Soprano::Node::literalToN3( Soprano::LiteralValue(fileInfo.lastModified()) ) );
183 
184  needToIndex = !model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference ).boolValue();
185  }
186 
187  if( needToIndex ) {
188  kDebug() << path;
189  return true;
190  }
191 
192  return false;
193 }
194 
195 bool BasicIndexingQueue::shouldIndexContents(const QString& dir)
196 {
197  return FileIndexerConfig::self()->shouldFolderBeIndexed( dir );
198 }
199 
200 void BasicIndexingQueue::index(const QString& path)
201 {
202  kDebug() << path;
203  const QUrl fileUrl = QUrl::fromLocalFile( path );
204  emit beginIndexingFile( fileUrl );
205 
206  KJob* job = clearIndexedData( fileUrl );
207  connect( job, SIGNAL(finished(KJob*)), this, SLOT(slotClearIndexedDataFinished(KJob*)) );
208 }
209 
210 void BasicIndexingQueue::slotClearIndexedDataFinished(KJob* job)
211 {
212  if( job->error() ) {
213  kDebug() << job->errorString();
214  }
215 
216  SimpleIndexingJob* indexingJob = new SimpleIndexingJob( m_currentUrl, m_currentMimeType );
217  indexingJob->start();
218 
219  connect( indexingJob, SIGNAL(finished(KJob*)), this, SLOT(slotIndexingFinished(KJob*)) );
220 }
221 
222 void BasicIndexingQueue::slotIndexingFinished(KJob* job)
223 {
224  if( job->error() ) {
225  kDebug() << job->errorString();
226  }
227 
228  QUrl url = m_currentUrl;
229  m_currentUrl.clear();
230  m_currentMimeType.clear();
231  m_currentFlags = NoUpdateFlags;
232 
233  emit endIndexingFile( url );
234 
235  // Continue the queue
236  finishIteration();
237 }
238 
239 
240 }
fileindexerconfig.h
Nepomuk2::FileIndexerConfig::shouldFolderBeIndexed
bool shouldFolderBeIndexed(const QString &path) const
Check if the folder at path should be indexed.
Definition: fileindexerconfig.cpp:179
Nepomuk2::ForceUpdate
The files in the folder should be updated regardless of their state.
Definition: basicindexingqueue.h:51
Nepomuk2::FileIndexerConfig::shouldMimeTypeBeIndexed
bool shouldMimeTypeBeIndexed(const QString &mimeType) const
Checks if mimeType should be indexed.
Definition: fileindexerconfig.cpp:219
Nepomuk2::BasicIndexingQueue::processNextIteration
virtual void processNextIteration()
Process the next iteration in your queue.
Definition: basicindexingqueue.cpp:94
util.h
QObject
Nepomuk2::BasicIndexingQueue::isEmpty
virtual bool isEmpty()
Definition: basicindexingqueue.cpp:70
Nepomuk2::IndexingQueue::startedIndexing
void startedIndexing()
The derived queues must emit this signal when their queue gets filled up.
basicindexingqueue.h
Nepomuk2::clearIndexedData
KJob * clearIndexedData(const QUrl &url)
remove all indexed data for url the datamanagement way
Definition: util.cpp:42
Nepomuk2::FileIndexerConfig::shouldFileBeIndexed
bool shouldFileBeIndexed(const QString &fileName) const
Check fileName for all exclude filters.
Definition: fileindexerconfig.cpp:212
Nepomuk2::IndexingQueue::callForNextIteration
void callForNextIteration()
Definition: indexingqueue.cpp:64
Nepomuk2::BasicIndexingQueue::clear
void clear()
Definition: basicindexingqueue.cpp:42
Nepomuk2::BasicIndexingQueue::enqueue
void enqueue(const QString &path)
Definition: basicindexingqueue.cpp:75
Nepomuk2::NoUpdateFlags
No flags, only used to make code more readable.
Definition: basicindexingqueue.h:34
Nepomuk2::IndexingQueue::finishIteration
void finishIteration()
Call this function when you have finished processing the iteration from processNextIteration.
Definition: indexingqueue.cpp:81
Nepomuk2::BasicIndexingQueue::endIndexingFile
void endIndexingFile(const QUrl &url)
Nepomuk2::UpdateRecursive
The folder should be updated recursive.
Definition: basicindexingqueue.h:39
Nepomuk2::ResourceManager::instance
static ResourceManager * instance()
Definition: resourcemanager.cpp:270
resourcemanager.h
Nepomuk2::BasicIndexingQueue::currentFlags
UpdateDirFlags currentFlags() const
Definition: basicindexingqueue.cpp:64
Nepomuk2::IndexingQueue
An abstract class representing the queue.
Definition: indexingqueue.h:36
Nepomuk2::BasicIndexingQueue::currentUrl
QUrl currentUrl() const
Definition: basicindexingqueue.cpp:59
Nepomuk2::BasicIndexingQueue::BasicIndexingQueue
BasicIndexingQueue(QObject *parent=0)
Definition: basicindexingqueue.cpp:37
simpleindexer.h
Nepomuk2::BasicIndexingQueue::beginIndexingFile
void beginIndexingFile(const QUrl &url)
Nepomuk2::FileIndexerConfig::self
static FileIndexerConfig * self()
Get the first created instance of FileIndexerConfig.
Definition: fileindexerconfig.cpp:82
Nepomuk2::ResourceManager::mainModel
Soprano::Model * mainModel()
Retrieve the main data storage model.
Definition: resourcemanager.cpp:363
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