File indexing completed on 2024-04-28 03:51:47

0001 /*
0002     This file is part of the KDE Baloo Project
0003     SPDX-FileCopyrightText: 2021 Stefan BrĂ¼ns <stefan.bruens@rwth-aachen.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "commandpipe.h"
0009 #include <QVector>
0010 #include "baloodebug.h"
0011 #include <QIODevice>
0012 namespace Baloo {
0013 namespace Private {
0014 
0015 enum BatchStatus : quint8 {
0016     Invalid = 'X',
0017     UrlStarted = 'S',
0018     UrlFinished = 'F',
0019     UrlFailed = 'f',
0020     BatchFinished = 'B',
0021 };
0022 
0023 ControllerPipe::ControllerPipe(QIODevice* commandPipe, QIODevice* statusPipe)
0024     : m_commandStream(commandPipe)
0025     , m_statusStream(statusPipe)
0026 {
0027 }
0028 
0029 void ControllerPipe::processIds(const QVector<quint64>& ids)
0030 {
0031     m_commandStream << ids;
0032 }
0033 
0034 void ControllerPipe::processStatusData()
0035 {
0036     QString url;
0037     BatchStatus event{Invalid};
0038 
0039     while (true) {
0040         m_statusStream.startTransaction();
0041         m_statusStream >> event;
0042 
0043         if ((m_statusStream.status() != QDataStream::Ok) && m_statusStream.device()->atEnd()) {
0044         m_statusStream.rollbackTransaction();
0045             break;
0046         }
0047 
0048         if (event == BatchFinished) {
0049             if (m_statusStream.commitTransaction()) {
0050                 Q_EMIT batchFinished();
0051                 continue;
0052             } else {
0053                 break;
0054             }
0055         }
0056 
0057         m_statusStream >> url;
0058         if (!m_statusStream.commitTransaction()) {
0059             break;
0060         }
0061 
0062         switch (event) {
0063         case UrlStarted:
0064             Q_EMIT urlStarted(url);
0065             break;
0066 
0067         case UrlFinished:
0068             Q_EMIT urlFinished(url);
0069             break;
0070 
0071         case UrlFailed:
0072             Q_EMIT urlFailed(url);
0073             break;
0074 
0075         default:
0076             qCCritical(BALOO) << "Got unknown result from extractor" << event << url;
0077         }
0078     }
0079 }
0080 
0081 WorkerPipe::WorkerPipe(QIODevice* commandPipe, QIODevice* statusPipe)
0082     : m_commandStream(commandPipe)
0083     , m_statusStream(statusPipe)
0084 {
0085 }
0086 
0087 void WorkerPipe::processIdData()
0088 {
0089     QVector<quint64> ids;
0090 
0091     while (true) {
0092         m_commandStream.startTransaction();
0093         m_commandStream >> ids;
0094 
0095         /* QIODevice::atEnd() has to be checked *after* reading from
0096          * the QDataStream, but *before* commitTransaction to get
0097          * the correct pipe status.
0098          * QDataStream::status() will be `ReadPastEnd` for both partial
0099          * reads as well as closed pipes
0100          */
0101         if ((m_commandStream.status() != QDataStream::Ok) && m_commandStream.device()->atEnd()) {
0102             m_commandStream.rollbackTransaction();
0103             Q_EMIT inputEnd();
0104             return;
0105         }
0106 
0107         if (!m_commandStream.commitTransaction()) {
0108             return;
0109         }
0110 
0111         Q_EMIT newDocumentIds(ids);
0112         if (m_commandStream.device()->atEnd()) {
0113             return;
0114         }
0115     }
0116 }
0117 
0118 void WorkerPipe::urlStarted(const QString& url)
0119 {
0120     m_statusStream << UrlStarted << url;
0121 }
0122 
0123 void WorkerPipe::urlFinished(const QString& url)
0124 {
0125     m_statusStream << UrlFinished << url;
0126 }
0127 
0128 void WorkerPipe::urlFailed(const QString& url)
0129 {
0130     m_statusStream << UrlFailed << url;
0131 }
0132 
0133 void WorkerPipe::batchFinished()
0134 {
0135     m_statusStream << BatchFinished;
0136 }
0137 
0138 } // namespace Private
0139 } // namespace Baloo
0140 
0141 #include "moc_commandpipe.cpp"