-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathPrefsWindowController.m
executable file
·242 lines (192 loc) · 5.9 KB
/
PrefsWindowController.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
//
// 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 saveOutput;
@synthesize shouldSaveNow;
@synthesize disableVTQueries;
@synthesize showTrustedItems;
@synthesize disableUpdateCheck;
//automatically called when nib is loaded
// ->center window
-(void)awakeFromNib
{
//center
[self.window center];
}
//automatically invoked when window is loaded
// ->set to white
-(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 = STATE_ENABLED;
}
//check if 'disable update check' button should be selected
if(YES == self.disableUpdateCheck)
{
//set
self.disableUpdateCheckBtn.state = STATE_ENABLED;
}
//check if 'disable vt queries' button should be selected
if(YES == self.disableVTQueries)
{
//set
self.disableVTQueriesBtn.state = STATE_ENABLED;
}
//check if 'save output' button should be selected
if(YES == self.saveOutput)
{
//set
self.saveOutputBtn.state = STATE_ENABLED;
}
//capture existing prefs
// ->needed to trigger re-saves
[self captureExistingPrefs];
return;
}
//register default prefs
// only used if user hasn't set any
-(void)registerDefaults
{
//set defaults
[[NSUserDefaults standardUserDefaults] registerDefaults:@{PREF_SHOW_TRUSTED_ITEMS:@NO, PREF_DISABLE_UPDATE_CHECK:@NO, PREF_DISABLE_VT_QUERIRES:@NO, PREF_SAVE_OUTPUT:@NO}];
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 '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 'save output'
if(nil != [defaults objectForKey:PREF_SAVE_OUTPUT])
{
//save
self.saveOutput = [defaults boolForKey:PREF_SAVE_OUTPUT];
}
}
return;
}
//save existing prefs
-(void)captureExistingPrefs
{
//save current state of 'include os/trusted' components
self.showTrustedItems = self.showTrustedItemsBtn.state;
//save current state of 'disable update checks'
self.disableUpdateCheck = self.disableUpdateCheckBtn.state;
//save current state of 'disable VT'
self.disableVTQueries = self.disableVTQueriesBtn.state;
//save current state of 'save' button
self.saveOutput = self.saveOutputBtn.state;
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];
//first, any prefs changed, a 'save' set
// ->set 'save now' flag
if( ((self.showTrustedItems != self.showTrustedItemsBtn.state) ||
(self.disableUpdateCheck != self.disableUpdateCheckBtn.state) ||
(self.disableVTQueries != self.disableVTQueriesBtn.state) ||
(self.saveOutput != self.saveOutputBtn.state) ) &&
(YES == self.saveOutputBtn.state) )
{
//set
self.shouldSaveNow = YES;
}
//don't save
else
{
//unset
self.shouldSaveNow = NO;
}
//save hiding OS components flag
self.showTrustedItems = self.showTrustedItemsBtn.state;
//save current state of 'disable update checks'
self.disableUpdateCheck = self.disableUpdateCheckBtn.state;
//save disabling VT flag
self.disableVTQueries = self.disableVTQueriesBtn.state;
//save save output flag
self.saveOutput = self.saveOutputBtn.state;
//save 'show trusted items'
[defaults setBool:self.showTrustedItems forKey:PREF_SHOW_TRUSTED_ITEMS];
//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 'save output'
[defaults setBool:self.saveOutput forKey:PREF_SAVE_OUTPUT];
//flush/save
[defaults synchronize];
//call back up into app delegate for filtering/hiding OS components
[((AppDelegate*)[[NSApplication sharedApplication] delegate]) applyPreferences];
return;
}
//'OK' button handler
// ->save prefs and close window
-(IBAction)closeWindow:(id)sender
{
//close
[self.window close];
return;
}
@end