add CLI play-playlist option, to play given playlist name. (#608)

* add CLI play-playlist option, to play given playlist name.

* Apply MR change requests
This commit is contained in:
Nicolas Toussaint 2020-12-04 19:08:41 +01:00 committed by GitHub
parent 5b5f728f49
commit 3a9a1f0a94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 32 deletions

View File

@ -44,36 +44,37 @@ const char *CommandlineOptions::kHelpText =
"%1: strawberry [%2] [%3]\n"
"\n"
"%4:\n"
" -p, --play %5\n"
" -t, --play-pause %6\n"
" -u, --pause %7\n"
" -s, --stop %8\n"
" -q, --stop-after-current %9\n"
" -r, --previous %10\n"
" -f, --next %11\n"
" -v, --volume <value> %12\n"
" --volume-up %13\n"
" --volume-down %14\n"
" --volume-increase-by %15\n"
" --volume-decrease-by %16\n"
" --seek-to <seconds> %17\n"
" --seek-by <seconds> %18\n"
" --restart-or-previous %19\n"
" -p, --play %5\n"
" -t, --play-pause %6\n"
" -u, --pause %7\n"
" -s, --stop %8\n"
" -q, --stop-after-current %9\n"
" -r, --previous %10\n"
" -f, --next %11\n"
" -v, --volume <value> %12\n"
" --volume-up %13\n"
" --volume-down %14\n"
" --volume-increase-by %15\n"
" --volume-decrease-by %16\n"
" --seek-to <seconds> %17\n"
" --seek-by <seconds> %18\n"
" --restart-or-previous %19\n"
"\n"
"%20:\n"
" -c, --create <name> %21\n"
" -a, --append %22\n"
" -l, --load %23\n"
" -k, --play-track <n> %24\n"
" -c, --create <name> %21\n"
" -a, --append %22\n"
" -l, --load %23\n"
" -k, --play-track <n> %24\n"
" -i, --play-playlist <name> %25\n"
"\n"
"%25:\n"
" -o, --show-osd %26\n"
" -y, --toggle-pretty-osd %27\n"
" -g, --language <lang> %28\n"
" --quiet %29\n"
" --verbose %30\n"
" --log-levels <levels> %31\n"
" --version %32\n";
"%26:\n"
" -o, --show-osd %27\n"
" -y, --toggle-pretty-osd %28\n"
" -g, --language <lang> %29\n"
" --quiet %30\n"
" --verbose %31\n"
" --log-levels <levels> %32\n"
" --version %33\n";
const char *CommandlineOptions::kVersionText = "Strawberry %1";
@ -138,6 +139,7 @@ bool CommandlineOptions::Parse() {
{"append", no_argument, nullptr, 'a'},
{"load", no_argument, nullptr, 'l'},
{"play-track", required_argument, nullptr, 'k'},
{"play-playlist", required_argument, nullptr, 'i'},
{"show-osd", no_argument, nullptr, 'o'},
{"toggle-pretty-osd", no_argument, nullptr, 'y'},
{"language", required_argument, nullptr, 'g'},
@ -150,7 +152,7 @@ bool CommandlineOptions::Parse() {
// Parse the arguments
bool ok = false;
forever {
int c = getopt_long(argc_, argv_, "hptusqrfv:c:alk:oyg:", kOptions, nullptr);
int c = getopt_long(argc_, argv_, "hptusqrfv:c:alk:i:oyg:", kOptions, nullptr);
// End of the options
if (c == -1) break;
@ -179,7 +181,8 @@ bool CommandlineOptions::Parse() {
tr("Create a new playlist with files"),
tr("Append files/URLs to the playlist"),
tr("Loads files/URLs, replacing current playlist"),
tr("Play the <n>th track in the playlist"))
tr("Play the <n>th track in the playlist"),
tr("Play given playlist"))
.arg(tr("Other options"), tr("Display the on-screen-display"),
tr("Toggle visibility for the pretty on-screen-display"),
tr("Change the language"),
@ -213,6 +216,10 @@ bool CommandlineOptions::Parse() {
case 'f':
player_action_ = Player_Next;
break;
case 'i':
player_action_ = Player_PlayPlaylist;
playlist_name_ = QString(optarg);
break;
case 'c':
url_list_action_ = UrlList_CreateNew;
playlist_name_ = QString(optarg);
@ -362,7 +369,8 @@ QDataStream& operator<<(QDataStream &s, const CommandlineOptions &a) {
<< a.show_osd_
<< a.urls_
<< a.log_levels_
<< a.toggle_pretty_osd_;
<< a.toggle_pretty_osd_
<< a.playlist_name_;
return s;
@ -382,7 +390,8 @@ QDataStream& operator>>(QDataStream &s, CommandlineOptions &a) {
>> a.show_osd_
>> a.urls_
>> a.log_levels_
>> a.toggle_pretty_osd_;
>> a.toggle_pretty_osd_
>> a.playlist_name_;
a.player_action_ = CommandlineOptions::PlayerAction(player_action);
a.url_list_action_ = CommandlineOptions::UrlListAction(url_list_action);

View File

@ -57,6 +57,7 @@ class CommandlineOptions {
Player_Next = 6,
Player_RestartOrPrevious = 7,
Player_StopAfterCurrent = 8,
Player_PlayPlaylist = 9,
};
bool Parse();

View File

@ -2274,6 +2274,14 @@ void MainWindow::CommandlineOptionsReceived(const CommandlineOptions &options) {
case CommandlineOptions::Player_Next:
app_->player()->Next();
break;
case CommandlineOptions::Player_PlayPlaylist:
if (options.playlist_name().isEmpty()) {
qLog(Error) << "ERROR: playlist name missing";
}
else {
app_->player()->PlayPlaylist(options.playlist_name());
}
break;
case CommandlineOptions::Player_RestartOrPrevious:
app_->player()->RestartOrPrevious();
break;

View File

@ -398,6 +398,36 @@ void Player::NextItem(const Engine::TrackChangeFlags change, const Playlist::Aut
}
void Player::PlayPlaylist(const QString &playlistName) {
PlayPlaylistInternal(Engine::Manual, Playlist::AutoScroll_Always, playlistName);
}
void Player::PlayPlaylistInternal(Engine::TrackChangeFlags change, const Playlist::AutoScroll autoscroll, const QString &playlistName) {
Playlist *playlist = nullptr;
for (Playlist *p : app_->playlist_manager()->GetAllPlaylists()) {
if (playlistName == app_->playlist_manager()->GetPlaylistName(p->id())) {
playlist = p;
break;
}
}
if (playlist == nullptr) {
qLog(Warning) << "Playlist '" << playlistName << "' not found.";
return;
}
app_->playlist_manager()->SetActivePlaylist(playlist->id());
app_->playlist_manager()->SetCurrentPlaylist(playlist->id());
if (playlist->rowCount() == 0) return;
int i = app_->playlist_manager()->active()->current_row();
if (i == -1) i = app_->playlist_manager()->active()->last_played_row();
if (i == -1) i = 0;
PlayAt(i, change, autoscroll, true);
}
bool Player::HandleStopAfter(const Playlist::AutoScroll autoscroll) {
if (app_->playlist_manager()->active()->stop_after_current()) {

View File

@ -81,8 +81,8 @@ class PlayerInterface : public QObject {
// Skips this track. Might load more of the current radio station.
virtual void Next() = 0;
virtual void Previous() = 0;
virtual void PlayPlaylist(const QString &playlistName) = 0;
virtual void SetVolume(const int value) = 0;
virtual void VolumeUp() = 0;
virtual void VolumeDown() = 0;
@ -163,6 +163,7 @@ class Player : public PlayerInterface {
void RestartOrPrevious() override;
void Next() override;
void Previous() override;
void PlayPlaylist(const QString &playlistName) override;
void SetVolume(const int value) override;
void VolumeUp() override { SetVolume(GetVolume() + 5); }
void VolumeDown() override { SetVolume(GetVolume() - 5); }
@ -195,6 +196,7 @@ class Player : public PlayerInterface {
void PreviousItem(const Engine::TrackChangeFlags change);
void NextInternal(const Engine::TrackChangeFlags, const Playlist::AutoScroll autoscroll);
void PlayPlaylistInternal(Engine::TrackChangeFlags, const Playlist::AutoScroll autoscroll, const QString &playlistName);
void FatalError();
void ValidSongRequested(const QUrl&);