-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDelegate.m
More file actions
executable file
·557 lines (429 loc) · 21.2 KB
/
Copy pathAppDelegate.m
File metadata and controls
executable file
·557 lines (429 loc) · 21.2 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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//
// AppDelegate.m
// SinK3
//
// Created by Andrew on 2/1/13.
// Copyright (c) 2013 Vox Fera. All rights reserved.
//
#import "AppDelegate.h"
#import "AppController.h"
#import "FRAppCommon.h"
#import "FRMIDIInput.h"
#import "TextSlice.h"
#import "SettingsFile.h"
/////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
@implementation AppDelegate
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize managedObjectContext = _managedObjectContext;
//////////////////////////////////////////////////////////////////////
- (IBAction)expandSidebar:(id)sender {
if([[NSUserDefaults standardUserDefaults] boolForKey:@"f_sidebarVisible"]){
[self ensmallen];
}else{
[self embiggen];
}
}
// SETTINGS FILES ////////////////
- (IBAction)refreshSettingsDir:(id)sender {
[SettingsFile refreshSettingsSidebarWithArrayController:_settingsFiles];
}
-(void)saveSettings:(SettingsFile*)sf{
[SettingsFile saveCurrentSettingsToPath:sf.path withDisplayName:sf.name];
}
-(void)deleteSettings:(SettingsFile*)sf{
[SettingsFile deleteSettingsFile:sf.path];
}
- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex
{
if (proposedMinimumPosition < 85)
{
proposedMinimumPosition = 85;
}
return proposedMinimumPosition;
}
- (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMax ofSubviewAt:(NSInteger)dividerIndex
{
if (proposedMax > 700)
{
proposedMax = 700;
}
return proposedMax ;
}
- (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview{
return NO;
}
-(void)doubleClickSettings{
SettingsFile* thisFile = [[(AppDelegate *)[[NSApplication sharedApplication] delegate] settingsFiles] selectedObjects][0];
// This loads the cached version
/*
for(NSString* key in thisFile.settings){
[[NSUserDefaults standardUserDefaults] setValue:[thisFile.settings valueForKey:key] forKey:key];
}
*/
// This loads from disk
[SettingsFile loadSettingsFromPath:thisFile.path];
}
- (IBAction)launchPreferences:(id)sender {
[prefsWindow makeKeyAndOrderFront:self];
[prefsWindow center];
}
// Loads a default textify.settings file
- (IBAction)resetEverything:(id)sender {
NSString *file = [[NSBundle mainBundle] pathForResource:@"defaultSettings" ofType:@"settings"];
[SettingsFile loadSettingsFromPath:file];
}
- (IBAction)FontButton:(id)sender {
NSFont *fontToUse=(NSFont *)[NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] dataForKey:@"fontSelected"]];
//NSFont* fontToUse=[NSFont fontWithName:@"Helvetica" size:20];
NSFontManager * fontManager = [NSFontManager sharedFontManager];
[fontManager setTarget:self];
[fontManager setSelectedFont:fontToUse isMultiple:NO];
[fontManager orderFrontFontPanel:self];
}
- (void)changeFont:(id)sender{
NSFontManager *fontManager = [NSFontManager sharedFontManager];
NSFont *panelFont = [fontManager convertFont:[fontManager selectedFont]];
[panelFont setValue:@"20" forKey:@"size"];
NSData *fontSelected = [NSArchiver archivedDataWithRootObject:panelFont];
[[NSUserDefaults standardUserDefaults] setValue:fontSelected forKey:@"fontSelected"];
[[NSUserDefaults standardUserDefaults] setValue:[(NSFont *)[NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] dataForKey:@"fontSelected"]] familyName] forKey:@"fontRequested"];
}
- (unsigned int)validModesForFontPanel:(NSFontPanel *)fontPanel{
return NSFontPanelFaceModeMask | NSFontPanelCollectionModeMask;// | NSFontPanelSizeModeMask;
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSString *file = [[NSBundle mainBundle] pathForResource:@"default_prefs" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:file];
[preferences registerDefaults:dict];
// MIDI controller
[[FRAppCommon sharedFRAppCommon] setMidiConfigController:_midiConfigController];
midiInput = [[FRMIDIInput alloc] init];
if([[NSUserDefaults standardUserDefaults] valueForKey:@"f_sidebarVisible"]){
[self embiggen];
}else{
[self ensmallen];
}
// Settings file list
[self refreshSettingsDir:self];
}
-(void)applicationWillTerminate:(NSNotification *)notification{
//[AppController restoreResolution];
[self saveSliceFile];
}
- (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:
(NSTabViewItem *)tabViewItem{
int requestedIndex = (int)[tabView indexOfTabViewItem:tabViewItem];
if (requestedIndex == 0){
}else if (requestedIndex == 1){
int selectionIndex = (int)[[[(AppDelegate *)[[NSApplication sharedApplication] delegate] slicedTextCollection] selectionIndexes] firstIndex]; int arrayCount = (int)[[[(AppDelegate *)[[NSApplication sharedApplication] delegate] slicedText] arrangedObjects] count];
if(selectionIndex < 0 || arrayCount < selectionIndex || arrayCount==0){
//[AppController alertUser:@"Not Available" info:@"To activate TextSlicer mode, expand and drag a text file onto the TextSlicer to the right."];
//return NO;
}
}else if (requestedIndex == 2){
if([[[NSUserDefaults standardUserDefaults] valueForKey:@"externalFilename"] length] ==0){
[AppController alertUser:@"Not Available" info:@"To activate, drag a text file onto the display window below."];
return NO;
}
}
return YES;
}
-(void)awakeFromNib{
// Do we have a slicer to load?
[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"isSlicerAvailable"];
if ([[NSUserDefaults standardUserDefaults] valueForKey:@"textSliceFilename"]){
[(AppDelegate *)[[NSApplication sharedApplication] delegate] loadSliceFileFromPath:[[NSUserDefaults standardUserDefaults] valueForKey:@"textSliceFilename"]];
[self embiggen];
}else{
[[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"textSliceFilenameWithoutPath"];
[[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"textSliceFilename"];
[self ensmallen];
}
}
-(void)embiggen{
dispatch_async(dispatch_get_main_queue(), ^{
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"f_sidebarVisible"];
NSRect frame = [_window frame];
frame.size = NSMakeSize(1024,789);
[_window setFrame:frame display:YES animate:YES];
});
}
-(void)ensmallen{
dispatch_async(dispatch_get_main_queue(), ^{
[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"f_sidebarVisible"];
NSRect frame = [_window frame];
frame.size = NSMakeSize(640,789);
[_window setFrame:frame display:YES animate:YES];
});
}
// Button: Select a destination for logfiles
- (IBAction)selectSettingsFolder:(id)sender {
NSOpenPanel* dlg =[NSOpenPanel openPanel];
[dlg setPrompt:@"Choose"];
[dlg setCanChooseFiles:NO];
[dlg setCanChooseDirectories:YES];
[dlg runModal];
NSString *path = [[[dlg URLs] objectAtIndex:0] path];
[[NSUserDefaults standardUserDefaults] setValue:path forKey:@"settingsDirectory"];
[SettingsFile refreshSettingsSidebarWithArrayController:_settingsFiles];
}
- (IBAction)toggleSidebar:(id)sender {
if ([(NSButton *)sender state ] == NSOnState){
[self embiggen];
}else{
[self ensmallen];
}
}
- (IBAction)saveSliceFileButton:(id)sender {
NSLog(@"Save slice file");
[(AppDelegate *)[[NSApplication sharedApplication] delegate] saveSliceFile];
}
// Save slices to disk
-(void)saveSliceFile{
// For each slice
NSString *outputFileAsString;
for(TextSlice* thisSlice in [[(AppDelegate *)[[NSApplication sharedApplication] delegate] slicedText] arrangedObjects]){
// If we have text
if ([thisSlice displayText]){
NSString* thisSliceText=[thisSlice displayText];
// Aggregate lines
outputFileAsString = [NSString stringWithFormat:@"%@%@\r\n\r\n",outputFileAsString?outputFileAsString:@"",thisSliceText];
}
}
// Write to disk
[outputFileAsString writeToFile:[[NSUserDefaults standardUserDefaults] valueForKey:@"textSliceFilename"] atomically:NO encoding:NSUTF8StringEncoding error:nil];
}
// Load file
-(void)loadSliceFileFromPath:(NSString*)path{
[self loadSliceFileFromPath:path restoringIndexTo:0];
}
-(void)loadSliceFileFromPath:(NSString*)path restoringIndexTo:(int)selectionIndex{
// Clear the array
NSRange range = NSMakeRange(0, [[[(AppDelegate *)[[NSApplication sharedApplication] delegate] slicedText] arrangedObjects] count]);
[[(AppDelegate *)[[NSApplication sharedApplication] delegate] slicedText] removeObjectsAtArrangedObjectIndexes:[NSIndexSet indexSetWithIndexesInRange:range]];
// Split the file on newlines
NSString *contents = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *splitContents = [contents componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]];
// Progress bar
[self setCancelLoad:FALSE];
[self setProgressAmount:0.0];
// Grab the blocks between newlines (we do it this way so it should work with all kinds of newlines)
NSMutableArray* arrayOfNewSlices=[[NSMutableArray alloc] init];
int sliceCount=(int)[splitContents count];
//NSLog(@"Loading %i chunks...",sliceCount);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *blockAggregator;
int chunkCount=0;
for(NSString* textChunk in splitContents){
// Cancel?
if(_cancelLoad){break;}
// If we hit a blank line, save previous as a block
if([[textChunk stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0){
// Add new object
if ([[blockAggregator stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] != 0){
TextSlice *newSlice=[[TextSlice alloc] init];
newSlice.displayText=blockAggregator;
[arrayOfNewSlices addObject:newSlice];
}
blockAggregator=@"";
// Otherwise just aggregate
}else{
// Aggregate
if ([[blockAggregator stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] != 0){
blockAggregator = [NSString stringWithFormat:@"%@\n%@",blockAggregator,textChunk];
}else{
blockAggregator = textChunk;
}
// Edgecase... EOF with no newline after
if([splitContents lastObject] == textChunk){
TextSlice *newSlice=[[TextSlice alloc] init];
//Eliminate blank lines from each slice
NSArray *splitContents = [blockAggregator componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]];
NSString *cleanedOutput=[[NSString alloc] init];
for(NSString* line in splitContents){
if([line length] > 0){
cleanedOutput=[NSString stringWithFormat:@"%@\n%@",cleanedOutput,line];
}
}
newSlice.displayText=cleanedOutput;
if(newSlice.displayText){
[arrayOfNewSlices addObject:newSlice];
}
}
}
self.progressAmount=(double)(((double)chunkCount/(double)sliceCount)*100);
chunkCount++;
}
// Callback run on main thread
dispatch_async(dispatch_get_main_queue(), ^{
if(_cancelLoad){
[self setCancelLoad:FALSE];
[self setIsLoading:false];
[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"isSlicerAvailable"];
[arrayOfNewSlices removeAllObjects];
}else{
[[(AppDelegate *)[[NSApplication sharedApplication] delegate] slicedText] addObjects:arrayOfNewSlices];
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"isSlicerAvailable"];
[self setIsLoading:false];
// Restore selection index
NSArrayController* ac = [(AppDelegate *)[[NSApplication sharedApplication] delegate] slicedText];
//NSLog(@"Attempting to restore to: %i of %li",selectionIndex,(unsigned long)[[ac arrangedObjects] count]);
if([[ac arrangedObjects] count] >= selectionIndex){
[ac setSelectionIndex:selectionIndex];
}else{
[ac setSelectionIndex:0];
}
}
});
});
}
-(IBAction)watchfile_clear:(id)sender {
[[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"displayText"];
[[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"externalFilename"];
[[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"textSliceFilenameWithoutPath"];
[[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"textSliceFilename"];
[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"isSlicerAvailable"];
NSRange range = NSMakeRange(0, [[_slicedText arrangedObjects] count]);
[_slicedText removeObjectsAtArrangedObjectIndexes:[NSIndexSet indexSetWithIndexesInRange:range]];
}
///////////////////////////////////////////////////////////////////////
// APPLICATION
///////////////////////////////////////////////////////////////////////
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)application {
return YES;
}
// Returns the directory the application uses to store the Core Data store file. This code uses a directory named "andrewsempere.org.SinK3" in the user's Application Support directory.
- (NSURL *)applicationFilesDirectory
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *appSupportURL = [[fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] lastObject];
return [appSupportURL URLByAppendingPathComponent:@"andrewsempere.org.SinK3"];
}
// Creates if necessary and returns the managed object model for the application.
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"SinK3" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
// Returns the persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. (The directory for the store is created, if necessary.)
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator) {
return _persistentStoreCoordinator;
}
NSManagedObjectModel *mom = [self managedObjectModel];
if (!mom) {
NSLog(@"%@:%@ No model to generate a store from", [self class], NSStringFromSelector(_cmd));
return nil;
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *applicationFilesDirectory = [self applicationFilesDirectory];
NSError *error = nil;
NSDictionary *properties = [applicationFilesDirectory resourceValuesForKeys:@[NSURLIsDirectoryKey] error:&error];
if (!properties) {
BOOL ok = NO;
if ([error code] == NSFileReadNoSuchFileError) {
ok = [fileManager createDirectoryAtPath:[applicationFilesDirectory path] withIntermediateDirectories:YES attributes:nil error:&error];
}
if (!ok) {
[[NSApplication sharedApplication] presentError:error];
return nil;
}
} else {
if (![properties[NSURLIsDirectoryKey] boolValue]) {
// Customize and localize this error.
NSString *failureDescription = [NSString stringWithFormat:@"Expected a folder to store application data, found a file (%@).", [applicationFilesDirectory path]];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:failureDescription forKey:NSLocalizedDescriptionKey];
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:101 userInfo:dict];
[[NSApplication sharedApplication] presentError:error];
return nil;
}
}
NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"SinK3.storedata"];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
if (![coordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error]) {
[[NSApplication sharedApplication] presentError:error];
return nil;
}
_persistentStoreCoordinator = coordinator;
return _persistentStoreCoordinator;
}
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:@"Failed to initialize the store" forKey:NSLocalizedDescriptionKey];
[dict setValue:@"There was an error building up the data file." forKey:NSLocalizedFailureReasonErrorKey];
NSError *error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
[[NSApplication sharedApplication] presentError:error];
return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;
}
// Returns the NSUndoManager for the application. In this case, the manager returned is that of the managed object context for the application.
- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window
{
return [[self managedObjectContext] undoManager];
}
// Performs the save action for the application, which is to send the save: message to the application's managed object context. Any encountered errors are presented to the user.
- (IBAction)saveAction:(id)sender
{
NSError *error = nil;
if (![[self managedObjectContext] commitEditing]) {
NSLog(@"%@:%@ unable to commit editing before saving", [self class], NSStringFromSelector(_cmd));
}
if (![[self managedObjectContext] save:&error]) {
[[NSApplication sharedApplication] presentError:error];
}
}
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender{
// Save changes in the application's managed object context before the application terminates.
if (!_managedObjectContext) {
return NSTerminateNow;
}
if (![[self managedObjectContext] commitEditing]) {
NSLog(@"%@:%@ unable to commit editing to terminate", [self class], NSStringFromSelector(_cmd));
return NSTerminateCancel;
}
if (![[self managedObjectContext] hasChanges]) {
return NSTerminateNow;
}
NSError *error = nil;
if (![[self managedObjectContext] save:&error]) {
// Customize this code block to include application-specific recovery steps.
BOOL result = [sender presentError:error];
if (result) {
return NSTerminateCancel;
}
NSString *question = NSLocalizedString(@"Could not save changes while quitting. Quit anyway?", @"Quit without saves error question message");
NSString *info = NSLocalizedString(@"Quitting now will lose any changes you have made since the last successful save", @"Quit without saves error question info");
NSString *quitButton = NSLocalizedString(@"Quit anyway", @"Quit anyway button title");
NSString *cancelButton = NSLocalizedString(@"Cancel", @"Cancel button title");
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:question];
[alert setInformativeText:info];
[alert addButtonWithTitle:quitButton];
[alert addButtonWithTitle:cancelButton];
NSInteger answer = [alert runModal];
if (answer == NSAlertAlternateReturn) {
return NSTerminateCancel;
}
}
return NSTerminateNow;
}
@end