Click here to Skip to main content
15,893,644 members
Articles / Programming Languages / XML

Retrieving CD Information from a remote Freedb database

Rate me:
Please Sign up or sign in to vote.
4.92/5 (32 votes)
29 Jul 2004CPOL10 min read 220.4K   4.3K   125  
Describes a library that can be used to retrieve Audio CD information from the CDDB compatible freedb database.
#region COPYRIGHT (c) 2004 by Brian Weeres
/* Copyright (c) 2004 by Brian Weeres
 * 
 * Email: bweeres@protegra.com; bweeres@hotmail.com
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * If you modify it then please indicate so. 
 *
 * The software is provided "AS IS" and there are no warranties or implied warranties.
 * In no event shall Brian Weeres and/or Protegra Technology Group be liable for any special, 
 * direct, indirect, or consequential damages or any damages whatsoever resulting for any reason 
 * out of the use or performance of this software
 * 
 */
#endregion
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
using Freedb;

namespace FreedbDemo
{
	/// <summary>
	/// Summary description for DLGPreferences.
	/// </summary>
	public class DLGPreferences : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ComboBox CBDefaultServer;
		private SiteCollection m_sites = null;





		// not bothing with private and public at this point
		public string UserName;
		public string HostName;
		public Site SelectedSite = null;
		private FreedbHelper m_freedb = null;
		
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox TBUserName;
		private System.Windows.Forms.TextBox TBHostName;
		private System.Windows.Forms.Button BTNOkay;
		private System.Windows.Forms.Button BTNCancel;


		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;


		public DLGPreferences(FreedbHelper helper)
		{
			m_freedb = helper;
			InitializeComponent();
			UserName = m_freedb.UserName;
			HostName = m_freedb.Hostname;
			this.TBUserName.Text = this.UserName;
			this.TBHostName.Text = this.HostName;
			SelectedSite = m_freedb.CurrentSite;

			try
			{
				Debug.Assert(m_freedb != null,"The FreedbHelper object is null. It must be intialized prior to calling this dialog." );
				//load up the sites

				string result = m_freedb.GetSites(Freedb.Site.PROTOCOLS.HTTP,out m_sites);
				if (result == FreedbHelper.ResponseCodes.CODE_210)
					PopulateCombo();
			}

			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}



		private void PopulateCombo()
		{
			Site defaultSite = null;
			Site mainSite = null;
			foreach (Site site in m_sites )
			{
				int index = CBDefaultServer.Items.Add(site);

				//determine which site should be currently selected. 
				//Since we always retrieve the sites the site objects themselves will not be the same instance
				//so figure out which one matches our current site by comparing the url's 
				if (m_freedb.CurrentSite != null && site.GetUrl() == m_freedb.CurrentSite.GetUrl())
					defaultSite = site;

				// always figure out the main site because one of the other sites may be not listed anymore
				if (site.GetUrl() == FreedbHelper.MAIN_FREEDB_SITE)
					mainSite = site;
			}

			//set the currently selected one
			if (defaultSite != null)
				CBDefaultServer.SelectedItem = defaultSite;
			else
				CBDefaultServer.SelectedItem = mainSite;
		}


		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.CBDefaultServer = new System.Windows.Forms.ComboBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.TBUserName = new System.Windows.Forms.TextBox();
			this.TBHostName = new System.Windows.Forms.TextBox();
			this.BTNOkay = new System.Windows.Forms.Button();
			this.BTNCancel = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(120, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "Select Default Server:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// CBDefaultServer
			// 
			this.CBDefaultServer.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.CBDefaultServer.Location = new System.Drawing.Point(144, 16);
			this.CBDefaultServer.MaxDropDownItems = 15;
			this.CBDefaultServer.Name = "CBDefaultServer";
			this.CBDefaultServer.Size = new System.Drawing.Size(328, 21);
			this.CBDefaultServer.TabIndex = 1;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(24, 48);
			this.label2.Name = "label2";
			this.label2.TabIndex = 2;
			this.label2.Text = "UserName:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(24, 80);
			this.label3.Name = "label3";
			this.label3.TabIndex = 3;
			this.label3.Text = "HostName:";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// TBUserName
			// 
			this.TBUserName.Location = new System.Drawing.Point(144, 48);
			this.TBUserName.Name = "TBUserName";
			this.TBUserName.Size = new System.Drawing.Size(328, 20);
			this.TBUserName.TabIndex = 4;
			this.TBUserName.Text = "textBox1";
			// 
			// TBHostName
			// 
			this.TBHostName.Location = new System.Drawing.Point(144, 80);
			this.TBHostName.Name = "TBHostName";
			this.TBHostName.Size = new System.Drawing.Size(328, 20);
			this.TBHostName.TabIndex = 5;
			this.TBHostName.Text = "textBox1";
			// 
			// BTNOkay
			// 
			this.BTNOkay.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.BTNOkay.Location = new System.Drawing.Point(163, 120);
			this.BTNOkay.Name = "BTNOkay";
			this.BTNOkay.TabIndex = 6;
			this.BTNOkay.Text = "Okay";
			this.BTNOkay.Click += new System.EventHandler(this.BTNOkay_Click);
			// 
			// BTNCancel
			// 
			this.BTNCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.BTNCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.BTNCancel.Location = new System.Drawing.Point(267, 120);
			this.BTNCancel.Name = "BTNCancel";
			this.BTNCancel.TabIndex = 7;
			this.BTNCancel.Text = "Cancel";
			// 
			// DLGPreferences
			// 
			this.AcceptButton = this.BTNOkay;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.BTNCancel;
			this.ClientSize = new System.Drawing.Size(504, 158);
			this.Controls.Add(this.BTNCancel);
			this.Controls.Add(this.BTNOkay);
			this.Controls.Add(this.TBHostName);
			this.Controls.Add(this.TBUserName);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.CBDefaultServer);
			this.Controls.Add(this.label1);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "DLGPreferences";
			this.ShowInTaskbar = false;
			this.Text = "Preferences";
			this.ResumeLayout(false);

		}
		#endregion

		private void BTNOkay_Click(object sender, System.EventArgs e)
		{
			SelectedSite = (Site)this.CBDefaultServer.SelectedItem;
			UserName = this.TBUserName.Text;
			HostName = this.TBHostName.Text;
			this.DialogResult = DialogResult.OK;

		}



	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions