namespace HandBrakeWPF.Helpers\r
{\r
using System;\r
- using System.Diagnostics;\r
using System.IO;\r
using System.Linq;\r
- using System.Runtime.CompilerServices;\r
- using System.Threading.Tasks;\r
\r
using Caliburn.Micro;\r
\r
\r
using HandBrakeWPF.Extensions;\r
using HandBrakeWPF.Model.Options;\r
- using HandBrakeWPF.Services.Encode.Model;\r
using HandBrakeWPF.Services.Interfaces;\r
using HandBrakeWPF.Services.Presets.Model;\r
\r
/// Function which generates the filename and path automatically based on \r
/// the Source Name, DVD title and DVD Chapters\r
/// </summary>\r
- /// <param name="task">\r
- /// The task.\r
- /// </param>\r
- /// <param name="sourceOrLabelName">\r
- /// The Source or Label Name\r
- /// </param>\r
- /// <param name="presetName">\r
- /// The preset Name.\r
- /// </param>\r
- /// <returns>\r
- /// The Generated FileName\r
- /// </returns>\r
public static string AutoName(EncodeTask task, string sourceOrLabelName, Preset presetName)\r
{\r
IUserSettingService userSettingService = IoC.Get<IUserSettingService>();\r
task.Destination = string.Empty;\r
}\r
\r
- string autoNamePath = string.Empty;\r
if (task.Title != 0)\r
{\r
// Get the Source Name and remove any invalid characters\r
\r
// Remove Underscores\r
if (userSettingService.GetUserSetting<bool>(UserSettingConstants.AutoNameRemoveUnderscore))\r
+ {\r
sourceName = sourceName.Replace("_", " ");\r
+ }\r
\r
if (userSettingService.GetUserSetting<bool>(UserSettingConstants.RemovePunctuation))\r
{\r
\r
// Switch to "Title Case"\r
if (userSettingService.GetUserSetting<bool>(UserSettingConstants.AutoNameTitleCase))\r
+ {\r
sourceName = sourceName.ToTitleCase();\r
+ }\r
\r
// Get the Selected Title Number\r
-\r
string dvdTitle = task.Title.ToString();\r
\r
// Get the Chapter Start and Chapter End Numbers\r
string chapterFinish = task.EndPoint.ToString();\r
string combinedChapterTag = chapterStart;\r
if (chapterFinish != chapterStart && chapterFinish != string.Empty)\r
+ {\r
combinedChapterTag = chapterStart + "-" + chapterFinish;\r
+ }\r
\r
// Local method to check if we have a creation time in the meta data. If not, fall back to source file creation time.\r
DateTime obtainCreateDateObject()\r
{\r
return d;\r
}\r
+\r
try\r
{\r
return File.GetCreationTime(task.Source);\r
}\r
catch (Exception e)\r
{\r
- if (e is UnauthorizedAccessException ||\r
- e is PathTooLongException ||\r
- e is NotSupportedException)\r
+ if (e is UnauthorizedAccessException || e is PathTooLongException || e is NotSupportedException)\r
{\r
// Suspect the most likely concerns trying to grab the creation date in which we would want to swallow exception.\r
return default(DateTime);\r
}\r
+\r
throw e; \r
}\r
}\r
string createTime = creationDateTime.ToString("HH-mm"); \r
\r
/*\r
- * File Name\r
- */\r
- string destinationFilename;\r
- if (userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNameFormat) != string.Empty)\r
- {\r
- destinationFilename = userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNameFormat);\r
- destinationFilename =\r
- destinationFilename\r
- .RegexReplace(Constants.Source, sourceName)\r
- .RegexReplace(Constants.Title, dvdTitle)\r
- .RegexReplace(Constants.Chapters, combinedChapterTag)\r
- .RegexReplace(Constants.Date, DateTime.Now.Date.ToShortDateString().Replace('/', '-'))\r
- .RegexReplace(Constants.Time, DateTime.Now.ToString("HH-mm"))\r
- .RegexReplace(Constants.CretaionDate, createDate)\r
- .RegexReplace(Constants.CreationTime, createTime);\r
-\r
- if (task.VideoEncodeRateType == VideoEncodeRateType.ConstantQuality)\r
- {\r
- destinationFilename = destinationFilename.Replace(Constants.Quality, task.Quality.ToString());\r
- destinationFilename = destinationFilename.Replace(Constants.Bitrate, string.Empty);\r
- }\r
- else\r
- {\r
- destinationFilename = destinationFilename.Replace(\r
- Constants.Bitrate,\r
- task.VideoBitrate.ToString());\r
- destinationFilename = destinationFilename.Replace(Constants.Quality, string.Empty);\r
- }\r
- }\r
- else\r
- {\r
- destinationFilename = sourceName + "_T" + dvdTitle + "_C" + combinedChapterTag;\r
- }\r
-\r
- /*\r
- * File Extension\r
- */\r
- if (task.OutputFormat == OutputFormat.Mp4)\r
- {\r
- switch (userSettingService.GetUserSetting<int>(UserSettingConstants.UseM4v, typeof(int)))\r
- {\r
- case 0: // Automatic\r
- destinationFilename += task.IncludeChapterMarkers || MP4Helper.RequiresM4v(task) ? ".m4v" : ".mp4";\r
- break;\r
- case 1: // Always MP4\r
- destinationFilename += ".mp4";\r
- break;\r
- case 2: // Always M4V\r
- destinationFilename += ".m4v";\r
- break;\r
- }\r
- }\r
- else if (task.OutputFormat == OutputFormat.Mkv)\r
- {\r
- destinationFilename += ".mkv";\r
- }\r
- else if (task.OutputFormat == OutputFormat.WebM)\r
- {\r
- destinationFilename += ".webm";\r
- }\r
-\r
- /*\r
- * File Destination Path\r
+ * Generate the full path and filename\r
*/\r
-\r
- // If there is an auto name path, use it...\r
- if (userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim().StartsWith("{source_path}") && !string.IsNullOrEmpty(task.Source))\r
- {\r
- string savedPath = userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim().Replace("{source_path}\\", string.Empty).Replace("{source_path}", string.Empty);\r
-\r
- string directory = Directory.Exists(task.Source)\r
- ? task.Source\r
- : Path.GetDirectoryName(task.Source);\r
- string requestedPath = Path.Combine(directory, savedPath);\r
-\r
- autoNamePath = Path.Combine(requestedPath, destinationFilename);\r
- }\r
- else if (userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Contains("{source_folder_name}") && !string.IsNullOrEmpty(task.Source))\r
- {\r
- // Second Case: We have a Path, with "{source_folder}" in it, therefore we need to replace it with the folder name from the source.\r
- string path = Path.GetDirectoryName(task.Source);\r
- if (!string.IsNullOrEmpty(path))\r
- {\r
- string[] filesArray = path.Split(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);\r
- string sourceFolder = filesArray[filesArray.Length - 1];\r
-\r
- autoNamePath = Path.Combine(userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Replace("{source_folder_name}", sourceFolder), destinationFilename);\r
- }\r
- }\r
- else if (!task.Destination.Contains(Path.DirectorySeparatorChar.ToString()))\r
- {\r
- // Third case: If the destination box doesn't already contain a path, make one.\r
- if (userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim() != string.Empty &&\r
- userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim() != "Click 'Browse' to set the default location")\r
- {\r
- autoNamePath = Path.Combine(userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath), destinationFilename);\r
- }\r
- else \r
- {\r
- // ...otherwise, output to the source directory\r
- autoNamePath = null;\r
- }\r
- }\r
- else\r
- {\r
- // Otherwise, use the path that is already there.\r
- // Use the path and change the file extension to match the previous destination\r
- autoNamePath = Path.Combine(Path.GetDirectoryName(task.Destination), destinationFilename);\r
- }\r
-\r
+ string destinationFilename = GenerateDestinationFileName(task, userSettingService, sourceName, dvdTitle, combinedChapterTag, createDate, createTime);\r
+ string autoNamePath = GetAutonamePath(userSettingService, task, destinationFilename);\r
autoNamePath = CheckAndHandleFilenameCollisions(autoNamePath, destinationFilename, task, userSettingService);\r
+ return autoNamePath;\r
}\r
\r
- return autoNamePath;\r
+ return string.Empty;\r
}\r
\r
/// <summary>\r
Directory.Exists(userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim()));\r
}\r
\r
+ private static string GenerateDestinationFileName(EncodeTask task, IUserSettingService userSettingService, string sourceName, string dvdTitle, string combinedChapterTag, string createDate, string createTime)\r
+ {\r
+ string destinationFilename;\r
+ if (userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNameFormat) != string.Empty)\r
+ {\r
+ destinationFilename = userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNameFormat);\r
+ destinationFilename =\r
+ destinationFilename\r
+ .RegexReplace(Constants.Source, sourceName)\r
+ .RegexReplace(Constants.Title, dvdTitle)\r
+ .RegexReplace(Constants.Chapters, combinedChapterTag)\r
+ .RegexReplace(Constants.Date, DateTime.Now.Date.ToShortDateString().Replace('/', '-'))\r
+ .RegexReplace(Constants.Time, DateTime.Now.ToString("HH-mm"))\r
+ .RegexReplace(Constants.CretaionDate, createDate)\r
+ .RegexReplace(Constants.CreationTime, createTime);\r
+\r
+ if (task.VideoEncodeRateType == VideoEncodeRateType.ConstantQuality)\r
+ {\r
+ destinationFilename = destinationFilename.Replace(Constants.Quality, task.Quality.ToString());\r
+ destinationFilename = destinationFilename.Replace(Constants.Bitrate, string.Empty);\r
+ }\r
+ else\r
+ {\r
+ destinationFilename = destinationFilename.Replace(\r
+ Constants.Bitrate,\r
+ task.VideoBitrate.ToString());\r
+ destinationFilename = destinationFilename.Replace(Constants.Quality, string.Empty);\r
+ }\r
+ }\r
+ else\r
+ {\r
+ destinationFilename = sourceName + "_T" + dvdTitle + "_C" + combinedChapterTag;\r
+ }\r
+\r
+ /*\r
+ * File Extension\r
+ */\r
+ if (task.OutputFormat == OutputFormat.Mp4)\r
+ {\r
+ switch (userSettingService.GetUserSetting<int>(UserSettingConstants.UseM4v, typeof(int)))\r
+ {\r
+ case 0: // Automatic\r
+ destinationFilename += task.IncludeChapterMarkers || MP4Helper.RequiresM4v(task) ? ".m4v" : ".mp4";\r
+ break;\r
+ case 1: // Always MP4\r
+ destinationFilename += ".mp4";\r
+ break;\r
+ case 2: // Always M4V\r
+ destinationFilename += ".m4v";\r
+ break;\r
+ }\r
+ }\r
+ else if (task.OutputFormat == OutputFormat.Mkv)\r
+ {\r
+ destinationFilename += ".mkv";\r
+ }\r
+ else if (task.OutputFormat == OutputFormat.WebM)\r
+ {\r
+ destinationFilename += ".webm";\r
+ }\r
+\r
+ return destinationFilename;\r
+ }\r
+\r
+ private static string GetAutonamePath(IUserSettingService userSettingService, EncodeTask task, string destinationFilename)\r
+ {\r
+ string autoNamePath = string.Empty;\r
+\r
+ // If there is an auto name path, use it...\r
+ if (userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim().StartsWith("{source_path}") && !string.IsNullOrEmpty(task.Source))\r
+ {\r
+ string savedPath = userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim().Replace("{source_path}\\", string.Empty).Replace("{source_path}", string.Empty);\r
+\r
+ string directory = Directory.Exists(task.Source)\r
+ ? task.Source\r
+ : Path.GetDirectoryName(task.Source);\r
+ string requestedPath = Path.Combine(directory, savedPath);\r
+\r
+ autoNamePath = Path.Combine(requestedPath, destinationFilename);\r
+ }\r
+ else if (userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Contains("{source_folder_name}") && !string.IsNullOrEmpty(task.Source))\r
+ {\r
+ // Second Case: We have a Path, with "{source_folder}" in it, therefore we need to replace it with the folder name from the source.\r
+ string path = Path.GetDirectoryName(task.Source);\r
+ if (!string.IsNullOrEmpty(path))\r
+ {\r
+ string[] filesArray = path.Split(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);\r
+ string sourceFolder = filesArray[filesArray.Length - 1];\r
+\r
+ autoNamePath = Path.Combine(userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Replace("{source_folder_name}", sourceFolder), destinationFilename);\r
+ }\r
+ }\r
+ else if (!task.Destination.Contains(Path.DirectorySeparatorChar.ToString()))\r
+ {\r
+ // Third case: If the destination box doesn't already contain a path, make one.\r
+ if (userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim() != string.Empty &&\r
+ userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath).Trim() != "Click 'Browse' to set the default location")\r
+ {\r
+ autoNamePath = Path.Combine(userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNamePath), destinationFilename);\r
+ }\r
+ else\r
+ {\r
+ // ...otherwise, output to the source directory\r
+ autoNamePath = null;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ // Otherwise, use the path that is already there.\r
+ // Use the path and change the file extension to match the previous destination\r
+ autoNamePath = Path.Combine(Path.GetDirectoryName(task.Destination), destinationFilename);\r
+ }\r
+\r
+ return autoNamePath;\r
+ }\r
+\r
private static string CheckAndHandleFilenameCollisions(string autoNamePath, string destinationFilename, EncodeTask task, IUserSettingService userSettingService)\r
{\r
AutonameFileCollisionBehaviour behaviour = (AutonameFileCollisionBehaviour)userSettingService.GetUserSetting<int>(UserSettingConstants.AutonameFileCollisionBehaviour, typeof(int));\r