//====START_GENERATED_PROLOG======================================
//
//
//   COMPONENT_NAME: odother
//
//   CLASSES: none
//
//   ORIGINS: 27
//
//
//   (C) COPYRIGHT International Business Machines Corp. 1995,1996
//   All Rights Reserved
//   Licensed Materials - Property of IBM
//   US Government Users Restricted Rights - Use, duplication or
//   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//
//   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
//   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
//   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
//   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
//   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
//   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
//   OR PERFORMANCE OF THIS SOFTWARE.
//
//====END_GENERATED_PROLOG========================================
//
// @(#) 1.1 nt/src/samples/cdplayer/cdreg.cpp, odcdplayerpart, od96nt, odnt9712d 8/22/96 15:35:44 [ 3/21/97 17:32:48 ]

/*-------------------------------------------------------------------+
|                           CDRegistry.C                             |
| ================================================================== |
|                                                                    |
| Description:  CD Registry implementation                           |
|                                                                    |
| Author:       David Kuettel, Summer 1996                           |
|                                                                    |
+-------------------------------------------------------------------*/
#ifndef IBMSample_CDREGISTRY
#include "CDReg.h"
#endif

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>


/*-------------------------------------------------------------------+
|                     CDRegistry::CDRegistry ()                      |
| ================================================================== |
|                                                                    |
| Function:    CDRegistry default constructor                        |
|                                                                    |
| Algorithm:   o  ...                                                |
|                                                                    |
+-------------------------------------------------------------------*/
CDRegistry::CDRegistry ()
{
	;
}


/*-------------------------------------------------------------------+
|                    CDRegistry::~CDRegistry ()                      |
| ================================================================== |
|                                                                    |
| Function:    CDRegistry destructor...                              |
|                                                                    |
+-------------------------------------------------------------------*/
CDRegistry::~CDRegistry ()
{
	;
}


/*-------------------------------------------------------------------+
|                  CDRegistry::LookupSongTitles ()                   |
| ================================================================== |
|                                                                    |
| Function:    ...                                                   |
|                                                                    |
| Algorithm:   Looks at the following key structure in the           |
|              Windows system registry:                              |
|                                                                    |
|              HKEY_CURRENT_USER                                     |
|                  Software                                          |
|                     IBM                                            |
|                         IBM CDPlayer                               |
|                             Albums                                 |
|                                 {key} = <album name>               |
|                                     Artist = <artist's name>       |
|                                     Songs = (list of songs)        |
|                                                                    |
| Parameters:  ...                                                   |
|                                                                    |
| Returns:     ...                                                   |
|                                                                    |
+-------------------------------------------------------------------*/
long
CDRegistry::LookupSongTitles (CDAlbum& album)
{
	HKEY hDatabaseKey;
	HKEY hAlbumKey;
	HKEY hArtistKey;
	HKEY hSongKey;

	char buffer [256];
	long result;

	/*
	 *  Ensure the album search key is valid...
	 */
	if (album.GetKey () == NULL) return E_FAIL;	// shouldn't happen...

	do {
		/*
		 *  Now try and open the database...
		 */
		HKEY hDatabaseKey;
		result = RegOpenKeyEx (HKEY_CURRENT_USER, CDPLAYER_ROOT_KEY,
					0, KEY_READ, &hDatabaseKey);
		if (result) break;

		/*
		 *  Great, so far so good.  Now has this cd already been 
		 *  registered?
		 */
		strcpy (buffer, album.GetKey ());
		HKEY hAlbumKey;

		result = RegOpenKeyEx (hDatabaseKey, buffer, 0, KEY_READ, 
					&hAlbumKey);
		if (result) break;

		/*
		 *  Ok, this album has been registered before...
		 *  Get the album name..
		 */
		LONG size = 256;
		result = RegQueryValue (hAlbumKey, NULL, buffer, &size);
		if (result) break;

		album.SetTitle (buffer);
		
		/*
		 *  Ok, this album has been registered before...
		 *  Get the artists name..
		 */
		result = RegOpenKeyEx (hAlbumKey, "Artist", 0, KEY_READ, 
					&hArtistKey);
		if (result) break;

		size = 256;
		result = RegQueryValue (hArtistKey, NULL, buffer, &size);
		if (result) break;

		album.SetArtist (buffer);
		
		/*
		 *  Ok, now get the songs...
		 */
		result = RegOpenKeyEx (hAlbumKey, "Songs", 0, KEY_READ, 
					&hSongKey);
		if (result) break;

		List<CDSong*>& list = *album.GetSongs ();
		for (int i=0; i<list.Length (); i++) {
			char track [50];
			size = 256;

			sprintf (track, "Track[%02d]", i+1);

			result = RegQueryValue (hSongKey, track, buffer, &size);
			if (result) break;

			CDSong* song = list [i];
			song->SetTitle (buffer);
		}
	} while (0);

	RegCloseKey (hDatabaseKey);
	RegCloseKey (hAlbumKey);
	RegCloseKey (hArtistKey);
	RegCloseKey (hSongKey);

	return result;
}


