Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qhaikuclipboard.cpp
Go to the documentation of this file.
1// Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Tobias Koenig <tobias.koenig@kdab.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#if !defined(QT_NO_CLIPBOARD)
5
6#include "qhaikuclipboard.h"
7
8#include <QMimeData>
9#include <QThread>
10
11#include <Clipboard.h>
12
13using namespace Qt::StringLiterals;
14
16 : m_systemMimeData(nullptr)
17 , m_userMimeData(nullptr)
18{
19 if (be_clipboard)
20 be_clipboard->StartWatching(BMessenger(this));
21}
22
24{
25 if (be_clipboard)
26 be_clipboard->StopWatching(BMessenger(this));
27
28 delete m_userMimeData;
29 delete m_systemMimeData;
30}
31
33{
35 return 0;
36
37 if (m_userMimeData)
38 return m_userMimeData;
39
40 if (!be_clipboard->Lock())
41 return 0;
42
43 if (!m_systemMimeData)
44 m_systemMimeData = new QMimeData();
45 else
46 m_systemMimeData->clear();
47
48 const BMessage *clipboard = be_clipboard->Data();
49 if (clipboard) {
50 char *name = nullptr;
51 uint32 type = 0;
52 int32 count = 0;
53
54 for (int i = 0; clipboard->GetInfo(B_MIME_TYPE, i, &name, &type, &count) == B_OK; i++) {
55 const void *data = nullptr;
56 int32 dataLen = 0;
57
58 const status_t status = clipboard->FindData(name, B_MIME_TYPE, &data, &dataLen);
59 if (dataLen && (status == B_OK)) {
61 if (format == "text/plain"_L1) {
62 m_systemMimeData->setText(QString::fromLocal8Bit(reinterpret_cast<const char*>(data), dataLen));
63 } else if (format == "text/html"_L1) {
64 m_systemMimeData->setHtml(QString::fromLocal8Bit(reinterpret_cast<const char*>(data), dataLen));
65 } else {
66 m_systemMimeData->setData(format, QByteArray(reinterpret_cast<const char*>(data), dataLen));
67 }
68 }
69 }
70 }
71
72 be_clipboard->Unlock();
73
74 return m_systemMimeData;
75}
76
78{
80 return;
81
82 if (mimeData) {
83 if (m_systemMimeData == mimeData)
84 return;
85
86 if (m_userMimeData == mimeData)
87 return;
88 }
89
90 if (!be_clipboard->Lock())
91 return;
92
93 be_clipboard->Clear();
94 if (mimeData) {
95 BMessage *clipboard = be_clipboard->Data();
96 if (clipboard) {
100 clipboard->AddData(format.toUtf8(), B_MIME_TYPE, data, data.count());
101 }
102 }
103 }
104
105 if (be_clipboard->Commit() != B_OK)
106 qWarning("Unable to store mime data on clipboard");
107
108 be_clipboard->Unlock();
109
110 m_userMimeData = mimeData;
111
113}
114
119
121{
122 Q_UNUSED(mode);
123
124 return false;
125}
126
128{
129 if (message->what == B_CLIPBOARD_CHANGED) {
130 delete m_userMimeData;
131 m_userMimeData = nullptr;
132
134 }
135
136 BHandler::MessageReceived(message);
137}
138
139#endif
\inmodule QtCore
Definition qbytearray.h:57
char * data()
\macro QT_NO_CAST_FROM_BYTEARRAY
Definition qbytearray.h:611
Mode
\keyword clipboard mode
Definition qclipboard.h:27
bool ownsMode(QClipboard::Mode mode) const override
bool supportsMode(QClipboard::Mode mode) const override
QMimeData * mimeData(QClipboard::Mode mode=QClipboard::Clipboard) override
void MessageReceived(BMessage *message) override
void setMimeData(QMimeData *data, QClipboard::Mode mode=QClipboard::Clipboard) override
\inmodule QtCore
Definition qmimedata.h:16
void setHtml(const QString &html)
Sets html as the HTML (MIME type text/html) used to represent the data.
void setData(const QString &mimetype, const QByteArray &data)
Sets the data associated with the MIME type given by mimeType to the specified data.
void setText(const QString &text)
Sets text as the plain text (MIME type text/plain) used to represent the data.
QByteArray data(const QString &mimetype) const
Returns the data stored in the object in the format described by the MIME type specified by mimeType.
void clear()
Removes all the MIME type and data entries in the object.
virtual QStringList formats() const
Returns a list of formats supported by the object.
void emitChanged(QClipboard::Mode mode)
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromLocal8Bit(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5949
EGLint EGLint * formats
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
#define Q_FOREACH(variable, container)
Definition qforeach.h:66
#define qWarning
Definition qlogging.h:166
GLenum mode
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum type
GLuint GLsizei const GLchar * message
GLuint name
GLint GLsizei GLsizei GLenum format
#define Q_UNUSED(x)
QMimeData * mimeData
QObject::connect nullptr