1/*
2 This file is part of the KDE Baloo Project
3 SPDX-FileCopyrightText: 2021 Stefan BrĂ¼ns <stefan.bruens@rwth-aachen.de>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "commandpipe.h"
9#include <QVector>
10#include "baloodebug.h"
11#include <QIODevice>
12namespace Baloo {
13namespace Private {
14
15enum BatchStatus : quint8 {
16 Invalid = 'X',
17 UrlStarted = 'S',
18 UrlFinished = 'F',
19 UrlFailed = 'f',
20 BatchFinished = 'B',
21};
22
23ControllerPipe::ControllerPipe(QIODevice* commandPipe, QIODevice* statusPipe)
24 : m_commandStream(commandPipe)
25 , m_statusStream(statusPipe)
26{
27}
28
29void ControllerPipe::processIds(const QVector<quint64>& ids)
30{
31 m_commandStream << ids;
32}
33
34void ControllerPipe::processStatusData()
35{
36 QString url;
37 BatchStatus event{Invalid};
38
39 while (true) {
40 m_statusStream.startTransaction();
41 m_statusStream >> event;
42
43 if ((m_statusStream.status() != QDataStream::Ok) && m_statusStream.device()->atEnd()) {
44 m_statusStream.rollbackTransaction();
45 break;
46 }
47
48 if (event == BatchFinished) {
49 if (m_statusStream.commitTransaction()) {
50 Q_EMIT batchFinished();
51 continue;
52 } else {
53 break;
54 }
55 }
56
57 m_statusStream >> url;
58 if (!m_statusStream.commitTransaction()) {
59 break;
60 }
61
62 switch (event) {
63 case UrlStarted:
64 Q_EMIT urlStarted(url);
65 break;
66
67 case UrlFinished:
68 Q_EMIT urlFinished(url);
69 break;
70
71 case UrlFailed:
72 Q_EMIT urlFailed(url);
73 break;
74
75 default:
76 qCCritical(BALOO) << "Got unknown result from extractor" << event << url;
77 }
78 }
79}
80
81WorkerPipe::WorkerPipe(QIODevice* commandPipe, QIODevice* statusPipe)
82 : m_commandStream(commandPipe)
83 , m_statusStream(statusPipe)
84{
85}
86
87void WorkerPipe::processIdData()
88{
89 QVector<quint64> ids;
90
91 while (true) {
92 m_commandStream.startTransaction();
93 m_commandStream >> ids;
94
95 /* QIODevice::atEnd() has to be checked *after* reading from
96 * the QDataStream, but *before* commitTransaction to get
97 * the correct pipe status.
98 * QDataStream::status() will be `ReadPastEnd` for both partial
99 * reads as well as closed pipes
100 */
101 if ((m_commandStream.status() != QDataStream::Ok) && m_commandStream.device()->atEnd()) {
102 m_commandStream.rollbackTransaction();
103 Q_EMIT inputEnd();
104 return;
105 }
106
107 if (!m_commandStream.commitTransaction()) {
108 return;
109 }
110
111 Q_EMIT newDocumentIds(ids);
112 if (m_commandStream.device()->atEnd()) {
113 return;
114 }
115 }
116}
117
118void WorkerPipe::urlStarted(const QString& url)
119{
120 m_statusStream << UrlStarted << url;
121}
122
123void WorkerPipe::urlFinished(const QString& url)
124{
125 m_statusStream << UrlFinished << url;
126}
127
128void WorkerPipe::urlFailed(const QString& url)
129{
130 m_statusStream << UrlFailed << url;
131}
132
133void WorkerPipe::batchFinished()
134{
135 m_statusStream << BatchFinished;
136}
137
138} // namespace Private
139} // namespace Baloo
140
141#include "moc_commandpipe.cpp"
142

source code of baloo/src/file/extractor/commandpipe.cpp