Skip to content

Commit

Permalink
WinGui: Fix a potential crash when working with DVD discs during Auto…
Browse files Browse the repository at this point in the history
…Naming Fixes HandBrake#2837

Fix scrollbar not dispaying on source selection. Fixes HandBrake#2843
  • Loading branch information
sr55 committed May 10, 2020
1 parent 41a900a commit f3a438d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 72 deletions.
2 changes: 1 addition & 1 deletion win/CS/HandBrakeWPF/Controls/SourceSelection.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
Expand Down
32 changes: 32 additions & 0 deletions win/CS/HandBrakeWPF/Services/Scan/Model/Source.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
namespace HandBrakeWPF.Services.Scan.Model
{
using System.Collections.Generic;
using System.IO;
using System.Xaml;
using System.Xml.Serialization;

using HandBrakeWPF.Model;
using HandBrakeWPF.Utilities;

/// <summary>
/// An object representing a scanned DVD
/// </summary>
Expand All @@ -38,6 +43,8 @@ public Source()
[XmlIgnore]
public List<Title> Titles { get; set; }

public string SourceName { get; private set; }

/// <summary>
/// Copy this Source to another Source Model
/// </summary>
Expand All @@ -48,6 +55,31 @@ public void CopyTo(Source source)
{
source.Titles = this.Titles;
source.ScanPath = this.ScanPath;

// Scan Path is a File.
if (File.Exists(this.ScanPath))
{
this.SourceName = Path.GetFileNameWithoutExtension(this.ScanPath);
}

// Scan Path is a folder.
if (Directory.Exists(this.ScanPath))
{
// Check to see if it's a Drive. If yes, use the volume label.
foreach (DriveInformation item in DriveUtilities.GetDrives())
{
if (item.RootDirectory.Contains(this.ScanPath.Replace("\\\\", "\\")))
{
this.SourceName = item.VolumeLabel;
}
}

// Otherwise, it may be a path of files.
if (string.IsNullOrEmpty(this.SourceName))
{
this.SourceName = Path.GetFileName(this.ScanPath);
}
}
}
}
}
13 changes: 13 additions & 0 deletions win/CS/HandBrakeWPF/Services/Scan/Model/Title.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ public Title()
/// </summary>
public string SourceName { get; set; }

public string DisplaySourceName
{
get
{
if (!string.IsNullOrEmpty(this.SourceName))
{
return Path.GetFileNameWithoutExtension(this.SourceName);
}

return null;
}
}

public string SourceDisplayName
{
get
Expand Down
2 changes: 1 addition & 1 deletion win/CS/HandBrakeWPF/Utilities/DriveUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static List<DriveInformation> GetDrives()
{
Id = id,
VolumeLabel = curDrive.VolumeLabel,
RootDirectory = curDrive.RootDirectory.ToString()
RootDirectory = curDrive.RootDirectory?.ToString()
});
id++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@ public interface IQueueSelectionViewModel
/// <param name="scannedSource">
/// The scanned source.
/// </param>
/// <param name="sourceName">
/// The source Name.
/// </param>
/// <param name="addAction">
/// The add To Queue action
/// </param>
/// <param name="preset">
/// The preset.
/// </param>
void Setup(Source scannedSource, string sourceName, Action<IEnumerable<SelectionTitle>> addAction, Preset preset);
void Setup(Source scannedSource, Action<IEnumerable<SelectionTitle>> addAction, Preset preset);
}
}
84 changes: 22 additions & 62 deletions win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,50 +389,6 @@ public string SourceLabel
}
}

/// <summary>
/// Gets SourceName.
/// </summary>
public string SourceName
{
get
{
// Sanity Check
if (this.ScannedSource == null || this.ScannedSource.ScanPath == null || this.selectedTitle == null)
{
return string.Empty;
}

if (File.Exists(this.ScannedSource.ScanPath)) // Scan Path is a File.
{
return Path.GetFileNameWithoutExtension(this.ScannedSource.ScanPath);
}

if (Directory.Exists(this.ScannedSource.ScanPath)) // Scan Path is a folder.
{
// Check to see if it's a Drive. If yes, use the volume label.
foreach (DriveInformation item in DriveUtilities.GetDrives())
{
if (item.RootDirectory.Contains(this.ScannedSource.ScanPath.Replace("\\\\", "\\")))
{
return item.VolumeLabel;
}
}

// Otherwise, it may be a path of files.
if (!string.IsNullOrEmpty(this.selectedTitle.SourceName) && File.Exists(this.selectedTitle.SourceName)) // Selected Title is a file
{
return Path.GetFileNameWithoutExtension(this.selectedTitle.SourceName);
}
else if (Directory.Exists(this.selectedTitle.SourceName)) // Selected Title is a structured source.
{
return Path.GetFileName(this.ScannedSource.ScanPath);
}
}

return null;
}
}

/// <summary>
/// Gets RangeMode.
/// </summary>
Expand Down Expand Up @@ -606,6 +562,7 @@ public Title SelectedTitle
{
return this.selectedTitle;
}

set
{
if (!Equals(this.selectedTitle, value))
Expand All @@ -618,7 +575,7 @@ public Title SelectedTitle
}

