OSDN Git Service

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