From 6d41bc18b1c2e1bfd83f791f4b21703843418eba Mon Sep 17 00:00:00 2001 From: DarkWyrm Date: Tue, 9 Aug 2005 16:12:06 +0000 Subject: [PATCH] Added handling code for when CDDB lookup fails Changed the way logging is set in CDDBQuery Fixed a bug which caused some funky behavior in Shuffle mode Missed adjusting Loop and Shuffle buttons when AdjustButtonState was introduced. Fixed. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13922 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/cdplayer/CDDBSupport.cpp | 65 +++++++++++++++++++++++-------- src/apps/cdplayer/CDDBSupport.h | 7 +--- src/apps/cdplayer/CDEngine.cpp | 30 +++++++++++--- src/apps/cdplayer/CDEngine.h | 5 ++- src/apps/cdplayer/CDPlayer.cpp | 22 ++++++++++- src/apps/cdplayer/PlayList.cpp | 42 +++++++++++++++++++- 6 files changed, 141 insertions(+), 30 deletions(-) diff --git a/src/apps/cdplayer/CDDBSupport.cpp b/src/apps/cdplayer/CDDBSupport.cpp index eff26bcf20..2db1ac84f0 100644 --- a/src/apps/cdplayer/CDDBSupport.cpp +++ b/src/apps/cdplayer/CDDBSupport.cpp @@ -26,6 +26,14 @@ #include #include +//#define DEBUG_CDDB + +#ifdef DEBUG_CDDB +#define STRACE(x) printf x +#else +#define STRACE(x) /* nothing */ +#endif + const int kTerminatingSignal = SIGINT; // SIGCONT; // The SCSI table of contents consists of a 4-byte header followed by 100 track @@ -68,9 +76,8 @@ ThrowIfNotSize(ssize_t size) } -CDDBQuery::CDDBQuery(const char *server, int32 port, bool log) - : fLog(log), - fServerName(server), +CDDBQuery::CDDBQuery(const char *server, int32 port) + : fServerName(server), fPort(port), fConnected(false), fState(kInitial), @@ -177,8 +184,7 @@ CDDBQuery::GetSites(bool (*eachFunc)(const char *site, int port, const char *lat BString tmp; tmp = "sites\n"; - if (fLog) - printf(">%s", tmp.String()); + STRACE((">%s", tmp.String())); ThrowIfNotSize( fSocket.Send(tmp.String(), tmp.Length()) ); ReadLine(tmp); @@ -325,7 +331,7 @@ CDDBQuery::GetTrackTimes(const scsi_toc *toc, vector ×) } } -void +status_t CDDBQuery::ReadFromServer(BString &data) { // This function queries the given CDDB server for the existence of the disc's data and @@ -339,8 +345,7 @@ CDDBQuery::ReadFromServer(BString &data) query << "cddb query " << idString << ' ' << fTrackCount << ' ' << fFrameOffsetString << ' ' << fDiscLength << '\n'; - if (fLog) - printf(">%s", query.String()); + STRACE((">%s", query.String())); ThrowIfNotSize( fSocket.Send(query.String(), query.Length()) ); @@ -378,8 +383,10 @@ CDDBQuery::ReadFromServer(BString &data) } else { - printf("Error: %s\n",tmp.String()); - return; + // We get here for any time the CDDB server does not recognize the CD, amongst other things + STRACE(("CDDB lookup error: %s\n",tmp.String())); + fCategory = "misc"; + return B_NAME_NOT_FOUND; } } else @@ -404,6 +411,8 @@ CDDBQuery::ReadFromServer(BString &data) data << tmp << '\n'; } + + return B_OK; } void @@ -447,8 +456,7 @@ CDDBQuery::ReadLine(BString &buffer) if (ch == '\n') break; } - if (fLog) - printf("<%s\n", buffer.String()); + STRACE(("<%s\n", buffer.String())); } void @@ -465,8 +473,7 @@ CDDBQuery::IdentifySelf() BString tmp; tmp << "cddb hello " << username << " " << hostname << " Haiku_CD_Player v1.0\n"; - if (fLog) - printf(">%s", tmp.String()); + STRACE((">%s", tmp.String())); ThrowIfNotSize( fSocket.Send(tmp.String(), tmp.Length()) ); ReadLine(tmp); @@ -564,6 +571,7 @@ CDDBQuery::QueryThread(void *owner) // new content file, read it in from the server Connector connection(query); BString data; + query->ReadFromServer(data); query->ParseData(data); query->WriteFile(); @@ -594,6 +602,9 @@ CDDBQuery::WriteFile(void) BString filename(path.Path()); filename << "/" << fArtist << " - " << fTitle; + if(filename.Compare("Artist")==0) + filename << "." << fDiscID; + BFile file(filename.String(), B_READ_WRITE | B_CREATE_FILE | B_FAIL_IF_EXISTS); if(file.InitCheck() != B_OK) return; @@ -629,12 +640,34 @@ CDDBQuery::WriteFile(void) void CDDBQuery::ParseData(const BString &data) { + fTitle = ""; + fTrackNames.clear(); + + if(data.CountChars()<1) + { + // This case occurs when the CDDB lookup fails. On these occasions, we need to generate + // the file ourselves. This is actually pretty easy. + fArtist = "Artist"; + fTitle = "Audio CD"; + fCategory = "misc"; + + for(int32 i=0; iValue() == B_CONTROL_ON) engine->StartSkippingForward(); else @@ -303,7 +302,6 @@ CDPlayer::MessageReceived(BMessage *msg) } case M_REWIND: { - // TODO: Implement if(fRewind->Value() == B_CONTROL_ON) engine->StartSkippingBackward(); else @@ -319,11 +317,30 @@ CDPlayer::MessageReceived(BMessage *msg) case M_SHUFFLE: { engine->ToggleShuffle(); + + if(engine->IsShuffled() && fShuffle->GetState()==0) + { + fShuffle->SetState(1); + } + else + { + if(fShuffle->GetState()==1) + fShuffle->SetState(0); + } break; } case M_REPEAT: { engine->ToggleRepeat(); + if(engine->IsRepeated() && fRepeat->GetState()==0) + { + fRepeat->SetState(1); + } + else + { + if(fRepeat->GetState()==1) + fRepeat->SetState(0); + } break; } @@ -363,6 +380,7 @@ CDPlayer::NoticeChange(Notifier *notifier) else if(trs) { + if(fTrackMenu->CountItems() != engine->TrackStateWatcher()->GetNumTracks()) fTrackMenu->SetItemCount(engine->TrackStateWatcher()->GetNumTracks()); diff --git a/src/apps/cdplayer/PlayList.cpp b/src/apps/cdplayer/PlayList.cpp index 9930bbf08a..d578d5eeaa 100644 --- a/src/apps/cdplayer/PlayList.cpp +++ b/src/apps/cdplayer/PlayList.cpp @@ -3,6 +3,15 @@ #include #include +#define DEBUG_PLAYLIST + +#ifdef DEBUG_PLAYLIST +#include +#define STRACE(x) printf x +#else +#define STRACE(x) /* nothing */ +#endif + PlayList::PlayList(int16 count, int16 start) : fTrackCount(count), fTrackIndex(0), @@ -10,6 +19,8 @@ PlayList::PlayList(int16 count, int16 start) fRandom(false), fLoop(false) { + STRACE(("PlayList(count=%d,start=%d)\n",count,start)); + srand(real_time_clock_usecs()); if(fTrackCount < 0) @@ -25,6 +36,7 @@ PlayList::PlayList(int16 count, int16 start) fStartingTrack = 1; memset(fTrackList,-1,500); + Unrandomize(); } @@ -33,6 +45,8 @@ PlayList::SetTrackCount(const int16 &count) { fLocker.Lock(); + STRACE(("PlayList::SetTrackCount(%d)\n",count)); + if(count < 0) fTrackCount = 0; else @@ -52,6 +66,8 @@ PlayList::SetStartingTrack(const int16 &start) { fLocker.Lock(); + STRACE(("PlayList::SetStartingTrack(%d)\n",start)); + if(start >= TrackCount()) fStartingTrack = TrackCount() - 1; else @@ -66,6 +82,7 @@ PlayList::SetStartingTrack(const int16 &start) void PlayList::Rewind(void) { + STRACE(("PlayList::Rewind()\n")); fLocker.Lock(); fTrackIndex = 0; @@ -76,6 +93,7 @@ PlayList::Rewind(void) void PlayList::SetShuffle(const bool &random) { + STRACE(("PlayList::SetShuffle(%s)\n",random ? "random" : "sequential")); fLocker.Lock(); if(random) @@ -91,6 +109,7 @@ PlayList::SetShuffle(const bool &random) void PlayList::SetLoop(const bool &loop) { + STRACE(("PlayList::SetLoop(%s)\n",loop ? "loop" : "non-loop")); fLocker.Lock(); fLoop = loop; @@ -101,6 +120,8 @@ PlayList::SetLoop(const bool &loop) void PlayList::SetCurrentTrack(const int16 &track) { + STRACE(("PlayList::SetCurrentTrack(%d)\n",track)); + if(track < 0 || track > fTrackCount) return; @@ -124,6 +145,7 @@ PlayList::GetCurrentTrack(void) fLocker.Lock(); int16 value = fTrackList[fTrackIndex]; + STRACE(("PlayList::GetCurrentTrack()=%d\n",value)); fLocker.Unlock(); return value; @@ -137,6 +159,7 @@ PlayList::GetNextTrack(void) if(fTrackCount < 1) { fLocker.Unlock(); + STRACE(("PlayList::GetNextTrack()=-1 (no tracks)\n")); return -1; } @@ -147,6 +170,7 @@ PlayList::GetNextTrack(void) else { fLocker.Unlock(); + STRACE(("PlayList::GetNextTrack()=-1 (track index out of range)\n")); return -1; } } @@ -154,6 +178,8 @@ PlayList::GetNextTrack(void) fTrackIndex++; int16 value = fTrackList[fTrackIndex]; + STRACE(("PlayList::GetNextTrack()=%d\n",value)); + fLocker.Unlock(); return value; } @@ -166,6 +192,7 @@ PlayList::GetPreviousTrack(void) if(fTrackCount < 1) { fLocker.Unlock(); + STRACE(("PlayList::GetPreviousTrack()=-1 (no tracks)\n")); return -1; } @@ -176,6 +203,7 @@ PlayList::GetPreviousTrack(void) else { fLocker.Unlock(); + STRACE(("PlayList::GetPreviousTrack()=-1 (track index out of range)\n")); return -1; } } @@ -183,6 +211,7 @@ PlayList::GetPreviousTrack(void) fTrackIndex--; int16 value = fTrackList[fTrackIndex]; + STRACE(("PlayList::GetPreviousTrack()=%d\n",value)); fLocker.Unlock(); return value; } @@ -195,11 +224,13 @@ PlayList::GetLastTrack(void) if(fTrackCount < 1) { fLocker.Unlock(); + STRACE(("PlayList::GetLastTrack()=-1 (no tracks)\n")); return -1; } fTrackIndex = fTrackCount - 1; int16 value = fTrackList[fTrackIndex]; + STRACE(("PlayList::GetLastTrack()=%d\n",value)); fLocker.Unlock(); return value; } @@ -212,12 +243,13 @@ PlayList::GetFirstTrack(void) if(fTrackCount < 1) { fLocker.Unlock(); + STRACE(("PlayList::GetFirstTrack()=-1 (no tracks)\n")); return -1; } fTrackIndex = 0; int16 value = fTrackList[fTrackIndex]; - + STRACE(("PlayList::GetFirstTrack()=%d\n",value)); fLocker.Unlock(); return value; } @@ -225,6 +257,8 @@ PlayList::GetFirstTrack(void) void PlayList::Randomize(void) { + STRACE(("PlayList::Randomize()\n")); + // Reinitialize the count for(int16 i=fStartingTrack; i<=fTrackCount; i++) fTrackList[i - fStartingTrack] = i; @@ -249,11 +283,17 @@ PlayList::Randomize(void) fTrackList[first] = fTrackList[second]; fTrackList[second] = temp; } + + #ifdef DEBUG_PLAYLIST + for(int16 i=fStartingTrack; i<=fTrackCount; i++) + printf("\tSlot %d: track %d\n",i,fTrackList[i]); + #endif } void PlayList::Unrandomize(void) { + STRACE(("PlayList::Unrandomize()\n")); for(int16 i=fStartingTrack; i<=fTrackCount; i++) fTrackList[i - fStartingTrack] = i; }