Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Unix, copy to clipboard using wl-clipboard if xclip is not available #4323

Merged
merged 1 commit into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 38 additions & 23 deletions src/cli/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,48 +273,63 @@ 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;
}

QScopedPointer<QProcess> clipProcess(new QProcess(nullptr));
clipProcess->start(programName, arguments);
clipProcess->waitForStarted();
QStringList failedProgramNames;

if (clipProcess->state() != QProcess::Running) {
err << QObject::tr("Unable to start program %1").arg(programName);
err.flush();
return EXIT_FAILURE;
}
for (auto prog : clipPrograms) {
QScopedPointer<QProcess> clipProcess(new QProcess(nullptr));

// Skip empty parts, otherwise the program may clip the empty string
QStringList progArgs = prog.second.split(" ", QString::SkipEmptyParts);

if (clipProcess->write(text.toLatin1()) == -1) {
qDebug("Unable to write to process : %s", qPrintable(clipProcess->errorString()));
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;
}
}
clipProcess->waitForBytesWritten();
clipProcess->closeWriteChannel();
clipProcess->waitForFinished();

return clipProcess->exitCode();
// No clipping program worked
err << QObject::tr("All clipping programs failed. Tried %1\n").arg(failedProgramNames.join(", "));
err.flush();
return EXIT_FAILURE;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down