-
Notifications
You must be signed in to change notification settings - Fork 491
/
Copy pathStatusBarItem.m
417 lines (321 loc) · 10.9 KB
/
StatusBarItem.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
//
// file: StatusBarMenu.m
// project: lulu (login item)
// description: menu handler for status bar icon
//
// created by Patrick Wardle
// copyright (c) 2017 Objective-See. All rights reserved.
//
#import "consts.h"
#import "utilities.h"
#import "Extension.h"
#import "AppDelegate.h"
#import "StatusBarItem.h"
#import "StatusBarPopoverController.h"
/* GLOBALS */
//log handle
extern os_log_t logHandle;
//xpc for daemon comms
extern XPCDaemonClient* xpcDaemonClient;
//menu items
enum menuItems
{
status = 100,
toggle,
rulesShow,
rulesAdd,
rulesExport,
rulesImport,
rulesCleanup,
prefs,
monitor,
quit,
uninstall,
end
};
@implementation StatusBarItem
@synthesize isDisabled;
@synthesize statusItem;
@synthesize rulesMenuController;
//init method
// set some intial flags, init daemon comms, etc.
-(id)init:(NSMenu*)menu preferences:(NSDictionary*)preferences
{
//token
static dispatch_once_t onceToken = 0;
//super
self = [super init];
if(self != nil)
{
//init rules (sub)menu handler
rulesMenuController = [[RulesMenuController alloc] init];
//create item
[self createStatusItem:menu];
//set state based on (existing) preferences
self.isDisabled = [preferences[PREF_IS_DISABLED] boolValue];
//only once
// show popover
dispatch_once(&onceToken, ^{
//parent
NSDictionary* parent = nil;
//get real parent
parent = getRealParent(getpid());
//dbg msg
os_log_debug(logHandle, "(real) parent: %{public}@", parent);
//only show popover if we're not autolaunched
if(YES != [parent[@"CFBundleIdentifier"] isEqualToString:@"com.apple.loginwindow"])
{
//dbg msg
os_log_debug(logHandle, "...user launched, so will show status bar popover");
//show
[self showPopover];
}
});
//set initial menu state
[self setState];
}
return self;
}
//create status item
-(void)createStatusItem:(NSMenu*)menu
{
//init status item
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
//set menu / delegate
self.statusItem.menu = menu;
self.statusItem.menu.delegate = self;
//set handler for each menu item
[self setMenuHandler:menu];
return;
}
//menu 'will open' delegate
// make sure popover is closed
-(void)menuWillOpen:(NSMenu *)menu
{
//make sure to close popover first
if(YES == self.popover.shown)
{
//close
[self.popover performClose:nil];
}
return;
}
//set handler for menu item(s)
-(void)setMenuHandler:(NSMenu*)menu
{
//iterate over all menu items
// add target, enable, and handler for each
for(NSMenuItem* menuItem in menu.itemArray)
{
//set target
menuItem.target = self;
//enable
menuItem.enabled = YES;
//set action, to handler
menuItem.action = @selector(handler:);
//handle sub-menu(s)
if(nil != menuItem.submenu)
{
// Recursively set actions for submenu items
[self setMenuHandler:menuItem.submenu];
}
}
return;
}
//remove status item
-(void)removeStatusItem
{
//remove item
[[NSStatusBar systemStatusBar] removeStatusItem:self.statusItem];
//unset
self.statusItem = nil;
return;
}
//show popver
-(void)showPopover
{
//alloc popover
self.popover = [[NSPopover alloc] init];
//don't want highlight for popover
self.statusItem.button.cell.highlighted = NO;
//set target
self.statusItem.button.target = self;
//set view controller
self.popover.contentViewController = [[StatusBarPopoverController alloc] initWithNibName:@"StatusBarPopover" bundle:nil];
//set behavior
// don't want it close before timeout (unless user clicks '^')
self.popover.behavior = NSPopoverBehaviorApplicationDefined;
//set delegate
self.popover.delegate = self;
//show popover
// have to wait cuz...
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(),
^{
//show
[self.popover showRelativeToRect:self.statusItem.button.bounds ofView:self.statusItem.button preferredEdge:NSMinYEdge];
});
//wait a bit
// then automatically hide popup if user has not closed it
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(),
^{
//still visible?
// close it then...
if(YES == self.popover.shown)
{
//close
[self.popover performClose:nil];
}
//remove action handler
self.statusItem.button.action = nil;
//reset highlight mode
((NSButtonCell*)self.statusItem.button.cell).highlightsBy = NSContentsCellMask | NSChangeBackgroundCellMask;
});
return;
}
//cleanup popover
-(void)popoverDidClose:(NSNotification *)notification
{
//unset
self.popover = nil;
//reset highlight mode
((NSButtonCell*)self.statusItem.button.cell).highlightsBy = NSContentsCellMask | NSChangeBackgroundCellMask;
return;
}
//menu handler
-(void)handler:(id)sender
{
//dbg msg
os_log_debug(logHandle, "handling button click: %{public}@ (%ld)", ((NSButton*)sender).title, ((NSButton*)sender).tag);
//handle user selection
switch(((NSMenuItem*)sender).tag)
{
//toggle
case toggle:
{
//dbg msg
os_log_debug(logHandle, "toggling (%d -> %d)", self.isDisabled, !self.isDisabled);
//invert since toggling
self.isDisabled = !self.isDisabled;
//set menu state
[self setState];
//update prefs
[xpcDaemonClient updatePreferences:@{PREF_IS_DISABLED:[NSNumber numberWithBool:self.isDisabled]}];
//toggle network extension based on (new) state
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
//toggle
[[[Extension alloc] init] toggleNetworkExtension:!self.isDisabled];
});
break;
}
//rules: show
case rulesShow:
//show
[self.rulesMenuController showRules];
break;
//rules: add
case rulesAdd:
//show first
[self.rulesMenuController showRules];
//add
[self.rulesMenuController addRule];
break;
//rules: export
case rulesExport:
//show first
[self.rulesMenuController showRules];
//export
[self.rulesMenuController exportRules];
break;
//rules: import
case rulesImport:
//import
if(YES != [self.rulesMenuController importRules])
{
//show alert
showAlert(NSAlertStyleWarning, NSLocalizedString(@"ERROR: Failed to import rules", @"ERROR: Failed to import rules"), NSLocalizedString(@"See log for (more) details",@"See log for (more) details"), @[NSLocalizedString(@"OK", @"OK")]);
//bail
goto bail;
}
//then show rules
[self.rulesMenuController showRules];
break;
//rules: cleanup
case rulesCleanup:
//cleanup
if([self.rulesMenuController cleanupRules] < 0)
{
//show alert
showAlert(NSAlertStyleWarning, NSLocalizedString(@"ERROR: Failed to cleanup rules", @"ERROR: Failed to cleanup rules"), NSLocalizedString(@"See log for (more) details",@"See log for (more) details"), @[NSLocalizedString(@"OK",@"OK")]);
//bail
goto bail;
}
break;
//prefs
case prefs:
[((AppDelegate*)[[NSApplication sharedApplication] delegate]) showPreferences:sender];
break;
//monitor
// launch netiquette (with lulu args)
case monitor:
{
//path
NSURL* path = nil;
//error
NSError* error = nil;
//init path
path = [NSURL fileURLWithPath:[NSBundle.mainBundle.resourcePath stringByAppendingPathComponent:NETWORK_MONITOR]];
//dbg msg
os_log_debug(logHandle, "launching network monitor (%{public}@)", path);
//launch
// with args
if(nil == [NSWorkspace.sharedWorkspace launchApplicationAtURL:path options:0 configuration:[NSDictionary dictionaryWithObject:@[@"-lulu"] forKey:NSWorkspaceLaunchConfigurationArguments] error:&error])
{
//err msg
os_log_error(logHandle, "ERROR: failed to launch network monitor, %{public}@, (error: %{public}@)", path, error);
}
break;
}
//quit
case quit:
[((AppDelegate*)[[NSApplication sharedApplication] delegate]) quit:sender];
break;
//uninstall
case uninstall:
[((AppDelegate*)[[NSApplication sharedApplication] delegate]) uninstall:sender];
break;
default:
break;
}
bail:
return;
}
//set menu status
// logic based on 'isEnabled' iVar
-(void)setState
{
//dbg msg
os_log_debug(logHandle, "setting state to: %@", (self.isDisabled) ? @"disabled" : @"enabled");
//set to disabled
if(YES == self.isDisabled)
{
//update status
[self.statusItem.menu itemWithTag:status].title = NSLocalizedString(@"LuLu: disabled", @"LuLu: disabled");
//set icon
self.statusItem.button.image = [NSImage imageNamed:@"StatusInactive"];
//change toggle text
[self.statusItem.menu itemWithTag:toggle].title = NSLocalizedString(@"Enable", @"Enable");
}
//set to enabled
else
{
//update status
[self.statusItem.menu itemWithTag:status].title = NSLocalizedString(@"LuLu: enabled", @"LuLu: enabled");
//set icon
self.statusItem.button.image = [NSImage imageNamed:@"StatusActive"];
//change toggle text
[self.statusItem.menu itemWithTag:toggle].title = NSLocalizedString(@"Disable", @"Disable");
}
return;
}
@end