Implement cocoa clipboard support.
authorMorten Johan Sorvig <morten.sorvig@nokia.com>
Mon, 12 Mar 2012 11:07:51 +0000 (12:07 +0100)
committerQt by Nokia <qt-info@nokia.com>
Thu, 15 Mar 2012 10:40:50 +0000 (11:40 +0100)
Add QCoccoaClipboard which wraps the existing
QMacPasteboard implementation. Remove unused
QClipboard integration code from qmacclipboard.mm

Change mime type cleanup from using qAddPostRoutine
to using an explicit call to destroyMimieTypes in
the cocoa platform integration destructor. This is
necessary to ensure cleanup happens in the correct
order on app shutdown.

Change-Id: Ief0e0d996b04c8e84e9fd2cd3a17fb5bd73bb761
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
src/plugins/platforms/cocoa/cocoa.pro
src/plugins/platforms/cocoa/qcocoaclipboard.h [new file with mode: 0644]
src/plugins/platforms/cocoa/qcocoaclipboard.mm [new file with mode: 0644]
src/plugins/platforms/cocoa/qcocoaintegration.h
src/plugins/platforms/cocoa/qcocoaintegration.mm
src/plugins/platforms/cocoa/qmacclipboard.mm
src/plugins/platforms/cocoa/qmacmime.h
src/plugins/platforms/cocoa/qmacmime.mm

index c429325..e861f48 100644 (file)
@@ -27,6 +27,7 @@ OBJECTIVE_SOURCES += main.mm \
     qcocoafiledialoghelper.mm \
     qcocoafontdialoghelper.mm \
     qcocoacursor.mm \
+    qcocoaclipboard.mm \
     qcocoadrag.mm \
     qmacclipboard.mm \
     qmacmime.mm \
@@ -59,6 +60,7 @@ HEADERS += qcocoaintegration.h \
     qcocoafiledialoghelper.h \
     qcocoafontdialoghelper.h \
     qcocoacursor.h \
+    qcocoaclipboard.h \
     qcocoadrag.h \
     qmacclipboard.h \
     qmacmime.h \
diff --git a/src/plugins/platforms/cocoa/qcocoaclipboard.h b/src/plugins/platforms/cocoa/qcocoaclipboard.h
new file mode 100644 (file)
index 0000000..2750505
--- /dev/null
@@ -0,0 +1,70 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the plugins of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QCOCOACLIPBOARD_H
+#define QCOCOACLIPBOARD_H
+
+#include <qplatformclipboard_qpa.h>
+#include "qmacclipboard.h"
+#include <QtCore/QScopedPointer>
+
+QT_BEGIN_NAMESPACE
+
+class QCocoaClipboard : public QPlatformClipboard
+{
+public:
+    QCocoaClipboard();
+
+    QMimeData *mimeData(QClipboard::Mode mode = QClipboard::Clipboard);
+    void setMimeData(QMimeData *data, QClipboard::Mode mode = QClipboard::Clipboard);
+    bool supportsMode(QClipboard::Mode mode) const;
+    bool ownsMode(QClipboard::Mode mode) const;
+protected:
+    QMacPasteboard *pasteboardForMode(QClipboard::Mode mode) const;
+
+private:
+    QScopedPointer<QMacPasteboard> m_clipboard;
+    QScopedPointer<QMacPasteboard> m_find;
+};
+
+QT_END_NAMESPACE
+
+#endif
diff --git a/src/plugins/platforms/cocoa/qcocoaclipboard.mm b/src/plugins/platforms/cocoa/qcocoaclipboard.mm
new file mode 100644 (file)
index 0000000..799fb85
--- /dev/null
@@ -0,0 +1,93 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the plugins of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "QCocoaclipboard.h"
+#include "qmacmime.h"
+#include "qmacclipboard.h"
+
+QT_BEGIN_NAMESPACE
+
+QCocoaClipboard::QCocoaClipboard()
+    :m_clipboard(new QMacPasteboard(kPasteboardClipboard, QMacPasteboardMime::MIME_CLIP))
+    ,m_find(new QMacPasteboard(kPasteboardFind, QMacPasteboardMime::MIME_CLIP))
+{
+
+}
+
+QMimeData *QCocoaClipboard::mimeData(QClipboard::Mode mode)
+{
+    if (QMacPasteboard *pasteBoard = pasteboardForMode(mode)) {
+        pasteBoard->sync();
+        return pasteBoard->mimeData();
+    }
+    return 0;
+}
+
+void QCocoaClipboard::setMimeData(QMimeData *data, QClipboard::Mode mode)
+{
+    if (QMacPasteboard *pasteBoard = pasteboardForMode(mode)) {
+        pasteBoard->sync();
+        pasteBoard->setMimeData(data);
+        emitChanged(mode);
+    }
+}
+
+bool QCocoaClipboard::supportsMode(QClipboard::Mode mode) const
+{
+    return (mode == QClipboard::Clipboard || mode == QClipboard::FindBuffer);
+}
+
+bool QCocoaClipboard::ownsMode(QClipboard::Mode mode) const
+{
+    return false;
+}
+
+QMacPasteboard *QCocoaClipboard::pasteboardForMode(QClipboard::Mode mode) const
+{
+    if (mode == QClipboard::Clipboard)
+        return m_clipboard.data();
+    else if (mode == QClipboard::FindBuffer)
+        return m_find.data();
+    else
+        return 0;
+}
+
+QT_END_NAMESPACE
index 9248df0..97e7a7f 100644 (file)
@@ -46,6 +46,7 @@
 
 #include "qcocoaautoreleasepool.h"
 #include "qcocoacursor.h"
