From eeeef45c692d3497aacb7116cbebd939b672d4b8 Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Sun, 31 Jan 2010 01:03:06 +0000 Subject: [PATCH] * 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. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35352 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/mediaplayer/MainApp.cpp | 37 +++++++++++++++++-- src/apps/mediaplayer/MainWin.cpp | 8 +++- .../playlist/ImportPLItemsCommand.cpp | 9 +++-- src/apps/mediaplayer/playlist/Playlist.cpp | 7 +++- src/apps/mediaplayer/playlist/Playlist.h | 7 +++- .../mediaplayer/playlist/PlaylistWindow.cpp | 2 +- 6 files changed, 58 insertions(+), 12 deletions(-) diff --git a/src/apps/mediaplayer/MainApp.cpp b/src/apps/mediaplayer/MainApp.cpp index 0502f39d1e..1b73fcc0f4 100644 --- a/src/apps/mediaplayer/MainApp.cpp +++ b/src/apps/mediaplayer/MainApp.cpp @@ -196,9 +196,40 @@ MainApp::RefsReceived(BMessage* message) // 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(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(); } diff --git a/src/apps/mediaplayer/MainWin.cpp b/src/apps/mediaplayer/MainWin.cpp index 38b325fb79..6f3537fc1e 100644 --- a/src/apps/mediaplayer/MainWin.cpp +++ b/src/apps/mediaplayer/MainWin.cpp @@ -1122,9 +1122,13 @@ MainWin::_RefsReceived(BMessage* message) // the playlist is replaced by dropped files // or the dropped files are appended to the end // of the existing playlist if 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, diff --git a/src/apps/mediaplayer/playlist/ImportPLItemsCommand.cpp b/src/apps/mediaplayer/playlist/ImportPLItemsCommand.cpp index a77e1a62b4..2518c014b0 100644 --- a/src/apps/mediaplayer/playlist/ImportPLItemsCommand.cpp +++ b/src/apps/mediaplayer/playlist/ImportPLItemsCommand.cpp @@ -66,7 +66,7 @@ ImportPLItemsCommand::ImportPLItemsCommand(Playlist* playlist, 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 @@ ImportPLItemsCommand::Perform() 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 @@ ImportPLItemsCommand::Undo() 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++) { diff --git a/src/apps/mediaplayer/playlist/Playlist.cpp b/src/apps/mediaplayer/playlist/Playlist.cpp index d6af7ca738..e0359f04cd 100644 --- a/src/apps/mediaplayer/playlist/Playlist.cpp +++ b/src/apps/mediaplayer/playlist/Playlist.cpp @@ -409,10 +409,13 @@ Playlist::RemoveListener(Listener* listener) 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(); diff --git a/src/apps/mediaplayer/playlist/Playlist.h b/src/apps/mediaplayer/playlist/Playlist.h index cada300fe3..3f53847cea 100644 --- a/src/apps/mediaplayer/playlist/Playlist.h +++ b/src/apps/mediaplayer/playlist/Playlist.h @@ -33,6 +33,10 @@ class BString; 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 @@ public: // 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, diff --git a/src/apps/mediaplayer/playlist/PlaylistWindow.cpp b/src/apps/mediaplayer/playlist/PlaylistWindow.cpp index ed01acf9e0..cb3212cc2b 100644 --- a/src/apps/mediaplayer/playlist/PlaylistWindow.cpp +++ b/src/apps/mediaplayer/playlist/PlaylistWindow.cpp @@ -142,7 +142,7 @@ PlaylistWindow::MessageReceived(BMessage* message) 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