1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
|
/* This file is part of the KDE project
Copyright (C) by Andrew Stanley-Jones
Copyright (C) 2000 by Carsten Pfeiffer <pfeiffer@kde.org>
Copyright (C) 2004 Esben Mose Hansen <kde@mosehansen.dk>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <tqclipboard.h>
#include <tqcursor.h>
#include <tqdatetime.h>
#include <tqfile.h>
#include <tqintdict.h>
#include <tqpainter.h>
#include <tqtooltip.h>
#include <tqmime.h>
#include <tqdragobject.h>
#include <tdeaboutdata.h>
#include <tdeaction.h>
#include <tdeapplication.h>
#include <tdeconfig.h>
#include <tdeglobal.h>
#include <kipc.h>
#include <kkeydialog.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <ksavefile.h>
#include <kstandarddirs.h>
#include <ksimpleconfig.h>
#include <kstringhandler.h>
#include <ksystemtray.h>
#include <kurldrag.h>
#include <twin.h>
#include <kdebug.h>
#include <tdeglobalsettings.h>
#include <dcopclient.h>
#include <kiconloader.h>
#include <khelpmenu.h>
#include <kstdguiitem.h>
#include "configdialog.h"
#include "toplevel.h"
#include "urlgrabber.h"
#include "version.h"
#include "clipboardpoll.h"
#include "history.h"
#include "historyitem.h"
#include "historystringitem.h"
#include "klipperpopup.h"
#include <zlib.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <kclipboard.h>
namespace {
/**
* Use this when manipulating the clipboard
* from within clipboard-related signals.
*
* This avoids issues such as mouse-selections that immediately
* disappear.
* pattern: Resource Acqusition is Initialisation (RAII)
*
* (This is not threadsafe, so don't try to use such in threaded
* applications).
*/
struct Ignore {
Ignore(int& locklevel) : locklevelref(locklevel) {
locklevelref++;
}
~Ignore() {
locklevelref--;
}
private:
int& locklevelref;
};
}
/**
* Helper class to save history upon session exit.
*/
class KlipperSessionManaged : public KSessionManaged
{
public:
KlipperSessionManaged( KlipperWidget* k )
: klipper( k )
{}
/**
* Save state upon session exit.
*
* Saving history on session save
*/
virtual bool commitData( TQSessionManager& ) {
klipper->saveSession();
return true;
}
private:
KlipperWidget* klipper;
};
extern bool tqt_qclipboard_bailout_hack;
#if KDE_IS_VERSION( 15, 0, 0 )
#error Check status of #80072 with Qt4.
#endif
static void ensureGlobalSyncOff(TDEConfig* config);
// config == kapp->config for process, otherwise applet
KlipperWidget::KlipperWidget( TQWidget *parent, TDEConfig* config )
: TQWidget( parent )
, DCOPObject( "klipper" )
, m_overflowCounter( 0 )
, locklevel( 0 )
, m_config( config )
, m_pendingContentsCheck( false )
, session_managed( new KlipperSessionManaged( this ))
{
tqt_qclipboard_bailout_hack = true;
// We don't use the clipboardsynchronizer anymore, and it confuses Klipper
ensureGlobalSyncOff(m_config);
updateTimestamp(); // read initial X user time
setBackgroundMode( X11ParentRelative );
clip = kapp->clipboard();
connect( &m_overflowClearTimer, TQT_SIGNAL( timeout()), TQT_SLOT( slotClearOverflow()));
m_overflowClearTimer.start( 1000 );
connect( &m_pendingCheckTimer, TQT_SIGNAL( timeout()), TQT_SLOT( slotCheckPending()));
connect( &m_setClipboardTimer, TQT_SIGNAL( timeout()), TQT_SLOT( slotDelayedSetClipboard()));
m_history = new History( this, "main_history" );
// we need that collection, otherwise TDEToggleAction is not happy :}
TQString defaultGroup( "default" );
TDEActionCollection *collection = new TDEActionCollection( this, "my collection" );
toggleURLGrabAction = new TDEToggleAction( collection, "toggleUrlGrabAction" );
toggleURLGrabAction->setEnabled( true );
toggleURLGrabAction->setGroup( defaultGroup );
clearHistoryAction = new TDEAction( i18n("C&lear Clipboard History"),
"history_clear",
0,
TQT_TQOBJECT(history()),
TQT_SLOT( slotClear() ),
collection,
"clearHistoryAction" );
connect( clearHistoryAction, TQT_SIGNAL( activated() ), TQT_SLOT( slotClearClipboard() ) );
clearHistoryAction->setGroup( defaultGroup );
configureAction = new TDEAction( i18n("&Configure Klipper..."),
"configure",
0,
TQT_TQOBJECT(this),
TQT_SLOT( slotConfigure() ),
collection,
"configureAction" );
configureAction->setGroup( defaultGroup );
quitAction = new TDEAction( i18n("&Quit"),
"system-log-out",
0,
TQT_TQOBJECT(this),
TQT_SLOT( slotQuit() ),
collection,
"quitAction" );
quitAction->setGroup( "exit" );
myURLGrabber = 0L;
TDEConfig *kc = m_config;
readConfiguration( kc );
setURLGrabberEnabled( bURLGrabber );
hideTimer = new TQTime();
showTimer = new TQTime();
readProperties(m_config);
connect(kapp, TQT_SIGNAL(settingsChanged(int)), TQT_SLOT(slotSettingsChanged(int)));
poll = new ClipboardPoll( this );
connect( poll, TQT_SIGNAL( clipboardChanged( bool ) ),
this, TQT_SLOT( newClipData( bool ) ) );
if ( isApplet() || !isShown() ) {
m_pixmap = KSystemTray::loadIcon( "klipper" );
}
else {
m_pixmap = KSystemTray::loadSizedIcon( "klipper", width() );
}
m_iconOrigWidth = m_pixmap.width();
m_iconOrigHeight = m_pixmap.height();
adjustSize();
globalKeys = new TDEGlobalAccel(TQT_TQOBJECT(this));
TDEGlobalAccel* keys = globalKeys;
#include "klipperbindings.cpp"
// the keys need to be read from kdeglobals, not kickerrc --ellis, 22/9/02
globalKeys->readSettings();
globalKeys->updateConnections();
toggleURLGrabAction->setShortcut(globalKeys->shortcut("Enable/Disable Clipboard Actions"));
connect( toggleURLGrabAction, TQT_SIGNAL( toggled( bool )),
this, TQT_SLOT( setURLGrabberEnabled( bool )));
KlipperPopup* popup = history()->popup();
connect ( history(), TQT_SIGNAL( topChanged() ), TQT_SLOT( slotHistoryTopChanged() ) );
connect( popup, TQT_SIGNAL( aboutToHide() ), TQT_SLOT( slotStartHideTimer() ) );
connect( popup, TQT_SIGNAL( aboutToShow() ), TQT_SLOT( slotStartShowTimer() ) );
popup->plugAction( toggleURLGrabAction );
popup->plugAction( clearHistoryAction );
popup->plugAction( configureAction );
if ( !isApplet() ) {
popup->plugAction( quitAction );
}
TQToolTip::add( this, i18n("Klipper - clipboard tool") );
}
KlipperWidget::~KlipperWidget()
{
delete session_managed;
delete showTimer;
delete hideTimer;
delete myURLGrabber;
if( m_config != kapp->config())
delete m_config;
tqt_qclipboard_bailout_hack = false;
}
void KlipperWidget::adjustSize()
{
resize( m_pixmap.size() );
}
// DCOP
TQString KlipperWidget::getClipboardContents()
{
return getClipboardHistoryItem(0);
}
// DCOP - don't call from Klipper itself
void KlipperWidget::setClipboardContents(TQString s)
{
Ignore lock( locklevel );
updateTimestamp();
HistoryStringItem* item = new HistoryStringItem( s );
setClipboard( *item, Clipboard | Selection);
history()->insert( item );
}
// DCOP - don't call from Klipper itself
void KlipperWidget::clearClipboardContents()
{
updateTimestamp();
slotClearClipboard();
}
// DCOP - don't call from Klipper itself
void KlipperWidget::clearClipboardHistory()
{
updateTimestamp();
slotClearClipboard();
history()->slotClear();
saveSession();
}
void KlipperWidget::mousePressEvent(TQMouseEvent *e)
{
if ( e->button() != Qt::LeftButton && e->button() != Qt::RightButton )
return;
// if we only hid the menu less than a third of a second ago,
// it's probably because the user clicked on the klipper icon
// to hide it, and therefore won't want it shown again.
if ( hideTimer->elapsed() > 300 ) {
slotPopupMenu();
}
}
void KlipperWidget::paintEvent(TQPaintEvent *)
{
TQPainter p(this);
// Honor Free Desktop specifications that allow for arbitrary system tray icon sizes
if (m_scaledpixmap.isNull() ||
(m_iconOrigWidth != width()) || (m_iconOrigHeight != height())) {
TQImage newIcon;
m_iconOrigWidth = width();
m_iconOrigHeight = height();
m_pixmap = KSystemTray::loadSizedIcon( "klipper", width() );
newIcon = m_pixmap;
newIcon = newIcon.smoothScale(width(), height());
m_scaledpixmap = newIcon;
}
int x = (width() - m_scaledpixmap.width()) / 2;
int y = (height() - m_scaledpixmap.height()) / 2;
if ( x < 0 ) x = 0;
if ( y < 0 ) y = 0;
p.drawPixmap(x, y, m_scaledpixmap);
p.end();
}
void KlipperWidget::slotStartHideTimer()
{
hideTimer->start();
}
void KlipperWidget::slotStartShowTimer()
{
showTimer->start();
}
void KlipperWidget::showPopupMenu( TQPopupMenu *menu )
{
Q_ASSERT( menu != 0L );
TQSize size = menu->sizeHint(); // geometry is not valid until it's shown
if (bPopupAtMouse) {
TQPoint g = TQCursor::pos();
if ( size.height() < g.y() )
menu->popup(TQPoint( g.x(), g.y() - size.height()));
else
menu->popup(TQPoint(g.x(), g.y()));
} else {
KWin::WindowInfo i = KWin::windowInfo( winId(), NET::WMGeometry );
TQRect g = i.geometry();
TQRect screen = TDEGlobalSettings::desktopGeometry(g.center());
if ( g.x()-screen.x() > screen.width()/2 &&
g.y()-screen.y() + size.height() > screen.height() )
menu->popup(TQPoint( g.x(), g.y() - size.height()));
else
menu->popup(TQPoint( g.x() + width(), g.y() + height()));
// menu->exec(mapToGlobal(TQPoint( width()/2, height()/2 )));
}
}
bool KlipperWidget::loadHistory() {
static const char* const failed_load_warning =
"Failed to load history resource. Clipboard history cannot be read.";
// don't use "appdata", klipper is also a kicker applet
TQString history_file_name = ::locateLocal( "data", "klipper/history2.lst" );
TQFile history_file( history_file_name );
bool oldfile = false;
if ( !history_file.exists() ) { // backwards compatibility
oldfile = true;
history_file_name = ::locateLocal( "data", "klipper/history.lst" );
history_file.setName( history_file_name );
if ( !history_file.exists() ) {
history_file_name = ::locateLocal( "data", "kicker/history.lst" );
history_file.setName( history_file_name );
if ( !history_file.exists() ) {
return false;
}
}
}
if ( !history_file.open( IO_ReadOnly ) ) {
kdWarning() << failed_load_warning << ": " << TQString(history_file.errorString()) << endl;
return false;
}
TQDataStream file_stream( &history_file );
if( file_stream.atEnd()) {
kdWarning() << failed_load_warning << endl;
return false;
}
TQDataStream* history_stream = &file_stream;
TQByteArray data;
if( !oldfile ) {
TQ_UINT32 crc;
file_stream >> crc >> data;
if( crc32( 0, reinterpret_cast<unsigned char *>( data.data() ), data.size() ) != crc ) {
kdWarning() << failed_load_warning << ": " << TQString(history_file.errorString()) << endl;
return false;
}
history_stream = new TQDataStream( data, IO_ReadOnly );
}
char* version;
*history_stream >> version;
delete[] version;
// The list needs to be reversed, as it is saved
// youngest-first to keep the most important clipboard
// items at the top, but the history is created oldest
// first.
TQPtrList<HistoryItem> reverseList;
for ( HistoryItem* item = HistoryItem::create( *history_stream );
item;
item = HistoryItem::create( *history_stream ) )
{
reverseList.prepend( item );
}
for ( HistoryItem* item = reverseList.first();
item;
item = reverseList.next() )
{
history()->forceInsert( item );
}
if ( !history()->empty() ) {
m_lastSelection = -1;
m_lastClipboard = -1;
setClipboard( *history()->first(), Clipboard | Selection );
}
if( history_stream != &file_stream )
delete history_stream;
return true;
}
void KlipperWidget::saveHistory() {
static const char* const failed_save_warning =
"Failed to save history. Clipboard history cannot be saved.";
// don't use "appdata", klipper is also a kicker applet
TQString history_file_name( ::locateLocal( "data", "klipper/history2.lst" ) );
if ( history_file_name.isNull() || history_file_name.isEmpty() ) {
kdWarning() << failed_save_warning << endl;
return;
}
KSaveFile history_file( history_file_name );
if ( history_file.status() != 0 ) {
kdWarning() << failed_save_warning << endl;
return;
}
TQByteArray data;
TQDataStream history_stream( data, IO_WriteOnly );
history_stream << klipper_version; // const char*
for ( const HistoryItem* item = history()->first(); item; item = history()->next() ) {
history_stream << item;
}
TQ_UINT32 crc = crc32( 0, reinterpret_cast<unsigned char *>( data.data() ), data.size() );
*history_file.dataStream() << crc << data;
}
void KlipperWidget::readProperties(TDEConfig *kc)
{
TQStringList dataList;
history()->slotClear();
if (bKeepContents) { // load old clipboard if configured
if ( !loadHistory() ) {
// Try to load from the old config file.
// Remove this at some point.
TDEConfigGroupSaver groupSaver(kc, "General");
dataList = kc->readListEntry("ClipboardData");
for (TQStringList::ConstIterator it = dataList.end();
it != dataList.begin();
)
{
history()->forceInsert( new HistoryStringItem( *( --it ) ) );
}
if ( !dataList.isEmpty() )
{
m_lastSelection = -1;
m_lastClipboard = -1;
setClipboard( *history()->first(), Clipboard | Selection );
}
}
}
}
void KlipperWidget::readConfiguration( TDEConfig *kc )
{
kc->setGroup("General");
bPopupAtMouse = kc->readBoolEntry("PopupAtMousePosition", false);
bKeepContents = kc->readBoolEntry("KeepClipboardContents", true);
bURLGrabber = kc->readBoolEntry("URLGrabberEnabled", false);
bReplayActionInHistory = kc->readBoolEntry("ReplayActionInHistory", false);
bNoNullClipboard = kc->readBoolEntry("NoEmptyClipboard", true);
bUseGUIRegExpEditor = kc->readBoolEntry("UseGUIRegExpEditor", true );
history()->max_size( kc->readNumEntry("MaxClipItems", 7) );
bIgnoreSelection = kc->readBoolEntry("IgnoreSelection", false);
bSynchronize = kc->readBoolEntry("Synchronize", false);
bSelectionTextOnly = kc->readBoolEntry("SelectionTextOnly",true);
bIgnoreImages = kc->readBoolEntry("IgnoreImages",true);
}
void KlipperWidget::writeConfiguration( TDEConfig *kc )
{
kc->setGroup("General");
kc->writeEntry("PopupAtMousePosition", bPopupAtMouse);
kc->writeEntry("KeepClipboardContents", bKeepContents);
kc->writeEntry("ReplayActionInHistory", bReplayActionInHistory);
kc->writeEntry("NoEmptyClipboard", bNoNullClipboard);
kc->writeEntry("UseGUIRegExpEditor", bUseGUIRegExpEditor);
kc->writeEntry("MaxClipItems", history()->max_size() );
kc->writeEntry("IgnoreSelection", bIgnoreSelection);
kc->writeEntry("Synchronize", bSynchronize );
kc->writeEntry("SelectionTextOnly", bSelectionTextOnly);
kc->writeEntry("TrackImages", bIgnoreImages);
kc->writeEntry("Version", klipper_version );
if ( myURLGrabber )
myURLGrabber->writeConfiguration( kc );
kc->sync();
}
// save session on shutdown. Don't simply use the c'tor, as that may not be called.
void KlipperWidget::saveSession()
{
if ( bKeepContents ) { // save the clipboard eventually
saveHistory();
}
}
void KlipperWidget::slotSettingsChanged( int category )
{
if ( category == (int) TDEApplication::SETTINGS_SHORTCUTS ) {
globalKeys->readSettings();
globalKeys->updateConnections();
toggleURLGrabAction->setShortcut(globalKeys->shortcut("Enable/Disable Clipboard Actions"));
}
}
void KlipperWidget::disableURLGrabber()
{
KMessageBox::information( 0L,
i18n( "You can enable URL actions later by right-clicking on the "
"Klipper icon and selecting 'Enable Actions'" ) );
setURLGrabberEnabled( false );
}
void KlipperWidget::slotConfigure()
{
bool haveURLGrabber = bURLGrabber;
if ( !myURLGrabber ) { // temporary, for the config-dialog
setURLGrabberEnabled( true );
readConfiguration( m_config );
}
ConfigDialog *dlg = new ConfigDialog( myURLGrabber->actionList(),
globalKeys, isApplet() );
dlg->setKeepContents( bKeepContents );
dlg->setPopupAtMousePos( bPopupAtMouse );
dlg->setStripWhiteSpace( myURLGrabber->stripWhiteSpace() );
dlg->setReplayActionInHistory( bReplayActionInHistory );
dlg->setNoNullClipboard( bNoNullClipboard );
dlg->setUseGUIRegExpEditor( bUseGUIRegExpEditor );
dlg->setPopupTimeout( myURLGrabber->popupTimeout() );
dlg->setMaxItems( history()->max_size() );
dlg->setIgnoreSelection( bIgnoreSelection );
dlg->setSynchronize( bSynchronize );
dlg->setNoActionsFor( myURLGrabber->avoidWindows() );
if ( dlg->exec() == TQDialog::Accepted ) {
bKeepContents = dlg->keepContents();
bPopupAtMouse = dlg->popupAtMousePos();
bReplayActionInHistory = dlg->replayActionInHistory();
bNoNullClipboard = dlg->noNullClipboard();
bIgnoreSelection = dlg->ignoreSelection();
bSynchronize = dlg->synchronize();
bUseGUIRegExpEditor = dlg->useGUIRegExpEditor();
dlg->commitShortcuts();
// the keys need to be written to kdeglobals, not kickerrc --ellis, 22/9/02
globalKeys->writeSettings(0, true);
globalKeys->updateConnections();
toggleURLGrabAction->setShortcut(globalKeys->shortcut("Enable/Disable Clipboard Actions"));
myURLGrabber->setActionList( dlg->actionList() );
myURLGrabber->setPopupTimeout( dlg->popupTimeout() );
myURLGrabber->setStripWhiteSpace( dlg->stripWhiteSpace() );
myURLGrabber->setAvoidWindows( dlg->noActionsFor() );
history()->max_size( dlg->maxItems() );
writeConfiguration( m_config );
}
setURLGrabberEnabled( haveURLGrabber );
delete dlg;
}
void KlipperWidget::slotQuit()
{
// If the menu was just opened, likely the user
// selected quit by accident while attempting to
// click the Klipper icon.
if ( showTimer->elapsed() < 300 ) {
return;
}
saveSession();
int autoStart = KMessageBox::questionYesNoCancel( 0L, i18n("Should Klipper start automatically\nwhen you login?"), i18n("Automatically Start Klipper?"), i18n("Start"), i18n("Do Not Start") );
TDEConfig *config = TDEGlobal::config();
config->setGroup("General");
if ( autoStart == KMessageBox::Yes ) {
config->writeEntry("AutoStart", true);
} else if ( autoStart == KMessageBox::No) {
config->writeEntry("AutoStart", false);
} else // cancel chosen don't quit
return;
config->sync();
kapp->quit();
}
void KlipperWidget::slotPopupMenu() {
KlipperPopup* popup = history()->popup();
popup->ensureClean();
showPopupMenu( popup );
}
void KlipperWidget::slotRepeatAction()
{
if ( !myURLGrabber ) {
myURLGrabber = new URLGrabber( m_config );
connect( myURLGrabber, TQT_SIGNAL( sigPopup( TQPopupMenu * )),
TQT_SLOT( showPopupMenu( TQPopupMenu * )) );
connect( myURLGrabber, TQT_SIGNAL( sigDisablePopup() ),
this, TQT_SLOT( disableURLGrabber() ) );
}
const HistoryStringItem* top = dynamic_cast<const HistoryStringItem*>( history()->first() );
if ( top ) {
myURLGrabber->invokeAction( top->text() );
}
}
void KlipperWidget::setURLGrabberEnabled( bool enable )
{
if (enable != bURLGrabber) {
bURLGrabber = enable;
TDEConfig *kc = m_config;
kc->setGroup("General");
kc->writeEntry("URLGrabberEnabled", bURLGrabber);
m_lastURLGrabberTextSelection = TQString();
m_lastURLGrabberTextClipboard = TQString();
}
toggleURLGrabAction->setChecked( enable );
if ( !bURLGrabber ) {
delete myURLGrabber;
myURLGrabber = 0L;
toggleURLGrabAction->setText(i18n("Enable &Actions"));
}
else {
toggleURLGrabAction->setText(i18n("&Actions Enabled"));
if ( !myURLGrabber ) {
myURLGrabber = new URLGrabber( m_config );
connect( myURLGrabber, TQT_SIGNAL( sigPopup( TQPopupMenu * )),
TQT_SLOT( showPopupMenu( TQPopupMenu * )) );
connect( myURLGrabber, TQT_SIGNAL( sigDisablePopup() ),
this, TQT_SLOT( disableURLGrabber() ) );
}
}
}
void KlipperWidget::toggleURLGrabber()
{
setURLGrabberEnabled( !bURLGrabber );
}
void KlipperWidget::slotHistoryTopChanged() {
if ( locklevel ) {
return;
}
const HistoryItem* topitem = history()->first();
if ( topitem ) {
setClipboard( *topitem, Clipboard | Selection );
}
if ( bReplayActionInHistory && bURLGrabber ) {
slotRepeatAction();
}
}
void KlipperWidget::slotClearClipboard()
{
Ignore lock( locklevel );
clip->clear(TQClipboard::Selection);
clip->clear(TQClipboard::Clipboard);
}
//XXX: Should die, and the DCOP signal handled sensible.
TQString KlipperWidget::clipboardContents( bool * /*isSelection*/ )
{
kdWarning() << "Obsolete function called. Please fix" << endl;
#if 0
bool selection = true;
TQMimeSource* data = clip->data(TQClipboard::Selection);
if ( data->serialNumber() == m_lastSelection )
{
TQString clipContents = clip->text(TQClipboard::Clipboard);
if ( clipContents != m_lastClipboard )
{
contents = clipContents;
selection = false;
}
else
selection = true;
}
if ( isSelection )
*isSelection = selection;
#endif
return 0;
}
void KlipperWidget::applyClipChanges( const TQMimeSource& clipData )
{
if ( locklevel )
return;
Ignore lock( locklevel );
history()->insert( HistoryItem::create( clipData ) );
}
void KlipperWidget::newClipData( bool selectionMode )
{
if ( locklevel ) {
return;
}
if( blockFetchingNewData())
return;
checkClipData( selectionMode );
}
void KlipperWidget::clipboardSignalArrived( bool selectionMode )
{
if ( locklevel ) {
return;
}
if( blockFetchingNewData())
return;
updateTimestamp();
checkClipData( selectionMode );
}
// Protection against too many clipboard data changes. Lyx responds to clipboard data
// requests with setting new clipboard data, so if Lyx takes over clipboard,
// Klipper notices, requests this data, this triggers "new" clipboard contents
// from Lyx, so Klipper notices again, requests this data, ... you get the idea.
const int MAX_CLIPBOARD_CHANGES = 10; // max changes per second
bool KlipperWidget::blockFetchingNewData()
{
// Hacks for #85198 and #80302.
// #85198 - block fetching new clipboard contents if Shift is pressed and mouse is not,
// this may mean the user is doing selection using the keyboard, in which case
// it's possible the app sets new clipboard contents after every change - Klipper's
// history would list them all.
// #80302 - OOo (v1.1.3 at least) has a bug that if Klipper requests its clipboard contents
// while the user is doing a selection using the mouse, OOo stops updating the clipboard
// contents, so in practice it's like the user has selected only the part which was
// selected when Klipper asked first.
ButtonState buttonstate = kapp->keyboardMouseState();
if( ( buttonstate & ( ShiftButton | Qt::LeftButton )) == ShiftButton // #85198
|| ( buttonstate & Qt::LeftButton ) == Qt::LeftButton ) { // #80302
m_pendingContentsCheck = true;
m_pendingCheckTimer.start( 100, true );
return true;
}
m_pendingContentsCheck = false;
if( ++m_overflowCounter > MAX_CLIPBOARD_CHANGES )
return true;
return false;
}
void KlipperWidget::slotCheckPending()
{
if( !m_pendingContentsCheck )
return;
m_pendingContentsCheck = false; // blockFetchingNewData() will be called again
updateTimestamp();
newClipData( true ); // always selection
}
void KlipperWidget::slotDelayedSetClipboard()
{
const HistoryItem *top = history()->first();
if (top)
{
if (bCheckForEmpty)
{
TQMimeSource *data = clip->data( bSavedSelectionMode ? TQClipboard::Selection : TQClipboard::Clipboard );
if ( !data )
{
kdWarning("No data in clipboard. This is not supposed to happen." );
return;
}
bool clipEmpty = ( data->format() == 0L );
if ( clipEmpty && bNoNullClipboard )
{
// keep old clipboard after someone set it to null
#ifdef NOISY_KLIPPER
kdDebug() << "Resetting clipboard (Prevent empty clipboard)" << endl;
#endif
setClipboard( *top, bSavedSelectionMode ? Selection : Clipboard );
return;
}
}
else
{
#ifdef NOISY_KLIPPER
kdDebug() << "Syncing selection and clipboard" << endl;
#endif
setClipboard( *top, bSavedSelectionMode ? Selection : Clipboard );
}
}
}
void KlipperWidget::checkClipData( bool selectionMode )
{
if ( ignoreClipboardChanges() ) // internal to klipper, ignoring TQSpinBox selections
{
// keep our old clipboard, thanks
// This won't quite work, but it's close enough for now.
// The trouble is that the top selection =! top clipboard
// but we don't track that yet. We will....
const HistoryItem* top = history()->first();
if ( top ) {
setClipboard( *top, selectionMode ? Selection : Clipboard);
}
return;
}
// debug code
// debug code
#ifdef NOISY_KLIPPER
kdDebug() << "Checking clip data" << endl;
#endif
#if 0
kdDebug() << "====== c h e c k C l i p D a t a ============================"
<< kdBacktrace()
<< "====== c h e c k C l i p D a t a ============================"
<< endl;;
#endif
#if 0
if ( sender() ) {
kdDebug() << "sender=" << sender()->name() << endl;
} else {
kdDebug() << "no sender" << endl;
}
#endif
#if 0
kdDebug() << "\nselectionMode=" << selectionMode
<< "\nserialNo=" << clip->data()->serialNumber() << " (sel,cli)=(" << m_lastSelection << "," << m_lastClipboard << ")"
<< "\nowning (sel,cli)=(" << clip->ownsSelection() << "," << clip->ownsClipboard() << ")"
<< "\ntext=" << clip->text( selectionMode ? TQClipboard::Selection : TQClipboard::Clipboard) << endl;
#endif
#if 0
const char *format;
int i = 0;
while ( (format = clip->data()->format( i++ )) )
{
tqDebug( " format: %s", format);
}
#endif
TQMimeSource* data = clip->data( selectionMode ? TQClipboard::Selection : TQClipboard::Clipboard );
if ( !data ) {
kdWarning("No data in clipboard. This not not supposed to happen." );
return;
}
int lastSerialNo = selectionMode ? m_lastSelection : m_lastClipboard;
bool changed = data->serialNumber() != lastSerialNo;
bool clipEmpty = ( data->format() == 0L );
if ( changed && clipEmpty && bNoNullClipboard )
{
// Make sure to call setClipboard() through the event loop.
// Using a direct call may crash another application that was
// changing the clipboard at the same time.
bSavedSelectionMode = selectionMode;
bCheckForEmpty = true;
m_setClipboardTimer.start(100, TRUE);
return;
}
// this must be below the "bNoNullClipboard" handling code!
// XXX: I want a better handling of selection/clipboard in general.
// XXX: Order sensitive code. Must die.
if ( selectionMode && bIgnoreSelection )
return;
if( selectionMode && bSelectionTextOnly && !TQTextDrag::canDecode( data ))
return;
if( KURLDrag::canDecode( data ))
; // ok
else if( TQTextDrag::canDecode( data ))
; // ok
else if( TQImageDrag::canDecode( data ))
{
// Limit mimetypes that are tracked by Klipper (this is basically a workaround
// for #109032). Can't add UI in 3.5 because of string freeze, and I'm not sure
// if this actually needs to be more configurable than only text vs all klipper knows.
if( bIgnoreImages )
return;
else {
// ok
}
}
else // unknown, ignore
return;
// store old contents:
if ( selectionMode )
m_lastSelection = data->serialNumber();
else
m_lastClipboard = data->serialNumber();
TQString& lastURLGrabberText = selectionMode
? m_lastURLGrabberTextSelection : m_lastURLGrabberTextClipboard;
if( TQTextDrag::canDecode( data ))
{
if ( bURLGrabber && myURLGrabber )
{
TQString text;
TQTextDrag::decode( data, text );
// Make sure URLGrabber doesn't repeat all the time if klipper reads the same
// text all the time (e.g. because XFixes is not available and the application
// has broken TIMESTAMP target). Using most recent history item may not always
// work.
if ( text != lastURLGrabberText )
{
lastURLGrabberText = text;
if ( myURLGrabber->checkNewData( text ) )
{
return; // don't add into the history
}
}
}
else
lastURLGrabberText = TQString();
}
else
lastURLGrabberText = TQString();
if (changed) {
applyClipChanges( *data );
#ifdef NOISY_KLIPPER
kdDebug() << "Synchronize?" << ( bSynchronize ? "yes" : "no" ) << endl;
#endif
if ( bSynchronize )
{
// Make sure to call setClipboard() through the event loop.
// Using a direct call may crash another application that was
// changing the clipboard at the same time.
bSavedSelectionMode = !selectionMode; // inverted in order to sync
bCheckForEmpty = false;
m_setClipboardTimer.start(100, TRUE);
}
}
}
void KlipperWidget::setClipboard( const HistoryItem& item, int mode )
{
Ignore lock( locklevel );
Q_ASSERT( ( mode & 1 ) == 0 ); // Warn if trying to pass a boolean as a mode.
if ( mode & Selection ) {
#ifdef NOSIY_KLIPPER
kdDebug() << "Setting selection to <" << item.text() << ">" << endl;
#endif
clip->setData( item.mimeSource(), TQClipboard::Selection );
m_lastSelection = clip->data()->serialNumber();
}
if ( mode & Clipboard ) {
#ifdef NOSIY_KLIPPER
kdDebug() << "Setting clipboard to <" << item.text() << ">" << endl;
#endif
clip->setData( item.mimeSource(), TQClipboard::Clipboard );
m_lastClipboard = clip->data()->serialNumber();
}
}
void KlipperWidget::slotClearOverflow()
{
if( m_overflowCounter > MAX_CLIPBOARD_CHANGES ) {
kdDebug() << "App owning the clipboard/selection is lame" << endl;
// update to the latest data - this unfortunately may trigger the problem again
newClipData( true ); // Always the selection.
}
m_overflowCounter = 0;
}
TQStringList KlipperWidget::getClipboardHistoryMenu()
{
TQStringList menu;
for ( const HistoryItem* item = history()->first(); item; item = history()->next() ) {
menu << item->text();
}
return menu;
}
TQString KlipperWidget::getClipboardHistoryItem(int i)
{
for ( const HistoryItem* item = history()->first(); item; item = history()->next() , i-- ) {
if ( i == 0 ) {
return item->text();
}
}
return TQString::null;
}
//
// changing a spinbox in klipper's config-dialog causes the lineedit-contents
// of the spinbox to be selected and hence the clipboard changes. But we don't
// want all those items in klipper's history. See #41917
//
bool KlipperWidget::ignoreClipboardChanges() const
{
TQWidget *focusWidget = tqApp->focusWidget();
if ( focusWidget )
{
if ( focusWidget->inherits( "TQSpinBox" ) ||
(focusWidget->parentWidget() &&
focusWidget->inherits("TQLineEdit") &&
focusWidget->parentWidget()->inherits("TQSpinWidget")) )
{
return true;
}
}
return false;
}
// TQClipboard uses tqt_x_time as the timestamp for selection operations.
// It is updated mainly from user actions, but Klipper polls the clipboard
// without any user action triggering it, so tqt_x_time may be old,
// which could possibly lead to TQClipboard reporting empty clipboard.
// Therefore, tqt_x_time needs to be updated to current X server timestamp.
// Call TDEApplication::updateUserTime() only from functions that are
// called from outside (DCOP), or from TQTimer timeout !
static Time next_x_time;
static Bool update_x_time_predicate( Display*, XEvent* event, XPointer )
{
if( next_x_time != CurrentTime )
return False;
// from qapplication_x11.cpp
switch ( event->type ) {
case ButtonPress:
// fallthrough intended
case ButtonRelease:
next_x_time = event->xbutton.time;
break;
case MotionNotify:
next_x_time = event->xmotion.time;
break;
case KeyPress:
// fallthrough intended
case KeyRelease:
next_x_time = event->xkey.time;
break;
case PropertyNotify:
next_x_time = event->xproperty.time;
break;
case EnterNotify:
case LeaveNotify:
next_x_time = event->xcrossing.time;
break;
case SelectionClear:
next_x_time = event->xselectionclear.time;
break;
default:
break;
}
return False;
}
void KlipperWidget::updateTimestamp()
{ // Qt3.3.0 and 3.3.1 use tqt_x_user_time for clipboard operations
Time time = ( strcmp( tqVersion(), "3.3.1" ) == 0
|| strcmp( tqVersion(), "3.3.0" ) == 0 )
? GET_QT_X_USER_TIME() : GET_QT_X_TIME();
static TQWidget* w = 0;
if ( !w )
w = new TQWidget;
unsigned char data[ 1 ];
XChangeProperty( tqt_xdisplay(), w->winId(), XA_ATOM, XA_ATOM, 8, PropModeAppend, data, 1 );
next_x_time = CurrentTime;
XEvent dummy;
XCheckIfEvent( tqt_xdisplay(), &dummy, update_x_time_predicate, NULL );
if( next_x_time == CurrentTime )
{
XSync( tqt_xdisplay(), False );
XCheckIfEvent( tqt_xdisplay(), &dummy, update_x_time_predicate, NULL );
}
Q_ASSERT( next_x_time != CurrentTime );
time = next_x_time;
XEvent ev; // remove the PropertyNotify event from the events queue
XWindowEvent( tqt_xdisplay(), w->winId(), PropertyChangeMask, &ev );
}
static const char * const description =
I18N_NOOP("TDE cut & paste history utility");
void KlipperWidget::createAboutData()
{
about_data = new TDEAboutData("klipper", I18N_NOOP("Klipper"),
klipper_version, description, TDEAboutData::License_GPL,
"(c) 1998, Andrew Stanley-Jones\n"
"1998-2002, Carsten Pfeiffer\n"
"2001, Patrick Dubroy");
about_data->addAuthor("Carsten Pfeiffer",
I18N_NOOP("Author"),
"pfeiffer@kde.org");
about_data->addAuthor("Andrew Stanley-Jones",
I18N_NOOP( "Original Author" ),
"asj@cban.com");
about_data->addAuthor("Patrick Dubroy",
I18N_NOOP("Contributor"),
"patrickdu@corel.com");
about_data->addAuthor( "Luboš Luňák",
I18N_NOOP("Bugfixes and optimizations"),
"l.lunak@kde.org");
about_data->addAuthor( "Esben Mose Hansen",
I18N_NOOP("Maintainer"),
"kde@mosehansen.dk");
}
void KlipperWidget::destroyAboutData()
{
delete about_data;
about_data = NULL;
}
TDEAboutData* KlipperWidget::about_data;
TDEAboutData* KlipperWidget::aboutData()
{
return about_data;
}
Klipper::Klipper( TQWidget* parent )
: KlipperWidget( parent, kapp->config())
{
}
// this sucks ... KUniqueApplication registers itself as 'klipper'
// for the unique-app detection calls (and it shouldn't use that name IMHO)
// but in Klipper it's not KUniqueApplication class who handles
// the DCOP calls, but an instance of class Klipper, registered as 'klipper'
// this below avoids a warning when KUniqueApplication wouldn't otherwise
// find newInstance() (which doesn't do anything in Klipper anyway)
int Klipper::newInstance()
{
kapp->dcopClient()->setPriorityCall(false); // Allow other dcop calls
return 0;
}
// this is used for quiting klipper process, if klipper is being started as an applet
// (AKA ugly hack)
void Klipper::quitProcess()
{
kapp->dcopClient()->detach();
kapp->quit();
}
static void ensureGlobalSyncOff(TDEConfig* config) {
config->setGroup("General");
if ( config->readBoolEntry( "SynchronizeClipboardAndSelection" ) ) {
kdDebug() << "Shutting off global synchronization" << endl;
config->writeEntry("SynchronizeClipboardAndSelection",
false,
true,
true );
config->sync();
TDEClipboardSynchronizer::setSynchronizing( false );
TDEClipboardSynchronizer::setReverseSynchronizing( false );
KIPC::sendMessageAll( KIPC::ClipboardConfigChanged, 0 );
}
}
#include "toplevel.moc"
|