-
Notifications
You must be signed in to change notification settings - Fork 36
/
IGKPreferencesController.m
479 lines (396 loc) · 12.8 KB
/
IGKPreferencesController.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//
// IGKPreferencesController.m
// Ingredients
//
// Created by Alex Gordon on 07/03/2010.
// Written in 2010 by Fileability.
//
#import "IGKPreferencesController.h"
@interface IGKPreferencesController ()
- (void)switchToView:(NSView *)view item:(NSToolbarItem *)toolbarItem animate:(BOOL)animate;
- (void)addDeveloperDirectoryPath:(NSString *)path;
- (void)reloadTableViews;
- (void)saveChanges;
@end
@implementation IGKPreferencesController
@synthesize startIntoDocsets;
- (id)init
{
if (self = [super initWithWindowNibName:@"IGKPreferences"])
{
developerDirectories = [[NSMutableArray alloc] init];
docsets = [[NSMutableArray alloc] init];
[self reloadTableViews];
}
return self;
}
- (void)windowDidLoad
{
[self reloadTableViews];
[self switchToView:startIntoDocsets ? docsetsView : generalView item:generalToolbarItem animate:NO];
if (startIntoDocsets)
{
[[[self window] toolbar] setSelectedItemIdentifier:@"Docsets"];
}
}
- (void)showWindow:(id)sender
{
[[self window] center];
[super showWindow:sender];
}
- (NSManagedObjectContext *)managedObjectContext
{
return [[[NSApp delegate] kitController] managedObjectContext];
}
#pragma mark Tab Switching
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
{
return [NSArray arrayWithObjects:@"General", @"Docsets", @"Updates", nil];
}
//Tab switching
- (IBAction)switchToGeneral:(id)sender
{
[self switchToView:generalView item:generalToolbarItem animate:YES];
}
- (IBAction)switchToDocsets:(id)sender
{
[self switchToView:docsetsView item:docsetsToolbarItem animate:YES];
}
- (IBAction)switchToUpdates:(id)sender
{
[self switchToView:updatesView item:updatesToolbarItem animate:YES];
}
- (void)switchToView:(NSView *)view item:(NSToolbarItem *)toolbarItem animate:(BOOL)animate
{
[[[self window] toolbar] setSelectedItemIdentifier:[toolbarItem itemIdentifier]];
[currentView removeFromSuperview];
[view setFrameOrigin:NSZeroPoint];
[[[self window] contentView] addSubview:view];
currentView = view;
CGFloat borderHeight = [[self window] frame].size.height - [[[self window] contentView] frame].size.height;
NSRect newWindowFrame = [[self window] frame];
newWindowFrame.size.height = [view frame].size.height + borderHeight;
newWindowFrame.origin.y += [[self window] frame].size.height - newWindowFrame.size.height;
[[self window] setFrame:newWindowFrame display:YES animate:animate];
}
- (void)reloadTableViews
{
if ([[NSUserDefaults standardUserDefaults] valueForKey:@"developerDirectories"])
developerDirectories = [[[NSUserDefaults standardUserDefaults] valueForKey:@"developerDirectories"] mutableCopy];
if ([[NSUserDefaults standardUserDefaults] valueForKey:@"docsets"])
docsets = [[[NSUserDefaults standardUserDefaults] valueForKey:@"docsets"] mutableCopy];
[developerDirectoriesTableView reloadData];
[docsetsTableView reloadData];
}
#pragma mark Docsets Logic
- (void)selectedFilterDocsetForPath:(NSString *)path
{
BOOL changedSomething = NO;
for (NSDictionary *docset in [docsets copy])
{
BOOL isDocset = [[docset valueForKey:@"path"] isEqual:path];
if ([[docset valueForKey:@"isSelected"] boolValue] != isDocset)
{
NSDictionary *newDocset = [docset mutableCopy];
[newDocset setValue:[NSNumber numberWithBool:isDocset] forKey:@"isSelected"];
[docsets replaceObjectAtIndex:[docsets indexOfObject:docset] withObject:newDocset];
changedSomething = YES;
}
}
if (changedSomething)
{
[self saveChangesNeedsRelaunch:NO];
[self reloadTableViews];
}
}
- (NSString *)selectedFilterDocsetPath
{
for (NSDictionary *docset in docsets)
{
if ([[docset valueForKey:@"isSelected"] boolValue])
{
return [docset valueForKey:@"path"];
}
}
return nil;
}
- (IBAction)addDeveloperDirectory:(id)sender
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setCanChooseFiles:NO];
[openPanel setCanChooseDirectories:YES];
[openPanel beginWithCompletionHandler:^(NSInteger result) {
if (result != NSFileHandlingPanelOKButton)
return;
NSString *path = [[openPanel URL] path];
//Sanity check path. If it really is a Developer directory, it should have a Library/version.plist
NSString *versionPlistPath = [path stringByAppendingPathComponent:@"Library/version.plist"];
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:versionPlistPath];
if (!exists)
{
NSBeep();
return;
}
[self addDeveloperDirectoryPath:path];
[self reloadTableViews];
}];
}
- (void)addDeveloperDirectoryPath:(NSString *)path
{
NSMutableDictionary *devDir = [[NSMutableDictionary alloc] init];
[devDir setValue:path forKey:@"path"];
for (NSDictionary *dict in developerDirectories)
{
if ([[dict valueForKey:@"path"] isEqual:path])
return;
}
[developerDirectories addObject:devDir];
[self saveChanges];
}
- (int)addDocsetWithPath:(NSString *)path localizedUserInterfaceName:(NSString *)localizedUserInterfaceName developerDirectory:(NSString *)devDir
{
NSMutableDictionary *docset = [[NSMutableDictionary alloc] init];
[docset setValue:[NSNumber numberWithBool:YES] forKey:@"isEnabled"];
[docset setValue:path forKey:@"path"];
[docset setValue:localizedUserInterfaceName forKey:@"name"];
[docset setValue:devDir forKey:@"developerDirectory"];
for (NSDictionary *dict in docsets)
{
if ([[dict valueForKey:@"path"] isEqual:path])
return ([[dict valueForKey:@"isEnabled"] boolValue] ? 1 : 0);
}
[docsets addObject:docset];
[self saveChanges];
return -1;
}
- (IBAction)removeSelectedDeveloperDirectories:(id)sender
{
NSIndexSet *selectedIndicies = [developerDirectoriesTableView selectedRowIndexes];
if ([selectedIndicies count] >= [developerDirectories count])
{
NSAlert *alert = [NSAlert alertWithMessageText:@"Are you sure you want to remove all developer directories?" defaultButton:@"Remove" alternateButton:@"Don't Remove" otherButton:nil informativeTextWithFormat:@""];
[alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(removeLastAlertDidEnd:returnCode:contextInfo:) contextInfo:nil];
}
else
{
[self removeSelectedDeveloperDirectories];
}
}
- (void)removeLastAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
if (returnCode == NSAlertDefaultReturn)
{
[self removeSelectedDeveloperDirectories];
}
}
- (void)removeSelectedDeveloperDirectories
{
NSIndexSet *selectedIndicies = [developerDirectoriesTableView selectedRowIndexes];
[developerDirectories removeObjectsAtIndexes:selectedIndicies];
[self saveChanges];
[self reloadTableViews];
}
- (void)saveChanges
{
[self saveChangesNeedsRelaunch:NO];
}
- (void)saveChangesNeedsRelaunch:(BOOL)needsRelaunch
{
BOOL devDirsChanged = NO;
if (![[[NSUserDefaults standardUserDefaults] valueForKey:@"developerDirectories"] isEqual:developerDirectories])
devDirsChanged = YES;
BOOL docsetsChanged = NO;
if (![[[NSUserDefaults standardUserDefaults] valueForKey:@"docsets"] isEqual:docsets])
docsetsChanged = YES;
[[NSUserDefaults standardUserDefaults] setValue:developerDirectories forKey:@"developerDirectories"];
[[NSUserDefaults standardUserDefaults] setValue:docsets forKey:@"docsets"];
if (needsRelaunch)
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"needsReindex"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
if (needsRelaunch)
{
if (devDirsChanged || docsetsChanged)
[[docsetsChangedStatusView animator] setHidden:NO];
}
}
- (IBAction)relaunch:(id)sender
{
Class SUUpdaterClass = NSClassFromString(@"SUUpdater");
if (SUUpdaterClass == Nil)
return;
// Copy the relauncher into a temporary directory so we can get to it after the new version's installed.
NSString *relaunchPath = nil;
NSString *relaunchPathToCopy = [[NSBundle bundleForClass:SUUpdaterClass] pathForResource:@"relaunch" ofType:@""];
if (![relaunchPathToCopy length])
return;
NSString *targetPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[relaunchPathToCopy lastPathComponent]];
if (![targetPath length])
return;
// Only the paranoid survive: if there's already a stray copy of relaunch there, we would have problems.
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:targetPath error:nil];
if ([[NSFileManager defaultManager] copyItemAtPath:relaunchPathToCopy toPath:targetPath error:&error])
relaunchPath = [targetPath retain];
if (!relaunchPath || ![[NSFileManager defaultManager] fileExistsAtPath:relaunchPath])
{
NSBeep();
return;
}
NSString *pathToRelaunch = [[NSBundle mainBundle] bundlePath];
[NSTask launchedTaskWithLaunchPath:relaunchPath arguments:[NSArray arrayWithObjects:pathToRelaunch, [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], nil]];
[NSApp terminate:self];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
if (tableView == developerDirectoriesTableView)
{
return [developerDirectories count];
}
else if (tableView == docsetsTableView)
{
return [docsets count];
}
return 0;
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
if (tableView == developerDirectoriesTableView)
{
NSString *path = [[developerDirectories objectAtIndex:row] valueForKey:@"path"];
if ([[tableColumn identifier] isEqual:@"icon"])
{
//FIXME: We need proper icons for docsets and developer directories
return [[NSWorkspace sharedWorkspace] iconForFile:path];
}
else if ([[tableColumn identifier] isEqual:@"path"])
{
return path;
}
}
else if (tableView == docsetsTableView)
{
NSDictionary *docset = [docsets objectAtIndex:row];
if ([[tableColumn identifier] isEqual:@"isEnabled"])
{
return [docset valueForKey:@"isEnabled"];
}
else if ([[tableColumn identifier] isEqual:@"icon"])
{
return [[NSWorkspace sharedWorkspace] iconForFile:[docset valueForKey:@"path"]];
}
else if ([[tableColumn identifier] isEqual:@"name"])
{
return [docset valueForKey:@"name"];
}
else if ([[tableColumn identifier] isEqual:@"developerDirectory"])
{
return [docset valueForKey:@"developerDirectory"];
}
}
return nil;
}
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
if (tableView == docsetsTableView)
{
if ([[tableColumn identifier] isEqual:@"isEnabled"])
{
NSDictionary *docset = [[docsets objectAtIndex:row] mutableCopy];
[docset setValue:[NSNumber numberWithBool:[object boolValue]] forKey:@"isEnabled"];
[docsets replaceObjectAtIndex:row withObject:docset];
[self saveChangesNeedsRelaunch:YES];
[self reloadTableViews];
}
}
}
/*
- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row
{
//Disallow selection for the docsets table view
if (tableView == docsetsTableView && row != -1)
return NO;
return YES;
}
*/
#pragma mark Updates Logic
- (NSString *)appVersion
{
return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
}
- (IBAction)checkForUpdates:(id)sender
{
[[SUUpdater sharedUpdater] checkForUpdates:sender];
}
- (void)setUpdateMatrixTag:(NSInteger)updateMatrixTag
{
if (updateMatrixTag == 1)
{
[[SUUpdater sharedUpdater] setAutomaticallyChecksForUpdates:YES];
[[SUUpdater sharedUpdater] setAutomaticallyDownloadsUpdates:YES];
}
else if (updateMatrixTag == 2)
{
[[SUUpdater sharedUpdater] setAutomaticallyChecksForUpdates:YES];
[[SUUpdater sharedUpdater] setAutomaticallyDownloadsUpdates:NO];
}
else //if (updateMatrixTag == 3)
{
[[SUUpdater sharedUpdater] setAutomaticallyChecksForUpdates:NO];
[[SUUpdater sharedUpdater] setAutomaticallyDownloadsUpdates:NO];
}
}
- (NSInteger)updateMatrixTag
{
BOOL checks = [[SUUpdater sharedUpdater] automaticallyChecksForUpdates];
BOOL downloads = [[SUUpdater sharedUpdater] automaticallyDownloadsUpdates];
if (checks && downloads)
return 1;
else if (checks && !downloads)
return 2;
else
return 3;
}
#pragma mark Singleton
static IGKPreferencesController *sharedPreferencesController = nil;
+ (IGKPreferencesController *)sharedPreferencesController
{
@synchronized(self) {
if (sharedPreferencesController == nil) {
[[self alloc] init]; // assignment not done here
}
}
return sharedPreferencesController;
}
+ (id)allocWithZone:(NSZone *)zone
{
@synchronized(self) {
if (sharedPreferencesController == nil) {
sharedPreferencesController = [super allocWithZone:zone];
return sharedPreferencesController; // assignment and return on first allocation
}
}
return sharedPreferencesController; //on subsequent allocation attempts return nil
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
- (id)retain
{
return self;
}
- (NSUInteger)retainCount
{
return UINT_MAX; //denotes an object that cannot be released
}
- (void)release
{
//do nothing
}
- (id)autorelease
{
return self;
}
@end