Skip to content

Commit

Permalink
WinGui: Prompt to create folder when the destination path does not ex…
Browse files Browse the repository at this point in the history
…ist. Fixes HandBrake#394
  • Loading branch information
sr55 committed Nov 16, 2016
1 parent ef33de1 commit b9c5daa
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 21 deletions.
45 changes: 41 additions & 4 deletions win/CS/HandBrakeWPF/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions win/CS/HandBrakeWPF/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ Do you wish to proceed?</value>
<value>You must first set the destination path for the output file before adding to the queue.</value>
</data>
<data name="Main_InvalidDestination" xml:space="preserve">
<value>The entered destination contained illegal characters. You must fix the path and filename before continuing.</value>
<value>The entered destination path contained illegal characters and will not be updated.</value>
</data>
<data name="Preview" xml:space="preserve">
<value>Preview</value>
Expand Down Expand Up @@ -809,7 +809,20 @@ Your preset file will be archived and new one created. You will need to re-creat
<data name="UserSettings_UnableToLoadSolution" xml:space="preserve">
<value>Your user settings file appears to be inaccessible or corrupted. You may have to delete the file and let HandBrake generate a new one.</value>
</data>
<data name="Main_NoPermissionsOnDirectory" xml:space="preserve">
<value>You do not have the appropriate folder permissions to write files into the directory you have chosen.</value>
<data name="Main_NoPermissionsOrMissingDirectory" xml:space="preserve">
<value>The output directory you have chosen either does not exist, or you do not have permissions to write files to it.</value>
</data>
<data name="DirectoryUtils_CreateFolder" xml:space="preserve">
<value>Create Folder?</value>
</data>
<data name="DirectoryUtils_CreateFolderMsg" xml:space="preserve">
<value>The folder you are trying to write to does not exist. Would you like HandBrake to create the following folder?
{0}</value>
</data>
<data name="MainViewModel_UnableToLaunchDestDir" xml:space="preserve">
<value>Unable to launch destination directory.</value>
</data>
<data name="MainViewModel_UnableToLaunchDestDirSolution" xml:space="preserve">
<value>Please check that you have a valid destination directory.</value>
</data>
</root>
20 changes: 19 additions & 1 deletion win/CS/HandBrakeWPF/Utilities/DirectoryUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ namespace HandBrakeWPF.Utilities
{
using System;
using System.IO;
using System.Windows;

using HandBrakeWPF.Properties;
using HandBrakeWPF.Services.Interfaces;

/// <summary>
/// The directory utilities.
Expand Down Expand Up @@ -42,11 +46,25 @@ public static string GetUserStoragePath(bool isNightly)
/// Simple way of checking if a directory is writeable.
/// </summary>
/// <param name="dirPath">Path to check</param>
/// <param name="createDirectoryPrompt">
/// Prompt to create directory if it doesn't exist.
/// </param>
/// <param name="errorService">
/// An instance of the error service to allow prompting.
/// </param>
/// <returns>True if writable</returns>
public static bool IsWritable(string dirPath)
public static bool IsWritable(string dirPath, bool createDirectoryPrompt, IErrorService errorService)
{
try
{
if (!Directory.Exists(dirPath))
{
MessageBoxResult result = errorService.ShowMessageBox(string.Format(Resources.DirectoryUtils_CreateFolderMsg, dirPath), Resources.DirectoryUtils_CreateFolder, MessageBoxButton.YesNo, MessageBoxImage.Question);
if (MessageBoxResult.Yes == result)
{
Directory.CreateDirectory(dirPath);
}
}
using (File.Create(Path.Combine(dirPath, Path.GetRandomFileName()), 1, FileOptions.DeleteOnClose))
{
}
Expand Down
52 changes: 39 additions & 13 deletions win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,21 +698,25 @@ public string Destination
{
if (!Equals(this.CurrentTask.Destination, value))
{
this.CurrentTask.Destination = value;
this.NotifyOfPropertyChange(() => this.Destination);

if (!string.IsNullOrEmpty(this.CurrentTask.Destination))
if (!string.IsNullOrEmpty(value))
{
string ext = string.Empty;
try
{
ext = Path.GetExtension(this.CurrentTask.Destination);
ext = Path.GetExtension(value);
}
catch (ArgumentException)
{
this.errorService.ShowMessageBox(Resources.Main_InvalidDestination, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
this.errorService.ShowMessageBox(
Resources.Main_InvalidDestination,
Resources.Error,
MessageBoxButton.OK,
MessageBoxImage.Error);
}

this.CurrentTask.Destination = value;
this.NotifyOfPropertyChange(() => this.Destination);

switch (ext)
{
case ".mkv":
Expand All @@ -726,6 +730,11 @@ public string Destination
break;
}
}
else
{
this.CurrentTask.Destination = string.Empty;
this.NotifyOfPropertyChange(() => this.Destination);
}
}
}
}
Expand Down Expand Up @@ -1397,9 +1406,9 @@ public bool AddToQueue()
return false;
}

if (!DirectoryUtilities.IsWritable(Path.GetDirectoryName(this.CurrentTask.Destination)))
if (!DirectoryUtilities.IsWritable(Path.GetDirectoryName(this.CurrentTask.Destination), true, this.errorService))
{
this.errorService.ShowMessageBox(Resources.Main_NoPermissionsOnDirectory, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
this.errorService.ShowMessageBox(Resources.Main_NoPermissionsOrMissingDirectory, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}

Expand Down Expand Up @@ -1794,14 +1803,31 @@ public void OpenDestinationDirectory()
{
if (!string.IsNullOrEmpty(this.Destination))
{
string directory = Path.GetDirectoryName(this.Destination);
if (!string.IsNullOrEmpty(directory))
try
{
Process.Start(directory);
string directory = Path.GetDirectoryName(this.Destination);
if (!string.IsNullOrEmpty(directory) && Directory.Exists(directory))
{
Process.Start(directory);
}
else
{
MessageBoxResult result =
errorService.ShowMessageBox(
string.Format(Resources.DirectoryUtils_CreateFolderMsg, directory),
Resources.DirectoryUtils_CreateFolder,
MessageBoxButton.YesNo,
MessageBoxImage.Question);
if (MessageBoxResult.Yes == result)
{
Directory.CreateDirectory(directory);
Process.Start(directory);
}
}
}
else
catch (Exception exc)
{
Process.Start(AppDomain.CurrentDomain.BaseDirectory);
this.errorService.ShowError(Resources.MainViewModel_UnableToLaunchDestDir, Resources.MainViewModel_UnableToLaunchDestDirSolution, exc);
}
}
}
Expand Down

0 comments on commit b9c5daa

Please sign in to comment.