<ColumnDefinition Width="Auto" />\r
</Grid.ColumnDefinitions>\r
<CheckBox Content="Create chapter markers" IsChecked="{Binding IncludeChapterMarkers, RelativeSource={RelativeSource AncestorType=UserControl}}" Grid.Column="0" VerticalAlignment="Center" />\r
- <Button Content="Import" Grid.Column="2" Width="75" Margin="0,0,10,0" />\r
- <Button Content="Export" Grid.Column="3" Width="75" />\r
+ <Button Content="Import" Name="import" Grid.Column="2" Width="75" Margin="0,0,10,0" Click="Import_Click" />\r
+ <Button Content="Export" Name="export" Grid.Column="3" Width="75" Click="Export_Click" />\r
</Grid>\r
\r
- <DataGrid Grid.Row="2" Margin="10" ItemsSource="{Binding Chapters, RelativeSource={RelativeSource AncestorType=UserControl}}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">\r
- \r
+ <DataGrid Grid.Row="2" Margin="10" ItemsSource="{Binding Chapters, RelativeSource={RelativeSource AncestorType=UserControl}}" \r
+ VerticalAlignment="Stretch" HorizontalAlignment="Stretch" AutoGenerateColumns="False"\r
+ CanUserSortColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False">\r
+ <DataGrid.Columns>\r
+ <DataGridTextColumn Header="Chapter Number" Width="150" Binding="{Binding ChapterNumber}" IsReadOnly="True" />\r
+ <DataGridTextColumn Header="Chapter Name" Width="*" Binding="{Binding ChapterName}" IsReadOnly="False" />\r
+ </DataGrid.Columns>\r
</DataGrid>\r
\r
\r
using System;\r
using System.Collections.Generic;\r
using System.Collections.ObjectModel;\r
+ using System.IO;\r
using System.Windows;\r
using System.Windows.Controls;\r
\r
+ using HandBrake.ApplicationServices.Exceptions;\r
using HandBrake.ApplicationServices.Model.Encoding;\r
+ using HandBrake.ApplicationServices.Parsing;\r
+\r
+ using Ookii.Dialogs.Wpf;\r
\r
/// <summary>\r
/// Interaction logic for ChaptersView.xaml\r
/// </summary>\r
public partial class ChaptersView : UserControl\r
{\r
+ /// <summary>\r
+ /// Gets or sets SourceChapterList.\r
+ /// </summary>\r
+ private ObservableCollection<Chapter> SourceChapterList { get; set; }\r
+\r
/// <summary>\r
/// Initializes a new instance of the <see cref="ChaptersView"/> class.\r
/// </summary>\r
public ChaptersView()\r
{\r
InitializeComponent();\r
+ this.SourceChapterList = new ObservableCollection<Chapter>();\r
}\r
\r
/// <summary>\r
/// <summary>\r
/// Gets or sets State.\r
/// </summary>\r
- public IList<ChapterMarker> Chapters\r
+ public ObservableCollection<ChapterMarker> Chapters\r
{\r
get { return (ObservableCollection<ChapterMarker>)this.GetValue(ChaptersProperty); }\r
set { this.SetValue(ChaptersProperty, value); }\r
}\r
\r
/// <summary>\r
- /// Import from CSV\r
+ /// Set the Source Chapters List\r
+ /// </summary>\r
+ /// <param name="sourceChapters">\r
+ /// The source chapters.\r
+ /// </param>\r
+ public void SetSourceChapters(IEnumerable<Chapter> sourceChapters)\r
+ {\r
+ // Cache the chapters in this screen\r
+ this.SourceChapterList = new ObservableCollection<Chapter>(sourceChapters);\r
+ this.Chapters.Clear();\r
+\r
+ // Then Add new Chapter Markers.\r
+ foreach (Chapter chapter in SourceChapterList)\r
+ {\r
+ ChapterMarker marker = new ChapterMarker(chapter.ChapterNumber, chapter.ChapterName);\r
+ this.Chapters.Add(marker);\r
+ }\r
+ }\r
+\r
+ /// <summary>\r
+ /// Export the Chapter Markers to a CSV file\r
/// </summary>\r
- public void Import()\r
+ /// <param name="filename">\r
+ /// The filename.\r
+ /// </param>\r
+ /// <exception cref="GeneralApplicationException">\r
+ /// Thrown when exporting fails.\r
+ /// </exception>\r
+ public void ExportChaptersToCSV(string filename)\r
{\r
- throw new NotImplementedException("Not Implemented Yet");\r
+ try\r
+ {\r
+ string csv = string.Empty;\r
+\r
+ foreach (ChapterMarker row in this.Chapters)\r
+ {\r
+ csv += row.ChapterNumber.ToString();\r
+ csv += ",";\r
+ csv += row.ChapterName.Replace(",", "\\,");\r
+ csv += Environment.NewLine;\r
+ }\r
+ StreamWriter file = new StreamWriter(filename);\r
+ file.Write(csv);\r
+ file.Close();\r
+ file.Dispose();\r
+ }\r
+ catch (Exception exc)\r
+ {\r
+ throw new GeneralApplicationException("Unable to save Chapter Makrers file! ", "Chapter marker names will NOT be saved in your encode.", exc);\r
+ }\r
+ }\r
+\r
+ /// <summary>\r
+ /// Import a CSV file\r
+ /// </summary>\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The RoutedEventArgs.\r
+ /// </param>\r
+ private void Import_Click(object sender, RoutedEventArgs e)\r
+ {\r
+ VistaOpenFileDialog dialog = new VistaOpenFileDialog { Filter = "CSV files (*.csv)|*.csv", CheckFileExists = true };\r
+ dialog.ShowDialog();\r
+ string filename = dialog.FileName;\r
+\r
+ if (string.IsNullOrEmpty(filename))\r
+ {\r
+ return;\r
+ }\r
+\r
+ IDictionary<int, string> chapterMap = new Dictionary<int, string>();\r
+ try\r
+ {\r
+ StreamReader sr = new StreamReader(filename);\r
+ string csv = sr.ReadLine();\r
+ while (csv != null)\r
+ {\r
+ if (csv.Trim() != string.Empty)\r
+ {\r
+ csv = csv.Replace("\\,", "<!comma!>");\r
+ string[] contents = csv.Split(',');\r
+ int chapter;\r
+ int.TryParse(contents[0], out chapter);\r
+ chapterMap.Add(chapter, contents[1].Replace("<!comma!>", ","));\r
+ }\r
+ csv = sr.ReadLine();\r
+ }\r
+ }\r
+ catch (Exception)\r
+ {\r
+ // Do Nothing\r
+ }\r
+\r
+ // Now iterate over each chatper we have, and set it's name\r
+ foreach (ChapterMarker item in Chapters)\r
+ {\r
+ string chapterName;\r
+ chapterMap.TryGetValue(item.ChapterNumber, out chapterName);\r
+ item.ChapterName = chapterName;\r
+ // TODO force a fresh of this property\r
+ }\r
}\r
\r
/// <summary>\r
- /// Export to CSV\r
+ /// Export a CSV file.\r
/// </summary>\r
- public void Export()\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The RoutedEventArgs.\r
+ /// </param>\r
+ private void Export_Click(object sender, RoutedEventArgs e)\r
{\r
- throw new NotImplementedException("Not Implemented Yet");\r
+ VistaSaveFileDialog saveFileDialog = new VistaSaveFileDialog { Filter = "Csv File|*.csv", DefaultExt = "csv", CheckPathExists = true };\r
+ saveFileDialog.ShowDialog();\r
+ if (!string.IsNullOrEmpty(saveFileDialog.FileName))\r
+ {\r
+ this.ExportChaptersToCSV(saveFileDialog.FileName);\r
+ }\r
}\r
}\r
}\r