From 92cb0c5d18d60b3ce7dc816341444371cf9cefed Mon Sep 17 00:00:00 2001 From: Dario Casalinuovo Date: Thu, 30 Jun 2016 16:33:24 +0200 Subject: [PATCH] MediaPlayer: Add GUI to open network streams --- src/apps/mediaplayer/Jamfile | 2 + src/apps/mediaplayer/MainApp.h | 4 +- src/apps/mediaplayer/MainWin.cpp | 18 ++++ src/apps/mediaplayer/NetworkStreamWin.cpp | 86 +++++++++++++++++++ src/apps/mediaplayer/NetworkStreamWin.h | 28 ++++++ .../playlist/ImportPLItemsCommand.cpp | 2 +- src/apps/mediaplayer/playlist/Playlist.cpp | 11 ++- src/apps/mediaplayer/playlist/Playlist.h | 3 +- .../mediaplayer/playlist/PlaylistItem.cpp | 2 + .../mediaplayer/playlist/PlaylistListView.cpp | 6 +- .../mediaplayer/playlist/PlaylistListView.h | 2 +- .../mediaplayer/playlist/PlaylistWindow.cpp | 3 +- .../mediaplayer/playlist/UrlPlaylistItem.cpp | 24 +++--- 13 files changed, 172 insertions(+), 19 deletions(-) create mode 100644 src/apps/mediaplayer/NetworkStreamWin.cpp create mode 100644 src/apps/mediaplayer/NetworkStreamWin.h diff --git a/src/apps/mediaplayer/Jamfile b/src/apps/mediaplayer/Jamfile index 63ec26dc10..cf145798fa 100644 --- a/src/apps/mediaplayer/Jamfile +++ b/src/apps/mediaplayer/Jamfile @@ -112,6 +112,7 @@ Application MediaPlayer : InfoWin.cpp MainApp.cpp MainWin.cpp + NetworkStreamWin.cpp VideoView.cpp : be game media tracker translation textencoding [ TargetLibstdc++ ] @@ -128,6 +129,7 @@ DoCatalogs MediaPlayer : MainApp.cpp MainWin.cpp MovePLItemsCommand.cpp + NetworkStreamWin.cpp PeakView.cpp PlaylistItem.cpp PlaylistWindow.cpp diff --git a/src/apps/mediaplayer/MainApp.h b/src/apps/mediaplayer/MainApp.h index 5111df8745..282b765ab0 100644 --- a/src/apps/mediaplayer/MainApp.h +++ b/src/apps/mediaplayer/MainApp.h @@ -46,7 +46,9 @@ enum { M_OPEN_PANEL_RESULT = 'oprs', M_SAVE_PANEL_RESULT = 'sprs', - M_OPEN_PREVIOUS_PLAYLIST = 'oppp' + M_OPEN_PREVIOUS_PLAYLIST = 'oppp', + + M_URL_RECEIVED = 'urrc' }; diff --git a/src/apps/mediaplayer/MainWin.cpp b/src/apps/mediaplayer/MainWin.cpp index 524fad01b0..46727288ee 100644 --- a/src/apps/mediaplayer/MainWin.cpp +++ b/src/apps/mediaplayer/MainWin.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -55,6 +56,7 @@ #include "DurationToString.h" #include "FilePlaylistItem.h" #include "MainApp.h" +#include "NetworkStreamWin.h" #include "PeakView.h" #include "PlaylistItem.h" #include "PlaylistObserver.h" @@ -76,6 +78,7 @@ int MainWin::sNoVideoWidth = MIN_WIDTH; enum { M_DUMMY = 0x100, M_FILE_OPEN = 0x1000, + M_NETWORK_STREAM_OPEN, M_FILE_INFO, M_FILE_PLAYLIST, M_FILE_CLOSE, @@ -617,8 +620,10 @@ MainWin::MessageReceived(BMessage* msg) } case B_REFS_RECEIVED: + case M_URL_RECEIVED: _RefsReceived(msg); break; + case B_SIMPLE_DATA: if (msg->HasRef("refs")) _RefsReceived(msg); @@ -831,6 +836,15 @@ MainWin::MessageReceived(BMessage* msg) be_app->PostMessage(&appMessage); break; } + + case M_NETWORK_STREAM_OPEN: + { + BMessenger target(this); + NetworkStreamWin* win = new NetworkStreamWin(target); + win->Show(); + break; + } + case M_FILE_INFO: ShowFileInfo(); break; @@ -1499,6 +1513,10 @@ MainWin::_CreateMenu() item->SetShortcut('O', 0); fFileMenu->AddItem(item); + item = new BMenuItem(B_TRANSLATE("Open network stream"), + new BMessage(M_NETWORK_STREAM_OPEN)); + fFileMenu->AddItem(item); + fFileMenu->AddSeparatorItem(); fFileMenu->AddItem(new BMenuItem(B_TRANSLATE("File info" B_UTF8_ELLIPSIS), diff --git a/src/apps/mediaplayer/NetworkStreamWin.cpp b/src/apps/mediaplayer/NetworkStreamWin.cpp new file mode 100644 index 0000000000..e95a3fafa1 --- /dev/null +++ b/src/apps/mediaplayer/NetworkStreamWin.cpp @@ -0,0 +1,86 @@ +/* + * Copyright 2016 Dario Casalinuovo. All rights reserved. + * Distributed under the terms of the MIT License. + * + */ + + +#include "NetworkStreamWin.h" + +#include +#include +#include +#include + +#include "MainApp.h" + + +#undef B_TRANSLATION_CONTEXT +#define B_TRANSLATION_CONTEXT "MediaPlayer-NetworkStream" + + +enum { + M_OPEN_URL = 0, + M_CANCEL +}; + + +NetworkStreamWin::NetworkStreamWin(BMessenger target) + : + BWindow(BRect(0, 0, 300, 100), "Open Network Stream", + B_TITLED_WINDOW, B_NOT_RESIZABLE), + fTarget(target) +{ + fTextControl = new BTextControl("InputControl", + "Insert URL", NULL, NULL); + + BLayoutBuilder::Group<>(this, B_VERTICAL) + .SetInsets(0, 0, 0, 0) + .Add(fTextControl) + .AddGroup(B_HORIZONTAL) + .Add(new BButton("Ok", new BMessage(M_OPEN_URL))) + .Add(new BButton("Cancel", new BMessage(M_CANCEL))) + .End() + .End(); +} + + +NetworkStreamWin::~NetworkStreamWin() +{ +} + + +void +NetworkStreamWin::MessageReceived(BMessage* message) +{ + switch(message->what) { + case M_OPEN_URL: + { + BUrl url(fTextControl->Text()); + if (!url.IsValid()) { + BAlert* alert = new BAlert(B_TRANSLATE("Bad URL"), + B_TRANSLATE("Invalid URL inserted!"), + B_TRANSLATE("OK")); + alert->Go(); + return; + } + + BMessage archivedUrl; + url.Archive(&archivedUrl); + + BMessage msg(M_URL_RECEIVED); + msg.AddMessage("mediaplayer:url", &archivedUrl); + fTarget.SendMessage(&msg); + + Quit(); + break; + } + + case M_CANCEL: + Quit(); + break; + + default: + BWindow::MessageReceived(message); + } +} diff --git a/src/apps/mediaplayer/NetworkStreamWin.h b/src/apps/mediaplayer/NetworkStreamWin.h new file mode 100644 index 0000000000..b88f629831 --- /dev/null +++ b/src/apps/mediaplayer/NetworkStreamWin.h @@ -0,0 +1,28 @@ +/* + * Copyright 2016 Dario Casalinuovo. All rights reserved. + * Distributed under the terms of the MIT License. + * + */ +#ifndef __NETWORK_STREAM_WIN_H +#define __NETWORK_STREAM_WIN_H + + +#include +#include +#include + + +class NetworkStreamWin : public BWindow +{ +public: + NetworkStreamWin(BMessenger target); + virtual ~NetworkStreamWin(); + + virtual void MessageReceived(BMessage* message); + +private: + BMessenger fTarget; + BTextControl* fTextControl; +}; + +#endif diff --git a/src/apps/mediaplayer/playlist/ImportPLItemsCommand.cpp b/src/apps/mediaplayer/playlist/ImportPLItemsCommand.cpp index be2f6889d6..8c906c4a25 100644 --- a/src/apps/mediaplayer/playlist/ImportPLItemsCommand.cpp +++ b/src/apps/mediaplayer/playlist/ImportPLItemsCommand.cpp @@ -45,7 +45,7 @@ ImportPLItemsCommand::ImportPLItemsCommand(Playlist* playlist, return; Playlist temp; - temp.AppendRefs(refsMessage); + temp.AppendItems(refsMessage); fNewCount = temp.CountItems(); if (fNewCount <= 0) diff --git a/src/apps/mediaplayer/playlist/Playlist.cpp b/src/apps/mediaplayer/playlist/Playlist.cpp index a4dd78d53d..dc4714a563 100644 --- a/src/apps/mediaplayer/playlist/Playlist.cpp +++ b/src/apps/mediaplayer/playlist/Playlist.cpp @@ -429,7 +429,7 @@ Playlist::RemoveListener(Listener* listener) void -Playlist::AppendRefs(const BMessage* refsReceivedMessage, int32 appendIndex) +Playlist::AppendItems(const BMessage* refsReceivedMessage, int32 appendIndex) { // the playlist is replaced by the refs in the message // or the refs are appended at the appendIndex @@ -448,6 +448,15 @@ Playlist::AppendRefs(const BMessage* refsReceivedMessage, int32 appendIndex) Playlist* playlist = add ? &temporaryPlaylist : this; bool sortPlaylist = true; + // TODO: This is not very fair, we should abstract from + // entry ref representation and support more URLs. + BMessage archivedUrl; + if (refsReceivedMessage->FindMessage("mediaplayer:url", &archivedUrl) + == B_OK) { + BUrl url(&archivedUrl); + AddItem(new UrlPlaylistItem(url)); + } + entry_ref ref; int32 subAppendIndex = CountItems(); for (int i = 0; refsReceivedMessage->FindRef("refs", i, &ref) == B_OK; diff --git a/src/apps/mediaplayer/playlist/Playlist.h b/src/apps/mediaplayer/playlist/Playlist.h index a048d789ba..1fb67e78eb 100644 --- a/src/apps/mediaplayer/playlist/Playlist.h +++ b/src/apps/mediaplayer/playlist/Playlist.h @@ -105,9 +105,10 @@ public: void RemoveListener(Listener* listener); // support functions - void AppendRefs(const BMessage* refsReceivedMessage, + void AppendItems(const BMessage* refsReceivedMessage, 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/PlaylistItem.cpp b/src/apps/mediaplayer/playlist/PlaylistItem.cpp index e6c1003122..e75f835ef9 100644 --- a/src/apps/mediaplayer/playlist/PlaylistItem.cpp +++ b/src/apps/mediaplayer/playlist/PlaylistItem.cpp @@ -139,6 +139,7 @@ PlaylistItem::TrackNumber() const bigtime_t PlaylistItem::Duration() { + printf("duration\n"); bigtime_t duration; if (GetAttribute(ATTR_INT64_DURATION, duration) != B_OK) { duration = this->_CalculateDuration(); @@ -188,6 +189,7 @@ PlaylistItem::_NotifyListeners() const bigtime_t PlaylistItem::_CalculateDuration() { + printf("calc duration\n"); // To be overridden in subclasses with more efficient methods TrackSupplier* supplier = GetTrackSupplier(); diff --git a/src/apps/mediaplayer/playlist/PlaylistListView.cpp b/src/apps/mediaplayer/playlist/PlaylistListView.cpp index 005a7d5492..cc28fa5e45 100644 --- a/src/apps/mediaplayer/playlist/PlaylistListView.cpp +++ b/src/apps/mediaplayer/playlist/PlaylistListView.cpp @@ -310,12 +310,12 @@ PlaylistListView::MessageReceived(BMessage* message) case B_SIMPLE_DATA: if (message->HasRef("refs")) - RefsReceived(message, fDropIndex); + ItemsReceived(message, fDropIndex); else if (message->HasPointer("list")) SimpleListView::MessageReceived(message); break; case B_REFS_RECEIVED: - RefsReceived(message, fDropIndex); + ItemsReceived(message, fDropIndex); break; default: @@ -506,7 +506,7 @@ PlaylistListView::DrawListItem(BView* owner, int32 index, BRect frame) const void -PlaylistListView::RefsReceived(BMessage* message, int32 appendIndex) +PlaylistListView::ItemsReceived(const BMessage* message, int32 appendIndex) { if (fCommandStack->Perform(new (nothrow) ImportPLItemsCommand(fPlaylist, message, appendIndex)) != B_OK) { diff --git a/src/apps/mediaplayer/playlist/PlaylistListView.h b/src/apps/mediaplayer/playlist/PlaylistListView.h index dc2bf83c89..fabdb0f38e 100644 --- a/src/apps/mediaplayer/playlist/PlaylistListView.h +++ b/src/apps/mediaplayer/playlist/PlaylistListView.h @@ -41,7 +41,7 @@ public: BRect frame) const; // PlaylistListView - void RefsReceived(BMessage* message, + void ItemsReceived(const BMessage* message, int32 appendIndex); void Randomize(); diff --git a/src/apps/mediaplayer/playlist/PlaylistWindow.cpp b/src/apps/mediaplayer/playlist/PlaylistWindow.cpp index e887ec031c..1fef9dc702 100644 --- a/src/apps/mediaplayer/playlist/PlaylistWindow.cpp +++ b/src/apps/mediaplayer/playlist/PlaylistWindow.cpp @@ -179,6 +179,7 @@ PlaylistWindow::MessageReceived(BMessage* message) break; } + case M_URL_RECEIVED: case B_REFS_RECEIVED: // Used for when we open a playlist from playlist window if (!message->HasInt32("append_index")) { @@ -193,7 +194,7 @@ PlaylistWindow::MessageReceived(BMessage* message) // outside of the playlist! int32 appendIndex; if (message->FindInt32("append_index", &appendIndex) == B_OK) - fListView->RefsReceived(message, appendIndex); + fListView->ItemsReceived(message, appendIndex); break; } diff --git a/src/apps/mediaplayer/playlist/UrlPlaylistItem.cpp b/src/apps/mediaplayer/playlist/UrlPlaylistItem.cpp index d987ef676c..cc28d81035 100644 --- a/src/apps/mediaplayer/playlist/UrlPlaylistItem.cpp +++ b/src/apps/mediaplayer/playlist/UrlPlaylistItem.cpp @@ -15,7 +15,6 @@ UrlPlaylistItem::UrlPlaylistItem(BUrl url) : fUrl(url) { - } @@ -52,7 +51,7 @@ UrlPlaylistItem::Instantiate(BMessage* archive) status_t UrlPlaylistItem::Archive(BMessage* into, bool deep) const { - return B_ERROR; + return B_NOT_SUPPORTED; } @@ -66,35 +65,40 @@ UrlPlaylistItem::SetAttribute(const Attribute& attribute, const BString& string) status_t UrlPlaylistItem::GetAttribute(const Attribute& attribute, BString& string) const { - return B_ERROR; + if (attribute == ATTR_STRING_NAME) { + string = fUrl.UrlString(); + return B_OK; + } + + return B_NOT_SUPPORTED; } status_t UrlPlaylistItem::SetAttribute(const Attribute& attribute, const int32& value) { - return B_ERROR; + return B_NOT_SUPPORTED; } status_t UrlPlaylistItem::GetAttribute(const Attribute& attribute, int32& value) const { - return B_ERROR; + return B_NOT_SUPPORTED; } status_t UrlPlaylistItem::SetAttribute(const Attribute& attribute, const int64& value) { - return B_ERROR; + return B_NOT_SUPPORTED; } status_t UrlPlaylistItem::GetAttribute(const Attribute& attribute, int64& value) const { - return B_ERROR; + return B_NOT_SUPPORTED; } @@ -108,21 +112,21 @@ UrlPlaylistItem::LocationURI() const status_t UrlPlaylistItem::GetIcon(BBitmap* bitmap, icon_size iconSize) const { - return B_ERROR; + return B_NOT_SUPPORTED; } status_t UrlPlaylistItem::MoveIntoTrash() { - return B_ERROR; + return B_NOT_SUPPORTED; } status_t UrlPlaylistItem::RestoreFromTrash() { - return B_ERROR; + return B_NOT_SUPPORTED; }