Fixing CVS and spacing lameness
[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:type('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            for (i = 1; i <= numPresets; i++) {
178                   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];
179                   if (theObj) [presets addObject:theObj];
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:[self iTunesPSN]];
189     return result;
190 }
191
192 - (BOOL)play
193 {
194     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:[self iTunesPSN]];
195     return YES;
196 }
197
198 - (BOOL)pause
199 {
200     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:[self iTunesPSN]];
201     return YES;
202 }
203
204 - (BOOL)goToNextSong
205 {
206     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:[self iTunesPSN]];
207     return YES;
208 }
209
210 - (BOOL)goToPreviousSong
211 {
212     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:[self iTunesPSN]];
213     return YES;
214 }
215
216 - (BOOL)fastForward
217 {
218     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:[self iTunesPSN]];
219     return YES;
220 }
221
222 - (BOOL)rewind
223 {
224     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:[self 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:[self 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:[self 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:[self iTunesPSN]];
244     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:1, '----':obj { form:'prop', want:type('prop'), seld:type('pEQ '), from:'null'() }" eventClass:@"core" eventID:@"setd" appPSN:[self iTunesPSN]];
245     return NO;
246 }
247
248 - (ProcessSerialNumber)iTunesPSN
249 {
250     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
251     ProcessSerialNumber number;
252     int i, count = [apps count];
253
254                number.highLongOfPSN = kNoProcess;
255
256                for (i = 0; i < count; i++)
257                       {
258                           NSDictionary *curApp = [apps objectAtIndex:i];
259                          
260                                  if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
261                                      {
262                                                 number.highLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberHigh"] intValue];
263                                                 number.lowLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberLow"] intValue];
264                                             }
265                              }
266     return number;}
267
268 - (void)applicationLaunched:(NSNotification *)note
269 {
270     NSDictionary *info = [note userInfo];
271
272     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
273         iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
274         iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
275
276         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
277     }
278 }
279
280 - (void)applicationTerminated:(NSNotification *)note
281 {
282     NSDictionary *info = [note userInfo];
283
284     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
285         iTunesPSN.highLongOfPSN = kNoProcess;
286         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];
287     }
288 }
289
290 @end