-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIIAssistantController.m
302 lines (244 loc) · 9.6 KB
/
IIAssistantController.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
//
// IIAssistantController.m
// iTunesImport
//
// Created by Alexander Strange on 1/25/08.
//
#import "IIAssistantController.h"
#import "IIAlbum.h"
#import "Utilities.h"
#import "iTunesApplication.h"
#import <QTKit/QTKit.h>
@implementation IIAssistantController
- (IBAction)advance:(id)sender
{
[nextButton setEnabled:NO];
[assistantTabView selectNextTabViewItem:self];
int nextTab = [assistantTabView indexOfTabViewItem:[assistantTabView selectedTabViewItem]];
switch (nextTab) {
case 1:
if (!albumTags) {albumTags = [album tags]; [self setAlbumFields];}
[albumTrackTable setDataSource:albumTags];
[nextButton setEnabled:YES];
break;
case 2:
[self gatherAlbumTags];
[imageChoiceView setDataSource:self];
[self loadAlbumImages];
[nextButton setEnabled:YES];
break;
case 3:
[self importIntoiTunes];
}
}
- (void)awakeFromNib
{
parsingThread = nil;
album = nil;
albumTags = nil;
[assistantWindow setTitle:@"iTunes Importer"];
[albumTrackTable setColumnAutoresizingStyle:NSTableViewReverseSequentialColumnAutoresizingStyle];
imageNameArray = [[NSMutableArray alloc] init];
imageArray = [[NSMutableArray alloc] init];
[consoleView setFont:[NSFont fontWithName:@"Monaco" size:10]];
[QTMovie initialize];
}
#pragma mark -- Album Choice
- (IBAction)chooseAlbum:(id)sender {
NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setCanChooseDirectories:YES];
[panel beginSheetForDirectory:nil file:nil modalForWindow:assistantWindow modalDelegate:self didEndSelector:@selector(chooseAlbumPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL];
}
- (void)chooseAlbumPanelDidEnd:(NSOpenPanel *)panel returnCode:(int)returnCode contextInfo:(void*)contextInfo
{
[chooseAlbumField setStringValue:[panel filename]];
[self albumChosen:self];
}
- (void)validateAlbumOnThread
{
if ([parsingThread isExecuting]) [parsingThread cancel];
if (parsingThread) [parsingThread release];
parsingThread = [[NSThread alloc] initWithTarget:self selector:@selector(validateAlbumThread) object:nil];
[parsingThread start];
}
- (IBAction)albumChosen:(id)sender
{
NSString *name = [chooseAlbumField stringValue];
if ([name isValidFilename]) {
[self validateAlbumOnThread];
}
}
- (void)validateAlbumThread
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[progressIndicator performSelectorOnMainThread:@selector(startAnimation:) withObject:self waitUntilDone:NO];
if (album) [album release];
album = [GetAlbumForFileSource(GetFileSourceForPath([chooseAlbumField stringValue])) retain];
NSString *desc = [album description];
[albumTypeLabel performSelectorOnMainThread:@selector(setStringValue:) withObject:desc?desc:@"none?" waitUntilDone:NO];
[nextButton setEnabled:desc ? YES : NO];
[progressIndicator performSelectorOnMainThread:@selector(stopAnimation:) withObject:self waitUntilDone:NO];
[pool release];
}
/*
-(void)setupEncodingView
{
if(!encodingview)
{
NSNib *nib=[[[NSNib alloc] initWithNibNamed:@"EncodingView" bundle:nil] autorelease];
[nib instantiateNibWithOwner:self topLevelObjects:nil];
}
NSImage *icon=[[NSWorkspace sharedWorkspace] iconForFile:archivename];
[icon setSize:[encodingicon frame].size];
[encodingicon setImage:icon];
[encodingpopup buildEncodingListMatchingBytes:name_bytes];
if(selected_encoding)
{
int index=[encodingpopup indexOfItemWithTag:selected_encoding];
if(index>=0) [encodingpopup selectItemAtIndex:index];
else [encodingpopup selectItemAtIndex:[encodingpopup indexOfItemWithTag:NSISOLatin1StringEncoding]];
}
[self selectEncoding:self];
[self setDisplayedView:encodingview];
}
*/
#pragma mark -- Tag Editing
- (void)setAlbumFields
{
#define set(tfield, field) if (albumTags->field) [tfield setStringValue:albumTags->field];
set(albumArtistField, artist);
set(albumTitleField, title);
set(albumComposerField, composer);
set(albumGenreField, genre);
if (albumTags->year) [albumYearField setIntValue:albumTags->year];
}
- (void)gatherAlbumTags
{
#define get(tfield, field) if (albumTags->field) [albumTags->field release]; albumTags->field = [[tfield stringValue] retain];
get(albumArtistField, artist);
get(albumTitleField, title);
get(albumComposerField, composer);
get(albumGenreField, genre);
albumTags->year = [albumYearField intValue];
[album recanonicalizeTags];
}
#pragma mark -- Image Choice
- (void)loadAlbumImages
{
imagesThread = [[NSThread alloc] initWithTarget:self selector:@selector(loadAlbumImagesThread) object:nil];
[imagesThread start];
}
- (void)loadAlbumImagesThread
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[progressIndicator performSelectorOnMainThread:@selector(startAnimation:) withObject:self waitUntilDone:NO];
IIFileSource *fs = [album fileSource];
NSArray *imageNames = [fs filesWithExtension:@".jpg" atTopLevel:NO];
for (NSString *imageName in imageNames) {
NSData *imageData = [fs dataFromFile:imageName];
if (!imageData) continue;
NSImage *image = [[[NSImage alloc] initWithData:imageData] autorelease];
if (!image) continue;
[imageNameArray addObject:imageName];
[imageArray addObject:image];
[imageChoiceView performSelectorOnMainThread:@selector(reloadData) withObject:self waitUntilDone:NO];
}
[progressIndicator performSelectorOnMainThread:@selector(stopAnimation:) withObject:self waitUntilDone:NO];
[pool release];
}
- (NSUInteger) numberOfItemsInImageBrowser:(IKImageBrowserView *) aBrowser
{
return [imageArray count];
}
- (id) imageBrowser:(IKImageBrowserView *) aBrowser itemAtIndex:(NSUInteger)index
{
return [imageArray objectAtIndex:index];
}
- (void) imageBrowserSelectionDidChange:(IKImageBrowserView *) aBrowser
{
NSIndexSet *is = [aBrowser selectionIndexes];
if ([is count] == 0) [curImageView setImage:nil];
else [curImageView setImage:[imageArray objectAtIndex:[is firstIndex]]];
}
#pragma mark -- Import
- (void)importIntoiTunes
{
iTunesImportThread = [[NSThread alloc] initWithTarget:self selector:@selector(iTunesImportThread) object:nil];
[iTunesImportThread start];
}
- (void)_appendLineToConsole:(NSString*)line
{
NSAttributedString *s = [[[NSAttributedString alloc] initWithString:line] autorelease];
NSAttributedString *nl = [[[NSAttributedString alloc] initWithString:@"\n"] autorelease];
NSTextStorage *textStorage = [consoleView textStorage];
[textStorage appendAttributedString:s];
[textStorage appendAttributedString:nl];
}
- (void)appendLineToConsole:(NSString*)line
{
[self performSelectorOnMainThread:@selector(_appendLineToConsole:) withObject:line waitUntilDone:NO];
}
- (id)eventDidFail:(const AppleEvent *)event withError:(NSError *)error
{
NSLog(@"failure with error: %@", error);
@throw [NSException exceptionWithName:NSGenericException reason:[error description] userInfo:nil];
}
- (void)iTunesImportThread
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
NSMutableArray *ittracks = [NSMutableArray array];
[progressIndicator performSelectorOnMainThread:@selector(startAnimation:) withObject:self waitUntilDone:NO];
//SBElementArray *library = [iTunes.sources objectWithName:@"Library"];
[iTunes setDelegate:self];
[iTunes setTimeout:kNoTimeOut];
BOOL reencode = [album shouldReencode], success;
//iTunesUserPlaylist *dbgPlaylist = [[[[iTunes sources] objectAtIndex:0] userPlaylists] objectWithName:@"itt"];
NSImage *artwork = [curImageView image];
#define do(act) \
{NSAutoreleasePool *p2 = [[NSAutoreleasePool alloc] init]; \
do { @try {success=true; act;} @catch (NSException *e) {success = false; NSLog(@"'%s' failed, retrying.", #act); sleep(2);}} while (!success); \
[p2 release];}
for (TrackTags *tr in albumTags->tracks) {
[self appendLineToConsole:[NSString stringWithFormat:@"Importing %d \"%@\"…", tr->num, tr->title]];
NSString *path = [album pathToTrackWithID:tr->tid];
iTunesTrack *nt = [iTunes add:[NSArray arrayWithObject:[NSURL fileURLWithPath:path isDirectory:NO]] to:nil];
if (!nt) {
[self appendLineToConsole:[NSString stringWithFormat:@"iTunes didn't want to add \"%@\"", path]];
// here it should try reencode...
continue;
}
[ittracks addObject:nt];
do(nt.album = albumTags->title);
if (albumTags->hasAlbumArtist)
do(nt.albumArtist = albumTags->artist);
do(nt.artist = [tr->artist length]?tr->artist:albumTags->artist);
if ([tr->comment length]) do(nt.comment = tr->comment);
do(nt.composer = [tr->composer length] ? tr->composer : albumTags->composer);
do(nt.discCount = 1);
do(nt.discNumber = 1);
do(nt.genre = [tr->genre length] ? tr->genre : albumTags->genre);
do(nt.name = tr->title);
do(nt.trackNumber = tr->num);
do(nt.trackCount = [albumTags->tracks count]);
do(nt.year = tr->year ? tr->year : albumTags->year);
if (artwork) {
iTunesArtwork *art = [[nt artworks] objectAtIndex:0];
@try {
art.data = artwork;
} @catch (NSException *e) {
NSLog(@"Setting artwork failed: %@", e);
}
}
}
if (reencode) {
[self appendLineToConsole:@"Converting to AAC…"];
do([iTunes convert:ittracks]);
for (iTunesTrack *t in ittracks) {
}
}
[self appendLineToConsole:@"Done."];
[progressIndicator performSelectorOnMainThread:@selector(stopAnimation:) withObject:self waitUntilDone:NO];
[pool release];
}
@end