-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathApplicationController.m
361 lines (300 loc) · 13.2 KB
/
ApplicationController.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
/*
* Copyright (C) 2005 - 2020 Stephen F. Booth <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#import "ApplicationController.h"
#import "ServicesProvider.h"
#import "PreferencesController.h"
#import "FileConversionController.h"
#import "AcknowledgmentsController.h"
#import "ComponentVersionsController.h"
#import "MediaController.h"
#import "RipperController.h"
#import "EncoderController.h"
#import "LogController.h"
#import "FormatsController.h"
#import "FileFormatNotSupportedException.h"
#import "CoreAudioUtilities.h"
#import "UtilityFunctions.h"
#import "BooleanArrayValueTransformer.h"
#import "ImageDimensionsValueTransformer.h"
#import "NegateBooleanArrayValueTransformer.h"
#import "MultiplicationValueTransformer.h"
#import "BOOLToStringValueTransformer.h"
#import "UppercaseStringValueTransformer.h"
static ApplicationController *sharedController = nil;
@implementation ApplicationController
+ (void) initialize
{
// Set up the ValueTransformers
NSValueTransformer *transformer;
NSString *defaultsValuesPath;
NSDictionary *defaultsValuesDictionary;
transformer = [[[BooleanArrayValueTransformer alloc] init] autorelease];
[NSValueTransformer setValueTransformer:transformer forName:@"BooleanArrayValueTransformer"];
transformer = [[[ImageDimensionsValueTransformer alloc] init] autorelease];
[NSValueTransformer setValueTransformer:transformer forName:@"ImageDimensionsValueTransformer"];
transformer = [[[NegateBooleanArrayValueTransformer alloc] init] autorelease];
[NSValueTransformer setValueTransformer:transformer forName:@"NegateBooleanArrayValueTransformer"];
transformer = [[[MultiplicationValueTransformer alloc] initWithMultiplier:10] autorelease];
[NSValueTransformer setValueTransformer:transformer forName:@"MultiplyByTenValueTransformer"];
transformer = [[[BOOLToStringValueTransformer alloc] init] autorelease];
[NSValueTransformer setValueTransformer:transformer forName:@"BOOLToStringValueTransformer"];
transformer = [[[UppercaseStringValueTransformer alloc] init] autorelease];
[NSValueTransformer setValueTransformer:transformer forName:@"UppercaseStringValueTransformer"];
@try {
defaultsValuesPath = [[NSBundle mainBundle] pathForResource:@"ApplicationControllerDefaults" ofType:@"plist"];
NSAssert1(nil != defaultsValuesPath, NSLocalizedStringFromTable(@"Your installation of Max appears to be incomplete.", @"Exceptions", @""), @"ApplicationControllerDefaults.plist");
defaultsValuesDictionary = [NSDictionary dictionaryWithContentsOfFile:defaultsValuesPath];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsValuesDictionary];
}
@catch(NSException *exception) {
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:NSLocalizedStringFromTable(@"OK", @"General", @"")];
[alert setMessageText:[NSString stringWithFormat:NSLocalizedStringFromTable(@"An error occurred while initializing the %@ class.", @"Exceptions", @""), @"ApplicationController"]];
[[LogController sharedController] logMessage:[NSString stringWithFormat:NSLocalizedStringFromTable(@"An error occurred while initializing the %@ class.", @"Exceptions", @""), @"ApplicationController"]];
[alert setInformativeText:[exception reason]];
[alert setAlertStyle:NSWarningAlertStyle];
[alert runModal];
}
}
+ (ApplicationController *) sharedController
{
@synchronized(self) {
if(nil == sharedController)
[[self alloc] init];
}
return sharedController;
}
+ (id) allocWithZone:(NSZone *)zone
{
@synchronized(self) {
if(nil == sharedController) {
sharedController = [super allocWithZone:zone];
return sharedController;
}
}
return nil;
}
- (id) copyWithZone:(NSZone *)zone { return self; }
- (id) retain { return self; }
- (NSUInteger) retainCount { return UINT_MAX; /* denotes an object that cannot be released */ }
- (oneway void) release { /* do nothing */ }
- (id) autorelease { return self; }
- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender { return NO; }
- (IBAction) showPreferences:(id)sender { [[PreferencesController sharedPreferences] showWindow:sender]; }
- (IBAction) showAcknowledgments:(id)sender { [[AcknowledgmentsController sharedController] showWindow:sender]; }
- (IBAction) showComponentVersions:(id)sender { [[ComponentVersionsController sharedController] showWindow:sender]; }
- (void) applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSArray *openWindows = nil;
// Setup MediaController to receive DiskAppeared/DiskDisappeared callbacks
[MediaController sharedController];
// Register services
[[NSApplication sharedApplication] setServicesProvider:[[ServicesProvider alloc] init]];
// Show windows that were left open from last time
openWindows = [[NSUserDefaults standardUserDefaults] stringArrayForKey:@"openWindows"];
if(nil != openWindows) {
if([openWindows containsObject:@"Ripper"]) {
[[[RipperController sharedController] window] orderFront:self];
}
if([openWindows containsObject:@"Encoder"]) {
[[[EncoderController sharedController] window] orderFront:self];
}
if([openWindows containsObject:@"Log"]) {
[[[LogController sharedController] window] orderFront:self];
}
if([openWindows containsObject:@"FileConversion"]) {
[[[FileConversionController sharedController] window] orderFront:self];
}
if([openWindows containsObject:@"Formats"]) {
[[[FormatsController sharedController] window] orderFront:self];
}
}
// Log startup
[[LogController sharedController] logMessage:NSLocalizedStringFromTable(@"Max successfully launched", @"Log", @"")];
}
- (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication *) sender
{
NSMutableArray *openWindows = nil;
if([[RipperController sharedController] hasTasks] || [[EncoderController sharedController] hasTasks]) {
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:NSLocalizedStringFromTable(@"OK", @"General", @"")];
[alert addButtonWithTitle:NSLocalizedStringFromTable(@"Cancel", @"General", @"")];
[alert setMessageText:NSLocalizedStringFromTable(@"Do you want to quit while there are tasks in progress?", @"General", @"")];
[alert setInformativeText:NSLocalizedStringFromTable(@"The resulting files will be lost if you quit now.", @"General", @"")];
[alert setAlertStyle:NSWarningAlertStyle];
if(NSAlertSecondButtonReturn == [alert runModal]) {
return NSTerminateCancel;
}
// Remove all tasks
else {
[[RipperController sharedController] stopAllTasks:self];
[[EncoderController sharedController] stopAllTasks:self];
}
}
// Save open windows
openWindows = [NSMutableArray array];
if([[[RipperController sharedController] window] isVisible]) {
[openWindows addObject:@"Ripper"];
}
if([[[EncoderController sharedController] window] isVisible]) {
[openWindows addObject:@"Encoder"];
}
if([[[LogController sharedController] window] isVisible]) {
[openWindows addObject:@"Log"];
}
if([[[FileConversionController sharedController] window] isVisible]) {
[openWindows addObject:@"FileConversion"];
}
if([[[FormatsController sharedController] window] isVisible]) {
[openWindows addObject:@"Formats"];
}
[[NSUserDefaults standardUserDefaults] setObject:openWindows forKey:@"openWindows"];
return NSTerminateNow;
}
- (BOOL) application:(NSApplication *)theApplication openFile:(NSString *)filename
{
NSDocument *document;
NSError *error;
// First try our document types
// [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL fileURLWithPath:filename] display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
// }];
document = [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL fileURLWithPath:filename] display:YES error:&error];
if(nil != document)
return YES;
else if([GetAudioExtensions() containsObject:[[filename pathExtension] lowercaseString]]) {
[self encodeFiles:[NSArray arrayWithObject:filename]];
return YES;
}
return NO;
}
- (IBAction) encodeFile:(id)sender
{
[[FileConversionController sharedController] showWindow:self];
[[FileConversionController sharedController] addFiles:self];
}
- (void) encodeFiles:(NSArray *)filenames
{
[[FileConversionController sharedController] showWindow:self];
for(NSString *filename in filenames)
[[FileConversionController sharedController] addFile:filename];
}
- (IBAction) toggleRipperWindow:(id)sender
{
NSWindow *ripperWindow = [[RipperController sharedController] window];
if([ripperWindow isVisible]) {
[ripperWindow performClose:self];
}
else {
[ripperWindow makeKeyAndOrderFront:self];
}
}
- (IBAction) toggleEncoderWindow:(id)sender
{
NSWindow *encoderWindow = [[EncoderController sharedController] window];
if([encoderWindow isVisible]) {
[encoderWindow performClose:self];
}
else {
[encoderWindow makeKeyAndOrderFront:self];
}
}
- (IBAction) toggleLogWindow:(id)sender
{
NSWindow *logWindow = [[LogController sharedController] window];
if([logWindow isVisible]) {
[logWindow performClose:self];
}
else {
[logWindow makeKeyAndOrderFront:self];
}
}
- (IBAction) toggleFormatsWindow:(id)sender
{
NSWindow *formatsWindow = [[FormatsController sharedController] window];
if([formatsWindow isVisible])
[formatsWindow performClose:self];
else
[formatsWindow makeKeyAndOrderFront:self];
}
- (IBAction) openHomeURL:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://sbooth.org/Max/"]];
}
- (BOOL) validateMenuItem:(NSMenuItem *)item
{
BOOL result = YES;
if(@selector(encodeFile:) == [item action]) {
result = ! [[[FileConversionController sharedController] window] isVisible];
}
return result;
}
@end
#pragma mark Scripting
@implementation NSApplication (ScriptingAdditions)
- (id) handleConvertScriptCommand:(NSScriptCommand *)command
{
id directParameter = [command directParameter];
Class directParameterClass = [directParameter class];
@try {
if([directParameterClass isEqual:[NSURL class]]) {
NSURL *url = (NSURL *)directParameter;
if([url isFileURL]) {
[[ApplicationController sharedController] encodeFiles:[NSArray arrayWithObject:[url path]]];
}
}
else if([directParameterClass isEqual:[NSArray class]]) {
NSArray *urlArray;
NSURL *url;
NSMutableArray *filenamesArray;
urlArray = (NSArray *)directParameter;
filenamesArray = [NSMutableArray arrayWithCapacity:[urlArray count]];
for(url in urlArray) {
if([url isFileURL]) {
[filenamesArray addObject:[url path]];
}
}
[[ApplicationController sharedController] encodeFiles:filenamesArray];
}
}
@catch(FileFormatNotSupportedException *exception) {
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:NSLocalizedStringFromTable(@"OK", @"General", @"")];
[alert setMessageText:[NSString stringWithFormat:NSLocalizedStringFromTable(@"An error occurred while opening the file \"%@\" for conversion.", @"Exceptions", @""), [[exception userInfo] objectForKey:@"filename"]]];
[[LogController sharedController] logMessage:[NSString stringWithFormat:NSLocalizedStringFromTable(@"An error occurred while opening the file \"%@\" for conversion.", @"Exceptions", @""), [[exception userInfo] objectForKey:@"filename"]]];
[alert setInformativeText:[exception reason]];
[alert setAlertStyle:NSWarningAlertStyle];
[alert runModal];
}
@catch(NSException *exception) {
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:NSLocalizedStringFromTable(@"OK", @"General", @"")];
if(nil != [exception userInfo] && nil != [[exception userInfo] objectForKey:@"filename"]) {
[alert setMessageText:[NSString stringWithFormat:NSLocalizedStringFromTable(@"An error occurred while opening the file \"%@\" for conversion.", @"Exceptions", @""), [[exception userInfo] objectForKey:@"filename"]]];
[[LogController sharedController] logMessage:[NSString stringWithFormat:NSLocalizedStringFromTable(@"An error occurred while opening the file \"%@\" for conversion.", @"Exceptions", @""), [[exception userInfo] objectForKey:@"filename"]]];
}
else {
[alert setMessageText:NSLocalizedStringFromTable(@"An error occurred during file conversion.", @"Exceptions", @"")];
[[LogController sharedController] logMessage:NSLocalizedStringFromTable(@"An error occurred during file conversion.", @"Exceptions", @"")];
}
[alert setInformativeText:[exception reason]];
[alert setAlertStyle:NSWarningAlertStyle];
[alert runModal];
}
return nil;
}
@end