[CLI] On Unix, copy to clipboard using wl-clipboard if xclip is not available.

This commit is contained in:
Tezkerek 2020-04-09 15:02:13 +03:00 committed by Janek Bevendorff
parent ef668f552e
commit 6128e5d582
2 changed files with 43 additions and 24 deletions

View File

@ -273,48 +273,63 @@ namespace Utils
{ {
TextStream err(Utils::STDERR); TextStream err(Utils::STDERR);
QString programName = ""; // List of programs and their arguments
QStringList arguments; QList<QPair<QString, QString>> clipPrograms;
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
programName = "xclip"; if (QProcessEnvironment::systemEnvironment().contains("WAYLAND_DISPLAY")) {
arguments << "-i" clipPrograms << qMakePair(QStringLiteral("wl-copy"), QStringLiteral(""));
<< "-selection" } else {
<< "clipboard"; clipPrograms << qMakePair(QStringLiteral("xclip"), QStringLiteral("-selection clipboard -i"));
}
#endif #endif
#ifdef Q_OS_MACOS #ifdef Q_OS_MACOS
programName = "pbcopy"; clipPrograms << qMakePair(QStringLiteral("pbcopy"), QStringLiteral(""));
#endif #endif
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
programName = "clip"; clipPrograms << qMakePair(QStringLiteral("clip"), QStringLiteral(""));
#endif #endif
if (programName.isEmpty()) { if (clipPrograms.isEmpty()) {
err << QObject::tr("No program defined for clipboard manipulation"); err << QObject::tr("No program defined for clipboard manipulation");
err.flush(); err.flush();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
QScopedPointer<QProcess> clipProcess(new QProcess(nullptr)); QStringList failedProgramNames;
clipProcess->start(programName, arguments);
clipProcess->waitForStarted();
if (clipProcess->state() != QProcess::Running) { for (auto prog : clipPrograms) {
err << QObject::tr("Unable to start program %1").arg(programName); QScopedPointer<QProcess> clipProcess(new QProcess(nullptr));
err.flush();
return EXIT_FAILURE; // Skip empty parts, otherwise the program may clip the empty string
QStringList progArgs = prog.second.split(" ", QString::SkipEmptyParts);
clipProcess->start(prog.first, progArgs);
clipProcess->waitForStarted();
if (clipProcess->state() != QProcess::Running) {
failedProgramNames.append(prog.first);
continue;
}
if (clipProcess->write(text.toLatin1()) == -1) {
qDebug("Unable to write to process : %s", qPrintable(clipProcess->errorString()));
}
clipProcess->waitForBytesWritten();
clipProcess->closeWriteChannel();
clipProcess->waitForFinished();
if (clipProcess->exitCode() == EXIT_SUCCESS) {
return EXIT_SUCCESS;
}
} }
if (clipProcess->write(text.toLatin1()) == -1) { // No clipping program worked
qDebug("Unable to write to process : %s", qPrintable(clipProcess->errorString())); err << QObject::tr("All clipping programs failed. Tried %1\n").arg(failedProgramNames.join(", "));
} err.flush();
clipProcess->waitForBytesWritten(); return EXIT_FAILURE;
clipProcess->closeWriteChannel();
clipProcess->waitForFinished();
return clipProcess->exitCode();
} }
/** /**

View File

@ -473,6 +473,10 @@ void TestCli::testClip()
m_stdoutFile->reset(); m_stdoutFile->reset();
QString errorOutput(m_stderrFile->readAll()); QString errorOutput(m_stderrFile->readAll());
if (QProcessEnvironment::systemEnvironment().contains("WAYLAND_DISPLAY")) {
QSKIP("Clip test skipped due to QClipboard and Wayland issues");
}
if (errorOutput.contains("Unable to start program") if (errorOutput.contains("Unable to start program")
|| errorOutput.contains("No program defined for clipboard manipulation")) { || errorOutput.contains("No program defined for clipboard manipulation")) {
QSKIP("Clip test skipped due to missing clipboard tool"); QSKIP("Clip test skipped due to missing clipboard tool");