-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathQSPasteboardController.m
746 lines (594 loc) · 25.6 KB
/
QSPasteboardController.m
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
#import "QSPasteboardMonitor.h"
#import "QSPasteboardController.h"
#import "QSPasteboardAccessoryCell.h"
@interface QSController : NSWindowController
- (QSInterfaceController *)interfaceController;
@end
@interface QSPasteboardController () <QSTableViewDataSource, QSTableViewDelegate> {
NSIndexSet *_draggedRows;
}
@end
@implementation QSPasteboardController
@dynamic window;
+ (void)initialize {
// add clipboard option to the Quicksilver menu
NSMenu *modulesMenu = [[[NSApp mainMenu] itemWithTag:128] submenu];
NSMenuItem *modMenuItem = [modulesMenu addItemWithTitle:@"Clipboard History" action:@selector(showClipboard:) keyEquivalent:@"l"];
[modMenuItem setTarget:self];
[QSPasteboardMonitor sharedInstance];
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:kCapturePasteboardHistory])
[QSPasteboardController sharedInstance];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(saveVisibilityState:) name:@"QSEventNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showClipboardHidden:) name:@"QSApplicationDidFinishLaunchingNotification" object:nil];
NSImage *image = [[NSImage alloc] initByReferencingFile:
[[NSBundle bundleForClass:[QSPasteboardController class]]pathForImageResource:@"Clipboard"]];
[image shrinkToSize:QSSize16];
[modMenuItem setImage:image];
// set up the default apps to ignore clipboard content from: 1Password, Keychain and Wallet
if (![defaults objectForKey:@"clipboardIgnoreApps"]) {
NSArray *apps = [NSArray arrayWithObjects:@"com.agilebits.onepassword-osx",@"com.acrylic.wallet",@"com.apple.keychainaccess",@"com.microsoft.RDC",nil];
NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:2];
for (NSString *app in apps) {
if ([[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:app] ) {
[tempArray addObject:app];
}
}
[defaults setObject:tempArray forKey:@"clipboardIgnoreApps"];
}
}
+ (QSPasteboardController *)sharedInstance {
static id _sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[[self class] allocWithZone:nil] init];
});
return _sharedInstance;
}
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent {
return YES;
}
- (BOOL)acceptsFirstResponder {
return YES;
}
+ (BOOL)validateMenuItem:(NSMenuItem*)anItem {
return YES;
}
+ (void)showClipboardHidden:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:@"QSPasteboardHistoryIsVisible"] || [[[self sharedInstance] window] isDocked]) {
[[[self sharedInstance] window] orderFrontHidden:sender];
}
}
+ (void)showClipboard:(id)sender {
QSGCDMainAsync(^{
[[[self sharedInstance] window] toggle:sender];
});
}
// saves the state of the shelf window when Quicksilver goes to quit (used on next QS launch - see +loadPlugIn)
+(void)saveVisibilityState:(NSNotification *)notif {
if ([[notif object] isEqualToString:@"QSQuicksilverWillQuitEvent"]) {
BOOL visible = ![[[self sharedInstance] window] hidden];
[[NSUserDefaults standardUserDefaults] setBool:visible forKey:@"QSPasteboardHistoryIsVisible"];
if ([[NSUserDefaults standardUserDefaults] boolForKey:kDiscardPasteboardHistoryOnQuit]) {
// make sure any existing history is overwritten
[[QSLib shelfNamed:@"QSPasteboardHistory"] removeAllObjects];
[QSLib savePasteboardHistory];
}
}
}
- (void)clearStore {
[pasteboardStoreArray removeAllObjects];
for (NSInteger i = 0; i<10; i++) {
[pasteboardStoreArray addObject:[QSNullObject nullObject]];
};
}
- (id)init {
if (self = [super initWithWindowNibName:@"Pasteboard" owner:self]) {
pasteboardHistoryArray = [[QSLibrarian sharedInstance] shelfNamed:@"QSPasteboardHistory"];
self.currentArray = pasteboardHistoryArray;
mode = QSPasteboardHistoryMode;
pasteboardStoreArray = [[NSMutableArray alloc] init];
[self clearStore];
pasteboardCacheArray = [[NSMutableArray alloc] init];
// ***warning * if pasteboard is empty, put last copyied item onto it
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pasteboardChanged:) name:QSPasteboardDidChangeNotification object:nil];
if (defaultBool(@"QSClipboardModule/EnableHotKeys") ) {
QSHotKeyEvent *hotKey;
hotKey = (QSHotKeyEvent *)[QSHotKeyEvent getHotKeyForKeyCode:37 character:[@"L" characterAtIndex:0]
modifierFlags:NSCommandKeyMask | NSControlKeyMask];
[hotKey setTarget:self selector:@selector(showHistory:)];
[hotKey setEnabled:YES];
hotKey = (QSHotKeyEvent *)[QSHotKeyEvent getHotKeyForKeyCode:37 character:[@"L" characterAtIndex:0]
modifierFlags:NSCommandKeyMask | NSShiftKeyMask | NSAlternateKeyMask];
[hotKey setTarget:self selector:@selector(showStore:)];
[hotKey setEnabled:YES];
hotKey = (QSHotKeyEvent *)[QSHotKeyEvent getHotKeyForKeyCode:37 character:[@"L" characterAtIndex:0]
modifierFlags:NSCommandKeyMask | NSControlKeyMask | NSAlternateKeyMask];
[hotKey setTarget:self selector:@selector(showQueue:)];
[hotKey setEnabled:YES];
hotKey = (QSHotKeyEvent *)[QSHotKeyEvent getHotKeyForKeyCode:37 character:[@"L" characterAtIndex:0]
modifierFlags:NSCommandKeyMask | NSControlKeyMask | NSShiftKeyMask];
[hotKey setTarget:self selector:@selector(showStack:)];
[hotKey setEnabled:YES];
hotKey = (QSHotKeyEvent *)[QSHotKeyEvent getHotKeyForKeyCode:9 character:[@"V" characterAtIndex:0]
modifierFlags:NSCommandKeyMask | NSControlKeyMask];
[hotKey setTarget:self selector:@selector(qsPaste:)];
[hotKey setEnabled:YES];
}
}
return self;
}
- (IBAction)showHistory:(id)sender {
[self switchToMode:QSPasteboardHistoryMode];
[[self window] show:sender];
}
- (IBAction)showStore:(id)sender {
[self switchToMode:QSPasteboardStoreMode];
[[self window] show:sender];
}
- (IBAction)showQueue:(id)sender {
[self switchToMode:QSPasteboardQueueMode];
[[self window] show:sender];
}
- (IBAction)showStack:(id)sender {
[self switchToMode:QSPasteboardStackMode];
[[self window] show:sender];
}
// used for the 'clip store copy' objects (see the plist)
- (void)copyNumber:(NSNumber *)number {
NSUInteger zeroIndexNumber = [number unsignedIntegerValue];
[pasteboardHistoryTable selectRowIndexes:[NSIndexSet indexSetWithIndex:zeroIndexNumber] byExtendingSelection:NO];
[self copyToClipboard:[self selectedObject]];
}
// used for the 'clip store paste' objects
- (void)pasteNumber:(NSNumber *)number {
[self pasteNumber:[number unsignedIntegerValue] plainText:NO];
}
- (void)pasteNumber:(NSUInteger)number plainText:(BOOL)plainText {
NSIndexSet *rowSet = [NSIndexSet indexSetWithIndex:number];
if (mode == QSPasteboardStoreMode && plainText == YES) {
[pasteboardStoreArray replaceObjectAtIndex:number withObject:[QSObject objectWithPasteboard:[NSPasteboard generalPasteboard]]];
[pasteboardHistoryTable selectRowIndexes:rowSet byExtendingSelection:NO];
} else {
[pasteboardHistoryTable selectRowIndexes:rowSet byExtendingSelection:NO];
[self pasteItem:self];
}
}
- (void)pasteItem:(id)sender {
[[self window] resignKeyWindowNow];
asPlainText = (([[NSApp currentEvent] modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask) == NSEventModifierFlagOption);
[self qsPaste:nil];
[self hideWindow:sender];
[[(QSController *)[NSApp delegate] interfaceController] hideWindows:self];
// scroll to first item
[pasteboardHistoryTable selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
// ***warning * the clipboard should be restored
}
- (IBAction)qsPaste:(id)sender {
QSObject *selectedObject = [self selectedObject];
switch (mode) {
case QSPasteboardHistoryMode:
case QSPasteboardStoreMode:
[self copyToClipboard:selectedObject];
QSForcePaste();
break;
case QSPasteboardQueueMode:
case QSPasteboardStackMode:
if ([pasteboardCacheArray count]) {
id object = (sender ? [pasteboardCacheArray objectAtIndex:0] : selectedObject);
supressCapture = YES;
[self copyToClipboard:object];
QSForcePaste();
if (sender) {
[pasteboardCacheArray removeObjectAtIndex:0];
[pasteboardHistoryTable reloadData];
}
} else {
NSBeep();
}
break;
default:
break;
}
}
- (void)awakeFromNib {
[[self window] addInternalWidgetsForStyleMask:NSUtilityWindowMask closeOnly:NO];
[pasteboardHistoryTable registerForDraggedTypes:standardPasteboardTypes];
[[self window] setLevel:27];
[[self window] setHidesOnDeactivate:NO];
// [self window].styleMask |= NSWindowStyleMaskResizable;
[pasteboardHistoryArray makeObjectsPerformSelector:@selector(loadIcon)];
[[self window] setAutosaveName:@"QSPasteboardHistoryWindow"]; // should use the real methods to do this
//NSImageCell *imageCell = nil;
[pasteboardHistoryTable setVerticalMotionCanBeginDrag: TRUE];
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
CGFloat rowHeight = [def doubleForKey:@"QSPasteboardRowHeight"];
adjustRowsToFit = [def boolForKey:@"QSPasteboardAdjustRowHeight"];
[pasteboardHistoryTable setRowHeight:rowHeight?rowHeight:36];
// [[self window] setNextResponder:self];
[pasteboardHistoryTable setTarget:self];
//[pasteboardHistoryTable setAction:@selector(tableAction:)];
[pasteboardHistoryTable setDoubleAction:@selector(pasteItem:)];
[pasteboardHistoryTable setTarget:self];
[pasteboardHistoryTable setOpaque:YES];
QSObjectCell *objectCell = [[QSObjectCell alloc] init];
if ([objectCell respondsToSelector:@selector(showsRichText)]) {
objectCell.showsRichText = YES;
}
[[pasteboardHistoryTable tableColumnWithIdentifier: @"object"] setDataCell:objectCell];
NSCell *numberCell = [[QSPasteboardAccessoryCell alloc] init];
[[pasteboardHistoryTable tableColumnWithIdentifier: @"sequence"] setDataCell:numberCell];
if ([pasteboardHistoryArray count]) {
NSPasteboard *pboard = [NSPasteboard generalPasteboard];
if (![[pboard types] count]) {
[[pasteboardHistoryArray objectAtIndex:0] putOnPasteboard:pboard];
}
[pasteboardItemView setObjectValue:[pasteboardHistoryArray objectAtIndex:0]];
}
[pasteboardHistoryTable bind:@"backgroundColor"
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.QSAppearance3B"
options:[NSDictionary dictionaryWithObject:NSUnarchiveFromDataTransformerName
forKey:@"NSValueTransformerName"]];
[pasteboardHistoryTable bind:@"highlightColor"
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.QSAppearance3A"
options:[NSDictionary dictionaryWithObject:NSUnarchiveFromDataTransformerName
forKey:@"NSValueTransformerName"]];
[[[pasteboardHistoryTable tableColumnWithIdentifier:@"object"] dataCell] bind:@"textColor"
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.QSAppearance3T"
options:[NSDictionary dictionaryWithObject:NSUnarchiveFromDataTransformerName forKey:@"NSValueTransformerName"]];
[[[pasteboardHistoryTable tableColumnWithIdentifier:@"sequence"] dataCell] bind:@"textColor"
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.QSAppearance3T"
options:[NSDictionary dictionaryWithObject:NSUnarchiveFromDataTransformerName forKey:@"NSValueTransformerName"]];
[pasteboardHistoryTable setGridColor:[[NSColor blackColor] colorWithAlphaComponent:0.1]];
}
- (id)resolveProxyObject:(id)proxy {
NSPasteboard *pboard;
if ([[proxy identifier] isEqualToString:@"QSGeneralPasteboardProxy"]) {
pboard = [NSPasteboard generalPasteboard];
} else {
pboard = [NSPasteboard pasteboardWithName:NSFindPboard];
}
QSObject *newObject = [QSObject objectWithPasteboard:pboard];
return newObject;
}
- (NSArray *)typesForProxyObject:(id)proxy {
return standardPasteboardTypes;
}
// Called when an item should be added to the clipboard history
- (void)pasteboardChanged:(NSNotification*)notif {
QSGCDMainAsync(^{
[self handlePasteboardChanged:notif];
});
}
- (void)handlePasteboardChanged:(NSNotification *)notif {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults boolForKey:kCapturePasteboardHistory]) return;
// don't add the object to the clipboard if the user's whitelisted the app
if ([[defaults arrayForKey:@"clipboardIgnoreApps"] containsObject:[[notif object] objectForKey:@"ActiveApp"]]) {
return;
}
NSInteger maxCount = [[NSUserDefaults standardUserDefaults] integerForKey:kCapturePasteboardHistoryCount];
NSPasteboard *pboard = [[notif object] objectForKey:@"Pasteboard"];
if (![pboard pasteboardItems].count) {
// Pasteboard is empty. Password apps (e.g. 1Password) push an empty item item to the pasteboard
return;
}
// get a new object from the general (system-wide) pasteboard
QSObject *newObject = [QSObject objectWithPasteboard:pboard];
if (!newObject) {
return;
}
// run through the pasteboard history, removing unused (over the max count) objects
while ((NSInteger)[pasteboardHistoryArray count] > maxCount) [pasteboardHistoryArray removeLastObject];
// Store the most 'rich' (has more data types) version of an object on the pasteboard (e.g. a QSObject with RTF data over just plaintext data)
NSIndexSet *existingObjectIndex = [pasteboardHistoryArray indexesOfObjectsPassingTest:^BOOL(QSObject *pasteboardObject, NSUInteger idx, BOOL *stop) {
if([[pasteboardObject stringValue] isEqualToString:[newObject stringValue]]) {
if ([pasteboardObject dataDictionary].count > [newObject dataDictionary].count) {
[[newObject dataDictionary] addEntriesFromDictionary:[pasteboardObject dataDictionary]];
}
*stop = YES;
return YES;
}
return NO;
}];
[pasteboardHistoryArray removeObjectsAtIndexes:existingObjectIndex];
[pasteboardHistoryArray insertObject:newObject atIndex:0];
if (!supressCapture) {
switch (mode) {
case QSPasteboardQueueMode:
[pasteboardCacheArray addObject:newObject];
break;
case QSPasteboardStackMode:
[pasteboardCacheArray insertObject:newObject atIndex:0];
break;
default:
break;
}
}
supressCapture = NO;
id selectedObject = [self selectedObject];
/* Safeguard against weird objects getting in here, like QSNullObject */
if (![selectedObject respondsToSelector:@selector(stringValue)])
selectedObject = nil;
BOOL recievingSelection = [[selectedObject stringValue] isEqualToString:[newObject stringValue]];
if (recievingSelection) {
[pasteboardHistoryTable selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
[pasteboardHistoryTable scrollRowToVisible:0];
} else {
NSUInteger row = [pasteboardHistoryTable selectedRow];
if (row>0) {
if (row+1<[pasteboardHistoryArray count]) {
[pasteboardHistoryTable selectRowIndexes:[NSIndexSet indexSetWithIndex:row+1] byExtendingSelection:NO];
[pasteboardHistoryTable scrollRowToVisible:row+1];
}
else
[pasteboardHistoryTable deselectRow:row];
}
}
[pasteboardHistoryTable reloadData];
if (![[NSUserDefaults standardUserDefaults] boolForKey:kDiscardPasteboardHistoryOnQuit]) {
[QSLib savePasteboardHistory];
}
}
- (IBAction)clearHistory:(id)sender {
switch (mode) {
case QSPasteboardHistoryMode:
if ([pasteboardHistoryArray count]) {
[pasteboardHistoryArray removeObjectsInRange:NSMakeRange(1, [pasteboardHistoryArray count] -1)];
[pasteboardHistoryTable reloadData];
[QSLib savePasteboardHistory];
}
break;
case QSPasteboardStoreMode:
[self clearStore];
break;
case QSPasteboardQueueMode:
case QSPasteboardStackMode:
[pasteboardCacheArray removeAllObjects];
default:
break;
}
[pasteboardHistoryTable reloadData];
}
- (void)deleteBackward:(id)sender {
NSInteger index = [pasteboardHistoryTable selectedRow];
switch (mode) {
case QSPasteboardHistoryMode:
if (index) {
[pasteboardHistoryArray removeObjectAtIndex:index];
[pasteboardHistoryTable reloadData];
if (![[NSUserDefaults standardUserDefaults] boolForKey:kDiscardPasteboardHistoryOnQuit]) {
[QSLib savePasteboardHistory];
}
}
break;
case QSPasteboardStoreMode:
[pasteboardStoreArray replaceObjectAtIndex:index withObject:[QSNullObject nullObject]];
break;
case QSPasteboardQueueMode:
case QSPasteboardStackMode:
[pasteboardCacheArray removeObjectAtIndex:index];
default:
break;
}
[pasteboardHistoryTable reloadData];
}
- (void)tableAction:(id)sender {
}
- (IBAction)hideWindow:(id)sender {
[[self window] saveFrame];
if (![[self window] isDocked] && [[NSUserDefaults standardUserDefaults] boolForKey:@"QSPasteboardController HideAfterPasting"]) {
[[self window] orderOut:self];
} else {
[[self window] hide:self];
}
}
- (id)selectedObject {
NSInteger index = [pasteboardHistoryTable selectedRow];
if (index < 0 || index >= (NSInteger)self.currentArray.count) return nil;
return [self.currentArray objectAtIndex:index];
}
- (void)copyToClipboard:(QSObject *)obj {
if (!asPlainText) {
[obj putOnPasteboard:[NSPasteboard generalPasteboard]];
} else {
[obj putOnPasteboardAsPlainTextOnly:[NSPasteboard generalPasteboard]];
}
}
#pragma mark Key Handling
- (void)keyDown:(NSEvent *)theEvent {
NSString *chars = [theEvent charactersIgnoringModifiers];
static NSArray *keys = nil;
if (keys == nil) {
keys = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9"];
}
if ([keys containsObject:
chars]) {
// switch between human numbering and machine numbering (0/1 start) so -1 from the chars integer value
[self pasteNumber:[chars integerValue] - 1 plainText:([theEvent modifierFlags] & NSAlternateKeyMask)];
return;
}
else if ([chars characterAtIndex:0] == NSCarriageReturnCharacter || [chars characterAtIndex:0] == NSEnterCharacter) {
[self pasteItem:self];
return;
}
[self interpretKeyEvents:[NSArray arrayWithObject:theEvent]];
}
- (BOOL)performKeyEquivalent:(NSEvent *)theEvent {
//NSLog(@"%@", theEvent);
return [pasteboardMenu performKeyEquivalent:(NSEvent *)theEvent];
return NO;
}
- (void)insertNewline:(id)sender {
[self pasteItem:self];
}
# pragma mark Menu Handling
- (IBAction)toggleAdjustRows:(id)sender {
adjustRowsToFit = !adjustRowsToFit;
if (adjustRowsToFit) [self adjustRowHeight];
[[NSUserDefaults standardUserDefaults] setBool:adjustRowsToFit forKey:@"QSPasteboardAdjustRowHeight"];
}
- (IBAction)showPreferences:(id)sender {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"qs://preferences#QSPasteboardPrefPane"]];
}
- (IBAction)setMode:(id)sender {
[self switchToMode:[sender tag]];
}
- (void)switchToMode:(NSUInteger)newMode {
mode = newMode;
switch (mode) {
case QSPasteboardHistoryMode:
[titleField setStringValue:@"Clipboard History"];
self.currentArray = pasteboardHistoryArray;
break;
case QSPasteboardStoreMode:
[titleField setStringValue:@"Clipboard Storage"];
self.currentArray = pasteboardStoreArray;
break;
case QSPasteboardQueueMode:
[titleField setStringValue:@"Clipboard Cache Old"];
[self setCacheIsReversed:YES];
self.currentArray = pasteboardCacheArray;
break;
case QSPasteboardStackMode:
[titleField setStringValue:@"Clipboard Cache New"];
[self setCacheIsReversed:NO];
self.currentArray = pasteboardCacheArray;
default:
break;
}
[pasteboardHistoryTable reloadData];
}
- (void)setCacheIsReversed:(BOOL)reverse {
if (reverse != cacheIsReversed) {
[pasteboardCacheArray reverse];
cacheIsReversed = reverse;
}
}
- (BOOL)validateMenuItem:(NSMenuItem*)anItem {
//NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([anItem action] == @selector(setMode:) ) {
[anItem setState:([anItem tag] == mode)];
return YES;
}
if ([anItem action] == @selector(toggleAdjustRows:) ) {
[anItem setState:adjustRowsToFit];
return YES;
}
return YES;
}
# pragma mark Table Handling
- (NSInteger) numberOfRowsInTableView:(NSTableView *)tableView {
return [self.currentArray count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
if (rowIndex < 0 || rowIndex >= (NSInteger)self.currentArray.count) {
return nil;
}
if ([[aTableColumn identifier] isEqualToString:@"object"])
return [self.currentArray objectAtIndex:rowIndex];
if ([[aTableColumn identifier] isEqualToString:@"sequence"]) {
if (rowIndex < 9) return [NSNumber numberWithInteger:rowIndex+1];
}
return nil;
}
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
//NSLog(@"setCell %d", rowIndex);
if (rowIndex < 0 || rowIndex >= (NSInteger)self.currentArray.count) return;
if ([[aTableColumn identifier] isEqualToString:@"object"]) {
[aCell setRepresentedObject:[self.currentArray objectAtIndex:rowIndex]];
[aCell setState:NSOffState];
}
}
- (void)tableView:(NSTableView *)tableView draggingSession:(NSDraggingSession *)session endedAtPoint:(NSPoint)screenPoint operation:(NSDragOperation)operation {
NSRect windowScreenRect =[self.window convertRectToScreen:self.window.frame];
if (!NSPointInRect(screenPoint, windowScreenRect)) {
// We dropped outside our window, hide ourselves
[self hideWindow:self];
}
}
- (BOOL)tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard {
if ([rowIndexes count] > 1) return NO;
NSArray *draggedObjects = [self.currentArray objectsAtIndexes:rowIndexes];
if ([draggedObjects[0] isKindOfClass:[QSNullObject class]]) return NO;
_draggedRows = rowIndexes;
[draggedObjects[0] putOnPasteboard:pboard];
return YES;
}
- (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)operation {
switch (mode) {
case QSPasteboardHistoryMode:
if ([info draggingSource] == tableView) return NSDragOperationNone;
[tableView setDropRow:0 dropOperation:NSTableViewDropAbove];
break;
case QSPasteboardStoreMode:
if (operation == NSTableViewDropAbove || [_draggedRows containsIndex:row]) return NSDragOperationNone;
break;
case QSPasteboardQueueMode:
case QSPasteboardStackMode:
if (operation != NSTableViewDropAbove) return NSDragOperationNone;
break;
default:
break;
}
if ([info draggingSource] == tableView)
return NSDragOperationMove;
return NSDragOperationCopy;
}
- (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id <NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation {
BOOL isSelfDrag = ([info draggingSource] == tableView); // Are we currently dragging objects from our contents
NSArray <QSObject *> *objects = nil;
if (isSelfDrag)
objects = [self.currentArray objectsAtIndexes:_draggedRows];
else
/* FIXME: What actually happens if you multi-drag something else in here ? */
objects = @[[QSObject objectWithPasteboard:[info draggingPasteboard]]];
switch (mode) {
case QSPasteboardHistoryMode:
if ([info draggingSource] != tableView) {
for (QSObject *object in objects) {
[object putOnPasteboard:[NSPasteboard generalPasteboard]];
}
}
break;
case QSPasteboardStoreMode:
[pasteboardStoreArray replaceObjectAtIndex:row withObject:objects[0]];
if (isSelfDrag)
[pasteboardStoreArray replaceObjectAtIndex:[_draggedRows firstIndex] withObject:[QSNullObject nullObject]];
break;
case QSPasteboardQueueMode:
case QSPasteboardStackMode:
if (isSelfDrag)
[pasteboardCacheArray moveIndex:[_draggedRows firstIndex] toIndex:row];
else
[pasteboardCacheArray insertObjectsFromArray:objects atIndex:row];
break;
default:
break;
}
[pasteboardHistoryTable reloadData];
// NSLog(@"source %@", [info draggingSource]);
return YES;
}
# pragma mark Window Handling
- (void)adjustRowHeight {
CGFloat height = (NSInteger) (NSHeight([[pasteboardHistoryTable enclosingScrollView] frame])/10-2);
height = MAX(height, 10.0);
[[NSUserDefaults standardUserDefaults] setDouble:height forKey:@"QSPasteboardRowHeight"];
[pasteboardHistoryTable setRowHeight:height];
}
- (void)windowDidResize:(NSNotification *)aNotification {
NSInteger key = [[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask;
if ((adjustRowsToFit || key) && !(adjustRowsToFit && key) )
[self adjustRowHeight];
//if (!adjustRowsToFit && ") ) {
// }
}
@end