OSDN Git Service

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