• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • kernel
kclipboard.cpp
Go to the documentation of this file.
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 "kclipboard.h"
20#include "ksharedconfig.h"
21#include "kglobal.h"
22#include "kglobalsettings.h"
23
24#include <QtCore/QMimeData>
25#include <QtDBus/QtDBus>
26#include <QtGui/QApplication>
27#include <kconfiggroup.h>
28
29/*
30 * This class provides an automatic synchronization of the X11 Clipboard and Selection
31 * buffers. There are two configuration options in the kdeglobals configuration file,
32 * in the [General] section:
33 * - SynchronizeClipboardAndSelection - whenever the Selection changes, Clipboard is
34 * set to the same value. This can be also enabled in Klipper.
35 * - ClipboardSetSelection - whenever the Clipboard changes, Selection is set
36 * to the same value. This setting is only for die-hard fans of the old broken
37 * KDE1/2 behavior, which can potentionally lead to unexpected problems,
38 * and this setting therefore can be enabled only in the configuration file.
39 *
40 * Whenever reporting any bug only remotely related to clipboard, first make
41 * sure you can reproduce it when both these two options are turned off,
42 * especially the second one.
43 */
44
45class KClipboardSynchronizer::Private
46{
47 public:
48 Private(KClipboardSynchronizer *q)
49 : q(q)
50 {
51 }
52
53 void setupSignals();
54
55 static void setClipboard( const QMimeData* data, QClipboard::Mode mode );
56
57 void _k_slotSelectionChanged();
58 void _k_slotClipboardChanged();
59 void _k_slotNotifyChange(int, int);
60
61 KClipboardSynchronizer *q;
62 static bool s_sync;
63 static bool s_reverse_sync;
64 static bool s_blocked;
65};
66
67bool KClipboardSynchronizer::Private::s_sync = false;
68bool KClipboardSynchronizer::Private::s_reverse_sync = false;
69bool KClipboardSynchronizer::Private::s_blocked = false;
70
71KClipboardSynchronizer * KClipboardSynchronizer::self()
72{
73 K_GLOBAL_STATIC(KClipboardSynchronizer, s_self)
74 return s_self;
75}
76
77KClipboardSynchronizer::KClipboardSynchronizer( QObject *parent )
78 : QObject( parent ), d(new Private(this))
79{
80 KConfigGroup config( KGlobal::config(), "General" );
81 Private::s_sync = config.readEntry("SynchronizeClipboardAndSelection", Private::s_sync);
82 Private::s_reverse_sync = config.readEntry("ClipboardSetSelection", Private::s_reverse_sync);
83
84 d->setupSignals();
85}
86
87KClipboardSynchronizer::~KClipboardSynchronizer()
88{
89 delete d;
90}
91
92void KClipboardSynchronizer::Private::setupSignals()
93{
94 QClipboard *clip = QApplication::clipboard();
95 disconnect( clip, NULL, q, NULL );
96 if( s_sync )
97 connect( clip, SIGNAL(selectionChanged()),
98 q, SLOT(_k_slotSelectionChanged()));
99 if( s_reverse_sync )
100 connect( clip, SIGNAL(dataChanged()),
101 q, SLOT(_k_slotClipboardChanged()));
102
103 QDBusConnection::sessionBus().connect( QString(), "/KGlobalSettings", "org.kde.KGlobalSettings",
104 "notifyChange", q, SLOT(_k_slotNotifyChange(int,int)) );
105}
106
107void KClipboardSynchronizer::Private::_k_slotSelectionChanged()
108{
109 QClipboard *clip = QApplication::clipboard();
110
111// qDebug("*** sel changed: %i", s_blocked);
112 if ( s_blocked || !clip->ownsSelection() )
113 return;
114
115 setClipboard( clip->mimeData( QClipboard::Selection ),
116 QClipboard::Clipboard );
117}
118
119void KClipboardSynchronizer::Private::_k_slotClipboardChanged()
120{
121 QClipboard *clip = QApplication::clipboard();
122
123// qDebug("*** clip changed : %i (implicit: %i, ownz: clip: %i, selection: %i)", s_blocked, s_implicitSelection, clip->ownsClipboard(), clip->ownsSelection());
124 if ( s_blocked || !clip->ownsClipboard() )
125 return;
126
127 setClipboard( clip->mimeData( QClipboard::Clipboard ),
128 QClipboard::Selection );
129}
130
131void KClipboardSynchronizer::Private::_k_slotNotifyChange(int changeType, int arg)
132{
133 if (changeType == KGlobalSettings::ClipboardConfigChanged) {
134 s_sync = (arg & Synchronize);
135 KClipboardSynchronizer::self()->d->setupSignals();
136 }
137}
138
139void KClipboardSynchronizer::Private::setClipboard( const QMimeData *data, QClipboard::Mode mode )
140{
141// qDebug("---> setting clipboard: %p", data);
142
143 QClipboard *clip = QApplication::clipboard();
144
145 s_blocked = true;
146
147 QMimeData* clipData = const_cast<QMimeData*>( data );
148 if ( mode == QClipboard::Clipboard )
149 {
150 clip->setMimeData( clipData, QClipboard::Clipboard );
151 }
152 else if ( mode == QClipboard::Selection )
153 {
154 clip->setMimeData( clipData, QClipboard::Selection );
155 }
156
157 s_blocked = false;
158}
159
160void KClipboardSynchronizer::setSynchronizing( bool sync )
161{
162 Private::s_sync = sync;
163 self()->d->setupSignals();
164}
165
166bool KClipboardSynchronizer::isSynchronizing()
167{
168 return Private::s_sync;
169}
170
171void KClipboardSynchronizer::setReverseSynchronizing( bool enable )
172{
173 Private::s_reverse_sync = enable;
174 self()->d->setupSignals();
175}
176
177bool KClipboardSynchronizer::isReverseSynchronizing()
178{
179 return Private::s_reverse_sync;
180}
181
182#include "kclipboard.moc"
KClipboardSynchronizer
This class is only for internal use.
Definition: kclipboard.h:36
KClipboardSynchronizer::isSynchronizing
static bool isSynchronizing()
Checks whether Clipboard and Selection will be synchronized upon changes.
Definition: kclipboard.cpp:166
KClipboardSynchronizer::self
static KClipboardSynchronizer * self()
Returns the KClipboardSynchronizer singleton object.
Definition: kclipboard.cpp:71
KClipboardSynchronizer::isReverseSynchronizing
static bool isReverseSynchronizing()
Checks whether the Clipboard buffer will be copied to the Selection buffer upon changes.
Definition: kclipboard.cpp:177
KClipboardSynchronizer::setSynchronizing
static void setSynchronizing(bool sync)
Configures KClipboardSynchronizer to synchronize the Selection to Clipboard whenever it changes.
Definition: kclipboard.cpp:160
KClipboardSynchronizer::~KClipboardSynchronizer
~KClipboardSynchronizer()
Definition: kclipboard.cpp:87
KClipboardSynchronizer::setReverseSynchronizing
static void setReverseSynchronizing(bool enable)
Configures KClipboardSynchronizer to copy the Clipboard buffer to the Selection buffer whenever the C...
Definition: kclipboard.cpp:171
KConfigGroup
KGlobalSettings::ClipboardConfigChanged
@ ClipboardConfigChanged
Definition: kglobalsettings.h:545
QObject
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
kclipboard.h
kconfiggroup.h
kglobal.h
kglobalsettings.h
ksharedconfig.h
config
KSharedConfigPtr config()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

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

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal