• 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
fileindexingjob.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Nepomuk KDE project.
3  Copyright (C) 2010-2011 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 "fileindexingjob.h"
23 #include "util.h"
24 #include "fileindexerconfig.h"
25 #include "resourcemanager.h"
26 #include "kext.h"
27 
28 #include <KUrl>
29 #include <KDebug>
30 #include <KProcess>
31 #include <KStandardDirs>
32 
33 #include <Soprano/Node>
34 #include <Soprano/Model>
35 #include <Soprano/QueryResultIterator>
36 
37 #include <QtCore/QFileInfo>
38 #include <QtCore/QTimer>
39 
40 using namespace Nepomuk2::Vocabulary;
41 
42 Nepomuk2::FileIndexingJob::FileIndexingJob(const QUrl& fileUrl, QObject* parent)
43  : KJob(parent),
44  m_url( fileUrl )
45 {
46  // setup the timer used to kill the indexer process if it seems to get stuck
47  m_processTimer = new QTimer(this);
48  m_processTimer->setSingleShot(true);
49  connect(m_processTimer, SIGNAL(timeout()),
50  this, SLOT(slotProcessTimerTimeout()));
51 }
52 
53 void Nepomuk2::FileIndexingJob::start()
54 {
55  if( !QFile::exists(m_url.toLocalFile()) ) {
56  QTimer::singleShot( 0, this, SLOT(slotProcessNonExistingFile()) );
57  return;
58  }
59 
60  // setup the external process which does the actual indexing
61  const QString exe = KStandardDirs::findExe(QLatin1String("nepomukindexer"));
62 
63  kDebug() << "Running" << exe << m_url.toLocalFile();
64 
65  m_process = new KProcess( this );
66 
67  QStringList args;
68  args << m_url.toLocalFile();
69 
70  m_process->setProgram( exe, args );
71  m_process->setOutputChannelMode(KProcess::OnlyStdoutChannel);
72  connect( m_process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotIndexedFile(int, QProcess::ExitStatus)) );
73  m_process->start();
74 
75  // start the timer which will kill the process if it does not terminate after 5 minutes
76  m_processTimer->start(5*60*1000);
77 }
78 
79 void Nepomuk2::FileIndexingJob::slotProcessNonExistingFile()
80 {
81  QString query = QString::fromLatin1("select ?r where { ?r nie:url %1. }")
82  .arg( Soprano::Node::resourceToN3(m_url) );
83  Soprano::Model* model = ResourceManager::instance()->mainModel();
84  Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
85  while( it.next() ) {
86  QUrl uri = it[0].uri();
87 
88  // We do not just delete the resource cause it could be part of some removeable media
89  // which is not mounted. When the device is mounted, then the file will get reindexed
90  model->removeAllStatements( uri, KExt::indexingLevel(), QUrl() );
91  }
92 
93  emitResult();
94 }
95 
96 
97 void Nepomuk2::FileIndexingJob::slotIndexedFile(int exitCode, QProcess::ExitStatus exitStatus)
98 {
99  // stop the timer since there is no need to kill the process anymore
100  m_processTimer->stop();
101 
102  //kDebug() << "Indexing of " << m_url.toLocalFile() << "finished with exit code" << exitCode;
103  if(exitStatus != QProcess::NormalExit) {
104  setError( IndexerCrashed );
105  setErrorText( QLatin1String( "Indexer process crashed on " ) + m_url.toLocalFile() );
106  }
107  if(exitCode == 1) {
108  setError( IndexerFailed );
109  setErrorText( QLatin1String( "Indexer process returned with an error for " ) + m_url.toLocalFile() );
110  if(FileIndexerConfig::self()->isDebugModeEnabled()) {
111  QFile errorLogFile(KStandardDirs::locateLocal("data", QLatin1String("nepomuk/file-indexer-error-log"), true));
112  if(errorLogFile.open(QIODevice::Append)) {
113  QTextStream s(&errorLogFile);
114  s << m_url.toLocalFile() << ": " << QString::fromLocal8Bit(m_process->readAllStandardOutput()) << endl;
115  }
116  }
117  }
118  emitResult();
119 }
120 
121 void Nepomuk2::FileIndexingJob::slotProcessTimerTimeout()
122 {
123  m_process->disconnect(this);
124  m_process->kill();
125  m_process->waitForFinished();
126  setError( KJob::KilledJobError );
127  setErrorText( QLatin1String("Indexer process got stuck for") + m_url.toLocalFile() );
128  emitResult();
129 }
130 
131 #include "fileindexingjob.moc"
fileindexerconfig.h
util.h
Nepomuk2::FileIndexerConfig::isDebugModeEnabled
bool isDebugModeEnabled() const
Check if the debug mode is enabled.
Definition: fileindexerconfig.cpp:481
QObject
Nepomuk2::FileIndexingJob::FileIndexingJob
FileIndexingJob(const QUrl &fileUrl, QObject *parent=0)
Definition: fileindexingjob.cpp:42
Nepomuk2::ResourceManager::instance
static ResourceManager * instance()
Definition: resourcemanager.cpp:270
resourcemanager.h
fileindexingjob.h
Nepomuk2::FileIndexingJob::start
virtual void start()
Definition: fileindexingjob.cpp:53
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