// --------------------------------------------------------------------------------------------------------------------
//
// 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.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
{
///
/// Gets or sets SourceChapterList.
///
private ObservableCollection SourceChapterList { get; set; }
///
/// Initializes a new instance of the class.
///
///
/// The window manager.
///
///
/// The user Setting Service.
///
public ChaptersViewModel(IWindowManager windowManager, IUserSettingService userSettingService)
{
this.Chapters = new ObservableCollection();
}
///
/// Gets or sets State.
///
public ObservableCollection Chapters { get; set; }
///
/// Gets or sets a value indicating whether chapter markers are enabled.
///
public bool IncludeChapterMarkers { get; set; }
///
/// 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 SourceChapterList)
{
ChapterMarker marker = new ChapterMarker(chapter.ChapterNumber, chapter.ChapterName);
this.Chapters.Add(marker);
}
}
///
/// 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;
}
StreamWriter 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
///
private void Import()
{
VistaOpenFileDialog 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
{
StreamReader 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 Chapters)
{
string chapterName;
chapterMap.TryGetValue(item.ChapterNumber, out chapterName);
item.ChapterName = chapterName;
// TODO force a fresh of this property
}
}
///
/// Export a CSV file.
///
private void Export()
{
VistaSaveFileDialog saveFileDialog = new VistaSaveFileDialog { Filter = "Csv File|*.csv", DefaultExt = "csv", CheckPathExists = true };
saveFileDialog.ShowDialog();
if (!string.IsNullOrEmpty(saveFileDialog.FileName))
{
this.ExportChaptersToCSV(saveFileDialog.FileName);
}
}
}
}