refactor: WlClipboard::isAvailable use QStandardPaths::findExecutable to check if the apps exist. remove WlClipboard::checkCommandExists

This commit is contained in:
sithlord48 2025-11-09 21:33:22 -05:00 committed by Chris Rizzitello
parent d157713a85
commit c7e90d3cf5
2 changed files with 6 additions and 38 deletions

View file

@ -20,8 +20,13 @@
#include <sys/wait.h>
#include <unistd.h>
#include <QStandardPaths>
namespace {
inline static const auto s_copyApp = QStringLiteral("wl-copy");
inline static const auto s_pasteApp = QStringLiteral("wl-paste");
// MIME types for different clipboard formats
const char *const kMimeTypeText = "text/plain;charset=utf-8";
const char *const kMimeTypeHtml = "text/html";
@ -175,41 +180,7 @@ ClipboardID WlClipboard::getID() const
bool WlClipboard::isAvailable()
{
return checkCommandExists("wl-paste") && checkCommandExists("wl-copy");
}
bool WlClipboard::checkCommandExists(const char *command)
{
std::vector<const char *> args = {command, "--help", nullptr};
// Set up file actions for posix_spawn
posix_spawn_file_actions_t fileActions;
posix_spawn_file_actions_init(&fileActions);
// Redirect stdout and stderr to /dev/null
posix_spawn_file_actions_addopen(&fileActions, STDOUT_FILENO, "/dev/null", O_WRONLY, 0);
posix_spawn_file_actions_addopen(&fileActions, STDERR_FILENO, "/dev/null", O_WRONLY, 0);
extern char **environ;
pid_t pid;
int spawnResult = posix_spawnp(&pid, command, &fileActions, nullptr, const_cast<char *const *>(args.data()), environ);
posix_spawn_file_actions_destroy(&fileActions);
if (spawnResult != 0) {
return false;
}
ProcessGuard processGuard(pid);
int status;
bool success = waitpidWithTimeout(pid, &status, kCommandTimeout);
if (success) {
processGuard.release();
return WIFEXITED(status) && WEXITSTATUS(status) == 0;
} else {
return false;
}
return !QStandardPaths::findExecutable(s_copyApp).isEmpty() && !QStandardPaths::findExecutable(s_pasteApp).isEmpty();
}
bool WlClipboard::isEnabled()

View file

@ -70,9 +70,6 @@ private:
//! Execute a command with input data
bool executeCommandWithInput(const std::vector<const char *> &args, const std::string &input) const;
//! Check if a command exists
static bool checkCommandExists(const char *command);
//! Convert IClipboard format to MIME type
std::string formatToMimeType(Format format) const;