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