Click here to Skip to main content
15,881,588 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.4K   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.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using BSE.Platten.Audio;
using BSE.Platten.BO;
using BSE.Platten.Common;

namespace BSE.Platten.Tunes
{
    public partial class ClipBoard : UserControl
    {
        #region EventsPublic

        public event EventHandler<AlbumSelectEventArgs> AlbumSelecting;

        #endregion

        #region FieldsPrivate

        private BSE.Platten.BO.Environment m_environment;
        private Control m_parentControl;
        private CTrack m_selectedTrack;
        private ToolStripMenuItem m_parentToolStripMenuItem;

        #endregion

        #region Properties

        public CPlayerManager PlayerManager
        {
            get { return this.m_playerManager; }
            set
            {
                this.m_playerManager = value;
                this.m_lstvClipBoard.PlayerManager = this.m_playerManager;
                if (this.m_trackInfo != null)
                {
                    this.m_trackInfo.PlayerManager = this.m_playerManager;
                }
            }
        }

        public BSE.Platten.BO.Environment Environment
        {
            get { return this.m_environment; }
            set
            {
                this.m_environment = value;
                if (this.m_trackInfo != null)
                {
                    this.m_trackInfo.Environment = this.m_environment;
                }
            }
        }

        public ToolStripMenuItem ParentToolStripMenuItem
        {
            get { return this.m_parentToolStripMenuItem; }
            set { this.m_parentToolStripMenuItem = value; }
        }

        #endregion

        #region MethodsPublic

        public ClipBoard()
        {
            InitializeComponent();
            this.m_lstvClipBoard.AlternatingBackColor = BSE.Platten.Common.BSEColors.AlternatingBackColor;
        }

        #endregion

        #region MethodsProtected

        protected void OnAlbumSelecting(AlbumSelectEventArgs e)
        {
            if (AlbumSelecting != null)
            {
                AlbumSelecting(this, e);
            }
        }

        #endregion

        #region MethodsPrivate

        private void CClipBoard_Load(object sender, EventArgs e)
        {
            this.m_parentControl = this.Parent;
            if (this.m_parentControl != null)
            {
                this.m_parentControl.VisibleChanged += new EventHandler(ClipBoardVisibleChanged);
            }
        }
        
        private void PlaySelectedTracksClick(object sender, EventArgs e)
        {
            PlaySelectedTracks(BSE.Platten.Audio.PlayMode.Playlist);
        }

        private void SelectAlbumClick(object sender, EventArgs e)
        {
            OnAlbumSelecting(new AlbumSelectEventArgs(this.m_selectedTrack.TitelId));
        }
        
        private void TrackInfoAlbumSelecting(object sender, AlbumSelectEventArgs e)
        {
            OnAlbumSelecting(new AlbumSelectEventArgs(e.TitelId));
        }

        private void ClipBoardDragDrop(object sender, DragEventArgs e)
        {
            BSE.Windows.Forms.CDataObject dataObject = e.Data as BSE.Windows.Forms.CDataObject;
            if (dataObject != null)
            {
                // get the currently hovered row that the items will be dragged to
                Point clientPoint = this.m_lstvClipBoard.PointToClient(new Point(e.X, e.Y));
                ListViewItem hoverListViewItem = this.m_lstvClipBoard.GetItemAt(clientPoint.X, clientPoint.Y);

                BSE.Windows.Forms.CDraggedListViewObjects draggedListViewObjects = null;
                //check the overload of the dataobject
                if (dataObject.GetDataPresent(typeof(BSE.Windows.Forms.CDraggedListViewObjects)))
                {
                    draggedListViewObjects
                        = (BSE.Windows.Forms.CDraggedListViewObjects)dataObject.GetData(
                        typeof(BSE.Windows.Forms.CDraggedListViewObjects));
                    if (draggedListViewObjects.ParentListView == null)
                    {
                        return;
                    }
                }
                if (dataObject.DragData != null)
                {
                    draggedListViewObjects
                        = (BSE.Windows.Forms.CDraggedListViewObjects)dataObject.DragData;

                    if (draggedListViewObjects.ParentListView == null)
                    {
                        return;
                    }
                }
                if (draggedListViewObjects != null)
                {
                    object dragObject = draggedListViewObjects.DragObjects[0];
                    //Wenn das Object vom Typ ListViewItem ist werden die ListViewItems in die ListView eingef�gt
                    if (dragObject is System.Windows.Forms.ListViewItem)
                    {
                        InsertListViewItems(hoverListViewItem, draggedListViewObjects);
                    }
                    PlayListDragDropData playListDragDropData = dragObject as PlayListDragDropData;
                    if (playListDragDropData != null)
                    {
                        switch (playListDragDropData.PlayListDragDropMode)
                        {
                            case PlayList.PlayListDragDropMode.Albums:
                                InsertListViewItemsAlbum(hoverListViewItem, playListDragDropData.Id);
                                break;
                            case PlayList.PlayListDragDropMode.PlayList:
                                InsertListViewItemTrack(hoverListViewItem, playListDragDropData.Id);
                                break;
                        }
                    }
                }
            }
        }
        private void ClipBoardDragEnter(object sender, DragEventArgs e)
        {
            if (e.Data is BSE.Windows.Forms.CDataObject)
            {
                this.m_lblInformation.Visible = false;
            }
        }

