| 1 |
// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
|
| 3 |
#include "macclipboard.h" |
| 4 |
|
| 5 |
#include "common/common.h" |
| 6 |
#include "common/mimetypes.h" |
| 7 |
#include "common/textdata.h" |
| 8 |
#include "platform/platformnativeinterface.h" |
| 9 |
#include "platform/platformwindow.h" |
| 10 |
|
| 11 |
#include <QApplication> |
| 12 |
#include <QClipboard> |
| 13 |
#include <QMimeData> |
| 14 |
|
| 15 |
#include <QStringEncoder> |
| 16 |
|
| 17 |
#include "mactimer.h" |
| 18 |
|
| 19 |
#include <Cocoa/Cocoa.h> |
| 20 |
#include <Carbon/Carbon.h> |
| 21 |
|
| 22 |
namespace { |
| 23 |
|
| 24 |
int clipboardMonitorIntervalMs() |
| 25 |
{ |
| 26 |
bool ok = false; |
| 27 |
const int ms = qEnvironmentVariableIntValue("COPYQ_CLIPBOARD_MONITOR_INTERVAL_MS", &ok); |
| 28 |
return (ok && ms > 0) ? ms : 500; |
| 29 |
} |
| 30 |
|
| 31 |
int clipboardMonitorToleranceMs() |
| 32 |
{ |
| 33 |
bool ok = false; |
| 34 |
const int ms = qEnvironmentVariableIntValue("COPYQ_CLIPBOARD_MONITOR_TOLERANCE_MS", &ok); |
| 35 |
return (ok && ms >= 0) ? ms : 5000; |
| 36 |
} |
| 37 |
|
| 38 |
} // namespace |
| 39 |
|
| 40 |
void MacClipboard::startMonitoringBackend(const QStringList &formats, ClipboardModeMask modes) |
| 41 |
{ |
| 42 |
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard]; |
| 43 |
m_prevChangeCount = [pasteboard changeCount]; |
| 44 |
|
| 45 |
m_timer = new MacTimer(this); |
| 46 |
m_timer->setInterval(clipboardMonitorIntervalMs()); |
| 47 |
m_timer->setTolerance(clipboardMonitorToleranceMs()); |
| 48 |
connect(m_timer, &MacTimer::timeout, this, &MacClipboard::clipboardTimeout); |
| 49 |
m_timer->start(); |
| 50 |
|
| 51 |
DummyClipboard::startMonitoringBackend(formats, modes); |
| 52 |
} |
| 53 |
|
| 54 |
void MacClipboard::stopMonitoringBackend() |
| 55 |
{ |
| 56 |
if (m_timer) { |
| 57 |
m_timer->stop(); |
| 58 |
m_timer->deleteLater(); |
| 59 |
m_timer = nullptr; |
| 60 |
} |
| 61 |
DummyClipboard::stopMonitoringBackend(); |
| 62 |
} |
| 63 |
|
| 64 |
void MacClipboard::setData(ClipboardMode mode, const QVariantMap &dataMap) |
| 65 |
{ |
| 66 |
auto dataMapForMac = dataMap; |
| 67 |
|
| 68 |
// Qt adds BOM to UTF-16 text but some application on OS X |
| 69 |
// doesn't expect BOM to be present. |
| 70 |
// This converts text to UTF-16 without BOM. |
| 71 |
const auto text = getTextData(dataMap); |
| 72 |
if ( !text.isEmpty() ) { |
| 73 |
auto encoder = QStringEncoder(QStringConverter::Utf16); |
| 74 |
const QByteArray data = encoder.encode(text); |
| 75 |
dataMapForMac[QStringLiteral("public.utf16-plain-text")] = data; |
| 76 |
} |
| 77 |
|
| 78 |
DummyClipboard::setData(mode, dataMapForMac); |
| 79 |
} |
| 80 |
|
| 81 |
bool MacClipboard::isHidden(const QMimeData &data) const |
| 82 |
{ |
| 83 |
return data.hasFormat( QStringLiteral("application/x-nspasteboard-concealed-type") ); |
| 84 |
} |
| 85 |
|
| 86 |
void MacClipboard::onChanged(int mode) |
| 87 |
{ |
| 88 |
if (mode != QClipboard::Clipboard) |
| 89 |
return; |
| 90 |
|
| 91 |
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard]; |
| 92 |
NSInteger newCount = [pasteboard changeCount]; |
| 93 |
|
| 94 |
if (newCount == m_prevChangeCount) |
| 95 |
return; |
| 96 |
|
| 97 |
m_prevChangeCount = newCount; |
| 98 |
|
| 99 |
emitConnectionChanged(ClipboardMode::Clipboard); |
| 100 |
} |
| 101 |
|
| 102 |
void MacClipboard::clipboardTimeout() { |
| 103 |
m_timer->stop(); |
| 104 |
onChanged(QClipboard::Clipboard); |
| 105 |
if (m_timer) |
| 106 |
m_timer->start(); |
| 107 |
} |