ollie/kde/gui/plumber.cpp

95 lines
2.5 KiB
C++

#include "plumber.h"
#include <QStandardPaths>
#include <QFileInfo>
#include <QDir>
#include <QDebug>
Plumber::Plumber(QObject *parent)
: QObject(parent)
{
// Find plumb binary - check common plan9port locations
QStringList searchPaths = {
"/usr/local/plan9/bin/plumb",
"/usr/lib/plan9/bin/plumb",
QDir::homePath() + "/.local/plan9/bin/plumb",
QDir::homePath() + "/plan9port/bin/plumb"
};
// Also check PLAN9 environment variable
QString plan9 = qEnvironmentVariable("PLAN9");
if (!plan9.isEmpty()) {
searchPaths.prepend(plan9 + "/bin/plumb");
}
for (const QString &path : searchPaths) {
if (QFileInfo::exists(path)) {
m_plumbPath = path;
qDebug() << "Plumber: found plumb at" << m_plumbPath;
break;
}
}
if (m_plumbPath.isEmpty()) {
qDebug() << "Plumber: plumb binary not found, B3-plumb disabled";
}
}
bool Plumber::available() const
{
return !m_plumbPath.isEmpty();
}
void Plumber::plumb(const QString &text)
{
if (text.isEmpty()) return;
if (!available()) {
emit plumbError("plumb binary not found");
return;
}
QProcess *proc = new QProcess(this);
connect(proc, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [this, proc](int exitCode, QProcess::ExitStatus status) {
if (exitCode != 0 || status != QProcess::NormalExit) {
QString err = proc->readAllStandardError();
if (!err.isEmpty()) {
emit plumbError(err);
}
}
proc->deleteLater();
});
// plumb reads from stdin
proc->start(m_plumbPath, QStringList());
proc->write(text.toUtf8());
proc->closeWriteChannel();
}
void Plumber::plumbTo(const QString &text, const QString &dst)
{
if (text.isEmpty()) return;
if (!available()) {
emit plumbError("plumb binary not found");
return;
}
QProcess *proc = new QProcess(this);
connect(proc, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [this, proc](int exitCode, QProcess::ExitStatus status) {
if (exitCode != 0 || status != QProcess::NormalExit) {
QString err = proc->readAllStandardError();
if (!err.isEmpty()) {
emit plumbError(err);
}
}
proc->deleteLater();
});
// plumb -d destination reads from stdin
proc->start(m_plumbPath, QStringList() << "-d" << dst);
proc->write(text.toUtf8());
proc->closeWriteChannel();
}