1 /***************************************************************************
2  *   Copyright (C) 2008-2021 by Andrzej Rybczak                            *
3  *   andrzej@rybczak.net                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
19  ***************************************************************************/
20 
21 #ifndef NCMPCPP_LYRICS_H
22 #define NCMPCPP_LYRICS_H
23 
24 #include <atomic>
25 #include <boost/optional.hpp>
26 #include <boost/thread/future.hpp>
27 #include <memory>
28 #include <queue>
29 
30 #include "interfaces.h"
31 #include "lyrics_fetcher.h"
32 #include "screens/screen.h"
33 #include "song.h"
34 #include "utility/shared_resource.h"
35 
36 struct Lyrics: Screen<NC::Scrollpad>, Tabbable
37 {
38 	Lyrics();
39 
40 	// Screen<NC::Scrollpad> implementation
41 	virtual void resize() override;
42 	virtual void switchTo() override;
43 
44 	virtual std::wstring title() override;
typeLyrics45 	virtual ScreenType type() override { return ScreenType::Lyrics; }
46 
47 	virtual void update() override;
48 
isLockableLyrics49 	virtual bool isLockable() override { return true; }
isMergableLyrics50 	virtual bool isMergable() override { return true; }
51 
52 	// other members
53 	void fetch(const MPD::Song &s);
54 	void refetchCurrent();
55 	void edit();
56 	void toggleFetcher();
57 
58 	void fetchInBackground(const MPD::Song &s, bool notify_);
59 	boost::optional<std::string> tryTakeConsumerMessage();
60 
61 private:
62 	struct ConsumerState
63 	{
64 		struct Song
65 		{
SongLyrics::ConsumerState::Song66 			Song()
67 				: m_notify(false)
68 			{ }
69 
SongLyrics::ConsumerState::Song70 			Song(const MPD::Song &s, bool notify_)
71 				: m_song(s), m_notify(notify_)
72 			{ }
73 
songLyrics::ConsumerState::Song74 			const MPD::Song &song() const { return m_song; }
notifyLyrics::ConsumerState::Song75 			bool notify() const { return m_notify; }
76 
77 		private:
78 			MPD::Song m_song;
79 			bool m_notify;
80 		};
81 
ConsumerStateLyrics::ConsumerState82 		ConsumerState()
83 			: running(false)
84 		{ }
85 
86 		bool running;
87 		std::queue<Song> songs;
88 		boost::optional<std::string> message;
89 	};
90 
91 	void clearWorker();
92 	void stopDownload();
93 
94 	bool m_refresh_window;
95 	size_t m_scroll_begin;
96 
97 	std::shared_ptr<Shared<NC::Buffer>> m_shared_buffer;
98 	std::shared_ptr<std::atomic<bool>> m_download_stopper;
99 
100 	MPD::Song m_song;
101 	LyricsFetcher *m_fetcher;
102 	boost::BOOST_THREAD_FUTURE<boost::optional<std::string>> m_worker;
103 
104 	Shared<ConsumerState> m_consumer_state;
105 };
106 
107 extern Lyrics *myLyrics;
108 
109 #endif // NCMPCPP_LYRICS_H
110