File indexing completed on 2024-12-15 04:44:00

0001 /*
0002   SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "akonadisearchdebugsearchjob.h"
0008 
0009 #include <QProcess>
0010 #include <QStandardPaths>
0011 
0012 using namespace Akonadi::Search;
0013 AkonadiSearchDebugSearchJob::AkonadiSearchDebugSearchJob(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 AkonadiSearchDebugSearchJob::~AkonadiSearchDebugSearchJob() = default;
0019 
0020 void AkonadiSearchDebugSearchJob::start()
0021 {
0022     // "delve" is also a name of Go debugger, some distros prefer xapian-delve
0023     // for that reason, so try that first and fallback to "delve"
0024     QString delvePath = QStandardPaths::findExecutable(QStringLiteral("xapian-delve"));
0025     if (delvePath.isEmpty()) {
0026         delvePath = QStandardPaths::findExecutable(QStringLiteral("delve"));
0027     }
0028     if (delvePath.isEmpty()) {
0029         // Don't translate it. Just debug
0030         Q_EMIT error(QStringLiteral("\"delve\" not installed on computer."));
0031         deleteLater();
0032         return;
0033     } else {
0034         mProcess = new QProcess(this);
0035         connect(mProcess, &QProcess::readyReadStandardOutput, this, &AkonadiSearchDebugSearchJob::slotReadStandard);
0036         connect(mProcess, &QProcess::readyReadStandardError, this, &AkonadiSearchDebugSearchJob::slotReadError);
0037         mProcess->setWorkingDirectory(mPath);
0038         QStringList arguments;
0039         arguments << QStringLiteral("-r") << mAkonadiId;
0040         arguments << mPath;
0041         mProcess->start(delvePath, QStringList() << arguments);
0042     }
0043 }
0044 
0045 void AkonadiSearchDebugSearchJob::slotReadStandard()
0046 {
0047     const QByteArray stdStrg = mProcess->readAllStandardOutput();
0048     Q_EMIT result(QString::fromUtf8(stdStrg));
0049     mProcess->close();
0050     mProcess->deleteLater();
0051     mProcess = nullptr;
0052     deleteLater();
0053 }
0054 
0055 void AkonadiSearchDebugSearchJob::slotReadError()
0056 {
0057     const QByteArray errorStrg = mProcess->readAllStandardOutput();
0058     Q_EMIT error(QString::fromUtf8(errorStrg));
0059     mProcess->close();
0060     mProcess->deleteLater();
0061     mProcess = nullptr;
0062     deleteLater();
0063 }
0064 
0065 void AkonadiSearchDebugSearchJob::setAkonadiId(const QString &id)
0066 {
0067     mAkonadiId = id;
0068 }
0069 
0070 void AkonadiSearchDebugSearchJob::setArguments(const QStringList &args)
0071 {
0072     mArguments = args;
0073 }
0074 
0075 void AkonadiSearchDebugSearchJob::setSearchPath(const QString &path)
0076 {
0077     mPath = path;
0078 }
0079 
0080 #include "moc_akonadisearchdebugsearchjob.cpp"