Action::Action(QObject *parent) : QProcess(parent) , m_failed(false) , m_firstProcess(NULL) , m_currentLine(-1) { setProcessChannelMode(QProcess::SeparateChannels); connect( this, SIGNAL(error(QProcess::ProcessError)), SLOT(actionError(QProcess::ProcessError)) ); connect( this, SIGNAL(started()), SLOT(actionStarted()) ); connect( this, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(actionFinished()) ); connect( this, SIGNAL(readyReadStandardError()), SLOT(actionErrorOutput()) ); connect( this, SIGNAL(readyReadStandardOutput()), this, SLOT(actionOutput()) ); quintptr id = actionId(this); QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); env.insert("COPYQ_ACTION_ID", QString::number(id)); setProcessEnvironment(env); setProperty("COPYQ_ACTION_ID", id); const QMutexLocker lock(&actionsLock); actions.append(this); }
/*! * \brief Translates text on not Linux paltforms. */ QString Translator::notLinuxTranslate(QString text) { QString name = parent->getCurrentSourceLang() + "-" + parent->getCurrentTargetLang(); if (name.isEmpty()) return ""; QDir dir(QDir(DATALOCATION).absoluteFilePath("usr/share/apertium/modes")); if (!dir.exists() || !dir.exists(name + ".mode")) return ""; QFile file(dir.absoluteFilePath(name + ".mode")); if (file.open(QIODevice::ReadOnly) == false) { return ""; } QString mode = file.readAll(); file.close(); mode = mode.trimmed(); if (mode.isEmpty()) { return ""; } mode.replace("$1", "-g"); mode.remove("$2"); if (mode.indexOf("'/usr/share") == -1) { mode.replace(QRegularExpression("(\\s*)(/usr/share/\\S+)(\\s*)"), "\\1\"\\2\"\\3"); } mode.replace("/usr/share", QDir(DATALOCATION).absolutePath() + "/usr/share"); #ifdef Q_OS_WIN // Windows can't handle C:/ paths in ' quotes mode.replace("'", "\""); #define OS_SEP ";" #else #define OS_SEP ":" #endif auto env = QProcessEnvironment::systemEnvironment(); env.insert("PATH", QDir(DATALOCATION).absoluteFilePath("apertium-all-dev/bin") + OS_SEP + env.value("PATH")); env.insert("LC_ALL", "en_US.UTF-8"); auto run = new QProcess(this); run->setProcessEnvironment(env); run->setProcessChannelMode(QProcess::MergedChannels); #ifdef Q_OS_WIN run->setNativeArguments(mode); run->start("cmd", QStringList() << "/D" << "/Q" << "/S" << "/C"); #else run->start("/bin/sh", QStringList() << "-c" << mode); #endif run->waitForStarted(); run->write(text.toUtf8() + " "); run->closeWriteChannel(); run->waitForFinished(); run->deleteLater(); return QString::fromUtf8(run->readAll()); }
IpProcess::IpProcess(QObject *parent) : QProcess(parent) { errStr = ""; QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); // Add an environment variable //env.insert("TMPDIR", "C:\\MyApp\\temp"); // Append a value to an environment variable //env.insert("PATH", env.value("Path") + ";C:\\Bin"); setProcessEnvironment(env); connect(this, SIGNAL(error(QProcess::ProcessError)), this, SLOT(slotError(QProcess::ProcessError)) ); }
void Action::start() { closeSubCommands(); if ( m_currentLine + 1 >= m_cmds.size() ) { finish(); return; } ++m_currentLine; const QList<QStringList> &cmds = m_cmds[m_currentLine]; Q_ASSERT( !cmds.isEmpty() ); QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); if (m_id != -1) env.insert("COPYQ_ACTION_ID", QString::number(m_id)); if ( !m_name.isEmpty() ) env.insert("COPYQ_ACTION_NAME", m_name); for (int i = 0; i < cmds.size(); ++i) { auto process = new QProcess(this); m_processes.push_back(process); process->setProcessEnvironment(env); if ( !m_workingDirectoryPath.isEmpty() ) process->setWorkingDirectory(m_workingDirectoryPath); connectProcessError(process, this, &Action::onSubProcessError); connect( process, &QProcess::readyReadStandardError, this, &Action::onSubProcessErrorOutput ); } pipeThroughProcesses(m_processes.begin(), m_processes.end()); QProcess *lastProcess = m_processes.back(); connect( lastProcess, &QProcess::started, this, &Action::onSubProcessStarted ); connectProcessFinished( lastProcess, this, &Action::onSubProcessFinished ); connect( lastProcess, &QProcess::readyReadStandardOutput, this, &Action::onSubProcessOutput ); // Writing directly to stdin of a process on Windows can hang the app. QProcess *firstProcess = m_processes.front(); connect( firstProcess, &QProcess::started, this, &Action::writeInput, Qt::QueuedConnection ); connect( firstProcess, &QProcess::bytesWritten, this, &Action::onBytesWritten, Qt::QueuedConnection ); const bool needWrite = !m_input.isEmpty(); if (m_processes.size() == 1) { const auto mode = (needWrite && m_readOutput) ? QIODevice::ReadWrite : needWrite ? QIODevice::WriteOnly : m_readOutput ? QIODevice::ReadOnly : QIODevice::NotOpen; startProcess(firstProcess, cmds.first(), mode); } else { auto it = m_processes.begin(); auto cmdIt = cmds.constBegin(); startProcess(*it, *cmdIt, needWrite ? QIODevice::ReadWrite : QIODevice::ReadOnly); for (++it, ++cmdIt; it != m_processes.end() - 1; ++it, ++cmdIt) startProcess(*it, *cmdIt, QIODevice::ReadWrite); startProcess(lastProcess, cmds.last(), m_readOutput ? QIODevice::ReadWrite : QIODevice::WriteOnly); } }