Fixed various memory leaks.
[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                 _timeRange = NSMakeRange([[text mutableString] length] + 1, [time length]);
157                 [[text mutableString] appendFormat:@"\n%@", time];
158         //text = [text stringByAppendingString:[@"\n" stringByAppendingString:time]];
159     }
160     if ( track ) {
161                 [[text mutableString] appendFormat:@"\n%@", track];
162         //text = [text stringByAppendingString:[@"\n" stringByAppendingString:track]];
163     }
164     if (playCount > -1) {
165                 [[text mutableString] appendFormat:@"\n%@: %i", NSLocalizedString(@"playCount", @"Play Count"), playCount];
166         //text = [text stringByAppendingString:[NSString stringWithFormat:@"\n%@: %i", NSLocalizedString(@"playCount", @"Play Count"), playCount]];
167     }
168     if ( rating > -1 ) {
169
170         NSString *ratingString = [NSString string];
171         NSString *emptyChar    = [NSString stringWithUTF8String:"☆"];
172         NSString *fullChar     = [NSString stringWithUTF8String:"★"];
173         int       i, start = [[text mutableString] length], size = 18;
174         
175         for ( i = 1; i < 6; i++ ) {
176                 
177             if ( rating >= i ) {
178                 ratingString = [ratingString stringByAppendingString:fullChar];
179             } else {
180                 ratingString = [ratingString stringByAppendingString:emptyChar];
181             }
182         }
183                 
184                 [[text mutableString] appendFormat:@"\n%@", ratingString];
185                 if ([_window sizing] == ITTransientStatusWindowSmall) {
186                         size /= SMALL_DIVISOR;
187                 } else if ([_window sizing] == ITTransientStatusWindowMini) {
188                         size /= MINI_DIVISOR;
189                 }
190                 [text setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"AppleGothic" size:size], NSFontAttributeName, nil, nil] range:NSMakeRange(start + 1, 5)];
191         //text = [text stringByAppendingString:[@"\n" stringByAppendingString:ratingString]];
192     }
193     
194     [_window buildTextWindowWithString:text];
195     [_window appear:self];
196         [text release];
197 }
198
199 - (void)showUpcomingSongsWindowWithTitles:(NSArray *)titleStrings
200 {
201 //  NSString *bull = [NSString stringWithUTF8String:"‣ "];
202     NSString *bull = [NSString stringWithUTF8String:"♪ "];
203     NSString *end  = [@"\n" stringByAppendingString:bull];
204     [_window setImage:[NSImage imageNamed:@"Upcoming"]];
205     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
206     [_window buildTextWindowWithString:[bull stringByAppendingString:[titleStrings componentsJoinedByString:end]]];
207     [_window appear:self];
208 }
209
210 - (void)showVolumeWindowWithLevel:(float)level
211 {
212     [_window setImage:[NSImage imageNamed:@"Volume"]];
213     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
214     [_window buildMeterWindowWithCharacter:[NSString stringWithUTF8String:"▊"]
215                                       size:18
216                                      count:10
217                                     active:( ceil(level * 100) / 10 )];
218     [_window appear:self];
219 }
220
221 - (void)showRatingWindowWithRating:(float)rating
222 {
223     [_window setImage:[NSImage imageNamed:@"Rating"]];
224     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
225     [_window buildMeterWindowWithCharacter:[NSString stringWithUTF8String:"★"]
226                                       size:48
227                                      count:5
228                                     active:( ceil(rating * 100) / 20 )];
229     [_window appear:self];
230 }
231
232 - (void)showShuffleWindow:(BOOL)shuffle
233 {
234     [_window setImage:[NSImage imageNamed:@"Shuffle"]];
235     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
236     [_window buildTextWindowWithString:( shuffle ? NSLocalizedString(@"shuffleOn", @"Shuffle On") : NSLocalizedString(@"shuffleOff", @"Shuffle Off"))];
237     [_window appear:self];
238 }
239
240 - (void)showRepeatWindowWithMode:(StatusWindowRepeatMode)mode
241 {
242     NSString *string = nil;
243     
244     if ( mode == StatusWindowRepeatNone ) {
245         string = NSLocalizedString(@"repeatOff", @"Repeat Off");
246     } else if ( mode == StatusWindowRepeatGroup ) {
247         string = NSLocalizedString(@"repeatPlaylist", @"Repeat Playlist");
248     } else if ( mode == StatusWindowRepeatTrack ) {
249         string = NSLocalizedString(@"repeatOneTrack", @"Repeat One Track");;
250     }
251     
252     [_window setImage:[NSImage imageNamed:@"Repeat"]];
253     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
254     [_window buildTextWindowWithString:string];
255     [_window appear:self];
256 }
257
258 - (void)showSongShufflabilityWindow:(BOOL)shufflable
259 {
260     [_window setImage:[NSImage imageNamed:@"Shuffle"]];
261     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
262     [_window buildTextWindowWithString:( !shufflable ? NSLocalizedString(@"shufflableOn", @"Current Song Skipped When Shuffling") : NSLocalizedString(@"shufflableOff", @"Current Song Not Skipped When Shuffling"))];
263     [_window appear:self];
264 }
265
266 - (void)showSetupQueryWindow
267 {
268     NSString *message = NSLocalizedString(@"autolaunch_msg", @"Would you like MenuTunes to launch\nautomatically at startup?");
269
270     [_window setImage:[NSImage imageNamed:@"Setup"]];
271     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
272     [_window buildDialogWindowWithMessage:message
273                             defaultButton:NSLocalizedString(@"launch_at_startup", @"Launch at Startup")
274                           alternateButton:NSLocalizedString(@"launch_manually", @"Launch Manually")
275                                    target:[PreferencesController sharedPrefs]
276                             defaultAction:@selector(autoLaunchOK)
277                           alternateAction:@selector(autoLaunchCancel)];
278
279     [_window appear:self];
280     [_window setLocked:YES];
281 }
282
283
284 - (void)showRegistrationQueryWindow
285 {
286     NSString *message = NSLocalizedString(@"trialexpired_msg", @"Your 7-day unlimited trial period has elapsed.\nYou must register to continue using MenuTunes.");
287
288     [_window setImage:[NSImage imageNamed:@"Register"]];
289     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
290     [_window buildDialogWindowWithMessage:message
291                             defaultButton:NSLocalizedString(@"registernow", @"Register Now")
292                           alternateButton:NSLocalizedString(@"quitmenutunes", @"Quit MenuTunes")
293                                    target:[MainController sharedController]
294                             defaultAction:@selector(registerNowOK)
295                           alternateAction:@selector(registerNowCancel)];
296
297     [_window appear:self];
298     [_window setLocked:YES];
299 }
300
301 - (void)showReconnectQueryWindow
302 {
303     NSString *message = NSLocalizedString(@"sharedplayeravailable_msg", @"The selected shared player is available again.\nWould you like to reconnect to it?");
304     [_window setLocked:NO];
305     [_window setImage:[NSImage imageNamed:@"Setup"]];
306     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
307     [_window buildDialogWindowWithMessage:message
308                             defaultButton:NSLocalizedString(@"reconnect", @"Reconnect")
309                           alternateButton:NSLocalizedString(@"ignore", @"Ignore")
310                                    target:[MainController sharedController]
311                             defaultAction:@selector(reconnect)
312                           alternateAction:@selector(cancelReconnect)];
313
314     [_window appear:self];
315     [_window setLocked:YES];
316 }
317
318 - (void)showNetworkErrorQueryWindow
319 {
320     NSString *message = NSLocalizedString(@"sharedplayerunreachable_msg", @"The remote MenuTunes server is unreachable.\nMenuTunes will revert back to the local player.");
321
322     [_window setImage:[NSImage imageNamed:@"Setup"]];
323     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
324     [_window buildDialogWindowWithMessage:message
325                             defaultButton:@" OK "
326                           alternateButton:nil
327                                    target:[MainController sharedController]
328                             defaultAction:@selector(cancelReconnect)
329                           alternateAction:nil];
330
331     [_window appear:self];
332     [_window setLocked:YES];
333 }
334
335 - (void)showPreferencesUpdateWindow
336 {
337     NSString *message = NSLocalizedString(@"reconfigureprefs_msg", @"The new features in this version of MenuTunes\nrequire you to reconfigure your preferences.");
338
339     [_window setImage:[NSImage imageNamed:@"Setup"]];
340     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
341     [_window buildDialogWindowWithMessage:message
342                             defaultButton:NSLocalizedString(@"showpreferences", @"Show Preferences")
343                           alternateButton:@"OK"
344                                    target:[MainController sharedController]
345                             defaultAction:@selector(showPreferencesAndClose)
346                           alternateAction:@selector(cancelReconnect)];
347
348     [_window appear:self];
349     [_window setLocked:YES];
350 }
351
352 - (void)showDebugModeEnabledWindow
353 {
354         [_window setImage:[NSImage imageNamed:@"Setup"]];
355     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
356     [_window buildDialogWindowWithMessage:NSLocalizedString(@"debugmodeenabled", @"Debug Mode Enabled")
357                             defaultButton:@"OK"
358                           alternateButton:nil
359                                    target:[MainController sharedController]
360                             defaultAction:@selector(cancelReconnect)
361                           alternateAction:nil];
362     [_window appear:self];
363         [_window setLocked:YES];
364 }
365
366 - (void)updateTime:(NSString *)time
367 {
368         if (time && [time length]) {
369                 [_window updateTime:time range:_timeRange];
370         }
371 }
372
373 @end