void Lyrics::DownloadInBackground(const MPD::Song &s) { if (s.empty() || s.getArtist().empty() || s.getTitle().empty()) return; std::string filename = GenerateFilename(s); std::ifstream f(filename.c_str()); if (f.is_open()) { f.close(); return; } Statusbar::msg("Fetching lyrics for \"%s\"...", s.toString(Config.song_status_format_no_colors, Config.tags_separator).c_str()); MPD::Song *s_copy = new MPD::Song(s); pthread_mutex_lock(&itsDIBLock); if (itsWorkersNumber == itsMaxWorkersNumber) itsToDownload.push(s_copy); else { ++itsWorkersNumber; pthread_t t; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_create(&t, &attr, DownloadInBackgroundImpl, s_copy); } pthread_mutex_unlock(&itsDIBLock); }
void Lyrics::update() { # ifdef HAVE_CURL_CURL_H if (isReadyToTake) Take(); if (isDownloadInProgress) { w.flush(); w.refresh(); } # endif // HAVE_CURL_CURL_H if (ReloadNP) { const MPD::Song s = myPlaylist->nowPlayingSong(); if (!s.empty() && !s.getArtist().empty() && !s.getTitle().empty()) { drawHeader(); itsScrollBegin = 0; itsSong = s; Load(); } ReloadNP = 0; } }
void Lyrics::fetchInBackground(const MPD::Song &s) { auto consumer = [this] { std::string lyrics_file; while (true) { MPD::Song qs; { auto queue = m_shared_queue.acquire(); assert(queue->first); if (queue->second.empty()) { queue->first = false; break; } lyrics_file = lyricsFilename(queue->second.front()); if (!boost::filesystem::exists(lyrics_file)) qs = queue->second.front(); queue->second.pop(); } if (!qs.empty()) { auto lyrics = downloadLyrics(qs, nullptr, m_fetcher); if (lyrics) saveLyrics(lyrics_file, *lyrics); } } }; auto queue = m_shared_queue.acquire(); queue->second.push(s); // Start the consumer if it's not running. if (!queue->first) { std::thread t(consumer); t.detach(); queue->first = true; } }