-
Notifications
You must be signed in to change notification settings - Fork 13
/
TaskTableController.m
908 lines (727 loc) · 20.2 KB
/
TaskTableController.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
//
// ItemTableController.m
// TaskExplorer
//
// Created by Patrick Wardle on 2/18/15.
// Copyright (c) 2015 Objective-See. All rights reserved.
//
#import "KKRow.h"
#import "Binary.h"
#import "Consts.h"
#import "ItemBase.h"
#import "ItemView.h"
#import "VTButton.h"
#import "kkRowCell.h"
#import "Utilities.h"
#import "AppDelegate.h"
#import "TaskTableController.h"
#import "InfoWindowController.h"
@implementation TaskTableController
@synthesize itemView;
@synthesize isFiltered;
@synthesize tableItems;
@synthesize selectedRow;
@synthesize isBottomPane;
@synthesize filteredItems;
@synthesize ignoreSelection;
@synthesize vtWindowController;
@synthesize infoWindowController;
@synthesize didInit;
-(void)awakeFromNib
{
//single time init
if(YES != self.didInit)
{
//init selected row
self.selectedRow = 0;
//alloc array for filtered items
filteredItems = [NSMutableArray array];
//extand tree view
if(YES == [self.itemView isKindOfClass:[NSOutlineView class]])
{
//expand
[(NSOutlineView*)self.itemView expandItem:nil expandChildren:YES];
}
//set flag
self.didInit = YES;
}
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;
//tasks
OrderedDictionary* tasks = nil;
//for top pane
// ->use tasks from enumerator
if(YES != self.isBottomPane)
{
//when not filtered
// ->use all tasks
if(YES != isFiltered)
{
//get tasks
tasks = taskEnumerator.tasks;
//set count
rows = tasks.count;
}
//when filtered
// ->use filtered items
else
{
//set count
rows = self.filteredItems.count;
}
}
//bottom pane uses 'tableItems' iVar
else
{
//when not filtered
// ->use all items
if(YES != isFiltered)
{
//set row count
rows = self.tableItems.count;
}
//when filtered
// ->use filtered items
else
{
//set count
rows = self.filteredItems.count;
}
}
return rows;
}
//automatically invoked when user selects row
// ->only care about for top pane, trigger load bottom view
-(void)tableViewSelectionDidChange:(NSNotification *)notification
{
//only handle user selections
// ->not those from 'reloadData'
if(YES != self.ignoreSelection)
{
//handle selection
[self handleRowSelection];
}
return;
}
//table delegate method
// ->return cell for row
-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
//tasks
OrderedDictionary* tasks = nil;
//item obj
// ->contains data for view
id item = nil;
//row view
NSView* rowView = nil;
//for TOP PANE
// ->use tasks from enumerator or filtered items
if(YES != self.isBottomPane)
{
//when not filtered
// ->use tasks
if(YES != isFiltered)
{
//grab tasks
tasks = taskEnumerator.tasks;
//sanity check
// ->make sure there is table item for row
if(tasks.count <= row)
{
//bail
goto bail;
}
//get task object
// ->by index to get key, then by key
item = tasks[[tasks keyAtIndex:row]];
}
//when filtered
// ->use filtered items
else
{
//sanity check
// ->make sure there is table item for row
if(self.filteredItems.count <= row)
{
//bail
goto bail;
}
//get task object
item = self.filteredItems[row];
}
}
//for BOTTOM PANE
else
{
//when not filtered
// ->use all items
if(YES != isFiltered)
{
//sanity check
// ->make sure there is table item for row
if(self.tableItems.count <= row)
{
//bail
goto bail;
}
//grab item
// ->dylib/file/network item
item = [self.tableItems objectAtIndex:row];
}
//when filtered
// ->use filtered items
else
{
//sanity check
// ->make sure there is table item for row
if(self.filteredItems.count <= row)
{
//bail
goto bail;
}
//set count
item = [self.filteredItems objectAtIndex:row];
}
}
//create custom item view
if(nil != item)
{
//create
rowView = createItemView(tableView, self, item);
}
return rowView;
//bail
bail:
return nil;
}
//automatically invoked
// ->create custom (sub-classed) NSTableRowView
-(NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
{
//row view
KKRow* rowView = nil;
//row ID
static NSString* const kRowIdentifier = @"TableRowView";
//try grab existing row view
rowView = [tableView makeViewWithIdentifier:kRowIdentifier owner:self];
//make new if needed
if(nil == rowView)
{
//create new
// ->size doesn't matter
rowView = [[KKRow alloc] initWithFrame:NSZeroRect];
//set row ID
rowView.identifier = kRowIdentifier;
}
return rowView;
}
//automatically invoked when mouse entered
// ->highlight button
-(void)mouseEntered:(NSEvent*)theEvent
{
//mouse entered
// ->highlight (visual) state
buttonAppearance(self.itemView, theEvent, NO);
return;
}
//automatically invoked when mouse exits
// ->unhighlight/reset button
-(void)mouseExited:(NSEvent*)theEvent
{
//mouse exited
// ->so reset button to original (visual) state
buttonAppearance(self.itemView, theEvent, YES);
return;
}
//scroll back up to top of table
-(void)scrollToTop
{
//scroll if more than 1 row
if([self.itemView numberOfRows] > 0)
{
//top
[self.itemView scrollRowToVisible:0];
}
}
//reload table
-(void)reloadTable
{
//reload table
[self.itemView reloadData];
//scroll to top
[self scrollToTop];
return;
}
//custom reload
// ->ensures selected row remains selected
-(void)refresh
{
//tasks
OrderedDictionary* tasks = nil;
//selected task
Task* selectedTask = nil;
//task index after reload
NSUInteger taskIndex = 0;
//grab tasks
tasks = taskEnumerator.tasks;
//get task
selectedTask = [self taskForRow:nil];
//ignore selection change though
self.ignoreSelection = YES;
//always reload
[self.itemView reloadData];
//don't ignore selection
self.ignoreSelection = NO;
//when an item was selected
// ->get its index and make sure that's still selected
if(nil != selectedTask)
{
//get task's index
// ->flat view, can do a straight lookup
if(YES != [self.itemView isKindOfClass:[NSOutlineView class]])
{
//get index
taskIndex = [tasks indexOfKey:selectedTask.pid];
}
//get task's index
// ->outline view, use 'rowForItem' method
else
{
taskIndex = [(NSOutlineView*)self.itemView rowForItem:selectedTask];
}
//(re)select task's row
// ->but only if task still exists (e.g. didn't exit)
if(NSNotFound != taskIndex)
{
//begin updates
[self.itemView beginUpdates];
//(re)select
[self.itemView selectRowIndexes:[NSIndexSet indexSetWithIndex:taskIndex] byExtendingSelection:NO];
//end updates
[self.itemView endUpdates];
}
}
/*
//otherwise select first row
else
{
//selected row cell
NSTableCellView* rowView = nil;
//default to first row
self.selectedRow = 0;
//sanity check
if(0 == [self numberOfRowsInTableView:self.itemView])
{
//bail
goto bail;
}
//get first row
rowView = [self.itemView viewAtColumn:0 row:0 makeIfNecessary:YES];
//extract task
// ->pid of task is view's id :)
Task* task = tasks[[NSNumber numberWithInteger:(rowView.tag - PID_TAG_DELTA)]];
//save task
((AppDelegate*)[[NSApplication sharedApplication] delegate]).currentTask = task;
//reload bottom pane
[((AppDelegate*)[[NSApplication sharedApplication] delegate]) selectBottomPaneContent:nil];
}
*/
//bail
bail:
return;
}
//grab a task at a row
-(Task*)taskForRow:(id)sender
{
//index of row
NSInteger taskRow = 0;
//selected row cell
NSTableCellView* rowView = nil;
//tasks
OrderedDictionary* tasks = nil;
//task
Task* task = nil;
//grab tasks
tasks = taskEnumerator.tasks;
//use sender if provided
if(nil != sender)
{
//grab row
taskRow = [self.itemView rowForView:sender];
}
//otherwise use selected row
else
{
//grab row
taskRow = [self.itemView selectedRow];
}
//sanity check(s)
// ->make sure row is decent
if( (-1 == taskRow) ||
((YES != self.isFiltered) && (tasks.count < taskRow)) ||
((YES == self.isFiltered) && (self.filteredItems.count < taskRow)) )
{
//bail
goto bail;
}
//get row that's about to be selected
rowView = [self.itemView viewAtColumn:0 row:taskRow makeIfNecessary:YES];
//extract task
// ->pid of task is view's id :)
task = tasks[[NSNumber numberWithInteger:(rowView.tag - PID_TAG_DELTA)]];
//bail
bail:
return task;
}
//grab an item at a row
-(ItemBase*)itemForRow:(id)sender
{
//index of row
NSInteger itemRow = 0;
//item
ItemBase* item = nil;
//grab row
itemRow = [self.itemView rowForView:sender];
//sanity check(s)
// ->make sure row has item
if( (-1 == itemRow) ||
((YES != self.isFiltered) && (self.tableItems.count < itemRow)) ||
((YES == self.isFiltered) && (self.filteredItems.count < itemRow)) )
{
//bail
goto bail;
}
//when not filtered
// ->just grab item
if(YES != self.isFiltered)
{
//get item
item = self.tableItems[itemRow];
}
//when filtered
// ->grab from filtered item
else
{
//get item
item = self.filteredItems[itemRow];
}
//bail
bail:
return item;
}
//automatically invoked when user clicks the 'show in finder' icon
// ->open Finder to show item
-(IBAction)showInFinder:(id)sender
{
//item
// ->task, dylib, file, etc
id item = nil;
//path to binary
NSString* path = nil;
//file open error alert
NSAlert* errorAlert = nil;
//for top pane
// ->get task
if(YES != self.isBottomPane)
{
//get task
item = [self taskForRow:sender];
//get path
path = ((Task*)item).binary.path;
}
//bottom pane
else
{
//get item
item = [self itemForRow:sender];
//get path
path = ((Binary*)item).path;
}
//sanity check
if(nil == path)
{
//bail
goto bail;
}
//open item in Finder
// ->error alert shown if file open fails
if(YES != [[NSWorkspace sharedWorkspace] selectFile:path inFileViewerRootedAtPath:@""])
{
//alloc/init alert
errorAlert = [NSAlert alertWithMessageText:[NSString stringWithFormat:@"ERROR:\nfailed to open %@", path] defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:@"errno value: %d", errno];
//show it
[errorAlert runModal];
}
//bail
bail:
return;
}
//automatically invoked when user clicks the 'info' icon
// ->create/configure/display info window for task/dylib/file/etc
-(IBAction)showInfo:(id)sender
{
//item
// ->task, dylib, file, etc
id item = nil;
//for top pane
// ->get task
if(YES != self.isBottomPane)
{
//get task
item = [self taskForRow:sender];
}
//bottom pane
else
{
//get item
item = [self itemForRow:sender];
}
//alloc/init info window
infoWindowController = [[InfoWindowController alloc] initWithItem:item];
//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:(id)sender
{
//item
// ->task, dylib, file, etc
Binary* binary = nil;
//for top pane
// ->get binary from task
if(YES != self.isBottomPane)
{
//get task's binary
binary = [(Task*)[self taskForRow:sender] binary];
}
//bottom pane
// ->straight assign
else
{
//get item
binary = (Binary*)[self itemForRow:sender];
}
//bail on nil binaries
if(nil == binary)
{
//bail
goto bail;
}
//alloc/init info window
vtWindowController = [[VTInfoWindowController alloc] initWithItem:binary];
//show it
[self.vtWindowController.windowController showWindow:self];
//bail
bail:
return;
}
//OUTLINE VIEW STUFFZ
-(NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
//tasks
OrderedDictionary* tasks = nil;
//grab tasks
tasks = taskEnumerator.tasks;
//number of children
NSUInteger numberOfChildren = 0;
//when item is nil
// ->count is number of root items children
if(nil == item)
{
//kids
numberOfChildren = ((Task*)[tasks objectForKey:@0]).children.count;
}
//otherwise
// ->give number of item's kids
else
{
//kids
numberOfChildren = [((Task*)item).children count];
}
return numberOfChildren;
}
-(BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
//only no for leafs
// ->items w/o kids
if( (nil != item) &&
(0 == [[item children] count]) )
{
return NO;
}
else
{
return YES;
}
//return !item ? YES : [[item children] count] != 0;
}
//return child
-(id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
//tasks
OrderedDictionary* tasks = nil;
//task
Task* task = nil;
//grab all tasks
tasks = taskEnumerator.tasks;
//root item
if(nil == item)
{
//root
task = [tasks objectForKey:@0];
}
//non-root items
// ->return *their* child!
else
{
task = [tasks objectForKey:[(Task*)item children][index]];
}
return task;
}
//automatically ccalled when row is selected in outline view
// ->invoke helper function to handle selection
-(void)outlineViewSelectionDidChange:(NSNotification *)notification
{
//handle selection
[self handleRowSelection];
return;
}
//table delegate method
// ->return cell for row
-(NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
return createItemView(outlineView, self, (Task*)item);
}
//automatically invoked
// ->create custom (sub-classed) NSTableRowView
-(NSTableRowView *)outlineView:(NSOutlineView *)outlineView rowViewForItem:(id)item
{
//row view
KKRow* rowView = nil;
//row ID
static NSString* const kRowIdentifier = @"OutlineRowView";
//try grab existing row view
rowView = [outlineView makeViewWithIdentifier:kRowIdentifier owner:self];
//make new if needed
if(nil == rowView)
{
//create new
// ->size doesn't matter
rowView = [[KKRow alloc] initWithFrame:NSZeroRect];
//set row ID
rowView.identifier = kRowIdentifier;
}
return rowView;
}
/* LOGIC FOR BOTH */
//handle when user clicks row
// ->update bottom pane w/ task's dylibs/files/etc
-(void)handleRowSelection
{
//tasks
__block OrderedDictionary* tasks = nil;
//task
Task* task = nil;
//newly selected row (index)
__block NSInteger newlySelectedRow = -1;
//selected row cell
NSTableCellView* selectedView = nil;
//ignore events for bottom-pane
if(YES == self.isBottomPane)
{
//ignore
goto bail;
}
//grab tasks
tasks = taskEnumerator.tasks;
//get index of newly selected row
newlySelectedRow = [self.itemView selectedRow];
//sanity check
if( (-1 == newlySelectedRow) ||
((YES != self.isFiltered) && (newlySelectedRow >= tasks.count)) ||
((YES == self.isFiltered) && (newlySelectedRow >= self.filteredItems.count)) )
{
//bail
goto bail;
}
//get view that's about to be selected
selectedView = [self.itemView viewAtColumn:0 row:newlySelectedRow makeIfNecessary:YES];
//extract task
// ->pid of task is view's id :)
task = tasks[[NSNumber numberWithInteger:(selectedView.tag - PID_TAG_DELTA)]];
//check if task is dead
if(YES != isAlive([task.pid intValue]))
{
//make it red
((kkRowCell*)selectedView).color = [NSColor redColor];
//draw
[selectedView setNeedsDisplay:YES];
//make hidden it after .33 second
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.33 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//reset color
((kkRowCell*)selectedView).color = nil;
//remove task from task emumerator
[taskEnumerator removeTask:task];
//when filtering
// ->remove deceased task from filter tasks
if(YES == isFiltered)
{
//remove
[self.filteredItems removeObject:task];
}
//reload table
[self.itemView reloadData];
//sanity check
// ->make sure removed task wasn't last
if(tasks.count <= newlySelectedRow)
{
//reset to (new) last
newlySelectedRow = tasks.count - 1;
}
//sanity check
// ->reset to first item on other errors
else if(-1 == newlySelectedRow)
{
//set to first
newlySelectedRow = 0;
}
//begin updates
[self.itemView beginUpdates];
//re-select
[self.itemView selectRowIndexes:[NSIndexSet indexSetWithIndex:newlySelectedRow] byExtendingSelection:NO];
//end updates
[self.itemView endUpdates];
//reload bottom pane
[((AppDelegate*)[[NSApplication sharedApplication] delegate]) selectBottomPaneContent:nil];
});
//bail
goto bail;
}
//ignore if row selection and task didn't change
if( (newlySelectedRow == self.selectedRow) &&
(((AppDelegate*)[[NSApplication sharedApplication] delegate]).currentTask == task) )
{
//ignore
goto bail;
}
//save newly selected row index
self.selectedRow = [self.itemView selectedRow];
//save currently selected task
((AppDelegate*)[[NSApplication sharedApplication] delegate]).currentTask = task;
//reload bottom pane
[((AppDelegate*)[[NSApplication sharedApplication] delegate]) selectBottomPaneContent:nil];
bail:
;
return;
}
@end