Skip to content
This repository
tree: b8ede52aae
Fetching contributors…

Cannot retrieve contributors at this time

file 101 lines (66 sloc) 2.554 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
//
// UpdateOperation.m
// Album Artwork Assistant
//
// Created by Marc Liyanage on 14.07.08.
// Copyright 2008-2009 Marc Liyanage <http://www.entropy.ch>. All rights reserved.
//

#import "UpdateOperation.h"
#import "GTMNSAppleScript+Handler.h"
#import "NSObject+DDExtensions.h"

@implementation UpdateOperation

@synthesize fileUrl;

- (id)initWithTracks:(NSArray *)t imageData:(NSData *)iData statusDelegate:(id <StatusDelegateProtocol>)sd {
self = [super init];
if (!self) return self;
tracks = t;
imageData = iData;
statusDelegate = sd;
didComplete = NO;
return self;
}


- (void)main {

@try {

if (!self.fileUrl) {
@throw [NSException exceptionWithName:@"TempFileWrite" reason:NSLocalizedString(@"cant_write_tempfile", @"") userInfo:nil];
}

NSString *tempFilePath = [self.fileUrl path];

[statusDelegate startBusy:[NSString stringWithFormat:NSLocalizedString(@"adding_image_to_%@", @""), [self albumTitle]]];

NSString *scptPath = [[NSBundle mainBundle] pathForResource:@"embed-artwork" ofType:@"scpt" inDirectory:@"Scripts"];
NSURL *scptUrl = [NSURL fileURLWithPath:scptPath];
NSDictionary *errorDict = nil;
NSAppleScript *script = [[NSAppleScript alloc] initWithContentsOfURL:scptUrl error:&errorDict];
if (errorDict) {
@throw [NSException exceptionWithName:@"AppleScriptLoad" reason:[errorDict valueForKey:NSAppleScriptErrorBriefMessage] userInfo:nil];
}

NSArray *params = [NSArray arrayWithObjects:tracks, tempFilePath, nil];
[[script dd_invokeOnMainThreadAndWaitUntilDone:YES] gtm_executePositionalHandler:@"embedArtwork" parameters:params error:&errorDict];
if (errorDict) {
@throw [NSException exceptionWithName:@"AppleScriptExecute" reason:[errorDict valueForKey:NSAppleScriptErrorBriefMessage] userInfo:nil];
}

didComplete = YES;

}

@catch (NSException *e) {
[statusDelegate displayErrorWithTitle:NSLocalizedString(@"cant_set_artwork", @"") message:[e reason]];
}

@finally {
[statusDelegate clearBusy];
}

}



- (BOOL)didComplete {
return didComplete;
}



- (NSString *)albumTitle {
return [[tracks objectAtIndex:0] valueForKey:@"track_album"];
}


- (NSURL *)fileUrl {
if (fileUrl) return fileUrl;

NSString *tempFilePath = [NSString stringWithFormat:@"%@/%@", NSTemporaryDirectory(), @"album-artwork-assistant.tmp"];

if (![imageData writeToFile:tempFilePath atomically:YES]) {
NSLog(@"Unable to store image data to temp file '%@'", tempFilePath);
return nil;
}
[self setFileUrl:[NSURL fileURLWithPath:tempFilePath]];
return fileUrl;
}








@end
Something went wrong with that request. Please try again.