using MediaBrowser.Common;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Localization;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Querying;
using MediaBrowser.Model.Serialization;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media.Imaging;
namespace MediaBrowser.ServerApplication
{
///
/// Interaction logic for LibraryExplorer.xaml
///
public partial class LibraryExplorer : Window
{
private readonly ILogger _logger;
private readonly IJsonSerializer _jsonSerializer;
private readonly ILibraryManager _libraryManager;
private readonly IDisplayPreferencesRepository _displayPreferencesManager;
private readonly IItemRepository _itemRepository;
///
/// The current user
///
private User CurrentUser;
///
/// Initializes a new instance of the class.
///
/// The json serializer.
/// The logger.
/// The app host.
/// The user manager.
/// The library manager.
/// The display preferences manager.
public LibraryExplorer(IJsonSerializer jsonSerializer, ILogger logger, IApplicationHost appHost, IUserManager userManager, ILibraryManager libraryManager, IDisplayPreferencesRepository displayPreferencesManager, IItemRepository itemRepo)
{
_logger = logger;
_jsonSerializer = jsonSerializer;
_libraryManager = libraryManager;
_displayPreferencesManager = displayPreferencesManager;
InitializeComponent();
lblVersion.Content = "Version: " + appHost.ApplicationVersion;
foreach (var user in userManager.Users)
ddlProfile.Items.Add(user);
ddlProfile.Items.Insert(0, new User { Name = "Physical" });
ddlProfile.SelectedIndex = 0;
ddlIndexBy.Visibility = ddlSortBy.Visibility = lblIndexBy.Visibility = lblSortBy.Visibility = Visibility.Hidden;
_itemRepository = itemRepo;
}
///
/// Handles the Click event of the btnLoad control.
///
/// The source of the event.
/// The instance containing the event data.
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
}
///
/// Loads the tree.
///
/// Task.
private async Task LoadTree()
{
tvwLibrary.Items.Clear();
lblLoading.Visibility = Visibility.Visible;
//grab UI context so we can update within the below task
var ui = TaskScheduler.FromCurrentSynchronizationContext();
//this whole async thing doesn't really work in this instance since all my work pretty much needs to be on the UI thread...
Cursor = Cursors.Wait;
await Task.Run(() =>
{
IEnumerable children = CurrentUser.Name == "Physical" ? new[] { _libraryManager.RootFolder } : _libraryManager.RootFolder.GetChildren(CurrentUser, true);
children = OrderByName(children, CurrentUser);
foreach (Folder folder in children)
{
var currentFolder = folder;
Task.Factory.StartNew(() =>
{
var prefs = ddlProfile.SelectedItem != null ? _displayPreferencesManager.GetDisplayPreferences(currentFolder.DisplayPreferencesId, (ddlProfile.SelectedItem as User).Id, "LibraryExplorer") ?? new DisplayPreferences { SortBy = ItemSortBy.SortName } : new DisplayPreferences { SortBy = ItemSortBy.SortName };
var node = new TreeViewItem { Tag = currentFolder };
var subChildren = currentFolder.GetChildren(CurrentUser, true);
subChildren = OrderByName(subChildren, CurrentUser);
AddChildren(node, subChildren, CurrentUser);
node.Header = currentFolder.Name + " (" +
node.Items.Count + ")";
tvwLibrary.Items.Add(node);
}, CancellationToken.None, TaskCreationOptions.None, ui);
}
});
lblLoading.Visibility = Visibility.Hidden;
Cursor = Cursors.Arrow;
}
///
/// Orders the name of the by.
///
/// The items.
/// The user.
/// IEnumerable{BaseItem}.
private IEnumerable OrderByName(IEnumerable items, User user)
{
return OrderBy(items, user, ItemSortBy.SortName);
}
///
/// Orders the name of the by.
///
/// The items.
/// The user.
/// IEnumerable{BaseItem}.
private IEnumerable OrderBy(IEnumerable items, User user, string order)
{
return _libraryManager.Sort(items, user, new[] { order }, SortOrder.Ascending);
}
///
/// Adds the children.
///
/// The parent.
/// The children.
/// The user.
private void AddChildren(TreeViewItem parent, IEnumerable children, User user)
{
foreach (var item in children)
{
var node = new TreeViewItem { Tag = item };
var subFolder = item as Folder;
if (subFolder != null)
{
var prefs = _displayPreferencesManager.GetDisplayPreferences(subFolder.DisplayPreferencesId, user.Id, "LibraryExplorer");
AddChildren(node, OrderBy(subFolder.GetChildren(user, true), user, prefs.SortBy), user);
node.Header = item.Name + " (" + node.Items.Count + ")";
}
else
{
node.Header = item.Name;
}
parent.Items.Add(node);
}
}
///
/// TVWs the library_ selected item changed.
///
/// The sender.
/// The e.
private async void tvwLibrary_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs