OSDN Git Service

CLI: update the built in presets
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmPreview.cs
index f2356ae..3a8a220 100644 (file)
-using System;\r
-using System.Windows.Forms;\r
-using System.Threading;\r
-using System.Diagnostics;\r
-using System.Runtime.InteropServices;\r
-using System.IO;\r
-using Handbrake.EncodeQueue;\r
-using Handbrake.Functions;\r
-using QTOControlLib;\r
-using QTOLibrary;\r
+/*  frmPreview.cs $\r
+    This file is part of the HandBrake source code.\r
+    Homepage: <http://handbrake.fr/>.\r
+    It may be used under the terms of the GNU General Public License. */\r
 \r
 namespace Handbrake\r
 {\r
+    using System;\r
+    using System.Diagnostics;\r
+    using System.IO;\r
+    using System.Runtime.InteropServices;\r
+    using System.Threading;\r
+    using System.Windows;\r
+    using System.Windows.Forms;\r
+    using Functions;\r
+\r
+    using HandBrake.ApplicationServices.Services;\r
+    using HandBrake.ApplicationServices.Services.Interfaces;\r
+\r
+    using QTOControlLib;\r
+    using QTOLibrary;\r
+\r
+    using MessageBox = System.Windows.Forms.MessageBox;\r
+\r
+    /// <summary>\r
+    /// The Preview Window\r
+    /// </summary>\r
     public partial class frmPreview : Form\r
     {\r
+        #region Private Variables\r
 \r
-        QueryGenerator hb_common_func = new QueryGenerator();\r
-        Encode process = new Encode();\r
-        private delegate void UpdateUIHandler();\r
-        String currently_playing = "";\r
-        readonly frmMain mainWindow;\r
+        /// <summary>\r
+        /// The Main Window\r
+        /// </summary>\r
+        private readonly frmMain mainWindow;\r
+\r
+        /// <summary>\r
+        /// True if QT is not installed\r
+        /// </summary>\r
+        private readonly bool noQt;\r
+\r
+        /// <summary>\r
+        /// The encode queue\r
+        /// </summary>\r
+        private readonly IQueue encodeQueue = new Queue();\r
+\r
+        /// <summary>\r
+        /// What is currently playing\r
+        /// </summary>\r
+        private string currentlyPlaying = string.Empty;\r
+\r
+        /// <summary>\r
+        /// Play With VLC tracker\r
+        /// </summary>\r
+        private bool playWithVlc;\r
+\r
+        /// <summary>\r
+        /// A Thread for the video player\r
+        /// </summary>\r
         private Thread player;\r
-        private Boolean noQT;\r
 \r
+        #endregion\r
+\r
+        /// <summary>\r
+        /// Initializes a new instance of the <see cref="frmPreview"/> class.\r
+        /// </summary>\r
+        /// <param name="mw">\r
+        /// The mw.\r
+        /// </param>\r
         public frmPreview(frmMain mw)\r
         {\r
             try\r
             {\r
                 InitializeComponent();\r
             }\r
-            catch (Exception exc)\r
+            catch (Exception)\r
             {\r
-                MessageBox.Show(mw, "It would appear QuickTime 7 is not installed or not accessible. QuickTime preview functionality will be disabled! \n\n Debug Info:\n" + exc, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+                this.noQt = true;\r
+\r
+                int borderWidth = (this.Width - this.ClientSize.Width) / 2;\r
+                int titlebarAndBorder = this.Height - this.ClientSize.Height;\r
+\r
+                this.Height = toolBar.Height + titlebarAndBorder + 1;\r
                 btn_playQT.Enabled = false;\r
-                noQT = true;\r
+                btn_playQT.Visible = false;\r
             }\r
+\r
             this.mainWindow = mw;\r
+\r
             cb_preview.SelectedIndex = 0;\r
             cb_duration.SelectedIndex = 1;\r
+\r
+            cb_preview.Items.Clear();\r
+            for (int i = 1; i <= Properties.Settings.Default.previewScanCount; i++)\r
+            {\r
+                cb_preview.Items.Add(i.ToString());\r
+            }\r
+\r
+            cb_preview.SelectedIndex = 0;\r
+\r
+            encodeQueue.EncodeStarted += this.EncodeQueueEncodeStarted;\r
+            encodeQueue.EncodeEnded += this.EncodeQueueEncodeEnded;\r
         }\r
 \r
-        #region Encode Sample\r
-        private void btn_playVLC_Click(object sender, EventArgs e)\r
+        #region Delegates\r
+        /// <summary>\r
+        /// Update UI Delegate\r
+        /// </summary>\r
+        /// <param name="sender">\r
+        /// The sender.\r
+        /// </param>\r
+        /// <param name="e">\r
+        /// The e.\r
+        /// </param>\r
+        private delegate void UpdateUiHandler(object sender, EventArgs e);\r
+\r
+        /// <summary>\r
+        /// The Open Movie Handler\r
+        /// </summary>\r
+        private delegate void OpenMovieHandler();\r
+        #endregion\r
+\r
+        #region Event Handlers\r
+        /// <summary>\r
+        /// The encode has started\r
+        /// </summary>\r
+        /// <param name="sender">\r
+        /// The sender.\r
+        /// </param>\r
+        /// <param name="e">\r
+        /// The e.\r
+        /// </param>\r
+        private void EncodeQueueEncodeStarted(object sender, EventArgs e)\r
         {\r
-            lbl_status.Visible = true;\r
+            encodeQueue.EncodeStatusChanged += this.EncodeQueueEncodeStatusChanged;\r
+        }\r
+\r
+        /// <summary>\r
+        /// The Enocde has ended\r
+        /// </summary>\r
+        /// <param name="sender">\r
+        /// The sender.\r
+        /// </param>\r
+        /// <param name="e">\r
+        /// The e.\r
+        /// </param>\r
+        private void EncodeQueueEncodeEnded(object sender, EventArgs e)\r
+        {\r
+            encodeQueue.EncodeStatusChanged -= this.EncodeQueueEncodeStatusChanged;\r
+\r
             try\r
             {\r
-                QTControl.URL = "";\r
-                if (File.Exists(currently_playing))\r
-                    File.Delete(currently_playing);\r
+                if (this.InvokeRequired)\r
+                {\r
+                    this.BeginInvoke(new UpdateUiHandler(EncodeQueueEncodeEnded), new[] { sender, e });\r
+                    return;\r
+                }\r
+\r
+                ProgressBarStatus.Visible = false;\r
+                lbl_encodeStatus.Visible = false;\r
+\r
+                if (!this.noQt)\r
+                    btn_playQT.Enabled = true;\r
+                btn_playVLC.Enabled = true;\r
+\r
+                this.Text = this.Text.Replace(" (Encoding)", string.Empty);\r
+\r
+                // Get the sample filename\r
+                if (this.mainWindow.text_destination.Text != string.Empty)\r
+                    this.currentlyPlaying =\r
+                        this.mainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").\r
+                            Replace(".mkv", "_sample.mkv");\r
+\r
+                // Play back in QT or VLC\r
+                if (!playWithVlc)\r
+                    Play();\r
+                else\r
+                    PlayVlc();\r
+            }\r
+            catch (Exception exc)\r
+            {\r
+                Main.ShowExceptiowWindow("An Unexpected error has occured", exc.ToString());\r
             }\r
-            catch (Exception)\r
+        }\r
+\r
+        /// <summary>\r
+        /// Encode status has changed\r
+        /// </summary>\r
+        /// <param name="sender">\r
+        /// The sender.\r
+        /// </param>\r
+        /// <param name="e">\r
+        /// The e.\r
+        /// </param>\r
+        private void EncodeQueueEncodeStatusChanged(object sender, HandBrake.ApplicationServices.EncodeProgressEventArgs e)\r
+        {\r
+            if (this.InvokeRequired)\r
             {\r
-                MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                this.BeginInvoke(new Encode.EncodeProgessStatus(this.EncodeQueueEncodeStatusChanged), new[] { sender, e });\r
+                return;\r
             }\r
 \r
-            btn_playQT.Enabled = false;\r
-            btn_playVLC.Enabled = false;\r
-            lbl_status.Text = "Encoding Sample for (VLC) ...";\r
-            String query = hb_common_func.generatePreviewQuery(mainWindow, cb_duration.Text, cb_preview.Text);\r
-            ThreadPool.QueueUserWorkItem(procMonitor, query);\r
+            lbl_encodeStatus.Text = e.PercentComplete + "%";\r
+            ProgressBarStatus.Value = (int)Math.Round(e.PercentComplete);\r
         }\r
-        private void btn_playQT_Click(object sender, EventArgs e)\r
+        #endregion\r
+\r
+        #region Encode Sample\r
+\r
+        /// <summary>\r
+        /// Play with VLC\r
+        /// </summary>\r
+        /// <param name="sender">\r
+        /// The sender.\r
+        /// </param>\r
+        /// <param name="e">\r
+        /// The e.\r
+        /// </param>\r
+        private void PlayVlcClick(object sender, EventArgs e)\r
         {\r
-            lbl_status.Visible = true;\r
+            ProgressBarStatus.Visible = true;\r
+            ProgressBarStatus.Value = 0;\r
+            lbl_encodeStatus.Visible = true;\r
+            playWithVlc = true;\r
+            this.panel1.Visible = false;\r
+            \r
             try\r
             {\r
-                QTControl.URL = "";\r
-                if (File.Exists(currently_playing))\r
-                    File.Delete(currently_playing);\r
+                if (!this.noQt)\r
+                    QTControl.URL = string.Empty;\r
+\r
+                if (File.Exists(this.currentlyPlaying))\r
+                    File.Delete(this.currentlyPlaying);\r
             }\r
             catch (Exception)\r
             {\r
-                MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.",\r
+                                "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
             }\r
 \r
             btn_playQT.Enabled = false;\r
             btn_playVLC.Enabled = false;\r
-            lbl_status.Text = "Encoding Sample for (QT) ...";\r
-            String query = hb_common_func.generatePreviewQuery(mainWindow, cb_duration.Text, cb_preview.Text);\r
-            ThreadPool.QueueUserWorkItem(procMonitor, query);\r
+            this.Text += " (Encoding)";\r
+            int duration;\r
+            int.TryParse(cb_duration.Text, out duration);\r
+            string query = QueryGenerator.GeneratePreviewQuery(this.mainWindow, duration, cb_preview.Text);\r
+            ThreadPool.QueueUserWorkItem(this.CreatePreview, query);\r
         }\r
-        private void procMonitor(object state)\r
+\r
+        /// <summary>\r
+        /// Encode and Play with QT\r
+        /// </summary>\r
+        /// <param name="sender">\r
+        /// The sender.\r
+        /// </param>\r
+        /// <param name="e">\r
+        /// The e.\r
+        /// </param>\r
+        private void PlayQtClick(object sender, EventArgs e)\r
         {\r
-            // Make sure we are not already encoding and if we are then display an error.\r
-            if (process.hbProcess != null)\r
-                MessageBox.Show(this, "Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+            playWithVlc = false;\r
+            this.panel1.Visible = true;\r
+            if (this.noQt)\r
+            {\r
+                MessageBox.Show(this,\r
+                                "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.",\r
+                                "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+                return;\r
+            }\r
+            if (this.mainWindow.text_destination.Text.Contains(".mkv"))\r
+            {\r
+                MessageBox.Show(this,\r
+                                "The QuickTime Control does not support MKV files, It is recommended you use the VLC option instead.",\r
+                                "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+            }\r
             else\r
             {\r
-                process.runCli((string)state);\r
-                if (process.hbProcess != null)\r
+                ProgressBarStatus.Visible = true;\r
+                ProgressBarStatus.Value = 0;\r
+                lbl_encodeStatus.Visible = true;\r
+                try\r
                 {\r
-                    process.hbProcess.WaitForExit();\r
-                    process.hbProcess = null;\r
+                    QTControl.URL = string.Empty;\r
+                    if (File.Exists(this.currentlyPlaying))\r
+                        File.Delete(this.currentlyPlaying);\r
                 }\r
-                encodeCompleted();\r
-            }\r
-        }\r
-        private void encodeCompleted()\r
-        {\r
-            try\r
-            {\r
-                if (InvokeRequired)\r
+                catch (Exception)\r
                 {\r
-                    BeginInvoke(new UpdateUIHandler(encodeCompleted));\r
-                    return;\r
+                    MessageBox.Show(this,\r
+                                    "Unable to delete previous preview file. You may need to restart the application.",\r
+                                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
                 }\r
-                if (!noQT)\r
-                    btn_playQT.Enabled = true;\r
-                btn_playVLC.Enabled = true;\r
-\r
-                // Decide which player to use.\r
-                String playerSelection = lbl_status.Text.Contains("QT") ? "QT" : "VLC";\r
 \r
-                lbl_status.Text = "Loading Clip ...";\r
-\r
-                // Get the sample filename\r
-                if (mainWindow.text_destination.Text != "")\r
-                    currently_playing = mainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").Replace(".mkv", "_sample.mkv");;\r
-\r
-                // Play back in QT or VLC\r
-                if (playerSelection == "QT")\r
-                    play();\r
-                else\r
-                    playVLC();\r
+                btn_playQT.Enabled = false;\r
+                btn_playVLC.Enabled = false;\r
+                this.Text += " (Encoding)";\r
+                int duration;\r
+                int.TryParse(cb_duration.Text, out duration);\r
+                string query = QueryGenerator.GeneratePreviewQuery(this.mainWindow, duration, cb_preview.Text);\r
 \r
-                lbl_status.Text = "";\r
+                ThreadPool.QueueUserWorkItem(this.CreatePreview, query);\r
             }\r
-            catch (Exception exc)\r
+        }\r
+\r
+        /// <summary>\r
+        /// Create the Preview.\r
+        /// </summary>\r
+        /// <param name="state">\r
+        /// The state.\r
+        /// </param>\r
+        private void CreatePreview(object state)\r
+        {\r
+            // Make sure we are not already encoding and if we are then display an error.\r
+            if (encodeQueue.IsEncoding)\r
             {\r
-                MessageBox.Show(this, "frmPreview.cs encodeCompleted " + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                MessageBox.Show(\r
+                    this,\r
+                    "Handbrake is already encoding a video!",\r
+                    "Warning",\r
+                    MessageBoxButtons.OK,\r
+                    MessageBoxIcon.Warning);\r
+\r
+                return;\r
             }\r
+\r
+            encodeQueue.CreatePreviewSample((string)state);\r
         }\r
+\r
         #endregion\r
 \r
         #region Playback\r
@@ -138,37 +333,62 @@ namespace Handbrake
         /// <summary>\r
         /// Play the video back in the QuickTime control\r
         /// </summary>\r
-        private void play()\r
+        private void Play()\r
         {\r
-            player = new Thread(OpenMovie) { IsBackground = true };\r
-            player.Start();\r
-            lbl_status.Visible = false;\r
+            this.player = new Thread(OpenMovie) { IsBackground = true };\r
+            this.player.Start();\r
         }\r
 \r
         /// <summary>\r
-        /// Play the video back in an external VLC player\r
+        /// Play the video back in an external VLC Player\r
         /// </summary>\r
-        private void playVLC()\r
+        private void PlayVlc()\r
         {\r
-            // Launch VLC and play video.\r
-            if (currently_playing != "")\r
+            // Launch VLC and Play video.\r
+            if (this.currentlyPlaying != string.Empty)\r
             {\r
-                if (File.Exists(currently_playing))\r
+                if (File.Exists(this.currentlyPlaying))\r
                 {\r
+                    // Attempt to find VLC if it doesn't exist in the default set location.\r
+                    string vlcPath;\r
+\r
+                    if (8 == IntPtr.Size ||\r
+                        (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))\r
+                        vlcPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");\r
+                    else\r
+                        vlcPath = Environment.GetEnvironmentVariable("ProgramFiles");\r
+\r
+                    vlcPath = vlcPath != null\r
+                                  ? vlcPath + @"\VideoLAN\VLC\vlc.exe"\r
+                                  : @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";\r
+\r
+                    if (!File.Exists(Properties.Settings.Default.VLC_Path))\r
+                    {\r
+                        if (File.Exists(vlcPath))\r
+                        {\r
+                            Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";\r
+                            Properties.Settings.Default.Save(); // Save this new path if it does\r
+                        }\r
+                        else\r
+                        {\r
+                            MessageBox.Show(this,\r
+                                            "Unable to detect VLC Player. \nPlease make sure VLC is installed and the directory specified in HandBrake's options is correct. (See: \"Tools Menu > Options > Picture Tab\") ",\r
+                                            "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+                        }\r
+                    }\r
+\r
                     if (File.Exists(Properties.Settings.Default.VLC_Path))\r
                     {\r
-                        String args = "\"" + currently_playing + "\"";\r
+                        string args = "\"" + this.currentlyPlaying + "\"";\r
                         ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);\r
                         Process.Start(vlc);\r
-                        lbl_status.Text = "VLC will now launch.";\r
                     }\r
-                    else\r
-                        MessageBox.Show(this, "Unable to detect VLC Player. \nPlease make sure VLC is installed and the directory specified in the program options is correct.", "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
                 }\r
                 else\r
-                    MessageBox.Show(this, "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.", "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+                    MessageBox.Show(this,\r
+                                    "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.",\r
+                                    "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
             }\r
-            lbl_status.Visible = false;\r
         }\r
 \r
         /// <summary>\r
@@ -181,27 +401,41 @@ namespace Handbrake
             {\r
                 if (InvokeRequired)\r
                 {\r
-                    BeginInvoke(new UpdateUIHandler(OpenMovie));\r
+                    BeginInvoke(new OpenMovieHandler(OpenMovie));\r
                     return;\r
                 }\r
-                QTControl.Sizing = QTSizingModeEnum.qtControlFitsMovie;\r
-                QTControl.URL = currently_playing;\r
-                QTControl.Sizing = QTSizingModeEnum.qtMovieFitsControl;\r
+                QTControl.URL = this.currentlyPlaying;\r
+                QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);\r
+                QTControl.URL = this.currentlyPlaying;\r
                 QTControl.Show();\r
 \r
                 this.ClientSize = QTControl.Size;\r
-                this.Height += 25;\r
+                this.Height += toolBar.Height;\r
             }\r
             catch (COMException ex)\r
             {\r
                 QTUtils qtu = new QTUtils();\r
-                MessageBox.Show(this, "Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") + "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode), "QT", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+                Main.ShowExceptiowWindow("Unable to open movie.", ex + Environment.NewLine + qtu.QTErrorFromErrorCode(ex.ErrorCode));\r
             }\r
             catch (Exception ex)\r
             {\r
-                MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+                Main.ShowExceptiowWindow("Unable to open movie.", ex.ToString());\r
             }\r
         }\r
+\r
         #endregion\r
+\r
+        /// <summary>\r
+        /// Remove any subscribed events then close.\r
+        /// </summary>\r
+        /// <param name="e">\r
+        /// The e.\r
+        /// </param>\r
+        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)\r
+        {\r
+            encodeQueue.EncodeStarted -= this.EncodeQueueEncodeStarted;\r
+            encodeQueue.EncodeEnded -= this.EncodeQueueEncodeEnded;\r
+            base.OnClosing(e);\r
+        }\r
     }\r
-}\r
+}
\ No newline at end of file