695c6845f158af3f510205b2218752b67846b678
[MenuTunes.git] / AudioscrobblerController.m
1 /*
2  *      MenuTunes
3  *  AudioscrobblerController
4  *    Audioscrobbler Support Class
5  *
6  *  Original Author : Kent Sutherland <kent.sutherland@ithinksw.com>
7  *   Responsibility : Kent Sutherland <kent.sutherland@ithinksw.com>
8  *
9  *  Copyright (c) 2005 iThink Software.
10  *  All Rights Reserved
11  *
12  */
13
14 #import "AudioscrobblerController.h"
15
16 static AudioscrobblerController *_sharedController = nil;
17
18 @implementation AudioscrobblerController
19
20 + (void)load
21 {
22         //[[AudioscrobblerController sharedController] attemptHandshake];
23 }
24
25 + (AudioscrobblerController *)sharedController
26 {
27         if (!_sharedController) {
28                 _sharedController = [[AudioscrobblerController alloc] init];
29         }
30         return _sharedController;
31 }
32
33 - (id)init
34 {
35         if ( (self = [super init]) ) {
36                 _handshakeCompleted = NO;
37                 _responseData = nil;
38         }
39         return self;
40 }
41
42 - (void)dealloc
43 {
44         [_responseData release];
45         [super dealloc];
46 }
47
48 - (void)attemptHandshake
49 {
50         NSString *version = [[[NSBundle bundleWithPath:[[NSWorkspace sharedWorkspace] fullPathForApplication:@"iTunes.app"]] infoDictionary] objectForKey:@"CFBundleVersion"], *user = @"Tristrex";
51         NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://post.audioscrobbler.com/?hs=true&p=1.1&c=est&v=\"%@\"&u=\"%@\"", version, user]];
52         NSURLConnection *connection;
53         
54         _responseData = [[NSMutableData alloc] init];
55         connection = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self];
56         
57         _handshakeCompleted = YES;
58 }
59
60 - (BOOL)handshakeCompleted
61 {
62         return _handshakeCompleted;
63 }
64
65 #pragma mark -
66
67 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
68 {
69         NSLog(@"Failed with an error: %@", error);
70 }
71
72 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
73 {
74         [_responseData appendData:data];
75 }
76
77 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
78 {
79         NSString *string = [[NSString alloc] initWithData:_responseData encoding:NSASCIIStringEncoding];
80         NSLog(@"Rawr: %@", string);
81         [string release];
82 }
83
84 @end