• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
kclipboard.cpp
1/* This file is part of the KDE libraries
2 Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2, as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*/
18
19#include <tdeapplication.h>
20#include <tdeconfig.h>
21#include <tdeglobal.h>
22
23#include "kclipboard.h"
24
25/*
26 * This class provides an automatic synchronization of the X11 Clipboard and Selection
27 * buffers. There are two configuration options in the kdeglobals configuration file,
28 * in the [General] section:
29 * - SynchronizeClipboardAndSelection - whenever the Selection changes, Clipboard is
30 * set to the same value. This can be also enabled in Klipper.
31 * - ClipboardSetSelection - whenever the Clipboard changes, Selection is set
32 * to the same value. This setting is only for die-hard fans of the old broken
33 * KDE1/2 behavior, which can potentionally lead to unexpected problems,
34 * and this setting therefore can be enabled only in the configuration file.
35 *
36 * Whenever reporting any bug only remotely related to clipboard, first make
37 * sure you can reproduce it when both these two options are turned off,
38 * especially the second one.
39 */
40
41class TDEClipboardSynchronizer::MimeSource : public TQMimeSource
42{
43public:
44 MimeSource( const TQMimeSource * src )
45 : TQMimeSource(),
46 m_formats( true ) // deep copies!
47 {
48 m_formats.setAutoDelete( true );
49 m_data.setAutoDelete( true );
50
51 if ( src )
52 {
53 TQByteArray *byteArray;
54 const char *format;
55 int i = 0;
56 while ( (format = src->format( i++ )) )
57 {
58 byteArray = new TQByteArray();
59 *byteArray = src->encodedData( format ).copy();
60 m_data.append( byteArray );
61 m_formats.append( format );
62 }
63 }
64 }
65
66 ~MimeSource() {}
67
68 virtual const char *format( int i ) const {
69 if ( i < (int) m_formats.count() )
70 return m_formats.at( i );
71 else
72 return 0L;
73 }
74 virtual bool provides( const char *mimeType ) const {
75 return ( m_formats.find( mimeType ) > -1 );
76 }
77 virtual TQByteArray encodedData( const char *format ) const
78 {
79 int index = m_formats.find( format );
80 if ( index > -1 )
81 return *(m_data.at( index ));
82
83 return TQByteArray();
84 }
85
86private:
87 mutable TQStrList m_formats;
88 mutable TQPtrList<TQByteArray> m_data;
89};
90
91
92TDEClipboardSynchronizer * TDEClipboardSynchronizer::s_self = 0L;
93bool TDEClipboardSynchronizer::s_sync = false;
94bool TDEClipboardSynchronizer::s_reverse_sync = false;
95bool TDEClipboardSynchronizer::s_blocked = false;
96
97TDEClipboardSynchronizer * TDEClipboardSynchronizer::self()
98{
99 if ( !s_self )
100 s_self = new TDEClipboardSynchronizer( tdeApp, "KDE Clipboard" );
101
102 return s_self;
103}
104
105TDEClipboardSynchronizer::TDEClipboardSynchronizer( TQObject *parent, const char *name )
106 : TQObject( parent, name )
107{
108 s_self = this;
109
110 TDEConfigGroup config( TDEGlobal::config(), "General" );
111 s_sync = config.readBoolEntry( "SynchronizeClipboardAndSelection", s_sync);
112 s_reverse_sync = config.readBoolEntry( "ClipboardSetSelection",
113 s_reverse_sync );
114
115 setupSignals();
116}
117
118TDEClipboardSynchronizer::~TDEClipboardSynchronizer()
119{
120 if ( s_self == this )
121 s_self = 0L;
122}
123
124void TDEClipboardSynchronizer::setupSignals()
125{
126 TQClipboard *clip = TQApplication::clipboard();
127 disconnect( clip, NULL, this, NULL );
128 if( s_sync )
129 connect( clip, TQ_SIGNAL( selectionChanged() ),
130 TQ_SLOT( slotSelectionChanged() ));
131 if( s_reverse_sync )
132 connect( clip, TQ_SIGNAL( dataChanged() ),
133 TQ_SLOT( slotClipboardChanged() ));
134}
135
136void TDEClipboardSynchronizer::slotSelectionChanged()
137{
138 TQClipboard *clip = TQApplication::clipboard();
139
140// tqDebug("*** sel changed: %i", s_blocked);
141 if ( s_blocked || !clip->ownsSelection() )
142 return;
143
144 setClipboard( new MimeSource( clip->data( TQClipboard::Selection) ),
145 TQClipboard::Clipboard );
146}
147
148void TDEClipboardSynchronizer::slotClipboardChanged()
149{
150 TQClipboard *clip = TQApplication::clipboard();
151
152// tqDebug("*** clip changed : %i (implicit: %i, ownz: clip: %i, selection: %i)", s_blocked, s_implicitSelection, clip->ownsClipboard(), clip->ownsSelection());
153 if ( s_blocked || !clip->ownsClipboard() )
154 return;
155
156 setClipboard( new MimeSource( clip->data( TQClipboard::Clipboard ) ),
157 TQClipboard::Selection );
158}
159
160void TDEClipboardSynchronizer::setClipboard( TQMimeSource *data, TQClipboard::Mode mode )
161{
162// tqDebug("---> setting clipboard: %p", data);
163
164 TQClipboard *clip = TQApplication::clipboard();
165
166 s_blocked = true;
167
168 if ( mode == TQClipboard::Clipboard )
169 {
170 clip->setData( data, TQClipboard::Clipboard );
171 }
172 else if ( mode == TQClipboard::Selection )
173 {
174 clip->setData( data, TQClipboard::Selection );
175 }
176
177 s_blocked = false;
178}
179
180void TDEClipboardSynchronizer::setSynchronizing( bool sync )
181{
182 s_sync = sync;
183 self()->setupSignals();
184}
185
186void TDEClipboardSynchronizer::setReverseSynchronizing( bool enable )
187{
188 s_reverse_sync = enable;
189 self()->setupSignals();
190}
191
192// private, called by TDEApplication
193void TDEClipboardSynchronizer::newConfiguration( int config )
194{
195 s_sync = (config & Synchronize);
196 self()->setupSignals();
197}
198
199#include "kclipboard.moc"
TDEClipboardSynchronizer
This class is only for internal use.
Definition: kclipboard.h:37
TDEClipboardSynchronizer::setReverseSynchronizing
static void setReverseSynchronizing(bool enable)
Configures TDEClipboardSynchronizer to copy the Clipboard buffer to the Selection buffer whenever the...
Definition: kclipboard.cpp:186
TDEClipboardSynchronizer::setSynchronizing
static void setSynchronizing(bool sync)
Configures TDEClipboardSynchronizer to synchronize the Selection to Clipboard whenever it changes.
Definition: kclipboard.cpp:180
TDEClipboardSynchronizer::self
static TDEClipboardSynchronizer * self()
Returns the TDEClipboardSynchronizer singleton object.
Definition: kclipboard.cpp:97
TDEConfigGroup
A TDEConfigBase derived class for one specific group in a TDEConfig object.
Definition: tdeconfigbase.h:2127
TDEGlobal::config
static TDEConfig * config()
Returns the general config object.
Definition: tdeglobal.cpp:65

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.4
This website is maintained by Timothy Pearson.