2006-10-24 07:15:46 +00:00
|
|
|
//
|
2007-04-27 06:41:34 +00:00
|
|
|
// File: VLAppController.mm - Application wide controller
|
2006-10-24 07:15:46 +00:00
|
|
|
//
|
2007-04-27 06:41:34 +00:00
|
|
|
// Author(s):
|
|
|
|
//
|
|
|
|
// (MN) Matthias Neeracher
|
|
|
|
//
|
|
|
|
// Copyright © 2005-2007 Matthias Neeracher
|
2006-10-24 07:15:46 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "VLAppController.h"
|
|
|
|
#import "VLPitchTransformer.h"
|
|
|
|
#import "VLSoundOut.h"
|
|
|
|
|
|
|
|
@implementation VLAppController
|
|
|
|
|
|
|
|
+ (void)setupDefaults
|
|
|
|
{
|
|
|
|
// load the default values for the user defaults
|
|
|
|
NSString * userDefaultsValuesPath =
|
|
|
|
[[NSBundle mainBundle] pathForResource:@"UserDefaults" ofType:@"plist"];
|
|
|
|
NSDictionary * userDefaultsValuesDict =
|
|
|
|
[NSDictionary dictionaryWithContentsOfFile:userDefaultsValuesPath];
|
|
|
|
|
|
|
|
// set them in the standard user defaults
|
|
|
|
[[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsValuesDict];
|
|
|
|
|
|
|
|
// if your application supports resetting a subset of the defaults to
|
|
|
|
// factory values, you should set those values
|
|
|
|
// in the shared user defaults controller
|
|
|
|
NSArray * resettableUserDefaultsKeys =
|
|
|
|
[NSArray arrayWithObjects:@"VLLowPitch",@"VLHighPitch",nil];
|
|
|
|
NSDictionary * initialValuesDict =
|
|
|
|
[userDefaultsValuesDict dictionaryWithValuesForKeys:resettableUserDefaultsKeys];
|
|
|
|
|
|
|
|
// Set the initial values in the shared user defaults controller
|
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] setInitialValues:initialValuesDict];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)setupTransformers
|
|
|
|
{
|
|
|
|
VLPitchTransformer * pitchTransformer;
|
|
|
|
|
|
|
|
// create an autoreleased instance of our value transformer
|
|
|
|
pitchTransformer = [[[VLPitchTransformer alloc] init] autorelease];
|
|
|
|
|
|
|
|
// register it with the name that we refer to it with
|
|
|
|
[NSValueTransformer setValueTransformer:pitchTransformer
|
|
|
|
forName:@"VLPitchTransformer"];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)initialize
|
|
|
|
{
|
|
|
|
[self setupDefaults];
|
|
|
|
[self setupTransformers];
|
|
|
|
}
|
|
|
|
|
2006-10-26 08:21:18 +00:00
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
if (self = [super init]) {
|
|
|
|
toolPath = nil;
|
|
|
|
appPath = nil;
|
|
|
|
}
|
2006-10-28 09:18:55 +00:00
|
|
|
return self;
|
2006-10-26 08:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*)getLineFromCommand:(NSString*)command
|
|
|
|
{
|
2006-11-10 08:54:00 +00:00
|
|
|
char line[1000];
|
|
|
|
FILE * output = popen([command UTF8String], "r");
|
|
|
|
NSString * outLine = nil;
|
|
|
|
for (int attempts=0; attempts<5; ++attempts)
|
|
|
|
if (fgets(line, 1000, output)) {
|
|
|
|
size_t len = strlen(line);
|
|
|
|
if (len && line[len-1]=='\n') {
|
|
|
|
line[len-1] = 0;
|
|
|
|
outLine = [NSString stringWithUTF8String:line];
|
|
|
|
}
|
|
|
|
} else if (feof(output))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
clearerr(output);
|
|
|
|
|
|
|
|
if (!outLine)
|
|
|
|
NSLog(@"Failed command: %@ %s (%d)\n", command,
|
|
|
|
feof(output) ? "EOF" : "Error", errno);
|
|
|
|
|
2006-10-26 08:21:18 +00:00
|
|
|
pclose(output);
|
2006-11-10 08:54:00 +00:00
|
|
|
|
|
|
|
return outLine;
|
2006-10-26 08:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)lilypondVersion:(NSString *)path
|
|
|
|
{
|
|
|
|
NSString * cmd =
|
|
|
|
[NSString stringWithFormat:
|
2006-10-28 09:18:55 +00:00
|
|
|
@"%@ --version | head -1 | awk '{ print $3 }'",
|
2006-10-26 08:21:18 +00:00
|
|
|
path];
|
|
|
|
return [self getLineFromCommand:cmd];
|
|
|
|
}
|
|
|
|
|
2007-04-23 03:43:39 +00:00
|
|
|
- (void)adviseLilypondInstallation:(id)sender
|
|
|
|
{
|
|
|
|
// [NSApp terminate:self];
|
|
|
|
}
|
|
|
|
|
2006-10-26 08:21:18 +00:00
|
|
|
- (void)awakeFromNib
|
|
|
|
{
|
|
|
|
[lilypondPath setAutoenablesItems:NO];
|
|
|
|
|
|
|
|
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
|
|
|
|
NSFileManager * fileManager = [NSFileManager defaultManager];
|
|
|
|
NSString * lilyPath = [defaults stringForKey:@"VLLilypondPath"];
|
|
|
|
NSRange app = [lilyPath rangeOfString:@".app"];
|
|
|
|
bool wantTool = app.location == NSNotFound;
|
|
|
|
|
|
|
|
if ([fileManager isExecutableFileAtPath:lilyPath]) {
|
|
|
|
//
|
|
|
|
// Path still valid, figure out what it is
|
|
|
|
//
|
|
|
|
if (wantTool)
|
|
|
|
toolPath = lilyPath;
|
|
|
|
else
|
|
|
|
appPath = lilyPath;
|
|
|
|
}
|
|
|
|
if (!appPath)
|
|
|
|
appPath =
|
|
|
|
[[[NSWorkspace sharedWorkspace]
|
|
|
|
absolutePathForAppBundleWithIdentifier:@"org.lilypond.lilypond"]
|
|
|
|
stringByAppendingPathComponent:@"Contents/Resources/bin/lilypond"];
|
|
|
|
if (!toolPath)
|
|
|
|
toolPath = [self getLineFromCommand:@"bash -l which lilypond"];
|
|
|
|
|
|
|
|
NSString * appVersion = nil;
|
|
|
|
NSString * toolVersion = nil;
|
|
|
|
|
|
|
|
if (appPath) {
|
|
|
|
appVersion = [self lilypondVersion:appPath];
|
|
|
|
if (!appVersion)
|
|
|
|
appPath = nil;
|
2006-10-29 08:21:25 +00:00
|
|
|
else
|
|
|
|
[appPath retain];
|
2006-10-26 08:21:18 +00:00
|
|
|
}
|
|
|
|
if (toolPath) {
|
|
|
|
toolVersion = [self lilypondVersion:toolPath];
|
|
|
|
if (!toolVersion)
|
|
|
|
toolPath = nil;
|
2006-10-29 08:21:25 +00:00
|
|
|
else
|
|
|
|
[toolPath retain];
|
2006-10-26 08:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NSMenuItem * toolItem = [lilypondPath itemAtIndex:0];
|
|
|
|
NSMenuItem * appItem = [lilypondPath itemAtIndex:1];
|
|
|
|
|
|
|
|
if (toolPath) {
|
|
|
|
[toolItem setTitle:
|
|
|
|
[NSString stringWithFormat:@"%@ (%@)", toolPath, toolVersion]];
|
|
|
|
} else {
|
|
|
|
[toolItem setTitle:@"lilypond tool not installed"];
|
|
|
|
[toolItem setEnabled:NO];
|
|
|
|
}
|
|
|
|
if (appPath) {
|
|
|
|
NSRange r = [appPath rangeOfString:@"/Contents/Resources"];
|
|
|
|
[appItem setTitle:
|
|
|
|
[NSString stringWithFormat:@"%@ (%@)",
|
|
|
|
[appPath substringToIndex:r.location], appVersion]];
|
|
|
|
} else {
|
|
|
|
[appItem setTitle:@"Lilypond.app not installed"];
|
|
|
|
[appItem setEnabled:NO];
|
|
|
|
}
|
|
|
|
if (!toolPath && appPath) {
|
|
|
|
wantTool = false;
|
|
|
|
[defaults setObject:appPath forKey:@"VLLilypondPath"];
|
|
|
|
} else if (toolPath && !appPath) {
|
|
|
|
wantTool = true;
|
|
|
|
[defaults setObject:toolPath forKey:@"VLLilypondPath"];
|
|
|
|
}
|
2007-04-23 03:43:39 +00:00
|
|
|
[lilypondPath selectItemWithTag:wantTool ? 0 : 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)promptForSoftwareInstallation:(NSString *)label
|
|
|
|
withTitle:(NSString *)title
|
|
|
|
explanation:(NSString *)expl
|
|
|
|
script:(NSString *)script
|
|
|
|
url:(NSURL *)url
|
|
|
|
{
|
|
|
|
NSString * hasFink = [self getLineFromCommand:@"bash -l which fink"];
|
|
|
|
|
2007-04-24 18:32:32 +00:00
|
|
|
int response =
|
2007-04-23 03:43:39 +00:00
|
|
|
[[NSAlert alertWithMessageText:title
|
|
|
|
defaultButton: hasFink
|
|
|
|
? @"Install through fink"
|
|
|
|
: label
|
|
|
|
alternateButton:@"Continue"
|
|
|
|
otherButton:hasFink
|
|
|
|
? label
|
|
|
|
: @""
|
|
|
|
informativeTextWithFormat: expl, hasFink
|
|
|
|
? @"\n\nSince you have fink installed already, you may "
|
|
|
|
"choose to install this package through fink." : @""]
|
|
|
|
runModal];
|
|
|
|
|
|
|
|
if (response == NSAlertAlternateReturn)
|
|
|
|
return NO;
|
|
|
|
else if (hasFink && response == NSAlertDefaultReturn) {
|
|
|
|
NSDictionary * error;
|
|
|
|
NSURL * scptURL =
|
|
|
|
[NSURL fileURLWithPath:
|
|
|
|
[[NSBundle mainBundle] pathForResource:script
|
|
|
|
ofType:@"scpt"]];
|
|
|
|
NSAppleScript * scpt =
|
|
|
|
[[NSAppleScript alloc]
|
|
|
|
initWithContentsOfURL:scptURL error:&error];
|
|
|
|
[scpt executeAndReturnError:&error];
|
|
|
|
} else {
|
|
|
|
[[NSWorkspace sharedWorkspace] openURL:url];
|
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationWillFinishLaunching:(NSNotification *)aNotification
|
|
|
|
{
|
|
|
|
BOOL quit = NO;
|
|
|
|
|
|
|
|
if (!toolPath && !appPath)
|
|
|
|
if ([self promptForSoftwareInstallation:@"Download from lilypond.org"
|
|
|
|
withTitle: @"Lilypond Not Found!"
|
|
|
|
explanation:
|
|
|
|
@"Couldn't find an installation of Lilypond, which "
|
|
|
|
"is needed to typeset the sheet music. If you continue "
|
|
|
|
"without installing, you will be unable to preview, "
|
|
|
|
"print, or save as PDF.%@"
|
|
|
|
script:@"installLilypond"
|
|
|
|
url:[NSURL URLWithString:@"http://lilypond.org/web/install"]]
|
|
|
|
)
|
|
|
|
quit = YES;
|
|
|
|
if (![self getLineFromCommand:@"bash -l which python2.5"])
|
|
|
|
if ([self promptForSoftwareInstallation:@"Download from python.org"
|
|
|
|
withTitle: @"Python 2.5 Not Found!"
|
|
|
|
explanation:
|
|
|
|
@"Python 2.5 is needed to play accompaniments. The "
|
|
|
|
"version preinstalled on your computer is not recent "
|
|
|
|
"enough. If you continue without installing, you will be "
|
|
|
|
"unable to play accompaniments, or save as MIDI.%@"
|
|
|
|
script:@"installPython"
|
|
|
|
url:[NSURL URLWithString:@"http://www.python.org/download"]]
|
|
|
|
)
|
|
|
|
quit = YES;
|
|
|
|
|
|
|
|
if (quit) {
|
|
|
|
[[NSAlert alertWithMessageText:@"Quit and Restart"
|
|
|
|
defaultButton: @"OK" alternateButton: @"" otherButton: @""
|
|
|
|
informativeTextWithFormat:
|
|
|
|
@"The software you have chosen to install will be "
|
|
|
|
"available after you restart this application."]
|
|
|
|
runModal];
|
|
|
|
[NSApp terminate:self];
|
|
|
|
}
|
2006-10-26 08:21:18 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 07:15:46 +00:00
|
|
|
- (IBAction) playNewPitch:(id)sender
|
|
|
|
{
|
|
|
|
VLNote note(VLFraction(1,4), [sender intValue]);
|
|
|
|
|
|
|
|
VLSoundOut::Instance()->PlayNote(note);
|
|
|
|
}
|
|
|
|
|
2006-10-26 08:21:18 +00:00
|
|
|
- (IBAction) selectLilypondPath:(id)sender
|
|
|
|
{
|
|
|
|
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
|
|
|
|
|
2006-10-29 08:21:25 +00:00
|
|
|
switch ([[sender selectedItem] tag]) {
|
2006-10-26 08:21:18 +00:00
|
|
|
case 0:
|
|
|
|
[defaults setObject:toolPath forKey:@"VLLilypondPath"];
|
|
|
|
break;
|
|
|
|
case 1:
|
2006-10-29 08:21:25 +00:00
|
|
|
[defaults setObject:appPath forKey:@"VLLilypondPath"];
|
2006-10-26 08:21:18 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-30 05:56:35 +00:00
|
|
|
- (IBAction) goToHelpURL:(id)sender
|
|
|
|
{
|
|
|
|
NSString * helpString;
|
|
|
|
|
|
|
|
switch ([sender tag]) {
|
|
|
|
case 0:
|
|
|
|
helpString = @"http://vocaleasel.sf.net";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
helpString = @"http://sourceforge.net/tracker/?func=add&group_id=195076&atid=951989";
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
helpString = @"http://sourceforge.net/tracker/?func=add&group_id=195076&atid=951992";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:helpString]];
|
|
|
|
}
|
|
|
|
|
2006-10-24 07:15:46 +00:00
|
|
|
@end
|