OSDN Git Service

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