forked from jason-tratta/QAutoSaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreferenceController.m
416 lines (276 loc) · 10.1 KB
/
PreferenceController.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
//
// PreferenceController.m
// QAutoSaver
//
// Created by Jason Tratta 2/4/09.
// Copyright 2009 Sound Character. All rights reserved.
//
#import "PreferenceController.h"
NSString * const JATRestoreDefaultsNotification = @"RestoreDefaults";
NSString * const JATBackUpTextKey = @"BackUpText";
NSString * const JATBackUpTextChangeNotification = @"BackUpTextChanged";
NSString * const JATSavePathKey = @"SavePathControl";
NSString * const JATBackUpCheckKey = @"BackupCheckBox";
NSString * const JATTimeCheckKey = @"TimeCheckBox";
NSString * const JATSaveUnModKey = @"ModToggle";
NSString * const JATDateBoxState = @"DateBoxState";
NSString * const JATDateBoxString = @"DateBoxString";
@implementation PreferenceController
@synthesize path;
@synthesize stringName;
@synthesize workspaceName;
@synthesize capturedPath;
@synthesize savePath;
@synthesize modState;
// Register the preferance defaults
+(void) initialize
{
//Create a dictionary
NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
//Create the defaults
NSString *defaultBackUpText = @"_BACKUP";
NSString *defaultSavePath = @"file://localhost/";
NSString *defaultBackupCheckBox = @"1";
NSString *defaultTimeCheckBox = @"1";
NSString *defaultSaveModBox = @"1";
NSString *defaultDateBoxState =@"0";
NSString *defaultDateString =@"_MMddYY_HHmm";
//Put the defaults in the dictionary
[defaultValues setObject:defaultBackUpText forKey:JATBackUpTextKey];
[defaultValues setObject:defaultSavePath forKey:JATSavePathKey];
[defaultValues setObject:defaultBackupCheckBox forKey:JATBackUpCheckKey];
[defaultValues setObject:defaultTimeCheckBox forKey:JATTimeCheckKey];
[defaultValues setObject:defaultSaveModBox forKey:JATSaveUnModKey];
[defaultValues setObject:defaultDateBoxState forKey:JATDateBoxState];
[defaultValues setObject:defaultDateString forKey:JATDateBoxString];
//Register the Dictionary of defualts
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
NSLog(@"registed defaults: %@", defaultValues);
}
-(id)init
{
[super init];
//Build Array for TimeStamp Data Source
times = [NSArray arrayWithObjects: @"_MMddYY_HHmm", @"_MM_dd_YY", @"_MMdd_HHmm", @"_MM-dd-hh-mm", @"_dd-hh-mm", nil];
return self;
}
-(void)awakeFromNib
{
//Get and set all user pref fields
[self setBackUpTextField];
[self setBackupCheckBox];
[self setTimeCheckBox];
[self setPathControl];
[self setSaveModBox];
[self setDateComboBox];
}
#pragma mark BackUp Text Methods
-(void)setBackUpTextField
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *backUpString = [defaults objectForKey:JATBackUpTextKey];
[backUpTextField setStringValue:backUpString];
NSLog(@"BackupString is %@", backUpString);
NSLog(@"Field Says %@", [backUpTextField stringValue]);
}
-(NSString *) backUpTextField
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *backUpString = [defaults objectForKey:JATBackUpTextKey];
return backUpString;
}
//When the user types in a textfield, update the pref file
- (void)controlTextDidChange:(NSNotification *)JATBackUpTextChangeNotification
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *backUpString = [backUpTextField stringValue];
[defaults setObject:backUpString forKey:JATBackUpTextKey];
[self setBackUpTextField];
}
#pragma mark Restore defaults
// Restores the default preferances
-(IBAction)restorePrefDefaults: (id) sender
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:JATBackUpTextKey];
[defaults removeObjectForKey:JATSavePathKey];
[defaults removeObjectForKey:JATBackUpCheckKey];
[defaults removeObjectForKey:JATTimeCheckKey];
[defaults removeObjectForKey:JATSaveUnModKey];
[defaults removeObjectForKey:JATDateBoxState];
[defaults removeObjectForKey:JATDateBoxString];
[self setBackUpTextField];
[self setBackupCheckBox];
[self setTimeCheckBox];
[self setSaveModBox];
[self setDateComboBox];
[self setPathControl];
[nc postNotificationName:(NSString *) JATRestoreDefaultsNotification object:self];
}
#pragma mark CheckBox Checking
-(IBAction)backupCheckBoxToggle: (id) sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int i = [backUpTextOptionCheckBox state];
NSString *backUpBoxState = [[NSNumber numberWithInt:i] stringValue];
[defaults setObject:backUpBoxState forKey:JATBackUpCheckKey];
}
-(IBAction)dateCheckBoxToggle: (id) sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int i = [dateTextOptionCheckBox state];
NSString *dateBoxState = [[NSNumber numberWithInt:i] stringValue];
[defaults setObject:dateBoxState forKey:JATTimeCheckKey];
NSLog(@"Date CheckBox Changed");
}
-(void)setBackupCheckBox
{
int i;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *backUpCheck = [defaults objectForKey:JATBackUpCheckKey];
i = [backUpCheck intValue];
[backUpTextOptionCheckBox setState:i];
}
-(void)setTimeCheckBox
{
int i;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *timeCheck = [defaults objectForKey:JATTimeCheckKey];
i = [timeCheck intValue];
[dateTextOptionCheckBox setState:i];
}
-(void)setSaveModBox
{
int i;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *modCheck = [defaults objectForKey:JATSaveUnModKey];
i = [modCheck intValue];
[saveUnmodifiedBox setState:i];
}
-(IBAction)saveUnModBoxToggle: (id) sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int i = [saveUnmodifiedBox state];
NSString *modBoxState = [[NSNumber numberWithInt:i] stringValue];
[defaults setObject:modBoxState forKey:JATSaveUnModKey];
}
-(int)getSaveUnmodifiedBoxState
{
int i;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *modCheck = [defaults objectForKey:JATSaveUnModKey];
i = [modCheck intValue];
return i;
}
-(int) getBackupCheckBoxState
{
int i;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *boxCheck = [defaults objectForKey:JATBackUpCheckKey];
i = [boxCheck intValue];
return i;
}
-(int) getDateCheckBoxState
{
int i;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *boxCheck = [defaults objectForKey:JATTimeCheckKey];
i = [boxCheck intValue];
return i;
}
#pragma mark Path Control / Save Window
-(IBAction) pathControl: (id) sender
{
[self pathControl];
}
-(void) pathControl
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *pathString;
// Values to recongifure NSOpenPanel
NSString *title = @"Select Path";
NSString *message = @"Select or create a directory. Tags will be added to the filename based on your preferences";
NSString *choose = @"Choose";
int spaceName;
// Recongifure the standard NSOpenPanel
save = [NSOpenPanel openPanel]; // Opens the open panel
[save setTitle:title]; // Sets the Tile of the window to select path
[save setCanCreateDirectories:YES]; //Allows user to create new folders
[save setMessage:message]; //Explanation text above the save text box
[save setPrompt:choose]; //Changes the default save button to say "Choose"
[save setCanCreateDirectories:YES];
[save setCanChooseDirectories:YES];
[save setCanChooseFiles:NO];
[save setDelegate:self];
spaceName = [save runModalForTypes:nil]; //Inits the default file name
if (spaceName == NSOKButton){
path = [save URL];
savePath = [save URL];
pathString = [savePath absoluteString];
[pathController setURL:path];
NSLog(@"Path is %@", path);
[defaults setObject:pathString forKey:JATSavePathKey];
}
}
-(void)setPathControl
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *pathString = [defaults objectForKey:JATSavePathKey];
NSURL *pathURL = [NSURL URLWithString:pathString];
path = pathURL;
[pathController setURL:path];
}
-(NSString *)getSavePath
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *pathString = [defaults objectForKey:JATSavePathKey];
NSString *returnString, *returnStringPassOne;
//NSFileManager in the MoveFiles Method does not work when passed file:// This removes it from the string
returnStringPassOne = [pathString stringByReplacingOccurrencesOfString:@"file://localhost" withString:@""];
returnString = [returnStringPassOne stringByReplacingOccurrencesOfString:@"%20" withString:@" "];
NSLog(@"ReturnString is %@", returnString);
return returnString;
}
#pragma mark Date Stamps
-(void)setDateComboBox
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *selectionString = [defaults objectForKey:JATDateBoxState];
int i;
i = [selectionString intValue];
NSLog(@"Save Pref int is %i", i);
[timeStampSelector selectItemAtIndex:i];
}
-(NSString *)getDateString
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *dateString = [defaults objectForKey:JATDateBoxString];
return dateString;
}
- (void)comboBoxSelectionDidChange:(NSNotification *)notification
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int i = [timeStampSelector indexOfSelectedItem];
NSString *dateSelectionState = [[NSNumber numberWithInt:i] stringValue];
[defaults setObject:dateSelectionState forKey:JATDateBoxState];
NSString *stringValue = [times objectAtIndex:i];
[defaults setObject:stringValue forKey:JATDateBoxString];
NSLog(@"Item Selected is %i", i);
NSString *test = [defaults objectForKey:JATDateBoxString];
NSLog(@"SavedString is %@", test);
[self setDateComboBox];
}
// Combo box data source methods
- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox
{
return [times count];
}
- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index
{
return [times objectAtIndex:index];
}
- (NSUInteger)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)string {
return [times indexOfObject: string];
}
@end