OSDN Git Service

e27241921e616643202961e91cda77923f5c630a
[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                     queue.write2disk("hb_queue_recovery.dat"); // Update the queue recovery file\r
137 \r
138                     setEncValue();\r
139                     updateUIElements();\r
140 \r
141                     hbProc = cliObj.runCli(this, query);\r
142 \r
143                     hbProc.WaitForExit();\r
144                     hbProc.Close();\r
145                     hbProc.Dispose();\r
146                     hbProc = null;\r
147 \r
148                     query = "";\r
149 \r
150                     if (cancel == true)\r
151                     {\r
152                         break;\r
153                     }\r
154                 }\r
155 \r
156                 resetQueue();\r
157 \r
158                 // After the encode is done, we may want to shutdown, suspend etc.\r
159                 cliObj.afterEncodeAction();\r
160             }\r
161             catch (Exception exc)\r
162             {\r
163                 MessageBox.Show(exc.ToString());\r
164             }\r
165         }\r
166 \r
167         // Reset's the window to the default state.\r
168         private void resetQueue()\r
169         {\r
170             try\r
171             {\r
172                 if (this.InvokeRequired)\r
173                 {\r
174                     this.BeginInvoke(new ProgressUpdateHandler(resetQueue));\r
175                     return;\r
176 \r
177                 }\r
178                 btn_stop.Visible = false;\r
179                 btn_encode.Enabled = true;\r
180 \r
181                 if (cancel == true)\r
182                 {\r
183                     lbl_progressValue.Text = "Encode Queue Cancelled!";\r
184                 }\r
185                 else\r
186                 {\r
187                     lbl_progressValue.Text = "Encode Queue Completed!";\r
188                 }\r
189 \r
190                 progressBar.Value = 0;\r
191 \r
192                 lbl_source.Text = "-";\r
193                 lbl_dest.Text = "-";\r
194                 lbl_vEnc.Text = "-";\r
195                 lbl_aEnc.Text = "-";\r
196                 lbl_title.Text = "-";\r
197                 lbl_chapt.Text = "-";\r
198             }\r
199             catch (Exception exc)\r
200             {\r
201                 MessageBox.Show(exc.ToString());\r
202             }\r
203         }\r
204 \r
205         // Stop's the queue from continuing. \r
206         private void btn_stop_Click(object sender, EventArgs e)\r
207         {\r
208             cancel = true;\r
209             btn_stop.Visible = false;\r
210             btn_encode.Enabled = true;\r
211             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
212         }\r
213 \r
214         // Updates the progress bar and progress label for a new status.\r
215         private void updateUIElements()\r
216         {\r
217             try\r
218             {\r
219                 if (this.InvokeRequired)\r
220                 {\r
221                     this.BeginInvoke(new ProgressUpdateHandler(updateUIElements));\r
222                     return;\r
223                 }\r
224 \r
225                 redrawQueue();\r
226 \r
227                 progressBar.PerformStep();\r
228                 lbl_progressValue.Text = string.Format("{0} %", progressBar.Value);\r
229             }\r
230             catch (Exception exc)\r
231             {\r
232                 MessageBox.Show(exc.ToString());\r
233             }\r
234         }\r
235 \r
236         // Set's the information lables about the current encode.\r
237         private void setEncValue()\r
238         {\r
239             try\r
240             {\r
241                 if (this.InvokeRequired)\r
242                 {\r
243                     this.BeginInvoke(new setEncoding(setEncValue));\r
244                 }\r
245 \r
246                 // found query is a global varible\r
247                 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue.getLastQuery());\r
248                 lbl_source.Text = parsed.Source;\r
249                 lbl_dest.Text = parsed.Destination;\r
250 \r
251 \r
252                 if (parsed.DVDTitle == 0)\r
253                     lbl_title.Text = "Auto";\r
254                 else\r
255                     lbl_title.Text = parsed.DVDTitle.ToString();\r
256 \r
257                 string chapters = "";\r
258                 if (parsed.DVDChapterStart == 0)\r
259                 {\r
260                     lbl_chapt.Text = "Auto";\r
261                 }\r
262                 else\r
263                 {\r
264                     chapters = parsed.DVDChapterStart.ToString();\r
265                     if (parsed.DVDChapterFinish != 0)\r
266                         chapters = chapters + " - " + parsed.DVDChapterFinish;\r
267                     lbl_chapt.Text = chapters;\r
268                 }\r
269 \r
270                 lbl_vEnc.Text = parsed.VideoEncoder;\r
271                 lbl_aEnc.Text = parsed.AudioEncoder1;\r
272             }\r
273             catch (Exception)\r
274             {\r
275                 // Do Nothing\r
276             }\r
277         }\r
278 \r
279         // Move an item up the Queue\r
280         private void btn_up_Click(object sender, EventArgs e)\r
281         {\r
282             if (list_queue.SelectedIndices.Count != 0)\r
283             {\r
284                 queue.moveUp(list_queue.SelectedIndices[0]);\r
285                 queue.write2disk("hb_queue_recovery.dat"); // Update the queue recovery file\r
286                 redrawQueue();\r
287             }\r
288         }\r
289 \r
290         // Move an item down the Queue\r
291         private void btn_down_Click(object sender, EventArgs e)\r
292         {\r
293             if (list_queue.SelectedIndices.Count != 0)\r
294             {\r
295                 queue.moveDown(list_queue.SelectedIndices[0]);\r
296                 queue.write2disk("hb_queue_recovery.dat"); // Update the queue recovery file\r
297                 redrawQueue();\r
298             }\r
299         }\r
300 \r
301         // Remove an item from the queue\r
302         private void btn_delete_Click(object sender, EventArgs e)\r
303         {\r
304             if (list_queue.SelectedIndices.Count != 0)\r
305             {\r
306                 queue.remove(list_queue.SelectedIndices[0]);\r
307                 queue.write2disk("hb_queue_recovery.dat"); // Update the queue recovery file\r
308                 redrawQueue();\r
309             }\r
310         }\r
311 \r
312         // Generate a Saveable batch script on the users request\r
313         private void mnu_batch_Click(object sender, EventArgs e)\r
314         {\r
315             SaveFile.Filter = "Batch|.bat";\r
316             SaveFile.ShowDialog();\r
317             if (SaveFile.FileName != String.Empty)\r
318                 queue.writeBatchScript(SaveFile.FileName);\r
319         }\r
320 \r
321         // Export the HandBrake Queue to a file.\r
322         private void mnu_export_Click(object sender, EventArgs e)\r
323         {\r
324             SaveFile.Filter = "HandBrake Queue|*.queue";\r
325             SaveFile.ShowDialog();\r
326             if (SaveFile.FileName != String.Empty)\r
327                 queue.write2disk(SaveFile.FileName);\r
328         }\r
329 \r
330         // Import an exported queue\r
331         private void mnu_import_Click(object sender, EventArgs e)\r
332         {\r
333             OpenFile.ShowDialog();\r
334             if (OpenFile.FileName != String.Empty)\r
335                 queue.recoverQueue(OpenFile.FileName);\r
336             redrawQueue();\r
337         }\r
338 \r
339         // Delete a selected item on the queue, if the delete key is pressed.\r
340         private void list_queue_deleteKey(object sender, KeyEventArgs e)\r
341         {\r
342             if (e.KeyCode == Keys.Delete)\r
343             {\r
344                 if (list_queue.SelectedIndices.Count != 0)\r
345                 {\r
346                     queue.remove(list_queue.SelectedIndices[0]);\r
347                     queue.write2disk("hb_queue_recovery.dat"); // Update the queue recovery file\r
348                     redrawQueue();\r
349                 }\r
350             }\r
351         }\r
352 \r
353         // Hide's the window from the users view.\r
354         private void btn_Close_Click(object sender, EventArgs e)\r
355         {\r
356             this.Hide();\r
357         }\r
358 \r
359         // Hide's the window when the user tries to "x" out of the window instead of closing it.\r
360         protected override void OnClosing(CancelEventArgs e)\r
361         {\r
362             e.Cancel = true;\r
363             this.Hide();\r
364             base.OnClosing(e);\r
365         }\r
366 \r
367 \r
368     }\r
369 }