PageRenderTime 36ms CodeModel.GetById 12ms app.highlight 17ms RepoModel.GetById 2ms app.codeStats 1ms

/Application/GUI/Controls/Navigation.xaml.cs

http://yet-another-music-application.googlecode.com/
C# | 615 lines | 394 code | 71 blank | 150 comment | 98 complexity | f66766adee2bc567a95df3c90a979b73 MD5 | raw file
  1/**
  2 * Navigation.xaml.cs
  3 * 
  4 * The left-hand navigation tree.
  5 * 
  6 * * * * * * * * *
  7 * 
  8 * This code is part of the Stoffi Music Player Project.
  9 * Visit our website at: stoffiplayer.com
 10 *
 11 * This program is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU General Public License
 13 * as published by the Free Software Foundation; either version
 14 * 3 of the License, or (at your option) any later version.
 15 * 
 16 * See stoffiplayer.com/license for more information.
 17 **/
 18
 19using System;
 20using System.Collections;
 21using System.Collections.Generic;
 22using System.Linq;
 23using System.Text;
 24using System.Windows;
 25using System.Windows.Controls;
 26using System.Windows.Data;
 27using System.Windows.Documents;
 28using System.Windows.Input;
 29using System.Windows.Media;
 30using System.Windows.Media.Imaging;
 31using System.Windows.Navigation;
 32using System.Windows.Shapes;
 33
 34namespace Stoffi
 35{
 36	/// <summary>
 37	/// Interaction logic for Navigation.xaml
 38	/// </summary>
 39	public partial class Navigation : UserControl
 40	{
 41		#region Members
 42
 43		public ContextMenu playlistMenu;
 44		public List<TreeViewItem> historyList = new List<TreeViewItem>();
 45		public MenuItem playlistMenuSave = new MenuItem();
 46		public MenuItem playlistMenuRename = new MenuItem();
 47		public MenuItem playlistMenuRemove = new MenuItem();
 48		public MenuItem playlistMenuShare = new MenuItem();
 49		private EditableTextBlock rightClickedEtb;
 50		private StoffiWindow parent;
 51
 52		#endregion
 53
 54		#region Properties
 55
 56		/// <summary>
 57		/// Gets or sets the queue of tracks to add to a playlist after it has been created
 58		/// </summary>
 59		public List<object> AddToPlaylistQueue { get; set; }
 60
 61		/// <summary>
 62		/// TODO: Remove this!
 63		/// </summary>
 64		public StoffiWindow ParentWindow
 65		{
 66			get { return parent; }
 67			set { parent = value; }
 68		}
 69
 70		#endregion
 71
 72		#region Constructor
 73
 74		/// <summary>
 75		/// Creates a navigation class
 76		/// </summary>
 77		public Navigation()
 78		{
 79			U.L(LogLevel.Debug, "NAVIGATION", "Initialize");
 80			InitializeComponent();
 81			U.L(LogLevel.Debug, "NAVIGATION", "Initialized");
 82			SettingsManager.PropertyChanged += new PropertyChangedWithValuesEventHandler(SettingsManager_PropertyChanged);
 83			NowPlayingIcon.Source = Utilities.GetIcoImage("pack://application:,,,/GUI/Images/Icons/NoteSingle.ico", 16, 16);
 84			MusicIcon.Source = Utilities.GetIcoImage("pack://application:,,,/GUI/Images/Icons/NoteSingle.ico", 16, 16);
 85			FilesIcon.Source = Utilities.GetIcoImage("pack://application:,,,/GUI/Images/Icons/FileAudio.ico", 16, 16);
 86			YoutubeIcon.Source = Utilities.GetIcoImage("pack://application:,,,/GUI/Images/Icons/YouTube.ico", 16, 16);
 87			QueueIcon.Source = Utilities.GetIcoImage("pack://application:,,,/GUI/Images/Icons/Queue.ico", 16, 16);
 88			HistoryIcon.Source = Utilities.GetIcoImage("pack://application:,,,/GUI/Images/Icons/Clock.ico", 16, 16);
 89			PlaylistsIcon.Source = Utilities.GetIcoImage("pack://application:,,,/GUI/Images/Icons/DiscAudio.ico", 16, 16);
 90			CreateNewIcon.Source = Utilities.GetIcoImage("pack://application:,,,/GUI/Images/Icons/DiscAudio.ico", 16, 16);
 91			U.L(LogLevel.Debug, "NAVIGATION", "Created");
 92		}
 93
 94		#endregion
 95
 96		#region Methods
 97
 98		#region Override
 99
100		/// <summary>
101		/// 
102		/// </summary>
103		/// <param name="e"></param>
104		protected override void OnInitialized(EventArgs e)
105		{
106			BorderThickness = new Thickness(0);
107
108			playlistMenu = new ContextMenu();
109
110			playlistMenuSave = new MenuItem();
111			playlistMenuSave.Header = U.T("MenuSavePlaylist");
112			playlistMenuSave.Click += SavePlaylist_Clicked;
113			playlistMenu.Items.Add(playlistMenuSave);
114
115			playlistMenuRename = new MenuItem();
116			playlistMenuRename.Header = U.T("MenuRenamePlaylist");
117			playlistMenuRename.Click += RenamePlaylist_Clicked;
118			playlistMenu.Items.Add(playlistMenuRename);
119
120			playlistMenuRemove = new MenuItem();
121			playlistMenuRemove.Header = U.T("MenuRemovePlaylist");
122			playlistMenuRemove.Click += RemovePlaylist_Clicked;
123			playlistMenu.Items.Add(playlistMenuRemove);
124
125			playlistMenuShare = new MenuItem();
126			playlistMenuShare.Header = U.T("MenuSharePlaylist");
127			playlistMenuShare.Click += SharePlaylist_Clicked;
128			playlistMenu.Items.Add(playlistMenuShare);
129
130			base.OnInitialized(e);
131		}
132
133		#endregion
134
135		#region Public
136
137		/// <summary>
138		/// 
139		/// </summary>
140		/// <param name="text"></param>
141		public void SwitchNavigation(string text)
142		{
143			if (text == null) text = "Files";
144
145			ParentWindow.MainContainer.Children.Remove(ParentWindow.ControlPanel);
146			ParentWindow.MusicPanel.Visibility = System.Windows.Visibility.Visible;
147			ParentWindow.PlaybackControls.Search.Box.IsEnabled = true;
148
149			if (text.StartsWith("Playlist:"))
150			{
151				String playlistName = text.Split(new[]{':'},2)[1];
152				foreach (TreeViewItem tvi in Playlists.Items)
153				{
154					PlaylistData pl = Tvi2Pl(tvi);
155					if (pl != null && pl.Name == playlistName)
156					{
157						tvi.Focus();
158						break;
159					}
160				}
161			}
162			else if (text == "History")
163				History.Focus();
164			else if (text == "Queue")
165				Queue.Focus();
166			else if (text == "Files")
167				Files.Focus();
168			else if (text == "NowPlaying")
169				NowPlaying.Focus();
170			else if (text == "YouTube")
171				Youtube.Focus();
172		}
173
174		#endregion
175
176		#region Private
177
178		/// <summary>
179		/// 
180		/// </summary>
181		/// <param name="tvi"></param>
182		/// <returns></returns>
183		private PlaylistData Tvi2Pl(TreeViewItem tvi)
184		{
185			StackPanel sp = tvi.Header as StackPanel;
186			EditableTextBlock etb = null;
187
188			foreach (object child in sp.Children)
189				if (child is EditableTextBlock)
190					etb = child as EditableTextBlock;
191
192			if (etb == null) return null;
193
194			return PlaylistManager.FindPlaylist(etb.Text);
195		}
196
197		/// <summary>
198		/// 
199		/// </summary>
200		/// <param name="tl"></param>
201		/// <param name="header"></param>
202		/// <param name="tvi"></param>
203		/// <param name="name"></param>
204		/// <param name="time"></param>
205		private void ToggleTrackLists(ViewDetails tl, string header, TreeViewItem tvi, string name, int time)
206		{
207			ParentWindow.NowPlayingContainer.Visibility = System.Windows.Visibility.Collapsed;
208			ParentWindow.YouTubeContainer.Visibility = System.Windows.Visibility.Collapsed;
209			ParentWindow.TrackContainer.Visibility = System.Windows.Visibility.Visible;
210			ParentWindow.FileTracks.Visibility = ParentWindow.FileTracks == tl ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
211			ParentWindow.HistoryTracks.Visibility = ParentWindow.HistoryTracks == tl ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
212			ParentWindow.QueueTracks.Visibility = ParentWindow.QueueTracks == tl ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
213			foreach (DictionaryEntry pltl in ParentWindow.PlaylistTrackLists)
214				((ViewDetails)pltl.Value).Visibility = ((ViewDetails)pltl.Value) == tl ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
215
216			ParentWindow.PlaybackControls.Search.Box.IsEnabled = true;
217			
218			//if (name != "NowPlaying")
219				SettingsManager.CurrentSelectedNavigation = name;
220
221			if (tl != null)
222			{
223				int tracks = 0;
224				if (tl == ParentWindow.FileTracks)
225						tracks = SettingsManager.FileTracks.Count;
226				else if (tl == ParentWindow.HistoryTracks)
227						tracks = SettingsManager.HistoryTracks.Count;
228				else if (tl == ParentWindow.QueueTracks)
229						tracks = SettingsManager.QueueTracks.Count;
230				else
231				{
232					foreach (DictionaryEntry pltl in ParentWindow.PlaylistTrackLists)
233					{
234						if (tl == (ViewDetails)pltl.Value)
235						{
236							PlaylistData p = PlaylistManager.FindPlaylist((String)pltl.Key);
237							if (p != null)
238							{
239								tracks = p.Tracks.Count;
240								break;
241							}
242						}
243					}
244				}
245				ParentWindow.InfoPaneTracks.Text = tracks.ToString() + " " + U.T("HeaderTracks");
246				ParentWindow.InfoPaneDuration.Text = U.TimeSpanToLongString(new TimeSpan(0, 0, time));
247				ParentWindow.InfoPaneTitle.Text = header;
248
249				ViewDetailsConfig vdc;
250				if (name == "History")
251					vdc = SettingsManager.HistoryListConfig;
252				else if (name == "Queue")
253					vdc = SettingsManager.QueueListConfig;
254				else if (name.StartsWith("Playlist:"))
255					vdc = PlaylistManager.FindPlaylist(name.Split(new[]{':'},2)[1]).ListConfig;
256				else
257					vdc = SettingsManager.FileListConfig;
258
259				if (vdc.Filter == null || vdc.Filter == "" || vdc.Filter == U.T("PlaybackSearch"))
260				{
261					ParentWindow.PlaybackControls.Search.IsActive = false;
262				}
263				else
264				{
265					ParentWindow.PlaybackControls.Search.IsActive = true;
266					ParentWindow.PlaybackControls.Search.Text = vdc.Filter;
267				}
268				tl.Filter = vdc.Filter;
269			}
270
271			tvi.Focus();
272			historyList.Remove(tvi);
273			historyList.Add(tvi);
274		}
275
276		#endregion
277
278		#region Event Handlers
279
280		/// <summary>
281		/// Invoked when a property of the settings manager changes
282		/// </summary>
283		/// <param name="sender">The sender of the event</param>
284		/// <param name="e">The event data</param>
285		private void SettingsManager_PropertyChanged(object sender, PropertyChangedWithValuesEventArgs e)
286		{
287			switch (e.PropertyName)
288			{
289				case "CurrentSelectedNavigation":
290					SwitchNavigation(SettingsManager.CurrentSelectedNavigation);
291					break;
292			}
293		}
294
295		/// <summary>
296		/// 
297		/// </summary>
298		/// <param name="sender"></param>
299		/// <param name="e"></param>
300		private void Navigation_Loaded(object sender, RoutedEventArgs e)
301		{
302			if (SettingsManager.CurrentSelectedNavigation.StartsWith("Playlist:"))
303				if (PlaylistManager.FindPlaylist(SettingsManager.CurrentSelectedNavigation.Split(new[] { ':' }, 2)[1]) == null)
304					SettingsManager.CurrentSelectedNavigation = "Files";
305
306			if (System.Windows.Forms.VisualStyles.VisualStyleInformation.DisplayName == "")
307			{
308				NavigationTree.ItemContainerStyle = (Style)TryFindResource("ClassicNavigationStyle");
309				Library.ItemContainerStyle = (Style)TryFindResource("ClassicNavigationStyle");
310				Playlists.ItemContainerStyle = (Style)TryFindResource("ClassicNavigationStyle");
311			}
312			else
313			{
314				NavigationTree.ItemContainerStyle = (Style)TryFindResource("AeroNavigationStyle");
315				Library.ItemContainerStyle = (Style)TryFindResource("AeroNavigationStyle");
316				Playlists.ItemContainerStyle = (Style)TryFindResource("AeroNavigationStyle");
317			}
318
319			AddToPlaylistQueue = new List<object>();
320		}
321
322		/// <summary>
323		/// 
324		/// </summary>
325		/// <param name="sender"></param>
326		/// <param name="e"></param>
327		private void NowPlaying_Selected(object sender, RoutedEventArgs e)
328		{
329			ToggleTrackLists(null, "", (TreeViewItem)sender, "NowPlaying", 0);
330			ParentWindow.TrackContainer.Visibility = System.Windows.Visibility.Collapsed;
331			ParentWindow.YouTubeContainer.Visibility = System.Windows.Visibility.Collapsed;
332			ParentWindow.NowPlayingContainer.Visibility = System.Windows.Visibility.Visible;
333		}
334
335		/// <summary>
336		/// 
337		/// </summary>
338		/// <param name="sender"></param>
339		/// <param name="e"></param>
340		private void Files_Selected(object sender, RoutedEventArgs e)
341		{
342			ToggleTrackLists(ParentWindow.FileTracks, U.T("NavigationFilesTitle"), (TreeViewItem)sender, "Files", (int)ParentWindow.LibraryTime);
343		}
344
345		/// <summary>
346		/// 
347		/// </summary>
348		/// <param name="sender"></param>
349		/// <param name="e"></param>
350		private void Youtube_Selected(object sender, RoutedEventArgs e)
351		{
352			ToggleTrackLists(null, "", (TreeViewItem)sender, "YouTube", 0);
353			ParentWindow.TrackContainer.Visibility = System.Windows.Visibility.Collapsed;
354			ParentWindow.NowPlayingContainer.Visibility = System.Windows.Visibility.Collapsed;
355			ParentWindow.YouTubeContainer.Visibility = System.Windows.Visibility.Visible;
356		}
357
358		/// <summary>
359		/// 
360		/// </summary>
361		/// <param name="sender"></param>
362		/// <param name="e"></param>
363		private void Queue_Selected(object sender, RoutedEventArgs e)
364		{
365			ToggleTrackLists(ParentWindow.QueueTracks, U.T("NavigationQueueTitle"), (TreeViewItem)sender, "Queue", (int)ParentWindow.QueueTime);
366		}
367
368		/// <summary>
369		/// 
370		/// </summary>
371		/// <param name="sender"></param>
372		/// <param name="e"></param>
373		private void History_Selected(object sender, RoutedEventArgs e)
374		{
375			ToggleTrackLists(ParentWindow.HistoryTracks, U.T("NavigationHistoryTitle"), (TreeViewItem)sender, "History", (int)ParentWindow.HistoryTime);
376		}
377
378		/// <summary>
379		/// 
380		/// </summary>
381		/// <param name="sender"></param>
382		/// <param name="e"></param>
383		public void Playlists_Selected(object sender, RoutedEventArgs e)
384		{
385			TreeViewItem tvi = sender as TreeViewItem;
386			if (tvi.IsSelected)
387			{
388				historyList.Remove(tvi);
389				historyList.Add(tvi);
390			}
391		}
392
393		/// <summary>
394		/// 
395		/// </summary>
396		/// <param name="sender"></param>
397		/// <param name="e"></param>
398		public void Playlist_Selected(object sender, RoutedEventArgs e)
399		{
400			PlaylistData playlist = Tvi2Pl(sender as TreeViewItem);
401			if (playlist == null) return;
402
403			foreach (DictionaryEntry pltl in ParentWindow.PlaylistTrackLists)
404			{
405				if ((string)pltl.Key == playlist.Name)
406				{
407					ToggleTrackLists((ViewDetails)pltl.Value, U.T("NavigationPlaylistTitle") + " '" + playlist.Name + "'", (TreeViewItem)sender, "Playlist:" + playlist.Name, (int)playlist.Time);
408					((TreeViewItem)sender).Focus();
409					SettingsManager.CurrentSelectedNavigation = "Playlist:" + playlist.Name;
410					break;
411				}
412			}
413		}
414
415		/// <summary>
416		/// 
417		/// </summary>
418		/// <param name="sender"></param>
419		/// <param name="e"></param>
420		public void Playlist_Edited(object sender, EditableTextBlockEventArgs e)
421		{
422			PlaylistManager.RenamePlaylist(e.OldText, e.NewText);
423		}
424
425		/// <summary>
426		/// 
427		/// </summary>
428		/// <param name="sender"></param>
429		/// <param name="e"></param>
430		private void CreateNewPlaylist_Selected(object sender, RoutedEventArgs e)
431		{
432			TreeViewItem item = sender as TreeViewItem;
433			StackPanel sp = item.Header as StackPanel;
434			EditableTextBlock etb = sp.Children[1] as EditableTextBlock;
435
436			if (etb.IsEditable)
437				etb.IsInEditMode = true;
438		}
439
440		/// <summary>
441		/// 
442		/// </summary>
443		/// <param name="sender"></param>
444		/// <param name="e"></param>
445		private void CreateNewPlaylist_Edited(object sender, EditableTextBlockEventArgs e)
446		{
447			if (e.NewText != U.T("NavigationCreateNew") && e.NewText != "")
448			{
449				List<object> addQueue = AddToPlaylistQueue;
450				PlaylistManager.CreatePlaylist(e.NewText);
451				if (addQueue.Count > 0)
452				{
453					PlaylistManager.AddToPlaylist(addQueue, e.NewText);
454					addQueue.Clear();
455				}
456			}
457			else
458			{
459				CreateNewPlaylist_Canceled(e);
460			}
461		}
462
463		/// <summary>
464		/// 
465		/// </summary>
466		/// <param name="e"></param>
467		private void CreateNewPlaylist_Canceled(EventArgs e)
468		{
469			if (historyList.Count > 0)
470				historyList.Last<TreeViewItem>().Focus();
471
472			else if (Playlists.Items.Count > 1)
473				((TreeViewItem)Playlists.Items[0]).Focus();
474
475			else
476				Library.Focus();
477		}
478
479		/// <summary>
480		/// 
481		/// </summary>
482		/// <param name="sender"></param>
483		/// <param name="e"></param>
484		public void RemovePlaylist_Clicked(object sender, RoutedEventArgs e)
485		{
486			if (rightClickedEtb != null)
487			{
488				PlaylistData pld = PlaylistManager.FindPlaylist(rightClickedEtb.Text);
489				if (pld != null)
490				{
491					string title = U.T("MessageDeletePlaylist", "Title");
492					string message = U.T("MessageDeletePlaylist", "Message");
493					message = message.Replace("%name", rightClickedEtb.Text);
494					if (pld.Tracks.Count == 0 || MessageBox.Show(message, title, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
495						PlaylistManager.RemovePlaylist(rightClickedEtb.Text);
496				}
497			}
498		}
499
500		/// <summary>
501		/// 
502		/// </summary>
503		/// <param name="sender"></param>
504		/// <param name="e"></param>
505		public void SavePlaylist_Clicked(object sender, RoutedEventArgs e)
506		{
507			if (rightClickedEtb != null)
508			{
509				try
510				{
511					Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog();
512					dialog.Title = "Save Playlist";
513					dialog.DefaultExt = ".pls";
514					dialog.Filter = "Playlist (*.pls)|*.pls|Playlist (*.m3u)|*.m3u";
515					dialog.FileName = rightClickedEtb.Text;
516					bool result = (bool)dialog.ShowDialog();
517					if (result == true)
518					{
519						PlaylistManager.SavePlaylist(dialog.FileName, rightClickedEtb.Text);
520					}
521				}
522				catch (Exception exc)
523				{
524					MessageBox.Show(U.T("MessageSavingPlaylist", "Message") + ":\n" + exc.Message,
525									U.T("MessageSavingPlaylist", "Title"),
526									MessageBoxButton.OK,
527									MessageBoxImage.Error);
528				}
529			}
530		}
531
532		/// <summary>
533		/// 
534		/// </summary>
535		/// <param name="sender"></param>
536		/// <param name="e"></param>
537		public void RenamePlaylist_Clicked(object sender, RoutedEventArgs e)
538		{
539			if (rightClickedEtb != null && rightClickedEtb.IsEditable)
540				rightClickedEtb.IsInEditMode = true;
541		}
542
543		/// <summary>
544		/// 
545		/// </summary>
546		/// <param name="sender"></param>
547		/// <param name="e"></param>
548		public void SharePlaylist_Clicked(object sender, RoutedEventArgs e)
549		{
550			if (NavigationTree.SelectedItem is TreeViewItem)
551			{
552				TreeViewItem tvi = NavigationTree.SelectedItem as TreeViewItem;
553
554				if (tvi.Header is StackPanel)
555				{
556					StackPanel sp = tvi.Header as StackPanel;
557					EditableTextBlock etb = sp.Children[1] as EditableTextBlock;
558
559					ServiceManager.SharePlaylist(PlaylistManager.FindPlaylist(etb.Text));
560				}
561			}
562		}
563
564		/// <summary>
565		/// 
566		/// </summary>
567		/// <param name="sender"></param>
568		/// <param name="e"></param>
569		public void Playlist_Drop(object sender, DragEventArgs e)
570		{
571			List<object> DraggedItems = e.Data.GetData(typeof(List<object>)) as List<object>;
572			TreeViewItem tvi = sender as TreeViewItem;
573			StackPanel sp = tvi.Header as StackPanel;
574			EditableTextBlock etb = sp.Children[1] as EditableTextBlock;
575
576			if (etb.Text == U.T("NavigationCreateNew"))
577			{
578				AddToPlaylistQueue.Clear();
579				foreach (TrackData track in DraggedItems)
580					AddToPlaylistQueue.Add(track);
581				CreateNewPlaylistETB.IsInEditMode = true;
582			}
583			else
584			{
585				PlaylistManager.AddToPlaylist(DraggedItems, etb.Text);
586			}
587		}
588
589		/// <summary>
590		/// 
591		/// </summary>
592		/// <param name="sender"></param>
593		/// <param name="e"></param>
594		private void NavigationTree_KeyUp(object sender, KeyEventArgs e)
595		{
596			if (e.Key == Key.Delete)
597				RemovePlaylist_Clicked(sender, e);
598		}
599
600		/// <summary>
601		/// Invoked when the user right-clicks on a playlist
602		/// </summary>
603		/// <param name="sender">The sender of the event</param>
604		/// <param name="e">The event data</param>
605		public void Playlist_ContextMenuOpening(object sender, ContextMenuEventArgs e)
606		{
607			if (e.Source is EditableTextBlock)
608				rightClickedEtb = e.Source as EditableTextBlock;
609		}
610
611		#endregion
612
613		#endregion
614	}
615}