Album artwork checkbox automatically will be checked if the user is
[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
15 static StatusWindowController *sharedController;
16
17
18 @implementation StatusWindowController
19
20
21 + (StatusWindowController *)sharedController
22 {
23     if ( ! sharedController ) {
24         sharedController = [[StatusWindowController alloc] init];
25     }
26     
27     return sharedController;
28 }
29
30
31 - (id)init
32 {
33     if ( ( self = [super init] ) ) {
34         
35         float exitDelay;
36         NSString *entryClass;
37         NSString *exitClass;
38         NSArray  *classList = [ITWindowEffect effectClasses];
39         float entrySpeed;
40         float exitSpeed;
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         [_window setExitMode:ITTransientStatusWindowExitAfterDelay];
57         [_window setExitDelay:(exitDelay ? exitDelay : 4.0)];
58         
59         [_window setHorizontalPosition:[df integerForKey:@"statusWindowHorizontalPosition"]];
60         [_window setVerticalPosition:[df integerForKey:@"statusWindowVerticalPosition"]];
61         
62         [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
63         
64         if ( [classList containsObject:NSClassFromString(entryClass)] ) {
65             entryEffect = [[[NSClassFromString(entryClass) alloc] initWithWindow:_window] autorelease];
66         } else {
67             entryEffect = [[[ITCutWindowEffect alloc] initWithWindow:_window] autorelease];
68         }
69         
70         if ( [classList containsObject:NSClassFromString(exitClass)] ) {
71             exitEffect = [[[NSClassFromString(exitClass) alloc] initWithWindow:_window] autorelease];
72         } else {
73             exitEffect = [[[ITDissolveWindowEffect alloc] initWithWindow:_window] autorelease];
74         }
75         
76         [_window setEntryEffect:entryEffect];
77         [_window setExitEffect:exitEffect];
78         
79         [[_window entryEffect] setEffectTime:(entrySpeed ? entrySpeed : 0.8)];
80         [[_window exitEffect]  setEffectTime:(exitSpeed  ? exitSpeed  : 0.8)];
81         
82         [(ITTSWBackgroundView *)[_window contentView]setBackgroundMode:
83             (ITTSWBackgroundMode)[df integerForKey:@"statusWindowBackgroundMode"]];
84         
85         colorData = [df dataForKey:@"statusWindowBackgroundColor"];
86         
87         if ( colorData ) {
88             [(ITTSWBackgroundView *)[_window contentView] setBackgroundColor:
89                 (NSColor *)[NSUnarchiver unarchiveObjectWithData:colorData]];
90         } else {
91             [(ITTSWBackgroundView *)[_window contentView] setBackgroundColor:[NSColor blueColor]];
92         }
93     }
94     
95     return self;
96 }
97
98 - (void)dealloc
99 {
100     [_window release];
101     [super dealloc];
102 }
103
104 - (void)showSongInfoWindowWithSource:(ITMTRemotePlayerSource)source
105                                title:            (NSString *)title
106                                album:            (NSString *)album
107                               artist:            (NSString *)artist
108                                 time:            (NSString *)time  // FLOW: Should probably be NSDate or something.
109                                track:            (NSString *)track
110                               rating:                   (int)rating
111                                image:             (NSImage *)art
112 {
113     NSImage  *image = nil;
114     NSString *text  = title;
115     
116     if ( art != nil ) {
117         NSImageRep *artRep = [art bestRepresentationForDevice:nil];
118         image = [[NSImage alloc] initWithSize:NSMakeSize(110, 110)];
119         [image lockFocus];
120         {
121             [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
122             [[NSGraphicsContext currentContext] setShouldAntialias:YES];
123             [artRep drawInRect:NSMakeRect(3, 3, 104, 104)];
124         }
125         [image unlockFocus];
126         [image autorelease];
127     } else if ( source == ITMTRemoteLibrarySource ) {
128         image = [NSImage imageNamed:@"Library"];
129     } else if ( source == ITMTRemoteCDSource ) {
130         image = [NSImage imageNamed:@"CD"];
131     } else if ( source == ITMTRemoteRadioSource ) {
132         image = [NSImage imageNamed:@"Radio"];
133     } else if ( source == ITMTRemoteiPodSource ) {
134         image = [NSImage imageNamed:@"iPod"];
135     } else if ( source == ITMTRemoteGenericDeviceSource ) {
136         image = [NSImage imageNamed:@"MP3Player"];
137     } else if ( source == ITMTRemoteSharedLibrarySource ) {
138         image = [NSImage imageNamed:@"Library"];
139     }
140     
141     [_window setImage:image];
142     
143     if ( album ) {
144         text = [text stringByAppendingString:[@"\n" stringByAppendingString:album]];
145     }
146     if ( artist ) {
147         text = [text stringByAppendingString:[@"\n" stringByAppendingString:artist]];
148     }
149     if ( time ) {
150         text = [text stringByAppendingString:[@"\n" stringByAppendingString:time]];
151     }
152     if ( track ) {
153         text = [text stringByAppendingString:[@"\n" stringByAppendingString:track]];
154     }
155     if ( rating > -1 ) {
156
157         NSString *ratingString = [NSString string];
158         NSString *emptyChar    = [NSString stringWithUTF8String:"☆"];
159         NSString *fullChar     = [NSString stringWithUTF8String:"★"];
160         int       i;
161         
162         for ( i = 1; i < 6; i++ ) {
163                 
164             if ( rating >= i ) {
165                 ratingString = [ratingString stringByAppendingString:fullChar];
166             } else {
167                 ratingString = [ratingString stringByAppendingString:emptyChar];
168             }
169         }
170     
171         text = [text stringByAppendingString:[@"\n" stringByAppendingString:ratingString]];
172     }
173     
174     
175     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
176     [_window buildTextWindowWithString:text];
177     [_window appear:self];
178 }
179
180 - (void)showUpcomingSongsWindowWithTitles:(NSArray *)titleStrings
181 {
182 //  NSString *bull = [NSString stringWithUTF8String:"‣ "];
183     NSString *bull = [NSString stringWithUTF8String:"♪ "];
184     NSString *end  = [@"\n" stringByAppendingString:bull];
185     [_window setImage:[NSImage imageNamed:@"Upcoming"]];
186     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
187     [_window buildTextWindowWithString:[bull stringByAppendingString:[titleStrings componentsJoinedByString:end]]];
188     [_window appear:self];
189 }
190
191 - (void)showVolumeWindowWithLevel:(float)level
192 {
193     [_window setImage:[NSImage imageNamed:@"Volume"]];
194     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
195     [_window buildMeterWindowWithCharacter:[NSString stringWithUTF8String:"▊"]
196                                       size:18
197                                      count:10
198                                     active:( ceil(level * 100) / 10 )];
199     [_window appear:self];
200 }
201
202 - (void)showRatingWindowWithRating:(float)rating
203 {
204     [_window setImage:[NSImage imageNamed:@"Rating"]];
205     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
206     [_window buildMeterWindowWithCharacter:[NSString stringWithUTF8String:"★"]
207                                       size:48
208                                      count:5
209                                     active:( ceil(rating * 100) / 20 )];
210     [_window appear:self];
211 }
212
213 - (void)showShuffleWindow:(BOOL)shuffle
214 {
215     [_window setImage:[NSImage imageNamed:@"Shuffle"]];
216     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
217     [_window buildTextWindowWithString:( shuffle ? @"Shuffle On" : @"Shuffle Off")];
218     [_window appear:self];
219 }
220
221 - (void)showRepeatWindowWithMode:(StatusWindowRepeatMode)mode
222 {
223     NSString *string = nil;
224     
225     if ( mode == StatusWindowRepeatNone ) {
226         string = @"Repeat Off";
227     } else if ( mode == StatusWindowRepeatGroup ) {
228         string = @"Repeat Playlist";
229     } else if ( mode == StatusWindowRepeatTrack ) {
230         string = @"Repeat One Track";
231     }
232     
233     [_window setImage:[NSImage imageNamed:@"Repeat"]];
234     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
235     [_window buildTextWindowWithString:string];
236     [_window appear:self];
237 }
238
239 - (void)showSetupQueryWindow
240 {
241     NSString *message = @"Would you like MenuTunes to launch\nautomatically at startup?";
242
243     [_window setImage:[NSImage imageNamed:@"Setup"]];
244     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
245     [_window buildDialogWindowWithMessage:message
246                             defaultButton:@"Launch at Startup"
247                           alternateButton:@"Launch Manually"
248                                    target:[PreferencesController sharedPrefs]
249                             defaultAction:@selector(autoLaunchOK)
250                           alternateAction:@selector(autoLaunchCancel)];
251
252     [_window appear:self];
253     [_window setLocked:YES];
254 }
255
256
257 - (void)showRegistrationQueryWindow
258 {
259     NSString *message = @"Your 7-day unlimited trial period has elapsed.\nYou must register to continue using MenuTunes.";
260
261     [_window setImage:[NSImage imageNamed:@"Register"]];
262     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
263     [_window buildDialogWindowWithMessage:message
264                             defaultButton:@"Register Now"
265                           alternateButton:@"Quit MenuTunes"
266                                    target:[MainController sharedController]
267                             defaultAction:@selector(registerNowOK)
268                           alternateAction:@selector(registerNowCancel)];
269
270     [_window appear:self];
271     [_window setLocked:YES];
272 }
273
274 - (void)showReconnectQueryWindow
275 {
276     NSString *message = @"The selected shared player is available again.\nWould you like to reconnect to it?.";
277
278     [_window setImage:[NSImage imageNamed:@"Setup"]];
279     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
280     [_window buildDialogWindowWithMessage:message
281                             defaultButton:@"Reconnect"
282                           alternateButton:@"Ignore"
283                                    target:[MainController sharedController]
284                             defaultAction:@selector(reconnect)
285                           alternateAction:@selector(cancelReconnect)];
286
287     [_window appear:self];
288     [_window setLocked:YES];
289 }
290
291 - (void)showPreferencesUpdateWindow
292 {
293     NSString *message = @"The new features in this version of MenuTunes\nrequire you to reconfigure your preferences.";
294
295     [_window setImage:[NSImage imageNamed:@"Setup"]];
296     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
297     [_window buildDialogWindowWithMessage:message
298                             defaultButton:@"Show Preferences"
299                           alternateButton:@"OK"
300                                    target:[MainController sharedController]
301                             defaultAction:@selector(showPreferencesAndClose)
302                           alternateAction:@selector(cancelReconnect)];
303
304     [_window appear:self];
305     [_window setLocked:YES];
306 }
307
308 @end