Added support for attributed strings, which allows us to have non-sucky stars in...
[MenuTunes.git] / StatusWindowController.m
1 #import "StatusWindowController.h"
2 #import "StatusWindow.h"
3 #import "PreferencesController.h"
4 #import "MainController.h"
5
6 #import <ITKit/ITTSWBackgroundView.h>
7 #import <ITKit/ITWindowEffect.h>
8 #import <ITKit/ITCutWindowEffect.h>
9 #import <ITKit/ITDissolveWindowEffect.h>
10 #import <ITKit/ITSlideHorizontallyWindowEffect.h>
11 #import <ITKit/ITSlideVerticallyWindowEffect.h>
12 #import <ITKit/ITPivotWindowEffect.h>
13
14 static StatusWindowController *sharedController;
15
16 @implementation StatusWindowController
17
18
19 + (StatusWindowController *)sharedController
20 {
21     if ( ! sharedController ) {
22         sharedController = [[StatusWindowController alloc] init];
23     }
24     
25     return sharedController;
26 }
27
28
29 - (id)init
30 {
31     if ( ( self = [super init] ) ) {
32         
33         float exitDelay;
34         NSString *entryClass;
35         NSString *exitClass;
36         NSArray  *classList = [ITWindowEffect effectClasses];
37         float entrySpeed;
38         float exitSpeed;
39                 NSArray *screens = [NSScreen screens];
40                 int screenIndex;
41         
42         NSData *colorData;
43         
44         ITWindowEffect *entryEffect;
45         ITWindowEffect *exitEffect;
46         
47         _window = [[StatusWindow sharedWindow] retain];
48         df = [[NSUserDefaults standardUserDefaults] retain];
49         
50         exitDelay  = [df floatForKey:@"statusWindowVanishDelay"];
51         entryClass = [df stringForKey:@"statusWindowAppearanceEffect"];
52         exitClass  = [df stringForKey:@"statusWindowVanishEffect"];
53         entrySpeed = [df floatForKey:@"statusWindowAppearanceSpeed"];
54         exitSpeed  = [df floatForKey:@"statusWindowVanishSpeed"];
55                 
56                 screenIndex = [df integerForKey:@"statusWindowScreenIndex"];
57                 if (screenIndex >= [screens count]) {
58                         screenIndex = 0;
59                 }
60                 [_window setScreen:[screens objectAtIndex:screenIndex]];
61                 
62         [_window setExitMode:ITTransientStatusWindowExitAfterDelay];
63         [_window setExitDelay:(exitDelay ? exitDelay : 4.0)];
64         
65         [_window setHorizontalPosition:[df integerForKey:@"statusWindowHorizontalPosition"]];
66         [_window setVerticalPosition:[df integerForKey:@"statusWindowVerticalPosition"]];
67         
68         [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
69         
70         if ( [classList containsObject:NSClassFromString(entryClass)] ) {
71             entryEffect = [[[NSClassFromString(entryClass) alloc] initWithWindow:_window] autorelease];
72         } else {
73             entryEffect = [[[ITCutWindowEffect alloc] initWithWindow:_window] autorelease];
74         }
75         
76         if ( [classList containsObject:NSClassFromString(exitClass)] ) {
77             exitEffect = [[[NSClassFromString(exitClass) alloc] initWithWindow:_window] autorelease];
78         } else {
79             exitEffect = [[[ITDissolveWindowEffect alloc] initWithWindow:_window] autorelease];
80         }
81         
82         [_window setEntryEffect:entryEffect];
83         [_window setExitEffect:exitEffect];
84         
85         [[_window entryEffect] setEffectTime:(entrySpeed ? entrySpeed : 0.8)];
86         [[_window exitEffect]  setEffectTime:(exitSpeed  ? exitSpeed  : 0.8)];
87         
88         [(ITTSWBackgroundView *)[_window contentView]setBackgroundMode:
89             (ITTSWBackgroundMode)[df integerForKey:@"statusWindowBackgroundMode"]];
90         
91         colorData = [df dataForKey:@"statusWindowBackgroundColor"];
92         
93         if ( colorData ) {
94             [(ITTSWBackgroundView *)[_window contentView] setBackgroundColor:
95                 (NSColor *)[NSUnarchiver unarchiveObjectWithData:colorData]];
96         } else {
97             [(ITTSWBackgroundView *)[_window contentView] setBackgroundColor:[NSColor blueColor]];
98         }
99     }
100     
101     return self;
102 }
103
104 - (void)dealloc
105 {
106     [_window release];
107     [super dealloc];
108 }
109
110 - (void)showSongInfoWindowWithSource:(ITMTRemotePlayerSource)source
111                                title:            (NSString *)title
112                                album:            (NSString *)album
113                               artist:            (NSString *)artist
114                             composer:            (NSString *)composer
115                                 time:            (NSString *)time  // FLOW: Should probably be NSDate or something.
116                                track:            (NSString *)track
117                               rating:                   (int)rating
118                            playCount:                   (int)playCount
119                                image:             (NSImage *)art
120 {
121     NSImage  *image = nil;
122     NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:title];
123     
124     if ( art != nil ) {
125         image = art;
126     } else if ( source == ITMTRemoteLibrarySource ) {
127         image = [NSImage imageNamed:@"Library"];
128     } else if ( source == ITMTRemoteCDSource ) {
129         image = [NSImage imageNamed:@"CD"];
130     } else if ( source == ITMTRemoteRadioSource ) {
131         image = [NSImage imageNamed:@"Radio"];
132     } else if ( source == ITMTRemoteiPodSource ) {
133         image = [NSImage imageNamed:@"iPod"];
134     } else if ( source == ITMTRemoteGenericDeviceSource ) {
135         image = [NSImage imageNamed:@"MP3Player"];
136     } else if ( source == ITMTRemoteSharedLibrarySource ) {
137         image = [NSImage imageNamed:@"Library"];
138     }
139     
140     [_window setImage:image];
141         [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
142     
143     if ( album ) {
144                 [[text mutableString] appendFormat:@"\n%@", album];
145         //text = [text stringByAppendingString:[@"\n" stringByAppendingString:album]];
146     }
147     if ( artist ) {
148                 [[text mutableString] appendFormat:@"\n%@", artist];
149         //text = [text stringByAppendingString:[@"\n" stringByAppendingString:artist]];
150     }
151     if ( composer ) {
152                 [[text mutableString] appendFormat:@"\n%@", composer];
153         //text = [text stringByAppendingString:[@"\n" stringByAppendingString:composer]];
154     }
155     if ( time ) {
156                 [[text mutableString] appendFormat:@"\n%@", time];
157         //text = [text stringByAppendingString:[@"\n" stringByAppendingString:time]];
158     }
159     if ( track ) {
160                 [[text mutableString] appendFormat:@"\n%@", track];
161         //text = [text stringByAppendingString:[@"\n" stringByAppendingString:track]];
162     }
163     if (playCount > -1) {
164                 [[text mutableString] appendFormat:@"\n%@: %i", NSLocalizedString(@"playCount", @"Play Count"), playCount];
165         //text = [text stringByAppendingString:[NSString stringWithFormat:@"\n%@: %i", NSLocalizedString(@"playCount", @"Play Count"), playCount]];
166     }
167     if ( rating > -1 ) {
168
169         NSString *ratingString = [NSString string];
170         NSString *emptyChar    = [NSString stringWithUTF8String:"☆"];
171         NSString *fullChar     = [NSString stringWithUTF8String:"★"];
172         int       i, start = [[text mutableString] length], size = 18;
173         
174         for ( i = 1; i < 6; i++ ) {
175                 
176             if ( rating >= i ) {
177                 ratingString = [ratingString stringByAppendingString:fullChar];
178             } else {
179                 ratingString = [ratingString stringByAppendingString:emptyChar];
180             }
181         }
182                 
183                 [[text mutableString] appendFormat:@"\n%@", ratingString];
184                 if ([_window sizing] == ITTransientStatusWindowSmall) {
185                         size /= SMALL_DIVISOR;
186                 } else if ([_window sizing] == ITTransientStatusWindowMini) {
187                         size /= MINI_DIVISOR;
188                 }
189                 [text setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"AppleGothic" size:size], NSFontAttributeName, nil, nil] range:NSMakeRange(start + 1, 5)];
190         //text = [text stringByAppendingString:[@"\n" stringByAppendingString:ratingString]];
191     }
192     
193     [_window buildTextWindowWithString:text];
194     [_window appear:self];
195 }
196
197 - (void)showUpcomingSongsWindowWithTitles:(NSArray *)titleStrings
198 {
199 //  NSString *bull = [NSString stringWithUTF8String:"‣ "];
200     NSString *bull = [NSString stringWithUTF8String:"♪ "];
201     NSString *end  = [@"\n" stringByAppendingString:bull];
202     [_window setImage:[NSImage imageNamed:@"Upcoming"]];
203     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
204     [_window buildTextWindowWithString:[bull stringByAppendingString:[titleStrings componentsJoinedByString:end]]];
205     [_window appear:self];
206 }
207
208 - (void)showVolumeWindowWithLevel:(float)level
209 {
210     [_window setImage:[NSImage imageNamed:@"Volume"]];
211     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
212     [_window buildMeterWindowWithCharacter:[NSString stringWithUTF8String:"▊"]
213                                       size:18
214                                      count:10
215                                     active:( ceil(level * 100) / 10 )];
216     [_window appear:self];
217 }
218
219 - (void)showRatingWindowWithRating:(float)rating
220 {
221     [_window setImage:[NSImage imageNamed:@"Rating"]];
222     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
223     [_window buildMeterWindowWithCharacter:[NSString stringWithUTF8String:"★"]
224                                       size:48
225                                      count:5
226                                     active:( ceil(rating * 100) / 20 )];
227     [_window appear:self];
228 }
229
230 - (void)showShuffleWindow:(BOOL)shuffle
231 {
232     [_window setImage:[NSImage imageNamed:@"Shuffle"]];
233     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
234     [_window buildTextWindowWithString:( shuffle ? NSLocalizedString(@"shuffleOn", @"Shuffle On") : NSLocalizedString(@"shuffleOff", @"Shuffle Off"))];
235     [_window appear:self];
236 }
237
238 - (void)showRepeatWindowWithMode:(StatusWindowRepeatMode)mode
239 {
240     NSString *string = nil;
241     
242     if ( mode == StatusWindowRepeatNone ) {
243         string = NSLocalizedString(@"repeatOff", @"Repeat Off");
244     } else if ( mode == StatusWindowRepeatGroup ) {
245         string = NSLocalizedString(@"repeatPlaylist", @"Repeat Playlist");
246     } else if ( mode == StatusWindowRepeatTrack ) {
247         string = NSLocalizedString(@"repeatOneTrack", @"Repeat One Track");;
248     }
249     
250     [_window setImage:[NSImage imageNamed:@"Repeat"]];
251     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
252     [_window buildTextWindowWithString:string];
253     [_window appear:self];
254 }
255
256 - (void)showSetupQueryWindow
257 {
258     NSString *message = NSLocalizedString(@"autolaunch_msg", @"Would you like MenuTunes to launch\nautomatically at startup?");
259
260     [_window setImage:[NSImage imageNamed:@"Setup"]];
261     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
262     [_window buildDialogWindowWithMessage:message
263                             defaultButton:NSLocalizedString(@"launch_at_startup", @"Launch at Startup")
264                           alternateButton:NSLocalizedString(@"launch_manually", @"Launch Manually")
265                                    target:[PreferencesController sharedPrefs]
266                             defaultAction:@selector(autoLaunchOK)
267                           alternateAction:@selector(autoLaunchCancel)];
268
269     [_window appear:self];
270     [_window setLocked:YES];
271 }
272
273
274 - (void)showRegistrationQueryWindow
275 {
276     NSString *message = NSLocalizedString(@"trialexpired_msg", @"Your 7-day unlimited trial period has elapsed.\nYou must register to continue using MenuTunes.");
277
278     [_window setImage:[NSImage imageNamed:@"Register"]];
279     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
280     [_window buildDialogWindowWithMessage:message
281                             defaultButton:NSLocalizedString(@"registernow", @"Register Now")
282                           alternateButton:NSLocalizedString(@"quitmenutunes", @"Quit MenuTunes")
283                                    target:[MainController sharedController]
284                             defaultAction:@selector(registerNowOK)
285                           alternateAction:@selector(registerNowCancel)];
286
287     [_window appear:self];
288     [_window setLocked:YES];
289 }
290
291 - (void)showReconnectQueryWindow
292 {
293     NSString *message = NSLocalizedString(@"sharedplayeravailable_msg", @"The selected shared player is available again.\nWould you like to reconnect to it?");
294     [_window setLocked:NO];
295     [_window setImage:[NSImage imageNamed:@"Setup"]];
296     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
297     [_window buildDialogWindowWithMessage:message
298                             defaultButton:NSLocalizedString(@"reconnect", @"Reconnect")
299                           alternateButton:NSLocalizedString(@"ignore", @"Ignore")
300                                    target:[MainController sharedController]
301                             defaultAction:@selector(reconnect)
302                           alternateAction:@selector(cancelReconnect)];
303
304     [_window appear:self];
305     [_window setLocked:YES];
306 }
307
308 - (void)showNetworkErrorQueryWindow
309 {
310     NSString *message = NSLocalizedString(@"sharedplayerunreachable_msg", @"The remote MenuTunes server is unreachable.\nMenuTunes will revert back to the local player.");
311
312     [_window setImage:[NSImage imageNamed:@"Setup"]];
313     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
314     [_window buildDialogWindowWithMessage:message
315                             defaultButton:@" OK "
316                           alternateButton:nil
317                                    target:[MainController sharedController]
318                             defaultAction:@selector(cancelReconnect)
319                           alternateAction:nil];
320
321     [_window appear:self];
322     [_window setLocked:YES];
323 }
324
325 - (void)showPreferencesUpdateWindow
326 {
327     NSString *message = NSLocalizedString(@"reconfigureprefs_msg", @"The new features in this version of MenuTunes\nrequire you to reconfigure your preferences.");
328
329     [_window setImage:[NSImage imageNamed:@"Setup"]];
330     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
331     [_window buildDialogWindowWithMessage:message
332                             defaultButton:NSLocalizedString(@"showpreferences", @"Show Preferences")
333                           alternateButton:@"OK"
334                                    target:[MainController sharedController]
335                             defaultAction:@selector(showPreferencesAndClose)
336                           alternateAction:@selector(cancelReconnect)];
337
338     [_window appear:self];
339     [_window setLocked:YES];
340 }
341
342 - (void)showDebugModeEnabledWindow
343 {
344         [_window setImage:[NSImage imageNamed:@"Setup"]];
345     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
346     [_window buildDialogWindowWithMessage:NSLocalizedString(@"debugmodeenabled", @"Debug Mode Enabled")
347                             defaultButton:@"OK"
348                           alternateButton:nil
349                                    target:[MainController sharedController]
350                             defaultAction:@selector(cancelReconnect)
351                           alternateAction:nil];
352     [_window appear:self];
353         [_window setLocked:YES];
354 }
355
356 @end