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:
Volker Hilsheimer 2022-11-02 14:13:54 +01:00
parent a1622083db
commit bf08aac46a
9 changed files with 63 additions and 59 deletions

View File

@ -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. globally accessed list of available converters.
*/ */
QMacMime::QMacMime(QMacPasteboardMimeType t) QMacMime::QMacMime(HandlerScope scope)
: m_type(t) : m_scope(scope)
{ {
QMacMimeRegistry::registerMimeConverter(this); QMacMimeRegistry::registerMimeConverter(this);
} }
@ -98,8 +98,9 @@ QMacMime::QMacMime(QMacPasteboardMimeType t)
globally accessed list of available converters. globally accessed list of available converters.
*/ */
QMacMime::QMacMime() QMacMime::QMacMime()
: QMacMime(MIME_ALL) : QMacMime(HandlerScope::All)
{} {
}
/* /*
Destroys a conversion object, removing it from the global Destroys a conversion object, removing it from the global
@ -119,10 +120,9 @@ int QMacMime::count(const QMimeData *mimeData) const
return 1; return 1;
} }
class QMacMimeAny : public QMacMime class QMacMimeAny : public QMacMime {
{
public: public:
QMacMimeAny() : QMacMime(MIME_ALL_COMPATIBLE) {} QMacMimeAny() : QMacMime(HandlerScope::AllCompatible) {}
QString flavorFor(const QString &mime) const override; QString flavorFor(const QString &mime) const override;
QString mimeFor(const QString &flav) const override; QString mimeFor(const QString &flav) const override;
@ -175,10 +175,11 @@ QList<QByteArray> QMacMimeAny::convertFromMime(const QString &mime, const QVaria
return ret; return ret;
} }
class QMacMimeTypeName : public QMacMime class QMacMimeTypeName : public QMacMime {
{ private:
public: public:
QMacMimeTypeName(): QMacMime(MIME_ALL_COMPATIBLE) {} QMacMimeTypeName(): QMacMime(HandlerScope::AllCompatible) {}
QString flavorFor(const QString &mime) const override; QString flavorFor(const QString &mime) const override;
QString mimeFor(const QString &flav) const override; QString mimeFor(const QString &flav) const override;
@ -217,7 +218,8 @@ QList<QByteArray> QMacMimeTypeName::convertFromMime(const QString &, const QVari
return ret; return ret;
} }
class QMacMimePlainTextFallback : public QMacMime { class QMacMimePlainTextFallback : public QMacMime
{
public: public:
QString flavorFor(const QString &mime) const override; QString flavorFor(const QString &mime) const override;
QString mimeFor(const QString &flav) const override; QString mimeFor(const QString &flav) const override;

View File

@ -25,21 +25,21 @@ QT_BEGIN_NAMESPACE
class Q_GUI_EXPORT QMacMime class Q_GUI_EXPORT QMacMime
{ {
public: public:
enum QMacPasteboardMimeType enum class HandlerScope : uchar
{ {
MIME_DND = 0x01, DnD = 0x01,
MIME_CLIP = 0x02, Clipboard = 0x02,
MIME_QT_CONVERTOR = 0x04, Qt_compatible = 0x04,
MIME_QT3_CONVERTOR = 0x08, Qt3_compatible = 0x08,
MIME_ALL = MIME_DND|MIME_CLIP, All = DnD|Clipboard,
MIME_ALL_COMPATIBLE = MIME_ALL|MIME_QT_CONVERTOR AllCompatible = All|Qt_compatible
}; };
QMacMime(); QMacMime();
explicit QMacMime(QMacPasteboardMimeType type); // internal explicit QMacMime(HandlerScope scope); // internal
virtual ~QMacMime(); 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 bool canConvert(const QString &mime, const QString &flav) const = 0;
virtual QString mimeFor(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; virtual int count(const QMimeData *mimeData) const;
private: private:
const QMacPasteboardMimeType m_type; const HandlerScope m_scope;
}; };
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -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(); MimeList *mimes = globalMimeList();
for (MimeList::const_iterator it = mimes->constBegin(); it != mimes->constEnd(); ++it) { for (MimeList::const_iterator it = mimes->constBegin(); it != mimes->constEnd(); ++it) {
const bool relevantScope = uchar((*it)->scope()) & uchar(scope);
#ifdef DEBUG_MIME_MAPS #ifdef DEBUG_MIME_MAPS
qDebug("QMacMimeRegistry::flavorToMime: attempting (%d) for flavor %s [%s]", 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 #endif
if ((*it)->type() & t) { if (relevantScope) {
QString mimeType = (*it)->mimeFor(flav); QString mimeType = (*it)->mimeFor(flav);
if (!mimeType.isNull()) if (!mimeType.isNull())
return mimeType; 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 ret;
MimeList *mimes = globalMimeList(); MimeList *mimes = globalMimeList();
for (MimeList::const_iterator it = mimes->constBegin(); it != mimes->constEnd(); ++it) { 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)); ret.append((*it));
} }
return ret; return ret;

View File

@ -17,13 +17,12 @@
#include <QtGui/private/qtguiglobal_p.h> #include <QtGui/private/qtguiglobal_p.h>
#include <QtGui/private/qmacmime_p.h>
#include <CoreFoundation/CoreFoundation.h> #include <CoreFoundation/CoreFoundation.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QMacMime;
namespace QMacMimeRegistry { namespace QMacMimeRegistry {
Q_GUI_EXPORT void initializeMimeTypes(); Q_GUI_EXPORT void initializeMimeTypes();
Q_GUI_EXPORT void destroyMimeTypes(); Q_GUI_EXPORT void destroyMimeTypes();
@ -31,8 +30,8 @@ namespace QMacMimeRegistry {
Q_GUI_EXPORT void registerMimeConverter(QMacMime *); Q_GUI_EXPORT void registerMimeConverter(QMacMime *);
Q_GUI_EXPORT void unregisterMimeConverter(QMacMime *); Q_GUI_EXPORT void unregisterMimeConverter(QMacMime *);
Q_GUI_EXPORT QList<QMacMime *> all(uchar); Q_GUI_EXPORT QList<QMacMime *> all(QMacMime::HandlerScope scope);
Q_GUI_EXPORT QString flavorToMime(uchar, QString flav); Q_GUI_EXPORT QString flavorToMime(QMacMime::HandlerScope scope, const QString &flav);
Q_GUI_EXPORT void registerDraggedTypes(const QStringList &types); Q_GUI_EXPORT void registerDraggedTypes(const QStringList &types);
Q_GUI_EXPORT const QStringList& enabledDraggedTypes(); Q_GUI_EXPORT const QStringList& enabledDraggedTypes();

View File

@ -10,8 +10,8 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
QCocoaClipboard::QCocoaClipboard() QCocoaClipboard::QCocoaClipboard()
:m_clipboard(new QMacPasteboard(kPasteboardClipboard, QMacMime::MIME_CLIP)) :m_clipboard(new QMacPasteboard(kPasteboardClipboard, QMacMime::HandlerScope::Clipboard))
,m_find(new QMacPasteboard(kPasteboardFind, QMacMime::MIME_CLIP)) ,m_find(new QMacPasteboard(kPasteboardFind, QMacMime::HandlerScope::Clipboard))
{ {
connect(qGuiApp, &QGuiApplication::applicationStateChanged, this, &QCocoaClipboard::handleApplicationStateChanged); connect(qGuiApp, &QGuiApplication::applicationStateChanged, this, &QCocoaClipboard::handleApplicationStateChanged);
} }

View File

@ -98,7 +98,7 @@ Qt::DropAction QCocoaDrag::drag(QDrag *o)
m_drag = o; m_drag = o;
m_executed_drop_action = Qt::IgnoreAction; 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")); m_drag->mimeData()->setData("application/x-qt-mime-type-name"_L1, QByteArray("dummy"));
dragBoard.setMimeData(m_drag->mimeData(), QMacPasteboard::LazyRequest); dragBoard.setMimeData(m_drag->mimeData(), QMacPasteboard::LazyRequest);
@ -305,7 +305,7 @@ QStringList QCocoaDropData::formats_sys() const
qDebug("DnD: Cannot get PasteBoard!"); qDebug("DnD: Cannot get PasteBoard!");
return formats; return formats;
} }
formats = QMacPasteboard(board, QMacMime::MIME_DND).formats(); formats = QMacPasteboard(board, QMacMime::HandlerScope::DnD).formats();
return formats; return formats;
} }
@ -317,7 +317,7 @@ QVariant QCocoaDropData::retrieveData_sys(const QString &mimeType, QMetaType typ
qDebug("DnD: Cannot get PasteBoard!"); qDebug("DnD: Cannot get PasteBoard!");
return data; return data;
} }
data = QMacPasteboard(board, QMacMime::MIME_DND).retrieveData(mimeType, type); data = QMacPasteboard(board, QMacMime::HandlerScope::DnD).retrieveData(mimeType, type);
CFRelease(board); CFRelease(board);
return data; return data;
} }
@ -330,7 +330,7 @@ bool QCocoaDropData::hasFormat_sys(const QString &mimeType) const
qDebug("DnD: Cannot get PasteBoard!"); qDebug("DnD: Cannot get PasteBoard!");
return has; return has;
} }
has = QMacPasteboard(board, QMacMime::MIME_DND).hasFormat(mimeType); has = QMacPasteboard(board, QMacMime::HandlerScope::DnD).hasFormat(mimeType);
CFRelease(board); CFRelease(board);
return has; return has;
} }

View File

@ -5,6 +5,7 @@
#define QMACCLIPBOARD_H #define QMACCLIPBOARD_H
#include <QtGui> #include <QtGui>
#include <QtGui/private/qmacmime_p.h>
#include <ApplicationServices/ApplicationServices.h> #include <ApplicationServices/ApplicationServices.h>
@ -41,16 +42,16 @@ private:
QList<Promise> promises; QList<Promise> promises;
PasteboardRef paste; PasteboardRef paste;
uchar mime_type; const QMacMime::HandlerScope scope;
mutable QPointer<QMimeData> mime; mutable QPointer<QMimeData> mime;
mutable bool mac_mime_source; mutable bool mac_mime_source;
bool resolvingBeforeDestruction; bool resolvingBeforeDestruction;
static OSStatus promiseKeeper(PasteboardRef, PasteboardItemID, CFStringRef, void *); static OSStatus promiseKeeper(PasteboardRef, PasteboardItemID, CFStringRef, void *);
void clear_helper(); void clear_helper();
public: public:
QMacPasteboard(PasteboardRef p, uchar mime_type=0); QMacPasteboard(PasteboardRef p, QMacMime::HandlerScope scope = QMacMime::HandlerScope::All);
QMacPasteboard(uchar mime_type); QMacPasteboard(QMacMime::HandlerScope scope);
QMacPasteboard(CFStringRef name=nullptr, uchar mime_type=0); QMacPasteboard(CFStringRef name=nullptr, QMacMime::HandlerScope scope = QMacMime::HandlerScope::All);
~QMacPasteboard(); ~QMacPasteboard();
bool hasFlavor(QString flavor) const; bool hasFlavor(QString flavor) const;

View File

@ -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; mac_mime_source = false;
mime_type = mt ? mt : uchar(QMacMime::MIME_ALL);
paste = p; paste = p;
CFRetain(paste); CFRetain(paste);
resolvingBeforeDestruction = false; resolvingBeforeDestruction = false;
} }
QMacPasteboard::QMacPasteboard(uchar mt) QMacPasteboard::QMacPasteboard(QMacMime::HandlerScope scope)
: scope(scope)
{ {
mac_mime_source = false; mac_mime_source = false;
mime_type = mt ? mt : uchar(QMacMime::MIME_ALL);
paste = nullptr; paste = nullptr;
OSStatus err = PasteboardCreate(nullptr, &paste); OSStatus err = PasteboardCreate(nullptr, &paste);
if (err == noErr) { if (err == noErr) {
@ -92,10 +92,10 @@ QMacPasteboard::QMacPasteboard(uchar mt)
resolvingBeforeDestruction = false; resolvingBeforeDestruction = false;
} }
QMacPasteboard::QMacPasteboard(CFStringRef name, uchar mt) QMacPasteboard::QMacPasteboard(CFStringRef name, QMacMime::HandlerScope scope)
: scope(scope)
{ {
mac_mime_source = false; mac_mime_source = false;
mime_type = mt ? mt : uchar(QMacMime::MIME_ALL);
paste = nullptr; paste = nullptr;
OSStatus err = PasteboardCreate(name, &paste); OSStatus err = PasteboardCreate(name, &paste);
if (err == noErr) { if (err == noErr) {
@ -112,7 +112,7 @@ QMacPasteboard::~QMacPasteboard()
Commit all promises for paste when shutting down, Commit all promises for paste when shutting down,
unless we are the stack-allocated clipboard used by QCocoaDrag. unless we are the stack-allocated clipboard used by QCocoaDrag.
*/ */
if (mime_type == QMacMime::MIME_DND) if (scope == QMacMime::HandlerScope::DnD)
resolvingBeforeDestruction = true; resolvingBeforeDestruction = true;
PasteboardResolvePromises(paste); PasteboardResolvePromises(paste);
if (paste) if (paste)
@ -132,7 +132,7 @@ OSStatus QMacPasteboard::promiseKeeper(PasteboardRef paste, PasteboardItemID id,
// Find the kept promise // Find the kept promise
QList<QMacMime*> availableConverters QList<QMacMime*> availableConverters
= QMacMimeRegistry::all(QMacMime::MIME_ALL); = QMacMimeRegistry::all(QMacMime::HandlerScope::All);
const QString flavorAsQString = QString::fromCFString(flavor); const QString flavorAsQString = QString::fromCFString(flavor);
QMacPasteboard::Promise promise; QMacPasteboard::Promise promise;
for (int i = 0; i < qpaste->promises.size(); i++){ for (int i = 0; i < qpaste->promises.size(); i++){
@ -299,7 +299,7 @@ QMacPasteboard::setMimeData(QMimeData *mime_src, DataRequestType dataRequestType
delete mime; delete mime;
mime = mime_src; mime = mime_src;
const QList<QMacMime*> availableConverters = QMacMimeRegistry::all(mime_type); const QList<QMacMime*> availableConverters = QMacMimeRegistry::all(scope);
if (mime != nullptr) { if (mime != nullptr) {
clear_helper(); clear_helper();
QStringList formats = mime_src->formats(); QStringList formats = mime_src->formats();
@ -373,7 +373,7 @@ QMacPasteboard::formats() const
for (int i = 0; i < type_count; ++i) { for (int i = 0; i < type_count; ++i) {
const QString flavor = QString::fromCFString((CFStringRef)CFArrayGetValueAtIndex(types, i)); const QString flavor = QString::fromCFString((CFStringRef)CFArrayGetValueAtIndex(types, i));
qCDebug(lcQpaClipboard, " -%s", qPrintable(QString(flavor))); 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)) { if (!mimeType.isEmpty() && !ret.contains(mimeType)) {
qCDebug(lcQpaClipboard, " -<%lld> %s [%s]", ret.size(), qPrintable(mimeType), qPrintable(QString(flavor))); qCDebug(lcQpaClipboard, " -<%lld> %s [%s]", ret.size(), qPrintable(mimeType), qPrintable(QString(flavor)));
ret << mimeType; ret << mimeType;
@ -409,8 +409,8 @@ QMacPasteboard::hasFormat(const QString &format) const
const int type_count = CFArrayGetCount(types); const int type_count = CFArrayGetCount(types);
for (int i = 0; i < type_count; ++i) { for (int i = 0; i < type_count; ++i) {
const QString flavor = QString::fromCFString((CFStringRef)CFArrayGetValueAtIndex(types, i)); const QString flavor = QString::fromCFString((CFStringRef)CFArrayGetValueAtIndex(types, i));
qCDebug(lcQpaClipboard, " -%s [0x%x]", qPrintable(QString(flavor)), mime_type); qCDebug(lcQpaClipboard, " -%s [0x%x]", qPrintable(flavor), uchar(scope));
QString mimeType = QMacMimeRegistry::flavorToMime(mime_type, flavor); QString mimeType = QMacMimeRegistry::flavorToMime(scope, flavor);
if (!mimeType.isEmpty()) if (!mimeType.isEmpty())
qCDebug(lcQpaClipboard, " - %s", qPrintable(mimeType)); qCDebug(lcQpaClipboard, " - %s", qPrintable(mimeType));
if (mimeType == format) if (mimeType == format)
@ -433,7 +433,7 @@ QMacPasteboard::retrieveData(const QString &format, QMetaType) const
return QByteArray(); return QByteArray();
qCDebug(lcQpaClipboard, "Pasteboard: retrieveData [%s]", qPrintable(format)); 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) { for (const auto *c : availableConverters) {
QString c_flavor = c->flavorFor(format); QString c_flavor = c->flavorFor(format);
if (!c_flavor.isEmpty()) { if (!c_flavor.isEmpty()) {

View File

@ -117,7 +117,7 @@ QStringList QIOSMimeData::formats() const
for (NSUInteger i = 0; i < [pasteboardTypes count]; ++i) { for (NSUInteger i = 0; i < [pasteboardTypes count]; ++i) {
const QString uti = QString::fromNSString([pasteboardTypes objectAtIndex: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)) if (!mimeType.isEmpty() && !foundMimeTypes.contains(mimeType))
foundMimeTypes << mimeType; foundMimeTypes << mimeType;
} }
@ -130,7 +130,7 @@ QVariant QIOSMimeData::retrieveData(const QString &mimeType, QMetaType) const
UIPasteboard *pb = [UIPasteboard pasteboardWithQClipboardMode:m_mode]; UIPasteboard *pb = [UIPasteboard pasteboardWithQClipboardMode:m_mode];
NSArray<NSString *> *pasteboardTypes = [pb pasteboardTypes]; 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))) if (!converter->canConvert(mimeType, converter->flavorFor(mimeType)))
continue; continue;
@ -184,7 +184,7 @@ void QIOSClipboard::setMimeData(QMimeData *mimeData, QClipboard::Mode mode)
NSMutableDictionary<NSString *, id> *pbItem = [NSMutableDictionary<NSString *, id> dictionaryWithCapacity:mimeData->formats().size()]; NSMutableDictionary<NSString *, id> *pbItem = [NSMutableDictionary<NSString *, id> dictionaryWithCapacity:mimeData->formats().size()];
foreach (const QString &mimeType, mimeData->formats()) { 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); QString uti = converter->flavorFor(mimeType);
if (uti.isEmpty() || !converter->canConvert(mimeType, uti)) if (uti.isEmpty() || !converter->canConvert(mimeType, uti))
continue; continue;