From 4f3040e0ecd126a0e27253aea596100777b48b5d Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Sat, 29 Aug 2009 23:53:36 +0200 Subject: [PATCH] move code responsible for adding selected items to playlists to misc.cpp --- src/Makefile.am | 12 +-- src/misc.cpp | 190 ++++++++++++++++++++++++++++++++++++++++++++++++ src/misc.h | 56 ++++++++++++++ src/mpdpp.cpp | 3 +- src/mpdpp.h | 2 +- src/ncmpcpp.cpp | 131 +-------------------------------- 6 files changed, 258 insertions(+), 136 deletions(-) create mode 100644 src/misc.cpp create mode 100644 src/misc.h diff --git a/src/Makefile.am b/src/Makefile.am index c626a64a..b1d72a14 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,9 +1,9 @@ bin_PROGRAMS = ncmpcpp -ncmpcpp_SOURCES = browser.cpp charset.cpp clock.cpp conv.cpp display.cpp help.cpp \ - helpers.cpp info.cpp libmpdclient.c lyrics.cpp media_library.cpp menu.cpp \ - mpdpp.cpp ncmpcpp.cpp outputs.cpp playlist.cpp playlist_editor.cpp scrollpad.cpp \ - search_engine.cpp settings.cpp song.cpp status.cpp str_pool.c tag_editor.cpp \ - visualizer.cpp window.cpp +ncmpcpp_SOURCES = browser.cpp charset.cpp clock.cpp conv.cpp display.cpp \ + help.cpp helpers.cpp info.cpp libmpdclient.c lyrics.cpp media_library.cpp \ + menu.cpp misc.cpp mpdpp.cpp ncmpcpp.cpp outputs.cpp playlist.cpp \ + playlist_editor.cpp scrollpad.cpp search_engine.cpp settings.cpp song.cpp status.cpp \ + str_pool.c tag_editor.cpp visualizer.cpp window.cpp # set the include path found by configure INCLUDES= $(all_includes) @@ -11,6 +11,6 @@ INCLUDES= $(all_includes) # the library search path. ncmpcpp_LDFLAGS = $(all_libraries) noinst_HEADERS = browser.h charset.h clock.h conv.h display.h global.h help.h \ - helpers.h home.h info.h lyrics.h media_library.h menu.h mpdpp.h outputs.h \ + helpers.h home.h info.h lyrics.h media_library.h menu.h misc.h mpdpp.h outputs.h \ playlist_editor.h screen.h scrollpad.h search_engine.h settings.h song.h tag_editor.h \ visualizer.h window.h diff --git a/src/misc.cpp b/src/misc.cpp new file mode 100644 index 00000000..ace26710 --- /dev/null +++ b/src/misc.cpp @@ -0,0 +1,190 @@ +/*************************************************************************** + * Copyright (C) 2008-2009 by Andrzej Rybczak * + * electricityispower@gmail.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "charset.h" +#include "browser.h" +#include "display.h" +#include "global.h" +#include "misc.h" +#include "mpdpp.h" +#include "playlist.h" +#include "playlist_editor.h" + +using Global::MainHeight; +using Global::MainStartY; +using Global::myScreen; +using Global::myOldScreen; + +SelectedItemsAdder *mySelectedItemsAdder = new SelectedItemsAdder; + +void SelectedItemsAdder::Init() +{ + SetDimensions(); + w = new Menu((COLS-Width)/2, (MainHeight-Height)/2+MainStartY, Width, Height, "Add selected items to...", Config.main_color, Config.window_border); + w->SetTimeout(ncmpcpp_window_timeout); + w->CyclicScrolling(Config.use_cyclic_scrolling); + w->HighlightColor(Config.main_highlight_color); + w->SetItemDisplayer(Display::Generic); + isInitialized = 1; +} + +void SelectedItemsAdder::SwitchTo() +{ + if (myScreen == this) + { + myOldScreen->SwitchTo(); + return; + } + if (!myScreen->allowsSelection()) + return; + if (!myScreen->GetList()->hasSelected()) + { + ShowMessage("No selected items!"); + return; + } + if (MainHeight < 5) + { + ShowMessage("Screen is too small to display this window!"); + return; + } + + if (!isInitialized) + Init(); + + // Resize() can fall back to old screen, so we need it updated + myOldScreen = myScreen; + + if (hasToBeResized) + Resize(); + + bool playlists_not_active = myScreen == myBrowser && Config.local_browser; + if (playlists_not_active) + ShowMessage("Local items cannot be added to m3u playlist!"); + + w->Clear(); + w->Reset(); + w->AddOption("Current MPD playlist"); + w->AddOption("Create new playlist", 0, playlists_not_active); + w->AddSeparator(); + + MPD::TagList playlists; + Mpd.GetPlaylists(playlists); + for (MPD::TagList::iterator it = playlists.begin(); it != playlists.end(); ++it) + { + utf_to_locale(*it); + w->AddOption(*it, 0, playlists_not_active); + } + w->AddSeparator(); + w->AddOption("Cancel"); + + myScreen = this; + w->Window::Clear(); +} + +void SelectedItemsAdder::Resize() +{ + SetDimensions(); + if (Height < 5) // screen to low to display this window + return myOldScreen->SwitchTo(); + w->Resize(Width, Height); + w->MoveTo((COLS-Width)/2, (MainHeight-Height)/2+MainStartY); + if (myOldScreen && myOldScreen->hasToBeResized) // resize background window + { + myOldScreen->Resize(); + myOldScreen->Refresh(); + } + hasToBeResized = 0; +} + +std::basic_string SelectedItemsAdder::Title() +{ + return myOldScreen->Title(); +} + +void SelectedItemsAdder::EnterPressed() +{ + size_t pos = w->Choice(); + + MPD::SongList list; + if (pos != w->Size()-1) + myOldScreen->GetSelectedSongs(list); + + if (pos == 0) // add to mpd playlist + { + if (myPlaylist->Add(list, 0)) + ShowMessage("Selected items added!"); + } + else if (pos == 1) // create new playlist + { + LockStatusbar(); + Statusbar() << "Save playlist as: "; + std::string playlist = Global::wFooter->GetString(); + UnlockStatusbar(); + if (!playlist.empty()) + { + std::string utf_playlist = locale_to_utf_cpy(playlist); + Mpd.StartCommandsList(); + for (MPD::SongList::const_iterator it = list.begin(); it != list.end(); ++it) + Mpd.AddToPlaylist(utf_playlist, **it); + if (Mpd.CommitCommandsList()) + ShowMessage("Selected items added to playlist \"%s\"!", playlist.c_str()); + } + } + else if (pos > 1 && pos < w->Size()-1) // add items to existing playlist + { + std::string playlist = locale_to_utf_cpy(w->Current()); + Mpd.StartCommandsList(); + for (MPD::SongList::const_iterator it = list.begin(); it != list.end(); ++it) + Mpd.AddToPlaylist(playlist, **it); + if (Mpd.CommitCommandsList()) + ShowMessage("Selected items added to playlist \"%s\"!", w->Current().c_str()); + } + if (pos != w->Size()-1) + { + // refresh playlist's lists + if (!Config.local_browser && myBrowser->Main() && myBrowser->CurrentDir() == "/") + myBrowser->GetDirectory("/"); + if (myPlaylistEditor->Main()) + myPlaylistEditor->Playlists->Clear(0); // make playlist editor update itself + } + MPD::FreeSongList(list); + SwitchTo(); +} + +void SelectedItemsAdder::MouseButtonPressed(MEVENT me) +{ + if (w->Empty() || !w->hasCoords(me.x, me.y) || size_t(me.y) >= w->Size()) + return; + if (me.bstate & (BUTTON1_PRESSED | BUTTON3_PRESSED)) + { + w->Goto(me.y); + if (me.bstate & BUTTON3_PRESSED) + EnterPressed(); + } + else + Screen< Menu >::MouseButtonPressed(me); +} + +void SelectedItemsAdder::SetDimensions() +{ + Width = COLS*0.6; + Height = std::min(size_t(LINES*0.6), MainHeight); +} + diff --git a/src/misc.h b/src/misc.h new file mode 100644 index 00000000..d04cb78b --- /dev/null +++ b/src/misc.h @@ -0,0 +1,56 @@ +/*************************************************************************** + * Copyright (C) 2008-2009 by Andrzej Rybczak * + * electricityispower@gmail.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef _MISC_H +#define _MISC_H + +#include "ncmpcpp.h" +#include "screen.h" + +class SelectedItemsAdder : public Screen< Menu > +{ + public: + virtual void SwitchTo(); + virtual void Resize(); + + virtual std::basic_string Title(); + + virtual void EnterPressed(); + virtual void SpacePressed() { } + virtual void MouseButtonPressed(MEVENT); + + virtual bool allowsSelection() { return false; } + + virtual List *GetList() { return w; } + + protected: + virtual void Init(); + + private: + void SetDimensions(); + + size_t Width; + size_t Height; +}; + +extern SelectedItemsAdder *mySelectedItemsAdder; + +#endif + diff --git a/src/mpdpp.cpp b/src/mpdpp.cpp index cd35a9d4..5c79833a 100644 --- a/src/mpdpp.cpp +++ b/src/mpdpp.cpp @@ -645,7 +645,7 @@ void Connection::StartCommandsList() } -void Connection::CommitCommandsList() +bool Connection::CommitCommandsList() { if (isConnected) { @@ -656,6 +656,7 @@ void Connection::CommitCommandsList() itsErrorHandler(this, MPD_ACK_ERROR_PLAYLIST_MAX, Message::FullPlaylist, NULL); isCommandsListEnabled = 0; } + return !CheckForErrors(); } void Connection::DeletePlaylist(const std::string &name) const diff --git a/src/mpdpp.h b/src/mpdpp.h index 071ab387..93b084e8 100644 --- a/src/mpdpp.h +++ b/src/mpdpp.h @@ -158,7 +158,7 @@ namespace MPD void DeleteID(int) const; void Delete(const std::string &, int) const; void StartCommandsList(); - void CommitCommandsList(); + bool CommitCommandsList(); void DeletePlaylist(const std::string &) const; bool SavePlaylist(const std::string &) const; diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index 419c3c20..2303be0c 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -38,6 +38,7 @@ #include "help.h" #include "helpers.h" #include "media_library.h" +#include "misc.h" #include "lyrics.h" #include "playlist.h" #include "playlist_editor.h" @@ -438,6 +439,7 @@ int main(int argc, char *argv[]) myPlaylistEditor->hasToBeResized = 1; myInfo->hasToBeResized = 1; myLyrics->hasToBeResized = 1; + mySelectedItemsAdder->hasToBeResized = 1; # ifdef HAVE_TAGLIB_H myTinyTagEditor->hasToBeResized = 1; @@ -1545,134 +1547,7 @@ int main(int argc, char *argv[]) } else if (Keypressed(input, Key.AddSelected)) { - if (!myScreen->allowsSelection()) - continue; - - SongList result; - myScreen->GetSelectedSongs(result); - - if (result.empty()) - { - ShowMessage("No selected items!"); - continue; - } - if (MainHeight < 5) - { - ShowMessage("Screen is too small to display this window!"); - continue; - } - - const size_t dialog_width = COLS*0.8; - const size_t dialog_height = std::min(size_t(LINES*0.6), MainHeight); - - Menu mDialog((COLS-dialog_width)/2, (MainHeight-dialog_height)/2+MainStartY, dialog_width, dialog_height, "Add selected items to...", Config.main_color, Config.window_border); - mDialog.SetTimeout(ncmpcpp_window_timeout); - mDialog.CyclicScrolling(Config.use_cyclic_scrolling); - mDialog.SetItemDisplayer(Display::Generic); - - bool playlists_not_active = myScreen == myBrowser && Config.local_browser; - - if (playlists_not_active) - { - ShowMessage("Local items cannot be added to m3u playlist!"); - } - - mDialog.AddOption("Current MPD playlist"); - mDialog.AddOption("Create new playlist", 0, playlists_not_active); - mDialog.AddSeparator(); - TagList playlists; - Mpd.GetPlaylists(playlists); - for (TagList::iterator it = playlists.begin(); it != playlists.end(); ++it) - { - utf_to_locale(*it); - mDialog.AddOption(*it, 0, playlists_not_active); - } - mDialog.AddSeparator(); - mDialog.AddOption("Cancel"); - - mDialog.Display(); - - Playlist::BlockRefreshing = 1; - while (!Keypressed(input, Key.Enter)) - { - TraceMpdStatus(); - mDialog.Refresh(); - mDialog.ReadKey(input); - - if (Keypressed(input, Key.Up)) - mDialog.Scroll(wUp); - else if (Keypressed(input, Key.Down)) - mDialog.Scroll(wDown); - else if (Keypressed(input, Key.PageUp)) - mDialog.Scroll(wPageUp); - else if (Keypressed(input, Key.PageDown)) - mDialog.Scroll(wPageDown); - else if (Keypressed(input, Key.Home)) - mDialog.Scroll(wHome); - else if (Keypressed(input, Key.End)) - mDialog.Scroll(wEnd); - } - Playlist::BlockRefreshing = 0; - - size_t id = mDialog.Choice(); - - myScreen->Refresh(); - - if (id == 0) - { - Mpd.StartCommandsList(); - SongList::const_iterator it = result.begin(); - for (; it != result.end(); ++it) - if (Mpd.AddSong(**it) < 0) - break; - Mpd.CommitCommandsList(); - - if (it != result.begin()) - { - ShowMessage("Selected items added!"); - Song &s = myPlaylist->Main()->at(myPlaylist->Main()->Size()-result.size()); - if (s.GetHash() != result[0]->GetHash()) - ShowMessage("%s", MPD::Message::PartOfSongsAdded); - } - } - else if (id == 1) - { - LockStatusbar(); - Statusbar() << "Save playlist as: "; - std::string playlist = wFooter->GetString(); - std::string real_playlist = playlist; - locale_to_utf(real_playlist); - UnlockStatusbar(); - if (!playlist.empty()) - { - Mpd.StartCommandsList(); - for (SongList::const_iterator it = result.begin(); it != result.end(); ++it) - Mpd.AddToPlaylist(real_playlist, **it); - Mpd.CommitCommandsList(); - ShowMessage("Selected items added to playlist \"%s\"!", playlist.c_str()); - } - } - else if (id > 1 && id < mDialog.Size()-1) - { - std::string playlist = locale_to_utf_cpy(mDialog.Current()); - Mpd.StartCommandsList(); - for (SongList::const_iterator it = result.begin(); it != result.end(); ++it) - Mpd.AddToPlaylist(playlist, **it); - Mpd.CommitCommandsList(); - ShowMessage("Selected items added to playlist \"%s\"!", mDialog.Current().c_str()); - } - - if (id != mDialog.Size()-1) - { - // refresh playlist's lists - if (!Config.local_browser && myBrowser->Main() && myBrowser->CurrentDir() == "/") - myBrowser->GetDirectory("/"); - if (myPlaylistEditor->Main()) - myPlaylistEditor->Playlists->Clear(0); // make playlist editor update itself - } - if (myScreen == myPlaylist) - myPlaylist->EnableHighlighting(); - FreeSongList(result); + mySelectedItemsAdder->SwitchTo(); } else if (Keypressed(input, Key.Crop)) {