+#include "qcocoaclipboard.h"
 #include "qcocoadrag.h"
 
 #include <QtCore/QScopedPointer>
@@ -91,6 +92,7 @@ public:
     QPlatformNativeInterface *nativeInterface() const;
     QPlatformInputContext *inputContext() const;
     QPlatformAccessibility *accessibility() const;
+    QPlatformClipboard *clipboard() const;
     QPlatformDrag *drag() const;
 
     QStringList themeNames() const;
@@ -105,6 +107,7 @@ private:
     QScopedPointer<QPlatformAccessibility> mAccessibility;
     QScopedPointer<QPlatformTheme> mPlatformTheme;
     QList<QCocoaScreen *> mScreens;
+    QCocoaClipboard  *mCocoaClipboard;
     QScopedPointer<QCocoaDrag> mCocoaDrag;
 };
 
index 96027e0..f5febd4 100644 (file)
@@ -93,6 +93,7 @@ QCocoaIntegration::QCocoaIntegration()
     , mEventDispatcher(new QCocoaEventDispatcher())
     , mInputContext(new QCocoaInputContext)
     , mAccessibility(new QPlatformAccessibility)
+    , mCocoaClipboard(new QCocoaClipboard)
     , mCocoaDrag(new QCocoaDrag)
 {
     QCocoaAutoReleasePool pool;
@@ -140,13 +141,19 @@ QCocoaIntegration::QCocoaIntegration()
         screenAdded(screen);
     }
 
