You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
200 lines
6.0 KiB
200 lines
6.0 KiB
15 years ago
|
/* This file is part of the KDE libraries
|
||
|
Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License version 2, as published by the Free Software Foundation.
|
||
|
|
||
|
This library is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
|
#include <kapplication.h>
|
||
|
#include <kconfig.h>
|
||
|
#include <kglobal.h>
|
||
|
|
||
|
#include "kclipboard.h"
|
||
|
|
||
|
/*
|
||
|
* This class provides an automatic synchronization of the X11 Clipboard and Selection
|
||
|
* buffers. There are two configuration options in the kdeglobals configuration file,
|
||
|
* in the [General] section:
|
||
|
* - SynchronizeClipboardAndSelection - whenever the Selection changes, Clipboard is
|
||
|
* set to the same value. This can be also enabled in Klipper.
|
||
|
* - ClipboardSetSelection - whenever the Clipboard changes, Selection is set
|
||
|
* to the same value. This setting is only for die-hard fans of the old broken
|
||
|
* KDE1/2 behavior, which can potentionally lead to unexpected problems,
|
||
|
* and this setting therefore can be enabled only in the configuration file.
|
||
|
*
|
||
|
* Whenever reporting any bug only remotely related to clipboard, first make
|
||
|
* sure you can reproduce it when both these two options are turned off,
|
||
|
* especially the second one.
|
||
|
*/
|
||
|
|
||
14 years ago
|
class KClipboardSynchronizer::MimeSource : public TQMimeSource
|
||
15 years ago
|
{
|
||
|
public:
|
||
15 years ago
|
MimeSource( const TQMimeSource * src )
|
||
|
: TQMimeSource(),
|
||
15 years ago
|
m_formats( true ) // deep copies!
|
||
|
{
|
||
|
m_formats.setAutoDelete( true );
|
||
|
m_data.setAutoDelete( true );
|
||
|
|
||
|
if ( src )
|
||
|
{
|
||
15 years ago
|
TQByteArray *byteArray;
|
||
15 years ago
|
const char *format;
|
||
|
int i = 0;
|
||
|
while ( (format = src->format( i++ )) )
|
||
|
{
|
||
15 years ago
|
byteArray = new TQByteArray();
|
||
14 years ago
|
*byteArray = src->tqencodedData( format ).copy();
|
||
15 years ago
|
m_data.append( byteArray );
|
||
|
m_formats.append( format );
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
~MimeSource() {}
|
||
|
|
||
|
virtual const char *format( int i ) const {
|
||
|
if ( i < (int) m_formats.count() )
|
||
14 years ago
|
return m_formats.tqat( i );
|
||
15 years ago
|
else
|
||
|
return 0L;
|
||
|
}
|
||
|
virtual bool provides( const char *mimeType ) const {
|
||
13 years ago
|
return ( m_formats.find( mimeType ) > -1 );
|
||
15 years ago
|
}
|
||
14 years ago
|
virtual TQByteArray tqencodedData( const char *format ) const
|
||
15 years ago
|
{
|
||
13 years ago
|
int index = m_formats.find( format );
|
||
15 years ago
|
if ( index > -1 )
|
||
14 years ago
|
return *(m_data.tqat( index ));
|
||
15 years ago
|
|
||
15 years ago
|
return TQByteArray();
|
||
15 years ago
|
}
|
||
|
|
||
|
private:
|
||
15 years ago
|
mutable TQStrList m_formats;
|
||
|
mutable TQPtrList<TQByteArray> m_data;
|
||
15 years ago
|
};
|
||
|
|
||
|
|
||
|
KClipboardSynchronizer * KClipboardSynchronizer::s_self = 0L;
|
||
|
bool KClipboardSynchronizer::s_sync = false;
|
||
|
bool KClipboardSynchronizer::s_reverse_sync = false;
|
||
|
bool KClipboardSynchronizer::s_blocked = false;
|
||
|
|
||
|
KClipboardSynchronizer * KClipboardSynchronizer::self()
|
||
|
{
|
||
|
if ( !s_self )
|
||
14 years ago
|
s_self = new KClipboardSynchronizer( TQT_TQOBJECT(kapp), "KDE Clipboard" );
|
||
15 years ago
|
|
||
|
return s_self;
|
||
|
}
|
||
|
|
||
15 years ago
|
KClipboardSynchronizer::KClipboardSynchronizer( TQObject *parent, const char *name )
|
||
|
: TQObject( parent, name )
|
||
15 years ago
|
{
|
||
|
s_self = this;
|
||
|
|
||
|
KConfigGroup config( KGlobal::config(), "General" );
|
||
|
s_sync = config.readBoolEntry( "SynchronizeClipboardAndSelection", s_sync);
|
||
|
s_reverse_sync = config.readBoolEntry( "ClipboardSetSelection",
|
||
|
s_reverse_sync );
|
||
|
|
||
|
setupSignals();
|
||
|
}
|
||
|
|
||
|
KClipboardSynchronizer::~KClipboardSynchronizer()
|
||
|
{
|
||
|
if ( s_self == this )
|
||
|
s_self = 0L;
|
||
|
}
|
||
|
|
||
|
void KClipboardSynchronizer::setupSignals()
|
||
|
{
|
||
14 years ago
|
TQClipboard *clip = TQApplication::tqclipboard();
|
||
15 years ago
|
disconnect( clip, NULL, this, NULL );
|
||
|
if( s_sync )
|
||
15 years ago
|
connect( clip, TQT_SIGNAL( selectionChanged() ),
|
||
|
TQT_SLOT( slotSelectionChanged() ));
|
||
15 years ago
|
if( s_reverse_sync )
|
||
15 years ago
|
connect( clip, TQT_SIGNAL( dataChanged() ),
|
||
|
TQT_SLOT( slotClipboardChanged() ));
|
||
15 years ago
|
}
|
||
|
|
||
|
void KClipboardSynchronizer::slotSelectionChanged()
|
||
|
{
|
||
14 years ago
|
TQClipboard *clip = TQApplication::tqclipboard();
|
||
15 years ago
|
|
||
|
// qDebug("*** sel changed: %i", s_blocked);
|
||
|
if ( s_blocked || !clip->ownsSelection() )
|
||
|
return;
|
||
|
|
||
14 years ago
|
setClipboard( new MimeSource( clip->data( TQClipboard::Selection) ),
|
||
|
TQClipboard::Clipboard );
|
||
15 years ago
|
}
|
||
|
|
||
|
void KClipboardSynchronizer::slotClipboardChanged()
|
||
|
{
|
||
14 years ago
|
TQClipboard *clip = TQApplication::tqclipboard();
|
||
15 years ago
|
|
||
|
// 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;
|
||
|
|
||
14 years ago
|
setClipboard( new MimeSource( clip->data( TQClipboard::Clipboard ) ),
|
||
|
TQClipboard::Selection );
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void KClipboardSynchronizer::setClipboard( TQMimeSource *data, TQClipboard::Mode mode )
|
||
15 years ago
|
{
|
||
|
// qDebug("---> setting clipboard: %p", data);
|
||
|
|
||
14 years ago
|
TQClipboard *clip = TQApplication::tqclipboard();
|
||
15 years ago
|
|
||
|
s_blocked = true;
|
||
|
|
||
14 years ago
|
if ( mode == TQClipboard::Clipboard )
|
||
15 years ago
|
{
|
||
14 years ago
|
clip->setData( data, TQClipboard::Clipboard );
|
||
15 years ago
|
}
|
||
14 years ago
|
else if ( mode == TQClipboard::Selection )
|
||
15 years ago
|
{
|
||
14 years ago
|
clip->setData( data, TQClipboard::Selection );
|
||
15 years ago
|
}
|
||
|
|
||
|
s_blocked = false;
|
||
|
}
|
||
|
|
||
|
void KClipboardSynchronizer::setSynchronizing( bool sync )
|
||
|
{
|
||
|
s_sync = sync;
|
||
|
self()->setupSignals();
|
||
|
}
|
||
|
|
||
|
void KClipboardSynchronizer::setReverseSynchronizing( bool enable )
|
||
|
{
|
||
|
s_reverse_sync = enable;
|
||
|
self()->setupSignals();
|
||
|
}
|
||
|
|
||
|
// private, called by KApplication
|
||
|
void KClipboardSynchronizer::newConfiguration( int config )
|
||
|
{
|
||
|
s_sync = (config & Synchronize);
|
||
|
self()->setupSignals();
|
||
|
}
|
||
|
|
||
|
#include "kclipboard.moc"
|