refactor: wlClipboard fix some sonar issues
This commit is contained in:
parent
50fd3365e8
commit
92e3e9ac64
2 changed files with 17 additions and 18 deletions
|
|
@ -41,7 +41,6 @@ const char *const s_mimeTypeHtmlUtf8 = "text/html;charset=UTF-8";
|
||||||
const char *const s_mimeTypeHtmlWindows = "HTML Format";
|
const char *const s_mimeTypeHtmlWindows = "HTML Format";
|
||||||
|
|
||||||
// Command timeout (milliseconds)
|
// Command timeout (milliseconds)
|
||||||
const int kCommandTimeout = 5000;
|
|
||||||
const int kCacheValidityMs = 100;
|
const int kCacheValidityMs = 100;
|
||||||
const int kMonitorIntervalMs = 1000;
|
const int kMonitorIntervalMs = 1000;
|
||||||
const int kMaxConsecutiveErrors = 5;
|
const int kMaxConsecutiveErrors = 5;
|
||||||
|
|
@ -218,26 +217,24 @@ bool WlClipboard::has(Format format) const
|
||||||
return m_cachedAvailable[static_cast<int>(format)];
|
return m_cachedAvailable[static_cast<int>(format)];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update cache by checking available MIME types
|
if (const auto availableTypes = getAvailableMimeTypes(); availableTypes.isEmpty()) {
|
||||||
const auto availableTypes = const_cast<WlClipboard *>(this)->getAvailableMimeTypes();
|
|
||||||
|
|
||||||
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;
|
||||||
m_cachedData[i].clear();
|
m_cachedData[i].clear();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
using enum IClipboard::Format;
|
||||||
// Check each format against available types
|
// Check each format against available types
|
||||||
for (int i = 0; i < static_cast<int>(Format::TotalFormats); ++i) {
|
for (int i = 0; i < static_cast<int>(TotalFormats); ++i) {
|
||||||
Format currentFormat = static_cast<Format>(i);
|
auto currentFormat = static_cast<Format>(i);
|
||||||
const auto mimeType = formatToMimeType(currentFormat);
|
const auto mimeType = formatToMimeType(currentFormat);
|
||||||
|
|
||||||
m_cachedAvailable[i] = false;
|
m_cachedAvailable[i] = false;
|
||||||
if (!mimeType.isEmpty()) {
|
if (!mimeType.isEmpty()) {
|
||||||
for (const auto &available : availableTypes) {
|
for (const auto &available : availableTypes) {
|
||||||
if (available == mimeType || (currentFormat == Format::Text && available == "text/plain") ||
|
if (available == mimeType || (currentFormat == Text && available == QStringLiteral("text/plain")) ||
|
||||||
(currentFormat == Format::HTML && available.startsWith("text/html"))) {
|
(currentFormat == HTML && available.startsWith(QStringLiteral("text/html")))) {
|
||||||
m_cachedAvailable[i] = true;
|
m_cachedAvailable[i] = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -295,11 +292,12 @@ std::string WlClipboard::get(Format format) const
|
||||||
QString WlClipboard::formatToMimeType(Format format) const
|
QString WlClipboard::formatToMimeType(Format format) const
|
||||||
{
|
{
|
||||||
switch (format) {
|
switch (format) {
|
||||||
case Format::Text:
|
using enum IClipboard::Format;
|
||||||
|
case Text:
|
||||||
return s_mimeTypeText;
|
return s_mimeTypeText;
|
||||||
case Format::HTML:
|
case HTML:
|
||||||
return s_mimeTypeHtml;
|
return s_mimeTypeHtml;
|
||||||
case Format::Bitmap:
|
case Bitmap:
|
||||||
return s_mimeTypeBmp;
|
return s_mimeTypeBmp;
|
||||||
default:
|
default:
|
||||||
return {};
|
return {};
|
||||||
|
|
@ -308,17 +306,18 @@ QString WlClipboard::formatToMimeType(Format format) const
|
||||||
|
|
||||||
IClipboard::Format WlClipboard::mimeTypeToFormat(const QString &mimeType) const
|
IClipboard::Format WlClipboard::mimeTypeToFormat(const QString &mimeType) const
|
||||||
{
|
{
|
||||||
|
using enum IClipboard::Format;
|
||||||
if (mimeType == s_mimeTypeText || mimeType == QStringLiteral("text/plain")) {
|
if (mimeType == s_mimeTypeText || mimeType == QStringLiteral("text/plain")) {
|
||||||
return Format::Text;
|
return Text;
|
||||||
}
|
}
|
||||||
if (mimeType == s_mimeTypeHtml || mimeType == s_mimeTypeHtmlUtf8 || mimeType == s_mimeTypeHtmlWindows ||
|
if (mimeType == s_mimeTypeHtml || mimeType == s_mimeTypeHtmlUtf8 || mimeType == s_mimeTypeHtmlWindows ||
|
||||||
mimeType.contains("text/html")) {
|
mimeType.contains("text/html")) {
|
||||||
return Format::HTML;
|
return HTML;
|
||||||
}
|
}
|
||||||
if (mimeType == s_mimeTypeBmp) {
|
if (mimeType == s_mimeTypeBmp) {
|
||||||
return Format::Bitmap;
|
return Bitmap;
|
||||||
}
|
}
|
||||||
return Format::Text; // Default fallback
|
return Text; // Default fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList WlClipboard::getAvailableMimeTypes() const
|
QStringList WlClipboard::getAvailableMimeTypes() const
|
||||||
|
|
@ -359,7 +358,7 @@ void WlClipboard::monitorClipboard()
|
||||||
// Clear cache when clipboard changes
|
// Clear cache when clipboard changes
|
||||||
std::scoped_lock<std::mutex> lock(m_cacheMutex);
|
std::scoped_lock<std::mutex> lock(m_cacheMutex);
|
||||||
invalidateCache();
|
invalidateCache();
|
||||||
const_cast<WlClipboard *>(this)->updateOwnership(false);
|
updateOwnership(false);
|
||||||
}
|
}
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
LOG_WARN("clipboard monitoring error: %s", e.what());
|
LOG_WARN("clipboard monitoring error: %s", e.what());
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ class WlClipboard : public QObject, public IClipboard
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
WlClipboard(ClipboardID id);
|
explicit WlClipboard(ClipboardID id);
|
||||||
WlClipboard(WlClipboard const &) = delete;
|
WlClipboard(WlClipboard const &) = delete;
|
||||||
WlClipboard(WlClipboard &&) = delete;
|
WlClipboard(WlClipboard &&) = delete;
|
||||||
~WlClipboard() override;
|
~WlClipboard() override;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue