28 #include "../NativeWindowFromNode.h"
30 #include <nsComponentManagerUtils.h>
31 #include <nsServiceManagerUtils.h>
32 #include <nsICategoryManager.h>
33 #include <nsIObserverService.h>
34 #include <nsIWindowMediator.h>
35 #include <nsIDOMWindowInternal.h>
36 #include <nsIDOMWindow.h>
37 #include <nsIDOMDocument.h>
38 #include <nsIDOMElement.h>
39 #include <nsStringAPI.h>
40 #include <nsIStringBundle.h>
41 #include <nsObjCExceptions.h>
42 #include <sbIApplicationController.h>
43 #include <sbIMediacoreManager.h>
44 #include <sbIMediacorePlaybackControl.h>
45 #include <sbIMediacoreSequencer.h>
46 #include <sbIMediacoreVolumeControl.h>
47 #include <sbIMediacoreStatus.h>
49 #include <sbIMediaItem.h>
51 #import <AppKit/AppKit.h>
57 - (void)setAppDelegateOverride:(
id)aDelegateOverride;
66 - (void)onPlayPauseSelected:(
id)aSender;
67 - (void)onNextSelected:(
id)aSender;
68 - (void)onPreviousSelected:(
id)aSender;
69 - (void)onStopSelected:(
id)aSender;
70 - (void)onMuteSelected:(
id)aSender;
76 - (void)_setOverride:(
id)aOverrideObj;
77 - (void)_addNowPlayingControls:(NSMenu *)aMenu;
78 - (void)_addPlayerControlMenuItems:(NSMenu *)aMenu;
79 - (void)_appendMenuItem:(NSString *)aTitle
81 menu:(NSMenu *)aParentMenu;
85 - (NSString *)_stringForLocalizedKey:(const PRUnichar *)aBuffer;
93 if ((
self = [super
init])) {
102 [
self _setOverride:nil];
106 - (void)_setOverride:(
id)aOverrideObj
108 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
110 id delegate = [[NSApplication sharedApplication] delegate];
112 [delegate respondsToSelector:
@selector(setAppDelegateOverride:)])
114 [delegate setAppDelegateOverride:aOverrideObj];
117 NS_OBJC_END_TRY_ABORT_BLOCK;
121 - (BOOL)applicationShouldHandleReopen:(NSApplication*)theApp
122 hasVisibleWindows:(BOOL)flag
124 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
127 nsCOMPtr<nsIWindowMediator> winMed =
128 do_GetService(NS_WINDOWMEDIATOR_CONTRACTID, &rv);
129 NS_ENSURE_SUCCESS(rv, NO);
131 nsCOMPtr<nsIDOMWindowInternal> domWinInternal;
132 rv = winMed->GetMostRecentWindow(NS_LITERAL_STRING(
"Songbird:Main").
get(),
133 getter_AddRefs(domWinInternal));
134 if (NS_SUCCEEDED(rv) && domWinInternal) {
136 if (window && [window isMiniaturized]) {
137 [window deminiaturize:self];
143 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NO);
146 - (NSMenu *)applicationDockMenu:(NSApplication *)sender
148 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
151 nsCOMPtr<nsIWindowMediator>
wm =
152 do_GetService(NS_WINDOWMEDIATOR_CONTRACTID, &rv);
153 NS_ENSURE_SUCCESS(rv, nil);
156 nsCOMPtr<nsISimpleEnumerator> orderedWindowList;
157 rv = wm->GetZOrderDOMWindowEnumerator(nsnull, PR_TRUE,
158 getter_AddRefs(orderedWindowList));
159 NS_ENSURE_SUCCESS(rv, nil);
160 PRBool anyWindows =
false;
161 rv = orderedWindowList->HasMoreElements(&anyWindows);
162 NS_ENSURE_SUCCESS(rv, nil);
163 nsCOMPtr<nsISupports> frontWindow;
164 rv = orderedWindowList->GetNext(getter_AddRefs(frontWindow));
165 NS_ENSURE_SUCCESS(rv, nil);
170 nsCOMPtr<nsISimpleEnumerator> windowList;
171 rv = wm->GetEnumerator(nsnull, getter_AddRefs(windowList));
172 NS_ENSURE_SUCCESS(rv, nil);
175 NSMenu *
menu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
177 while (NS_SUCCEEDED(windowList->HasMoreElements(&more)) && more) {
179 nsCOMPtr<nsISupports> curWindow;
180 rv = windowList->GetNext(getter_AddRefs(curWindow));
181 NS_ENSURE_SUCCESS(rv, nil);
183 nsCOMPtr<nsIDOMWindow> domWindow = do_QueryInterface(curWindow, &rv);
188 nsCOMPtr<nsIDOMDocument> document;
189 domWindow->GetDocument(getter_AddRefs(document));
193 nsCOMPtr<nsIDOMElement>
element;
194 document->GetDocumentElement(getter_AddRefs(element));
199 rv = element->GetAttribute(NS_LITERAL_STRING(
"id"), idValue);
200 if (idValue.Equals(NS_LITERAL_STRING(
"video_osd_controls_win")))
207 NSString *windowTitle = [cocoaWindow title];
212 NSMenuItem *
menuItem = [[NSMenuItem alloc]
213 initWithTitle:windowTitle
214 action:@selector(dockMenuItemSelected:)
216 [menuItem setTarget:[[NSApplication sharedApplication] delegate]];
217 [menuItem setRepresentedObject:cocoaWindow];
220 if (SameCOMIdentity(domWindow, frontWindow))
221 [menuItem setState:NSOnState];
223 [menu addItem:menuItem];
227 [
self _addNowPlayingControls:menu];
228 [
self _addPlayerControlMenuItems:menu];
232 NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
235 - (void)_addNowPlayingControls:(NSMenu *)aMenu
237 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
240 nsCOMPtr<sbIMediacoreManager> manager =
241 do_GetService(
"@songbirdnest.com/Songbird/Mediacore/Manager;1", &rv);
246 nsCOMPtr<sbIMediacoreSequencer> sequencer;
247 rv = manager->GetSequencer(getter_AddRefs(sequencer));
252 nsCOMPtr<sbIMediaItem> curMediaItem;
253 rv = sequencer->GetCurrentItem(getter_AddRefs(curMediaItem));
254 if (NS_FAILED(rv) || !curMediaItem) {
262 rv = curMediaItem->GetProperty(trackProp, trackName);
268 rv = curMediaItem->GetProperty(artistProp, artistName);
274 trackName.Insert(NS_LITERAL_STRING(
" "), 0);
275 artistName.Insert(NS_LITERAL_STRING(
" "), 0);
278 [NSString stringWithCharacters:(unichar *)trackName.get()
279 length:trackName.Length()];
282 [aMenu addItem:[NSMenuItem separatorItem]];
285 nsString nowPlaying(NS_LITERAL_STRING(
"albumart.displaypane.title.playing"));
286 [
self _appendMenuItem:[
self _stringForLocalizedKey:nowPlaying.get()]
291 [
self _appendMenuItem:trackStr action:nil menu:aMenu];
294 NSString *artistStr =
295 [NSString stringWithCharacters:(unichar *)artistName.get()
296 length:artistName.Length()];
297 [
self _appendMenuItem:artistStr action:nil menu:aMenu];
299 NS_OBJC_END_TRY_ABORT_BLOCK;
302 - (void)_addPlayerControlMenuItems:(NSMenu *)aMenu
304 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
306 [aMenu addItem:[NSMenuItem separatorItem]];
309 nsString playPauseCommand;
310 if ([
self _isPlaybackPlaying]) {
311 playPauseCommand.AssignLiteral(
"playback.label.pause");
314 playPauseCommand.AssignLiteral(
"playback.label.play");
316 [
self _appendMenuItem:[
self _stringForLocalizedKey:playPauseCommand.get()]
317 action:@selector(onPlayPauseSelected:)
321 nsString nextLabel(NS_LITERAL_STRING(
"playback.label.next"));
322 [
self _appendMenuItem:[
self _stringForLocalizedKey:nextLabel.get()]
323 action:@selector(onNextSelected:)
327 nsString previousLabel(NS_LITERAL_STRING(
"playback.label.previous"));
328 [
self _appendMenuItem:[
self _stringForLocalizedKey:previousLabel.get()]
329 action:@selector(onPreviousSelected:)
333 nsString stopLabel(NS_LITERAL_STRING(
"playback.label.stop"));
334 [
self _appendMenuItem:[
self _stringForLocalizedKey:stopLabel.get()]
335 action:@selector(onStopSelected:)
339 nsString muteLabel(NS_LITERAL_STRING(
"playback.label.mute"));
340 NSMenuItem *muteMenuItem =
341 [[NSMenuItem alloc] initWithTitle:[
self _stringForLocalizedKey:muteLabel.get()]
342 action:@selector(onMuteSelected:)
345 int muteState = ([
self _isPlaybackMuted] ? NSOnState : NSOffState);
346 [muteMenuItem setState:muteState];
347 [muteMenuItem setTarget:self];
348 [aMenu addItem:muteMenuItem];
350 NS_OBJC_END_TRY_ABORT_BLOCK;
353 - (void)_appendMenuItem:(NSString *)aTitle
354 action:(
SEL)aSelector
355 menu:(NSMenu *)aParentMenu
357 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
359 NSMenuItem *menuItem =
360 [[NSMenuItem alloc] initWithTitle:aTitle
363 [menuItem setTarget:self];
364 [aParentMenu addItem:menuItem];
367 NS_OBJC_END_TRY_ABORT_BLOCK;
370 - (void)onPlayPauseSelected:(
id)aSender
372 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
375 nsCOMPtr<sbIMediacoreManager> manager =
376 do_GetService(
"@songbirdnest.com/Songbird/Mediacore/Manager;1", &rv);
381 nsCOMPtr<sbIMediacorePlaybackControl> playbackControl;
382 rv = manager->GetPlaybackControl(getter_AddRefs(playbackControl));
383 if (NS_SUCCEEDED(rv) && playbackControl && [
self _isPlaybackPlaying]) {
384 playbackControl->Pause();
387 nsCOMPtr<sbIApplicationController> app =
388 do_GetService(
"@songbirdnest.com/Songbird/ApplicationController;1", &rv);
389 NS_ENSURE_SUCCESS(rv,);
390 rv = app->PlayDefault();
391 NS_ENSURE_SUCCESS(rv,);
394 NS_OBJC_END_TRY_ABORT_BLOCK;
397 - (void)onNextSelected:(
id)aSender
399 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
402 nsCOMPtr<sbIMediacoreManager> manager =
403 do_GetService(
"@songbirdnest.com/Songbird/Mediacore/Manager;1", &rv);
408 nsCOMPtr<sbIMediacoreSequencer> sequencer;
409 rv = manager->GetSequencer(getter_AddRefs(sequencer));
414 sequencer->Next(PR_FALSE);
416 NS_OBJC_END_TRY_ABORT_BLOCK;
419 - (void)onPreviousSelected:(
id)aSender
421 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
424 nsCOMPtr<sbIMediacoreManager> manager =
425 do_GetService(
"@songbirdnest.com/Songbird/Mediacore/Manager;1", &rv);
430 nsCOMPtr<sbIMediacoreSequencer> sequencer;
431 rv = manager->GetSequencer(getter_AddRefs(sequencer));
436 sequencer->Previous(PR_FALSE);
438 NS_OBJC_END_TRY_ABORT_BLOCK;
441 - (void)onStopSelected:(
id)aSender
443 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
446 nsCOMPtr<sbIMediacoreManager> manager =
447 do_GetService(
"@songbirdnest.com/Songbird/Mediacore/Manager;1", &rv);
452 nsCOMPtr<sbIMediacoreSequencer> sequencer;
453 rv = manager->GetSequencer(getter_AddRefs(sequencer));
458 sequencer->Stop(PR_FALSE);
460 NS_OBJC_END_TRY_ABORT_BLOCK;
463 - (void)onMuteSelected:(
id)aSender
465 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
467 NSMenuItem *senderItem = (NSMenuItem *)aSender;
473 nsCOMPtr<sbIMediacoreManager> manager =
474 do_GetService(
"@songbirdnest.com/Songbird/Mediacore/Manager;1", &rv);
479 nsCOMPtr<sbIMediacoreVolumeControl> volControl;
480 rv = manager->GetVolumeControl(getter_AddRefs(volControl));
485 PRBool isMuted = PR_FALSE;
486 rv = volControl->GetMute(&isMuted);
491 if ([
self _isPlaybackMuted]) {
492 volControl->SetMute(PR_FALSE);
493 [senderItem setState:NSOffState];
496 volControl->SetMute(PR_TRUE);
497 [senderItem setState:NSOnState];
500 NS_OBJC_END_TRY_ABORT_BLOCK;
503 - (BOOL)_isPlaybackMuted
505 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
509 nsCOMPtr<sbIMediacoreManager> manager =
510 do_GetService(
"@songbirdnest.com/Songbird/Mediacore/Manager;1", &rv);
511 NS_ENSURE_SUCCESS(rv, NO);
513 nsCOMPtr<sbIMediacoreVolumeControl> volControl;
514 rv = manager->GetVolumeControl(getter_AddRefs(volControl));
515 NS_ENSURE_SUCCESS(rv, NO);
517 PRBool isMuted = PR_FALSE;
518 rv = volControl->GetMute(&isMuted);
519 NS_ENSURE_SUCCESS(rv, NO);
523 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NO);
526 - (BOOL)_isPlaybackPlaying
528 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
531 nsCOMPtr<sbIMediacoreManager> manager =
532 do_GetService(
"@songbirdnest.com/Songbird/Mediacore/Manager;1", &rv);
533 NS_ENSURE_SUCCESS(rv, NO);
535 nsCOMPtr<sbIMediacoreStatus> status;
536 rv = manager->GetStatus(getter_AddRefs(status));
537 NS_ENSURE_SUCCESS(rv, NO);
540 rv = status->GetState(&state);
541 NS_ENSURE_SUCCESS(rv, NO);
546 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NO);
549 - (BOOL)_isPlaybackStoppable
551 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
554 nsCOMPtr<sbIMediacoreManager> manager =
555 do_GetService(
"@songbirdnest.com/Songbird/Mediacore/Manager;1", &rv);
556 NS_ENSURE_SUCCESS(rv, NO);
558 nsCOMPtr<sbIMediacoreStatus> status;
559 rv = manager->GetStatus(getter_AddRefs(status));
560 NS_ENSURE_SUCCESS(rv, NO);
563 rv = status->GetState(&state);
564 NS_ENSURE_SUCCESS(rv, NO);
570 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NO);
573 - (NSString *)_stringForLocalizedKey:(const PRUnichar *)aBuffer
575 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
578 nsCOMPtr<nsIStringBundleService> strBundleService =
579 do_GetService(
"@mozilla.org/intl/stringbundle;1", &rv);
580 NS_ENSURE_SUCCESS(rv,
@"");
582 nsCOMPtr<nsIStringBundle> songbirdBundle;
583 rv = strBundleService->CreateBundle(
"chrome://songbird/locale/songbird.properties",
584 getter_AddRefs(songbirdBundle));
585 NS_ENSURE_SUCCESS(rv, nil);
587 nsString localizedStr;
588 rv = songbirdBundle->GetStringFromName(aBuffer, getter_Copies(localizedStr));
589 NS_ENSURE_SUCCESS(rv,
@"");
591 return [NSString stringWithCharacters:(unichar *)localizedStr.get()
592 length:localizedStr.Length()];
594 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(
@"");
603 sbMacAppDelegateManager::sbMacAppDelegateManager()
608 sbMacAppDelegateManager::~sbMacAppDelegateManager()
621 do_GetService(
"@mozilla.org/observer-service;1", &rv);
622 NS_ENSURE_SUCCESS(rv, rv);
624 rv = observerService->AddObserver(
this,
"final-ui-startup", PR_FALSE);
625 NS_ENSURE_SUCCESS(rv, rv);
627 rv = observerService->AddObserver(
this,
"quit-application-granted", PR_FALSE);
628 NS_ENSURE_SUCCESS(rv, rv);
634 sbMacAppDelegateManager::Observe(
nsISupports *aSubject,
636 const PRUnichar *
aData)
638 NS_ENSURE_ARG_POINTER(aTopic);
640 if (strcmp(aTopic,
"final-ui-startup") == 0) {
643 else if (strcmp(aTopic,
"quit-application-granted") == 0) {
645 nsCOMPtr<nsIObserverService> observerService =
646 do_GetService(
"@mozilla.org/observer-service;1", &rv);
647 NS_ENSURE_SUCCESS(rv, rv);
649 rv = observerService->RemoveObserver(
this,
"final-ui-startup");
650 NS_ENSURE_SUCCESS(rv, rv);
652 rv = observerService->RemoveObserver(
this, aTopic);
653 NS_ENSURE_SUCCESS(rv, rv);
660 sbMacAppDelegateManager::RegisterSelf(nsIComponentManager *aCompMgr,
662 const char *aLoaderStr,
664 const nsModuleComponentInfo *aInfo)
666 NS_ENSURE_ARG_POINTER(aCompMgr);
667 NS_ENSURE_ARG_POINTER(aPath);
668 NS_ENSURE_ARG_POINTER(aLoaderStr);
669 NS_ENSURE_ARG_POINTER(aType);
670 NS_ENSURE_ARG_POINTER(aInfo);
672 nsresult rv = NS_ERROR_UNEXPECTED;
673 nsCOMPtr<nsICategoryManager> catMgr =
674 do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
675 NS_ENSURE_SUCCESS(rv, rv);
677 rv = catMgr->AddCategoryEntry(
"app-startup",
681 PR_TRUE, PR_TRUE, nsnull);
static nsCOMPtr< nsIObserverService > observerService
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
#define SONGBIRD_MACAPPDELEGATEMANAGER_CONTRACTID
BOOL _isPlaybackPlaying()
BOOL _isPlaybackStoppable()
static void * get(nsISupports *window)
void _setOverride:(id aOverrideObj)
#define SONGBIRD_MACAPPDELEGATEMANAGER_CLASSNAME
#define SB_PROPERTY_ARTISTNAME
#define SB_PROPERTY_TRACKNAME
_updateTextAndScrollDataForFrame aData