KDECore
kclipboard.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <kapplication.h>
00020 #include <kconfig.h>
00021 #include <kglobal.h>
00022
00023 #include "kclipboard.h"
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 class KClipboardSynchronizer::MimeSource : public QMimeSource
00042 {
00043 public:
00044 MimeSource( const QMimeSource * src )
00045 : QMimeSource(),
00046 m_formats( true )
00047 {
00048 m_formats.setAutoDelete( true );
00049 m_data.setAutoDelete( true );
00050
00051 if ( src )
00052 {
00053 QByteArray *byteArray;
00054 const char *format;
00055 int i = 0;
00056 while ( (format = src->format( i++ )) )
00057 {
00058 byteArray = new QByteArray();
00059 *byteArray = src->encodedData( format ).copy();
00060 m_data.append( byteArray );
00061 m_formats.append( format );
00062 }
00063 }
00064 }
00065
00066 ~MimeSource() {}
00067
00068 virtual const char *format( int i ) const {
00069 if ( i < (int) m_formats.count() )
00070 return m_formats.at( i );
00071 else
00072 return 0L;
00073 }
00074 virtual bool provides( const char *mimeType ) const {
00075 return ( m_formats.find( mimeType ) > -1 );
00076 }
00077 virtual QByteArray encodedData( const char *format ) const
00078 {
00079 int index = m_formats.find( format );
00080 if ( index > -1 )
00081 return *(m_data.at( index ));
00082
00083 return QByteArray();
00084 }
00085
00086 private:
00087 mutable QStrList m_formats;
00088 mutable QPtrList<QByteArray> m_data;
00089 };
00090
00091
00092 KClipboardSynchronizer * KClipboardSynchronizer::s_self = 0L;
00093 bool KClipboardSynchronizer::s_sync = false;
00094 bool KClipboardSynchronizer::s_reverse_sync = false;
00095 bool KClipboardSynchronizer::s_blocked = false;
00096
00097 KClipboardSynchronizer * KClipboardSynchronizer::self()
00098 {
00099 if ( !s_self )
00100 s_self = new KClipboardSynchronizer( kapp, "KDE Clipboard" );
00101
00102 return s_self;
00103 }
00104
00105 KClipboardSynchronizer::KClipboardSynchronizer( QObject *parent, const char *name )
00106 : QObject( parent, name )
00107 {
00108 s_self = this;
00109
00110 KConfigGroup config( KGlobal::config(), "General" );
00111 s_sync = config.readBoolEntry( "SynchronizeClipboardAndSelection", s_sync);
00112 s_reverse_sync = config.readBoolEntry( "ClipboardSetSelection",
00113 s_reverse_sync );
00114
00115 setupSignals();
00116 }
00117
00118 KClipboardSynchronizer::~KClipboardSynchronizer()
00119 {
00120 if ( s_self == this )
00121 s_self = 0L;
00122 }
00123
00124 void KClipboardSynchronizer::setupSignals()
00125 {
00126 QClipboard *clip = QApplication::clipboard();
00127 disconnect( clip, NULL, this, NULL );
00128 if( s_sync )
00129 connect( clip, SIGNAL( selectionChanged() ),
00130 SLOT( slotSelectionChanged() ));
00131 if( s_reverse_sync )
00132 connect( clip, SIGNAL( dataChanged() ),
00133 SLOT( slotClipboardChanged() ));
00134 }
00135
00136 void KClipboardSynchronizer::slotSelectionChanged()
00137 {
00138 QClipboard *clip = QApplication::clipboard();
00139
00140
00141 if ( s_blocked || !clip->ownsSelection() )
00142 return;
00143
00144 setClipboard( new MimeSource( clip->data( QClipboard::Selection) ),
00145 QClipboard::Clipboard );
00146 }
00147
00148 void KClipboardSynchronizer::slotClipboardChanged()
00149 {
00150 QClipboard *clip = QApplication::clipboard();
00151
00152
00153 if ( s_blocked || !clip->ownsClipboard() )
00154 return;
00155
00156 setClipboard( new MimeSource( clip->data( QClipboard::Clipboard ) ),
00157 QClipboard::Selection );
00158 }
00159
00160 void KClipboardSynchronizer::setClipboard( QMimeSource *data, QClipboard::Mode mode )
00161 {
00162
00163
00164 QClipboard *clip = QApplication::clipboard();
00165
00166 s_blocked = true;
00167
00168 if ( mode == QClipboard::Clipboard )
00169 {
00170 clip->setData( data, QClipboard::Clipboard );
00171 }
00172 else if ( mode == QClipboard::Selection )
00173 {
00174 clip->setData( data, QClipboard::Selection );
00175 }
00176
00177 s_blocked = false;
00178 }
00179
00180 void KClipboardSynchronizer::setSynchronizing( bool sync )
00181 {
00182 s_sync = sync;
00183 self()->setupSignals();
00184 }
00185
00186 void KClipboardSynchronizer::setReverseSynchronizing( bool enable )
00187 {
00188 s_reverse_sync = enable;
00189 self()->setupSignals();
00190 }
00191
00192
00193 void KClipboardSynchronizer::newConfiguration( int config )
00194 {
00195 s_sync = (config & Synchronize);
00196 self()->setupSignals();
00197 }
00198
00199 #include "kclipboard.moc"