37 #include "nsIInterfaceRequestor.h"
38 #include "nsIComponentManager.h"
39 #include "nsIServiceManager.h"
40 #include "nsIComponentManager.h"
44 #include "nsComponentManagerUtils.h"
45 #include "nsServiceManagerUtils.h"
46 #include "nsCOMArray.h"
47 #include "nsIMutableArray.h"
48 #include "nsEnumeratorUtils.h"
49 #include "nsArrayUtils.h"
50 #include "nsArrayEnumerator.h"
52 #include "nsCRTGlue.h"
53 #include <nsStringGlue.h>
57 #include <sbIMediaList.h>
58 #include <sbILibrary.h>
59 #include <sbILibraryManager.h>
61 #define MODULE_SHORTCIRCUIT 0
63 #if MODULE_SHORTCIRCUIT
64 # define METHOD_SHORTCIRCUIT return NS_OK;
65 # define VMETHOD_SHORTCIRCUIT return;
67 # define METHOD_SHORTCIRCUIT
68 # define VMETHOD_SHORTCIRCUIT
85 CPlaylistCommandsManager::FindOrCreateRootCommand(commandobjmap_t *map,
86 const nsAString &aSearchString,
89 NS_ENSURE_ARG_POINTER(map);
90 NS_ENSURE_ARG_POINTER(_retval);
93 nsString searchString(aSearchString);
94 nsCOMPtr<sbIPlaylistCommandsBuilder> rootCommand;
95 commandobjmap_t::iterator iter = map->find(searchString);
96 if (iter != map->end()) {
98 rootCommand = iter->second;
102 rootCommand = do_CreateInstance
103 (
"@songbirdnest.com/Songbird/PlaylistCommandsBuilder;1", &rv);
104 NS_ENSURE_SUCCESS(rv, rv);
107 rv = rootCommand->Init(searchString);
108 NS_ENSURE_SUCCESS(rv, rv);
109 (*map)[searchString] = rootCommand;
113 listenermap_t::iterator it;
114 it = m_ListenerMap.find(searchString);
115 if (it != m_ListenerMap.end())
117 nsCOMArray<sbIPlaylistCommandsListener> listeners = it->second;
118 PRUint32 length = listeners.Count();
119 for (PRUint32
i=0;
i < length;
i++)
121 rv = rootCommand->AddListener(listeners[
i]);
122 NS_ENSURE_SUCCESS(rv, rv);
126 NS_ADDREF(*_retval = rootCommand);
134 const nsAString &aContextGUID,
135 const nsAString &aPlaylistType,
138 NS_ENSURE_ARG_POINTER(map);
139 NS_ENSURE_ARG_POINTER(aCommandObj);
143 nsString
key(aContextGUID);
144 nsString type(aPlaylistType);
147 nsresult rv = aCommandObj->GetId(
id);
148 if ( !NS_SUCCEEDED(rv) ||
id.IsEmpty() )
150 NS_ERROR(
"PlaylistCommandsManager::Cannot register a playlist command without an id");
151 return NS_ERROR_INVALID_ARG;
153 nsCOMPtr<sbIPlaylistCommandsBuilder> rootCommand;
156 if (!type.IsEmpty()) {
157 rv = FindOrCreateRootCommand(map, type, getter_AddRefs(rootCommand));
158 NS_ENSURE_SUCCESS(rv, rv);
160 rv = rootCommand->AddCommandObject(aCommandObj);
161 NS_ENSURE_SUCCESS(rv, rv);
165 if (!key.IsEmpty()) {
166 rv = FindOrCreateRootCommand(map, key, getter_AddRefs(rootCommand));
167 NS_ENSURE_SUCCESS(rv, rv);
169 rv = rootCommand->AddCommandObject(aCommandObj);
170 NS_ENSURE_SUCCESS(rv, rv);
180 const nsAString &aContextGUID,
181 const nsAString &aPlaylistType,
184 NS_ENSURE_ARG_POINTER(map);
185 NS_ENSURE_ARG_POINTER(aCommandObj);
190 nsString
key(aContextGUID);
191 nsString type(aPlaylistType);
193 nsCOMPtr<sbIPlaylistCommands> rootCommand;
195 if (!type.IsEmpty()) {
196 commandobjmap_t::iterator iter = map->find(type);
197 if (iter != map->end()) {
198 rootCommand = iter->second;
199 rv = rootCommand->RemoveCommandObject(aCommandObj);
200 NS_ENSURE_SUCCESS(rv, rv);
206 NS_ENSURE_SUCCESS(rv, rv);
208 if (numCommands == 0) {
209 rv = rootCommand->ShutdownCommands();
210 NS_ENSURE_SUCCESS(rv, rv);
217 if (!key.IsEmpty()) {
218 commandobjmap_t::iterator iter = map->find(key);
219 if (iter != map->end()) {
220 rootCommand = iter->second;
221 rv = rootCommand->RemoveCommandObject(aCommandObj);
222 NS_ENSURE_SUCCESS(rv, rv);
228 NS_ENSURE_SUCCESS(rv, rv);
230 if (numCommands == 0) {
231 rv = rootCommand->ShutdownCommands();
232 NS_ENSURE_SUCCESS(rv, rv);
244 CPlaylistCommandsManager::GetPlaylistCommands(commandobjmap_t *map,
245 const nsAString &aContextGUID,
246 const nsAString &aPlaylistType,
249 NS_ENSURE_ARG_POINTER(map);
250 NS_ENSURE_ARG_POINTER(_retval);
254 nsString
key(aContextGUID);
255 nsString type(aPlaylistType);
257 nsCOMPtr<sbIPlaylistCommands> rootCommand;
259 commandobjmap_t::iterator iterGUID = map->find(
key);
260 if (iterGUID != map->end())
263 NS_ADDREF(*_retval = iterGUID->second);
267 commandobjmap_t::iterator iterType = map->find(type);
268 if (iterType != map->end())
271 NS_ADDREF(*_retval = iterType->second);
300 LibraryPlaylistCommandsListener::HandleSavedLibraryCommands
301 (PRBool aIsRegistering,
302 libraryGuidToCommandsMap_t *aSavedCommandsMap,
303 commandobjmap_t *aRegistrationMap,
304 const nsAString &aLibraryGUID,
305 const nsAString &aListGUID)
307 NS_ENSURE_ARG_POINTER(aSavedCommandsMap);
308 NS_ENSURE_ARG_POINTER(aRegistrationMap);
311 nsString libGuid(aLibraryGUID);
314 libraryGuidToCommandsMap_t::iterator searchCmdsIter =
315 aSavedCommandsMap->find(libGuid);
318 if (searchCmdsIter != aSavedCommandsMap->end()) {
321 nsCOMArray<sbIPlaylistCommands> *registeredCommands =
322 &searchCmdsIter->second;
325 PRUint32 length = registeredCommands->Count();
326 for (PRUint32
i = 0;
i < length;
i++) {
327 nsCOMPtr<sbIPlaylistCommands> command = (*registeredCommands)[
i];
329 if (aIsRegistering) {
335 NS_ENSURE_SUCCESS(rv, rv);
343 NS_ENSURE_SUCCESS(rv, rv);
353 LibraryPlaylistCommandsListener::RegisterSavedLibraryCommands
354 (libraryGuidToCommandsMap_t *aSavedCommandsMap,
355 commandobjmap_t *aRegistrationMap,
356 const nsAString &aLibraryGUID,
357 const nsAString &aListGUID)
359 NS_ENSURE_ARG_POINTER(aSavedCommandsMap);
360 NS_ENSURE_ARG_POINTER(aRegistrationMap);
362 return HandleSavedLibraryCommands(PR_TRUE,
370 LibraryPlaylistCommandsListener::UnregisterSavedLibraryCommands
371 (libraryGuidToCommandsMap_t *aSavedCommandsMap,
372 commandobjmap_t *aRegistrationMap,
373 const nsAString &aLibraryGUID,
374 const nsAString &aListGUID)
376 NS_ENSURE_ARG_POINTER(aSavedCommandsMap);
377 NS_ENSURE_ARG_POINTER(aRegistrationMap);
379 return HandleSavedLibraryCommands(PR_FALSE,
387 LibraryPlaylistCommandsListener::OnItemAdded(
sbIMediaList *aMediaList,
392 NS_ENSURE_ARG_POINTER(aMediaList);
393 NS_ENSURE_ARG_POINTER(aMediaItem);
397 nsCOMPtr<sbIMediaList> list = do_QueryInterface(aMediaItem, &rv);
398 if (NS_FAILED(rv) || !list) {
404 rv = aMediaItem->GetGuid(listGuid);
405 NS_ENSURE_SUCCESS(rv, rv);
408 nsCOMPtr<sbILibrary> library;
409 rv = aMediaItem->GetLibrary(getter_AddRefs(library));
410 NS_ENSURE_SUCCESS(rv, rv);
413 rv = library->GetGuid(libGuid);
414 NS_ENSURE_SUCCESS(rv, rv);
417 rv = RegisterSavedLibraryCommands
422 NS_ENSURE_SUCCESS(rv, rv);
426 rv = RegisterSavedLibraryCommands
431 NS_ENSURE_SUCCESS(rv, rv);
437 LibraryPlaylistCommandsListener::OnBeforeItemRemoved(
sbIMediaList *aMediaList,
442 NS_ENSURE_ARG_POINTER(aMediaList);
443 NS_ENSURE_ARG_POINTER(aMediaItem);
448 nsCOMPtr<sbIMediaList> list = do_QueryInterface(aMediaItem, &rv);
449 if (NS_FAILED(rv) || !list) {
455 rv = aMediaItem->GetGuid(listGuid);
456 NS_ENSURE_SUCCESS(rv, rv);
459 nsCOMPtr<sbILibrary> library;
460 rv = aMediaItem->GetLibrary(getter_AddRefs(library));
461 NS_ENSURE_SUCCESS(rv, rv);
464 rv = library->GetGuid(libGuid);
465 NS_ENSURE_SUCCESS(rv, rv);
469 rv = UnregisterSavedLibraryCommands
474 NS_ENSURE_SUCCESS(rv, rv);
478 rv = UnregisterSavedLibraryCommands
483 NS_ENSURE_SUCCESS(rv, rv);
490 LibraryPlaylistCommandsListener::OnAfterItemRemoved(
sbIMediaList *aMediaList,
497 LibraryPlaylistCommandsListener::OnItemUpdated(
sbIMediaList *aMediaList,
504 LibraryPlaylistCommandsListener::OnItemMoved(
sbIMediaList *aMediaList,
511 LibraryPlaylistCommandsListener::OnBeforeListCleared(
sbIMediaList *aMediaList,
512 PRBool aExcludeLists,
517 LibraryPlaylistCommandsListener::OnListCleared(
sbIMediaList *aMediaList,
518 PRBool aExcludeLists,
523 LibraryPlaylistCommandsListener::OnBatchBegin(
sbIMediaList *aMediaList)
527 LibraryPlaylistCommandsListener::OnBatchEnd(
sbIMediaList *aMediaList)
532 CPlaylistCommandsManager::GetAllMediaListsForLibrary
536 NS_ENSURE_ARG_POINTER(aLibrary);
537 NS_ENSURE_ARG_POINTER(_retval);
541 nsCOMPtr<nsIArray> mediaLists;
543 NS_LITERAL_STRING(
"1"),
544 getter_AddRefs(mediaLists));
545 if (rv == NS_ERROR_NOT_AVAILABLE) {
548 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
551 NS_ENSURE_SUCCESS(rv, rv);
554 nsCOMPtr<sbIMediaList> libAsList( do_QueryInterface(aLibrary, &rv) );
555 NS_ENSURE_SUCCESS(rv, rv);
557 nsCOMPtr<nsIMutableArray> mutMediaLists( do_QueryInterface(mediaLists, &rv) );
558 NS_ENSURE_SUCCESS(rv, rv);
560 rv = mutMediaLists->AppendElement(libAsList, PR_FALSE);
561 NS_ENSURE_SUCCESS(rv, rv);
563 NS_ADDREF(*_retval = mutMediaLists);
570 CPlaylistCommandsManager::RegisterPlaylistCommandsForLibrary
571 (PRBool aTargetServicePane,
575 NS_ENSURE_ARG_POINTER(aLibrary);
576 NS_ENSURE_ARG_POINTER(aCommandObj);
580 nsCOMPtr<nsIArray> mediaLists;
581 rv = GetAllMediaListsForLibrary(aLibrary,
582 getter_AddRefs(mediaLists));
583 NS_ENSURE_SUCCESS(rv, rv);
592 rv = mediaLists->GetLength(&length);
593 NS_ENSURE_SUCCESS(rv, rv);
594 for (PRUint32
i = 0;
i < length;
i++) {
595 nsCOMPtr<sbIMediaList> currList = do_QueryElementAt(mediaLists,
i, &rv);
596 NS_ENSURE_SUCCESS(rv, rv);
599 rv = currList->GetGuid(guid);
600 NS_ENSURE_SUCCESS(rv, rv);
606 NS_ENSURE_SUCCESS(rv, rv);
617 rv = aLibrary->GetGuid(libGuid);
618 NS_ENSURE_SUCCESS(rv, rv);
622 libraryGuidToCommandsMap_t::iterator registeredCommandsIter =
623 libraryToPlaylistCommandsMap->find(libGuid);
625 if (registeredCommandsIter == libraryToPlaylistCommandsMap->end()) {
628 (*libraryToPlaylistCommandsMap)[libGuid].AppendObject(aCommandObj);
632 nsCOMPtr<LibraryPlaylistCommandsListener>
listener =
634 NS_ENSURE_TRUE(listener, NS_ERROR_OUT_OF_MEMORY);
636 rv = aLibrary->AddListener(listener,
641 NS_ENSURE_SUCCESS(rv, rv);
649 nsCOMArray<sbIPlaylistCommands> *registeredCommands = ®isteredCommandsIter->second;
650 registeredCommands->AppendObject(aCommandObj);
658 CPlaylistCommandsManager::UnregisterPlaylistCommandsForLibrary
659 (PRBool aTargetServicePane,
663 NS_ENSURE_ARG_POINTER(aLibrary);
664 NS_ENSURE_ARG_POINTER(aCommandObj);
668 nsCOMPtr<nsIArray> mediaLists;
669 rv = GetAllMediaListsForLibrary(aLibrary,
670 getter_AddRefs(mediaLists));
671 NS_ENSURE_SUCCESS(rv, rv);
680 rv = mediaLists->GetLength(&length);
681 NS_ENSURE_SUCCESS(rv, rv);
683 bool unregisterFailure =
false;
684 for (PRUint32
i = 0;
i < length;
i++) {
685 nsCOMPtr<sbIMediaList> currList = do_QueryElementAt(mediaLists,
i, &rv);
687 unregisterFailure =
true;
692 rv = currList->GetGuid(guid);
694 unregisterFailure =
true;
703 unregisterFailure =
true;
708 rv = aLibrary->GetGuid(libGuid);
709 NS_ENSURE_SUCCESS(rv, rv);
714 unregisterFailure =
true;
717 if (unregisterFailure) {
718 NS_ERROR(
"Error while unregistering playlist commands from a library");
726 CPlaylistCommandsManager::RegisterPlaylistCommandsMediaList
727 (
const nsAString &aContextGUID,
728 const nsAString &aPlaylistType,
739 CPlaylistCommandsManager::UnregisterPlaylistCommandsMediaList
740 (
const nsAString &aContextGUID,
741 const nsAString &aPlaylistType,
752 CPlaylistCommandsManager::GetPlaylistCommandsMediaList
753 (
const nsAString &aContextGUID,
754 const nsAString &aPlaylistType,
765 CPlaylistCommandsManager::RegisterPlaylistCommandsMediaItem
766 (
const nsAString &aContextGUID,
767 const nsAString &aPlaylistType,
778 CPlaylistCommandsManager::UnregisterPlaylistCommandsMediaItem
779 (
const nsAString &aContextGUID,
780 const nsAString &aPlaylistType,
791 CPlaylistCommandsManager::GetPlaylistCommandsMediaItem
792 (
const nsAString &aContextGUID,
793 const nsAString &aPlaylistType,
804 CPlaylistCommandsManager::FindAllRootCommands(
const nsAString &aContextGUID,
805 const nsAString &aContextType,
808 NS_ENSURE_ARG_POINTER(_retval);
811 nsString guid(aContextGUID);
812 nsString type(aContextType);
813 nsCOMArray<sbIPlaylistCommands>
array;
814 nsCOMPtr<sbIPlaylistCommands> rootCommand;
820 rv = GetPlaylistCommandsMediaList(guid,
822 getter_AddRefs(rootCommand));
823 NS_ENSURE_SUCCESS(rv, rv);
828 array.AppendObject(rootCommand);
832 rv = GetPlaylistCommandsMediaItem(guid,
834 getter_AddRefs(rootCommand));
835 NS_ENSURE_SUCCESS(rv, rv);
839 array.AppendObject(rootCommand);
849 getter_AddRefs(rootCommand));
850 NS_ENSURE_SUCCESS(rv, rv);
855 array.AppendObject(rootCommand);
861 getter_AddRefs(rootCommand));
862 NS_ENSURE_SUCCESS(rv, rv);
867 array.AppendObject(rootCommand);
871 return NS_NewArrayEnumerator(_retval, array);
876 CPlaylistCommandsManager::AddListenerForMediaList(
sbIMediaList *aMediaList,
879 NS_ENSURE_ARG_POINTER(aMediaList);
880 NS_ENSURE_ARG_POINTER(aListener);
886 rv = aMediaList->GetGuid(guid);
887 NS_ENSURE_SUCCESS(rv, rv);
889 rv = aMediaList->GetType(type);
890 NS_ENSURE_SUCCESS(rv, rv);
896 m_ListenerMap[guid].AppendObject(aListener);
897 m_ListenerMap[type].AppendObject(aListener);
902 nsCOMPtr<nsISimpleEnumerator> cmdEnum;
903 rv = FindAllRootCommands(guid, type, getter_AddRefs(cmdEnum));
904 NS_ENSURE_SUCCESS(rv, rv);
908 while (NS_SUCCEEDED(cmdEnum->HasMoreElements(&hasMore)) && hasMore)
910 nsCOMPtr<sbIPlaylistCommands> rootCommand;
911 if (NS_SUCCEEDED(cmdEnum->GetNext(getter_AddRefs(rootCommand))) && rootCommand)
914 rv = rootCommand->AddListener(aListener);
915 NS_ENSURE_SUCCESS(rv, rv);
924 CPlaylistCommandsManager::RemoveListenerFromRootCommands
925 (
const nsString &aContextGUID,
926 const nsString &aPlaylistType,
929 NS_ENSURE_ARG_POINTER(aListener);
932 nsCOMPtr<nsISimpleEnumerator> rootCmdEnum;
933 nsresult rv = FindAllRootCommands(aContextGUID, aPlaylistType, getter_AddRefs(rootCmdEnum));
934 NS_ENSURE_SUCCESS(rv, rv);
938 while (NS_SUCCEEDED(rootCmdEnum->HasMoreElements(&hasMore)) && hasMore)
940 nsCOMPtr<sbIPlaylistCommands> rootCommand;
941 if (NS_SUCCEEDED(rootCmdEnum->GetNext(getter_AddRefs(rootCommand))) && rootCommand)
943 rv = rootCommand->RemoveListener(aListener);
944 NS_ENSURE_SUCCESS(rv, rv);
951 CPlaylistCommandsManager::RemoveListenerInListenerMap
952 (
const nsString &aSearchString,
955 NS_ENSURE_ARG_POINTER(aListener);
957 listenermap_t::iterator foundListeners;
959 foundListeners = m_ListenerMap.find(aSearchString);
962 if (foundListeners != m_ListenerMap.end())
966 nsCOMArray<sbIPlaylistCommandsListener> *listeners = &foundListeners->second;
967 PRUint32 length = listeners->Count();
968 for (PRUint32
i=0;
i < length;
i++)
970 if ((*listeners)[
i] == aListener)
972 listeners->RemoveObjectAt(
i);
980 m_ListenerMap.erase(foundListeners);
990 CPlaylistCommandsManager::RemoveListenerForMediaList(
sbIMediaList *aMediaList,
993 NS_ENSURE_ARG_POINTER(aMediaList);
994 NS_ENSURE_ARG_POINTER(aListener);
1000 rv = aMediaList->GetGuid(guid);
1001 NS_ENSURE_SUCCESS(rv, rv);
1003 rv = aMediaList->GetType(type);
1004 NS_ENSURE_SUCCESS(rv, rv);
1008 rv = RemoveListenerInListenerMap(guid, aListener);
1009 NS_ENSURE_SUCCESS(rv, rv);
1011 rv = RemoveListenerInListenerMap(type, aListener);
1012 NS_ENSURE_SUCCESS(rv, rv);
1015 rv = RemoveListenerFromRootCommands(guid, type, aListener);
1016 NS_ENSURE_SUCCESS(rv, rv);
1023 CPlaylistCommandsManager::Publish(
const nsAString &aCommandsGUID,
1026 NS_ENSURE_ARG_POINTER(aCommandObj);
1027 nsString guid(aCommandsGUID);
1035 CPlaylistCommandsManager::Withdraw(
const nsAString &aCommandsGUID,
1038 NS_ENSURE_ARG_POINTER(aCommandObj);
1039 nsString guid(aCommandsGUID);
1050 nsString guid(aCommandsGUID);
1054 if (cmds) cmds->Duplicate(&dup);
A helper used to construct sbIPlaylistCommands interfaces.
NS_IMETHODIMP RegisterPlaylistCommands(commandobjmap_t *map, const nsAString &aContextGUID, const nsAString &aPlaylistType, sbIPlaylistCommands *aCommandObj)
libraryGuidToCommandsMap_t m_LibraryGuidToServicePaneCommandsMap
The interface for receiving notices when a Command Object is added to or removed from another Command...
NS_DECL_ISUPPORTS NS_DECL_SBIPLAYLISTCOMMANDSMANAGER CPlaylistCommandsManager()
std::map< nsString, nsCOMPtr< sbIPlaylistCommandsBuilder > > commandobjmap_t
#define METHOD_SHORTCIRCUIT
NS_IMPL_ISUPPORTS1(CPlaylistCommandsManager, sbIPlaylistCommandsManager)
virtual ~CPlaylistCommandsManager()
virtual ~LibraryPlaylistCommandsListener()
commandobjmap_t m_ServicePaneCommandObjMap
libraryGuidToCommandsMap_t m_LibraryGuidToMenuOrToolbarCommandsMap
Songbird PlaylistCommandsManager Component Definition.
NS_IMETHODIMP UnregisterPlaylistCommands(commandobjmap_t *map, const nsAString &aContextGUID, const nsAString &aPlaylistType, sbIPlaylistCommands *aCommandObj)
Media library abstraction.
An interface that represents the set of command buttons and context menu items available on a display...
StringArrayEnumerator prototype hasMore
libraryGuidToLibraryListenerMap_t m_LibraryGuidToLibraryListenerMap
NS_IMPL_THREADSAFE_ISUPPORTS1(LibraryPlaylistCommandsListener, sbIMediaListListener) LibraryPlaylistCommandsListener
#define SB_PROPERTY_ISLIST
friend class LibraryPlaylistCommandsListener
std::map< nsString, nsCOMPtr< sbIPlaylistCommands > > m_publishedCommands
An interface to carry around arrays of nsIProperty instances. Users of this interface should only QI ...
std::map< nsString, nsCOMArray< sbIPlaylistCommands > > libraryGuidToCommandsMap_t
_getSelectedPageStyle s i
The service used to register, unregister, and list playlist commands for a particular playlist guid o...
commandobjmap_t m_PlaylistCommandObjMap