[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,38 +273,45 @@ namespace Utils
{
TextStream err(Utils::STDERR);
QString programName = "";
QStringList arguments;
// List of programs and their arguments
QList<QPair<QString, QString>> clipPrograms;
#ifdef Q_OS_UNIX
programName = "xclip";
arguments << "-i"
<< "-selection"
<< "clipboard";
if (QProcessEnvironment::systemEnvironment().contains("WAYLAND_DISPLAY")) {
clipPrograms << qMakePair(QStringLiteral("wl-copy"), QStringLiteral(""));
} else {
clipPrograms << qMakePair(QStringLiteral("xclip"), QStringLiteral("-selection clipboard -i"));
}
#endif
#ifdef Q_OS_MACOS
programName = "pbcopy";
clipPrograms << qMakePair(QStringLiteral("pbcopy"), QStringLiteral(""));
#endif
#ifdef Q_OS_WIN
programName = "clip";
clipPrograms << qMakePair(QStringLiteral("clip"), QStringLiteral(""));
#endif
if (programName.isEmpty()) {
if (clipPrograms.isEmpty()) {
err << QObject::tr("No program defined for clipboard manipulation");
err.flush();
return EXIT_FAILURE;
}
QStringList failedProgramNames;
for (auto prog : clipPrograms) {
QScopedPointer<QProcess> clipProcess(new QProcess(nullptr));
clipProcess->start(programName, arguments);
// 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) {
err << QObject::tr("Unable to start program %1").arg(programName);
err.flush();
return EXIT_FAILURE;
failedProgramNames.append(prog.first);
continue;
}
if (clipProcess->write(text.toLatin1()) == -1) {
@ -314,7 +321,15 @@ namespace Utils
clipProcess->closeWriteChannel();
clipProcess->waitForFinished();
return clipProcess->exitCode();
if (clipProcess->exitCode() == EXIT_SUCCESS) {
return EXIT_SUCCESS;
}
}
// No clipping program worked
err << QObject::tr("All clipping programs failed. Tried %1\n").arg(failedProgramNames.join(", "));
err.flush();
return EXIT_FAILURE;
}
/**

View File

@ -473,6 +473,10 @@ void TestCli::testClip()
m_stdoutFile->reset();
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")
|| errorOutput.contains("No program defined for clipboard manipulation")) {
QSKIP("Clip test skipped due to missing clipboard tool");