        private void ClipBoardDragLeave(object sender, EventArgs e)
        {
            if (this.m_lstvClipBoard.Items.Count == 0)
            {
                this.m_lblInformation.Visible = true;
            }
        }
        private void ClipBoardKeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                foreach (ListViewItem listViewItem in this.m_lstvClipBoard.SelectedItems)
                {
                    this.m_lstvClipBoard.Items.Remove(listViewItem);
                }
                if (this.m_lstvClipBoard.Items.Count == 0)
                {
                    this.m_lblInformation.Visible = true;
                }
            }
            if (e.KeyCode == Keys.T && e.Modifiers == Keys.Control)
            {
                if (this.m_lstvClipBoard.SelectedItems.Count > 0)
                {
                    ListViewItem listViewItem = this.m_lstvClipBoard.SelectedItems[0];
                    if (listViewItem != null)
                    {
                        this.m_selectedTrack = (CTrack)listViewItem.Tag;
                        SelectAlbumClick(sender, e);
                    }
                }
            }
        }
        
        private void ClipBoardMouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (this.m_lstvClipBoard.SelectedItems.Count > 0)
                {
                    ListViewItem listViewItem = this.m_lstvClipBoard.SelectedItems[0];
                    if (listViewItem != null)
                    {
                        this.m_selectedTrack = (CTrack)listViewItem.Tag;
                    }
                    this.m_ctxPlayList.Show(this.m_lstvClipBoard, new Point(e.X, e.Y));
                }
            }
        }
        
        private void ClipBoardResize(object sender, EventArgs e)
        {
            this.m_clmnDuration.Width = 60;
            this.m_clmnTitle.Width = this.m_lstvClipBoard.Width - this.m_clmnDuration.Width - 2 * SystemInformation.Border3DSize.Width;

            this.m_lblInformation.Width = this.m_lstvClipBoard.Width - 20;
            this.m_lblInformation.Left = this.m_lstvClipBoard.Left + 10;
        }
        
        private void ClipBoardVisibleChanged(object sender, EventArgs e)
        {
            if (this.m_parentToolStripMenuItem != null)
            {
                this.m_parentToolStripMenuItem.Checked = this.Visible;
            }
        }
        
        private void PlaySelectedTracks(BSE.Platten.Audio.PlayMode eMode)
        {
            CTrackCollection tracksCollection = new CTrackCollection(this.m_lstvClipBoard.SelectedItems.Count);
            foreach (ListViewItem listViewItem in this.m_lstvClipBoard.SelectedItems)
            {
                if (listViewItem.Tag is CTrack)
                {
                    CTrack track = (CTrack)listViewItem.Tag;
                    tracksCollection.Add(track);
                }
            }
            if (tracksCollection.Count > 0)
            {
                if (this.m_playerManager != null)
                {
                    try
                    {
                        this.m_playerManager.PlayTracks(tracksCollection, eMode);
                    }
                    catch (Exception exception)
                    {
                        GlobalizedMessageBox.Show(this,exception.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void InsertListViewItemsAlbum(ListViewItem hoverListViewItem, int iAlbumId)
        {
            if (this.m_environment != null)
            {
                CTunesBusinessObject tunesBusinessObject = new CTunesBusinessObject(this.m_environment.GetConnectionString());
                try
                {
                    CTrack[] tracks = tunesBusinessObject.GetAlbumTracksForPlayListByTitelId(iAlbumId);
                    if (tracks != null)
                    {
                        string strAudioHomeDirectory = CAlbum.GetAudioHomeDirectory(this.m_environment);
                        if (hoverListViewItem == null)
                        {
                            foreach (BSE.Platten.BO.CTrack track in tracks)
                            {
                                track.FileFullName = strAudioHomeDirectory + track.FileName;
                                ListViewItem listViewItem = CreateListViewItem(track);
                                if (listViewItem != null)
                                {
                                    this.m_lstvClipBoard.Items.Add(listViewItem);
                                }
                            }
                        }
                        else
                        {
                            int iHoverIndex = hoverListViewItem.Index;
                            for (int i = tracks.Length - 1; i >= 0; i--)
                            {
                                CTrack track = tracks[i];
                                if (track != null)
                                {
                                    track.FileFullName = strAudioHomeDirectory + track.FileName;
                                    ListViewItem listViewItem = CreateListViewItem(tracks[i]);
                                    if (listViewItem != null)
                                    {
                                        this.m_lstvClipBoard.Items.Insert(iHoverIndex, listViewItem);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    GlobalizedMessageBox.Show(this, exception.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void InsertListViewItemTrack(ListViewItem hoverListViewItem, int iLiedId)
        {
            if (this.m_environment != null)
            {
                CTunesBusinessObject tunesBusinessObject = new CTunesBusinessObject(this.m_environment.GetConnectionString());
                try
                {
                    CTrack track = tunesBusinessObject.GetTrackForPlayListByLiedId(iLiedId);
                    if (track != null)
                    {
                        string strAudioHomeDirectory = CAlbum.GetAudioHomeDirectory(this.m_environment);
                        if (hoverListViewItem == null)
                        {
                            track.FileFullName = strAudioHomeDirectory + track.FileName;
                            ListViewItem listViewItem = CreateListViewItem(track);
                            if (listViewItem != null)
                            {
                                this.m_lstvClipBoard.Items.Add(listViewItem);
                            }

                        }
                        else
                        {
                            int iHoverIndex = hoverListViewItem.Index;

                            track.FileFullName = strAudioHomeDirectory + track.FileName;
                            ListViewItem listViewItem = CreateListViewItem(track);
                            if (listViewItem != null)
                            {
                                this.m_lstvClipBoard.Items.Insert(iHoverIndex, listViewItem);
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    GlobalizedMessageBox.Show(this,exception.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void InsertListViewItems(ListViewItem hoverListViewItem, BSE.Windows.Forms.CDraggedListViewObjects draggedListViewObjects)
        {
            if (hoverListViewItem == null)
            {
                for (int i = 0; i < draggedListViewObjects.DragObjects.Count; i++)
                {
                    if (this.m_lstvClipBoard == draggedListViewObjects.ParentListView)
                    {
                        draggedListViewObjects.ParentListView.DragDropEffects = DragDropEffects.Move;
                    }
                    ListViewItem draggedListViewItem = (ListViewItem)draggedListViewObjects.DragObjects[i];
                    if (draggedListViewItem.Tag is CTrack)
                    {
                        CTrack track = draggedListViewItem.Tag as CTrack;
                        if (track != null)
                        {
                            ListViewItem listViewItem = CreateListViewItem(track);
                            if (listViewItem != null)
                            {
                                this.m_lstvClipBoard.Items.Add(listViewItem);
                            }
                        }
                    }
                }
            }
            else
            {
                int iHoverIndex = hoverListViewItem.Index;
                if (this.m_lstvClipBoard == draggedListViewObjects.ParentListView)
                {
                    draggedListViewObjects.ParentListView.DragDropEffects = DragDropEffects.Move;
                }
                for (int i = draggedListViewObjects.DragObjects.Count - 1; i >= 0; i--)
                {
                    ListViewItem draggedListViewItem = (ListViewItem)draggedListViewObjects.DragObjects[i];
                    if (draggedListViewItem.Tag is CTrack)
                    {
                        CTrack track = draggedListViewItem.Tag as CTrack;
                        if (track != null)
                        {
                            ListViewItem listViewItem = CreateListViewItem(track);
                            if (listViewItem != null)
                            {
                                this.m_lstvClipBoard.Items.Insert(iHoverIndex, listViewItem);
                            }
                        }
                    }
                }
            }

            if (draggedListViewObjects.ParentListView != null)
            {
                if (draggedListViewObjects.ParentListView.DragDropEffects == DragDropEffects.Move)
                {
                    foreach (ListViewItem listViewItemToRemove in draggedListViewObjects.ParentListView.SelectedItems)
                    {
                        draggedListViewObjects.ParentListView.Items.Remove(listViewItemToRemove);
                    }
                }
            }
        }

        private static ListViewItem CreateListViewItem(CTrack track)
        {
            ListViewItem listViewItem = null;
            if (track != null)
            {
                listViewItem = new ListViewItem();
                listViewItem.Text = track.Title;
                listViewItem.SubItems.Add(track.Duration.ToString());
                listViewItem.SubItems.Add(track.FileFullName);
                listViewItem.Tag = track;
            }
            return listViewItem;
        }

        #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