OSDN Git Service

LinGui: make Help->Guide work on windows/mingw
[handbrake-jp/handbrake-jp-git.git] / win / C# / Functions / Main.cs
index 3285b5f..d7380b2 100644 (file)
@@ -8,8 +8,10 @@ namespace Handbrake.Functions
     using System;\r
     using System.Collections.Generic;\r
     using System.Diagnostics;\r
+    using System.Globalization;\r
     using System.IO;\r
     using System.Net;\r
+    using System.Text;\r
     using System.Text.RegularExpressions;\r
     using System.Threading;\r
     using System.Windows.Forms;\r
@@ -57,31 +59,6 @@ namespace Handbrake.Functions
         }\r
 \r
         /// <summary>\r
-        /// Select the longest title in the DVD title dropdown menu on frmMain\r
-        /// </summary>\r
-        /// <param name="source">\r
-        /// The Source.\r
-        /// </param>\r
-        /// <returns>\r
-        /// The longest title.\r
-        /// </returns>\r
-        public static Title SelectLongestTitle(DVD source)\r
-        {\r
-            TimeSpan longestDurationFound = TimeSpan.FromSeconds(0.0);\r
-            Title returnTitle = null;\r
-\r
-            foreach (Title item in source.Titles)\r
-            {\r
-                if (item.Duration > longestDurationFound)\r
-                {\r
-                    returnTitle = item;\r
-                    longestDurationFound = item.Duration;\r
-                }\r
-            }\r
-            return returnTitle;\r
-        }\r
-\r
-        /// <summary>\r
         /// Set's up the DataGridView on the Chapters tab (frmMain)\r
         /// </summary>\r
         /// <param name="dataChpt">\r
@@ -151,7 +128,7 @@ namespace Handbrake.Functions
             foreach (DataGridViewRow item in dataChpt.Rows)\r
             {\r
                 string name;\r
-                chapterMap.TryGetValue((int) item.Cells[0].Value, out name);\r
+                chapterMap.TryGetValue((int)item.Cells[0].Value, out name);\r
                 item.Cells[1].Value = name ?? "Chapter " + item.Cells[0].Value;\r
             }\r
 \r
@@ -159,6 +136,40 @@ namespace Handbrake.Functions
         }\r
 \r
         /// <summary>\r
+        /// Create a CSV file with the data from the Main Window Chapters tab\r
+        /// </summary>\r
+        /// <param name="mainWindow">Main Window</param>\r
+        /// <param name="filePathName">Path to save the csv file</param>\r
+        /// <returns>True if successful </returns>\r
+        public static bool SaveChapterMarkersToCsv(frmMain mainWindow, string filePathName)\r
+        {\r
+            try\r
+            {\r
+                string csv = string.Empty;\r
+\r
+                foreach (DataGridViewRow row in mainWindow.data_chpt.Rows)\r
+                {\r
+                    csv += row.Cells[0].Value.ToString();\r
+                    csv += ",";\r
+                    csv += row.Cells[1].Value.ToString().Replace(",", "\\,");\r
+                    csv += Environment.NewLine;\r
+                }\r
+                StreamWriter file = new StreamWriter(filePathName);\r
+                file.Write(csv);\r
+                file.Close();\r
+                file.Dispose();\r
+                return true;\r
+            }\r
+            catch (Exception exc)\r
+            {\r
+                frmExceptionWindow exceptionWindow = new frmExceptionWindow();\r
+                exceptionWindow.Setup("Unable to save Chapter Makrers file! \nChapter marker names will NOT be saved in your encode", exc.ToString());\r
+                exceptionWindow.ShowDialog();\r
+                return false;\r
+            }\r
+        }\r
+\r
+        /// <summary>\r
         /// Function which generates the filename and path automatically based on \r
         /// the Source Name, DVD title and DVD Chapters\r
         /// </summary>\r
@@ -176,6 +187,21 @@ namespace Handbrake.Functions
                 // Get the Source Name \r
                 string sourceName = mainWindow.SourceName;\r
 \r
+                // Remove any illeagal characters from the source name\r
+                foreach (char character in Path.GetInvalidFileNameChars())\r
+                {\r
+                    if (autoNamePath != null)\r
+                    {\r
+                        sourceName = sourceName.Replace(character.ToString(), string.Empty);\r
+                    }\r
+                }\r
+\r
+                if (Properties.Settings.Default.AutoNameRemoveUnderscore)\r
+                    sourceName = sourceName.Replace("_", " ");\r
+\r
+                if (Properties.Settings.Default.AutoNameTitleCase)\r
+                    sourceName = TitleCase(sourceName);\r
+\r
                 // Get the Selected Title Number\r
                 string[] titlesplit = mainWindow.drp_dvdtitle.Text.Split(' ');\r
                 string dvdTitle = titlesplit[0].Replace("Automatic", string.Empty);\r
@@ -225,10 +251,12 @@ namespace Handbrake.Functions
                 else // Otherwise, use the path that is already there.\r
                 {\r
                     // Use the path and change the file extension to match the previous destination\r
-                    autoNamePath = Path.Combine(Path.GetDirectoryName(mainWindow.text_destination.Text), destinationFilename);\r
+                    autoNamePath = Path.Combine(Path.GetDirectoryName(mainWindow.text_destination.Text),\r
+                                                destinationFilename);\r
 \r
                     if (Path.HasExtension(mainWindow.text_destination.Text))\r
-                        autoNamePath = Path.ChangeExtension(autoNamePath, Path.GetExtension(mainWindow.text_destination.Text));\r
+                        autoNamePath = Path.ChangeExtension(autoNamePath,\r
+                                                            Path.GetExtension(mainWindow.text_destination.Text));\r
                 }\r
             }\r
 \r