/*-------------------------------------------------------------------+
|                 CDRegistry::RegisterSongTitles ()                  |
| ================================================================== |
|                                                                    |
| Function:    ...                                                   |
|                                                                    |
| Algorithm:   Register at the following key structure in the        |
|              Windows system registry:                              |
|                                                                    |
|              HKEY_CURRENT_USER                                     |
|                  Software                                          |
|                     IBM                                            |
|                         IBM CDPlayer                               |
|                             Albums                                 |
|                                 {key} = <album name>               |
|                                     Artist = <artist's name>       |
|                                     Songs = (list of songs)        |
|                                                                    |
| Parameters:  ...                                                   |
|                                                                    |
| Returns:     ...                                                   |
|                                                                    |
+-------------------------------------------------------------------*/
long
CDRegistry::RegisterSongTitles (CDAlbum& album)
{
	HKEY hAlbumKey;
	HKEY hArtistKey;
	HKEY hSongKey;
	HKEY hKey;

	char buffer [256];
	DWORD size;
	long result;

	/*
	 *  Ensure the album search key is valid...
	 */
	if (album.GetKey () == NULL) return E_FAIL;	// shouldn't happen...

	do {
		sprintf (buffer, "%s\\%s", CDPLAYER_ROOT_KEY, album.GetKey ());

		result = RegCreateKey (HKEY_CURRENT_USER, buffer, &hAlbumKey);
		if (result) break;

		/*
		 *  Save the Album name...
		 */
		if (album.GetTitle () == NULL)
			strcpy (buffer, "<Album Name>");
		else
			strcpy (buffer, album.GetTitle ());

		size = strlen (buffer);

		result = RegSetValue (hAlbumKey, NULL, REG_SZ, buffer, size);
		if (result) break;

		/*
		 *  Save the Artist name...
		 */
		result = RegCreateKey (hAlbumKey, "Artist", &hKey);
		if (result) break;

		if (album.GetArtist () == NULL)
			strcpy (buffer, "<Arist Name>");
		else
			strcpy (buffer, album.GetArtist ());
		size = strlen (buffer);

		result = RegSetValue (hKey, NULL, REG_SZ, buffer, size);
		if (result) break;

		/*
		 *  Save the Songs...
		 */
		result = RegCreateKey (hAlbumKey, "Songs", &hSongKey);
		if (result) break;

		List<CDSong*>& list = *album.GetSongs ();
		for (int i=0; i<list.Length (); i++) {
			char track [50];
			size = 256;

			sprintf (track, "Track[%02d]", i+1);

			result = RegCreateKey (hSongKey, track, &hKey);
			if (result) break;

			CDSong& song = *list [i];
			if (song.GetTitle () == NULL)
				strcpy (buffer, "<Song Name>");
			else
				strcpy (buffer, song.GetTitle ());

			result = RegSetValue (hKey, NULL, REG_SZ, buffer, size);
			if (result) break;
		}
	} while (0);

	RegCloseKey (hAlbumKey);
	RegCloseKey (hArtistKey);
	RegCloseKey (hSongKey);

	return result;
}
