-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMainController.m
More file actions
523 lines (454 loc) · 18.4 KB
/
MainController.m
File metadata and controls
523 lines (454 loc) · 18.4 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/***************************************************************************
* Copyright 2005-2009 Last.fm Ltd. *
* Copyright 2010 Max Howell <max@methylblue.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#import "AutoDash.h"
#import "ITunesListener.h"
#import "lastfm.h"
#import "MainController.h"
#import "NSDictionary+Track.h"
#import "ShareWindowController.h"
#import <Carbon/Carbon.h>
#import <WebKit/WebKit.h>
static bool scrobsub_fsref(FSRef* fsref)
{
OSStatus err = LSFindApplicationForInfo(kLSUnknownCreator, CFSTR("fm.last.Audioscrobbler"), NULL, fsref, NULL);
return err != kLSApplicationNotFoundErr;
}
static OSStatus MyHotKeyHandler(EventHandlerCallRef ref, EventRef e, void* userdata)
{
EventHotKeyID hkid;
GetEventParameter(e, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hkid), NULL, &hkid);
MainController* mc = userdata;
switch(hkid.id){
case 1:
[mc tag:userdata];
break;
case 2:
[mc share:userdata];
break;
case 3:
[mc love:userdata];
break;
case 4:
[mc lyrics:userdata];
break;
}
return noErr;
}
static LSSharedFileListItemRef audioscrobbler_session_login_item(LSSharedFileListRef login_items_ref)
{
LSSharedFileListItemRef login_item = NULL;
FSRef as_fsref;
if (!scrobsub_fsref(&as_fsref))
return NULL;
CFURLRef as_cfurl = CFURLCreateFromFSRef(kCFAllocatorDefault, &as_fsref);
UInt32 seed;
NSArray *items = [(NSArray*)LSSharedFileListCopySnapshot(login_items_ref, &seed) autorelease];
for (id id in items){
FSRef fsref;
LSSharedFileListItemRef item = (LSSharedFileListItemRef)id;
if (LSSharedFileListItemResolve(item, 0, NULL, &fsref) == noErr) {
CFURLRef cfurl = CFURLCreateFromFSRef(kCFAllocatorDefault, &fsref);
if (CFEqual(as_cfurl, cfurl)) {
login_item = item;
}
CFRelease(cfurl);
if (login_item)
break;
}
}
CFRelease(as_cfurl);
return login_item;
}
static NSString* downloads()
{
NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"Downloads"];
BOOL isdir = false;
if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir] && isdir)
return path;
return NSTemporaryDirectory();
}
@implementation MainController
+(void)initialize
{
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary
dictionaryWithObject:[NSNumber numberWithBool:false]
forKey:@"AutoDash"]];
}
-(NSDictionary*)registrationDictionaryForGrowl
{
NSArray* all = [NSArray arrayWithObjects:
ASGrowlTrackStarted,
ASGrowlTrackPaused,
ASGrowlTrackResumed,
ASGrowlPlaylistEnded,
ASGrowlLoveTrackQuery,
ASGrowlAuthenticationRequired,
ASGrowlErrorCommunication,
ASGrowlCorrectionSuggestion,
ASGrowlTrackIgnored,
ASGrowlSubmissionStatus,
nil];
NSArray* defaults = [NSArray arrayWithObjects:
ASGrowlTrackStarted,
ASGrowlTrackResumed,
ASGrowlPlaylistEnded,
ASGrowlLoveTrackQuery,
ASGrowlAuthenticationRequired,
ASGrowlErrorCommunication,
ASGrowlCorrectionSuggestion,
ASGrowlTrackIgnored,
ASGrowlSubmissionStatus,
nil];
return [NSDictionary dictionaryWithObjectsAndKeys:
all, GROWL_NOTIFICATIONS_ALL,
defaults, GROWL_NOTIFICATIONS_DEFAULT,
nil];
}
-(void)awakeFromNib
{
[NSURLCache sharedURLCache].memoryCapacity = 0; // save memory
status_item = [[[NSStatusBar systemStatusBar] statusItemWithLength:27] retain];
[status_item setHighlightMode:YES];
NSImage *icon = [NSImage imageNamed:@"icon"];
[icon setTemplate:YES];
[status_item setImage:icon];
[status_item setAlternateImage:[NSImage imageNamed:@"icon_inverted"]];
[status_item setEnabled:YES];
[status_item setMenu:menu];
[GrowlApplicationBridge setGrowlDelegate:self];
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"AutoDash"] boolValue] == true)
autodash = [[AutoDash alloc] init];
[NSApp setMainMenu:app_menu]; // so the close shortcut will work
/// Start at Login item
LSSharedFileListRef login_items_ref = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if(login_items_ref){
LSSharedFileListItemRef login_item = audioscrobbler_session_login_item(login_items_ref);
[start_at_login setState:login_item?NSOnState:NSOffState];
CFRelease(login_items_ref);
}
#if __AS_DEBUGGING__
[[menu itemAtIndex:[menu numberOfItems]-1] setTitle:@"Quit Debugscrobbler"];
#else
/// global shortcuts
EventTypeSpec type;
type.eventClass = kEventClassKeyboard;
type.eventKind = kEventHotKeyPressed;
InstallApplicationEventHandler(&MyHotKeyHandler, 1, &type, self, NULL);
EventHotKeyID kid;
EventHotKeyRef kref;
#define HOTKEY(key, sig, __id) \
kid.signature = sig; \
kid.id = __id; \
RegisterEventHotKey(kVK_ANSI_##key, cmdKey+optionKey+controlKey, kid, GetApplicationEventTarget(), 0, &kref);
HOTKEY(T, 'htk1', 1);
HOTKEY(S, 'htk2', 2);
HOTKEY(3, 'htk3', 3);
HOTKEY(L, 'htk4', 4);
#undef HOTKEY
#endif
lastfm = [[Lastfm alloc] initWithDelegate:self];
listener = [[ITunesListener alloc] initWithLastfm:lastfm delegate:self];
}
-(void)dealloc
{
[sharewc release];
[listener release];
[lastfm release];
[autodash release];
[status_item release];
[super dealloc];
}
-(bool)autohide
{
return false;
}
-(void)updateTitleMenu:(NSDictionary*)track
{
unsigned duration = track.duration;
[status setTitle:[NSString stringWithFormat:@"%@ [%d:%02d]", track.title, duration/60, duration%60]];
}
-(void)announceTrack:(NSDictionary*)track art:(NSData*)art notificationName:(NSString*)notificationName
{
NSMutableString* desc = [[[track objectForKey:@"Artist"] mutableCopy] autorelease];
[desc appendString:@"\n"];
[desc appendString:[track objectForKey:@"Album"]];
[GrowlApplicationBridge notifyWithTitle:track.title
description:desc
notificationName:notificationName
iconData:art
priority:0
isSticky:false
clickContext:track
identifier:ASGrowlTrackStarted];
}
-(void)iTunesTrackStarted:(NSDictionary*)track art:(NSData*)art
{
[love setEnabled:true];
[love setTitle:@"Love"];
[share setEnabled:true];
[tag setEnabled:true];
[lyrics setEnabled:true];
status_item.image = [NSImage imageNamed:@"icon"];
count++;
[self updateTitleMenu:track];
[self announceTrack:track art:art notificationName:ASGrowlTrackStarted];
}
-(void)iTunesTrackResumed:(NSDictionary *)track art:(NSData *)art
{
[self updateTitleMenu:track];
[self announceTrack:track art:art notificationName:ASGrowlTrackResumed];
}
-(void)iTunesTrackPaused:(NSDictionary*)track
{
[status setTitle:[track.title stringByAppendingString:@" [paused]"]];
[GrowlApplicationBridge notifyWithTitle:@"Playback Paused"
description:@"iTunes was paused"
notificationName:ASGrowlTrackPaused
iconData:nil
priority:0
isSticky:true
clickContext:track
identifier:ASGrowlTrackStarted];
}
-(void)iTunesPlaybackStopped
{
[status setTitle:@"Ready"];
[love setEnabled:false];
[tag setEnabled:false];
[share setEnabled:false];
[lyrics setEnabled:false];
[love setTitle:@"Love"];
status_item.image = [NSImage imageNamed:@"icon"];
NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
NSString* info = [NSString stringWithFormat:@"You played %@ tracks this session.",
[formatter stringFromNumber:[NSNumber numberWithUnsignedInt:count]]];
[formatter release];
count = 0;
[GrowlApplicationBridge notifyWithTitle:@"Playlist Ended"
description:info
notificationName:ASGrowlPlaylistEnded
iconData:nil
priority:0
isSticky:false
clickContext:nil];
}
-(void)iTunesTrackMetadataUpdated:(NSDictionary*)track
{
//TODO say, but already scrobbled! If so.
[self updateTitleMenu:track];
[GrowlApplicationBridge notifyWithTitle:@"Track Metadata Updated"
description:track.prettyTitle
notificationName:ASGrowlSubmissionStatus
iconData:nil
priority:-1
isSticky:false
clickContext:nil];
}
-(void)iTunesTrackWasRatedFourStarsOrAbove:(NSDictionary*)track
{
NSMutableDictionary* dict = [[track mutableCopy] autorelease];
[dict setObject:ASGrowlLoveTrackQuery forKey:@"Notification Name"];
[GrowlApplicationBridge notifyWithTitle:@"A+++++ Would Play Again!"
description:@"Click this notification to love this track at Last.fm"
notificationName:ASGrowlLoveTrackQuery
iconData:nil
priority:0
isSticky:false
clickContext:dict];
}
-(void)iTunesWontScrobble:(NSDictionary*)track because:(NSString*)reason
{
[GrowlApplicationBridge notifyWithTitle:@"Will Not Scrobble"
description:[NSString stringWithFormat:@"“%@” is %@.", track.prettyTitle, reason]
notificationName:ASGrowlTrackIgnored
iconData:nil
priority:0
isSticky:false
clickContext:nil];
}
-(void)lastfm:(Lastfm*)lastfm requiresAuth:(NSURL*)url
{
if (![GrowlApplicationBridge isGrowlInstalled] || ![GrowlApplicationBridge isGrowlRunning]) {
[[NSWorkspace sharedWorkspace] openURL:url];
return;
}
[GrowlApplicationBridge notifyWithTitle:@"Authentication Required"
description:@"Before you can scrobble, Last.fm want you to approve this app at their website. Click here to open your browser at the authorisation page."
notificationName:ASGrowlAuthenticationRequired
iconData:nil
priority:1
isSticky:true
clickContext:[url absoluteString] // click context must be NSCoding compliant
identifier:ASGrowlAuthenticationRequired];
}
-(void)lastfm:(Lastfm*)lastfm errorCode:(int)code errorMessage:(NSString*)message
{
[GrowlApplicationBridge notifyWithTitle:[NSString stringWithFormat:@"Error Code %d", code]
description:message
notificationName:ASGrowlErrorCommunication
iconData:nil
priority:2
isSticky:false
clickContext:nil
identifier:[message stringByAppendingString:ASGrowlErrorCommunication]];
}
-(void)lastfm:(Lastfm*)lastfm metadata:(NSDictionary*)metadata betterdata:(NSDictionary*)betterdata
{
[GrowlApplicationBridge notifyWithTitle:@"Suggested Metadata Correction"
description:betterdata.prettyTitle
notificationName:ASGrowlCorrectionSuggestion
iconData:nil
priority:-1
isSticky:false
clickContext:nil];
}
-(void)lastfm:(Lastfm*)lastfm scrobbled:(NSDictionary*)track failureMessage:(NSString*)message
{
NSMenuItem* item = [history_menu itemAtIndex:0];
if (!item.isEnabled)
[history_menu removeItem:item];
NSString* title = track.prettyTitle;
if (message) {
status_item.image = [NSImage imageNamed:@"icon_red"];
title = [title stringByAppendingFormat:@" (Failed: %@)", message];
} else {
status_item.image = [NSImage imageNamed:@"icon_green"];
title = [title stringByAppendingFormat:@" (OK)"];
}
item = [[NSMenuItem alloc] initWithTitle:title action:@selector(historyItemClicked:) keyEquivalent:@""];
[item setTarget:self];
[item setRepresentedObject:track.url];
[history_menu insertItem:item atIndex:0];
[item release];
// 18 items is about an hour
if([history_menu numberOfItems] > 18)
[history_menu removeItemAtIndex:15];
}
-(void)historyItemClicked:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[sender representedObject]];
}
-(void)growlNotificationWasClicked:(id)dict
{
if ([dict isKindOfClass:[NSString class]])
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:dict]];
}
else if([[dict objectForKey:@"Notification Name"] isEqualToString:ASGrowlLoveTrackQuery])
{
if (listener.track.pid == [dict pid])
[self love:self];
else
[lastfm love:dict];
// TODO need some kind of feedback
}
else
[[NSWorkspace sharedWorkspace] openURL:[dict url]];
}
-(void)growlNotificationTimedOut:(id)dict
{
// this because "not all displays support clickContext"
if ([dict isKindOfClass:[NSString class]]) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:dict]];
}
}
-(IBAction)love:(id)sender
{
const bool b = [lastfm love:listener.track];
if (b) {
[love setEnabled:false];
[love setTitle:@"Loved"];
}
}
-(IBAction)tag:(id)sender
{
NSURL* url = listener.track.url;
NSString* path = [[url path] stringByAppendingPathComponent:@"+tags"];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:path relativeToURL:url]];
}
-(IBAction)share:(id)sender
{
if(!sharewc)
sharewc = [[ShareWindowController alloc] initWithWindowNibName:@"ShareWindow"];
[sharewc showWindow:self];
[sharewc setTrack:listener.track];
[sharewc setLastfm:lastfm];
[sharewc.window makeKeyWindow];
}
-(IBAction)lyrics:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:listener.track.lyricWikiUrl];
}
-(IBAction)startAtLogin:(id)sender
{
FSRef fsref;
if (!scrobsub_fsref(&fsref)) return;
LSSharedFileListRef login_items_ref = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (login_items_ref == NULL) return;
LSSharedFileListItemRef item;
if (NSOffState == [sender state]){
item = LSSharedFileListInsertItemFSRef(login_items_ref,
kLSSharedFileListItemLast,
NULL, // name
NULL, // icon
&fsref,
NULL, NULL);
if (item){
[sender setState:NSOnState];
CFRelease(item);
}
}
else if ((item = audioscrobbler_session_login_item(login_items_ref))){
LSSharedFileListItemRemove(login_items_ref, item);
[sender setState:NSOffState];
}
CFRelease(login_items_ref);
}
-(IBAction)installDashboardWidget:(id)sender
{
NSString* bz2 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Last.fm.wdgt.tar.bz2"];
NSTask* task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/tar"];
[task setCurrentDirectoryPath:downloads()];
[task setArguments:[NSArray arrayWithObjects:@"xf", bz2, nil]];
[task launch];
[task waitUntilExit];
[[NSWorkspace sharedWorkspace] openFile:[[task currentDirectoryPath] stringByAppendingPathComponent:@"Last.fm.wdgt"]];
[task release];
}
-(IBAction)activateAutoDash:(id)sender
{
if ([sender state] == NSOnState)
autodash = [[AutoDash alloc] init];
else
[autodash release];
}
-(IBAction)about:(id)sender
{
// http://www.cocoadev.com/index.pl?NSStatusItem
// LSUIElement screws up Window ordering
[NSApp activateIgnoringOtherApps:YES];
[NSApp orderFrontStandardAboutPanel:sender];
}
-(IBAction)moreRecentHistory:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[Lastfm urlForUser:[lastfm username]]];
}
@end