Skip to content

Commit

Permalink
Better field behaviour, error messages, finding xml properly
Browse files Browse the repository at this point in the history
git-svn-id: svn://Zim.local/NanoFibre/trunk@624 6c154dfa-49ee-0310-9ea9-8312faa011d6
  • Loading branch information
jdelStrother committed Nov 25, 2006
1 parent 9535069 commit 2321365
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 9 deletions.
1 change: 1 addition & 0 deletions AppController.h
Expand Up @@ -5,6 +5,7 @@
@interface AppController : NSObject
{
IBOutlet NSArrayController *arrayController;
IBOutlet NSTextField *sizeField;
IBOutlet NSTextField *sizeLabel;
IBOutlet NSWindow *window;
}
Expand Down
68 changes: 67 additions & 1 deletion AppController.m
Expand Up @@ -4,10 +4,17 @@
#import "MusicLibrary.h"
#import "HumanDiskSizeTransformer.h";

@interface AppController(private)
-(void)findITunesXML;
@end

@implementation AppController

+(void)initialize
{
NSDictionary* defaults = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:3.5] forKey:@"maxSize"]; //3.5GB is about right for my 4GB nano...
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];

HumanDiskSizeTransformer* humanDiskSizeTransformer = [[HumanDiskSizeTransformer alloc] init];
[NSValueTransformer setValueTransformer:humanDiskSizeTransformer
forName:@"HumanDiskSizeTransformer"];
Expand All @@ -19,24 +26,43 @@ -(void)applicationDidFinishLaunching:(NSNotification *)aNotification
// If we're running in daemon mode, just make the changes and leave.
if ([[[NSProcessInfo processInfo] environment] objectForKey:@"fibreDaemon"])
{
[self makePlaylist:self];
[[MusicLibrary sharedLibrary] createFibrePlaylist];
[[NSApplication sharedApplication] terminate:self];
}
else
{
[self findITunesXML];
[window makeKeyAndOrderFront:self];
}
}

- (void)controlTextDidChange:(NSNotification *)aNotification
{
//It's a bit silly setting up cocoa bindings for the size controls, only to manually set it here...
//However, I can't figure out a way of persuading it to update the label on every keystroke otherwise.
NSString* sizeString = [[[[aNotification userInfo] objectForKey:@"NSFieldEditor"] textStorage] string];
NSNumber* size = [[NSValueTransformer valueTransformerForName:@"HumanDiskSizeTransformer"] reverseTransformedValue:sizeString];
NSString* readableSize = [[NSValueTransformer valueTransformerForName:@"HumanDiskSizeTransformer"] transformedValue:size];
if (readableSize)
[sizeLabel setStringValue:readableSize];
}

-(IBAction)makePlaylist:(id)sender
{
//Persuade the text field to end editing, so the size value is committed:
[sizeField selectText:self];

[[MusicLibrary sharedLibrary] createFibrePlaylist];

NSRunAlertPanel(@"Playlist Generated", @"Take a look in iTunes, you should have a NanoFibre playlist ready and waiting for your iPod syncing needs.", @"OK", nil, nil);
}


-(IBAction)installDaemon:(id)sender
{
//Persuade the text field to end editing, so the size value is committed:
[sizeField selectText:self];

if (NSRunAlertPanel(@"Install Daemon?",
@"This will install a daemon to /usr/local/bin, and a launch agent to ~/Library/LaunchAgents",
@"Install",@"Cancel",nil) == NSAlertDefaultReturn)
Expand Down Expand Up @@ -109,4 +135,44 @@ -(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theAppli
}



// This could use some love - it won't handle aliases etc.
-(void)findITunesXML
{
//Need to find the iTunes XML. Start by checking previous results:
BOOL libraryFound = NO;
NSString* pathToLibrary = [[NSUserDefaults standardUserDefaults] objectForKey:@"iTunesXMLPath"];
if (pathToLibrary && [[NSFileManager defaultManager] fileExistsAtPath:pathToLibrary])
return; //We're good to go


//OK, we don't know where it is. Check the obvious location:
pathToLibrary = [@"~/Music/iTunes/iTunes Music Library.xml" stringByExpandingTildeInPath];
if ([[NSFileManager defaultManager] fileExistsAtPath:pathToLibrary])
libraryFound = YES;

//Still not found it? Ask the user to locate it:
if (!libraryFound)
{
NSOpenPanel* openPanel = [NSOpenPanel openPanel];
[openPanel setTitle:@"Open iTunes Library"];
[openPanel setMessage:@"Where is your iTunes Music Library XML?"];
int panelResult = [openPanel runModalForTypes:[NSArray arrayWithObject:@"xml"]];
if (panelResult == NSOKButton)
{
pathToLibrary = [openPanel filename];
if ([[NSFileManager defaultManager] fileExistsAtPath:pathToLibrary])
libraryFound = YES;
}
}

if (libraryFound)
[[NSUserDefaults standardUserDefaults] setObject:pathToLibrary forKey:@"iTunesXMLPath"];
else
{
NSRunAlertPanel(@"Oh dear", @"We couldn't find your iTunes XML. Bad luck." ,@":(",nil,nil);
[[NSApplication sharedApplication] terminate:self];
}
}

@end
2 changes: 1 addition & 1 deletion English.lproj/MainMenu.nib/classes.nib

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions English.lproj/MainMenu.nib/info.nib

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified English.lproj/MainMenu.nib/keyedobjects.nib
Binary file not shown.
2 changes: 1 addition & 1 deletion Info.plist
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<string>1.1</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
Expand Down
2 changes: 1 addition & 1 deletion MusicLibrary.m
Expand Up @@ -114,7 +114,7 @@ -(void)dealloc
-(void)loadLibrary
{
// This isn't particularly robust : It doesn't handle music libraries in odd locations, or ones that are linked to the usual location with aliases.
NSString* libraryPath = [@"~/Music/iTunes/iTunes Music Library.xml" stringByExpandingTildeInPath];
NSString* libraryPath = [[NSUserDefaults standardUserDefaults] objectForKey:@"iTunesXMLPath"];

NSDictionary* xml = [NSDictionary dictionaryWithContentsOfFile:libraryPath];

Expand Down
2 changes: 1 addition & 1 deletion NanoFibre.xcodeproj/project.pbxproj
Expand Up @@ -33,7 +33,7 @@
isa = PBXContainerItemProxy;
containerPortal = 29B97313FDCFA39411CA2CEA /* Project object */;
proxyType = 1;
remoteGlobalIDString = FC0F29A60AE04CD700C72116 /* fibred */;
remoteGlobalIDString = FC0F29A60AE04CD700C72116;
remoteInfo = fibred;
};
/* End PBXContainerItemProxy section */
Expand Down

0 comments on commit 2321365

Please sign in to comment.