OSDN Git Service

1a50f1cc02ccda39b6b23844aba2897a19c0217b
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmQueue.cs
1 /*  frmQueue.cs $\r
2         \r
3            This file is part of the HandBrake source code.\r
4            Homepage: <http://handbrake.fr>.\r
5            It may be used under the terms of the GNU General Public License. */\r
6 \r
7 using System;\r
8 using System.Collections.Generic;\r
9 using System.ComponentModel;\r
10 using System.Data;\r
11 using System.Drawing;\r
12 using System.Text;\r
13 using System.Windows.Forms;\r
14 using System.Threading;\r
15 using System.Diagnostics;\r
16 using System.Runtime.InteropServices;\r
17 using System.IO;\r
18 \r
19 namespace Handbrake\r
20 {\r
21     public partial class frmQueue : Form\r
22     {\r
23         private delegate void ProgressUpdateHandler();\r
24         private delegate void setEncoding();\r
25         Functions.CLI cliObj = new Functions.CLI();\r
26         Boolean cancel = false;\r
27         Process hbProc = null;\r
28         Functions.Queue queue;\r
29 \r
30         public frmQueue()\r
31         {\r
32             InitializeComponent();\r
33         }\r
34 \r
35         /// <summary>\r
36         /// Initializes the Queue list with the Arraylist from the Queue class\r
37         /// </summary>\r
38         /// <param name="qw"></param>\r
39         public void setQueue(Functions.Queue qw)\r
40         {\r
41             queue = qw;\r
42             redrawQueue();\r
43         }\r
44 \r
45         /// <summary>\r
46         /// Returns if there is currently an item being encoded by the queue\r
47         /// </summary>\r
48         /// <returns>Boolean true if encoding</returns>\r
49         public Boolean isEncoding()\r
50         {\r
51             if (hbProc == null)\r
52                 return false;\r
53             else\r
54                 return true;\r
55         }\r
56 \r
57         // Redraw's the queue with the latest data from the Queue class\r
58         private void redrawQueue()\r
59         {\r
60             list_queue.Items.Clear();\r
61             foreach (string queue_item in queue.getQueue())\r
62             {\r
63                 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue_item);\r
64 \r
65                 // Get the DVD Title\r
66                 string title = "";\r
67                 if (parsed.DVDTitle == 0)\r
68                     title = "Auto";\r
69                 else\r
70                     title = parsed.DVDTitle.ToString();\r
71 \r
72                 // Get the DVD Chapters\r
73                 string chapters = "";\r
74                 if (parsed.DVDChapterStart == 0)\r
75                     chapters = "Auto";\r
76                 else\r
77                 {\r
78                     chapters = parsed.DVDChapterStart.ToString();\r
79                     if (parsed.DVDChapterFinish != 0)\r
80                         chapters = chapters + " - " + parsed.DVDChapterFinish;\r
81                 }\r
82 \r
83                 ListViewItem item = new ListViewItem();\r
84                 item.Text = title; // Title\r
85                 item.SubItems.Add(chapters); // Chapters\r
86                 item.SubItems.Add(parsed.Source); // Source\r
87                 item.SubItems.Add(parsed.Destination); // Destination\r
88                 item.SubItems.Add(parsed.VideoEncoder); // Video\r
89                 item.SubItems.Add(parsed.AudioEncoder1); // Audio\r
90 \r
91                 list_queue.Items.Add(item);\r
92             }\r
93         }\r
94 \r
95         // Initializes the encode process\r
96         private void btn_encode_Click(object sender, EventArgs e)\r
97         {\r
98             if (queue.count() != 0)\r
99             {\r
100                 lbl_status.Visible = false;\r
101                 btn_encode.Enabled = false;\r
102             }\r
103             cancel = false;\r
104 \r
105             // Start the encode\r
106             try\r
107             {\r
108                 if (queue.count() != 0)\r
109                 {\r
110                     // Setup or reset some values\r
111                     btn_stop.Visible = true;\r
112                     progressBar.Value = 0;\r
113                     lbl_progressValue.Text = "0 %";\r
114                     progressBar.Step = 100 / queue.count();\r
115                     progressBar.Update();\r
116                     Thread theQ = new Thread(startProc);\r
117                     theQ.Start();\r
118                 }\r
119             }\r
120             catch (Exception exc)\r
121             {\r
122                 MessageBox.Show(exc.ToString());\r
123             }\r
124         }\r
125 \r
126         // Starts the encoding process\r
127         private void startProc(object state)\r
128         {\r
129             try\r
130             {\r
131                 // Run through each item on the queue\r
132                 while (queue.count() != 0)\r
133                 {\r
134                     string query = queue.getNextItemForEncoding();\r
135 \r
136                     setEncValue();\r
137                     updateUIElements();\r
138 \r
139                     hbProc = cliObj.runCli(this, query);\r
140 \r
141                     hbProc.WaitForExit();\r
142                     hbProc.Close();\r
143                     hbProc.Dispose();\r
144                     hbProc = null;\r
145 \r
146                     query = "";\r
147 \r
148                     if (cancel == true)\r
149                     {\r
150                         break;\r
151                     }\r
152                 }\r
153 \r
154                 resetQueue();\r
155 \r
156                 // After the encode is done, we may want to shutdown, suspend etc.\r
157                 cliObj.afterEncodeAction();\r
158             }\r
159             catch (Exception exc)\r
160             {\r
161                 MessageBox.Show(exc.ToString());\r
162             }\r
163         }\r
164 \r
165         // Reset's the window to the default state.\r
166         private void resetQueue()\r
167         {\r
168             try\r
169             {\r
170                 if (this.InvokeRequired)\r
171                 {\r
172                     this.BeginInvoke(new ProgressUpdateHandler(resetQueue));\r
173                     return;\r
174 \r
175                 }\r
176                 btn_stop.Visible = false;\r
177                 btn_encode.Enabled = true;\r
178 \r
179                 if (cancel == true)\r
180                 {\r
181                     lbl_status.Visible = true;\r
182                     lbl_status.Text = "Encode Queue Cancelled!";\r
183                 }\r
184                 else\r
185                 {\r
186                     lbl_status.Visible = true;\r
187                     lbl_status.Text = "Encode Queue Completed!";\r
188                 }\r
189 \r
190                 lbl_progressValue.Text = "0 %";\r
191                 progressBar.Value = 0;\r
192                 progressBar.Update();\r
193 \r
194                 lbl_source.Text = "-";\r
195                 lbl_dest.Text = "-";\r
196                 lbl_vEnc.Text = "-";\r
197                 lbl_aEnc.Text = "-";\r
198                 lbl_title.Text = "-";\r
199                 lbl_chapt.Text = "-";\r
200             }\r
201             catch (Exception exc)\r
202             {\r
203                 MessageBox.Show(exc.ToString());\r
204             }\r
205         }\r
206 \r
207         // Stop's the queue from continuing. \r
208         private void btn_stop_Click(object sender, EventArgs e)\r
209         {\r
210             cancel = true;\r
211             btn_stop.Visible = false;\r
212             btn_encode.Enabled = true;\r
213             MessageBox.Show("No further items on the queue will start. The current encode process will continue until it is finished. \nClick 'Encode Video' when you wish to continue encoding the queue.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
214         }\r
215 \r
216         // Updates the progress bar and progress label for a new status.\r
217         private void updateUIElements()\r
218         {\r
219             try\r
220             {\r
221                 if (this.InvokeRequired)\r
222                 {\r
223                     this.BeginInvoke(new ProgressUpdateHandler(updateUIElements));\r
224                     return;\r
225                 }\r
226 \r
227                 redrawQueue();\r
228 \r
229                 progressBar.PerformStep();\r
230                 lbl_progressValue.Text = string.Format("{0} %", progressBar.Value);\r
231                 progressBar.Update();\r
232             }\r
233             catch (Exception exc)\r
234             {\r
235                 MessageBox.Show(exc.ToString());\r
236             }\r
237         }\r
238 \r
239         // Set's the information lables about the current encode.\r
240         private void setEncValue()\r
241         {\r
242             try\r
243             {\r
244                 if (this.InvokeRequired)\r
245                 {\r
246                     this.BeginInvoke(new setEncoding(setEncValue));\r
247                 }\r
248 \r
249                 // found query is a global varible\r
250                 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue.getLastQuery());\r
251                 lbl_source.Text = parsed.Source;\r
252                 lbl_dest.Text = parsed.Destination;\r
253 \r
254 \r
255                 if (parsed.DVDTitle == 0)\r
256                     lbl_title.Text = "Auto";\r
257                 else\r
258                     lbl_title.Text = parsed.DVDTitle.ToString();\r
259 \r
260                 string chapters = "";\r
261                 if (parsed.DVDChapterStart == 0)\r
262                 {\r
263                     lbl_chapt.Text = "Auto";\r
264                 }\r
265                 else\r
266                 {\r
267                     chapters = parsed.DVDChapterStart.ToString();\r
268                     if (parsed.DVDChapterFinish != 0)\r
269                         chapters = chapters + " - " + parsed.DVDChapterFinish;\r
270                     lbl_chapt.Text = chapters;\r
271                 }\r
272 \r
273                 lbl_vEnc.Text = parsed.VideoEncoder;\r
274                 lbl_aEnc.Text = parsed.AudioEncoder1;\r
275             }\r
276             catch (Exception)\r
277             {\r
278                 // Do Nothing\r
279             }\r
280         }\r
281 \r
282         // Move an item up the Queue\r
283         private void btn_up_Click(object sender, EventArgs e)\r
284         {\r
285             if (list_queue.SelectedIndices.Count != 0)\r
286             {\r
287                 queue.moveUp(list_queue.SelectedIndices[0]);\r
288                 redrawQueue();\r
289             }\r
290         }\r
291 \r
292         // Move an item down the Queue\r
293         private void btn_down_Click(object sender, EventArgs e)\r
294         {\r
295             if (list_queue.SelectedIndices.Count != 0)\r
296             {\r
297                 queue.moveDown(list_queue.SelectedIndices[0]);\r
298                 redrawQueue();\r
299             }\r
300         }\r
301 \r
302         // Remove an item from the queue\r
303         private void btn_delete_Click(object sender, EventArgs e)\r
304         {\r
305             if (list_queue.SelectedIndices.Count != 0)\r
306             {\r
307                 queue.remove(list_queue.SelectedIndices[0]);\r
308                 redrawQueue();\r
309             }\r
310         }\r
311 \r
312         // Generate a batch file script to run the encode seperate of HandBrake\r
313         private void btn_batch_Click(object sender, EventArgs e)\r
314         {\r
315             string queries = "";\r
316             foreach (string query_item in queue.getQueue()) \r
317             {\r
318                 string fullQuery = '"' + Application.StartupPath.ToString() + "\\HandBrakeCLI.exe" + '"' + query_item;\r
319 \r
320                 if (queries == "")\r
321                     queries = queries + fullQuery;\r
322                 else\r
323                     queries = queries + " && " + fullQuery;\r
324             }\r
325             string strCmdLine = queries;\r
326 \r
327             SaveFile.ShowDialog();\r
328             string filename = SaveFile.FileName;\r
329 \r
330             if (filename != "")\r
331             {\r
332                 try\r
333                 {\r
334                     // Create a StreamWriter and open the file, Write the batch file query to the file and \r
335                     // Close the stream\r
336                     StreamWriter line = new StreamWriter(filename);\r
337                     line.WriteLine(strCmdLine);\r
338                     line.Close();\r
339 \r
340                     MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
341                 }\r
342                 catch (Exception)\r
343                 {\r
344                     MessageBox.Show("Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
345                 }\r
346 \r
347             }\r
348         }\r
349 \r
350         // Hide's the window from the users view.\r
351         private void btn_Close_Click(object sender, EventArgs e)\r
352         {\r
353             this.Hide();\r
354         }\r
355 \r
356         // Hide's the window when the user tries to "x" out of the window instead of closing it.\r
357         protected override void OnClosing(CancelEventArgs e)\r
358         {\r
359             e.Cancel = true;\r
360             this.Hide();\r
361             base.OnClosing(e);\r
362         }\r
363 \r
364     }\r
365 }