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

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • 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 
45 class 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 
67 bool KClipboardSynchronizer::Private::s_sync = false;
68 bool KClipboardSynchronizer::Private::s_reverse_sync = false;
69 bool KClipboardSynchronizer::Private::s_blocked = false;
70 
71 KClipboardSynchronizer * KClipboardSynchronizer::self()
72 {
73  K_GLOBAL_STATIC(KClipboardSynchronizer, s_self)
74  return s_self;
75 }
76 
77 KClipboardSynchronizer::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 
87 KClipboardSynchronizer::~KClipboardSynchronizer()
88 {
89  delete d;
90 }
91 
92 void 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 
107 void 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 
119 void 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 
131 void 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 
139 void 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 
160 void KClipboardSynchronizer::setSynchronizing( bool sync )
161 {
162  Private::s_sync = sync;
163  self()->d->setupSignals();
164 }
165 
166 bool KClipboardSynchronizer::isSynchronizing()
167 {
168  return Private::s_sync;
169 }
170 
171 void KClipboardSynchronizer::setReverseSynchronizing( bool enable )
172 {
173  Private::s_reverse_sync = enable;
174  self()->d->setupSignals();
175 }
176 
177 bool KClipboardSynchronizer::isReverseSynchronizing()
178 {
179  return Private::s_reverse_sync;
180 }
181 
182 #include "kclipboard.moc"
kclipboard.h
KClipboardSynchronizer::~KClipboardSynchronizer
~KClipboardSynchronizer()
Definition: kclipboard.cpp:87
QClipboard::mimeData
const QMimeData * mimeData(Mode mode) const
kglobalsettings.h
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
QClipboard::ownsSelection
bool ownsSelection() const
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
QClipboard::ownsClipboard
bool ownsClipboard() const
QDBusConnection::sessionBus
QDBusConnection sessionBus()
QMimeData
config
KSharedConfigPtr config()
QClipboard
KGlobalSettings::ClipboardConfigChanged
Definition: kglobalsettings.h:545
kglobal.h
QApplication::clipboard
QClipboard * clipboard()
QObject
ksharedconfig.h
QString
QClipboard::setMimeData
void setMimeData(QMimeData *src, Mode mode)
KClipboardSynchronizer::setSynchronizing
static void setSynchronizing(bool sync)
Configures KClipboardSynchronizer to synchronize the Selection to Clipboard whenever it changes...
Definition: kclipboard.cpp:160
KClipboardSynchronizer::isReverseSynchronizing
static bool isReverseSynchronizing()
Checks whether the Clipboard buffer will be copied to the Selection buffer upon changes.
Definition: kclipboard.cpp:177
KConfigGroup
KClipboardSynchronizer
This class is only for internal use.
Definition: kclipboard.h:35
KClipboardSynchronizer::self
static KClipboardSynchronizer * self()
Returns the KClipboardSynchronizer singleton object.
Definition: kclipboard.cpp:71
QDBusConnection::connect
bool connect(const QString &service, const QString &path, const QString &interface, const QString &name, QObject *receiver, const char *slot)
kconfiggroup.h
KClipboardSynchronizer::isSynchronizing
static bool isSynchronizing()
Checks whether Clipboard and Selection will be synchronized upon changes.
Definition: kclipboard.cpp:166
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:23:59 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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