1#ifndef CDDBSUPPORT_H
2#define CDDBSUPPORT_H
3
4#include "CDAudioDevice.h"
5
6#include <Entry.h>
7#include <List.h>
8#include <NetEndpoint.h>
9#include <String.h>
10
11#include <scsi.h>
12#include <vector>
13
14using std::vector;
15
16
17class CDDBData {
18	public:
19		CDDBData(int32 discid = -1);
20		CDDBData(const CDDBData &from);
21		~CDDBData();
22		CDDBData& operator=(const CDDBData &from);
23
24		status_t Load(const entry_ref &ref);
25		status_t Save(const char *filename);
26
27		status_t Load();
28		status_t Save();
29
30		void MakeEmpty();
31
32		void SetDiscID(const int32 &id)
33		{
34			fDiscID = id;
35		}
36
37		int32		DiscID() const
38		{
39			return fDiscID;
40		}
41
42		uint16	CountTracks() const;
43		const char*	TrackAt(const int16 &index) const;
44		bool RenameTrack(const int32 &index, const char *newname);
45		void AddTrack(const char *track, const CDAudioTime &time,
46				const int16 &index=-1);
47		void RemoveTrack(const int16 &index);
48
49		CDAudioTime* TrackTimeAt(const int16 &index);
50
51		void SetDiscTime(const CDAudioTime time)
52		{
53			fDiscTime = time;
54		}
55
56		CDAudioTime* DiscTime()
57		{
58			return &fDiscTime;
59		}
60
61		void SetGenre(const char *genre)
62		{
63			fGenre=genre;
64		}
65
66		const char*	Genre()
67		{
68			return fGenre.String();
69		}
70
71		void SetArtist(const char *artist)
72		{
73			fArtist = artist;
74		}
75
76		const char*	Artist() const
77		{
78			return fArtist.String();
79		}
80
81		void SetAlbum(const char *album)
82		{
83			fAlbum = album;
84		}
85
86		const char*	Album() const
87		{
88			return fAlbum.String();
89		}
90
91		void SetYear(const int16 &year)
92		{
93			fYear = year;
94		}
95
96		int16 Year() const
97		{
98			return fYear;
99		}
100
101	private:
102		void		_EmptyLists();
103
104		int32		fDiscID;
105
106		BString		fArtist;
107		BString		fAlbum;
108		BString		fGenre;
109
110		BList		fTrackList;
111		BList		fTimeList;
112
113		CDAudioTime	fDiscTime;
114		int32		fYear;
115};
116
117
118// based on Jukebox by Chip Paul
119
120
121class CDDBQuery {
122	public:
123		CDDBQuery(const char *server, int32 port = 888);
124		~CDDBQuery();
125		void SetToSite(const char *server, int32 port);
126		status_t GetSites(bool (*)(const char *site, int port,
127			const char *latitude, const char *longitude,
128			const char *description, void *state), void *);
129
130		void SetToCD(const char *devicepath);
131
132		const char*	GetArtist()
133		{
134			return fCDData.Artist();
135		}
136
137		const char*	GetAlbum()
138		{
139			return fCDData.Album();
140		}
141
142		const char*	GetGenre()
143		{
144			return fCDData.Genre();
145		}
146
147		bool GetData(CDDBData *data, bigtime_t timeout);
148		void SetData(const CDDBData &data);
149
150		bool Ready() const
151		{
152			return fState == kDone;
153		}
154
155		int32 CurrentDiscID() const
156		{
157			return fCDData.DiscID();
158		}
159
160		static int32 GetDiscID(const scsi_toc *);
161		static int32 GetTrackCount(const scsi_toc *);
162		static CDAudioTime GetDiscTime(const scsi_toc *);
163		static void GetTrackTimes(const scsi_toc *, vector<CDAudioTime> &times);
164		static BString OffsetsToString(const scsi_toc *);
165
166	private:
167		status_t _Connect();
168		void _Disconnect();
169		bool _IsConnected() const;
170
171		static int32 _QueryThread(void *);
172
173		// cached retrieved data
174		enum State {
175			kInitial,
176			kReading,
177			kDone,
178			kInterrupting,
179			kError
180		};
181
182		void _SetDefaultInfo();
183		status_t _OpenContentFile(const int32 &discID);
184
185		status_t _ReadFromServer(BString &data);
186		void _ParseData(const BString &data);
187		void _WriteFile();
188
189		void _ReadLine(BString &);
190		status_t _IdentifySelf();
191
192		const char* _GetToken(const char*, BString &);
193
194		// state data
195		BString			fServerName;
196		BNetEndpoint	fSocket;
197		int32			fPort;
198		bool			fConnected;
199
200		thread_id		fThread;
201		State			fState;
202		status_t		fResult;
203
204		// disc information
205		CDDBData		fCDData;
206		BString			fFrameOffsetString;
207		scsi_toc		fSCSIData;
208};
209
210BString GetLineFromString(const char *string);
211
212#endif	// CDDBSUPPORT_H
213