// -------------------------------------------------------------------------------------------------------------------- // // This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. // // // The Chapters View Model // // -------------------------------------------------------------------------------------------------------------------- namespace HandBrakeWPF.ViewModels { using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel.Composition; using System.IO; using Caliburn.Micro; using HandBrake.ApplicationServices.Exceptions; using HandBrake.ApplicationServices.Model; using HandBrake.ApplicationServices.Model.Encoding; using HandBrake.ApplicationServices.Parsing; using HandBrake.ApplicationServices.Services.Interfaces; using HandBrakeWPF.ViewModels.Interfaces; using Ookii.Dialogs.Wpf; /// /// The Chapters View Model /// [Export(typeof(IChaptersViewModel))] public class ChaptersViewModel : ViewModelBase, IChaptersViewModel { #region Constants and Fields /// /// The include chapter markers. /// private bool includeChapterMarkers; #endregion #region Constructors and Destructors /// /// Initializes a new instance of the class. /// /// /// The window manager. /// /// /// The user Setting Service. /// public ChaptersViewModel(IWindowManager windowManager, IUserSettingService userSettingService) { this.Chapters = new ObservableCollection(); } #endregion #region Public Properties /// /// Gets or sets State. /// public ObservableCollection Chapters { get; set; } /// /// Gets or sets a value indicating whether chapter markers are enabled. /// public bool IncludeChapterMarkers { get { return this.includeChapterMarkers; } set { this.includeChapterMarkers = value; this.NotifyOfPropertyChange(() => this.IncludeChapterMarkers); } } #endregion #region Properties /// /// Gets or sets SourceChapterList. /// private ObservableCollection SourceChapterList { get; set; } #endregion #region Public Methods /// /// Export a CSV file. /// public void Export() { var saveFileDialog = new VistaSaveFileDialog { Filter = "Csv File|*.csv", DefaultExt = "csv", CheckPathExists = true }; saveFileDialog.ShowDialog(); if (!string.IsNullOrEmpty(saveFileDialog.FileName)) { this.ExportChaptersToCSV(saveFileDialog.FileName); } } /// /// Export the Chapter Markers to a CSV file /// /// /// The filename. /// /// /// Thrown when exporting fails. /// public void ExportChaptersToCSV(string filename) { try { string csv = string.Empty; foreach (ChapterMarker row in this.Chapters) { csv += row.ChapterNumber.ToString(); csv += ","; csv += row.ChapterName.Replace(",", "\\,"); csv += Environment.NewLine; } var file = new StreamWriter(filename); file.Write(csv); file.Close(); file.Dispose(); } catch (Exception exc) { throw new GeneralApplicationException( "Unable to save Chapter Makrers file! ", "Chapter marker names will NOT be saved in your encode.", exc); } } /// /// Import a CSV file /// public void Import() { var dialog = new VistaOpenFileDialog { Filter = "CSV files (*.csv)|*.csv", CheckFileExists = true }; dialog.ShowDialog(); string filename = dialog.FileName; if (string.IsNullOrEmpty(filename)) { return; } IDictionary chapterMap = new Dictionary(); try { var sr = new StreamReader(filename); string csv = sr.ReadLine(); while (csv != null) { if (csv.Trim() != string.Empty) { csv = csv.Replace("\\,", ""); string[] contents = csv.Split(','); int chapter; int.TryParse(contents[0], out chapter); chapterMap.Add(chapter, contents[1].Replace("", ",")); } csv = sr.ReadLine(); } } catch (Exception) { // Do Nothing } // Now iterate over each chatper we have, and set it's name foreach (ChapterMarker item in this.Chapters) { string chapterName; chapterMap.TryGetValue(item.ChapterNumber, out chapterName); item.ChapterName = chapterName; // TODO force a fresh of this property } } /// /// The set preset. /// /// /// The preset. /// /// /// The current Title. /// public void Setup(Preset preset, Title currentTitle) { this.IncludeChapterMarkers = preset.Task.IncludeChapterMarkers; this.SetSourceChapters(currentTitle.Chapters); } /// /// Set the Source Chapters List /// /// /// The source chapters. /// public void SetSourceChapters(IEnumerable sourceChapters) { // Cache the chapters in this screen this.SourceChapterList = new ObservableCollection(sourceChapters); this.Chapters.Clear(); // Then Add new Chapter Markers. foreach (Chapter chapter in this.SourceChapterList) { var marker = new ChapterMarker(chapter.ChapterNumber, chapter.ChapterName); this.Chapters.Add(marker); } } #endregion } }