rendered paste body//
// MainWindowController.m
// StarConsolidater
//
// Created by Руслан Федоров on 2/4/12.
// Copyright (c) 2012 MIPT iLab. All rights reserved.
//
#import "MainWindowController.h"
#import "ASPTrackTableCell.h"
#import "AEDForder.h"
@implementation MainWindowController
@synthesize foundTable;
#pragma mark Table Delegate/DataSource
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView{
if ([[aTableView identifier] isEqualToString:[foundTable identifier]]){
if (tracks!=nil)
return [tracks count];
else
return 1;
}
return 0;
}
- (NSView *)tableView:(NSTableView *)tableView
viewForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row
{
ASPTrackTableCell *newCell=[tableView makeViewWithIdentifier:@"TrackView" owner:self];
if(tracks!=nil){
NSDictionary *track=[tracks objectAtIndex:row];
if (track!=nil){
//Artist
if ([track valueForKey:@"Artist"]!=nil)
[newCell.artist setStringValue:[track valueForKey:@"Artist"]];
else
[newCell.artist setStringValue:@"Error getting Artist"];
//Title
if ([track valueForKey:@"Title"]!=nil)
[newCell.title setStringValue:[track valueForKey:@"Title"]];
else
[newCell.title setStringValue:@"Error getting Title"];
//Artwork
if ([[track valueForKey:@"Artwork"] isKindOfClass:[NSImage class]])
[newCell.cover setImage:[track valueForKey:@"Artwork"]];
else
[newCell.cover setImage:[NSImage imageNamed:@"NSActionTemplate"]];
//Location
if ([track valueForKey:@"Location"]!=nil){
NSDictionary *locDict=[track valueForKey:@"Location"];
NSString *kind=[locDict valueForKey:@"Kind"];
if ([kind isEqualToString:@"None"]){
newCell.color=[NSColor colorWithCalibratedRed:1.0 green:0.1 blue:0.1 alpha:0.5];
}
else if([kind isEqualToString:@"Local"
location=[[(NSURL*)[track valueForKey:@"Location"]pathComponents]objectAtIndex:2];
}
if (location!=nil){
[newCell.location setStringValue:location];
if ([location isEqualToString:@"aspcartman"])
newCell.color=
else
newCell.color=[NSColor colorWithCalibratedRed:0.5 green:0.0 blue:0.0 alpha:0.2];
}else{
[newCell.location setStringValue:@"No location value"];
newCell.color=[NSColor redColor];
}
}else //track=nil
}
else{
[newCell.location setStringValue:@"No location value"];
newCell.color=[NSColor colorWithCalibratedRed:0.5 green:0.0 blue:0.0 alpha:0.0];
}
return newCell;
}
#pragma mark iTunes
//Fetches info from iTunes and puts it to tracks variable
- (IBAction)fetchInfo:(id)sender{
NSArray *artists=nil, *titles=nil;
//Arttists strings
if ([[NSApp delegate] respondsToSelector:@selector(getPlaylistArtists:)]){
NSAppleEventDescriptor *descriptor=(NSAppleEventDescriptor*)[[NSApp delegate] performSelector:@selector(getPlaylistArtists:)];
if (descriptor!=nil) {
artists=[AEDForder fordDeep:descriptor];
}
}
//Titles strings
if ([[NSApp delegate] respondsToSelector:@selector(getPlaylistNames:)]){
NSAppleEventDescriptor *descriptor=(NSAppleEventDescriptor*)[[NSApp delegate] performSelector:@selector(getPlaylistNames:)];
if (descriptor!=nil) {
titles=[AEDForder fordDeep:descriptor];
}
}
//Artwork data
NSMutableArray *artworks=[NSMutableArray arrayWithCapacity:[titles count]];
NSAppleScript *scr;
NSAppleEventDescriptor *descriptor;
for (int i=0; i<[titles count]; i++){
scr=[[NSAppleScript alloc]initWithSource:
[NSString stringWithFormat:
@"tell application \"iTunes\"\n\
set aTrack to (track %d of user playlist \"Stars\")\n\
set ar to artwork 1 of aTrack\n\
set aData to data of ar\n\
return aData\n\
end tell",i+1]];
descriptor=[scr executeAndReturnError:nil];
NSData *data=(NSData*)[AEDForder ford:descriptor];
if (data!=nil){
[artworks addObject:[[NSImage alloc]initWithData:data]];
}else [artworks addObject:[NSData data]];
}
//Locations
NSMutableArray *locationDicts=[NSMutableArray arrayWithCapacity:[titles count]];
for (int i=0; i<[titles count]; i++){
scr=[[NSAppleScript alloc]initWithSource:
[NSString stringWithFormat:
@"tell application \"iTunes\"\n\
set aTrack to (track %d of user playlist \"Stars\")\n\
set loc to location of aTrack\n\
return loc\n\
end tell",i+1]];
descriptor=[scr executeAndReturnError:nil];
NSURL *url=(NSURL*)[AEDForder ford:descriptor];
[locationDicts addObject:[self locationDictionaryForLoc:url]];
}
NSMutableArray *startracks=[NSMutableArray arrayWithCapacity:[titles count]];
if (titles!=nil&&artists!=nil&&startracks!=nil) {
for (int i=0;i<[titles count];i++){
[startracks addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
[artists objectAtIndex:i],@"Artist", [titles objectAtIndex:i],@"Title", [NSNumber numberWithInt:i],@"Position",
[artworks objectAtIndex:i],@"Artwork",[locationDicts objectAtIndex:i],@"Location",nil]];
}
}
tracks=startracks;
[foundTable reloadData];
}
//Guesses paths using iTunes one
-(NSDictionary*)locationDictionaryForLoc:(NSURL*)sourceloc{
if (sourceloc!=nil){
//Templates
NSArray *localTemplate=[NSArray arrayWithObjects:
@"/",@"Users",@"aspcartman",@"Music",@"iTunes",@"iTunes Media",@"Music",nil];
NSArray *remoteTemplate=[NSArray arrayWithObjects:@"/",@"Volumes",@"Music",nil];
NSArray *path=[sourceloc pathComponents];
NSMutableArray *localPathArray,*remotePathArray;
NSURL *localPath,*remotePath;
NSString *kind;
//Determ local/remote and make PathArrays
if([path count]>3){
if([[path objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 3)]] isEqualToArray:remoteTemplate]){
remotePathArray=[NSMutableArray arrayWithArray:path];
kind=@"Remote";
}else
if ([path count]>7)
if([[path objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 7)]] isEqualToArray:localTemplate]){
localPathArray=[NSMutableArray arrayWithArray:path];
kind=@"Local";
}
}
if (localPathArray!=nil) {
remotePathArray=[NSMutableArray array];
[remotePathArray addObjectsFromArray:remoteTemplate];
[remotePathArray addObjectsFromArray:[localPathArray objectsAtIndexes:
[NSIndexSet indexSetWithIndexesInRange:
NSMakeRange(7,[localPathArray count]-9)]]];
}else if(remotePathArray!=nil){
localPathArray=[NSMutableArray array];
[localPathArray addObjectsFromArray:localTemplate];
[localPathArray addObjectsFromArray:[remotePathArray objectsAtIndexes:
[NSIndexSet indexSetWithIndexesInRange:
NSMakeRange(4,[remotePathArray count]-6)]]];
}else{//Path is not prefered
return [NSDictionary dictionaryWithObjectsAndKeys:sourceloc,@"iTunes",@"Other",@"Kind", nil];
}
//Make local path URL
NSMutableString *localPathString=[NSMutableString stringWithString:@"file://localhost"];
for (int i=1;i<[localPathArray count];i++){
[localPathString appendFormat:@"/%@",[localPathArray objectAtIndex:i]];
}
localPath=[NSURL URLWithString:[localPathString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//Make remote path URL
NSMutableString *remotePathString=[NSMutableString stringWithString:@"file://localhost"];
for (int i=1;i<[remotePathArray count];i++){
[remotePathString appendFormat:@"/%@",[remotePathArray objectAtIndex:i]];
}
remotePath=[NSURL URLWithString:[remotePathString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//Return
if (localPath!=nil&&remotePath!=nil)
return [NSDictionary dictionaryWithObjectsAndKeys:remotePathArray,@"Remote",localPathArray,@"Local",sourceloc,@"iTunes",kind,@"Kind" nil];
else
return [NSDictionary dictionaryWithObjectsAndKeys:sourceloc,@"iTunes",@"Other",@"Kind", nil];
}else return [NSDictionary dictionaryWithObjectsAndKeys:@"None",@"Kind", nil];
}
@end