1/*
2 SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
3 SPDX-FileCopyrightText: 2015 Pinak Ahuja <pinak.ahuja@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6*/
7
8#include "baloodebug.h"
9#include "extractorprocess.h"
10
11using namespace Baloo;
12
13ExtractorProcess::ExtractorProcess(const QString& extractorPath, QObject* parent)
14 : QObject(parent)
15 , m_extractorPath(extractorPath)
16 , m_extractorProcess(this)
17 , m_controller(&m_extractorProcess, &m_extractorProcess)
18{
19 using ControllerPipe = Baloo::Private::ControllerPipe;
20
21 connect(sender: &m_extractorProcess, signal: &QProcess::readyRead, context: &m_controller, slot: &ControllerPipe::processStatusData);
22 connect(sender: &m_controller, signal: &ControllerPipe::urlStarted, context: this, slot: &ExtractorProcess::startedIndexingFile);
23 connect(sender: &m_controller, signal: &ControllerPipe::urlFinished, context: this, slot: [this](const QString& url) {
24 Q_EMIT finishedIndexingFile(filePath: url, fileUpdated: true);
25 });
26 connect(sender: &m_controller, signal: &ControllerPipe::urlFailed, context: this, slot: [this](const QString& url) {
27 Q_EMIT finishedIndexingFile(filePath: url, fileUpdated: false);
28 });
29 connect(sender: &m_controller, signal: &ControllerPipe::batchFinished, context: this, slot: [this]() {
30 qCDebug(BALOO) << "Batch finished";
31 Q_EMIT done();
32 });
33
34 connect(sender: &m_extractorProcess, signal: qOverload<int, QProcess::ExitStatus>(&QProcess::finished), slot: [=](int exitCode, QProcess::ExitStatus exitStatus) {
35 if (exitStatus == QProcess::CrashExit) {
36 qCWarning(BALOO) << "Extractor crashed";
37 Q_EMIT failed();
38 } else if (exitCode == 1) {
39 // DB open error
40 Q_EMIT failed();
41 } else if (exitCode == 2) {
42 // DB transaction commit error
43 Q_EMIT failed();
44 } else if (exitCode == 253) {
45 // DrKonqi mangles signals depending on the core_pattern
46 // and does a regular exit with status 253 instead
47 qCWarning(BALOO) << "Extractor probably crashed";
48 Q_EMIT failed();
49 } else if (exitCode != 0) {
50 qCWarning(BALOO) << "Unexpected exit code:" << exitCode;
51 Q_EMIT failed();
52 }
53 });
54
55 m_extractorProcess.setProgram(m_extractorPath);
56 m_extractorProcess.setProcessChannelMode(QProcess::ForwardedErrorChannel);
57 m_extractorProcess.start();
58}
59
60ExtractorProcess::~ExtractorProcess()
61{
62 m_extractorProcess.closeWriteChannel();
63 m_extractorProcess.waitForFinished();
64 m_extractorProcess.close();
65}
66
67void ExtractorProcess::start()
68{
69 m_extractorProcess.start(mode: QIODevice::Unbuffered | QIODevice::ReadWrite);
70 m_extractorProcess.waitForStarted();
71 m_extractorProcess.setReadChannel(QProcess::StandardOutput);
72}
73
74void ExtractorProcess::index(const QVector<quint64>& fileIds)
75{
76 Q_ASSERT(!fileIds.isEmpty());
77 m_controller.processIds(ids: fileIds);
78}
79
80#include "moc_extractorprocess.cpp"
81

source code of baloo/src/file/extractorprocess.cpp