-
Notifications
You must be signed in to change notification settings - Fork 14
/
FlaggedItemWindowController.m
372 lines (298 loc) · 7.95 KB
/
FlaggedItemWindowController.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
//
// FlaggedItems.m
// TaskExplorer
//
// Created by Patrick Wardle on 8/14/15.
// Copyright (c) 2015 Objective-See. All rights reserved.
//
#import "KKRow.h"
#import "ItemView.h"
#import "Utilities.h"
#import "AppDelegate.h"
#import "FlaggedItemWindowController.h"
@interface FlaggedItems ()
@end
@implementation FlaggedItems
@synthesize didInit;
@synthesize flaggedItems;
@synthesize flaggedItemTable;
@synthesize vtWindowController;
@synthesize infoWindowController;
//automatically called when nib is loaded
// ->first time (when outlets aren't nil), init UI
-(void)awakeFromNib
{
//single time init
if(YES != self.didInit)
{
//init UI
[self initUI];
//set flag
self.didInit = YES;
}
return;
}
//init/prepare
// ->make sure everything is cleanly init'd before displaying
-(void)prepare
{
//array of flagged tasks
NSMutableArray* flaggedTasks = nil;
//init array for flagged items
flaggedItems = [NSMutableArray array];
//first time outlets are nil
// ->thus 'initUI' method called in 'awakeFromNib'
if(nil != self.window)
{
//can init UI
// ->center window, etc
[self initUI];
//set flag
self.didInit = YES;
}
//populate flagged items array
for(Binary* flaggedItem in taskEnumerator.flaggedItems)
{
//when binary is task binary
// ->find/save all tasks instances
if(YES == flaggedItem.isTaskBinary)
{
//get all tasks instances
flaggedTasks = [taskEnumerator tasksForBinary:flaggedItem];
//sync to save
@synchronized(self.flaggedItems)
{
//save all tasks
[self.flaggedItems addObjectsFromArray:flaggedTasks];
}
}
//when binary is dylib
// ->just add, since will be processed as single item
else
{
//sync to save
@synchronized(self.flaggedItems)
{
//add
[self.flaggedItems addObject:flaggedItem];
}
}
}
//always reload
[self.flaggedItemTable reloadData];
return;
}
//init the UI
// ->each time window is shown, reset, show spinner if needed, etc
-(void)initUI
{
//center
[self.window center];
//table reload
// ->make sure all is reset
[self.flaggedItemTable reloadData];
return;
}
//table delegate
// ->return number of rows, which is just number of items in the currently selected plugin
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
return self.flaggedItems.count;
}
//table delegate method
// ->return cell for row
-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
//row view
NSView* rowView = nil;
//sanity check
// ->make sure there is table item for row
if(row >= self.flaggedItems.count)
{
//bail
goto bail;
}
//create the view
// ->inits row w/ all required info
rowView = createItemView(tableView, self, [self.flaggedItems objectAtIndex:row]);
//bail
bail:
return rowView;
}
//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.flaggedItemTable, 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.flaggedItemTable, theEvent, YES);
return;
}
//invoked when the user clicks 'virus total' icon
// ->launch browser and browse to virus total's page
-(void)showVTInfo:(id)sender
{
//item
id item = nil;
//binary
Binary* binary = nil;
//row
NSInteger itemRow = 0;
//grab sender's row
itemRow = [self.flaggedItemTable rowForView:sender];
//sanity check(s)
// ->make sure row is decent
if( (-1 == itemRow) ||
(itemRow >= self.flaggedItems.count) )
{
//bail
goto bail;
}
//extract item for row
item = self.flaggedItems[itemRow];
//tasks
// ->grab binary
if(YES == [item isKindOfClass:[Task class]])
{
//get binary
binary = ((Task*)item).binary;
}
//binaries
// ->can use as is
else if(YES == [item isKindOfClass:[Binary class]])
{
//set
binary = item;
}
//sanity check
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;
}
//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;
//row
NSInteger itemRow = 0;
//grab sender's row
itemRow = [self.flaggedItemTable rowForView:sender];
//sanity check(s)
// ->make sure row is decent
if( (-1 == itemRow) ||
(itemRow >= self.flaggedItems.count) )
{
//bail
goto bail;
}
//grab item
item = self.flaggedItems[itemRow];
//alloc/init info window
infoWindowController = [[InfoWindowController alloc] initWithItem:item];
//show it
[self.infoWindowController.windowController showWindow:self];
//bail
bail:
return;
}
//automatically invoked when user clicks the 'show in finder' icon
// ->open Finder to show item
-(IBAction)showInFinder:(id)sender
{
//item
// ->task, dylib, or file
id item = nil;
//path to item
NSString* path = nil;
//row
NSInteger itemRow = 0;
//file open error alert
NSAlert* errorAlert = nil;
//grab sender's row
itemRow = [self.flaggedItemTable rowForView:sender];
//sanity check(s)
// ->make sure row is decent
if( (-1 == itemRow) ||
(itemRow >= self.flaggedItems.count) )
{
//bail
goto bail;
}
//extract item for row
item = self.flaggedItems[itemRow];
//tasks
// ->grab path from binary
if(YES == [item isKindOfClass:[Task class]])
{
//get path
path = ((Task*)item).binary.path;
}
//dylib
// ->use as is, to extract path
else if(YES == [item isKindOfClass:[Binary class]])
{
//get path
path = [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;
}
@end