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.BO;
using BSE.Platten.Audio;
using BSE.Platten.Common;
using System.Globalization;
using BSE.Platten.Tunes.Properties;
namespace BSE.Platten.Tunes
{
public partial class PlayList : UserControl
{
#region Constants
private const string m_strParentControlCaption = "Wiedergabeliste: ";
private const string m_strEmptyPlayList = "keine Playliste ausgew�hlt";
#endregion
#region Enums
public enum PlayListDragDropMode
{
Albums,
PlayList
}
#endregion
#region EventsPublic
public event EventHandler<PlayListEventArgs> PlayListInserted;
public event EventHandler<PlayListEventArgs> PlayListChanged;
public event EventHandler<PlayListEventArgs> PlayListDeleted;
public event EventHandler<AlbumSelectEventArgs> AlbumSelecting;
#endregion
#region FieldsPrivate
private BSE.Platten.BO.Environment m_environment;
private CPlaylist m_currentPlayList;
private CTrack m_selectedTrack;
private ToolStripMenuItem m_parentToolStripMenuItem;
private Control m_parentControl;
private bool m_bPlayListHasChanged;
#endregion
#region Properties
public CPlayerManager PlayerManager
{
get { return this.m_playerManager; }
set
{
this.m_playerManager = value;
this.m_lstvPlayList.PlayerManager = this.m_playerManager;
}
}
public BSE.Configuration.Configuration Settings
{
get { return this.m_settings; }
set { this.m_settings = value; }
}
public BSE.Platten.BO.Environment Environment
{
get { return this.m_environment; }
set { this.m_environment = value; }
}
public ToolStripMenuItem ParentToolStripMenuItem
{
get { return this.m_parentToolStripMenuItem; }
set { this.m_parentToolStripMenuItem = value; }
}
#endregion
#region MethodsPublic
public PlayList()
{
InitializeComponent();
this.m_lstvPlayList.AlternatingBackColor = BSE.Platten.Common.BSEColors.AlternatingBackColor;
UpdateViewState();
}
public void SetPlaylist(int iPlayListId)
{
SetCurrentPlayList(iPlayListId);
}
public void SetNewPlayList()
{
NewPlaylist();
}
public void SetDeletePlayList()
{
DeletePlaylist();
}
public void SetPlayTracks()
{
if (this.m_lstvPlayList.Items.Count == 0)
{
string strMessage = String.Format(
CultureInfo.CurrentUICulture,
Resources.IDS_PlayListEmptyException,
this.m_currentPlayList.Name);
GlobalizedMessageBox.Show(this, strMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
PlayTracks(BSE.Platten.Audio.PlayMode.Playlist);
}
public void SetPlayRandomTracks()
{
if (this.m_lstvPlayList.Items.Count == 0)
{
string strMessage = String.Format(
CultureInfo.CurrentUICulture,
Resources.IDS_PlayListEmptyException,
this.m_currentPlayList.Name);
GlobalizedMessageBox.Show(this,strMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
PlayTracks(BSE.Platten.Audio.PlayMode.Random);
}
#endregion
#region MethodsProtected
protected void OnPlayListInserted(PlayListEventArgs e)
{
if (PlayListInserted != null)
{
PlayListInserted(this, e);
}
}
protected void OnPlayListDeleted(PlayListEventArgs e)
{
if (PlayListDeleted != null)
{
PlayListDeleted(this, e);
}
}
protected void OnPlayListChanged(PlayListEventArgs e)
{
if (PlayListChanged != null)
{
PlayListChanged(this, e);
}
}
protected void OnAlbumSelecting(AlbumSelectEventArgs e)
{
if (AlbumSelecting != null)
{
AlbumSelecting(this, e);
}
}
#endregion
#region MethodsPrivate
private void PlayListLoad(object sender, EventArgs e)
{
Form parentForm = this.FindForm();
if (parentForm != null)
{
parentForm.FormClosing += new FormClosingEventHandler(PlayListFormClosing);
LoadSettings();
}
this.m_parentControl = this.Parent;
if (m_parentControl != null)
{
m_parentControl.Text = string.Format(CultureInfo.CurrentUICulture,"{0} {1}", Resources.IDS_PlayListCaptionbarText, Resources.IDS_PlayListCaptionbarTextNoPlaylistSelected);
m_parentControl.VisibleChanged += new EventHandler(PlayListVisibleChanged);
}
}
private void PlayListFormClosing(object sender, FormClosingEventArgs e)
{
SaveSettings();
}
private void PlayListVisibleChanged(object sender, EventArgs e)
{
if (this.m_parentToolStripMenuItem != null)
{
this.m_parentToolStripMenuItem.Checked = this.Visible;
}
}
private void PlayListMouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (this.m_lstvPlayList.SelectedItems.Count > 0)
{
ListViewItem listViewItem = this.m_lstvPlayList.SelectedItems[0];
if (listViewItem != null)
{
this.m_selectedTrack = (CTrack)listViewItem.Tag;
this.m_ctxPlayList.Show(this.m_lstvPlayList, new Point(e.X, e.Y));
}
}
}
}
private void PlayListKeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
foreach (ListViewItem listViewItem in this.m_lstvPlayList.SelectedItems)
{
this.m_lstvPlayList.Items.Remove(listViewItem);
this.m_bPlayListHasChanged = true;
}
UpdateViewState();
}
if (e.KeyCode == Keys.T && e.Modifiers == Keys.Control)
{
if (this.m_lstvPlayList.SelectedItems.Count > 0)
{
ListViewItem listViewItem = this.m_lstvPlayList.SelectedItems[0];
if (listViewItem != null)
{
this.m_selectedTrack = (CTrack)listViewItem.Tag;
SelectAlbumClick(sender, e);
}
}
}
}
private void NewPlayListClick(object sender, EventArgs e)
{
NewPlaylist();
}
private void SavePlayListClick(object sender, EventArgs e)
{
SavePlaylist();
}
private void DeletePlayListClick(object sender, EventArgs e)
{
DeletePlaylist();
}
private void PlayTracksClick(object sender, EventArgs e)
{
PlayTracks(BSE.Platten.Audio.PlayMode.Playlist);
}
private void PlaySelectedTracksClick(object sender, EventArgs e)
{
PlaySelectedTracks(BSE.Platten.Audio.PlayMode.Playlist);
}
private void PlayRandomClick(object sender, EventArgs e)
{
PlayTracks(BSE.Platten.Audio.PlayMode.Random);
}
private void SelectAlbumClick(object sender, EventArgs e)
{
OnAlbumSelecting(new AlbumSelectEventArgs(this.m_selectedTrack.TitelId));
}
private void NewPlayListInserted(object sender, PlayListEventArgs e)
{
if (e.PlayList != null)
{
this.m_lstvPlayList.Items.Clear();
this.m_currentPlayList = e.PlayList;
if (this.m_parentControl != null)
{
this.m_parentControl.Text = string.Format(
CultureInfo.CurrentUICulture,
"{0} {1}",
Resources.IDS_PlayListCaptionbarText,
Resources.IDS_PlayListCaptionbarTextNoPlaylistSelected, this.m_currentPlayList.Name);
this.m_parentControl.Refresh();
}
UpdateViewState();
OnPlayListInserted(new PlayListEventArgs(this.m_currentPlayList));
}
}
private void SetCurrentPlayList(int iPlayListId)
{
this.m_lstvPlayList.Items.Clear();
Cursor.Current = Cursors.WaitCursor;
if (this.m_environment != null)
{
CTunesBusinessObject tunesBusinessObject = new CTunesBusinessObject(this.m_environment.GetConnectionString());
try
{
this.m_currentPlayList = tunesBusinessObject.GetPlayListByPlayListId(iPlayListId);
if (this.m_currentPlayList != null)
{
CTrack[] tracks = m_currentPlayList.Tracks;
if (tracks != null)
{
string strAudioHomeDirectory = CAlbum.GetAudioHomeDirectory(this.m_environment);
foreach (CTrack track in tracks)
{
track.FileFullName = strAudioHomeDirectory + track.FileName;
ListViewItem listViewItem = CreateListViewItem(track);
if (listViewItem != null)
{
this.m_lstvPlayList.Items.Add(listViewItem);
}
}
}
if (this.m_parentControl != null)
{
this.m_parentControl.Text = string.Format(
CultureInfo.CurrentUICulture,
"{0} {1}",
Resources.IDS_PlayListCaptionbarText,
this.m_currentPlayList.Name);
}
OnPlayListChanged(new PlayListEventArgs(this.m_currentPlayList));
}
}
catch (Exception exception)
{
string strMessage = string.Format(
CultureInfo.CurrentUICulture,
"{0} {1}",
Resources.IDS_PlayListLoadException + exception.Message);
GlobalizedMessageBox.Show(this,strMessage, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
UpdateViewState();
}
}
Cursor.Current = Cursors.Default;
}
private void NewPlaylist()
{
if (this.m_environment != null)
{
using (NewPlayList newPlaylist = new NewPlayList(this.m_environment))
{
newPlaylist.PlayListInserted -= new EventHandler<PlayListEventArgs>(NewPlayListInserted);
newPlaylist.PlayListInserted += new EventHandler<PlayListEventArgs>(NewPlayListInserted);
newPlaylist.ShowDialog();
}
}
}
private void SavePlaylist()
{
Cursor.Current = Cursors.WaitCursor;
if (this.m_bPlayListHasChanged)
{
System.Collections.ArrayList aPlaylistEntries = new System.Collections.ArrayList();
CPlayListEntries[] playListEntries = null;
foreach (ListViewItem listViewItem in this.m_lstvPlayList.Items)
{
CTrack track = listViewItem.Tag as CTrack;
if (track != null)
{
CPlayListEntries playListEntry = new CPlayListEntries();
playListEntry.Guid = System.Guid.NewGuid();
playListEntry.EntryId = -1;
playListEntry.LiedId = track.LiedId;
playListEntry.PlayListId = this.m_currentPlayList.Id;
aPlaylistEntries.Add(playListEntry);
}
CPlayListEntries tmpPlayListEntry = listViewItem.Tag as CPlayListEntries;
if (tmpPlayListEntry != null)
{
aPlaylistEntries.Add(tmpPlayListEntry);
}
}
playListEntries = new CPlayListEntries[aPlaylistEntries.Count];
aPlaylistEntries.CopyTo(playListEntries);
this.m_currentPlayList.PlayListEntries = playListEntries;
CTunesBusinessObject tunesBusinessObject = new CTunesBusinessObject(this.m_environment.GetConnectionString());
try
{
tunesBusinessObject.SavePlayList(this.m_currentPlayList);
}
catch (Exception exception)
{
string strMessage = string.Format(
CultureInfo.CurrentUICulture,
"{0} {1}",
Resources.IDS_PlayListSaveException, exception.Message);
GlobalizedMessageBox.Show(this,strMessage, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
UpdateViewState();
Cursor.Current = Cursors.Default;
}
}
}
private void DeletePlaylist()
{
string strPlaylistName = this.m_currentPlayList.Name;
string strWarning = string.Format(CultureInfo.CurrentUICulture,Resources.IDS_PlayListDeleteWarningException, strPlaylistName);
DialogResult dialogResult = GlobalizedMessageBox.Show(
this,strWarning
, MessageBoxButtons.YesNo
, MessageBoxIcon.Warning);
if (dialogResult == DialogResult.Yes)
{
if (this.m_environment != null)
{
CTunesBusinessObject tunesBusinessObject = new CTunesBusinessObject(this.m_environment.GetConnectionString());
try
{
tunesBusinessObject.DeletePlayList(this.m_currentPlayList.Id);
}
catch (Exception exception)
{
string strMessage = string.Format(
CultureInfo.CurrentUICulture,
"{0} {1}", Resources.IDS_PlayListDeleteException, exception.Message);
GlobalizedMessageBox.Show(this, strMessage, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
this.m_lstvPlayList.Items.Clear();
this.m_currentPlayList = null;
if (this.m_parentControl != null)
{
this.m_parentControl.Text = string.Format(
CultureInfo.CurrentUICulture,
"{0} {1}",
Resources.IDS_PlayListCaptionbarText,
Resources.IDS_PlayListCaptionbarTextNoPlaylistSelected);
this.m_parentControl.Refresh();
}
UpdateViewState();
this.OnPlayListDeleted(new PlayListEventArgs(this.m_currentPlayList));
}
}
}
private void PlayListDragDrop(object sender, DragEventArgs e)
{
if (e.Data is BSE.Windows.Forms.CDataObject)
{
// get the currently hovered row that the items will be dragged to
Point clientPoint = this.m_lstvPlayList.PointToClient(new Point(e.X, e.Y));
ListViewItem hoverListViewItem = this.m_lstvPlayList.GetItemAt(clientPoint.X, clientPoint.Y);
//Get the dataobject
BSE.Windows.Forms.CDataObject dataObject = (BSE.Windows.Forms.CDataObject)e.Data;
BSE.Windows.Forms.CDraggedListViewObjects draggedListViewObjects = null;
//check the overload of the dataobject
if (dataObject.GetDataPresent(typeof(BSE.Windows.Forms.CDraggedListViewObjects).ToString()))
{
draggedListViewObjects
= (BSE.Windows.Forms.CDraggedListViewObjects)dataObject.GetData(
typeof(BSE.Windows.Forms.CDraggedListViewObjects).ToString());
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.GetType() == typeof(System.Windows.Forms.ListViewItem))
{
InsertListViewItems(hoverListViewItem, draggedListViewObjects);
}
if (dragObject.GetType() == typeof(PlayListDragDropData))
{
PlayListDragDropData playListDragDropData = (PlayListDragDropData)dragObject;
if (playListDragDropData.PlayListDragDropMode == PlayListDragDropMode.Albums)
{
InsertListViewItemsAlbum(hoverListViewItem, playListDragDropData.Id);
}
}
}
this.m_bPlayListHasChanged = true;
UpdateViewState();
}
}
private void PlayTracks(BSE.Platten.Audio.PlayMode eMode)
{
CTrackCollection tracksCollection = new CTrackCollection(this.m_lstvPlayList.Items.Count);
foreach (ListViewItem listViewItem in this.m_lstvPlayList.Items)
{
CTrack track = listViewItem.Tag as CTrack;
if (track != null)
{
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 PlaySelectedTracks(BSE.Platten.Audio.PlayMode eMode)
{
CTrackCollection tracksCollection = new CTrackCollection(this.m_lstvPlayList.SelectedItems.Count);
foreach (ListViewItem listViewItem in this.m_lstvPlayList.SelectedItems)
{
CTrack track = listViewItem.Tag as CTrack;
if (track != null)
{
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 InsertListViewItems(ListViewItem hoverListViewItem, BSE.Windows.Forms.CDraggedListViewObjects draggedListViewObjects)
{
if (hoverListViewItem == null)
{
for (int i = 0; i < draggedListViewObjects.DragObjects.Count; i++)
{
if (this.m_lstvPlayList == 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_lstvPlayList.Items.Add(listViewItem);
}
}
}
}
}
else
{
int iHoverIndex = hoverListViewItem.Index;
if (this.m_lstvPlayList == 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_lstvPlayList.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 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_lstvPlayList.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_lstvPlayList.Items.Insert(iHoverIndex, listViewItem);
}
}
}
}
}
}
catch (Exception exception)
{
GlobalizedMessageBox.Show(this,exception.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private static ListViewItem CreateListViewItem(CTrack track)
{
ListViewItem listViewItem = null;
if (track != null)
{
listViewItem = new ListViewItem();
listViewItem.Text = track.Title;
listViewItem.SubItems.Add(track.Album);
listViewItem.SubItems.Add(track.Interpret);
listViewItem.SubItems.Add(track.Duration.ToString());
listViewItem.SubItems.Add(track.Genre);
listViewItem.SubItems.Add(track.Year.ToString(CultureInfo.InvariantCulture));
listViewItem.SubItems.Add(track.FileFullName);
listViewItem.Tag = track;
}
return listViewItem;
}
private void UpdateViewState()
{
if (this.m_currentPlayList != null)
{
this.m_btnSave.Enabled = true;
this.m_btnDelete.Enabled = true;
}
else
{
this.m_btnSave.Enabled = false;
this.m_btnDelete.Enabled = false;
}
if (this.m_lstvPlayList.Items.Count > 0)
{
this.m_btnPlay.Enabled = true;
}
else
{
this.m_btnPlay.Enabled = false;
}
}
private void LoadSettings()
{
if (this.m_settings != null)
{
PlayListSettingsData playListSettingsData = new PlayListSettingsData();
playListSettingsData = playListSettingsData.LoadSettings(this, this.m_settings, playListSettingsData) as PlayListSettingsData;
if (playListSettingsData != null)
{
//-------------------------------------------------------------------------------
// Spaltenbreiten f�r die PlayList ListView neu setzen.
//-------------------------------------------------------------------------------
int iSizeLvwPlayList = this.m_lstvPlayList.Columns.Count;
int[] columnWidthsLvwPlayList = playListSettingsData.ColumnWidthsLvwPlayList;
if (columnWidthsLvwPlayList != null)
{
int iSizeIntArray = columnWidthsLvwPlayList.Length;
for (int i = 0; i < iSizeLvwPlayList; ++i)
{
if (i < iSizeIntArray)
{
this.m_lstvPlayList.Columns[i].Width = columnWidthsLvwPlayList[i];
}
else
{
break;
}
}
}
}
}
}
private void SaveSettings()
{
if (this.m_settings != null)
{
PlayListSettingsData playListSettingsData = new PlayListSettingsData();
//-------------------------------------------------------------------------------
// Spaltenbreiten f�r die PlayList ListView speichern.
//-------------------------------------------------------------------------------
int iSizeLvwPlayList = this.m_lstvPlayList.Columns.Count;
int[] columnWidthsLvwPlayList = new int[iSizeLvwPlayList];
for (int i = 0; i < iSizeLvwPlayList; ++i)
{
columnWidthsLvwPlayList[i] = this.m_lstvPlayList.Columns[i].Width;
}
playListSettingsData.ColumnWidthsLvwPlayList = columnWidthsLvwPlayList;
playListSettingsData.SaveSettings(this, this.m_settings, playListSettingsData);
}
}
#endregion
}
public class PlayListSettingsData : BaseControlSettingsData
{
#region Fields
/// <summary>
/// Spaltenbreiten f�r die PlayList ListView
/// </summary>
private int[] m_iarColumnWidthsLvwPlayList;
#endregion
#region Properties
/// <summary>
/// Spaltenbreiten f�r die PlayList ListView
/// </summary>
public int[] ColumnWidthsLvwPlayList
{
get { return this.m_iarColumnWidthsLvwPlayList; }
set { this.m_iarColumnWidthsLvwPlayList = value; }
}
#endregion
}
}