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