mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-02-02 09:34:58 -05:00
[CLI] On Unix, copy to clipboard using wl-clipboard if xclip is not available.
This commit is contained in:
parent
ef668f552e
commit
6128e5d582
@ -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);
|
||||
|
||||
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) {
|
||||
qDebug("Unable to write to process : %s", qPrintable(clipProcess->errorString()));
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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");
|
||||
|
Loading…
x
Reference in New Issue
Block a user