time remaining works now.
[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.";
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     
31     NSLog(@"iTunes Plugin loaded");
32     return YES;
33 }
34
35 - (BOOL)halt
36 {
37     iTunesPSN.highLongOfPSN = kNoProcess;
38     
39     //Unregister for application termination in NSWorkspace
40     return YES;
41 }
42
43 - (NSArray *)sources
44 {
45     return nil;
46 }
47
48 - (int)currentSourceIndex
49 {
50     return nil;
51 }
52
53 - (NSArray *)playlistsForCurrentSource
54 {
55     return nil;
56 }
57
58 - (int)currentPlaylistIndex
59 {
60     NSString *result;
61     result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pidx"
62                 fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd"
63                 appPSN:[self iTunesPSN]];
64     NSLog(@"result: %@", result);
65     return nil;
66 }
67
68 - (NSString *)songTitleAtIndex
69 {
70     return nil; 
71 }
72
73 - (int)currentSongIndex
74 {
75     NSString *result;
76     result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pidx"
77                 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
78                 appPSN:[self iTunesPSN]];
79     NSLog(@"result: %@", result);
80     return nil;
81 }
82
83 - (NSString *)currentSongTitle
84 {
85     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam"
86                 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
87                 appPSN:[self iTunesPSN]];
88 }
89
90 - (NSString *)currentSongArtist
91 {
92     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt"
93                 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
94                 appPSN:[self iTunesPSN]];
95 }
96
97 - (NSString *)currentSongAlbum
98 {
99     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb"
100                 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
101                 appPSN:[self iTunesPSN]];
102 }
103
104 - (NSString *)currentSongGenre
105 {
106     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen"
107                 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
108                 appPSN:[self iTunesPSN]];
109 }
110
111 - (NSString *)currentSongLength
112 {
113     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pDur"
114                 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
115                 appPSN:[self iTunesPSN]];
116 }
117
118 - (NSString *)currentSongRemaining
119 {
120     long duration = [[ITAppleEventCenter sharedCenter]
121                         sendTwoTierAEWithRequestedKeyForNumber:@"pDur"
122                         fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
123                         appPSN:[self iTunesPSN]];
124     long current = [[ITAppleEventCenter sharedCenter]
125                         sendAEWithRequestedKeyForNumber:@"pPos"
126                         eventClass:@"core" eventID:@"getd"
127                         appPSN:[self iTunesPSN]];
128     
129     return [[NSNumber numberWithLong:duration - current] stringValue];
130 }
131
132 - (NSArray *)eqPresets;
133 {
134     return nil;
135 }
136
137 - (BOOL)play
138 {
139     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play"
140             appPSN:[self iTunesPSN]];
141     return YES;
142 }
143
144 - (BOOL)pause
145 {
146     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus"
147             appPSN:[self iTunesPSN]];
148     return YES;
149 }
150
151 - (BOOL)goToNextSong
152 {
153     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next"
154             appPSN:[self iTunesPSN]];
155     
156     return YES;
157 }
158
159 - (BOOL)goToPreviousSong
160 {
161     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev"
162             appPSN:[self iTunesPSN]];
163     return YES;
164 }
165
166 - (BOOL)goToNextPlaylist
167 {
168     return NO;
169 }
170
171 - (BOOL)goToPreviousPlaylist
172 {
173     return NO;
174 }
175
176 - (BOOL)switchToSourceAtIndex:(int)index
177 {
178     return NO;
179 }
180
181 - (BOOL)switchToPlaylistAtIndex:(int)index
182 {
183     return NO;
184 }
185
186 - (BOOL)switchToSongAtIndex:(int)index
187 {
188     return NO;
189 }
190
191 - (BOOL)switchToEQAtIndex:(int)index
192 {
193     return NO;
194 }
195
196 - (ProcessSerialNumber)iTunesPSN
197 {
198     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
199     ProcessSerialNumber number;
200     int i;
201     
202     number.highLongOfPSN = kNoProcess;
203     
204     for (i = 0; i < [apps count]; i++)
205     {
206         NSDictionary *curApp = [apps objectAtIndex:i];
207         
208         if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
209         {
210             number.highLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberHigh"] intValue];
211             number.lowLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberLow"] intValue];
212         }
213     }
214     return number;
215 }
216
217 @end