@@ -254,9 +282,9 @@ namespace Handbrake.Functions
             Process cliProcess = new Process();\r
             ProcessStartInfo handBrakeCli = new ProcessStartInfo("HandBrakeCLI.exe", " -u -v0")\r
                                                 {\r
-                                                    UseShellExecute = false, \r
-                                                    RedirectStandardError = true, \r
-                                                    RedirectStandardOutput = true, \r
+                                                    UseShellExecute = false,\r
+                                                    RedirectStandardError = true,\r
+                                                    RedirectStandardOutput = true,\r
                                                     CreateNoWindow = true\r
                                                 };\r
             cliProcess.StartInfo = handBrakeCli;\r
@@ -302,7 +330,35 @@ namespace Handbrake.Functions
             }\r
             catch (Exception e)\r
             {\r
-                MessageBox.Show("Unable to retrieve version information from the CLI. \nError:\n" + e);\r
+                frmExceptionWindow exceptionWindow = new frmExceptionWindow();\r
+                exceptionWindow.Setup("Unable to retrieve version information from the CLI.", e.ToString());\r
+                exceptionWindow.ShowDialog();\r
+            }\r
+        }\r
+\r
+        /// <summary>\r
+        /// Check to make sure that the user has an up to date version of the CLI installed.\r
+        /// </summary>\r
+        public static void CheckForValidCliVersion()\r
+        {\r
+            // Make sure we have a recent version for svn builds\r
+            string version = Properties.Settings.Default.hb_version;\r
+            if (version.Contains("svn"))\r
+            {\r
+                version = version.Replace("svn", string.Empty).Trim();\r
+                int build;\r
+                int.TryParse(version, out build);\r
+                if (build < Properties.Settings.Default.hb_min_cli)\r
+                {\r
+                    MessageBox.Show(\r
+                        "It appears you are trying to use a CLI executable that is too old for this version of the HandBrake GUI.\n" +\r
+                        "Please update the HandBrakeCLI.exe to a newer build.\n\n" +\r
+                        "HandBrake build Detected: " + Properties.Settings.Default.hb_version,\r
+                        "Error",\r
+                        MessageBoxButtons.OK,\r
+                        MessageBoxIcon.Error);\r
+                    return;\r
+                }\r
             }\r
         }\r
 \r
@@ -471,7 +527,7 @@ namespace Handbrake.Functions
                                                                           UpdateCheckInformation info =\r
                                                                               new UpdateCheckInformation\r
                                                                                   {\r
-                                                                                      NewVersionAvailable = false, \r
+                                                                                      NewVersionAvailable = false,\r
                                                                                       BuildInformation = null\r
                                                                                   };\r
                                                                           callback(new UpdateCheckResult(debug, info));\r
@@ -486,7 +542,7 @@ namespace Handbrake.Functions
                                                                       UpdateCheckInformation info2 =\r
                                                                           new UpdateCheckInformation\r
                                                                               {\r
-                                                                                  NewVersionAvailable = latest > current, \r
+                                                                                  NewVersionAvailable = latest > current,\r
                                                                                   BuildInformation = reader\r
                                                                               };\r
                                                                       callback(new UpdateCheckResult(debug, info2));\r
@@ -509,7 +565,7 @@ namespace Handbrake.Functions
         /// </returns>\r
         public static UpdateCheckInformation EndCheckForUpdates(IAsyncResult result)\r
         {\r
-            UpdateCheckResult checkResult = (UpdateCheckResult) result;\r
+            UpdateCheckResult checkResult = (UpdateCheckResult)result;\r
             return checkResult.Result;\r
         }\r
 \r
@@ -728,7 +784,7 @@ namespace Handbrake.Functions
                     drives.Add(new DriveInformation\r
                                    {\r
                                        Id = id,\r
-                                       VolumeLabel = curDrive.VolumeLabel, \r
+                                       VolumeLabel = curDrive.VolumeLabel,\r
                                        RootDirectory = curDrive.RootDirectory + "VIDEO_TS"\r
                                    });\r
                     id++;\r
@@ -736,5 +792,44 @@ namespace Handbrake.Functions
             }\r
             return drives;\r
         }\r
+\r
+        /// <summary>\r
+        /// Change a string to Title Case/\r
+        /// </summary>\r
+        /// <param name="input">\r
+        /// The input.\r
+        /// </param>\r
+        /// <returns>\r
+        /// A string in title case.\r
+        /// </returns>\r
+        public static string TitleCase(string input)\r
+        {\r
+            string[] tokens = input.Split(' ');\r
+            StringBuilder sb = new StringBuilder(input.Length);\r
+            foreach (string s in tokens)\r
+            {\r
+                sb.Append(s[0].ToString().ToUpper());\r
+                sb.Append(s.Substring(1).ToLower());\r
+                sb.Append(" ");\r
+            }\r
+\r
+            return sb.ToString().Trim();\r
+        }\r
+\r
+        /// <summary>\r
+        /// Show the Exception Window\r
+        /// </summary>\r
+        /// <param name="shortError">\r
+        /// The short error.\r
+        /// </param>\r
+        /// <param name="longError">\r
+        /// The long error.\r
+        /// </param>\r
+        public static void ShowExceptiowWindow(string shortError, string longError)\r
+        {\r
+            frmExceptionWindow exceptionWindow = new frmExceptionWindow();\r
+            exceptionWindow.Setup(shortError, longError);\r
+            exceptionWindow.Show();\r
+        }\r
     }\r
 }
\ No newline at end of file