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
5458void  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: " 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": )" 
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+ 
66105QString 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 (" { \ "\ "\ "\" } " 
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: " 
203+             return ;
204+         }
205+ 
206+         _webSocket->sendTextMessage (R"( {"command": "confirmInsertIntoClipboard"})" 
148207    } else  {
149208        qWarning () << " Unknown message from web app: " 
150209    }
0 commit comments