Bugfixing
[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,count = [apps count];
50
51     for (i = 0; i < count; i++) {
52         if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"]
53                 isEqualToString:@"iTunes"]) {
54             return YES;
55         }
56     }
57     return NO;
58 }
59
60 - (PlayerState)playerState
61 {
62     long result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
63     
64     switch (result)
65     {
66         default:
67         case 'kPSS':
68             return stopped;
69         case 'kPSP':
70             return paused;
71         case 'kPSp':
72             return playing;
73         case 'kPSR':
74             return rewinding;
75         case 'kPSF':
76             return forwarding;
77     }
78     
79     return stopped;
80 }
81
82 - (NSArray *)playlists
83 {
84     long i = 0;
85     const signed long numPlaylists = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cPly'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:[self iTunesPSN]];
86     NSMutableArray *playlists = [[NSMutableArray alloc] initWithCapacity:numPlaylists];
87     NSLog(@"entered playlist");
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:[self iTunesPSN] fixForAECrappiness:i];
93                   NSLog(@"sent event cur %d max %d",i,numPlaylists);
94                   [playlists addObject:theObj];
95            }
96            NSLog(@"playlists end");
97            return [playlists autorelease];
98 }
99
100 - (int)numberOfSongsInPlaylistAtIndex:(int)index
101 {
102     return [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:[NSString stringWithFormat:@"kocl:'cTrk', '----':obj { form:'indx', want:type('cPly'), seld:long(\%lu), from:'null'() }",index] eventClass:@"core" eventID:@"cnte" appPSN:[self iTunesPSN]]
103
104 ;
105 }
106
107 - (NSString *)classOfPlaylistAtIndex:(int)index
108 {
109     int realResult = [[ITAppleEventCenter sharedCenter]
110                 sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
111
112     if (realResult == 'cRTP') return @"radio tuner playlist";
113     else return @"playlist";
114 }
115
116 - (int)currentPlaylistIndex
117 {
118     int result;
119     result = [[ITAppleEventCenter sharedCenter]
120                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
121     return result;
122 }
123
124 - (NSString *)songTitleAtIndex:(int)index
125 {
126     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:[self iTunesPSN]];
127 }
128
129 - (int)currentSongIndex
130 {
131     int result;
132     result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
133     return result;
134 }
135
136 - (NSString *)currentSongTitle
137 {
138     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
139 }
140
141 - (NSString *)currentSongArtist
142 {
143     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
144 }
145
146 - (NSString *)currentSongAlbum
147 {
148     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
149 }
150
151 - (NSString *)currentSongGenre
152 {
153     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
154 }
155
156 - (NSString *)currentSongLength
157 {
158     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
159 }
160
161 - (NSString *)currentSongRemaining
162 {
163     long duration = [[ITAppleEventCenter sharedCenter]
164                         sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
165     long current = [[ITAppleEventCenter sharedCenter]
166                         sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
167
168     return [[NSNumber numberWithLong:duration - current] stringValue];
169 }
170
171 - (NSArray *)eqPresets;
172 {
173     int i;
174     long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:[self iTunesPSN]];
175     NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
176
177
178            for (i = 0; i < numPresets; i++) {
179                   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:[self iTunesPSN] fixForAECrappiness:i];
180                   if (theObj) [presets addObject:theObj];
181            }
182            return [presets autorelease];
183 }
184
185 - (int)currentEQPresetIndex
186 {
187     int result;
188     result = [[ITAppleEventCenter sharedCenter]
189                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx"
190                                                                                                           fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd"
191                                                                                                                                     appPSN:[self iTunesPSN]];
192     return result;
193 }
194
195 - (BOOL)play
196 {
197     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play"
198                                                                                                                                                    appPSN:[self iTunesPSN]];
199     return YES;
200 }
201
202 - (BOOL)pause
203 {
204     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus"
205                                                                                                                                                    appPSN:[self iTunesPSN]];
206     return YES;
207 }
208
209 - (BOOL)goToNextSong
210 {
211     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next"
212                                                                                                                                                    appPSN:[self iTunesPSN]];
213     return YES;
214 }
215
216 - (BOOL)goToPreviousSong
217 {
218     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev"
219                                                                                                                                                    appPSN:[self iTunesPSN]];
220     return YES;
221 }
222
223 - (BOOL)fastForward
224 {
225     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast"
226                                                                                                                                                    appPSN:[self iTunesPSN]];
227     return YES;
228 }
229
230 - (BOOL)rewind
231 {
232     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd"
233                                                                                                                                                    appPSN:[self iTunesPSN]];
234     return YES;
235 }
236
237
238 - (BOOL)switchToPlaylistAtIndex:(int)index
239 {
240     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'name', want:type('cPly'), seld:long(\%lu), from:'null'() }",index] eventClass:@"hook" eventID:@"Play" appPSN:[self iTunesPSN]];
241     return NO;
242 }
243
244 - (BOOL)switchToSongAtIndex:(int)index
245 {
246     [[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:[self iTunesPSN]];
247     return NO;
248 }
249
250 - (BOOL)switchToEQAtIndex:(int)index
251 {
252     [[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:[self iTunesPSN]];
253     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:1, '----':obj { form:'prop', want:type('prop'), seld:type('pEQ '), from:'null'() }" eventClass:@"core" eventID:@"setd" appPSN:[self iTunesPSN]];
254     return NO;
255 }
256
257 - (ProcessSerialNumber)iTunesPSN
258 {
259     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
260     ProcessSerialNumber number;
261     int i, count = [apps count];
262
263                number.highLongOfPSN = kNoProcess;
264
265                for (i = 0; i < count; i++)
266                       {
267                           NSDictionary *curApp = [apps objectAtIndex:i];
268                          
269                                  if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
270                                      {
271                                                 number.highLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberHigh"] intValue];
272                                                 number.lowLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberLow"] intValue];
273                                             }
274                              }
275     return number;}
276
277 - (void)applicationLaunched:(NSNotification *)note
278 {
279     NSDictionary *info = [note userInfo];
280
281     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
282         iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
283         iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
284
285         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
286     }
287 }
288
289 - (void)applicationTerminated:(NSNotification *)note
290 {
291     NSDictionary *info = [note userInfo];
292
293     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
294         iTunesPSN.highLongOfPSN = kNoProcess;
295         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];
296     }
297 }
298
299 @end