Cleaning up the track art code (It still doesn't work right, but it's cleaner!)
[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         /*
118         NSImageRep *artRep = [art bestRepresentationForDevice:nil];
119         image = [[NSImage alloc] initWithSize:NSMakeSize(110, 110)];
120         [image lockFocus];
121         {
122             [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
123             [[NSGraphicsContext currentContext] setShouldAntialias:YES];
124             [artRep drawInRect:NSMakeRect(3, 3, 104, 104)];
125         }
126         [image unlockFocus];
127         [image autorelease];*/
128         image = art;
129         [image setScalesWhenResized:YES];
130         [image setSize:NSMakeSize(110,110)];
131     } else if ( source == ITMTRemoteLibrarySource ) {
132         image = [NSImage imageNamed:@"Library"];
133     } else if ( source == ITMTRemoteCDSource ) {
134         image = [NSImage imageNamed:@"CD"];
135     } else if ( source == ITMTRemoteRadioSource ) {
136         image = [NSImage imageNamed:@"Radio"];
137     } else if ( source == ITMTRemoteiPodSource ) {
138         image = [NSImage imageNamed:@"iPod"];
139     } else if ( source == ITMTRemoteGenericDeviceSource ) {
140         image = [NSImage imageNamed:@"MP3Player"];
141     } else if ( source == ITMTRemoteSharedLibrarySource ) {
142         image = [NSImage imageNamed:@"Library"];
143     }
144     
145     [_window setImage:image];
146     
147     if ( album ) {
148         text = [text stringByAppendingString:[@"\n" stringByAppendingString:album]];
149     }
150     if ( artist ) {
151         text = [text stringByAppendingString:[@"\n" stringByAppendingString:artist]];
152     }
153     if ( time ) {
154         text = [text stringByAppendingString:[@"\n" stringByAppendingString:time]];
155     }
156     if ( track ) {
157         text = [text stringByAppendingString:[@"\n" stringByAppendingString:track]];
158     }
159     if ( rating > -1 ) {
160
161         NSString *ratingString = [NSString string];
162         NSString *emptyChar    = [NSString stringWithUTF8String:"☆"];
163         NSString *fullChar     = [NSString stringWithUTF8String:"★"];
164         int       i;
165         
166         for ( i = 1; i < 6; i++ ) {
167                 
168             if ( rating >= i ) {
169                 ratingString = [ratingString stringByAppendingString:fullChar];
170             } else {
171                 ratingString = [ratingString stringByAppendingString:emptyChar];
172             }
173         }
174     
175         text = [text stringByAppendingString:[@"\n" stringByAppendingString:ratingString]];
176     }
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 = @"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:@"Launch at Startup"
251                           alternateButton:@"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 = @"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:@"Register Now"
269                           alternateButton:@"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 = @"The selected shared player is available again.\nWould you like to reconnect to it?.";
281
282     [_window setImage:[NSImage imageNamed:@"Setup"]];
283     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
284     [_window buildDialogWindowWithMessage:message
285                             defaultButton:@"Reconnect"
286                           alternateButton:@"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)showPreferencesUpdateWindow
296 {
297     NSString *message = @"The new features in this version of MenuTunes\nrequire you to reconfigure your preferences.";
298
299     [_window setImage:[NSImage imageNamed:@"Setup"]];
300     [_window setSizing:(ITTransientStatusWindowSizing)[df integerForKey:@"statusWindowSizing"]];
301     [_window buildDialogWindowWithMessage:message
302                             defaultButton:@"Show Preferences"
303                           alternateButton:@"OK"
304                                    target:[MainController sharedController]
305                             defaultAction:@selector(showPreferencesAndClose)
306                           alternateAction:@selector(cancelReconnect)];
307
308     [_window appear:self];
309     [_window setLocked:YES];
310 }
311
312 @end