Added preference saving for what screen you want.
[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     NSString *text  = 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     
142     if ( album ) {
143         text = [text stringByAppendingString:[@"\n" stringByAppendingString:album]];
144     }
145     if ( artist ) {
146         text = [text stringByAppendingString:[@"\n" stringByAppendingString:artist]];
147     }
148     if ( composer ) {
149         text = [text stringByAppendingString:[@"\n" stringByAppendingString:composer]];
150     }
151     if ( time ) {
152         text = [text stringByAppendingString:[@"\n" stringByAppendingString:time]];
153     }
154     if ( track ) {
155         text = [text stringByAppendingString:[@"\n" stringByAppendingString:track]];
156     }
157     if (playCount > -1) {
158         text = [text stringByAppendingString:[NSString stringWithFormat:@"\nPlay Count: %i", playCount]];
159     }
160     if ( rating > -1 ) {
161
162         NSString *ratingString = [NSString string];
163         NSString *emptyChar    = [NSString stringWithUTF8String:"☆"];
164         NSString *fullChar     = [NSString stringWithUTF8String:"★"];
165         int       i;
166         
167         for ( i = 1; i < 6; i++ ) {
168                 
169             if ( rating >= i ) {
170                 ratingString = [ratingString stringByAppendingString:fullChar];
171             } else {
172                 ratingString = [ratingString stringByAppendingString:emptyChar];
173             }
174         }
175     
176         text = [text stringByAppendingString:[@"\n" stringByAppendingString:ratingString]];
177     }
178     
179     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
180     [_window buildTextWindowWithString:text];
181     [_window appear:self];
182 }
183
184 - (void)showUpcomingSongsWindowWithTitles:(NSArray *)titleStrings
185 {
186 //  NSString *bull = [NSString stringWithUTF8String:"‣ "];
187     NSString *bull = [NSString stringWithUTF8String:"♪ "];
188     NSString *end  = [@"\n" stringByAppendingString:bull];
189     [_window setImage:[NSImage imageNamed:@"Upcoming"]];
190     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
191     [_window buildTextWindowWithString:[bull stringByAppendingString:[titleStrings componentsJoinedByString:end]]];
192     [_window appear:self];
193 }
194
195 - (void)showVolumeWindowWithLevel:(float)level
196 {
197     [_window setImage:[NSImage imageNamed:@"Volume"]];
198     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
199     [_window buildMeterWindowWithCharacter:[NSString stringWithUTF8String:"▊"]
200                                       size:18
201                                      count:10
202                                     active:( ceil(level * 100) / 10 )];
203     [_window appear:self];
204 }
205
206 - (void)showRatingWindowWithRating:(float)rating
207 {
208     [_window setImage:[NSImage imageNamed:@"Rating"]];
209     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
210     [_window buildMeterWindowWithCharacter:[NSString stringWithUTF8String:"★"]
211                                       size:48
212                                      count:5
213                                     active:( ceil(rating * 100) / 20 )];
214     [_window appear:self];
215 }
216
217 - (void)showShuffleWindow:(BOOL)shuffle
218 {
219     [_window setImage:[NSImage imageNamed:@"Shuffle"]];
220     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
221     [_window buildTextWindowWithString:( shuffle ? @"Shuffle On" : @"Shuffle Off")];
222     [_window appear:self];
223 }
224
225 - (void)showRepeatWindowWithMode:(StatusWindowRepeatMode)mode
226 {
227     NSString *string = nil;
228     
229     if ( mode == StatusWindowRepeatNone ) {
230         string = @"Repeat Off";
231     } else if ( mode == StatusWindowRepeatGroup ) {
232         string = @"Repeat Playlist";
233     } else if ( mode == StatusWindowRepeatTrack ) {
234         string = @"Repeat One Track";
235     }
236     
237     [_window setImage:[NSImage imageNamed:@"Repeat"]];
238     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
239     [_window buildTextWindowWithString:string];
240     [_window appear:self];
241 }
242
243 - (void)showSetupQueryWindow
244 {
245     NSString *message = NSLocalizedString(@"autolaunch_msg", @"Would you like MenuTunes to launch\nautomatically at startup?");
246
247     [_window setImage:[NSImage imageNamed:@"Setup"]];
248     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
249     [_window buildDialogWindowWithMessage:message
250                             defaultButton:NSLocalizedString(@"launch_at_startup", @"Launch at Startup")
251                           alternateButton:NSLocalizedString(@"launch_manually", @"Launch Manually")
252                                    target:[PreferencesController sharedPrefs]
253                             defaultAction:@selector(autoLaunchOK)
254                           alternateAction:@selector(autoLaunchCancel)];
255
256     [_window appear:self];
257     [_window setLocked:YES];
258 }
259
260
261 - (void)showRegistrationQueryWindow
262 {
263     NSString *message = NSLocalizedString(@"trialexpired_msg", @"Your 7-day unlimited trial period has elapsed.\nYou must register to continue using MenuTunes.");
264
265     [_window setImage:[NSImage imageNamed:@"Register"]];
266     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
267     [_window buildDialogWindowWithMessage:message
268                             defaultButton:NSLocalizedString(@"registernow", @"Register Now")
269                           alternateButton:NSLocalizedString(@"quitmenutunes", @"Quit MenuTunes")
270                                    target:[MainController sharedController]
271                             defaultAction:@selector(registerNowOK)
272                           alternateAction:@selector(registerNowCancel)];
273
274     [_window appear:self];
275     [_window setLocked:YES];
276 }
277
278 - (void)showReconnectQueryWindow
279 {
280     NSString *message = NSLocalizedString(@"sharedplayeravailable_msg", @"The selected shared player is available again.\nWould you like to reconnect to it?");
281     [_window setLocked:NO];
282     [_window setImage:[NSImage imageNamed:@"Setup"]];
283     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
284     [_window buildDialogWindowWithMessage:message
285                             defaultButton:NSLocalizedString(@"reconnect", @"Reconnect")
286                           alternateButton:NSLocalizedString(@"ignore", @"Ignore")
287                                    target:[MainController sharedController]
288                             defaultAction:@selector(reconnect)
289                           alternateAction:@selector(cancelReconnect)];
290
291     [_window appear:self];
292     [_window setLocked:YES];
293 }
294
295 - (void)showNetworkErrorQueryWindow
296 {
297     NSString *message = NSLocalizedString(@"sharedplayerunreachable_msg", @"The remote MenuTunes server is unreachable.\nMenuTunes will revert back to the local player.");
298
299     [_window setImage:[NSImage imageNamed:@"Setup"]];
300     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
301     [_window buildDialogWindowWithMessage:message
302                             defaultButton:@" OK "
303                           alternateButton:nil
304                                    target:[MainController sharedController]
305                             defaultAction:@selector(cancelReconnect)
306                           alternateAction:nil];
307
308     [_window appear:self];
309     [_window setLocked:YES];
310 }
311
312 - (void)showPreferencesUpdateWindow
313 {
314     NSString *message = NSLocalizedString(@"reconfigureprefs_msg", @"The new features in this version of MenuTunes\nrequire you to reconfigure your preferences.");
315
316     [_window setImage:[NSImage imageNamed:@"Setup"]];
317     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
318     [_window buildDialogWindowWithMessage:message
319                             defaultButton:NSLocalizedString(@"showpreferences", @"Show Preferences")
320                           alternateButton:@"OK"
321                                    target:[MainController sharedController]
322                             defaultAction:@selector(showPreferencesAndClose)
323                           alternateAction:@selector(cancelReconnect)];
324
325     [_window appear:self];
326     [_window setLocked:YES];
327 }
328
329 @end