OSDN Git Service

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