forked from bps/scrobblepod
-
Notifications
You must be signed in to change notification settings - Fork 3
/
BGLastFmAuthenticationManager.m
111 lines (90 loc) · 3.92 KB
/
BGLastFmAuthenticationManager.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
//
// BGLastFmAuthenticationManager.m
// ApiHubTester
//
// Created by Ben Gummer on 18/07/08.
// Copyright 2008 Ben Gummer. All rights reserved.
//
#import "BGLastFmAuthenticationManager.h"
#import "BGLastFmWebServiceHandshaker.h"
#import "BGLastFmSubmissionHandshaker.h"
#import "BGLastFmSubmissionHandshakeResponse.h"
#import "HubNotifications.h"
#import "HubStrings.h"
#import "Defines.h"
@implementation BGLastFmAuthenticationManager
@synthesize delegate;
-(id)initWithDelegate:(id)sender {
self = [super init];
if (self != nil) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newSessionKeyAcquired) name:APIHUB_WebServiceAuthorizationProcessing object:nil];
[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(getUrl:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
self.delegate = sender;
if (self.webServiceSessionKey && self.webServiceSessionKey.length>0) [self fetchNewSubmissionSessionKeyUsingWebServiceSessionKey];
}
return self;
}
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void)getUrl:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
NSString *url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
NSString *newToken = [[url componentsSeparatedByString:@"token="] lastObject];
if (newToken.length == 32) {
if ([BGLastFmWebServiceHandshaker fetchSessionKeyUsingToken:newToken]) {
[NSApp activateIgnoringOtherApps:YES];
[self fetchNewSubmissionSessionKeyUsingWebServiceSessionKey];
}
} else {
#if __LP64__
DLog(@"Invalid Token Received: %@ (length is %ld)", newToken,newToken.length);
#else
DLog(@"Invalid Token Received: %@ (length is %d)", newToken,newToken.length);
#endif
}
}
-(void)newSessionKeyAcquired {
DLog(@"Got Username:%@ and Session:%@",self.username,self.webServiceSessionKey);
SEL theSelector = @selector(newWebServiceSessionKeyAcquired);
if ([delegate respondsToSelector:theSelector]) [delegate performSelector:theSelector];
}
-(void)beginNewWebServiceSessionProcedure {
BGLastFmWebServiceHandshaker *tokenFetcher = [[BGLastFmWebServiceHandshaker alloc] init];
[tokenFetcher openAuthorizationSite];
[tokenFetcher release];
}
-(void)fetchNewSubmissionSessionKeyUsingWebServiceSessionKey {
NSString *wsSessionKey = self.webServiceSessionKey;
if (wsSessionKey == nil)
return;
BGLastFmSubmissionHandshaker *submissionFetcher = [[BGLastFmSubmissionHandshaker alloc] init];
BGLastFmSubmissionHandshakeResponse *response = [submissionFetcher performSubmissionHandshakeForUser:self.username withWebServiceSessionKey:self.webServiceSessionKey];
[submissionFetcher release];
NSString *submissionSessionKey = response.sessionKey;
if (submissionSessionKey == nil)
return;
DLog(@"Got Submission Key: %@",submissionSessionKey);
[[NSUserDefaults standardUserDefaults] setObject:submissionSessionKey forKey:BGSubmissionSessionKey];
[[NSUserDefaults standardUserDefaults] setObject:response.nowPlayingURL forKey:BGNowPlayingSubmissionURL];
[[NSUserDefaults standardUserDefaults] setObject:response.postURL forKey:BGScrobblingSubmissionURL];
SEL theSelector = @selector(newSubmissionSessionKeyAcquired);
if ([delegate respondsToSelector:theSelector])
[delegate performSelector:theSelector];
}
-(NSString *)webServiceSessionKey {
return [[NSUserDefaults standardUserDefaults] stringForKey:BGWebServiceSessionKey];
}
-(NSString *)submissionSessionKey {
return [[NSUserDefaults standardUserDefaults] stringForKey:BGSubmissionSessionKey];
}
-(NSString *)username {
return [[NSUserDefaults standardUserDefaults] stringForKey:BGPrefUsername];
}
-(NSString *)nowPlayingSubmissionURL {
return [[NSUserDefaults standardUserDefaults] stringForKey:BGNowPlayingSubmissionURL];
}
-(NSString *)scrobbleSubmissionURL {
return [[NSUserDefaults standardUserDefaults] stringForKey:BGScrobblingSubmissionURL];
}
@end