macOS: Make QMacMime's handler scope type-safe
The value indicates for which systems the handler is relevant, e.g. clipboard and/or drag'n'drop. Rename the enum from "MimeType", which is something else already, to "HandlerScope". Make the enum a scoped enum to avoid implicit conversion to uchar, and to allow for better value names. Use the type in APIs and only convert to uchar when needed. Make respective arguments default to both DnD and clipboard implicitly, instead of explicitly interpreting an invalid zero-value as a default value. Task-number: QTBUG-93632 Change-Id: I85ab982f6c9fe78ea4d030dd0b0791c8ab866f67 Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
parent
a1622083db
commit
bf08aac46a
@ -84,11 +84,11 @@ using namespace Qt::StringLiterals;
|
||||
*/
|
||||
|
||||
/*
|
||||
Constructs a new conversion object of type \a t, adding it to the
|
||||
Constructs a new conversion object of type \a scope, adding it to the
|
||||
globally accessed list of available converters.
|
||||
*/
|
||||
QMacMime::QMacMime(QMacPasteboardMimeType t)
|
||||
: m_type(t)
|
||||
QMacMime::QMacMime(HandlerScope scope)
|
||||
: m_scope(scope)
|
||||
{
|
||||
QMacMimeRegistry::registerMimeConverter(this);
|
||||
}
|
||||
@ -98,8 +98,9 @@ QMacMime::QMacMime(QMacPasteboardMimeType t)
|
||||
globally accessed list of available converters.
|
||||
*/
|
||||
QMacMime::QMacMime()
|
||||
: QMacMime(MIME_ALL)
|
||||
{}
|
||||
: QMacMime(HandlerScope::All)
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
Destroys a conversion object, removing it from the global
|
||||
@ -119,10 +120,9 @@ int QMacMime::count(const QMimeData *mimeData) const
|
||||
return 1;
|
||||
}
|
||||
|
||||
class QMacMimeAny : public QMacMime
|
||||
{
|
||||
class QMacMimeAny : public QMacMime {
|
||||
public:
|
||||
QMacMimeAny() : QMacMime(MIME_ALL_COMPATIBLE) {}
|
||||
QMacMimeAny() : QMacMime(HandlerScope::AllCompatible) {}
|
||||
|
||||
QString flavorFor(const QString &mime) const override;
|
||||
QString mimeFor(const QString &flav) const override;
|
||||
@ -175,10 +175,11 @@ QList<QByteArray> QMacMimeAny::convertFromMime(const QString &mime, const QVaria
|
||||
return ret;
|
||||
}
|
||||
|
||||
class QMacMimeTypeName : public QMacMime
|
||||
{
|
||||
class QMacMimeTypeName : public QMacMime {
|
||||
private:
|
||||
|
||||
public:
|
||||
QMacMimeTypeName(): QMacMime(MIME_ALL_COMPATIBLE) {}
|
||||
QMacMimeTypeName(): QMacMime(HandlerScope::AllCompatible) {}
|
||||
|
||||
QString flavorFor(const QString &mime) const override;
|
||||
QString mimeFor(const QString &flav) const override;
|
||||
@ -217,7 +218,8 @@ QList<QByteArray> QMacMimeTypeName::convertFromMime(const QString &, const QVari
|
||||
return ret;
|
||||
}
|
||||
|
||||
class QMacMimePlainTextFallback : public QMacMime {
|
||||
class QMacMimePlainTextFallback : public QMacMime
|
||||
{
|
||||
public:
|
||||
QString flavorFor(const QString &mime) const override;
|
||||
QString mimeFor(const QString &flav) const override;
|
||||
|
@ -25,21 +25,21 @@ QT_BEGIN_NAMESPACE
|
||||
class Q_GUI_EXPORT QMacMime
|
||||
{
|
||||
public:
|
||||
enum QMacPasteboardMimeType
|
||||
enum class HandlerScope : uchar
|
||||
{
|
||||
MIME_DND = 0x01,
|
||||
MIME_CLIP = 0x02,
|
||||
MIME_QT_CONVERTOR = 0x04,
|
||||
MIME_QT3_CONVERTOR = 0x08,
|
||||
MIME_ALL = MIME_DND|MIME_CLIP,
|
||||
MIME_ALL_COMPATIBLE = MIME_ALL|MIME_QT_CONVERTOR
|
||||
DnD = 0x01,
|
||||
Clipboard = 0x02,
|
||||
Qt_compatible = 0x04,
|
||||
Qt3_compatible = 0x08,
|
||||
All = DnD|Clipboard,
|
||||
AllCompatible = All|Qt_compatible
|
||||
};
|
||||
|
||||
QMacMime();
|
||||
explicit QMacMime(QMacPasteboardMimeType type); // internal
|
||||
explicit QMacMime(HandlerScope scope); // internal
|
||||
virtual ~QMacMime();
|
||||
|
||||
char type() const { return m_type; }
|
||||
HandlerScope scope() const { return m_scope; }
|
||||
|
||||
virtual bool canConvert(const QString &mime, const QString &flav) const = 0;
|
||||
virtual QString mimeFor(const QString &flav) const = 0;
|
||||
@ -49,7 +49,7 @@ public:
|
||||
virtual int count(const QMimeData *mimeData) const;
|
||||
|
||||
private:
|
||||
const QMacPasteboardMimeType m_type;
|
||||
const HandlerScope m_scope;
|
||||
};
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
@ -78,17 +78,18 @@ void destroyMimeTypes()
|
||||
}
|
||||
|
||||
/*
|
||||
Returns a MIME type of type \a t for \a flav, or 0 if none exists.
|
||||
Returns a MIME type of for scope \a scope for \a flav, or \nullptr if none exists.
|
||||
*/
|
||||
QString flavorToMime(uchar t, QString flav)
|
||||
QString flavorToMime(QMacMime::HandlerScope scope, const QString &flav)
|
||||
{
|
||||
MimeList *mimes = globalMimeList();
|
||||
for (MimeList::const_iterator it = mimes->constBegin(); it != mimes->constEnd(); ++it) {
|
||||
const bool relevantScope = uchar((*it)->scope()) & uchar(scope);
|
||||
#ifdef DEBUG_MIME_MAPS
|
||||
qDebug("QMacMimeRegistry::flavorToMime: attempting (%d) for flavor %s [%s]",
|
||||
(*it)->type() & t, qPrintable(flav), qPrintable((*it)->mimeFor(flav)));
|
||||
relevantScope, qPrintable(flav), qPrintable((*it)->mimeFor(flav)));
|
||||
#endif
|
||||
if ((*it)->type() & t) {
|
||||
if (relevantScope) {
|
||||
QString mimeType = (*it)->mimeFor(flav);
|
||||
if (!mimeType.isNull())
|
||||
return mimeType;
|
||||
@ -113,14 +114,15 @@ void unregisterMimeConverter(QMacMime *macMime)
|
||||
|
||||
|
||||
/*
|
||||
Returns a list of all currently defined QMacMime objects of type \a t.
|
||||
Returns a list of all currently defined QMacMime objects for scope \a scope.
|
||||
*/
|
||||
QList<QMacMime *> all(uchar t)
|
||||
QList<QMacMime *> all(QMacMime::HandlerScope scope)
|
||||
{
|
||||
MimeList ret;
|
||||
MimeList *mimes = globalMimeList();
|
||||
for (MimeList::const_iterator it = mimes->constBegin(); it != mimes->constEnd(); ++it) {
|
||||
if ((*it)->type() & t)
|
||||
const bool relevantScope = uchar((*it)->scope()) & uchar(scope);
|
||||
if (relevantScope)
|
||||
ret.append((*it));
|
||||
}
|
||||
return ret;
|
||||
|
@ -17,13 +17,12 @@
|
||||
|
||||
|
||||
#include <QtGui/private/qtguiglobal_p.h>
|
||||
#include <QtGui/private/qmacmime_p.h>
|
||||
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
class QMacMime;
|
||||
|
||||
namespace QMacMimeRegistry {
|
||||
Q_GUI_EXPORT void initializeMimeTypes();
|
||||
Q_GUI_EXPORT void destroyMimeTypes();
|
||||
@ -31,8 +30,8 @@ namespace QMacMimeRegistry {
|
||||
Q_GUI_EXPORT void registerMimeConverter(QMacMime *);
|
||||
Q_GUI_EXPORT void unregisterMimeConverter(QMacMime *);
|
||||
|
||||
Q_GUI_EXPORT QList<QMacMime *> all(uchar);
|
||||
Q_GUI_EXPORT QString flavorToMime(uchar, QString flav);
|
||||
Q_GUI_EXPORT QList<QMacMime *> all(QMacMime::HandlerScope scope);
|
||||
Q_GUI_EXPORT QString flavorToMime(QMacMime::HandlerScope scope, const QString &flav);
|
||||
|
||||
Q_GUI_EXPORT void registerDraggedTypes(const QStringList &types);
|
||||
Q_GUI_EXPORT const QStringList& enabledDraggedTypes();
|
||||
|
@ -10,8 +10,8 @@
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
QCocoaClipboard::QCocoaClipboard()
|
||||
:m_clipboard(new QMacPasteboard(kPasteboardClipboard, QMacMime::MIME_CLIP))
|
||||
,m_find(new QMacPasteboard(kPasteboardFind, QMacMime::MIME_CLIP))
|
||||
:m_clipboard(new QMacPasteboard(kPasteboardClipboard, QMacMime::HandlerScope::Clipboard))
|
||||
,m_find(new QMacPasteboard(kPasteboardFind, QMacMime::HandlerScope::Clipboard))
|
||||
{
|
||||
connect(qGuiApp, &QGuiApplication::applicationStateChanged, this, &QCocoaClipboard::handleApplicationStateChanged);
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ Qt::DropAction QCocoaDrag::drag(QDrag *o)
|
||||
m_drag = o;
|
||||
m_executed_drop_action = Qt::IgnoreAction;
|
||||
|
||||
QMacPasteboard dragBoard(CFStringRef(NSPasteboardNameDrag), QMacMime::MIME_DND);
|
||||
QMacPasteboard dragBoard(CFStringRef(NSPasteboardNameDrag), QMacMime::HandlerScope::DnD);
|
||||
m_drag->mimeData()->setData("application/x-qt-mime-type-name"_L1, QByteArray("dummy"));
|
||||
dragBoard.setMimeData(m_drag->mimeData(), QMacPasteboard::LazyRequest);
|
||||
|
||||
@ -305,7 +305,7 @@ QStringList QCocoaDropData::formats_sys() const
|
||||
qDebug("DnD: Cannot get PasteBoard!");
|
||||
return formats;
|
||||
}
|
||||
formats = QMacPasteboard(board, QMacMime::MIME_DND).formats();
|
||||
formats = QMacPasteboard(board, QMacMime::HandlerScope::DnD).formats();
|
||||
return formats;
|
||||
}
|
||||
|
||||
@ -317,7 +317,7 @@ QVariant QCocoaDropData::retrieveData_sys(const QString &mimeType, QMetaType typ
|
||||
qDebug("DnD: Cannot get PasteBoard!");
|
||||
return data;
|
||||
}
|
||||
data = QMacPasteboard(board, QMacMime::MIME_DND).retrieveData(mimeType, type);
|
||||
data = QMacPasteboard(board, QMacMime::HandlerScope::DnD).retrieveData(mimeType, type);
|
||||
CFRelease(board);
|
||||
return data;
|
||||
}
|
||||
@ -330,7 +330,7 @@ bool QCocoaDropData::hasFormat_sys(const QString &mimeType) const
|
||||
qDebug("DnD: Cannot get PasteBoard!");
|
||||
return has;
|
||||
}
|
||||
has = QMacPasteboard(board, QMacMime::MIME_DND).hasFormat(mimeType);
|
||||
has = QMacPasteboard(board, QMacMime::HandlerScope::DnD).hasFormat(mimeType);
|
||||
CFRelease(board);
|
||||
return has;
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
#define QMACCLIPBOARD_H
|
||||
|
||||
#include <QtGui>
|
||||
#include <QtGui/private/qmacmime_p.h>
|
||||
|
||||
#include <ApplicationServices/ApplicationServices.h>
|
||||
|
||||
@ -41,16 +42,16 @@ private:
|
||||
QList<Promise> promises;
|
||||
|
||||
PasteboardRef paste;
|
||||
uchar mime_type;
|
||||
const QMacMime::HandlerScope scope;
|
||||
mutable QPointer<QMimeData> mime;
|
||||
mutable bool mac_mime_source;
|
||||
bool resolvingBeforeDestruction;
|
||||
static OSStatus promiseKeeper(PasteboardRef, PasteboardItemID, CFStringRef, void *);
|
||||
void clear_helper();
|
||||
public:
|
||||
QMacPasteboard(PasteboardRef p, uchar mime_type=0);
|
||||
QMacPasteboard(uchar mime_type);
|
||||
QMacPasteboard(CFStringRef name=nullptr, uchar mime_type=0);
|
||||
QMacPasteboard(PasteboardRef p, QMacMime::HandlerScope scope = QMacMime::HandlerScope::All);
|
||||
QMacPasteboard(QMacMime::HandlerScope scope);
|
||||
QMacPasteboard(CFStringRef name=nullptr, QMacMime::HandlerScope scope = QMacMime::HandlerScope::All);
|
||||
~QMacPasteboard();
|
||||
|
||||
bool hasFlavor(QString flavor) const;
|
||||
|
@ -69,19 +69,19 @@ QMacPasteboard::Promise::Promise(int itemId, QMacMime *c, QString m, QMacMimeDat
|
||||
}
|
||||
}
|
||||
|
||||
QMacPasteboard::QMacPasteboard(PasteboardRef p, uchar mt)
|
||||
QMacPasteboard::QMacPasteboard(PasteboardRef p, QMacMime::HandlerScope scope)
|
||||
: scope(scope)
|
||||
{
|
||||
mac_mime_source = false;
|
||||
mime_type = mt ? mt : uchar(QMacMime::MIME_ALL);
|
||||
paste = p;
|
||||
CFRetain(paste);
|
||||
resolvingBeforeDestruction = false;
|
||||
}
|
||||
|
||||
QMacPasteboard::QMacPasteboard(uchar mt)
|
||||
QMacPasteboard::QMacPasteboard(QMacMime::HandlerScope scope)
|
||||
: scope(scope)
|
||||
{
|
||||
mac_mime_source = false;
|
||||
mime_type = mt ? mt : uchar(QMacMime::MIME_ALL);
|
||||
paste = nullptr;
|
||||
OSStatus err = PasteboardCreate(nullptr, &paste);
|
||||
if (err == noErr) {
|
||||
@ -92,10 +92,10 @@ QMacPasteboard::QMacPasteboard(uchar mt)
|
||||
resolvingBeforeDestruction = false;
|
||||
}
|
||||
|
||||
QMacPasteboard::QMacPasteboard(CFStringRef name, uchar mt)
|
||||
QMacPasteboard::QMacPasteboard(CFStringRef name, QMacMime::HandlerScope scope)
|
||||
: scope(scope)
|
||||
{
|
||||
mac_mime_source = false;
|
||||
mime_type = mt ? mt : uchar(QMacMime::MIME_ALL);
|
||||
paste = nullptr;
|
||||
OSStatus err = PasteboardCreate(name, &paste);
|
||||
if (err == noErr) {
|
||||
@ -112,7 +112,7 @@ QMacPasteboard::~QMacPasteboard()
|
||||
Commit all promises for paste when shutting down,
|
||||
unless we are the stack-allocated clipboard used by QCocoaDrag.
|
||||
*/
|
||||
if (mime_type == QMacMime::MIME_DND)
|
||||
if (scope == QMacMime::HandlerScope::DnD)
|
||||
resolvingBeforeDestruction = true;
|
||||
PasteboardResolvePromises(paste);
|
||||
if (paste)
|
||||
@ -132,7 +132,7 @@ OSStatus QMacPasteboard::promiseKeeper(PasteboardRef paste, PasteboardItemID id,
|
||||
|
||||
// Find the kept promise
|
||||
QList<QMacMime*> availableConverters
|
||||
= QMacMimeRegistry::all(QMacMime::MIME_ALL);
|
||||
= QMacMimeRegistry::all(QMacMime::HandlerScope::All);
|
||||
const QString flavorAsQString = QString::fromCFString(flavor);
|
||||
QMacPasteboard::Promise promise;
|
||||
for (int i = 0; i < qpaste->promises.size(); i++){
|
||||
@ -299,7 +299,7 @@ QMacPasteboard::setMimeData(QMimeData *mime_src, DataRequestType dataRequestType
|
||||
delete mime;
|
||||
mime = mime_src;
|
||||
|
||||
const QList<QMacMime*> availableConverters = QMacMimeRegistry::all(mime_type);
|
||||
const QList<QMacMime*> availableConverters = QMacMimeRegistry::all(scope);
|
||||
if (mime != nullptr) {
|
||||
clear_helper();
|
||||
QStringList formats = mime_src->formats();
|
||||
@ -373,7 +373,7 @@ QMacPasteboard::formats() const
|
||||
for (int i = 0; i < type_count; ++i) {
|
||||
const QString flavor = QString::fromCFString((CFStringRef)CFArrayGetValueAtIndex(types, i));
|
||||
qCDebug(lcQpaClipboard, " -%s", qPrintable(QString(flavor)));
|
||||
QString mimeType = QMacMimeRegistry::flavorToMime(mime_type, flavor);
|
||||
const QString mimeType = QMacMimeRegistry::flavorToMime(scope, flavor);
|
||||
if (!mimeType.isEmpty() && !ret.contains(mimeType)) {
|
||||
qCDebug(lcQpaClipboard, " -<%lld> %s [%s]", ret.size(), qPrintable(mimeType), qPrintable(QString(flavor)));
|
||||
ret << mimeType;
|
||||
@ -409,8 +409,8 @@ QMacPasteboard::hasFormat(const QString &format) const
|
||||
const int type_count = CFArrayGetCount(types);
|
||||
for (int i = 0; i < type_count; ++i) {
|
||||
const QString flavor = QString::fromCFString((CFStringRef)CFArrayGetValueAtIndex(types, i));
|
||||
qCDebug(lcQpaClipboard, " -%s [0x%x]", qPrintable(QString(flavor)), mime_type);
|
||||
QString mimeType = QMacMimeRegistry::flavorToMime(mime_type, flavor);
|
||||
qCDebug(lcQpaClipboard, " -%s [0x%x]", qPrintable(flavor), uchar(scope));
|
||||
QString mimeType = QMacMimeRegistry::flavorToMime(scope, flavor);
|
||||
if (!mimeType.isEmpty())
|
||||
qCDebug(lcQpaClipboard, " - %s", qPrintable(mimeType));
|
||||
if (mimeType == format)
|
||||
@ -433,7 +433,7 @@ QMacPasteboard::retrieveData(const QString &format, QMetaType) const
|
||||
return QByteArray();
|
||||
|
||||
qCDebug(lcQpaClipboard, "Pasteboard: retrieveData [%s]", qPrintable(format));
|
||||
const QList<QMacMime *> availableConverters = QMacMimeRegistry::all(mime_type);
|
||||
const QList<QMacMime *> availableConverters = QMacMimeRegistry::all(scope);
|
||||
for (const auto *c : availableConverters) {
|
||||
QString c_flavor = c->flavorFor(format);
|
||||
if (!c_flavor.isEmpty()) {
|
||||
|
@ -117,7 +117,7 @@ QStringList QIOSMimeData::formats() const
|
||||
|
||||
for (NSUInteger i = 0; i < [pasteboardTypes count]; ++i) {
|
||||
const QString uti = QString::fromNSString([pasteboardTypes objectAtIndex:i]);
|
||||
const QString mimeType = QMacMimeRegistry::flavorToMime(QMacMime::MIME_ALL, uti);
|
||||
const QString mimeType = QMacMimeRegistry::flavorToMime(QMacMime::HandlerScope::All, uti);
|
||||
if (!mimeType.isEmpty() && !foundMimeTypes.contains(mimeType))
|
||||
foundMimeTypes << mimeType;
|
||||
}
|
||||
@ -130,7 +130,7 @@ QVariant QIOSMimeData::retrieveData(const QString &mimeType, QMetaType) const
|
||||
UIPasteboard *pb = [UIPasteboard pasteboardWithQClipboardMode:m_mode];
|
||||
NSArray<NSString *> *pasteboardTypes = [pb pasteboardTypes];
|
||||
|
||||
foreach (QMacMime *converter, QMacMimeRegistry::all(QMacMime::MIME_ALL)) {
|
||||
foreach (QMacMime *converter, QMacMimeRegistry::all(QMacMime::HandlerScope::All)) {
|
||||
if (!converter->canConvert(mimeType, converter->flavorFor(mimeType)))
|
||||
continue;
|
||||
|
||||
@ -184,7 +184,7 @@ void QIOSClipboard::setMimeData(QMimeData *mimeData, QClipboard::Mode mode)
|
||||
NSMutableDictionary<NSString *, id> *pbItem = [NSMutableDictionary<NSString *, id> dictionaryWithCapacity:mimeData->formats().size()];
|
||||
|
||||
foreach (const QString &mimeType, mimeData->formats()) {
|
||||
foreach (QMacMime *converter, QMacMimeRegistry::all(QMacMime::MIME_ALL)) {
|
||||
foreach (QMacMime *converter, QMacMimeRegistry::all(QMacMime::HandlerScope::All)) {
|
||||
QString uti = converter->flavorFor(mimeType);
|
||||
if (uti.isEmpty() || !converter->canConvert(mimeType, uti))
|
||||
continue;
|
||||
|
Loading…
Reference in New Issue
Block a user