KClipboard Patch (1) - was: [Patch:] KClipboard / klipper / kpaint bug fixes

Helge Deller deller at gmx.de
Mon Oct 28 22:49:03 GMT 2002


This first patch cleans up kdelibs/kclipboard and adds a function 
KClipboard::provides( const char * mimetype )
which returns true if the clipboard content provides data of the given 
mimetype.

There are also some kdoc/doxygen changes acording to feedback by 
Tim Jansen <tim at tjansen.de>.

Ok to commit ?

Helge
-------------- next part --------------
Index: kclipboard.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kclipboard.cpp,v
retrieving revision 1.33
diff -u -p -r1.33 kclipboard.cpp
--- kclipboard.cpp	2002/10/24 22:23:44	1.33
+++ kclipboard.cpp	2002/10/28 22:40:14
@@ -23,67 +23,10 @@
 #include "kclipboard.h"
 
 
-class KClipboard::MimeSource : public QMimeSource
-{
-public:
-    MimeSource( const QMimeSource * src )
-        : QMimeSource(),
-          m_formats( true ) // deep copies!
-    {
-        m_formats.setAutoDelete( true );
-        m_data.setAutoDelete( true );
-
-        if ( src )
-        {
-            QByteArray *byteArray;
-            const char *format;
-            int i = 0;
-            while ( (format = src->format( i++ )) )
-            {
-                byteArray = new QByteArray();
-                *byteArray = src->encodedData( format ).copy();
-                m_data.append( byteArray );
-                m_formats.append( format );
-            }
-        }
-    }
-
-    ~MimeSource() {}
-
-    virtual const char *format( int i ) const {
-        if ( i < (int) m_formats.count() )
-            return m_formats.at( i );
-        else
-            return 0L;
-    }
-    virtual bool provides( const char *mimeType ) const {
-        return ( m_formats.find( mimeType ) > -1 );
-    }
-    virtual QByteArray encodedData( const char *format ) const
-    {
-        int index = m_formats.find( format );
-        if ( index > -1 )
-        {
-            // grmbl, gcc (2.95.3 at least) doesn't let me call m_data.at(),
-            // due to it being non-const. Even if mutable.
-            QPtrList<QByteArray> *list =
-                const_cast<QPtrList<QByteArray> *>( &m_data );
-            return *(list->at( index ));
-        }
-
-        return QByteArray();
-    }
-
-private:
-    mutable QStrList m_formats;
-    QPtrList<QByteArray> m_data;
-};
-
-
 KClipboard * KClipboard::s_self = 0L;
 bool KClipboard::s_sync = false;
 bool KClipboard::s_implicitSelection = true;
-bool KClipboard::s_blocked = false;
+int KClipboard::s_blocked = 0;
 
 KClipboard * KClipboard::self()
 {
@@ -126,7 +69,7 @@ void KClipboard::slotSelectionChanged()
 
     if ( s_sync )
     {
-        setClipboard( new MimeSource( clip->data( QClipboard::Selection) ),
+        setClipboard( clip->text( QClipboard::Selection ),
                       QClipboard::Clipboard );
     }
 }
@@ -135,35 +78,51 @@ void KClipboard::slotClipboardChanged()
 {
     QClipboard *clip = QApplication::clipboard();
 
-//     qDebug("*** clip changed : %i (implicit: %i, ownz: clip: %i, selection: %i)", s_blocked, s_implicitSelection, clip->ownsClipboard(), clip->ownsSelection());
+//     qDebug("*** clip changed : %i (implicit: %i, ownz: clip: %i, selection: %i)",
+//     s_blocked, s_implicitSelection, clip->ownsClipboard(), clip->ownsSelection());
     if ( s_blocked || !clip->ownsClipboard() )
         return;
 
-    if ( s_implicitSelection || s_sync )
+    /* syncronize selection to clipboard only when it's mimetype is "text*" */
+    if ( (s_implicitSelection || s_sync) && provides("text/") )
     {
-        setClipboard( new MimeSource( clip->data( QClipboard::Clipboard ) ),
+        setClipboard( clip->text( QClipboard::Clipboard ),
                       QClipboard::Selection );
     }
 }
 
-void KClipboard::setClipboard( QMimeSource *data, QClipboard::Mode mode )
+void KClipboard::setClipboard( const QString & text, QClipboard::Mode mode )
 {
 //     qDebug("---> setting clipboard: %p", data);
 
     QClipboard *clip = QApplication::clipboard();
 
-    s_blocked = true;
+    s_blocked++;
+    clip->setText( text, mode );
+    s_blocked--;
+}
 
-    if ( mode == QClipboard::Clipboard )
-    {
-        clip->setData( data, QClipboard::Clipboard );
-    }
-    else if ( mode == QClipboard::Selection )
-    {
-        clip->setData( data, QClipboard::Selection );
-    }
+bool KClipboard::provides( const char *mimetype )
+{
+    QClipboard *clip = QApplication::clipboard();
+    if (!clip || !mimetype)
+	return false;
 
-    s_blocked = false;
+    const QMimeSource *ms = clip->data( QClipboard::Clipboard );
+    if (!ms)
+	return false;
+
+    int i = 0;
+    const char *fmt;
+    do {
+	fmt = ms->format(i);
+	// qDebug("*** klipper: clipboard has format %s (%i)", fmt, i);
+	if (!fmt)
+		return false;
+	if (!strncmp(fmt, mimetype, strlen(mimetype)))
+		return true;
+	i++;
+    } while (1);
 }
 
 // private, called by KApplication
Index: kclipboard.h
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kclipboard.h,v
retrieving revision 1.20
diff -u -p -r1.20 kclipboard.h
--- kclipboard.h	2002/10/24 22:23:44	1.20
+++ kclipboard.h	2002/10/28 22:40:14
@@ -94,9 +94,9 @@ public:
     }
 
     /**
-     * Checks whether the  Clipboard buffer will be copied to the Selection
+     * Checks whether the Clipboard buffer will be copied to the Selection
      * buffer upon changes.
-     * @returns whether the Clipboard buffer will be copied to the Selection
+     * @return whether the Clipboard buffer will be copied to the Selection
      * buffer upon changes.
      * @see #setImplicitSelection
      */
@@ -105,6 +105,14 @@ public:
         return s_implicitSelection;
     }
 
+    /**
+     * Checks whether the Clipboard buffer provides contents of a given mimetype.
+     * @param mimetype string (e.g. "text/plain", "text/" or "image/")
+     * @return whether the Clipboard buffer provides this mimetype content.
+     * @since 3.1
+     */
+    static bool provides( const char *mimetype );
+
 protected:
     ~KClipboard();
 
@@ -115,15 +123,12 @@ private slots:
 private:
     KClipboard( QObject *parent = 0, const char *name = 0L );
 
-    // does not restore the old selection mode.
-    static void setClipboard( QMimeSource* data, QClipboard::Mode mode );
+    static void setClipboard( const QString & text, QClipboard::Mode mode );
 
     static KClipboard *s_self;
     static bool s_sync;
     static bool s_implicitSelection;
-    static bool s_blocked;
-
-    class MimeSource;
+    static int s_blocked;
 
 private:
     // needed by klipper


More information about the kde-core-devel mailing list