OSDN Git Service

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