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_MEDIA_LIBRARY_H
22 #define NCMPCPP_MEDIA_LIBRARY_H
23 
24 #include <boost/date_time/posix_time/posix_time_types.hpp>
25 
26 #include "interfaces.h"
27 #include "regex_filter.h"
28 #include "screens/screen.h"
29 #include "song_list.h"
30 
31 struct MediaLibrary: Screen<NC::Window *>, Filterable, HasColumns, HasSongs, Searchable, Tabbable
32 {
33 	MediaLibrary();
34 
35 	virtual void switchTo() override;
36 	virtual void resize() override;
37 
38 	virtual std::wstring title() override;
typeMediaLibrary39 	virtual ScreenType type() override { return ScreenType::MediaLibrary; }
40 
41 	virtual void refresh() override;
42 	virtual void update() override;
43 
44 	virtual int windowTimeout() override;
45 
46 	virtual void mouseButtonPressed(MEVENT me) override;
47 
isLockableMediaLibrary48 	virtual bool isLockable() override { return true; }
isMergableMediaLibrary49 	virtual bool isMergable() override { return true; }
50 
51 	// Searchable implementation
52 	virtual bool allowsSearching() override;
53 	virtual const std::string &searchConstraint() override;
54 	virtual void setSearchConstraint(const std::string &constraint) override;
55 	virtual void clearSearchConstraint() override;
56 	virtual bool search(SearchDirection direction, bool wrap, bool skip_current) override;
57 
58 	// Filterable implementation
59 	virtual bool allowsFiltering() override;
60 	virtual std::string currentFilter() override;
61 	virtual void applyFilter(const std::string &filter) override;
62 
63 	// HasSongs implementation
64 	virtual bool itemAvailable() override;
65 	virtual bool addItemToPlaylist(bool play) override;
66 	virtual std::vector<MPD::Song> getSelectedSongs() override;
67 
68 	// HasColumns implementation
69 	virtual bool previousColumnAvailable() override;
70 	virtual void previousColumn() override;
71 
72 	virtual bool nextColumnAvailable() override;
73 	virtual void nextColumn() override;
74 
75 	// other members
76 	void updateTimer();
77 	void toggleColumnsMode();
78 	int columns();
79 	void locateSong(const MPD::Song &s);
80 	void toggleSortMode();
81 
requestTagsUpdateMediaLibrary82 	void requestTagsUpdate() { m_tags_update_request = true; }
requestAlbumsUpdateMediaLibrary83 	void requestAlbumsUpdate() { m_albums_update_request = true; }
requestSongsUpdateMediaLibrary84 	void requestSongsUpdate() { m_songs_update_request = true; }
85 
86 	struct PrimaryTag
87 	{
PrimaryTagMediaLibrary::PrimaryTag88 		PrimaryTag() : m_mtime(0) { }
PrimaryTagMediaLibrary::PrimaryTag89 		PrimaryTag(std::string tag_, time_t mtime_)
90 		: m_tag(std::move(tag_)), m_mtime(mtime_) { }
91 
tagMediaLibrary::PrimaryTag92 		const std::string &tag() const { return m_tag; }
mtimeMediaLibrary::PrimaryTag93 		time_t mtime() const { return m_mtime; }
94 
95 	private:
96 		std::string m_tag;
97 		time_t m_mtime;
98 	};
99 
100 	struct Album
101 	{
AlbumMediaLibrary::Album102 		Album(std::string tag_, std::string album_, std::string date_, time_t mtime_)
103 		: m_tag(std::move(tag_)), m_album(std::move(album_))
104 		, m_date(std::move(date_)), m_mtime(mtime_) { }
105 
tagMediaLibrary::Album106 		const std::string &tag() const { return m_tag; }
albumMediaLibrary::Album107 		const std::string &album() const { return m_album; }
dateMediaLibrary::Album108 		const std::string &date() const { return m_date; }
mtimeMediaLibrary::Album109 		time_t mtime() const { return m_mtime; }
110 
111 	private:
112 		std::string m_tag;
113 		std::string m_album;
114 		std::string m_date;
115 		time_t m_mtime;
116 	};
117 
118 	struct AlbumEntry
119 	{
AlbumEntryMediaLibrary::AlbumEntry120 		AlbumEntry() : m_all_tracks_entry(false), m_album("", "", "", 0) { }
AlbumEntryMediaLibrary::AlbumEntry121 		explicit AlbumEntry(Album album_) : m_all_tracks_entry(false), m_album(album_) { }
122 
entryMediaLibrary::AlbumEntry123 		const Album &entry() const { return m_album; }
isAllTracksEntryMediaLibrary::AlbumEntry124 		bool isAllTracksEntry() const { return m_all_tracks_entry; }
125 
mkAllTracksEntryMediaLibrary::AlbumEntry126 		static AlbumEntry mkAllTracksEntry(std::string tag) {
127 			auto result = AlbumEntry(Album(tag, "", "", 0));
128 			result.m_all_tracks_entry = true;
129 			return result;
130 		}
131 
132 	private:
133 		bool m_all_tracks_entry;
134 		Album m_album;
135 	};
136 
137 	NC::Menu<PrimaryTag> Tags;
138 	NC::Menu<AlbumEntry> Albums;
139 	SongMenu Songs;
140 
141 private:
142 	bool m_tags_update_request;
143 	bool m_albums_update_request;
144 	bool m_songs_update_request;
145 
146 	boost::posix_time::ptime m_timer;
147 
148 	const int m_window_timeout;
149 	const boost::posix_time::time_duration m_fetching_delay;
150 
151 	Regex::Filter<PrimaryTag> m_tags_search_predicate;
152 	Regex::ItemFilter<AlbumEntry> m_albums_search_predicate;
153 	Regex::Filter<MPD::Song> m_songs_search_predicate;
154 
155 };
156 
157 extern MediaLibrary *myLibrary;
158 
159 #endif // NCMPCPP_MEDIA_LIBRARY_H
160 
161