-
Notifications
You must be signed in to change notification settings - Fork 445
/
clipboardmonitor.cpp
204 lines (169 loc) · 6.16 KB
/
clipboardmonitor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// SPDX-License-Identifier: GPL-3.0-or-later
#include "clipboardmonitor.h"
#include "common/action.h"
#include "common/appconfig.h"
#include "common/common.h"
#include "common/log.h"
#include "common/mimetypes.h"
#include "common/textdata.h"
#include "item/serialize.h"
#include "platform/platformclipboard.h"
#ifdef COPYQ_WS_X11
# include "platform/x11/x11info.h"
#endif
#include <QApplication>
#include <QClipboard>
namespace {
bool hasSameData(const QVariantMap &data, const QVariantMap &lastData)
{
for (auto it = lastData.constBegin(); it != lastData.constEnd(); ++it) {
const auto &format = it.key();
if ( !data.contains(format) )
return false;
}
for (auto it = data.constBegin(); it != data.constEnd(); ++it) {
const auto &format = it.key();
if ( !data[format].toByteArray().isEmpty()
&& data[format] != lastData.value(format) )
{
return false;
}
}
return true;
}
bool isClipboardDataHidden(const QVariantMap &data)
{
return data.value(mimeHidden).toByteArray() == "1";
}
bool isClipboardDataSecret(const QVariantMap &data)
{
return data.value(mimeSecret).toByteArray() == "1";
}
int defaultOwnerUpdateInterval()
{
#ifdef COPYQ_WS_X11
if ( X11Info::isPlatformX11() )
return 150;
#endif
return 0;
}
} // namespace
ClipboardMonitor::ClipboardMonitor(const QStringList &formats)
: m_clipboard(platformNativeInterface()->clipboard())
, m_formats(formats)
, m_ownerMonitor(this)
{
const AppConfig config;
m_storeClipboard = config.option<Config::check_clipboard>();
m_clipboardTab = config.option<Config::clipboard_tab>();
const int ownerUpdateInterval = config.option<Config::update_clipboard_owner_delay_ms>();
m_ownerMonitor.setUpdateInterval(
ownerUpdateInterval < 0 ? defaultOwnerUpdateInterval() : ownerUpdateInterval);
m_formats.append({mimeOwner, mimeWindowTitle, mimeItemNotes, mimeHidden, mimeSecret});
m_formats.removeDuplicates();
connect( m_clipboard.get(), &PlatformClipboard::changed,
this, &ClipboardMonitor::onClipboardChanged );
#ifdef HAS_MOUSE_SELECTIONS
m_storeSelection = config.option<Config::check_selection>();
m_runSelection = config.option<Config::run_selection>();
m_clipboardToSelection = config.option<Config::copy_clipboard>()
&& m_clipboard->isSelectionSupported();
m_selectionToClipboard = config.option<Config::copy_selection>();
if (!m_storeSelection && !m_runSelection && !m_selectionToClipboard) {
COPYQ_LOG("Disabling selection monitoring");
m_clipboard->setMonitoringEnabled(ClipboardMode::Selection, false);
}
#endif
}
void ClipboardMonitor::startMonitoring()
{
setClipboardOwner(currentClipboardOwner());
connect(QGuiApplication::clipboard(), &QClipboard::changed,
this, [this](){ m_ownerMonitor.update(); });
m_clipboard->startMonitoring(m_formats);
}
QString ClipboardMonitor::currentClipboardOwner()
{
QString owner;
emit fetchCurrentClipboardOwner(&owner);
return owner;
}
void ClipboardMonitor::setClipboardOwner(const QString &owner)
{
if (m_clipboardOwner != owner) {
m_clipboardOwner = owner;
m_clipboard->setClipboardOwner(m_clipboardOwner);
COPYQ_LOG(QStringLiteral("Clipboard owner: %1").arg(owner));
}
}
void ClipboardMonitor::onClipboardChanged(ClipboardMode mode)
{
m_ownerMonitor.update();
QVariantMap data = m_clipboard->data(mode, m_formats);
auto clipboardData = mode == ClipboardMode::Clipboard
? &m_clipboardData : &m_selectionData;
const bool isDataUnchanged = hasSameData(data, *clipboardData);
if (!isDataUnchanged) {
*clipboardData = data;
#ifdef HAS_MOUSE_SELECTIONS
} else if (isDataUnchanged && !m_runSelection && mode == ClipboardMode::Selection) {
return;
#endif
}
if ( !data.contains(mimeOwner)
&& !data.contains(mimeWindowTitle)
&& !m_clipboardOwner.isEmpty() )
{
data.insert(mimeWindowTitle, m_clipboardOwner.toUtf8());
}
COPYQ_LOG( QStringLiteral("%1 changed, owner is: %2")
.arg(QLatin1String(mode == ClipboardMode::Clipboard ? "Clipboard" : "Selection"),
getTextData(data, mimeOwner)) );
const auto defaultTab = m_clipboardTab.isEmpty() ? defaultClipboardTabName() : m_clipboardTab;
setTextData(&data, defaultTab, mimeCurrentTab);
#ifdef HAS_MOUSE_SELECTIONS
if (mode == ClipboardMode::Selection)
data.insert(mimeClipboardMode, QByteArrayLiteral("selection"));
if (mode == ClipboardMode::Clipboard ? m_storeClipboard : m_storeSelection) {
#else
if (m_storeClipboard) {
#endif
setTextData(&data, m_clipboardTab, mimeOutputTab);
}
if (isDataUnchanged) {
emit clipboardUnchanged(data);
return;
}
#ifdef HAS_MOUSE_SELECTIONS
if ( (mode == ClipboardMode::Clipboard ? m_clipboardToSelection : m_selectionToClipboard)
&& !data.contains(mimeOwner) )
{
const auto text = getTextData(data);
if ( !text.isEmpty() ) {
const auto targetMode = mode == ClipboardMode::Clipboard
? ClipboardMode::Selection
: ClipboardMode::Clipboard;
const QVariantMap targetData = m_clipboard->data(targetMode, {mimeText});
const uint targetTextHash = qHash( targetData.value(mimeText).toByteArray() );
const uint sourceTextHash = qHash( data.value(mimeText).toByteArray() );
emit synchronizeSelection(mode, sourceTextHash, targetTextHash);
}
}
// omit running run automatic commands when disabled
if ( !m_runSelection && mode == ClipboardMode::Selection ) {
if ( m_storeSelection && !m_clipboardTab.isEmpty() && !isClipboardDataSecret(data) )
emit saveData(data);
return;
}
#endif
// run script callbacks
if ( isClipboardDataSecret(data) ) {
emit secretClipboardChanged(data);
} else if ( isClipboardDataHidden(data) ) {
emit hiddenClipboardChanged(data);
} else if ( anySessionOwnsClipboardData(data) ) {
emit ownClipboardChanged(data);
} else {
emit clipboardChanged(data);
}
}