Skip to content

Commit caedd6f

Browse files
committed
#3371 webappclientservice: start implementation of clipboard sharing
Signed-off-by: Patrizio Bekerle <patrizio@bekerle.com>
1 parent ddc2603 commit caedd6f

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/services/webappclientservice.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include <utils/misc.h>
2323

24+
#include <QApplication>
25+
#include <QClipboard>
2426
#include <QJsonDocument>
2527
#include <QSslError>
2628
#include <QWebSocket>
@@ -49,6 +51,8 @@ WebAppClientService::WebAppClientService(QObject *parent) : QObject(parent) {
4951
connect(&_timerReconnect, SIGNAL(timeout()), this, SLOT(onReconnect()));
5052

5153
open();
54+
55+
initClipboardService();
5256
}
5357

5458
void WebAppClientService::open() {
@@ -63,6 +67,41 @@ void WebAppClientService::close() {
6367
_url = "";
6468
}
6569

70+
void WebAppClientService::initClipboardService() {
71+
const QClipboard *clipboard = QApplication::clipboard();
72+
73+
// react to clipboard changes
74+
connect(clipboard, &QClipboard::dataChanged, [this, clipboard]() {
75+
if (!_webSocket->isValid()) {
76+
return;
77+
}
78+
79+
const QMimeData *mimeData = clipboard->mimeData();
80+
qDebug() << __func__ << "mimeData: " << mimeData->text();
81+
82+
if (mimeData->hasImage()) {
83+
const QPixmap pixmap = clipboard->pixmap();
84+
QByteArray byteArray;
85+
QBuffer buffer(&byteArray);
86+
buffer.open(QIODevice::WriteOnly);
87+
pixmap.save(&buffer, "PNG");
88+
const QString content = byteArray.toBase64();
89+
90+
_webSocket->sendTextMessage(
91+
R"({"command": "insertClipboard", "mimeType": "image/png", "content": )" + content +
92+
"\"}");
93+
} else if (mimeData->hasHtml()) {
94+
_webSocket->sendTextMessage(
95+
R"({"command": "insertClipboard", "mimeType": "text/html", "content": ")" +
96+
mimeData->html() + "\"}");
97+
} else if (mimeData->hasText()) {
98+
_webSocket->sendTextMessage(
99+
R"({"command": "insertClipboard", "mimeType": "text/plain", "content": ")" +
100+
mimeData->text() + "\"}");
101+
}
102+
});
103+
}
104+
66105
QString WebAppClientService::getServerUrl() {
67106
return SettingsService()
68107
.value(QStringLiteral("webAppClientService/serverUrl"), getDefaultServerUrl())
@@ -143,8 +182,28 @@ void WebAppClientService::onTextMessageReceived(const QString &message) {
143182
mainWindow->insertDataUrlAsFileIntoCurrentNote(fileDataUrl);
144183
}
145184

146-
_webSocket->sendTextMessage("{\"command\": \"confirmInsert\"}");
185+
_webSocket->sendTextMessage(R"({"command": "confirmInsert"})");
147186
#endif
187+
} else if (command == "insertIntoClipboard") {
188+
QClipboard *clipboard = QApplication::clipboard();
189+
const QString mimeType = jsonObject.value(QStringLiteral("mimeType")).toString();
190+
const QString content = jsonObject.value(QStringLiteral("content")).toString();
191+
192+
if (mimeType == "text/plain") {
193+
clipboard->setText(content, QClipboard::Clipboard);
194+
} else if (mimeType == "text/html") {
195+
clipboard->setText(content, QClipboard::Clipboard);
196+
} else if (mimeType == "image") {
197+
const QByteArray imageData = QByteArray::fromBase64(content.toUtf8());
198+
QImage image;
199+
image.loadFromData(imageData);
200+
clipboard->setImage(image, QClipboard::Clipboard);
201+
} else {
202+
qWarning() << "Unknown mime data type from web app: " << mimeType;
203+
return;
204+
}
205+
206+
_webSocket->sendTextMessage(R"({"command": "confirmInsertIntoClipboard"})");
148207
} else {
149208
qWarning() << "Unknown message from web app: " << message;
150209
}

src/services/webappclientservice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class QString;
2525
class WebAppClientService : public QObject {
2626
Q_OBJECT
2727
public:
28+
void initClipboardService();
2829
explicit WebAppClientService(QObject *parent = nullptr);
2930
~WebAppClientService() override;
3031

0 commit comments

Comments
 (0)