-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathItemTableController.m
executable file
·945 lines (738 loc) · 29.2 KB
/
ItemTableController.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
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
//
// ItemTableController.m
// KnockKnock
//
// Created by Patrick Wardle on 2/18/15.
// Copyright (c) 2015 Objective-See. All rights reserved.
//
#import "File.h"
#import "Consts.h"
#import "Command.h"
#import "VTButton.h"
#import "Extension.h"
#import "Utilities.h"
#import "AppDelegate.h"
#import "Results/ItemBase.h"
#import "ItemTableController.h"
#import "InfoWindowController.h"
#import <AppKit/AppKit.h>
@implementation ItemTableController
@synthesize noItemsLabel;
@synthesize itemTableView;
@synthesize vtWindowController;
@synthesize infoWindowController;
//invoked automatically while nib is loaded
// ->note: outlets are nil here...
-(id)init
{
self = [super init];
if(nil != self)
{
;
}
return self;
}
//prevent table rows from being highlightable
-(void)awakeFromNib
{
//disable highlighting
[self.itemTableView setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];
return;
}
//table delegate
// ->return number of rows, which is just number of items in the currently selected plugin
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
//rows
NSUInteger rows = 0;
//plugin object
PluginBase* selectedPluginObj = nil;
//set selected plugin
selectedPluginObj = ((AppDelegate*)[[NSApplication sharedApplication] delegate]).selectedPlugin;
//invoke helper function to get array
// ->then grab count
rows = [[self getTableItems] count];
//no items have been found?
// display 'not found' msg
if( (0 == rows) &&
(nil != selectedPluginObj) )
{
//first time
// ->alloc/init it
if(nil == self.noItemsLabel)
{
//alloc
noItemsLabel = [[NSTextField alloc] init];
//no border
self.noItemsLabel.bordered = NO;
//no background color
self.noItemsLabel.backgroundColor = [NSColor clearColor];
//font
self.noItemsLabel.font = [NSFont fontWithName:@"Menlo-Regular" size:13];
//center text
self.noItemsLabel.alignment = NSCenterTextAlignment;
//make uneditable
self.noItemsLabel.editable = NO;
//use auto-layout
self.noItemsLabel.translatesAutoresizingMaskIntoConstraints = NO;
//add to table
[self.itemTableView addSubview:noItemsLabel];
//set width constraint
[self.noItemsLabel addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[noItemsLabel(==300)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(noItemsLabel)]];
//set height constraint
[self.noItemsLabel addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[noItemsLabel(==20)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(noItemsLabel)]];
//set top padding constraint
[self.noItemsLabel.superview addConstraint:[NSLayoutConstraint constraintWithItem:self.noItemsLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.noItemsLabel.superview attribute:NSLayoutAttributeTop multiplier:1.0f constant:35.0f]];
//set center constraint
[self.noItemsLabel.superview addConstraint:[NSLayoutConstraint constraintWithItem:self.noItemsLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.noItemsLabel.superview attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
}
//show label
self.noItemsLabel.hidden = NO;
//set string
self.noItemsLabel.stringValue = [NSString stringWithFormat:NSLocalizedString(@"No '%@' found",@"No %@ found"), [selectedPluginObj.name lowercaseString]];
}
//there *are* items
// ->hide label
else
{
//hide
self.noItemsLabel.hidden = YES;
}
return rows;
}
//table delegate method
// ->return cell for row
-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
//item cell
NSTableCellView *itemCell = nil;
//array backing table
// ->based on filtering options, will either be all items, or only unknown ones
NSArray* tableItems = nil;
//plugin item for row
// ->this can be a File, Command, or Extension obj
ItemBase* item = nil;
//string for name
// ->for File objs, can be attributed w/ packed/encrypted
NSMutableAttributedString* customizedItemName = nil;
//signature icon
NSImageView* signatureImageView = nil;
//VT detection ratio
NSString* vtDetectionRatio = nil;
//virus total button
// ->for File objects only...
VTButton* vtButton;
//attribute dictionary
NSMutableDictionary* stringAttributes = nil;
//paragraph style
NSMutableParagraphStyle* paragraphStyle = nil;
//tracking area
NSTrackingArea* trackingArea = nil;
//item path's top padding (constraint)
NSLayoutConstraint* itemPathTopPadding = nil;
//item name's left padding
NSLayoutConstraint* itemNameLeftPadding = nil;
//flag indicating row has tracking area
// ->ensures we don't add 2x
BOOL hasTrackingArea = NO;
//get array backing table
tableItems = [self getTableItems];
//sync
@synchronized (tableItems) {
//sanity check
// ->make sure there is table item for row
if(tableItems.count <= row)
{
//bail
goto bail;
}
//extract plugin item for row
item = tableItems[row];
} //sync
//handle Command items
// basic row, bails when done
if(YES == [item isKindOfClass:[Command class]])
{
//make table cell
itemCell = [tableView makeViewWithIdentifier:@"CommandCell" owner:self];
if(nil == itemCell)
{
//bail
goto bail;
}
//check if cell was previously used (by checking the item name)
// ->if so, set flag to indicated tracking area does not need to be added
if(YES != [itemCell.textField.stringValue isEqualToString:@"Command"])
{
//set flag
hasTrackingArea = YES;
}
//only have to add tracking area once
// ->add it the first time
if(NO == hasTrackingArea)
{
//init tracking area
// ->for 'show' button
trackingArea = [[NSTrackingArea alloc] initWithRect:[[itemCell viewWithTag:TABLE_ROW_SHOW_BUTTON] bounds]
options:(NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways)
owner:self userInfo:@{@"tag":[NSNumber numberWithUnsignedInteger:TABLE_ROW_SHOW_BUTTON]}];
//add tracking area to 'show' button
[[itemCell viewWithTag:TABLE_ROW_SHOW_BUTTON] addTrackingArea:trackingArea];
}
//set text to command
[itemCell.textField setStringValue:((Command*)item).command];
//set detailed text
// ->always item's path
[[itemCell viewWithTag:TABLE_ROW_SUB_TEXT_TAG] setStringValue:item.path];
//all done
goto bail;
}
//make table cell
itemCell = [tableView makeViewWithIdentifier:@"FileCell" owner:self];
if(nil == itemCell)
{
//bail
goto bail;
}
//grab item's name left constraint
itemNameLeftPadding = findConstraint(itemCell, @"itemNameLeftPadding");
//grab item's path top constraint
itemPathTopPadding = findConstraint(itemCell, @"itemPathTopPadding");
//set item's name left padding to default
if(nil != itemNameLeftPadding)
{
//set
itemNameLeftPadding.constant = 23;
}
//set item's path top padding to default
if(nil != itemPathTopPadding)
{
//shift up
itemPathTopPadding.constant = 2;
}
//check if cell was previously used (by checking the item name)
// if so, set flag to indicated tracking area does not need to be added
if(YES != [itemCell.textField.stringValue isEqualToString:@"Item Name"])
{
//set flag
hasTrackingArea = YES;
}
//default color
itemCell.textField.textColor = NSColor.controlTextColor;
//default
// ->hide plist label
[((NSTextField*)[itemCell viewWithTag:TABLE_ROW_PLIST_LABEL]) setHidden:YES];
//set main text
// ->name
itemCell.textField.attributedStringValue = [[NSMutableAttributedString alloc] initWithString:item.name];
//only have to add tracking area once
// ->add it the first time
if(NO == hasTrackingArea)
{
//init tracking area
// ->for 'show' button
trackingArea = [[NSTrackingArea alloc] initWithRect:[[itemCell viewWithTag:TABLE_ROW_SHOW_BUTTON] bounds]
options:(NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways)
owner:self userInfo:@{@"tag":[NSNumber numberWithUnsignedInteger:TABLE_ROW_SHOW_BUTTON]}];
//add tracking area to 'show' button
[[itemCell viewWithTag:TABLE_ROW_SHOW_BUTTON] addTrackingArea:trackingArea];
//init tracking area
// ->for 'info' button
trackingArea = [[NSTrackingArea alloc] initWithRect:[[itemCell viewWithTag:TABLE_ROW_INFO_BUTTON] bounds]
options:(NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways)
owner:self userInfo:@{@"tag":[NSNumber numberWithUnsignedInteger:TABLE_ROW_INFO_BUTTON]}];
//add tracking area to 'info' button
[[itemCell viewWithTag:TABLE_ROW_INFO_BUTTON] addTrackingArea:trackingArea];
}
//get signature image view
signatureImageView = [itemCell viewWithTag:TABLE_ROW_SIGNATURE_ICON];
//set detailed text
// ->path
if(YES == [item isKindOfClass:[File class]])
{
//grab virus total button
// ->need it for frame computations, etc
vtButton = [itemCell viewWithTag:TABLE_ROW_VT_BUTTON];
//set image
// ->app's icon
itemCell.imageView.image = getIconForBinary(((File*)item).path, ((File*)item).bundle);
//set signature icon
signatureImageView.image = getCodeSigningIcon((File*)item);
//show signature icon
signatureImageView.hidden = NO;
//set detailed text
// ->always item's path
[[itemCell viewWithTag:TABLE_ROW_SUB_TEXT_TAG] setStringValue:item.path];
//not editable!
[((NSTextField *)[itemCell viewWithTag:TABLE_ROW_SUB_TEXT_TAG]) setEditable:NO];
//for files w/ plist
// ->show plist
if(nil != ((File*)item).plist)
{
//shift up item path
// ->makes name for plist
if(nil != itemPathTopPadding)
{
//shift up
itemPathTopPadding.constant = -2;
}
//set plist
[((NSTextField*)[itemCell viewWithTag:TABLE_ROW_PLIST_LABEL]) setStringValue:((File*)item).plist];
//show
[((NSTextField*)[itemCell viewWithTag:TABLE_ROW_PLIST_LABEL]) setHidden:NO];
//not editable!
[((NSTextField *)[itemCell viewWithTag:TABLE_ROW_PLIST_LABEL]) setEditable:NO];
}
//configure/show VT info
// ->only if 'disable' preference not set
if(YES != ((AppDelegate*)[[NSApplication sharedApplication] delegate]).prefsWindowController.disableVTQueries)
{
//set button delegate
vtButton.delegate = self;
//save file obj
vtButton.fileObj = ((File*)item);
//check if have vt results
if(nil != ((File*)item).vtInfo)
{
//set font
[vtButton setFont:[NSFont fontWithName:@"Menlo-Bold" size:25]];
//enable
vtButton.enabled = YES;
//got VT results
// ->check 'permalink' to determine if file is known to VT
// then, show ratio and set to red if file is flagged
if(nil != ((File*)item).vtInfo[VT_RESULTS_URL])
{
//alloc paragraph style
paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//center the text
[paragraphStyle setAlignment:NSCenterTextAlignment];
//alloc attributes dictionary
stringAttributes = [NSMutableDictionary dictionary];
//set underlined attribute
stringAttributes[NSUnderlineStyleAttributeName] = @(NSUnderlineStyleSingle);
//set alignment (center)
stringAttributes[NSParagraphStyleAttributeName] = paragraphStyle;
//set font
stringAttributes[NSFontAttributeName] = [NSFont fontWithName:@"Menlo-Bold" size:15];
//compute detection ratio
vtDetectionRatio = [NSString stringWithFormat:@"%lu/%lu", (unsigned long)[((File*)item).vtInfo[VT_RESULTS_POSITIVES] unsignedIntegerValue], (unsigned long)[((File*)item).vtInfo[VT_RESULTS_TOTAL] unsignedIntegerValue]];
//known 'good' files (0 positivies)
// ->(re)set colors
if(0 == [((File*)item).vtInfo[VT_RESULTS_POSITIVES] unsignedIntegerValue])
{
//(re)set title color
itemCell.textField.textColor = NSColor.controlTextColor;
//(re)set color
stringAttributes[NSForegroundColorAttributeName] = NSColor.controlTextColor;
//set string (vt ratio), with attributes
[vtButton setAttributedTitle:[[NSAttributedString alloc] initWithString:vtDetectionRatio attributes:stringAttributes]];
//set color (gray)
stringAttributes[NSForegroundColorAttributeName] = [NSColor grayColor];
//set selected text color
[vtButton setAttributedAlternateTitle:[[NSAttributedString alloc] initWithString:vtDetectionRatio attributes:stringAttributes]];
}
//files flagged by VT
// ->set name and detection to red
else
{
//set title red
itemCell.textField.textColor = [NSColor redColor];
//set color (red)
stringAttributes[NSForegroundColorAttributeName] = [NSColor redColor];
//set string (vt ratio), with attributes
[vtButton setAttributedTitle:[[NSAttributedString alloc] initWithString:vtDetectionRatio attributes:stringAttributes]];
//set selected text color
[vtButton setAttributedAlternateTitle:[[NSAttributedString alloc] initWithString:vtDetectionRatio attributes:stringAttributes]];
}
//enable
[vtButton setEnabled:YES];
}
//file is not known
// ->reset title to '?'
else
{
//set title
[vtButton setTitle:@"?"];
}
}
//no VT results (e.g. unknown file)
else
{
//set font
[vtButton setFont:[NSFont fontWithName:@"Menlo-Bold" size:8]];
//set title
[vtButton setTitle:@"▪ ▪ ▪"];
//disable
vtButton.enabled = NO;
}
//show virus total button
vtButton.hidden = NO;
//show virus total label
[[itemCell viewWithTag:TABLE_ROW_VT_BUTTON+1] setHidden:NO];
}//show VT info (pref not disabled)
//hide VT info
else
{
//hide virus total button
vtButton.hidden = YES;
//hide virus total button label
[[itemCell viewWithTag:TABLE_ROW_VT_BUTTON+1] setHidden:YES];
}
//add 'packed' / 'encrypted' in red
// ->done here since VT stuff (above) sets name globally
if( (YES == ((File*)item).isPacked) ||
(YES == ((File*)item).isEncrypted) )
{
//init task string
customizedItemName = [[NSMutableAttributedString alloc] initWithString:@""];
//add existing name
// ->uses existing color
[customizedItemName appendAttributedString:[[NSMutableAttributedString alloc] initWithString:itemCell.textField.stringValue attributes:@{NSForegroundColorAttributeName:itemCell.textField.textColor}]];
//add '('
// ->color, light gray
[customizedItemName appendAttributedString:[[NSMutableAttributedString alloc] initWithString:@" (" attributes:@{NSForegroundColorAttributeName:[NSColor lightGrayColor]}]];
//add 'encrypted'
// ->green if trusted (apple, etc), otherwise red
if(YES == ((File*)item).isEncrypted)
{
//trusted; green
if(YES == ((File*)item).isTrusted)
{
//add
[customizedItemName appendAttributedString:[[NSAttributedString alloc] initWithString:@"encrypted" attributes:@{NSForegroundColorAttributeName:[NSColor colorWithDeviceRed:38.0f/256.0f green:191.0f/256.0f blue:99.0f/256.0f alpha:1.0f]}]];
}
//untrusted; red
else
{
//add
[customizedItemName appendAttributedString:[[NSAttributedString alloc] initWithString:@"encrypted" attributes:@{NSForegroundColorAttributeName:[NSColor redColor]}]];
}
}
//add 'packed'
// ->can't be both; and encryption takes precedence
else
{
//add
[customizedItemName appendAttributedString:[[NSAttributedString alloc] initWithString:@"packed" attributes:@{NSForegroundColorAttributeName:[NSColor redColor]}]];
}
//close string with ')'
// ->color; light gray
[customizedItemName appendAttributedString:[[NSMutableAttributedString alloc] initWithString:@")" attributes:@{NSForegroundColorAttributeName:[NSColor lightGrayColor]}]];
//update name
itemCell.textField.attributedStringValue = customizedItemName;
}
}//file(s)
//EXTENSIONS
else if(YES == [item isKindOfClass:[Extension class]])
{
//hide signature status
signatureImageView.hidden = YES;
//set item's name left padding back
// ->extensions don't have a signing icon
if(nil != itemNameLeftPadding)
{
//set
itemNameLeftPadding.constant = 8;
}
//for extensions
// ->path should start inline w/ name
//pathFrame.origin.x = 50;
//path should go to info button
//pathFrame.size.width = ((NSTextField*)[itemCell viewWithTag:TABLE_ROW_INFO_BUTTON]).frame.origin.x - pathFrame.origin.x;
//set detailed text
// ->always item's path
[[itemCell viewWithTag:TABLE_ROW_SUB_TEXT_TAG] setStringValue:item.path];
//set image
// ->will be browser's icon
itemCell.imageView.image = getIconForBinary(((Extension*)item).browser, nil);
//hide virus total icon
[[itemCell viewWithTag:TABLE_ROW_VT_BUTTON] setHidden:YES];
//hide virus total label
[[itemCell viewWithTag:TABLE_ROW_VT_BUTTON+1] setHidden:YES];
}//extension(s)
//bail
bail:
return itemCell;
}
//automatically invoked when mouse entered
-(void)mouseEntered:(NSEvent*)theEvent
{
//mouse entered
// ->highlight (visual) state
[self buttonAppearance:theEvent shouldReset:NO];
return;
}
//automaticall invoked when mouse exits
-(void)mouseExited:(NSEvent*)theEvent
{
//mouse exited
// ->so reset button to original (visual) state
[self buttonAppearance:theEvent shouldReset:YES];
return;
}
//set or unset button's highlight
-(void)buttonAppearance:(NSEvent*)theEvent shouldReset:(BOOL)shouldReset
{
//mouse point
NSPoint mousePoint = {0};
//row index
NSUInteger rowIndex = -1;
//current row
NSTableCellView* currentRow = nil;
//tag
NSUInteger tag = 0;
//button
NSButton* button = nil;
//image name
NSString* imageName = nil;
//extract tag
tag = [((NSDictionary*)theEvent.userData)[@"tag"] unsignedIntegerValue];
//restore button back to default (visual) state
if(YES == shouldReset)
{
//set image name
// ->'info'
if(TABLE_ROW_INFO_BUTTON == tag)
{
//set
imageName = @"info";
}
//set image name
// ->'info'
else if(TABLE_ROW_SHOW_BUTTON == tag)
{
//set
imageName = @"show";
}
}
//highlight button
else
{
//set image name
// ->'info'
if(TABLE_ROW_INFO_BUTTON == tag)
{
//set
imageName = @"infoOver";
}
//set image name
// ->'info'
else if(TABLE_ROW_SHOW_BUTTON == tag)
{
//set
imageName = @"showOver";
}
}
//grab mouse point
mousePoint = [self.itemTableView convertPoint:[theEvent locationInWindow] fromView:nil];
//compute row indow
rowIndex = [self.itemTableView rowAtPoint:mousePoint];
//sanity check
if(-1 == rowIndex)
{
//bail
goto bail;
}
//get row that's about to be selected
currentRow = [self.itemTableView viewAtColumn:0 row:rowIndex makeIfNecessary:YES];
//get button
// ->tag id of button, passed in userData var
button = [currentRow viewWithTag:[((NSDictionary*)theEvent.userData)[@"tag"] unsignedIntegerValue]];
//restore default button image
// ->for 'info' and 'show' buttons
if(nil != imageName)
{
//set image
[button setImage:[NSImage imageNamed:imageName]];
}
//bail
bail:
return;
}
//scroll back up to top of table
-(void)scrollToTop
{
//scroll if more than 1 row
if([self.itemTableView numberOfRows] > 0)
{
//top
[self.itemTableView scrollRowToVisible:0];
}
}
//helper function
// ->get items array (either all or just unknown)
-(NSArray*)getTableItems
{
//array backing table
// ->based on filtering options, will either be all items, or only unknown ones
NSArray* tableItems = nil;
//plugin object
PluginBase* selectedPluginObj = nil;
//set selected plugin from app delegate
selectedPluginObj = ((AppDelegate*)[[NSApplication sharedApplication] delegate]).selectedPlugin;
//set array backing table
// ->case: no filtering (i.e., all items)
if(YES == ((AppDelegate*)[[NSApplication sharedApplication] delegate]).prefsWindowController.showTrustedItems)
{
//set count
tableItems = selectedPluginObj.allItems;
}
//set array backing table
// ->case: filtering (i.e., unknown items)
else
{
//set count
tableItems = selectedPluginObj.untrustedItems;
}
return tableItems;
}
//show item in Finder
-(IBAction)showInFinder:(id)sender
{
//array backing table
NSArray* tableItems = nil;
//selected item
// ->will either be a File, Extension, or Command obj
ItemBase* selectedItem = nil;
//index of selected row
NSInteger selectedRow = 0;
//file open error alert
NSAlert* errorAlert = nil;
//grab selected row
selectedRow = [self.itemTableView rowForView:sender];
//grab item table items
tableItems = [self getTableItems];
//sync
@synchronized (tableItems) {
//sanity check
// ->make sure row has item
if(tableItems.count < selectedRow)
{
//bail
goto bail;
}
//extract selected item
selectedItem = tableItems[selectedRow];
}
//open item in Finder
// ->error alert shown if file open fails
if(YES != [[NSWorkspace sharedWorkspace] selectFile:[selectedItem pathForFinder] inFileViewerRootedAtPath:@""])
{
//alloc/init alert
errorAlert = [NSAlert alertWithMessageText:[NSString stringWithFormat:NSLocalizedString(@"ERROR:\nfailed to open %@", @"ERROR:\nfailed to open %@"), [selectedItem pathForFinder]] defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:NSLocalizedString(@"errno value: %d",@"errno value: %d"), errno];
//show it
[errorAlert runModal];
}
//bail
bail:
return;
}
//automatically invoked when user clicks the 'info' icon
// ->create/configure/display info window
-(IBAction)showInfo:(id)sender
{
//array backing table
NSArray* tableItems = nil;
//selected item
// ->will either be a File, Extension, or Command obj
ItemBase* selectedItem = nil;
//index of selected row
NSInteger selectedRow = 0;
//grab selected row
selectedRow = [self.itemTableView rowForView:sender];
//grab item table items
tableItems = [self getTableItems];
//sycn
@synchronized (tableItems) {
//sanity check
// ->make sure row has item
if(tableItems.count < selectedRow)
{
//bail
goto bail;
}
//extract selected item
// ->invoke helper function to get array backing table
selectedItem = tableItems[selectedRow];
}
//alloc/init info window
infoWindowController = [[InfoWindowController alloc] initWithItem:selectedItem];
//show it
[self.infoWindowController.windowController showWindow:self];
//bail
bail:
return;
}
//invoked when the user clicks 'virus total' icon
// ->launch browser and browse to virus total's page
-(void)showVTInfo:(NSView*)button
{
//array backing table
NSArray* tableItems = nil;
//selected item
File* selectedItem = nil;
//row that button was clicked on
NSUInteger rowIndex = -1;
//get row index
rowIndex = [self.itemTableView rowForView:button];
//grab item table items
tableItems = [self getTableItems];
@synchronized (tableItems) {
//sanity check
// ->make sure row has item
if(tableItems.count < rowIndex)
{
//bail
goto bail;
}
//sanity check
if(-1 != rowIndex)
{
//extract selected item
// ->invoke helper function to get array backing table
selectedItem = tableItems[rowIndex];
//alloc/init info window
vtWindowController = [[VTInfoWindowController alloc] initWithItem:selectedItem];
//show it
[self.vtWindowController.windowController showWindow:self];
}
} //sync
bail:
return;
}
//set code signing image
// ->either signed, unsigned, or unknown
NSImage* getCodeSigningIcon(File* binary)
{
//signature image
NSImage* codeSignIcon = nil;
//no signing info or signing error
if( (nil == binary.signingInfo) ||
(nil == binary.signingInfo[KEY_SIGNATURE_STATUS]) ||
(errSecSuccess != [binary.signingInfo[KEY_SIGNATURE_STATUS] intValue]) )
{
//set
codeSignIcon = [NSImage imageNamed:@"unknown"];
}
//apple?
else if(Apple == [binary.signingInfo[KEY_SIGNATURE_SIGNER] intValue])
{
//set
codeSignIcon = [NSImage imageNamed:@"signedAppleIcon"];
}
//signed
else if(errSecSuccess == [binary.signingInfo[KEY_SIGNATURE_STATUS] intValue])
{
//set
codeSignIcon = [NSImage imageNamed:@"signed"];
}
//unsigned
else if(errSecCSUnsigned == [binary.signingInfo[KEY_SIGNATURE_STATUS] intValue])
{
//set
codeSignIcon = [NSImage imageNamed:@"unsigned"];
}
return codeSignIcon;
}
@end