Author: mmlr Date: 2010-01-31 02:03:06 +0100 (Sun, 31 Jan 2010) New Revision: 35352 Changeset: http://dev.haiku-os.org/changeset/35352/haiku Modified: haiku/trunk/src/apps/mediaplayer/MainApp.cpp haiku/trunk/src/apps/mediaplayer/MainWin.cpp haiku/trunk/src/apps/mediaplayer/playlist/ImportPLItemsCommand.cpp haiku/trunk/src/apps/mediaplayer/playlist/Playlist.cpp haiku/trunk/src/apps/mediaplayer/playlist/Playlist.h haiku/trunk/src/apps/mediaplayer/playlist/PlaylistWindow.cpp Log: * Implemented a "refs catcher" that will catch refs received through a B_REFS_RECEIVED within a short amount of time (0.5 seconds right now) and send them to the last window to be appended to the playlist. This allows to select multiple media files in Tracker and get them inside a playlist of a single window instead of spawning many individual ones (like when filtering for an album, selecting all tracks and opening them by hitting enter). * Introduce special append index values APPEND_INDEX_REPLACE_PLAYLIST (-1, does the same as before) and APPEND_INDEX_APPEND_LAST. The latter is used when appending through a RefsReceived() call and ensures that the index is evaluated at the actual insertion time (in ImportPLItemsCommand::Perform()) as by the time this function is called the playlist count may have changed already due to multiple RefsReceived() invokations, which would then mess up the order. This makes the above item work as expected. Modified: haiku/trunk/src/apps/mediaplayer/MainApp.cpp =================================================================== --- haiku/trunk/src/apps/mediaplayer/MainApp.cpp 2010-01-31 00:25:10 UTC (rev 35351) +++ haiku/trunk/src/apps/mediaplayer/MainApp.cpp 2010-01-31 01:03:06 UTC (rev 35352) @@ -196,9 +196,40 @@ // or double clicked a file that's handled by this app. // Command line arguments are also redirected to here by // ArgvReceived() but without MIME type check. - // For each file we create a new window and send it a - // B_REFS_RECEIVED message with a single file. - NewWindow(message); + + // If multiple refs are received in short succession we + // combine them into a single window/playlist. Tracker + // will send multiple messages when opening a multi- + // selection for example and we don't want to spawn large + // numbers of windows when someone just tries to open an + // album. We use half a second time and prolong it for + // each new ref received. + static bigtime_t sLastRefsReceived = 0; + static MainWin* sLastRefsWindow = NULL; + + if (system_time() - sLastRefsReceived < 500000) { + // Find the last opened window + for (int32 i = CountWindows() - 1; i >= 0; i--) { + MainWin* playerWindow = dynamic_cast<MainWin*>(WindowAt(i)); + if (playerWindow == NULL) + continue; + + if (playerWindow != sLastRefsWindow) { + // The window has changed since the last refs + sLastRefsReceived = 0; + sLastRefsWindow = NULL; + break; + } + + message->AddBool("append to playlist", true); + playerWindow->PostMessage(message); + sLastRefsReceived = system_time(); + return; + } + } + + sLastRefsWindow = NewWindow(message); + sLastRefsReceived = system_time(); } Modified: haiku/trunk/src/apps/mediaplayer/MainWin.cpp =================================================================== --- haiku/trunk/src/apps/mediaplayer/MainWin.cpp 2010-01-31 00:25:10 UTC (rev 35351) +++ haiku/trunk/src/apps/mediaplayer/MainWin.cpp 2010-01-31 01:03:06 UTC (rev 35352) @@ -1122,9 +1122,13 @@ // the playlist is replaced by dropped files // or the dropped files are appended to the end // of the existing playlist if <shift> is pressed + bool append = false; + if (message->FindBool("append to playlist", &append) != B_OK) + append = modifiers() & B_SHIFT_KEY; + BAutolock _(fPlaylist); - int32 appendIndex = modifiers() & B_SHIFT_KEY ? - fPlaylist->CountItems() : -1; + int32 appendIndex = append ? APPEND_INDEX_APPEND_LAST + : APPEND_INDEX_REPLACE_PLAYLIST; message->AddInt32("append_index", appendIndex); // forward the message to the playlist window, Modified: haiku/trunk/src/apps/mediaplayer/playlist/ImportPLItemsCommand.cpp =================================================================== --- haiku/trunk/src/apps/mediaplayer/playlist/ImportPLItemsCommand.cpp 2010-01-31 00:25:10 UTC (rev 35351) +++ haiku/trunk/src/apps/mediaplayer/playlist/ImportPLItemsCommand.cpp 2010-01-31 01:03:06 UTC (rev 35352) @@ -66,7 +66,7 @@ fPlaylingIndex = fPlaylist->CurrentItemIndex(); - if (fToIndex < 0) { + if (fToIndex == APPEND_INDEX_REPLACE_PLAYLIST) { fOldCount = fPlaylist->CountItems(); if (fOldCount > 0) { fOldItems = new (nothrow) PlaylistItem*[fOldCount]; @@ -112,8 +112,11 @@ fItemsAdded = true; + if (fToIndex == APPEND_INDEX_APPEND_LAST) + fToIndex = fPlaylist->CountItems(); + int32 index = fToIndex; - if (fToIndex < 0) { + if (fToIndex == APPEND_INDEX_REPLACE_PLAYLIST) { fPlaylist->MakeEmpty(false); index = 0; } @@ -142,7 +145,7 @@ fItemsAdded = false; - if (fToIndex < 0) { + if (fToIndex == APPEND_INDEX_REPLACE_PLAYLIST) { // remove new items from playlist and restore old refs fPlaylist->MakeEmpty(false); for (int32 i = 0; i < fOldCount; i++) { Modified: haiku/trunk/src/apps/mediaplayer/playlist/Playlist.cpp =================================================================== --- haiku/trunk/src/apps/mediaplayer/playlist/Playlist.cpp 2010-01-31 00:25:10 UTC (rev 35351) +++ haiku/trunk/src/apps/mediaplayer/playlist/Playlist.cpp 2010-01-31 01:03:06 UTC (rev 35352) @@ -409,11 +409,14 @@ void Playlist::AppendRefs(const BMessage* refsReceivedMessage, int32 appendIndex) { - // the playlist ist replaced by the refs in the message + // the playlist is replaced by the refs in the message // or the refs are appended at the appendIndex // in the existing playlist - bool add = appendIndex >= 0; + if (appendIndex == APPEND_INDEX_APPEND_LAST) + appendIndex = CountItems(); + bool add = appendIndex != APPEND_INDEX_REPLACE_PLAYLIST; + if (!add) MakeEmpty(); Modified: haiku/trunk/src/apps/mediaplayer/playlist/Playlist.h =================================================================== --- haiku/trunk/src/apps/mediaplayer/playlist/Playlist.h 2010-01-31 00:25:10 UTC (rev 35351) +++ haiku/trunk/src/apps/mediaplayer/playlist/Playlist.h 2010-01-31 01:03:06 UTC (rev 35352) @@ -33,6 +33,10 @@ struct entry_ref; +// special append index values +#define APPEND_INDEX_REPLACE_PLAYLIST -1 +#define APPEND_INDEX_APPEND_LAST -2 + extern const uint32 kPlaylistMagicBytes; extern const char* kTextPlaylistMimeString; extern const char* kBinaryPlaylistMimeString; @@ -95,7 +99,8 @@ // support functions void AppendRefs(const BMessage* refsReceivedMessage, - int32 appendIndex = -1); + int32 appendIndex + = APPEND_INDEX_REPLACE_PLAYLIST); static void AppendToPlaylistRecursive(const entry_ref& ref, Playlist* playlist); static void AppendPlaylistToPlaylist(const entry_ref& ref, Modified: haiku/trunk/src/apps/mediaplayer/playlist/PlaylistWindow.cpp =================================================================== --- haiku/trunk/src/apps/mediaplayer/playlist/PlaylistWindow.cpp 2010-01-31 00:25:10 UTC (rev 35351) +++ haiku/trunk/src/apps/mediaplayer/playlist/PlaylistWindow.cpp 2010-01-31 01:03:06 UTC (rev 35352) @@ -142,7 +142,7 @@ case B_REFS_RECEIVED: // Used for when we open a playlist from playlist window - message->AddInt32("append_index", -1); + message->AddInt32("append_index", APPEND_INDEX_REPLACE_PLAYLIST); case B_SIMPLE_DATA: { // only accept this message when it comes from the // player window, _not_ when it is dropped in this window