This repository has been archived by the owner on Jun 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLastifyController.m
267 lines (212 loc) · 6.79 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
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
//
// 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 "SPGrowlDelegate+Lastify.h"
#import "NSButton+Lastify.h"
@implementation LastifyController
@synthesize
lastfm,
currentTrack,
currentArtist,
currentTags;
+ (void)load
{
[SPGrowlDelegate initLastify];
[[LastifyController sharedInstance] initLastfmConnection];
}
+ (LastifyController*)sharedInstance
{
static LastifyController *plugin = nil;
if(!plugin)
plugin = [[LastifyController alloc] init];
return plugin;
}
+ (BOOL)swapMethod:(SEL)firstSelector withMethod:(SEL)secondSelector onClass:(Class)class
{
Method firstMethod, secondMethod;
firstMethod = class_getInstanceMethod(class, firstSelector);
secondMethod = class_getInstanceMethod(class, secondSelector);
if (firstMethod == nil || secondMethod == nil)
return NO;
method_exchangeImplementations(firstMethod, secondMethod);
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
{
if(drawer != nil || [[SPController sharedController] mainWindow] == nil)
return;
[NSBundle loadNibNamed:@"LastifyInterface" owner:self];
[statusImage setImage:nil];
[loveButton setTextColor:[NSColor whiteColor]];
[banButton setTextColor:[NSColor whiteColor]];
[tagButton setTextColor:[NSColor whiteColor]];
[loginButton setTextColor:[NSColor whiteColor]];
[listButton setTextColor:[NSColor whiteColor]];
[drawer setParentWindow:[[SPController sharedController] mainWindow]];
NSSize contentSize = NSMakeSize(71, 200);
[drawer setMaxContentSize:contentSize];
[drawer setMinContentSize:contentSize];
[drawer setLeadingOffset:29];
[drawer setTrailingOffset:10];
[drawer openOnEdge:NSMaxXEdge];
[drawer setDelegate:self];
[drawer setContentSize:contentSize];
}
- (void)displayWorkingIcon
{
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"icon_loading" ofType:@"gif"];
NSImage *img = [[NSImage alloc] initWithContentsOfFile:path];
[statusImage setImage:img];
[img release], img = nil;
}
- (void)displayResultIcon:(BOOL)result
{
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:(result ? @"icon_tick" : @"icon_error") ofType:@"png"];
NSImage *img = [[NSImage alloc] initWithContentsOfFile:path];
[statusImage setImage:img];
[img release], img = nil;
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(clearResultIcon:) userInfo:nil repeats:FALSE];
}
- (void)clearResultIcon:(NSTimer*)timer
{
[statusImage setImage:nil];
}
- (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 'Continue' 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;
[self displayWorkingIcon];
BOOL result = [lastfm loveTrack:currentTrack byArtist:currentArtist];
[self displayResultIcon:result];
}
- (IBAction)banTrack:(id)sender
{
if(!currentTrack || !currentArtist)
return;
[self displayWorkingIcon];
BOOL result = [lastfm banTrack:currentTrack byArtist:currentArtist];
[self displayResultIcon:result];
}
- (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
{
[self displayWorkingIcon];
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];
}
BOOL result = TRUE;
if([addTags count] > 0)
{
NSLog(@"LASTIFY Adding tags: %@", addTags);
result = [lastfm addTags:addTags toTrack:currentTrack byArtist:currentArtist] && result;
}
if([removeTags count] > 0)
{
NSLog(@"LASTIFY Removing tags: %@", removeTags);
result = [lastfm removeTags:removeTags fromTrack:currentTrack byArtist:currentArtist] && result;
}
[removeTags release], removeTags = nil;
[tagPanel orderOut:nil];
[NSApp endSheet:tagPanel];
[self displayResultIcon:result];
}
- (void)startNewTrack:(NSString*)trackName byArtist:(NSString*)artistName
{
NSLog(@"LASTIFY track started: \"%@\" by %@", trackName, artistName);
[self loadUserInterface];
self.currentTrack = trackName;
self.currentArtist = artistName;
}
- (IBAction)taggingCancel:(id)sender
{
[tagPanel orderOut:nil];
[NSApp endSheet:tagPanel];
}
- (IBAction)addTrackToPlaylist:(id)sender
{
[playlistsController setContent:[lastfm getPlaylists]];
[NSApp beginSheet:playlistPanel modalForWindow:[[SPController sharedController] mainWindow] modalDelegate:self didEndSelector:NULL contextInfo:nil];
}
- (IBAction)playlistOK:(id)sender
{
NSDictionary *selectedPlaylist = [[playlistsController selectedObjects] objectAtIndex:0];
if(!selectedPlaylist)
return;
NSString *playlistID = [selectedPlaylist valueForKey:@"id"];
BOOL result = [lastfm addTrack:self.currentTrack byArtist:self.currentArtist toPlaylist:playlistID];
[self displayResultIcon:result];
[playlistPanel orderOut:nil];
[NSApp endSheet:playlistPanel];
}
- (IBAction)playlistCancel:(id)sender
{
[playlistPanel orderOut:nil];
[NSApp endSheet:playlistPanel];
}
- (BOOL)drawerShouldClose:(NSDrawer *)sender
{
return FALSE;
}
@end