-    QMacPasteboardMime::initialize();
+    QMacPasteboardMime::initializeMimeTypes();
 }
 
 QCocoaIntegration::~QCocoaIntegration()
 {
     [[NSApplication sharedApplication] setDelegate: 0];
 
+    // Delete the clipboard integration and destroy mime type converters.
+    // Deleting the clipboard integration flushes promised pastes using
+    // the mime converters - the ordering here is important.
+    delete mCocoaClipboard;
+    QMacPasteboardMime::destroyMimeTypes();
+
     // Delete screens in reverse order to avoid crash in case of multiple screens
     while (!mScreens.isEmpty()) {
         delete mScreens.takeLast();
@@ -206,6 +213,11 @@ QPlatformAccessibility *QCocoaIntegration::accessibility() const
     return mAccessibility.data();
 }
 
+QPlatformClipboard *QCocoaIntegration::clipboard() const
+{
+    return mCocoaClipboard;
+}
+
 QPlatformDrag *QCocoaIntegration::drag() const
 {
     return mCocoaDrag.data();
index d5af6de..eff2c5b 100644 (file)
@@ -63,106 +63,6 @@ QT_USE_NAMESPACE
  *****************************************************************************/
 //#define DEBUG_PASTEBOARD
 
-#ifndef QT_NO_CLIPBOARD
-
-/*****************************************************************************
-  QClipboard member functions for mac.
- *****************************************************************************/
-
-static QMacPasteboard *qt_mac_pasteboards[2] = {0, 0};
-
-static inline QMacPasteboard *qt_mac_pasteboard(QClipboard::Mode mode)
-{
-    Q_ASSERT(mode == QClipboard::Clipboard || mode == QClipboard::FindBuffer);
-    if (mode == QClipboard::Clipboard)
-        return qt_mac_pasteboards[0];
-    else
-        return qt_mac_pasteboards[1];
-}
-
-static void qt_mac_cleanupPasteboard() {
-    delete qt_mac_pasteboards[0];
-    delete qt_mac_pasteboards[1];
-    qt_mac_pasteboards[0] = 0;
-    qt_mac_pasteboards[1] = 0;
-}
-
-static bool qt_mac_updateScrap(QClipboard::Mode mode)
-{
-    if (!qt_mac_pasteboards[0]) {
-        qt_mac_pasteboards[0] = new QMacPasteboard(kPasteboardClipboard, QMacPasteboardMime::MIME_CLIP);
-        qt_mac_pasteboards[1] = new QMacPasteboard(kPasteboardFind, QMacPasteboardMime::MIME_CLIP);
-        qAddPostRoutine(qt_mac_cleanupPasteboard);
-        return true;
-    }
-    return qt_mac_pasteboard(mode)->sync();
-}
-
-void QClipboard::clear(Mode mode)
-{
-    if (!supportsMode(mode))
-        return;
-    qt_mac_updateScrap(mode);
-    qt_mac_pasteboard(mode)->clear();
-    setMimeData(0, mode);
-}
-
-void QClipboard::ownerDestroyed()
-{
-}
-
-
-void QClipboard::connectNotify(const char *signal)
-{
-    Q_UNUSED(signal);
-}
-
-bool QClipboard::event(QEvent *e)
-{
-    if (e->type() != QEvent::Clipboard)
-        return QObject::event(e);
-
-    if (qt_mac_updateScrap(QClipboard::Clipboard)) {
-        emitChanged(QClipboard::Clipboard);
-    }
-
-    if (qt_mac_updateScrap(QClipboard::FindBuffer)) {
-        emitChanged(QClipboard::FindBuffer);
-    }
-
-    return QObject::event(e);
-}
-
-const QMimeData *QClipboard::mimeData(Mode mode) const
-{
-    if (!supportsMode(mode))
-        return 0;
-    qt_mac_updateScrap(mode);
-    return qt_mac_pasteboard(mode)->mimeData();
-}
-
-void QClipboard::setMimeData(QMimeData *src, Mode mode)
-{
-    if (!supportsMode(mode))
-        return;
-    qt_mac_updateScrap(mode);
-    qt_mac_pasteboard(mode)->setMimeData(src);
-    emitChanged(mode);
-}
-
-bool QClipboard::supportsMode(Mode mode) const
-{
-    return (mode == Clipboard || mode == FindBuffer);
-}
-
-bool QClipboard::ownsMode(Mode mode) const
-{
-    Q_UNUSED(mode);
-    return false;
-}
-
-#endif // QT_NO_CLIPBOARD
-
 /*****************************************************************************
    QMacPasteboard code
 *****************************************************************************/
index 842caa5..7226cae 100644 (file)
@@ -58,7 +58,8 @@ public:
     explicit QMacPasteboardMime(char);
     virtual ~QMacPasteboardMime();
 
-    static void initialize();
+    static void initializeMimeTypes();
+    static void destroyMimeTypes();
 
     static QList<QMacPasteboardMime*> all(uchar);
     static QMacPasteboardMime *convertor(uchar, const QString &mime, QString flav);
index db86deb..8cb684f 100644 (file)
@@ -64,14 +64,6 @@ extern CGImageRef qt_mac_createCGImageFromQImage(const QImage &img, const QImage
 
 typedef QList<QMacPasteboardMime*> MimeList;
 Q_GLOBAL_STATIC(MimeList, globalMimeList)
-
-static void cleanup_mimes()
-{
-    MimeList *mimes = globalMimeList();
-    while (!mimes->isEmpty())
-        delete mimes->takeFirst();
-}
-
 Q_GLOBAL_STATIC(QStringList, globalDraggedTypesList)
 
 /*!
@@ -791,11 +783,9 @@ QList<QByteArray> QMacPasteboardMimeVCard::convertFromMime(const QString &mime,
 
   This is an internal function.
 */
-void QMacPasteboardMime::initialize()
+void QMacPasteboardMime::initializeMimeTypes()
 {
     if (globalMimeList()->isEmpty()) {
-        qAddPostRoutine(cleanup_mimes);
-
         //standard types that we wrap
         new QMacPasteboardMimeTiff;
         new QMacPasteboardMimeUnicodeText;
@@ -811,6 +801,16 @@ void QMacPasteboardMime::initialize()
 }
 
 /*!
+  \internal
+*/
+void QMacPasteboardMime::destroyMimeTypes()
+{
+    MimeList *mimes = globalMimeList();
+    while (!mimes->isEmpty())
+        delete mimes->takeFirst();
+}
+
+/*!
   Returns the most-recently created QMacPasteboardMime of type \a t that can convert
   between the \a mime and \a flav formats.  Returns 0 if no such convertor
   exists.