// Use the Path on the Title, or the Source Scan path if one doesn't exist.
this.SourceLabel = this.SourceName;
this.SourceLabel = this.ScannedSource?.SourceName ?? this.SelectedTitle?.DisplaySourceName;
this.CurrentTask.Source = !string.IsNullOrEmpty(this.selectedTitle.SourceName) ? this.selectedTitle.SourceName : this.ScannedSource.ScanPath;
this.CurrentTask.Title = value.TitleNumber;
this.NotifyOfPropertyChange(() => this.StartEndRangeItems);
Expand All @@ -640,7 +597,7 @@ public Title SelectedTitle
{
if (this.userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNameFormat) != null)
{
this.Destination = AutoNameHelper.AutoName(this.CurrentTask, this.SourceName, this.selectedPreset);
this.Destination = AutoNameHelper.AutoName(this.CurrentTask, this.ScannedSource?.SourceName ?? this.SelectedTitle?.DisplaySourceName, this.selectedPreset);
}
}

Expand Down Expand Up @@ -697,7 +654,7 @@ public long SelectedStartPoint
if (this.SelectedPointToPoint == PointToPointMode.Chapters && this.userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNameFormat) != null &&
this.userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNameFormat).Contains(Constants.Chapters))
{
this.Destination = AutoNameHelper.AutoName(this.CurrentTask, this.SourceName, this.selectedPreset);
this.Destination = AutoNameHelper.AutoName(this.CurrentTask, this.ScannedSource?.SourceName ?? this.SelectedTitle?.DisplaySourceName, this.selectedPreset);
}
}

Expand Down Expand Up @@ -727,7 +684,7 @@ public long SelectedEndPoint
if (this.SelectedPointToPoint == PointToPointMode.Chapters && this.userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNameFormat) != null &&
this.userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNameFormat).Contains(Constants.Chapters))
{
this.Destination = AutoNameHelper.AutoName(this.CurrentTask, this.SourceName, this.selectedPreset);
this.Destination = AutoNameHelper.AutoName(this.CurrentTask, this.ScannedSource?.SourceName ?? this.SelectedTitle?.DisplaySourceName, this.selectedPreset);
}

if (this.SelectedStartPoint > this.SelectedEndPoint && this.SelectedPointToPoint == PointToPointMode.Chapters)
Expand Down Expand Up @@ -1468,23 +1425,26 @@ public void AddSelectionToQueue()
Window window = Application.Current.Windows.Cast<Window>().FirstOrDefault(x => x.GetType() == typeof(QueueSelectionViewModel));
IQueueSelectionViewModel viewModel = IoC.Get<IQueueSelectionViewModel>();

viewModel.Setup(this.ScannedSource, this.SourceName, (tasks) =>
{
foreach (SelectionTitle title in tasks)
viewModel.Setup(
this.ScannedSource,
(tasks) =>
{
this.SelectedTitle = title.Title;
var addError = this.AddToQueue(true);
if (addError != null)
foreach (SelectionTitle title in tasks)
{
MessageBoxResult result = this.errorService.ShowMessageBox(addError.Message + Environment.NewLine + Environment.NewLine + Resources.Main_ContinueAddingToQueue, addError.Header, MessageBoxButton.YesNo, addError.ErrorType);

if (result == MessageBoxResult.No)
this.SelectedTitle = title.Title;
var addError = this.AddToQueue(true);
if (addError != null)
{
break;
MessageBoxResult result = this.errorService.ShowMessageBox(addError.Message + Environment.NewLine + Environment.NewLine + Resources.Main_ContinueAddingToQueue, addError.Header, MessageBoxButton.YesNo, addError.ErrorType);

if (result == MessageBoxResult.No)
{
break;
}
}
}
}
}, this.selectedPreset);
},
this.selectedPreset);

if (window != null)
{
Expand Down Expand Up @@ -2205,7 +2165,7 @@ private void QueueEditAction(bool successful, Source scannedSource)

// Cleanup
this.ShowStatusWindow = false;
this.SourceLabel = this.SourceName;
this.SourceLabel = this.ScannedSource?.SourceName ?? this.SelectedTitle?.DisplaySourceName;
this.StatusLabel = Resources.Main_ScanCompleted;
});
}
Expand Down Expand Up @@ -2422,7 +2382,7 @@ private void ScanCompleted(object sender, ScanCompletedEventArgs e)

if (e.Successful)
{
this.SourceLabel = this.SourceName;
this.SourceLabel = this.ScannedSource?.SourceName ?? this.SelectedTitle?.DisplaySourceName;
this.StatusLabel = Resources.Main_ScanCompleted;
}
else if (e.Cancelled)
Expand Down
6 changes: 2 additions & 4 deletions win/CS/HandBrakeWPF/ViewModels/QueueSelectionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,13 @@ public void Close()
/// <param name="scannedSource">
/// The scanned source.
/// </param>
/// <param name="srcName">
/// The src Name.
/// </param>
/// <param name="addAction">
/// The add Action.
/// </param>
/// <param name="preset">
/// The preset.
/// </param>
public void Setup(Source scannedSource, string srcName, Action<IEnumerable<SelectionTitle>> addAction, Preset preset)
public void Setup(Source scannedSource, Action<IEnumerable<SelectionTitle>> addAction, Preset preset)
{
this.TitleList.Clear();
this.addToQueue = addAction;
Expand All @@ -257,6 +254,7 @@ public void Setup(Source scannedSource, string srcName, Action<IEnumerable<Selec

foreach (Title item in titles)
{
string srcName = scannedSource?.SourceName ?? item.DisplaySourceName;
SelectionTitle title = new SelectionTitle(item, srcName) { IsSelected = true };
TitleList.Add(title);
}
Expand Down

0 comments on commit f3a438d

Please sign in to comment.