These are justsome formatting changes, but if I dont check them in, I'll keep having...
[MenuTunes.git] / iTunesRemote.m
1 #import "iTunesRemote.h"
2
3 @implementation iTunesRemote
4
5 + (id)remote
6 {
7     return [[[iTunesRemote alloc] init] autorelease];
8 }
9
10 - (NSString *)title
11 {
12     return @"iTunes Plug-in";
13 }
14
15 - (NSString *)information;
16 {
17     return @"Default MenuTunes plugin to control iTunes. Written by iThink Software.";
18 }
19
20 - (NSImage *)icon
21 {
22     return nil;
23 }
24
25 - (BOOL)begin
26 {
27     iTunesPSN = [self iTunesPSN];
28
29     //Register for application termination in NSWorkspace
30     [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
31     [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationTerminated:) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
32
33     return YES;
34 }
35
36 - (BOOL)halt
37 {
38     iTunesPSN.highLongOfPSN = kNoProcess;
39
40     //Unregister for application termination in NSWorkspace
41     [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
42
43     return YES;
44 }
45
46 - (BOOL)isAppRunning
47 {
48     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
49     int i;
50     int count = [apps count];
51
52     for (i = 0; i < count; i++) {
53         if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"]
54                 isEqualToString:@"iTunes"]) {
55             return YES;
56         }
57     }
58     return NO;
59 }
60
61 - (PlayerState)playerState
62 {
63     long result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
64     
65     switch (result)
66     {
67         default:
68         case 'kPSS':
69             return stopped;
70         case 'kPSP':
71             return playing;
72         case 'kPSp':
73             return paused;
74         case 'kPSR':
75             return rewinding;
76         case 'kPSF':
77             return forwarding;
78     }
79     
80     return stopped;
81 }
82
83 - (NSArray *)playlists
84 {
85     long i = 0;
86     const signed long numPlaylists = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cPly'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
87     NSMutableArray *playlists = [[NSMutableArray alloc] initWithCapacity:numPlaylists];
88     
89     for (i = 1; i <= numPlaylists; i++) {
90         const long j = i;
91         NSString *sendStr = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cPly'), seld:long(%lu), from:'null'() } }",(unsigned long)j];
92         NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:sendStr eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN fixForAECrappiness:i];
93         [playlists addObject:theObj];
94     }
95     return [playlists autorelease];
96 }
97
98 - (int)numberOfSongsInPlaylistAtIndex:(int)index
99 {
100     return [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:[NSString stringWithFormat:@"kocl:type('cTrk'), '----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:'null'() }",index] eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN]
101
102 ;
103 }
104
105 - (NSString *)classOfPlaylistAtIndex:(int)index
106 {
107     int realResult = [[ITAppleEventCenter sharedCenter]
108                 sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
109
110     if (realResult == 'cRTP') return @"radio tuner playlist";
111     else return @"playlist";
112 }
113
114 - (int)currentPlaylistIndex
115 {
116     int result;
117     result = [[ITAppleEventCenter sharedCenter]
118                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
119     return result;
120 }
121
122 - (NSString *)songTitleAtIndex:(int)index
123 {
124     return [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cTrk'), seld:long(%lu), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } } }",index] eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
125 }
126
127 - (int)currentSongIndex
128 {
129     int result;
130     result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
131     return result;
132 }
133
134 - (NSString *)currentSongTitle
135 {
136     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
137 }
138
139 - (NSString *)currentSongArtist
140 {
141     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
142 }
143
144 - (NSString *)currentSongAlbum
145 {
146     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
147 }
148
149 - (NSString *)currentSongGenre
150 {
151     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
152 }
153
154 - (NSString *)currentSongLength
155 {
156     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
157 }
158
159 - (NSString *)currentSongRemaining
160 {
161     long duration = [[ITAppleEventCenter sharedCenter]
162                         sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
163     long current = [[ITAppleEventCenter sharedCenter]
164                         sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
165
166     return [[NSNumber numberWithLong:duration - current] stringValue];
167 }
168
169 - (NSArray *)eqPresets;
170 {
171     int i;
172     long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
173     NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
174     
175     for (i = 1; i <= numPresets; i++) {
176         NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cEQP'), seld:long(%lu), from:'null'() } }",i] eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN fixForAECrappiness:i];
177         if (theObj) {
178             [presets addObject:theObj];
179         }
180     }
181     return [presets autorelease];
182 }
183
184 - (int)currentEQPresetIndex
185 {
186     int result;
187     result = [[ITAppleEventCenter sharedCenter]
188                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx"fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd"appPSN:iTunesPSN];
189     return result;
190 }
191
192 - (BOOL)play
193 {
194     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
195     return YES;
196 }
197
198 - (BOOL)pause
199 {
200     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:iTunesPSN];
201     return YES;
202 }
203
204 - (BOOL)goToNextSong
205 {
206     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:iTunesPSN];
207     return YES;
208 }
209
210 - (BOOL)goToPreviousSong
211 {
212     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:iTunesPSN];
213     return YES;
214 }
215
216 - (BOOL)fastForward
217 {
218     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:iTunesPSN];
219     return YES;
220 }
221
222 - (BOOL)rewind
223 {
224     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:iTunesPSN];
225     return YES;
226 }
227
228
229 - (BOOL)switchToPlaylistAtIndex:(int)index
230 {
231     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'name', want:type('cPly'), seld:long(%lu), from:'null'() }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
232     return NO;
233 }
234
235 - (BOOL)switchToSongAtIndex:(int)index
236 {
237     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cTrk'), seld:long(%lu), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
238     return NO;
239 }
240
241 - (BOOL)switchToEQAtIndex:(int)index
242 {
243     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:obj { form:'name', want:type('cEQP'), seld:long(%lu), from:'null'() }, '----':obj { form:'prop', want:type('prop'), seld:type('pEQP'), from:'null'() }",index] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
244     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:1, '----':obj { form:'prop', want:type('prop'), seld:type('pEQ '), from:'null'() }" eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
245     return NO;
246 }
247
248 - (ProcessSerialNumber)iTunesPSN
249 {
250     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
251     ProcessSerialNumber number;
252     int i;
253     int count = [apps count];
254     
255     number.highLongOfPSN = kNoProcess;
256     
257     for (i = 0; i < count; i++)
258     {
259         NSDictionary *curApp = [apps objectAtIndex:i];
260         
261         if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
262         {
263             number.highLongOfPSN = [[curApp objectForKey:
264                 @"NSApplicationProcessSerialNumberHigh"] intValue];
265             number.lowLongOfPSN = [[curApp objectForKey:
266                 @"NSApplicationProcessSerialNumberLow"] intValue];
267         }
268     }
269     return number;
270 }
271
272 - (void)applicationLaunched:(NSNotification *)note
273 {
274     NSDictionary *info = [note userInfo];
275
276     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
277         iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
278         iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
279
280         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
281     }
282 }
283
284 - (void)applicationTerminated:(NSNotification *)note
285 {
286     NSDictionary *info = [note userInfo];
287
288     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
289         iTunesPSN.highLongOfPSN = kNoProcess;
290         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];
291     }
292 }
293
294 @end