-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathPrefsWindowController.m
More file actions
executable file
·258 lines (199 loc) · 6.51 KB
/
PrefsWindowController.m
File metadata and controls
executable file
·258 lines (199 loc) · 6.51 KB
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
//
// PrefsWindowController.m
// KnockKnock
//
// Created by Patrick Wardle on 2/6/15.
// Copyright (c) 2015 Objective-See, LLC. All rights reserved.
//
#import "utilities.h"
#import "AppDelegate.h"
#import "PrefsWindowController.h"
@implementation PrefsWindowController
@synthesize okButton;
@synthesize disableVTQueries;
@synthesize showTrustedItems;
@synthesize disableUpdateCheck;
//automatically called when nib is loaded
// ->center window
-(void)awakeFromNib
{
//center
[self.window center];
[self.window makeFirstResponder:self.okButton];
}
//automatically invoked when window is loaded
// initialize prefs UI
-(void)windowDidLoad
{
//super
[super windowDidLoad];
//not in dark mode?
// make window white
if(YES != isDarkMode())
{
//make white
self.window.backgroundColor = NSColor.whiteColor;
}
//make button selected
[self.window makeFirstResponder:self.okButton];
//check if 'show trusted items' button should be selected
if(YES == self.showTrustedItems)
{
//set
self.showTrustedItemsBtn.state = NSControlStateValueOn;
}
//check if 'show trusted items' button should be selected
if(YES == self.startAtLogin)
{
//set
self.startAtLoginBtn.state = NSControlStateValueOn;
}
//check if 'disable update check' button should be selected
if(YES == self.disableUpdateCheck)
{
//set
self.disableUpdateCheckBtn.state = NSControlStateValueOn;
}
//check if 'disable vt queries' button should be selected
if(YES == self.disableVTQueries)
{
//set
self.disableVTQueriesBtn.state = NSControlStateValueOn;
}
//VT API key
if(0 != self.vtAPIKey.length) {
//set
self.apiTextField.stringValue = self.vtAPIKey;
}
//make url a hyperlink
makeTextViewHyperlink(self.getAPILink, [NSURL URLWithString:@"https://docs.virustotal.com/docs/please-give-me-an-api-key"]);
return;
}
-(void)windowDidBecomeKey:(NSNotification *)notification {
[self.window makeFirstResponder:self.okButton];
}
//register default prefs
// only used if user hasn't set any
-(void)registerDefaults
{
//set defaults
[[NSUserDefaults standardUserDefaults] registerDefaults:@{PREF_SHOW_TRUSTED_ITEMS:@NO, PREF_START_AT_LOGIN:@NO, PREF_DISABLE_UPDATE_CHECK:@NO, PREF_DISABLE_VT_QUERIRES:@YES}];
return;
}
//load (persistence) preferences from file system
-(void)loadPreferences
{
//user defaults
NSUserDefaults* defaults = nil;
//init
defaults = [NSUserDefaults standardUserDefaults];
//load prefs
// ->won't be any until user set some...
if(nil != defaults)
{
//load 'show trusted items'
if(nil != [defaults objectForKey:PREF_SHOW_TRUSTED_ITEMS])
{
//save
self.showTrustedItems = [defaults boolForKey:PREF_SHOW_TRUSTED_ITEMS];
}
//load 'start at login'
if(nil != [defaults objectForKey:PREF_START_AT_LOGIN])
{
//save
self.startAtLogin = [defaults boolForKey:PREF_START_AT_LOGIN];
}
//load 'disable update check'
if(nil != [defaults objectForKey:PREF_DISABLE_UPDATE_CHECK])
{
//save
self.disableUpdateCheck = [defaults boolForKey:PREF_DISABLE_UPDATE_CHECK];
}
//load 'disable vt queries'
if(nil != [defaults objectForKey:PREF_DISABLE_VT_QUERIRES])
{
//save
self.disableVTQueries = [defaults boolForKey:PREF_DISABLE_VT_QUERIRES];
}
}
//load API key
self.vtAPIKey = loadAPIKeyFromKeychain();
return;
}
//automatically invoked when window is closing
// ->make ourselves unmodal
-(void)windowWillClose:(NSNotification *)notification
{
//save prefs
[self savePrefs];
//make un-modal
[[NSApplication sharedApplication] stopModal];
return;
}
//save prefs
-(void)savePrefs
{
//user defaults
NSUserDefaults* defaults = nil;
//init
defaults = [NSUserDefaults standardUserDefaults];
//grab 'include macOS/known items'
self.showTrustedItems = self.showTrustedItemsBtn.state;
//grab 'start at login' state
self.startAtLogin = self.startAtLoginBtn.state;
//grab 'disable update checks' state
self.disableUpdateCheck = self.disableUpdateCheckBtn.state;
//grab: disable VT state
self.disableVTQueries = self.disableVTQueriesBtn.state;
//grab API key
self.vtAPIKey = self.apiTextField.stringValue;
//save 'show trusted items'
[defaults setBool:self.showTrustedItems forKey:PREF_SHOW_TRUSTED_ITEMS];
//log item state change?
// toggle login item (enable/disable)
if(self.startAtLogin != [defaults boolForKey:PREF_START_AT_LOGIN]) {
//toggle
toggleLoginItem(NSBundle.mainBundle.bundleURL, self.startAtLogin);
}
//now save 'start at login'
[defaults setBool:self.startAtLogin forKey:PREF_START_AT_LOGIN];
//save 'disable update checks'
[defaults setBool:self.disableUpdateCheck forKey:PREF_DISABLE_UPDATE_CHECK];
//save 'disable vt queries'
[defaults setBool:self.disableVTQueries forKey:PREF_DISABLE_VT_QUERIRES];
//save vt API key
saveAPIKeyToKeychain(self.apiTextField.stringValue);
//flush/save
[defaults synchronize];
//call back up into app delegate for filtering/hiding OS components
[((AppDelegate*)[[NSApplication sharedApplication] delegate]) applyPreferences];
return;
}
//show info about API
- (IBAction)showAPIHelp:(id)sender {
//popover
NSPopover *popover = [[NSPopover alloc] init];
//view controller
NSViewController *viewController = [[NSViewController alloc] init];
//set view
viewController.view = self.getAPIHelp;
//make url a hyperlink
makeTextViewHyperlink(self.getAPILinkPopover, [NSURL URLWithString:@"https://docs.virustotal.com/docs/please-give-me-an-api-key"]);
//init
popover.contentViewController = viewController;
popover.behavior = NSPopoverBehaviorTransient; // Closes when you click outside
//show relative to the button
[popover showRelativeToRect:[sender bounds]
ofView:sender
preferredEdge:NSRectEdgeMaxY];
return;
}
//'OK' button handler
// ->save prefs and close window
-(IBAction)closeWindow:(id)sender
{
//close
[self.window close];
return;
}
@end