#include "entry.h" #include #include Entry::Entry(const QString &name, const QString &tunnel, bool sudo, QObject *parent) : QObject{parent}, m_name{name}, m_tunnel{tunnel}, m_sudo{sudo} { connect(&m_process, &QProcess::errorOccurred, this, &Entry::errorOccurred); connect(&m_process, &QProcess::stateChanged, this, &Entry::stateChanged); connect(&m_process, &QProcess::readyReadStandardOutput, this, &Entry::readyReadStandardOutput); connect(&m_process, &QProcess::readyReadStandardError, this, &Entry::readyReadStandardError); } Entry::~Entry() { qDebug() << m_name; m_process.terminate(); if (!m_process.waitForFinished(250)) m_process.kill(); } Qt::CheckState Entry::state() const { switch (m_process.state()) { case QProcess::Starting: return Qt::PartiallyChecked; case QProcess::Running: return Qt::Checked; case QProcess::NotRunning: return Qt::Unchecked; } } void Entry::toggle() { if (m_process.state() == QProcess::NotRunning) start(); else stop(); } void Entry::start() { qDebug() << m_name; m_logOutput.clear(); m_process.start(binaryName(), arguments()); } void Entry::stop() { qDebug() << m_name; m_process.terminate(); } QString Entry::binaryName() const { return m_sudo ? "sudo" : "ssh"; } QStringList Entry::arguments() const { QStringList args { "-v", // Verbose mode. Causes ssh to print debugging messages about its progress. "-N", // Do not execute a remote command. This is useful for just forwarding ports. "-T", // Disable pseudo-terminal allocation. "-oServerAliveInterval=60", "-oExitOnForwardFailure=yes", m_tunnel, "pc178" }; if (m_sudo) { const QDir sshDir(QDir::home().absoluteFilePath(".ssh")); args.insert(0, "ssh"); args.insert(1, "-F"); args.insert(2, sshDir.absoluteFilePath("config")); args.insert(3, "-i"); args.insert(4, sshDir.absoluteFilePath("id_rsa")); } return args; } void Entry::errorOccurred(QProcess::ProcessError error) { qDebug() << m_name << error; } void Entry::stateChanged(QProcess::ProcessState state) { qDebug() << m_name << state; emit dataChanged(); } void Entry::readyReadStandardOutput() { m_process.setReadChannel(QProcess::StandardOutput); while (m_process.canReadLine()) { const auto line = m_process.readLine(); //qDebug() << m_name << line; m_logOutput.append(line); } } void Entry::readyReadStandardError() { m_process.setReadChannel(QProcess::StandardError); while (m_process.canReadLine()) { const auto line = m_process.readLine(); //qDebug() << m_name << line; m_logOutput.append(line); } }