Click here to Skip to main content
15,885,115 members
Articles / Desktop Programming / Windows Forms

BSEtunes

Rate me:
Please Sign up or sign in to vote.
4.67/5 (11 votes)
24 Apr 2010CPOL4 min read 64.6K   4.3K   58  
BSEtunes is a MySQL based, full manageable, networkable single or multiuser jukebox application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

using BSE.Platten.BO;
using BSE.Platten.Common;
using System.Globalization;
using BSE.Platten.Audio.Properties;

namespace BSE.Platten.Audio
{
    public partial class CTagger : BaseForm
    {
        #region Events

        public event System.EventHandler ReadingComplete;

        #endregion

        #region Delegates

        private delegate void WriteStatusHandler(CTrack track);
        private delegate void TagReadingCompleteHandler();
        private delegate void WindowCloseHandler();

        #endregion

        #region FieldsPrivate

        private BSE.Platten.BO.CAlbum m_album;
        private CTag m_tag;

        #endregion
        
        #region MethodsPublic
        
        public CTagger()
        {
            InitializeComponent();
        }

        public CTagger(BSE.Platten.BO.CAlbum album) : this()
        {
            this.m_album = album;
            if (this.m_album != null)
            {
                this.m_prgsTotalTime.Maximum = 1;
                this.m_prgsTotalTime.Maximum = this.m_album.Tracks.Length;
            }
        }

        #endregion

        #region MethodsProtected

        protected virtual void OnReadingComplete(object sender, System.EventArgs e)
        {
            if (this.ReadingComplete != null)
            {
                this.ReadingComplete(this, e);
            }
        }

        #endregion

        #region MethodsPrivate

        private void m_btnCancel_Click(object sender, EventArgs e)
        {
            if (this.m_tag != null)
            {
                this.m_tag.CancelRead();
            }
        }

        private void CTagger_Load(object sender, EventArgs e)
        {
            this.m_tag = new CTag(this.m_album,this,this);
            this.m_tag.CancelReading += new System.EventHandler(this.m_Tag_CancelReading);
            this.m_tag.ReadingAborted += new System.EventHandler(this.m_tag_ReadingAborted);
            this.m_tag.ReadingComplete += new System.EventHandler(this.m_Tag_ReadingComplete);
            this.m_tag.TagWritten += new EventHandler<WriteTagEventArgs>(this.m_tag_TagWritten);
        }

