/
LastifyController.m
194 lines (155 loc) · 4.56 KB
/
LastifyController.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
//
// LastifyController.m
// Lastify
//
// Created by George on 16/01/2009.
// Copyright 2008 George Brocklehurst. Some rights reserved (see accompanying LICENSE file for details).
//
#import <objc/objc-class.h>
#import "LastifyController.h"
#import "SPController.h"
#import "SPController+Lastify.h"
#import "SPGrowlDelegate+Lastify.h"
#import "NSButton+Lastify.h"
@implementation LastifyController
@synthesize
lastfm,
currentTrack,
currentArtist,
currentTags;
+ (void)load
{
[SPController initLastify];
[SPGrowlDelegate initLastify];
[[LastifyController sharedInstance] initLastfmConnection];
}
+ (LastifyController*)sharedInstance
{
static LastifyController *plugin = nil;
if(!plugin)
plugin = [[LastifyController alloc] init];
return plugin;
}
+ (BOOL)renameSelector:(SEL)originalSelector toSelector:(SEL)newSelector onClass:(Class)class
{
Method method = nil;
method = class_getInstanceMethod(class, originalSelector);
if (method == nil)
return NO;
method->method_name = newSelector;
return YES;
}
- (void)dealloc
{
[lastfm release], lastfm = nil;
[currentTrack release], currentTrack = nil;
[currentArtist release], currentArtist = nil;
[currentTags release], currentTags = nil;
[super dealloc];
}
- (void)initLastfmConnection
{
lastfm = [[LastifyLastfmClient alloc] initWithAPIKey:@"aa31898c9c79401a7ddaa6c8f089ccad" APISecret:@"92773b344ec2e14cd6f5780b83c06265"];
[lastfm authenticateQuietly];
}
- (void)loadUserInterface
{
[NSBundle loadNibNamed:@"LastifyInterface" owner:self];
[loveButton setTextColor:[NSColor whiteColor]];
[banButton setTextColor:[NSColor whiteColor]];
[tagButton setTextColor:[NSColor whiteColor]];
[loginButton setTextColor:[NSColor whiteColor]];
[drawer setParentWindow:[[SPController sharedController] mainWindow]];
NSSize contentSize = NSMakeSize(71, 200);
[drawer setMaxContentSize:contentSize];
[drawer setMinContentSize:contentSize];
[drawer setLeadingOffset:0];
[drawer setTrailingOffset:0];
[drawer openOnEdge:NSMaxXEdge];
[drawer setDelegate:self];
[drawer setContentSize:contentSize];
}
- (IBAction)auth:(id)sender
{
if(lastfm.sessionKey)
return;
[lastfm authenticateQuietly];
if(lastfm.sessionKey)
return;
NSAlert *authAlert = [NSAlert
alertWithMessageText:@"You must authorise Lastify to access your Last.fm profile information"
defaultButton:@"Continue"
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@"If you click OK the Last.fm website will open so you can authorise Lastify"];
if([authAlert runModal] == NSAlertAlternateReturn)
return;
[lastfm authenticate];
if(lastfm.waitingForUserAuth)
{
NSAlert *authCompleteAlert = [NSAlert
alertWithMessageText:@"Authorisation is complete"
defaultButton:@"Continue"
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@"I've authorised Lastify to access my Last.fm profile"];
if([authCompleteAlert runModal] == NSAlertAlternateReturn)
return;
[lastfm startNewSession];
}
}
- (IBAction)loveTrack:(id)sender
{
if(!currentTrack || !currentArtist)
return;
[lastfm loveTrack:currentTrack byArtist:currentArtist];
}
- (IBAction)banTrack:(id)sender
{
if(!currentTrack || !currentArtist)
return;
[lastfm banTrack:currentTrack byArtist:currentArtist];
}
- (IBAction)tagTrack:(id)sender
{
self.currentTags = [lastfm getTagsForTrack:currentTrack byArtist:currentArtist];
[tagField setObjectValue:self.currentTags];
[NSApp beginSheet:tagPanel modalForWindow:[[SPController sharedController] mainWindow] modalDelegate:self didEndSelector:NULL contextInfo:nil];
}
- (IBAction)taggingOK:(id)sender
{
NSArray *newTags = [tagField objectValue];
NSMutableArray *removeTags = [self.currentTags mutableCopy];
NSMutableArray *addTags = [NSMutableArray arrayWithCapacity:[newTags count]];
NSEnumerator *tagEnum = [newTags objectEnumerator];
NSString *tag;
while(tag = [tagEnum nextObject])
{
[removeTags removeObject:tag];
if(![self.currentTags containsObject:tag])
[addTags addObject:tag];
}
if([addTags count] > 0)
{
NSLog(@"********** LASTIFY Adding tags: %@", addTags);
[lastfm addTags:addTags toTrack:currentTrack byArtist:currentArtist];
}
if([removeTags count] > 0)
{
NSLog(@"********** LASTIFY Removing tags: %@", removeTags);
[lastfm removeTags:removeTags fromTrack:currentTrack byArtist:currentArtist];
}
[removeTags release], removeTags = nil;
[tagPanel orderOut:nil];
[NSApp endSheet:tagPanel];
}
- (IBAction)taggingCancel:(id)sender
{
[tagPanel orderOut:nil];
[NSApp endSheet:tagPanel];
}
- (BOOL)drawerShouldClose:(NSDrawer *)sender
{
return FALSE;
}
@end