summaryrefslogtreecommitdiff
path: root/src/search_engine.h
blob: 863e3db5948c1d69ee29781208a110bc0fee52c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/***************************************************************************
 *   Copyright (C) 2008-2014 by Andrzej Rybczak                            *
 *   electricityispower@gmail.com                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 ***************************************************************************/

#ifndef NCMPCPP_SEARCH_ENGINE_H
#define NCMPCPP_SEARCH_ENGINE_H

#include <cassert>

#include "interfaces.h"
#include "mpdpp.h"
#include "regex_filter.h"
#include "screen.h"
#include "song_list.h"

struct SEItem
{
	SEItem() : m_is_song(false), m_buffer(0) { }
	SEItem(NC::Buffer *buf) : m_is_song(false), m_buffer(buf) { }
	SEItem(const MPD::Song &s) : m_is_song(true), m_song(s) { }
	SEItem(const SEItem &ei) { *this = ei; }
	~SEItem() {
		if (!m_is_song)
			delete m_buffer;
	}

	NC::Buffer &mkBuffer() {
		assert(!m_is_song);
		delete m_buffer;
		m_buffer = new NC::Buffer();
		return *m_buffer;
	}

	bool isSong() const { return m_is_song; }

	NC::Buffer &buffer() { assert(!m_is_song && m_buffer); return *m_buffer; }
	MPD::Song &song() { assert(m_is_song); return m_song; }

	const NC::Buffer &buffer() const { assert(!m_is_song && m_buffer); return *m_buffer; }
	const MPD::Song &song() const { assert(m_is_song); return m_song; }

	SEItem &operator=(const SEItem &se) {
		if (this == &se)
			return *this;
		m_is_song = se.m_is_song;
		if (se.m_is_song)
			m_song = se.m_song;
		else if (se.m_buffer)
			m_buffer = new NC::Buffer(*se.m_buffer);
		else
			m_buffer = 0;
		return *this;
	}

private:
	bool m_is_song;

	NC::Buffer *m_buffer;
	MPD::Song m_song;
};

struct SearchEngineWindow: NC::Menu<SEItem>, SongList
{
	SearchEngineWindow() { }
	SearchEngineWindow(NC::Menu<SEItem> &&base)
	: NC::Menu<SEItem>(std::move(base)) { }

	virtual SongIterator currentS() OVERRIDE;
	virtual ConstSongIterator currentS() const OVERRIDE;
	virtual SongIterator beginS() OVERRIDE;
	virtual ConstSongIterator beginS() const OVERRIDE;
	virtual SongIterator endS() OVERRIDE;
	virtual ConstSongIterator endS() const OVERRIDE;

	virtual std::vector<MPD::Song> getSelectedSongs() OVERRIDE;
};

struct SearchEngine: Screen<SearchEngineWindow>, HasActions, HasSongs, Searchable, Tabbable
{
	SearchEngine();
	
	// Screen<SearchEngineWindow> implementation
	virtual void resize() OVERRIDE;
	virtual void switchTo() OVERRIDE;
	
	virtual std::wstring title() OVERRIDE;
	virtual ScreenType type() OVERRIDE { return ScreenType::SearchEngine; }
	
	virtual void update() OVERRIDE { }
	
	virtual void mouseButtonPressed(MEVENT me) OVERRIDE;
	
	virtual bool isLockable() OVERRIDE { return true; }
	virtual bool isMergable() OVERRIDE { return true; }
	
	// Searchable implementation
	virtual bool allowsSearching() OVERRIDE;
	virtual void setSearchConstraint(const std::string &constraint) OVERRIDE;
	virtual void clearConstraint() OVERRIDE;
	virtual bool find(SearchDirection direction, bool wrap, bool skip_current) OVERRIDE;

	// HasActions implementation
	virtual bool actionRunnable() OVERRIDE;
	virtual void runAction() OVERRIDE;

	// HasSongs implementation
	virtual bool itemAvailable() OVERRIDE;
	virtual bool addItemToPlaylist(bool play) OVERRIDE;
	virtual std::vector<MPD::Song> getSelectedSongs() OVERRIDE;
	
	// private members
	void reset();
	
	static size_t StaticOptions;
	static size_t SearchButton;
	static size_t ResetButton;
	
private:
	void Prepare();
	void Search();

	Regex::ItemFilter<SEItem> m_search_predicate;
	
	const char **SearchMode;
	
	static const char *SearchModes[];
	
	static const size_t ConstraintsNumber = 11;
	static const char *ConstraintsNames[];
	std::string itsConstraints[ConstraintsNumber];
	
	static bool MatchToPattern;
};

extern SearchEngine *mySearcher;

#endif // NCMPCPP_SEARCH_ENGINE_H