mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2024-10-01 01:26:01 -04:00
Merge pull request #1202 from weslly/feature/mac-pasteboard
Improve macOS pasteboard handling
This commit is contained in:
commit
1cb91fabb6
@ -155,6 +155,8 @@ if(APPLE)
|
||||
set(keepassx_SOURCES ${keepassx_SOURCES}
|
||||
core/ScreenLockListenerMac.h
|
||||
core/ScreenLockListenerMac.cpp
|
||||
core/MacPasteboard.h
|
||||
core/MacPasteboard.cpp
|
||||
)
|
||||
endif()
|
||||
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
|
||||
|
94
src/core/MacPasteboard.cpp
Normal file
94
src/core/MacPasteboard.cpp
Normal file
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 or (at your option)
|
||||
* version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "MacPasteboard.h"
|
||||
|
||||
QString MacPasteboard::convertorName() { return QLatin1String("MacPasteboard"); }
|
||||
|
||||
QString MacPasteboard::flavorFor(const QString& mimetype) {
|
||||
if (mimetype == QLatin1String("text/plain")) {
|
||||
return QLatin1String("public.utf8-plain-text");
|
||||
} else if (mimetype == QLatin1String("application/x-nspasteboard-concealed-type")) {
|
||||
return QLatin1String("org.nspasteboard.ConcealedType");
|
||||
}
|
||||
|
||||
int i = mimetype.indexOf(QLatin1String("charset="));
|
||||
|
||||
if (i >= 0) {
|
||||
QString cs(mimetype.mid(i + 8).toLower());
|
||||
i = cs.indexOf(QLatin1Char(';'));
|
||||
|
||||
if (i >= 0) {
|
||||
cs = cs.left(i);
|
||||
}
|
||||
|
||||
if (cs == QLatin1String("system")) {
|
||||
return QLatin1String("public.utf8-plain-text");
|
||||
} else if (cs == QLatin1String("iso-10646-ucs-2") ||
|
||||
cs == QLatin1String("utf16")) {
|
||||
return QLatin1String("public.utf16-plain-text");
|
||||
}
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString MacPasteboard::mimeFor(QString flavor) {
|
||||
if (flavor == QLatin1String("public.utf8-plain-text"))
|
||||
return QLatin1String("text/plain");
|
||||
if (flavor == QLatin1String("org.nspasteboard.ConcealedType"))
|
||||
return QLatin1String("application/x-nspasteboard-concealed-type");
|
||||
if (flavor == QLatin1String("public.utf16-plain-text"))
|
||||
return QLatin1String("text/plain;charset=utf16");
|
||||
return QString();
|
||||
}
|
||||
|
||||
bool MacPasteboard::canConvert(const QString& mimetype, QString flavor) {
|
||||
Q_UNUSED(mimetype);
|
||||
Q_UNUSED(flavor);
|
||||
return true;
|
||||
}
|
||||
|
||||
QVariant MacPasteboard::convertToMime(const QString& mimetype, QList<QByteArray> data, QString flavor) {
|
||||
if (data.count() > 1)
|
||||
qWarning("QMime::convertToMime: Cannot handle multiple member data");
|
||||
const QByteArray& firstData = data.first();
|
||||
QVariant ret;
|
||||
if (flavor == QLatin1String("public.utf8-plain-text")) {
|
||||
ret = QString::fromUtf8(firstData);
|
||||
} else if (flavor == QLatin1String("org.nspasteboard.ConcealedType")) {
|
||||
ret = QString::fromUtf8(firstData);
|
||||
} else if (flavor == QLatin1String("public.utf16-plain-text")) {
|
||||
ret = QTextCodec::codecForName("UTF-16")->toUnicode(firstData);
|
||||
} else {
|
||||
qWarning("QMime::convertToMime: unhandled mimetype: %s",
|
||||
qPrintable(mimetype));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
QList<QByteArray> MacPasteboard::convertFromMime(const QString&, QVariant data, QString flavor) {
|
||||
QList<QByteArray> ret;
|
||||
QString string = data.toString();
|
||||
if (flavor == QLatin1String("public.utf8-plain-text"))
|
||||
ret.append(string.toUtf8());
|
||||
else if (flavor == QLatin1String("org.nspasteboard.ConcealedType"))
|
||||
ret.append(string.toUtf8());
|
||||
else if (flavor == QLatin1String("public.utf16-plain-text"))
|
||||
ret.append(QTextCodec::codecForName("UTF-16")->fromUnicode(string));
|
||||
return ret;
|
||||
}
|
||||
|
37
src/core/MacPasteboard.h
Normal file
37
src/core/MacPasteboard.h
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 or (at your option)
|
||||
* version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef KEEPASSXC_MACPASTEBOARD_H
|
||||
#define KEEPASSXC_MACPASTEBOARD_H
|
||||
|
||||
#include <QMacPasteboardMime>
|
||||
#include <QTextCodec>
|
||||
|
||||
class MacPasteboard : public QMacPasteboardMime
|
||||
{
|
||||
public:
|
||||
explicit MacPasteboard() : QMacPasteboardMime(MIME_ALL) {}
|
||||
|
||||
QString convertorName() override;
|
||||
bool canConvert(const QString &mime, QString flav) override;
|
||||
QString mimeFor(QString flav) override;
|
||||
QString flavorFor(const QString &mime) override;
|
||||
QVariant convertToMime(const QString &mime, QList<QByteArray> data, QString flav) override;
|
||||
QList<QByteArray> convertFromMime(const QString &mime, QVariant data, QString flav) override;
|
||||
};
|
||||
|
||||
#endif // KEEPASSXC_MACPASTEBOARD_H
|
@ -28,6 +28,9 @@ Clipboard* Clipboard::m_instance(nullptr);
|
||||
Clipboard::Clipboard(QObject* parent)
|
||||
: QObject(parent)
|
||||
, m_timer(new QTimer(this))
|
||||
#ifdef Q_OS_MAC
|
||||
, m_pasteboard(new MacPasteboard)
|
||||
#endif
|
||||
{
|
||||
m_timer->setSingleShot(true);
|
||||
connect(m_timer, SIGNAL(timeout()), SLOT(clearClipboard()));
|
||||
@ -38,10 +41,17 @@ void Clipboard::setText(const QString& text)
|
||||
{
|
||||
QClipboard* clipboard = QApplication::clipboard();
|
||||
|
||||
#ifdef Q_OS_MAC
|
||||
QMimeData* mime = new QMimeData;
|
||||
mime->setText(text);
|
||||
mime->setData("application/x-nspasteboard-concealed-type", text.toUtf8());
|
||||
clipboard->setMimeData(mime, QClipboard::Clipboard);
|
||||
#else
|
||||
clipboard->setText(text, QClipboard::Clipboard);
|
||||
if (clipboard->supportsSelection()) {
|
||||
clipboard->setText(text, QClipboard::Selection);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (config()->get("security/clearclipboard").toBool()) {
|
||||
int timeout = config()->get("security/clearclipboardtimeout").toInt();
|
||||
|
@ -19,6 +19,9 @@
|
||||
#define KEEPASSX_CLIPBOARD_H
|
||||
|
||||
#include <QObject>
|
||||
#ifdef Q_OS_MAC
|
||||
#include "core/MacPasteboard.h"
|
||||
#endif
|
||||
|
||||
class QTimer;
|
||||
|
||||
@ -43,6 +46,9 @@ private:
|
||||
static Clipboard* m_instance;
|
||||
|
||||
QTimer* m_timer;
|
||||
#ifdef Q_OS_MAC
|
||||
QScopedPointer<MacPasteboard> m_pasteboard;
|
||||
#endif
|
||||
QString m_lastCopied;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user