Reset the screen that the status window is on when the system's screens change.
[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                 _currentType = StatusWindowNoType;
48                 
49         _window = [[StatusWindow sharedWindow] retain];
50         df = [[NSUserDefaults standardUserDefaults] retain];
51         
52         exitDelay  = [df floatForKey:@"statusWindowVanishDelay"];
53         entryClass = [df stringForKey:@"statusWindowAppearanceEffect"];
54         exitClass  = [df stringForKey:@"statusWindowVanishEffect"];
55         entrySpeed = [df floatForKey:@"statusWindowAppearanceSpeed"];
56         exitSpeed  = [df floatForKey:@"statusWindowVanishSpeed"];
57                 
58                 screenIndex = [df integerForKey:@"statusWindowScreenIndex"];
59                 if (screenIndex >= [screens count]) {
60                         screenIndex = 0;
61                 }
62                 [_window setScreen:[screens objectAtIndex:screenIndex]];
63                 
64         [_window setExitMode:ITTransientStatusWindowExitAfterDelay];
65         [_window setExitDelay:(exitDelay ? exitDelay : 4.0)];
66         
67         [_window setHorizontalPosition:[df integerForKey:@"statusWindowHorizontalPosition"]];
68         [_window setVerticalPosition:[df integerForKey:@"statusWindowVerticalPosition"]];
69         
70         [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
71         
72         if ( [classList containsObject:NSClassFromString(entryClass)] ) {
73             entryEffect = [[[NSClassFromString(entryClass) alloc] initWithWindow:_window] autorelease];
74         } else {
75             entryEffect = [[[ITCutWindowEffect alloc] initWithWindow:_window] autorelease];
76         }
77         
78         if ( [classList containsObject:NSClassFromString(exitClass)] ) {
79             exitEffect = [[[NSClassFromString(exitClass) alloc] initWithWindow:_window] autorelease];
80         } else {
81             exitEffect = [[[ITDissolveWindowEffect alloc] initWithWindow:_window] autorelease];
82         }
83         
84         [_window setEntryEffect:entryEffect];
85         [_window setExitEffect:exitEffect];
86         
87         [[_window entryEffect] setEffectTime:(entrySpeed ? entrySpeed : 0.8)];
88         [[_window exitEffect]  setEffectTime:(exitSpeed  ? exitSpeed  : 0.8)];
89         
90         [(ITTSWBackgroundView *)[_window contentView]setBackgroundMode:
91             (ITTSWBackgroundMode)[df integerForKey:@"statusWindowBackgroundMode"]];
92         
93         colorData = [df dataForKey:@"statusWindowBackgroundColor"];
94         
95         if ( colorData ) {
96             [(ITTSWBackgroundView *)[_window contentView] setBackgroundColor:
97                 (NSColor *)[NSUnarchiver unarchiveObjectWithData:colorData]];
98         } else {
99             [(ITTSWBackgroundView *)[_window contentView] setBackgroundColor:[NSColor blueColor]];
100         }
101         
102         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenParametersChanged:) name:NSApplicationDidChangeScreenParametersNotification object:nil];
103     }
104     
105     return self;
106 }
107
108 - (void)dealloc
109 {
110     [[NSNotificationCenter defaultCenter] removeObserver:self];
111     
112     [_window release];
113     [super dealloc];
114 }
115
116 - (void)screenParametersChanged:(NSNotification *)notification
117 {
118     NSArray *screens = [NSScreen screens];
119     int screenIndex = [df integerForKey:@"statusWindowScreenIndex"];
120     
121     if (screenIndex >= [screens count]) {
122         screenIndex = 0;
123     }
124     [_window setScreen:[screens objectAtIndex:screenIndex]];
125 }
126
127 - (void)showSongInfoWindowWithSource:(ITMTRemotePlayerSource)source
128                                title:            (NSString *)title
129                                album:            (NSString *)album
130                               artist:            (NSString *)artist
131                             composer:            (NSString *)composer
132                                 time:            (NSString *)time  // FLOW: Should probably be NSDate or something.
133                                track:            (NSString *)track
134                               rating:                   (int)rating
135                            playCount:                   (int)playCount
136                                image:             (NSImage *)art
137 {
138     NSImage  *image = nil;
139     NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:title];
140     
141     if ( art != nil ) {
142         image = art;
143     } else if ( source == ITMTRemoteLibrarySource ) {
144         image = [NSImage imageNamed:@"Library"];
145     } else if ( source == ITMTRemoteCDSource ) {
146         image = [NSImage imageNamed:@"CD"];
147     } else if ( source == ITMTRemoteRadioSource ) {
148         image = [NSImage imageNamed:@"Radio"];
149     } else if ( source == ITMTRemoteiPodSource ) {
150         image = [NSImage imageNamed:@"iPod"];
151     } else if ( source == ITMTRemoteGenericDeviceSource ) {
152         image = [NSImage imageNamed:@"MP3Player"];
153     } else if ( source == ITMTRemoteSharedLibrarySource ) {
154         image = [NSImage imageNamed:@"Library"];
155     }
156     
157     [_window setImage:image];
158         [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
159     
160     if ( album ) {
161                 [[text mutableString] appendFormat:@"\n%@", album];
162         //text = [text stringByAppendingString:[@"\n" stringByAppendingString:album]];
163     }
164     if ( artist ) {
165                 [[text mutableString] appendFormat:@"\n%@", artist];
166         //text = [text stringByAppendingString:[@"\n" stringByAppendingString:artist]];
167     }
168     if ( composer ) {
169                 [[text mutableString] appendFormat:@"\n%@", composer];
170         //text = [text stringByAppendingString:[@"\n" stringByAppendingString:composer]];
171     }
172     if ( time ) {
173                 _timeRange = NSMakeRange([[text mutableString] length] + 1, [time length]);
174                 [[text mutableString] appendFormat:@"\n%@", time];
175         //text = [text stringByAppendingString:[@"\n" stringByAppendingString:time]];
176     }
177     if ( track ) {
178                 [[text mutableString] appendFormat:@"\n%@", track];
179         //text = [text stringByAppendingString:[@"\n" stringByAppendingString:track]];
180     }
181     if (playCount > -1) {
182                 [[text mutableString] appendFormat:@"\n%@: %i", NSLocalizedString(@"playCount", @"Play Count"), playCount];
183         //text = [text stringByAppendingString:[NSString stringWithFormat:@"\n%@: %i", NSLocalizedString(@"playCount", @"Play Count"), playCount]];
184     }
185     if ( rating > -1 ) {
186
187         NSString *ratingString = [NSString string];
188         NSString *emptyChar    = [NSString stringWithUTF8String:"☆"];
189         NSString *fullChar     = [NSString stringWithUTF8String:"★"];
190         int       i, start = [[text mutableString] length], size = 18;
191         
192         for ( i = 1; i < 6; i++ ) {
193                 
194             if ( rating >= i ) {
195                 ratingString = [ratingString stringByAppendingString:fullChar];
196             } else {
197                 ratingString = [ratingString stringByAppendingString:emptyChar];
198             }
199         }
200                 
201                 [[text mutableString] appendFormat:@"\n%@", ratingString];
202                 if ([_window sizing] == ITTransientStatusWindowSmall) {
203                         size /= SMALL_DIVISOR;
204                 } else if ([_window sizing] == ITTransientStatusWindowMini) {
205                         size /= MINI_DIVISOR;
206                 }
207                 [text setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"AppleGothic" size:size], NSFontAttributeName, nil, nil] range:NSMakeRange(start + 1, 5)];
208         //text = [text stringByAppendingString:[@"\n" stringByAppendingString:ratingString]];
209     }
210     
211         _currentType = StatusWindowTrackInfoType;
212     [_window buildTextWindowWithString:text];
213     [_window appear:self];
214         [text release];
215 }
216
217 - (void)showAlbumArtWindowWithImage:(NSImage *)image
218 {
219         if (image) {
220                 _currentType = StatusWindowAlbumArtType;
221                 [_window setImage:[NSImage imageNamed:@"Library"]];
222                 [_window buildImageWindowWithImage:image];
223                 [_window appear:self];
224         }
225 }
226
227 - (void)showAlbumArtWindowWithErrorText:(NSString *)string
228 {
229         if (string && [string length] > 0) {
230                 _currentType = StatusWindowAlbumArtType;
231                 [_window buildTextWindowWithString:string];
232                 [_window appear:self];
233         }
234 }
235
236 - (void)showUpcomingSongsWindowWithTitles:(NSArray *)titleStrings
237 {
238 //  NSString *bull = [NSString stringWithUTF8String:"‣ "];
239     NSString *bull = [NSString stringWithUTF8String:"♪ "];
240     NSString *end  = [@"\n" stringByAppendingString:bull];
241     [_window setImage:[NSImage imageNamed:@"Upcoming"]];
242     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
243     [_window buildTextWindowWithString:[bull stringByAppendingString:[titleStrings componentsJoinedByString:end]]];
244         _currentType = StatusWindowUpcomingSongsType;
245     [_window appear:self];
246 }
247
248 - (void)showVolumeWindowWithLevel:(float)level
249 {
250     [_window setImage:[NSImage imageNamed:@"Volume"]];
251     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
252     [_window buildMeterWindowWithCharacter:[NSString stringWithUTF8String:"▊"]
253                                       size:18
254                                      count:10
255                                     active:( ceil(level * 100) / 10 )];
256         _currentType = StatusWindowVolumeType;
257     [_window appear:self];
258 }
259
260 - (void)showRatingWindowWithRating:(float)rating
261 {
262     [_window setImage:[NSImage imageNamed:@"Rating"]];
263     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
264     [_window buildMeterWindowWithCharacter:[NSString stringWithUTF8String:"★"]
265                                       size:48
266                                      count:5
267                                     active:( ceil(rating * 100) / 20 )];
268     _currentType = StatusWindowRatingType;
269         [_window appear:self];
270 }
271
272 - (void)showShuffleWindow:(BOOL)shuffle
273 {
274     [_window setImage:[NSImage imageNamed:@"Shuffle"]];
275     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
276     [_window buildTextWindowWithString:( shuffle ? NSLocalizedString(@"shuffleOn", @"Shuffle On") : NSLocalizedString(@"shuffleOff", @"Shuffle Off"))];
277         _currentType = StatusWindowRatingType;
278     [_window appear:self];
279 }
280
281 - (void)showRepeatWindowWithMode:(StatusWindowRepeatMode)mode
282 {
283     NSString *string = nil;
284     
285     if ( mode == StatusWindowRepeatNone ) {
286         string = NSLocalizedString(@"repeatOff", @"Repeat Off");
287     } else if ( mode == StatusWindowRepeatGroup ) {
288         string = NSLocalizedString(@"repeatPlaylist", @"Repeat Playlist");
289     } else if ( mode == StatusWindowRepeatTrack ) {
290         string = NSLocalizedString(@"repeatOneTrack", @"Repeat One Track");;
291     }
292     
293     [_window setImage:[NSImage imageNamed:@"Repeat"]];
294     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
295     [_window buildTextWindowWithString:string];
296         _currentType = StatusWindowRepeatType;
297     [_window appear:self];
298 }
299
300 - (void)showSongShufflabilityWindow:(BOOL)shufflable
301 {
302     [_window setImage:[NSImage imageNamed:@"Shuffle"]];
303     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
304     [_window buildTextWindowWithString:( !shufflable ? NSLocalizedString(@"shufflableOn", @"Current Song Skipped When Shuffling") : NSLocalizedString(@"shufflableOff", @"Current Song Not Skipped When Shuffling"))];
305         _currentType = StatusWindowShufflabilityType;
306     [_window appear:self];
307 }
308
309 - (void)showSetupQueryWindow
310 {
311     NSString *message = NSLocalizedString(@"autolaunch_msg", @"Would you like MenuTunes to launch\nautomatically at startup?");
312
313     [_window setImage:[NSImage imageNamed:@"Setup"]];
314     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
315     [_window buildDialogWindowWithMessage:message
316                             defaultButton:NSLocalizedString(@"launch_at_startup", @"Launch at Startup")
317                           alternateButton:NSLocalizedString(@"launch_manually", @"Launch Manually")
318                                    target:[PreferencesController sharedPrefs]
319                             defaultAction:@selector(autoLaunchOK)
320                           alternateAction:@selector(autoLaunchCancel)];
321
322         _currentType = StatusWindowSetupType;
323     [_window appear:self];
324     [_window setLocked:YES];
325 }
326
327 - (void)showReconnectQueryWindow
328 {
329     NSString *message = NSLocalizedString(@"sharedplayeravailable_msg", @"The selected shared player is available again.\nWould you like to reconnect to it?");
330     [_window setLocked:NO];
331     [_window setImage:[NSImage imageNamed:@"Setup"]];
332     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
333     [_window buildDialogWindowWithMessage:message
334                             defaultButton:NSLocalizedString(@"reconnect", @"Reconnect")
335                           alternateButton:NSLocalizedString(@"ignore", @"Ignore")
336                                    target:[MainController sharedController]
337                             defaultAction:@selector(reconnect)
338                           alternateAction:@selector(cancelReconnect)];
339
340         _currentType = StatusWindowNetworkType;
341     [_window appear:self];
342     [_window setLocked:YES];
343 }
344
345 - (void)showNetworkErrorQueryWindow
346 {
347     NSString *message = NSLocalizedString(@"sharedplayerunreachable_msg", @"The remote MenuTunes server is unreachable.\nMenuTunes will revert back to the local player.");
348
349     [_window setImage:[NSImage imageNamed:@"Setup"]];
350     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
351     [_window buildDialogWindowWithMessage:message
352                             defaultButton:@" OK "
353                           alternateButton:nil
354                                    target:[MainController sharedController]
355                             defaultAction:@selector(cancelReconnect)
356                           alternateAction:nil];
357
358         _currentType = StatusWindowNetworkType;
359     [_window appear:self];
360     [_window setLocked:YES];
361 }
362
363 - (void)showPreferencesUpdateWindow
364 {
365     NSString *message = NSLocalizedString(@"reconfigureprefs_msg", @"The new features in this version of MenuTunes\nrequire you to reconfigure your preferences.");
366
367     [_window setImage:[NSImage imageNamed:@"Setup"]];
368     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
369     [_window buildDialogWindowWithMessage:message
370                             defaultButton:NSLocalizedString(@"showpreferences", @"Show Preferences")
371                           alternateButton:@"OK"
372                                    target:[MainController sharedController]
373                             defaultAction:@selector(showPreferencesAndClose)
374                           alternateAction:@selector(cancelReconnect)];
375
376         _currentType = StatusWindowPreferencesType;
377     [_window appear:self];
378     [_window setLocked:YES];
379 }
380
381 - (void)showDebugModeEnabledWindow
382 {
383         [_window setImage:[NSImage imageNamed:@"Setup"]];
384     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
385     [_window buildDialogWindowWithMessage:NSLocalizedString(@"debugmodeenabled", @"Debug Mode Enabled")
386                             defaultButton:@"OK"
387                           alternateButton:nil
388                                    target:[MainController sharedController]
389                             defaultAction:@selector(cancelReconnect)
390                           alternateAction:nil];
391
392         _currentType = StatusWindowDebugType;
393     [_window appear:self];
394         [_window setLocked:YES];
395 }
396
397 - (StatusWindowType)currentStatusWindowType
398 {
399         return _currentType;
400 }
401
402 - (void)updateTime:(NSString *)time
403 {
404         if (time && [time length]) {
405                 [_window updateTime:time range:_timeRange];
406         }
407 }
408
409 @end