new feature: add random songs to playlist

This commit is contained in:
Andrzej Rybczak
2009-05-24 22:30:02 +02:00
parent 5f5d6bf03f
commit dd266b0103
6 changed files with 59 additions and 1 deletions

View File

@@ -191,6 +191,7 @@ void Help::GetKeybindings()
*w << " " << fmtBold << "Keys - Playlist screen\n -----------------------------------------\n" << fmtBoldEnd;
*w << DisplayKeys(Key.Enter) << "Play\n";
*w << DisplayKeys(Key.SwitchTagTypeList) << "Add random songs to playlist\n";
*w << DisplayKeys(Key.Delete) << "Delete item/selected items from playlist\n";
*w << DisplayKeys(Key.Clear) << "Clear playlist\n";
*w << DisplayKeys(Key.Crop) << "Clear playlist but hold currently playing/selected items\n";

View File

@@ -33,6 +33,11 @@ int StrToInt(const std::string &str)
return atoi(str.c_str());
}
long StrToLong(const std::string &str)
{
return atol(str.c_str());
}
std::string IntoStr(int l)
{
std::ostringstream ss;

View File

@@ -29,6 +29,7 @@
void ToLower(std::string &);
int StrToInt(const std::string &);
long StrToLong(const std::string &);
std::string IntoStr(int);

View File

@@ -18,6 +18,9 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <cstdlib>
#include <algorithm>
#include "charset.h"
#include "mpdpp.h"
@@ -560,6 +563,40 @@ int Connection::AddSong(const Song &s)
return !s.Empty() ? (AddSong((!s.IsFromDB() ? "file://" : "") + (s.Localized() ? locale_to_utf_cpy(s.GetFile()) : s.GetFile()))) : -1;
}
bool Connection::AddRandomSongs(size_t number)
{
if (!isConnected && !number)
return false;
TagList files;
mpd_sendListallCommand(itsConnection, "/");
while (char *file = mpd_getNextTag(itsConnection, MPD_TAG_ITEM_FILENAME))
{
files.push_back(file);
delete [] file;
}
mpd_finishCommand(itsConnection);
if (number > files.size())
{
if (itsErrorHandler)
itsErrorHandler(this, 0, "Requested number of random songs is bigger than size of your library!", itsErrorHandlerUserdata);
return false;
}
else
{
srand(time(0));
std::random_shuffle(files.begin(), files.end());
StartCommandsList();
TagList::const_iterator it = files.begin()+rand()%(files.size()-number);
for (size_t i = 0; i < number && it != files.end(); i++)
AddSong(*it++);
CommitCommandsList();
}
return true;
}
void Connection::Delete(int pos) const
{
if (isConnected)

View File

@@ -148,6 +148,7 @@ namespace MPD
int AddSong(const std::string &); // returns id of added song
int AddSong(const Song &); // returns id of added song
bool AddRandomSongs(size_t);
void Delete(int) const;
void DeleteID(int) const;
void Delete(const std::string &, int) const;

View File

@@ -309,6 +309,10 @@ int main(int argc, char *argv[])
{
myScreen->Scroll(wUp, Key.Up);
}
else if (input == 'v')
{
Mpd->AddRandomSongs(10);
}
else if (Keypressed(input, Key.Down))
{
myScreen->Scroll(wDown, Key.Down);
@@ -1716,7 +1720,16 @@ int main(int argc, char *argv[])
}
else if (Keypressed(input, Key.SwitchTagTypeList))
{
if (myScreen == myBrowser)
if (myScreen == myPlaylist)
{
LockStatusbar();
Statusbar() << "Number of random songs: ";
size_t number = StrToLong(wFooter->GetString());
UnlockStatusbar();
if (number && Mpd->AddRandomSongs(number))
ShowMessage("%lu random song%s added to playlist!", number, number == 1 ? "" : "s");
}
else if (myScreen == myBrowser)
{
myBrowser->ChangeBrowseMode();
}