add support for built-in mpd searching in search engine
[ncmpcpp.git] / src / mpdpp.h
blob5c3da20a5b93525e2ec31c1b2fb43180cce66701
1 /***************************************************************************
2 * Copyright (C) 2008-2009 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 _MPDPP_H
22 #define _MPDPP_H
24 #include <vector>
26 #include <mpd/client.h>
27 #include "song.h"
29 namespace MPD
31 namespace Message
33 extern const char *PartOfSongsAdded;
34 extern const char *FullPlaylist;
35 extern const char *FunctionDisabledFilteringEnabled;
38 enum ItemType { itDirectory, itSong, itPlaylist };
39 enum PlayerState { psUnknown, psStop, psPlay, psPause };
40 enum ReplayGainMode { rgmOff, rgmTrack, rgmAlbum };
42 struct Item
44 Item() : song(0) { }
45 Song *song;
46 ItemType type;
47 std::string name;
50 struct StatusChanges
52 StatusChanges() : Playlist(0), SongID(0), Database(0), DBUpdating(0), Volume(0), ElapsedTime(0), Crossfade(0), Random(0), Repeat(0), Single(0), Consume(0), PlayerState(0), StatusFlags(0), Outputs(0) { }
53 bool Playlist:1;
54 bool SongID:1;
55 bool Database:1;
56 bool DBUpdating:1;
57 bool Volume:1;
58 bool ElapsedTime:1;
59 bool Crossfade:1;
60 bool Random:1;
61 bool Repeat:1;
62 bool Single:1;
63 bool Consume:1;
64 bool PlayerState:1;
65 bool StatusFlags:1;
66 bool Outputs:1;
69 typedef std::pair<std::string, bool> Output;
71 typedef std::vector<Item> ItemList;
72 typedef std::vector<Song *> SongList;
73 typedef std::vector<std::string> TagList;
74 typedef std::vector<Output> OutputList;
76 void FreeSongList(SongList &);
77 void FreeItemList(ItemList &);
79 class Connection
81 typedef void (*StatusUpdater) (Connection *, StatusChanges, void *);
82 typedef void (*ErrorHandler) (Connection *, int, const char *, void *);
84 public:
85 Connection();
86 ~Connection();
88 bool Connect();
89 bool Connected() const;
90 void Disconnect();
92 const std::string & GetHostname() { return itsHost; }
93 int GetPort() { return itsPort; }
95 float Version() const;
97 void SetIdleEnabled(bool val) { isIdleEnabled = val; }
98 bool SupportsIdle() const { return supportsIdle; }
99 void OrderDataFetching() { hasData = 1; }
100 int GetFD() const { return itsFD; }
102 void SetHostname(const std::string &);
103 void SetPort(int port) { itsPort = port; }
104 void SetTimeout(int timeout) { itsTimeout = timeout; }
105 void SetPassword(const std::string &password) { itsPassword = password; }
106 bool SendPassword();
108 void SetStatusUpdater(StatusUpdater, void *);
109 void SetErrorHandler(ErrorHandler, void *);
110 void UpdateStatus();
111 void UpdateStats();
112 bool UpdateDirectory(const std::string &);
114 void Play();
115 void Play(int);
116 void PlayID(int);
117 void Pause(bool);
118 void Toggle();
119 void Stop();
120 void Next();
121 void Prev();
122 void Move(unsigned, unsigned);
123 void Swap(unsigned, unsigned);
124 void Seek(unsigned);
125 void Shuffle();
126 void ClearPlaylist();
128 bool isPlaying() const { return GetState() > psStop; }
130 PlayerState GetState() const { return itsCurrentStatus ? PlayerState(mpd_status_get_state(itsCurrentStatus)) : psUnknown; }
131 PlayerState GetOldState() const { return itsOldStatus ? PlayerState(mpd_status_get_state(itsOldStatus)) : psUnknown; }
132 bool GetRepeat() const { return itsCurrentStatus ? mpd_status_get_repeat(itsCurrentStatus) : 0; }
133 bool GetRandom() const { return itsCurrentStatus ? mpd_status_get_random(itsCurrentStatus) : 0; }
134 bool GetSingle() const { return itsCurrentStatus ? mpd_status_get_single(itsCurrentStatus) : 0; }
135 bool GetConsume() const { return itsCurrentStatus ? mpd_status_get_consume(itsCurrentStatus) : 0; }
136 bool GetDBIsUpdating() const { return itsCurrentStatus ? mpd_status_get_update_id(itsCurrentStatus) : 0; }
137 int GetVolume() const { return itsCurrentStatus ? mpd_status_get_volume(itsCurrentStatus) : -1; }
138 unsigned GetCrossfade() const { return itsCurrentStatus ? mpd_status_get_crossfade(itsCurrentStatus) : 0; }
139 unsigned GetPlaylistID() const { return itsCurrentStatus ? mpd_status_get_queue_version(itsCurrentStatus) : 0; }
140 unsigned GetOldPlaylistID() const { return itsOldStatus ? mpd_status_get_queue_version(itsOldStatus) : 0; }
141 unsigned GetElapsedTime() const { return itsCurrentStatus ? itsElapsed : 0; }
142 int GetTotalTime() const { return itsCurrentStatus ? mpd_status_get_total_time(itsCurrentStatus) : 0; }
143 unsigned GetBitrate() const { return itsCurrentStatus ? mpd_status_get_kbit_rate(itsCurrentStatus) : 0; }
145 unsigned NumberOfArtists() const { return itsStats ? mpd_stats_get_number_of_artists(itsStats) : 0; }
146 unsigned NumberOfAlbums() const { return itsStats ? mpd_stats_get_number_of_albums(itsStats) : 0; }
147 unsigned NumberOfSongs() const { return itsStats ? mpd_stats_get_number_of_songs(itsStats) : 0; }
148 unsigned long Uptime() const { return itsStats ? mpd_stats_get_uptime(itsStats) : 0; }
149 unsigned long DBUpdateTime() const { return itsStats ? mpd_stats_get_db_update_time(itsStats) : 0; }
150 unsigned long PlayTime() const { return itsStats ? mpd_stats_get_play_time(itsStats) : 0; }
151 unsigned long DBPlayTime() const { return itsStats ? mpd_stats_get_db_play_time(itsStats) : 0; }
153 size_t GetMaxPlaylistLength() const { return itsMaxPlaylistLength; }
154 size_t GetPlaylistLength() const { return itsCurrentStatus ? mpd_status_get_queue_length(itsCurrentStatus) : 0; }
155 void GetPlaylistChanges(unsigned, SongList &);
157 const std::string & GetErrorMessage() const { return itsErrorMessage; }
159 Song GetCurrentSong();
160 int GetCurrentSongPos() const;
161 Song GetSong(const std::string &);
162 void GetPlaylistContent(const std::string &, SongList &);
164 void SetRepeat(bool);
165 void SetRandom(bool);
166 void SetSingle(bool);
167 void SetConsume(bool);
168 void SetCrossfade(unsigned);
169 void SetVolume(unsigned);
171 std::string GetReplayGainMode();
172 void SetReplayGainMode(ReplayGainMode);
174 int AddSong(const std::string &, int = -1); // returns id of added song
175 int AddSong(const Song &, int = -1); // returns id of added song
176 bool AddRandomSongs(size_t);
177 void Add(const std::string &path);
178 bool Delete(unsigned);
179 bool DeleteID(unsigned);
180 bool Delete(const std::string &, unsigned);
181 void StartCommandsList();
182 bool CommitCommandsList();
184 bool DeletePlaylist(const std::string &);
185 bool SavePlaylist(const std::string &);
186 void ClearPlaylist(const std::string &);
187 void AddToPlaylist(const std::string &, const Song &);
188 void AddToPlaylist(const std::string &, const std::string &);
189 void Move(const std::string &, int, int);
190 bool Rename(const std::string &, const std::string &);
192 void StartSearch(bool);
193 void StartFieldSearch(mpd_tag_type);
194 void AddSearch(mpd_tag_type, const std::string &) const;
195 void AddSearchAny(const std::string &str) const;
196 void AddSearchURI(const std::string &str) const;
197 void CommitSearch(SongList &);
198 void CommitSearch(TagList &);
200 void GetPlaylists(TagList &);
201 void GetList(TagList &, mpd_tag_type);
202 void GetDirectory(const std::string &, ItemList &);
203 void GetDirectoryRecursive(const std::string &, SongList &);
204 void GetSongs(const std::string &, SongList &);
205 void GetDirectories(const std::string &, TagList &);
207 void GetOutputs(OutputList &);
208 bool EnableOutput(int);
209 bool DisableOutput(int);
211 void GetURLHandlers(TagList &v);
212 void GetTagTypes(TagList &v);
214 private:
215 void GoIdle();
216 int GoBusy();
218 int CheckForErrors();
220 mpd_connection *itsConnection;
221 bool isCommandsListEnabled;
223 std::string itsErrorMessage;
224 size_t itsMaxPlaylistLength;
226 int itsFD;
227 bool isIdle;
228 bool isIdleEnabled;
229 bool supportsIdle;
230 bool hasData;
232 std::string itsHost;
233 int itsPort;
234 int itsTimeout;
235 std::string itsPassword;
237 mpd_status *itsCurrentStatus;
238 mpd_status *itsOldStatus;
239 mpd_stats *itsStats;
241 unsigned itsElapsed;
242 time_t itsElapsedTimer[2];
244 StatusChanges itsChanges;
246 StatusUpdater itsUpdater;
247 void *itsStatusUpdaterUserdata;
248 ErrorHandler itsErrorHandler;
249 void *itsErrorHandlerUserdata;
251 mpd_tag_type itsSearchedField;
255 extern MPD::Connection Mpd;
257 #endif