        private void CTagger_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.m_tag != null)
            {
                System.Threading.Thread threadWriteTags = this.m_tag.Thread();
                if (threadWriteTags != null && threadWriteTags.IsAlive)
                {
                    this.m_tag.CancelRead();
                }
            }
        }

        private void m_Tag_CancelReading(object sender, System.EventArgs e)
        {
            if (this.IsHandleCreated == true)
            {
                this.Invoke(new WindowCloseHandler(WindowCloseEvent));
            }
        }

        private void m_tag_ReadingAborted(object sender, System.EventArgs e)
        {
            if (this.IsHandleCreated == true)
            {
                this.Invoke(new WindowCloseHandler(WindowCloseEvent));
            }
        }

        private void m_Tag_ReadingComplete(object sender, System.EventArgs e)
        {
            if (this.IsHandleCreated == true)
            {
                this.Invoke(new TagReadingCompleteHandler(TagReadingComplete));
            }
        }

        private void m_tag_TagWritten(object sender, WriteTagEventArgs e)
        {
            if (this.IsHandleCreated == true)
            {
                this.Invoke(new WriteStatusHandler(WriteStatusText), e.Track);
            }
        }

        private void WindowCloseEvent()
        {
            this.Close();
        }

        private void WriteStatusText(CTrack track)
        {
            if (track != null)
            {
                string strTagWritten = string.Format(CultureInfo.CurrentUICulture, Resources.IDS_TaggerTagWritten, track.FileFullName);
                if (track.Index == 1)
                {
                    this.m_txtStatus.AppendText(strTagWritten);
                }
                else
                {
                    this.m_txtStatus.AppendText(Environment.NewLine + strTagWritten);
                }
                this.m_prgsTotalTime.Value = track.Index;
            }
        }

        private void TagReadingComplete()
        {
            if (this.ReadingComplete != null)
            {
                this.ReadingComplete(this, new System.EventArgs());
            }
            this.Close();
        }

        #endregion

        #region internal class CTag

        internal class CTag
        {
            #region EventsPublic

            public event System.EventHandler CancelReading;
            public event System.EventHandler ReadingAborted;
            public event System.EventHandler ReadingComplete;
            public event EventHandler<WriteTagEventArgs> TagWritten;

            #endregion

            #region FieldsPrivate

            private System.Threading.Thread m_threadWriteTags;
            private BSE.Platten.BO.CAlbum m_album;
            private System.Timers.Timer m_timer;
            private bool m_bCancelReading;
            private IWin32Window m_owner;

            #endregion

            #region MethodsPublic

            public CTag(BSE.Platten.BO.CAlbum album,IWin32Window owner,Form parentForm)
            {
                this.m_album = album;
                this.m_owner = owner;
                this.m_timer = new System.Timers.Timer();
                this.m_timer.Enabled = true;
                this.m_timer.Elapsed += new System.Timers.ElapsedEventHandler(this.Timer_Elapsed);
                this.m_timer.Start();

                this.m_threadWriteTags =
                    new System.Threading.Thread(
                    new System.Threading.ThreadStart(this.WriteTags));
                this.m_threadWriteTags.Start();
            }

            public System.Threading.Thread Thread()
            {
                return this.m_threadWriteTags;
            }

            public void CancelRead()
            {
                if (this.m_threadWriteTags.ThreadState == System.Threading.ThreadState.Running)
                {
                    if (this.m_timer != null)
                    {
                        this.m_timer.Stop();
                    }
                    this.m_threadWriteTags.Abort();
                }
                if (this.m_bCancelReading == false)
                {
                    this.m_bCancelReading = true;
                    OnCancelReading(this, new System.EventArgs());
                }
            }

            #endregion

            #region MethodsProtected

            protected void OnReadingAborted(object sender, System.EventArgs e)
            {
                this.m_timer.Stop();
                if (ReadingAborted != null)
                {
                    ReadingAborted(this, e);
                }
            }

            protected void OnCancelReading(object sender, System.EventArgs e)
            {
                if (CancelReading != null)
                {
                    CancelReading(this, e);
                }
            }

            #endregion

            #region MethodsPrivate

            private void WriteTags()
            {
                try
                {
                    int iWriteTagsCounter = 0;
                    foreach (BSE.Platten.BO.CTrack track in this.m_album.Tracks)
                    {
                        if (track.FileFullName != null)
                        {
                            System.IO.FileInfo fileInfo = new System.IO.FileInfo(track.FileFullName);
                            if ((string.Compare(fileInfo.Extension.ToLower(),AudioformatExtensions.Mp3) == 0) ||
                                (string.Compare(fileInfo.Extension.ToLower(),AudioformatExtensions.Wma) == 0))
                            {
                                AudioData audioData = new CWMFMediaData();
                                audioData.SetAttributeAuthor(track.FileFullName, this.m_album.Interpret);
                                audioData.SetAttributeAlbumTitle(track.FileFullName, this.m_album.Title);
                                audioData.SetAttributeGenre(track.FileFullName, this.m_album.Genre);
                                audioData.SetAttributeYear(track.FileFullName, this.m_album.Year.ToString());
                                audioData.SetAttributeTrackNumber(track.FileFullName, track.TrackNumber.ToString());
                                if (string.IsNullOrEmpty(track.Title) == false)
                                {
                                    audioData.SetAttributeTitle(track.FileFullName, track.Title);
                                }
                                track.Index = iWriteTagsCounter + 1;
                                if (TagWritten != null)
                                {
                                    TagWritten(this, new BSE.Platten.Audio.WriteTagEventArgs(track));
                                }
                            }
                        }
                        iWriteTagsCounter++;
                    }
                }
                catch (System.Runtime.InteropServices.COMException comException)
                {
                    if (this.m_threadWriteTags.ThreadState != System.Threading.ThreadState.AbortRequested)
                    {
                        string strMessage = String.Format(
                            CultureInfo.InvariantCulture,"{0} {1}",
                            comException.Message,
                            Resources.IDS_TaggerComExceptionExtension);
                        
                        GlobalizedMessageBox.Show(this.m_owner,strMessage, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        OnReadingAborted(this, new System.EventArgs());
                    }
                }
                catch (Exception exception)
                {
                    if (this.m_threadWriteTags.ThreadState != System.Threading.ThreadState.AbortRequested)
                    {
                        GlobalizedMessageBox.Show(this.m_owner, exception.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        OnReadingAborted(this, new System.EventArgs());
                    }
                }
            }

            private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                if (this.m_threadWriteTags != null)
                {
                    if (this.m_threadWriteTags.ThreadState == System.Threading.ThreadState.Stopped)
                    {
                        this.m_timer.Stop();
                        if (this.ReadingComplete != null)
                        {
                            this.ReadingComplete(this.m_owner, new System.EventArgs());
                        }
                    }
                }
            }

            #endregion
        }

        #endregion
    }
}

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
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions