set SIGWINCH handler before initializing ncurses to avoid races
[ncmpcpp.git] / src / search_engine.h
blobe28df51ed45223a0ddfe403dbcd9101713e97f38
1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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 ***************************************************************************/
21 #ifndef NCMPCPP_SEARCH_ENGINE_H
22 #define NCMPCPP_SEARCH_ENGINE_H
24 #include <cassert>
26 #include "interfaces.h"
27 #include "mpdpp.h"
28 #include "screen.h"
30 struct SearchEngine: Screen<NC::Menu<struct SEItem>>, Filterable, HasSongs, Searchable, Tabbable
32 SearchEngine();
34 // Screen< NC::Menu<SEItem> > implementation
35 virtual void resize() OVERRIDE;
36 virtual void switchTo() OVERRIDE;
38 virtual std::wstring title() OVERRIDE;
39 virtual ScreenType type() OVERRIDE { return ScreenType::SearchEngine; }
41 virtual void update() OVERRIDE { }
43 virtual void enterPressed() OVERRIDE;
44 virtual void spacePressed() OVERRIDE;
45 virtual void mouseButtonPressed(MEVENT me) OVERRIDE;
47 virtual bool isMergable() OVERRIDE { return true; }
49 // Filterable implementation
50 virtual bool allowsFiltering() OVERRIDE;
51 virtual std::string currentFilter() OVERRIDE;
52 virtual void applyFilter(const std::string &filter) OVERRIDE;
54 // Searchable implementation
55 virtual bool allowsSearching() OVERRIDE;
56 virtual bool search(const std::string &constraint) OVERRIDE;
57 virtual void nextFound(bool wrap) OVERRIDE;
58 virtual void prevFound(bool wrap) OVERRIDE;
60 // HasSongs implementation
61 virtual ProxySongList proxySongList() OVERRIDE;
63 virtual bool allowsSelection() OVERRIDE;
64 virtual void reverseSelection() OVERRIDE;
65 virtual MPD::SongList getSelectedSongs() OVERRIDE;
67 // private members
68 void reset();
70 static size_t StaticOptions;
71 static size_t SearchButton;
72 static size_t ResetButton;
74 protected:
75 virtual bool isLockable() OVERRIDE { return true; }
77 private:
78 void Prepare();
79 void Search();
81 const char **SearchMode;
83 static const char *SearchModes[];
85 static const size_t ConstraintsNumber = 11;
86 static const char *ConstraintsNames[];
87 std::string itsConstraints[ConstraintsNumber];
89 static bool MatchToPattern;
92 struct SEItem
94 SEItem() : m_is_song(false), m_buffer(0) { }
95 SEItem(NC::Buffer *buf) : m_is_song(false), m_buffer(buf) { }
96 SEItem(const MPD::Song &s) : m_is_song(true), m_song(s) { }
97 SEItem(const SEItem &ei) { *this = ei; }
98 ~SEItem() {
99 if (!m_is_song)
100 delete m_buffer;
103 NC::Buffer &mkBuffer() {
104 assert(!m_is_song);
105 delete m_buffer;
106 m_buffer = new NC::Buffer();
107 return *m_buffer;
110 bool isSong() const { return m_is_song; }
112 NC::Buffer &buffer() { assert(!m_is_song && m_buffer); return *m_buffer; }
113 MPD::Song &song() { assert(m_is_song); return m_song; }
115 const NC::Buffer &buffer() const { assert(!m_is_song && m_buffer); return *m_buffer; }
116 const MPD::Song &song() const { assert(m_is_song); return m_song; }
118 SEItem &operator=(const SEItem &se) {
119 if (this == &se)
120 return *this;
121 m_is_song = se.m_is_song;
122 if (se.m_is_song)
123 m_song = se.m_song;
124 else if (se.m_buffer)
125 m_buffer = new NC::Buffer(*se.m_buffer);
126 else
127 m_buffer = 0;
128 return *this;
131 private:
132 bool m_is_song;
134 NC::Buffer *m_buffer;
135 MPD::Song m_song;
138 extern SearchEngine *mySearcher;
140 #endif // NCMPCPP_SEARCH_ENGINE_H