AE'd the album art method. Added image smoothing to the album art
[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         image = art;
118         [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationLow];
119         [image setScalesWhenResized:YES];
120         [image setSize:NSMakeSize(110, 110)];
121         /*if (size.width > 110 && size.width > size.height) {
122             [image setSize:NSMakeSize(110, (size.width / size.height) * 110)];
123         } else if (size.height > 110 && size.height > size.width) {
124             [image setSize:NSMakeSize((size.height / size.width) * 110, 110)];
125         }*/
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 ( time ) {
149         text = [text stringByAppendingString:[@"\n" stringByAppendingString:time]];
150     }
151     if ( track ) {
152         text = [text stringByAppendingString:[@"\n" stringByAppendingString:track]];
153     }
154     if ( rating > -1 ) {
155
156         NSString *ratingString = [NSString string];
157         NSString *emptyChar    = [NSString stringWithUTF8String:"☆"];
158         NSString *fullChar     = [NSString stringWithUTF8String:"★"];
159         int       i;
160         
161         for ( i = 1; i < 6; i++ ) {
162                 
163             if ( rating >= i ) {
164                 ratingString = [ratingString stringByAppendingString:fullChar];
165             } else {
166                 ratingString = [ratingString stringByAppendingString:emptyChar];
167             }
168         }
169     
170         text = [text stringByAppendingString:[@"\n" stringByAppendingString:ratingString]];
171     }
172     
173     
174     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
175     [_window buildTextWindowWithString:text];
176     [_window appear:self];
177 }
178
179 - (void)showUpcomingSongsWindowWithTitles:(NSArray *)titleStrings
180 {
181 //  NSString *bull = [NSString stringWithUTF8String:"‣ "];
182     NSString *bull = [NSString stringWithUTF8String:"♪ "];
183     NSString *end  = [@"\n" stringByAppendingString:bull];
184     [_window setImage:[NSImage imageNamed:@"Upcoming"]];
185     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
186     [_window buildTextWindowWithString:[bull stringByAppendingString:[titleStrings componentsJoinedByString:end]]];
187     [_window appear:self];
188 }
189
190 - (void)showVolumeWindowWithLevel:(float)level
191 {
192     [_window setImage:[NSImage imageNamed:@"Volume"]];
193     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
194     [_window buildMeterWindowWithCharacter:[NSString stringWithUTF8String:"▊"]
195                                       size:18
196                                      count:10
197                                     active:( ceil(level * 100) / 10 )];
198     [_window appear:self];
199 }
200
201 - (void)showRatingWindowWithRating:(float)rating
202 {
203     [_window setImage:[NSImage imageNamed:@"Rating"]];
204     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
205     [_window buildMeterWindowWithCharacter:[NSString stringWithUTF8String:"★"]
206                                       size:48
207                                      count:5
208                                     active:( ceil(rating * 100) / 20 )];
209     [_window appear:self];
210 }
211
212 - (void)showShuffleWindow:(BOOL)shuffle
213 {
214     [_window setImage:[NSImage imageNamed:@"Shuffle"]];
215     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
216     [_window buildTextWindowWithString:( shuffle ? @"Shuffle On" : @"Shuffle Off")];
217     [_window appear:self];
218 }
219
220 - (void)showRepeatWindowWithMode:(StatusWindowRepeatMode)mode
221 {
222     NSString *string = nil;
223     
224     if ( mode == StatusWindowRepeatNone ) {
225         string = @"Repeat Off";
226     } else if ( mode == StatusWindowRepeatGroup ) {
227         string = @"Repeat Playlist";
228     } else if ( mode == StatusWindowRepeatTrack ) {
229         string = @"Repeat One Track";
230     }
231     
232     [_window setImage:[NSImage imageNamed:@"Repeat"]];
233     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
234     [_window buildTextWindowWithString:string];
235     [_window appear:self];
236 }
237
238 - (void)showSetupQueryWindow
239 {
240     NSString *message = @"Would you like MenuTunes to launch\nautomatically at startup?";
241
242     [_window setImage:[NSImage imageNamed:@"Setup"]];
243     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
244     [_window buildDialogWindowWithMessage:message
245                             defaultButton:@"Launch at Startup"
246                           alternateButton:@"Launch Manually"
247                                    target:[PreferencesController sharedPrefs]
248                             defaultAction:@selector(autoLaunchOK)
249                           alternateAction:@selector(autoLaunchCancel)];
250
251     [_window appear:self];
252     [_window setLocked:YES];
253 }
254
255
256 - (void)showRegistrationQueryWindow
257 {
258     NSString *message = @"Your 7-day unlimited trial period has elapsed.\nYou must register to continue using MenuTunes.";
259
260     [_window setImage:[NSImage imageNamed:@"Register"]];
261     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
262     [_window buildDialogWindowWithMessage:message
263                             defaultButton:@"Register Now"
264                           alternateButton:@"Quit MenuTunes"
265                                    target:[MainController sharedController]
266                             defaultAction:@selector(registerNowOK)
267                           alternateAction:@selector(registerNowCancel)];
268
269     [_window appear:self];
270     [_window setLocked:YES];
271 }
272
273 - (void)showReconnectQueryWindow
274 {
275     NSString *message = @"The selected shared player is available again.\nWould you like to reconnect to it?.";
276
277     [_window setImage:[NSImage imageNamed:@"Register"]];
278     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
279     [_window buildDialogWindowWithMessage:message
280                             defaultButton:@"Reconnect"
281                           alternateButton:@"Ignore"
282                                    target:[MainController sharedController]
283                             defaultAction:@selector(reconnect)
284                           alternateAction:@selector(cancelReconnect)];
285
286     [_window appear:self];
287     [_window setLocked:YES];
288 }
289
290 - (void)showPreferencesUpdateWindow
291 {
292     NSString *message = @"The new features in this version of MenuTunes\nrequire you to reconfigure your preferences.";
293
294     [_window setImage:[NSImage imageNamed:@"Setup"]];
295     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
296     [_window buildDialogWindowWithMessage:message
297                             defaultButton:@"Show Preferences"
298                           alternateButton:@"OK"
299                                    target:[MainController sharedController]
300                             defaultAction:@selector(showPreferencesAndClose)
301                           alternateAction:@selector(cancelReconnect)];
302
303     [_window appear:self];
304     [_window setLocked:YES];
305 }
306
307 @end