OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmPreview.cs
1 using System;\r
2 using System.Windows.Forms;\r
3 using System.Threading;\r
4 using System.Diagnostics;\r
5 using System.Runtime.InteropServices;\r
6 using System.IO;\r
7 using AxQTOControlLib;\r
8 using Handbrake.EncodeQueue;\r
9 using Handbrake.Functions;\r
10 using QTOControlLib;\r
11 using QTOLibrary;\r
12 \r
13 namespace Handbrake\r
14 {\r
15     public partial class frmPreview : Form\r
16     {\r
17 \r
18         QueryGenerator hb_common_func = new QueryGenerator();\r
19         Encode process = new Encode();\r
20         private delegate void UpdateUIHandler();\r
21         String currently_playing = "";\r
22         readonly frmMain mainWindow;\r
23         private Process hbProc;\r
24         private Thread player;\r
25         private Boolean noQT;\r
26 \r
27         public frmPreview(frmMain mw)\r
28         {\r
29             try\r
30             {\r
31                 InitializeComponent();\r
32             }\r
33             catch (Exception exc)\r
34             {\r
35                 MessageBox.Show(mw, "It would appear QuickTime 7 is not installed. QuickTime preview functionality will be disabled! \n\n Debug Info:\n" + exc, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
36                 btn_playQT.Enabled = false;\r
37                 noQT = true;\r
38             }\r
39             this.mainWindow = mw;\r
40             cb_preview.SelectedIndex = 0;\r
41             cb_duration.SelectedIndex = 1;\r
42         }\r
43 \r
44         #region Encode Sample\r
45         private void btn_playVLC_Click(object sender, EventArgs e)\r
46         {\r
47             lbl_status.Visible = true;\r
48             try\r
49             {\r
50                 QTControl.URL = "";\r
51                 if (File.Exists(currently_playing))\r
52                     File.Delete(currently_playing);\r
53             }\r
54             catch (Exception)\r
55             {\r
56                 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
57             }\r
58 \r
59             btn_playQT.Enabled = false;\r
60             btn_playVLC.Enabled = false;\r
61             lbl_status.Text = "Encoding Sample for (VLC) ...";\r
62             String query = hb_common_func.GeneratePreviewQuery(mainWindow, cb_duration.Text, cb_preview.Text);\r
63             ThreadPool.QueueUserWorkItem(procMonitor, query);\r
64         }\r
65         private void btn_playQT_Click(object sender, EventArgs e)\r
66         {\r
67             lbl_status.Visible = true;\r
68             try\r
69             {\r
70                 QTControl.URL = "";\r
71                 if (File.Exists(currently_playing))\r
72                     File.Delete(currently_playing);\r
73             }\r
74             catch (Exception)\r
75             {\r
76                 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
77             }\r
78 \r
79             btn_playQT.Enabled = false;\r
80             btn_playVLC.Enabled = false;\r
81             lbl_status.Text = "Encoding Sample for (QT) ...";\r
82             String query = hb_common_func.GeneratePreviewQuery(mainWindow, cb_duration.Text, cb_preview.Text);\r
83             ThreadPool.QueueUserWorkItem(procMonitor, query);\r
84         }\r
85         private void procMonitor(object state)\r
86         {\r
87             // Make sure we are not already encoding and if we are then display an error.\r
88             if (hbProc != null)\r
89                 MessageBox.Show(this, "Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
90             else\r
91             {\r
92                 hbProc = process.runCli((string)state).hbProcProcess;\r
93                 hbProc.WaitForExit();\r
94                 hbProc = null;\r
95                 encodeCompleted();\r
96             }\r
97         }\r
98         private void encodeCompleted()\r
99         {\r
100             try\r
101             {\r
102                 if (InvokeRequired)\r
103                 {\r
104                     BeginInvoke(new UpdateUIHandler(encodeCompleted));\r
105                     return;\r
106                 }\r
107                 if (!noQT)\r
108                     btn_playQT.Enabled = true;\r
109                 btn_playVLC.Enabled = true;\r
110 \r
111                 // Decide which player to use.\r
112                 String playerSelection = lbl_status.Text.Contains("QT") ? "QT" : "VLC";\r
113 \r
114                 lbl_status.Text = "Loading Clip ...";\r
115 \r
116                 // Get the sample filename\r
117                 if (mainWindow.text_destination.Text != "")\r
118                     currently_playing = mainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").Replace(".mkv", "_sample.mkv");;\r
119 \r
120                 // Play back in QT or VLC\r
121                 if (playerSelection == "QT")\r
122                     play();\r
123                 else\r
124                     playVLC();\r
125 \r
126                 lbl_status.Text = "";\r
127             }\r
128             catch (Exception exc)\r
129             {\r
130                 MessageBox.Show(this, "frmPreview.cs encodeCompleted " + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
131             }\r
132         }\r
133         #endregion\r
134 \r
135         #region Playback\r
136 \r
137         /// <summary>\r
138         /// Play the video back in the QuickTime control\r
139         /// </summary>\r
140         private void play()\r
141         {\r
142             player = new Thread(OpenMovie) { IsBackground = true };\r
143             player.Start();\r
144             lbl_status.Visible = false;\r
145         }\r
146 \r
147         /// <summary>\r
148         /// Play the video back in an external VLC player\r
149         /// </summary>\r
150         private void playVLC()\r
151         {\r
152             // Launch VLC and play video.\r
153             if (currently_playing != "")\r
154             {\r
155                 if (File.Exists(currently_playing))\r
156                 {\r
157                     if (File.Exists(Properties.Settings.Default.VLC_Path))\r
158                     {\r
159                         String args = "\"" + currently_playing + "\"";\r
160                         ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);\r
161                         Process.Start(vlc);\r
162                         lbl_status.Text = "VLC will now launch.";\r
163                     }\r
164                     else\r
165                         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
166                 }\r
167                 else\r
168                     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
169             }\r
170             lbl_status.Visible = false;\r
171         }\r
172 \r
173         /// <summary>\r
174         /// QT control - Open the file\r
175         /// </summary>\r
176         [STAThread]\r
177         private void OpenMovie()\r
178         {\r
179             try\r
180             {\r
181                 if (InvokeRequired)\r
182                 {\r
183                     BeginInvoke(new UpdateUIHandler(OpenMovie));\r
184                     return;\r
185                 }\r
186                 QTControl.Sizing = QTSizingModeEnum.qtControlFitsMovie;\r
187                 QTControl.URL = currently_playing;\r
188                 QTControl.Sizing = QTSizingModeEnum.qtMovieFitsControl;\r
189                 QTControl.Show();\r
190 \r
191                 this.ClientSize = QTControl.Size;\r
192                 this.Height += 25;\r
193             }\r
194             catch (COMException ex)\r
195             {\r
196                 QTUtils qtu = new QTUtils();\r
197                 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
198             }\r
199             catch (Exception ex)\r
200             {\r
201                 MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
202             }\r
203         }\r
204         #endregion\r
205     }\r
206 }\r