1 /*
2  * Project: VizKit
3  * Version: 2.3
4 
5  * Date: 20090823
6  * File: VisualDataStore.h
7  *
8  */
9 
10 /***************************************************************************
11 
12 Copyright (c) 2004-2009 Heiko Wichmann (http://www.imagomat.de/vizkit)
13 
14 
15 This software is provided 'as-is', without any expressed or implied warranty.
16 In no event will the authors be held liable for any damages
17 arising from the use of this software.
18 
19 Permission is granted to anyone to use this software for any purpose,
20 including commercial applications, and to alter it and redistribute it
21 freely, subject to the following restrictions:
22 
23 1. The origin of this software must not be misrepresented;
24    you must not claim that you wrote the original software.
25    If you use this software in a product, an acknowledgment
26    in the product documentation would be appreciated
27    but is not required.
28 
29 2. Altered source versions must be plainly marked as such,
30    and must not be misrepresented as being the original software.
31 
32 3. This notice may not be removed or altered from any source distribution.
33 
34  ***************************************************************************/
35 
36 #ifndef VisualDataStore_h
37 #define VisualDataStore_h
38 
39 
40 #include "VisualTypes.h"
41 #include "VisualConfiguration.h"
42 #include "VisualItemIdentifier.h"
43 #include "VisualAudioMetaData.h"
44 
45 
46 #if TARGET_OS_WIN
47 #include <windows.h> // DWORD WINAPI
48 #endif
49 
50 #include <vector>
51 #include <string>
52 #include <map>
53 
54 
55 struct ITTrackInfo; // Forward declaration (to avoid include of header file).
56 struct ITStreamInfo; // Forward declaration (to avoid include of header file).
57 
58 namespace VizKit {
59 
60 	class VisualString; // Forward declaration (to avoid include of header file).
61 	class VisualImage; // Forward declaration (to avoid include of header file).
62 
63 	/**
64 	 * Stores essential data for the visualizer.
65 	 * The data stored is general (neither graphics related nor audio specific).
66 	 * All public methods are declared static.
67 	 */
68 	class VisualDataStore {
69 
70 	public:
71 
72 		typedef enum {
73 			kUnknownValue = 0,
74 			kTrackInfoTextureHeight,
75 			kLyricsTextureHeight
76 		} ValueKey;
77 
78 		/**
79 		 * Disposes the VisualDataStore.
80 		 */
81 		static void dispose(void);
82 
83 		/**
84 		 * Sets a persistent value (integer) which is not stored in preferences. The value is accessible during runtime.
85 		 * @param anIdentifier The identifier (key) of the preference value.
86 		 * @param anIntValue The value (integer) of the value.
87 		 */
88 		static void setValue(const ValueKey anIdentifier, const int anIntValue);
89 
90 		/**
91 		 * Sets a persistent value (float) which is not stored in preferences. The value is accessible during runtime.
92 		 * @param anIdentifier The identifier (key) of the preference value.
93 		 * @param aFloatValue The value (float) of the value.
94 		 */
95 		static void setValue(const ValueKey anIdentifier, const float aFloatValue);
96 
97 		/**
98 		 * Sets a persistent value (string) which is not stored in preferences. The value is accessible during runtime.
99 		 * @param anIdentifier The identifier (key) of the preference value.
100 		 * @param aCharValue The value (string) of the value.
101 		 */
102 		static void setValue(const ValueKey anIdentifier, const char* const aCharValue);
103 
104 		/**
105 		 * Sets a persistent value (boolean) which is not stored in preferences. The value is accessible during runtime.
106 		 * @param anIdentifier The identifier (key) of the preference value.
107 		 * @param aBoolValue The value (boolean) of the value.
108 		 */
109 		static void setValue(const ValueKey anIdentifier, const bool aBoolValue);
110 
111 		/**
112 		 * Retrieves a persistent value (integer) which was set previously but not stored in preferences.
113 		 * @param anIdentifier The identifier (key) of the preference value.
114 		 * @param wasSet Optional parameter that indicates whether the values was set previously.
115 		 * @return Returns a persistent value (integer) which was set previously but not stored in preferences.
116 		 */
117 		static int getValueInt(const ValueKey anIdentifier, bool* wasSet = NULL);
118 
119 		/**
120 		 * Retrieves a persistent value (float) which was set previously but not stored in preferences.
121 		 * @param anIdentifier The identifier (key) of the preference value.
122 		 * @param wasSet Optional parameter that indicates whether the values was set previously.
123 		 * @return Returns a persistent value (float) which was set previously but not stored in preferences.
124 		 */
125 		static float getValueFloat(const ValueKey anIdentifier, bool* wasSet = NULL);
126 
127 		/**
128 		 * Retrieves a persistent value (string) which was set previously but not stored in preferences.
129 		 * @param anIdentifier The identifier (key) of the preference value.
130 		 * @param[out] outPrefVal The character string value.
131 		 * @param wasSet Optional parameter that indicates whether the values was set previously.
132 		 * @return Returns a persistent value (string) which was set previously but not stored in preferences.
133 		 */
134 		static void getValueChar(const ValueKey anIdentifier, char* outPrefVal, bool* wasSet = NULL);
135 
136 		/**
137 		 * Retrieves a persistent value (boolean) which was set previously but not stored in preferences.
138 		 * @param anIdentifier The identifier (key) of the preference value.
139 		 * @param wasSet Optional parameter that indicates whether the values was set previously.
140 		 * @return Returns a persistent value (boolean) which was set previously but not stored in preferences.
141 		 */
142 		static bool getValueBool(const ValueKey anIdentifier, bool* wasSet = NULL);
143 
144 		/**
145 		 * Stores the major and minor version of the host application.
146 		 * @param majorVersionNum The major version number of the host application. 1st part of version number in BCD.
147 		 * @param minorVersionNum The minor version number of the host application. Minor and bug revision part of version number share a byte.
148 		 */
149 		static void setAppVersionNum(uint8 majorVersionNum, uint8 minorVersionNum);
150 
151 		/**
152 		 * Returns the major version of the host application.
153 		 * @return The major version number of the host application.
154 		 */
155 		static uint8 getAppVersionMajorNum(void);
156 
157 		/**
158 		 * Returns the minor version of the host application.
159 		 * @return The minor version number of the host application.
160 		 */
161 		static uint8 getAppVersionMinorNum(void);
162 
163 		/**
164 		 * Resets the currently set audio meta data.
165 		 */
166 		static void resetCurrAudioMetaData(void);
167 
168 		/**
169 		 * Sets the audio track name.
170 		 * @param audioTrackName The name of the current audio track.
171 		 * @param audioTrackNameLength The number of characters of the name of the current audio track.
172 		 */
173 		static void setAudioTrackName(const uint16* const audioTrackName, uint32 audioTrackNameLength);
174 
175 		/**
176 		 * Sets the audio track artist name.
177 		 * @param audioTrackArtistName The name of the artist of the current audio track.
178 		 * @param audioTrackArtistNameLength The number of characters of the name of the artist of the current audio track.
179 		 */
180 		static void setAudioTrackArtistName(const uint16* const audioTrackArtistName, uint32 audioTrackArtistNameLength);
181 
182 		/**
183 		 * Sets the audio track album name.
184 		 * @param audioTrackAlbumName The name of the album of the current audio track.
185 		 * @param audioTrackAlbumNameLength The number of characters of the name of the album of the current audio track.
186 		 */
187 		static void setAudioTrackAlbumName(const uint16* const audioTrackAlbumName, uint32 audioTrackAlbumNameLength);
188 
189 		/**
190 		 * Sets the audio track composer.
191 		 * @param audioTrackComposer The name of the composer of the current audio track.
192 		 * @param audioTrackComposerLength The number of characters of the name of the composer of the current audio track.
193 		 */
194 		static void setAudioTrackComposer(const uint16* const audioTrackComposer, uint32 audioTrackComposerLength);
195 
196 		/**
197 		 * Sets the title of an audio stream.
198 		 * @param audioStreamTitle The title of an audio stream.
199 		 * @param audioStreamTitleLength The number of characters of title of an audio stream.
200 		 */
201 		static void setAudioStreamTitle(const char* const audioStreamTitle, uint32 audioStreamTitleLength);
202 
203 		/**
204 		 * Sets the title of an audio stream.
205 		 * @param audioStreamTitle The title of an audio stream.
206 		 * @param audioStreamTitleLength The number of characters of title of an audio stream.
207 		 */
208 		static void setAudioStreamTitle(const uint16* const audioStreamTitle, uint32 audioStreamTitleLength);
209 
210 		/**
211 		 * Sets the message of an audio stream.
212 		 * @param audioStreamMessage The title of an audio stream.
213 		 * @param audioStreamMessageLength The number of characters of the message of an audio stream.
214 		 */
215 		static void setAudioStreamMessage(const char* const audioStreamMessage, uint32 audioStreamMessageLength);
216 
217 		/**
218 		 * Sets the message of an audio stream.
219 		 * @param audioStreamMessage The message of an audio stream.
220 		 * @param audioStreamMessageLength The number of characters of the message of an audio stream.
221 		 */
222 		static void setAudioStreamMessage(const uint16* const audioStreamMessage, uint32 audioStreamMessageLength);
223 
224 		/**
225 		 * Sets the URL of an audio stream.
226 		 * @param audioStreamURL The URL of an audio stream.
227 		 * @param audioStreamURLLength The number of characters of the URL of an audio stream.
228 		 */
229 		static void setAudioStreamURL(const char* const audioStreamURL, uint32 audioStreamURLLength);
230 
231 		/**
232 		 * Sets the URL of an audio stream.
233 		 * @param audioStreamURL The URL of an audio stream.
234 		 * @param audioStreamURLLength The number of characters of the URL of an audio stream.
235 		 */
236 		static void setAudioStreamURL(const uint16* const audioStreamURL, uint32 audioStreamURLLength);
237 
238 		/**
239 		 * Sets the size in bytes of the audio track.
240 		 * @param sizeInBytes The size in bytes of the audio track.
241 		 */
242 		static void setAudioTrackSizeInBytes(int sizeInBytes);
243 
244 		/**
245 		 * Sets the year of the audio track.
246 		 * @param aYear The year of the audio track.
247 		 */
248 		static void setAudioTrackYear(uint16 aYear);
249 
250 		/**
251 		 * Sets whether the current audio data is a stream.
252 		 */
253 		static void setAudioDataIsStream(bool isStream);
254 
255 		/**
256 		 * Stores the lyrics of the current audio track.
257 		 * @param lyricsString The lyrics of the current audio track.
258 		 */
259 		static void setLyricsOfCurrentTrack(const VisualString& lyricsString);
260 
261 		/**
262 		 * Analyzes the currently set audio track meta data.
263 		 * @return True if the track information changed (meaning: a new audio track started).
264 		 */
265 		static bool analyzeCurrentlySetMetadata(void);
266 
267 		/**
268 		 * Returns a string with the info for the current audio data which can be used for display.
269 		 * @return String with the info for the current audio data.
270 		 */
271 		static VisualString getInfoOfCurrentAudioDataForDisplay(void);
272 
273 		/**
274 		 * Returns the number of bytes of the current track.
275 		 * @return The number of bytes of the current track.
276 		 */
277 		static uint32 getTrackSizeInBytes(void);
278 
279 		/**
280 		 * Returns the year of the current track.
281 		 * @return The year of the current track.
282 		 */
283 		static uint16 getTrackYear(void);
284 
285 		/**
286 		 * Returns the name of the current audio track.
287 		 * @return The name of current track.
288 		 */
289 		static VisualString getNameOfCurrentTrack(void);
290 
291 		/**
292 		 * Returns the name of the artist of the current audio track.
293 		 * @return The name of artist of the current track.
294 		 */
295 		static VisualString getArtistOfCurrentTrack(void);
296 
297 		/**
298 		 * Returns the name of the album of the current audio track.
299 		 * @return The name of album of the current track.
300 		 */
301 		static VisualString getAlbumOfCurrentTrack(void);
302 
303 		/* Returns the lyrics of the current audio track.
304 		 * After new lyrics for the current track are available, a notification is sent (kLyricsAreAvailableInMetadataMsg).
305 		 * Clients that are interested in being informed about the availability of new lyrics for the current track should register for the notification kLyricsAreAvailableInMetadataMsg.
306 		 * @return The lyrics of the current track.
307 		*/
308 		static VisualString getLyricsOfCurrentTrack(void);
309 
310 		/* Returns the identifier of the current audio track.
311 		 * @return The identifier of the current audio track.
312 		*/
313 		static VisualItemIdentifier getIdentifierOfCurrentTrack(void);
314 
315 		/**
316 		 * Answers the question whether currently playing audio is stream or track.
317 		 * @return True if currently playing audio is stream. False if currently playing audio is a track.
318 		 */
319 		static bool currentlyPlayingAudioIsStream(void);
320 
321 		/**
322 		 * Creates an image of the album cover artwork of the current audio track.
323 		 * @return The image of the album cover artwork. Returns NULL if cover image is not available.
324 		 * @remarks The caller has to release the allocated memory by calling delete().
325 		 */
326 		static VisualImage* createCoverArtImage(void);
327 
328 		/* The lyrics are created.
329 		 * @return True on success, false on failure.
330 		 * @remarks The lyrics are created in a separate thread.
331 		 */
332 		static bool createLyricsOfCurrentTrack(void);
333 
334 		/* A separate thread in which lyrics are written into a file on disk.
335 		 * @param parameter Additional parameters (ignored).
336 		 * @return Status.
337 		 * @remarks The target file into which the lyrics were written is added to the notification queue.
338 		 */
339 #if TARGET_OS_MAC
340 		static OSStatus writeLyricsToFile(void* parameter);
341 #endif
342 #if TARGET_OS_WIN
343 		static DWORD WINAPI writeLyricsToFile(LPVOID lpParam);
344 #endif
345 
346 		/* Contents of a file are read and the string contents are sent as notification.
347 		 * @param fileWithStringContent File on disk with string contents.
348 		 * @return Status.
349 		 */
350 #if TARGET_OS_MAC
351 		static OSStatus readFileAndSendNotificationWithString(void* fileWithStringContent);
352 #endif
353 #if TARGET_OS_WIN
354 		static DWORD readFileAndSendNotificationWithString(LPVOID fileWithStringContent);
355 #endif
356 
357 		/* Contents of a file are read, the file is deleted, and the string contents are sent as notification.
358 		 * @param fileWithStringContent File on disk with string contents.
359 		 * @return Status.
360 		 */
361 #if TARGET_OS_MAC
362 		static OSStatus readFileAndRemoveFileAndSendNotificationWithString(void* fileWithStringContent);
363 #endif
364 #if TARGET_OS_WIN
365 		static DWORD readFileAndRemoveFileAndSendNotificationWithString(LPVOID fileWithStringContent);
366 #endif
367 		/**
368 		 * Stores label and value of a process info entry that is sent later to the VisualProcessMonitor.
369 		 * @param labelStr The string of the label.
370 		 * @param valueStr The value of the process info.
371 		 */
372 		static void setProcessInfo(const char* const labelStr, const char* const valueStr);
373 
374 		/**
375 		 * Returns a pointer to the process info map.
376 		 * @return The process info map.
377 		 */
378 		static const std::map<std::string, std::string>* const getProcessInfoMap();
379 
380 		/**
381 		 * The identifier of the image of the album cover artwork.
382 		 */
383 		static const VisualItemIdentifier albumCoverArtworkImageId;
384 
385 		/**
386 		 * The identifier of the file with lyrics string data.
387 		 */
388 		static VisualItemIdentifier fileWithLyricsStringId;
389 
390 		/**
391 		 * The identifier of the track for which the lyrics data is gathered.
392 		 */
393 		static VisualItemIdentifier trackIdentifierOfLyricsMetadata;
394 
395 		/**
396 		 * The identifier of the styled lyrics string.
397 		 */
398 		static const VisualItemIdentifier styledTrackLyricsStringId;
399 
400 		/**
401 		 * The identifier of the styled track info string.
402 		 */
403 		static const VisualItemIdentifier styledTrackInfoStringId;
404 
405 		/**
406 		 * The identifier of the update information string.
407 		 */
408 		static const VisualItemIdentifier updateInformationStringId;
409 
410 	private:
411 
412 		/** The constructor. VisualDataStore is a singleton class. The constructor is private. New instance of class can only be created internally. */
413 		VisualDataStore();
414 
415 		/** The destructor. VisualDataStore is a singleton class. The destructor is private. Instance of class can only be destructed internally. */
416 		~VisualDataStore();
417 
418 		/**
419 		 * Constructs a VisualDataStore. The VisualDataStore internally is a singleton. Returns a pointer to the initialized VisualDataStore.
420 		 * @return A pointer to the singleton instance.
421 		 */
422 		static VisualDataStore* getInstance(void);
423 
424 		/**
425 		 * Copy constructor.
426 		 * @param other Another VisualDataStore.
427 		 * @remarks Explicitely declared in private section and not implemented to enforce uniqueness of singleton pattern.
428 		 */
429 		VisualDataStore(const VisualDataStore& other);
430 
431 		/**
432 		 * Assignment operator.
433 		 * @remarks Explicitely declared in private section and not implemented to enforce uniqueness of singleton pattern.
434 		 */
435 		VisualDataStore& operator=(const VisualDataStore& other);
436 
437 		/** VisualDataStore is a singleton class. Pointer to private instance is handled internally. */
438 		static VisualDataStore* theVisualDataStore;
439 
440 		/** The format of the data that is stored with the values. */
441 		typedef enum {
442 			kIntPrefType = 0, /**< Integer type. */
443 			kCharPrefType, /**< 8-bit character type. */
444 			kFloatPrefType, /**< Float type. */
445 			kBoolPrefType /**< Float type. */
446 		} ValueDataType;
447 
448 		/** Stores a key-value-pair of values. The type of data of the value is set with the field dataType. */
449 		class Value {
450 		public:
451 			/** The key of the value (e.g. kTrackInfoTextureHeight). */
452 			ValueKey key;
453 			/** The integer value of the value (e.g. 32). */
454 			int valueInt;
455 			/** The float value of the value (e.g. 1.35). */
456 			float valueFloat;
457 			/** The char value of the value (e.g. "LucidaGrande"). */
458 			char valueChar[256];
459 			/** The boolean value of the value (true or false). */
460 			bool valueBool;
461 			/** The format of the value data. */
462 			ValueDataType dataType;
463 		};
464 
465 		/** Values are collected as a vector of pointers to Values. */
466 		typedef std::vector<Value*> ValueVector;
467 
468 		/** The ValueVectorIterator is an iterator of the ValueVector. */
469 		typedef ValueVector::iterator ValueVectorIterator;
470 
471 		/** Vector of values. */
472 		ValueVector valueVector;
473 
474 		/** The major version number of the host application iTunes. */
475 		uint8 appVersionMajorNum;
476 
477 		/** The minor version number of the host application iTunes. */
478 		uint8 appVersionMinorNum;
479 
480 		/** The number of entries in history of meta data of audio track or stream. */
481 		static const size_t audioMetaDataHistoryCount;
482 
483 		/** The current index of the history of meta data of audio track or stream. */
484 		size_t currAudioMetaDataHistoryIdx;
485 
486 		/** AudioMetaData entries are collected as a vector of pointers to AudioMetaData. */
487 		typedef std::vector<VisualAudioMetaData> AudioMetaDataVector;
488 
489 		/** The AudioMetaDataVectorIterator is an iterator of the AudioMetaDataVector. */
490 		typedef AudioMetaDataVector::iterator AudioMetaDataVectorIterator;
491 
492 		/** Vector of AudioMetaData. */
493 		AudioMetaDataVector audioMetaDataHistory;
494 
495 		/** Advance (increment) the history of AudioMetaData. The current index is moved one forward. */
496 		void advanceAudioMetaDataHistory(void);
497 
498 		/** Instance variable with some temporarily current audio meta data. */
499 		VisualAudioMetaData currAudioMetaData;
500 
501 		/** Flag that answers whether the lyrics creation thread is currently running. */
502 		static bool lyricsCreationThreadIsRunning;
503 
504 		/** The ProcessInfoMap is a map of strings that denote key and value of process info records. */
505 		typedef std::map<std::string, std::string> ProcessInfoMap;
506 
507 		/** A map of the process monitor info key-value-pairs. */
508 		ProcessInfoMap processInfoMap;
509 
510 		/** The ProcessInfoMapIterator is an iterator of the ProcessInfoMap. */
511 		typedef ProcessInfoMap::iterator ProcessInfoMapIterator;
512 
513 	};
514 
515 }
516 
517 #endif /* VisualDataStore_h */
518