refactor: WlClipboard, use QProcess to get available mimeTypes
This commit is contained in:
parent
9ed0b06a42
commit
7a60f0cc9e
2 changed files with 23 additions and 26 deletions
|
|
@ -21,6 +21,7 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
#include <QProcess>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
@ -28,6 +29,10 @@ namespace {
|
||||||
inline static const auto s_copyApp = QStringLiteral("wl-copy");
|
inline static const auto s_copyApp = QStringLiteral("wl-copy");
|
||||||
inline static const auto s_pasteApp = QStringLiteral("wl-paste");
|
inline static const auto s_pasteApp = QStringLiteral("wl-paste");
|
||||||
|
|
||||||
|
// wl-clipboard args
|
||||||
|
inline static const auto s_listTypes = QStringLiteral("--list-types");
|
||||||
|
inline static const auto s_isPrimary = QStringLiteral("--primary");
|
||||||
|
|
||||||
// MIME types for different clipboard formats
|
// MIME types for different clipboard formats
|
||||||
inline static const auto s_mimeTypeText = QStringLiteral("text/plain;charset=utf-8");
|
inline static const auto s_mimeTypeText = QStringLiteral("text/plain;charset=utf-8");
|
||||||
inline static const auto s_mimeTypeHtml = QStringLiteral("text/html");
|
inline static const auto s_mimeTypeHtml = QStringLiteral("text/html");
|
||||||
|
|
@ -322,9 +327,9 @@ bool WlClipboard::has(Format format) const
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update cache by checking available MIME types
|
// Update cache by checking available MIME types
|
||||||
std::vector<std::string> availableTypes = const_cast<WlClipboard *>(this)->getAvailableMimeTypes();
|
const auto availableTypes = const_cast<WlClipboard *>(this)->getAvailableMimeTypes();
|
||||||
|
|
||||||
if (availableTypes.empty()) {
|
if (availableTypes.isEmpty()) {
|
||||||
// No types available - mark all formats as unavailable
|
// No types available - mark all formats as unavailable
|
||||||
for (int i = 0; i < static_cast<int>(Format::TotalFormats); ++i) {
|
for (int i = 0; i < static_cast<int>(Format::TotalFormats); ++i) {
|
||||||
m_cachedAvailable[i] = false;
|
m_cachedAvailable[i] = false;
|
||||||
|
|
@ -338,9 +343,9 @@ bool WlClipboard::has(Format format) const
|
||||||
|
|
||||||
m_cachedAvailable[i] = false;
|
m_cachedAvailable[i] = false;
|
||||||
if (!mimeType.isEmpty()) {
|
if (!mimeType.isEmpty()) {
|
||||||
for (const std::string &available : availableTypes) {
|
for (const auto &available : availableTypes) {
|
||||||
if (available == mimeType || (currentFormat == Format::Text && available == "text/plain") ||
|
if (available == mimeType || (currentFormat == Format::Text && available == "text/plain") ||
|
||||||
(currentFormat == Format::HTML && available.find("text/html") == 0)) {
|
(currentFormat == Format::HTML && available.startsWith("text/html"))) {
|
||||||
m_cachedAvailable[i] = true;
|
m_cachedAvailable[i] = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -647,41 +652,33 @@ IClipboard::Format WlClipboard::mimeTypeToFormat(const QString &mimeType) const
|
||||||
return Format::Text; // Default fallback
|
return Format::Text; // Default fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> WlClipboard::getAvailableMimeTypes() const
|
QStringList WlClipboard::getAvailableMimeTypes() const
|
||||||
{
|
{
|
||||||
std::vector<const char *> args;
|
QProcess cmd;
|
||||||
if (m_useClipboard) {
|
cmd.setProgram(s_pasteApp);
|
||||||
args = {"wl-paste", "--list-types", nullptr};
|
|
||||||
} else {
|
|
||||||
args = {"wl-paste", "--list-types", "-p", nullptr};
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string result = executeCommand(args);
|
QStringList args = {s_listTypes};
|
||||||
std::vector<std::string> types;
|
if (!m_useClipboard)
|
||||||
|
args.append(s_isPrimary);
|
||||||
|
|
||||||
if (!result.empty()) {
|
cmd.setArguments(args);
|
||||||
std::istringstream iss(result);
|
cmd.start();
|
||||||
std::string type;
|
cmd.waitForFinished();
|
||||||
while (std::getline(iss, type)) {
|
|
||||||
if (!type.empty()) {
|
|
||||||
types.push_back(type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return types;
|
const static QChar newLine = QLatin1Char('\n');
|
||||||
|
return QString::fromLocal8Bit(cmd.readAll()).split(newLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WlClipboard::monitorClipboard()
|
void WlClipboard::monitorClipboard()
|
||||||
{
|
{
|
||||||
std::vector<std::string> lastTypes;
|
QStringList lastTypes;
|
||||||
int consecutiveErrors = 0;
|
int consecutiveErrors = 0;
|
||||||
|
|
||||||
while (!m_stopMonitoring) {
|
while (!m_stopMonitoring) {
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(kMonitorIntervalMs));
|
std::this_thread::sleep_for(std::chrono::milliseconds(kMonitorIntervalMs));
|
||||||
try {
|
try {
|
||||||
// Check if clipboard content has changed by comparing available types
|
// Check if clipboard content has changed by comparing available types
|
||||||
std::vector<std::string> currentTypes = getAvailableMimeTypes();
|
const auto currentTypes = getAvailableMimeTypes();
|
||||||
|
|
||||||
// Reset error counter on successful operation
|
// Reset error counter on successful operation
|
||||||
consecutiveErrors = 0;
|
consecutiveErrors = 0;
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,7 @@ private:
|
||||||
Format mimeTypeToFormat(const QString &mimeType) const;
|
Format mimeTypeToFormat(const QString &mimeType) const;
|
||||||
|
|
||||||
//! Get available MIME types from clipboard
|
//! Get available MIME types from clipboard
|
||||||
std::vector<std::string> getAvailableMimeTypes() const;
|
QStringList getAvailableMimeTypes() const;
|
||||||
|
|
||||||
//! Monitor clipboard changes in background thread
|
//! Monitor clipboard changes in background thread
|
||||||
void monitorClipboard();
|
void monitorClipboard();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue