OSDN Git Service

1cf2930336683afba8fc82283593748a5c405bfe
[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.ComponentModel;\r
10 using System.Data;\r
11 using System.Drawing;\r
12 using System.Text;\r
13 using System.Windows.Forms;\r
14 using System.Threading;\r
15 using System.Diagnostics;\r
16 using System.Runtime.InteropServices;\r
17 using System.IO;\r
18 \r
19 namespace Handbrake\r
20 {\r
21     public partial class frmQueue : Form\r
22     {\r
23         private delegate void ProgressUpdateHandler();\r
24         private delegate void setEncoding();\r
25         Functions.Encode cliObj = new Functions.Encode();\r
26         Boolean cancel = false;\r
27         Process hbProc = null;\r
28         Functions.Queue queue;\r
29 \r
30         public frmQueue()\r
31         {\r
32             InitializeComponent();\r
33         }\r
34 \r
35         /// <summary>\r
36         /// Initializes the Queue list with the Arraylist from the Queue class\r
37         /// </summary>\r
38         /// <param name="qw"></param>\r
39         public void setQueue(Functions.Queue qw)\r
40         {\r
41             queue = qw;\r
42             redrawQueue();\r
43         }\r
44 \r
45         /// <summary>\r
46         /// Returns if there is currently an item being encoded by the queue\r
47         /// </summary>\r
48         /// <returns>Boolean true if encoding</returns>\r
49         public Boolean isEncoding()\r
50         {\r
51             if (hbProc == null)\r
52                 return false;\r
53             else\r
54                 return true;\r
55         }\r
56 \r
57         // Redraw's the queue with the latest data from the Queue class\r
58         private void redrawQueue()\r
59         {\r
60             list_queue.Items.Clear();\r
61             foreach (string queue_item in queue.getQueue())\r
62             {\r
63                 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue_item);\r
64 \r
65                 // Get the DVD Title\r
66                 string title = "";\r
67                 if (parsed.DVDTitle == 0)\r
68                     title = "Auto";\r
69                 else\r
70                     title = parsed.DVDTitle.ToString();\r
71 \r
72                 // Get the DVD Chapters\r
73                 string chapters = "";\r
74                 if (parsed.DVDChapterStart == 0)\r
75                     chapters = "Auto";\r
76                 else\r
77                 {\r
78                     chapters = parsed.DVDChapterStart.ToString();\r
79                     if (parsed.DVDChapterFinish != 0)\r
80                         chapters = chapters + " - " + parsed.DVDChapterFinish;\r
81                 }\r
82 \r
83                 ListViewItem item = new ListViewItem();\r
84                 item.Text = title; // Title\r
85                 item.SubItems.Add(chapters); // Chapters\r
86                 item.SubItems.Add(parsed.Source); // Source\r
87                 item.SubItems.Add(parsed.Destination); // Destination\r
88                 item.SubItems.Add(parsed.VideoEncoder); // Video\r
89                 item.SubItems.Add(parsed.AudioEncoder1); // Audio\r
90 \r
91                 list_queue.Items.Add(item);\r
92             }\r
93         }\r
94 \r
95         // Initializes the encode process\r
96         private void btn_encode_Click(object sender, EventArgs e)\r
97         {\r
98             if (queue.count() != 0)\r
99             {\r
100                 btn_encode.Enabled = false;\r
101             }\r
102             cancel = false;\r
103 \r
104             // Start the encode\r
105             try\r
106             {\r
107                 if (queue.count() != 0)\r
108                 {\r
109                     // Setup or reset some values\r
110                     btn_stop.Visible = true;\r
111                     progressBar.Value = 0;\r
112                     lbl_progressValue.Text = "0 %";\r
113                     progressBar.Step = 100 / queue.count();   \r
114                     Thread theQ = new Thread(startProc);\r
115                     theQ.IsBackground = true;\r
116                     theQ.Start();\r
117                 }\r
118             }\r
119             catch (Exception exc)\r
120             {\r
121                 MessageBox.Show(exc.ToString());\r
122             }\r
123         }\r
124 \r
125         // Starts the encoding process\r
126         private void startProc(object state)\r
127         {\r
128             try\r
129             {\r
130                 // Run through each item on the queue\r
131                 while (queue.count() != 0)\r
132                 {\r
133                     string query = queue.getNextItemForEncoding();\r
134 \r
135                     setEncValue();\r
136                     updateUIElements();\r
137 \r
138                     hbProc = cliObj.runCli(this, query);\r
139 \r
140                     hbProc.WaitForExit();\r
141                     hbProc.Close();\r
142                     hbProc.Dispose();\r
143                     hbProc = null;\r
144 \r
145                     query = "";\r
146 \r
147                     if (cancel == true)\r
148                     {\r
149                         break;\r
150                     }\r
151                 }\r
152 \r
153                 resetQueue();\r
154 \r
155                 // After the encode is done, we may want to shutdown, suspend etc.\r
156                 cliObj.afterEncodeAction();\r
157             }\r
158             catch (Exception exc)\r
159             {\r
160                 MessageBox.Show(exc.ToString());\r
161             }\r
162         }\r
163 \r
164         // Reset's the window to the default state.\r
165         private void resetQueue()\r
166         {\r
167             try\r
168             {\r
169                 if (this.InvokeRequired)\r
170                 {\r
171                     this.BeginInvoke(new ProgressUpdateHandler(resetQueue));\r
172                     return;\r
173 \r
174                 }\r
175                 btn_stop.Visible = false;\r
176                 btn_encode.Enabled = true;\r
177 \r
178                 if (cancel == true)\r
179                 {\r
180                     lbl_progressValue.Text = "Encode Queue Cancelled!";\r
181                 }\r
182                 else\r
183                 {\r
184                     lbl_progressValue.Text = "Encode Queue Completed!";\r
185                 }\r
186 \r
187                 progressBar.Value = 0;\r
188 \r
189                 lbl_source.Text = "-";\r
190                 lbl_dest.Text = "-";\r
191                 lbl_vEnc.Text = "-";\r
192                 lbl_aEnc.Text = "-";\r
193                 lbl_title.Text = "-";\r
194                 lbl_chapt.Text = "-";\r
195             }\r
196             catch (Exception exc)\r
197             {\r
198                 MessageBox.Show(exc.ToString());\r
199             }\r
200         }\r
201 \r
202         // Stop's the queue from continuing. \r
203         private void btn_stop_Click(object sender, EventArgs e)\r
204         {\r
205             cancel = true;\r
206             btn_stop.Visible = false;\r
207             btn_encode.Enabled = true;\r
208             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
209         }\r
210 \r
211         // Updates the progress bar and progress label for a new status.\r
212         private void updateUIElements()\r
213         {\r
214             try\r
215             {\r
216                 if (this.InvokeRequired)\r
217                 {\r
218                     this.BeginInvoke(new ProgressUpdateHandler(updateUIElements));\r
219                     return;\r
220                 }\r
221 \r
222                 redrawQueue();\r
223 \r
224                 progressBar.PerformStep();\r
225                 lbl_progressValue.Text = string.Format("{0} %", progressBar.Value);\r
226             }\r
227             catch (Exception exc)\r
228             {\r
229                 MessageBox.Show(exc.ToString());\r
230             }\r
231         }\r
232 \r
233         // Set's the information lables about the current encode.\r
234         private void setEncValue()\r
235         {\r
236             try\r
237             {\r
238                 if (this.InvokeRequired)\r
239                 {\r
240                     this.BeginInvoke(new setEncoding(setEncValue));\r
241                 }\r
242 \r
243                 // found query is a global varible\r
244                 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue.getLastQuery());\r
245                 lbl_source.Text = parsed.Source;\r
246                 lbl_dest.Text = parsed.Destination;\r
247 \r
248 \r
249                 if (parsed.DVDTitle == 0)\r
250                     lbl_title.Text = "Auto";\r
251                 else\r
252                     lbl_title.Text = parsed.DVDTitle.ToString();\r
253 \r
254                 string chapters = "";\r
255                 if (parsed.DVDChapterStart == 0)\r
256                 {\r
257                     lbl_chapt.Text = "Auto";\r
258                 }\r
259                 else\r
260                 {\r
261                     chapters = parsed.DVDChapterStart.ToString();\r
262                     if (parsed.DVDChapterFinish != 0)\r
263                         chapters = chapters + " - " + parsed.DVDChapterFinish;\r
264                     lbl_chapt.Text = chapters;\r
265                 }\r
266 \r
267                 lbl_vEnc.Text = parsed.VideoEncoder;\r
268                 lbl_aEnc.Text = parsed.AudioEncoder1;\r
269             }\r
270             catch (Exception)\r
271             {\r
272                 // Do Nothing\r
273             }\r
274         }\r
275 \r
276         // Move an item up the Queue\r
277         private void btn_up_Click(object sender, EventArgs e)\r
278         {\r
279             if (list_queue.SelectedIndices.Count != 0)\r
280             {\r
281                 queue.moveUp(list_queue.SelectedIndices[0]);\r
282                 queue.write2disk(); // Update the queue recovery file\r
283                 redrawQueue();\r
284             }\r
285         }\r
286 \r
287         // Move an item down the Queue\r
288         private void btn_down_Click(object sender, EventArgs e)\r
289         {\r
290             if (list_queue.SelectedIndices.Count != 0)\r
291             {\r
292                 queue.moveDown(list_queue.SelectedIndices[0]);\r
293                 queue.write2disk(); // Update the queue recovery file\r
294                 redrawQueue();\r
295             }\r
296         }\r
297 \r
298         // Remove an item from the queue\r
299         private void btn_delete_Click(object sender, EventArgs e)\r
300         {\r
301             if (list_queue.SelectedIndices.Count != 0)\r
302             {\r
303                 queue.remove(list_queue.SelectedIndices[0]);\r
304                 queue.write2disk(); // Update the queue recovery file\r
305                 redrawQueue();\r
306             }\r
307         }\r
308 \r
309         // Generate a batch file script to run the encode seperate of HandBrake\r
310         private void btn_batch_Click(object sender, EventArgs e)\r
311         {\r
312             string queries = "";\r
313             foreach (string query_item in queue.getQueue()) \r
314             {\r
315                 string fullQuery = '"' + Application.StartupPath.ToString() + "\\HandBrakeCLI.exe" + '"' + query_item;\r
316 \r
317                 if (queries == string.Empty)\r
318                     queries = queries + fullQuery;\r
319                 else\r
320                     queries = queries + " && " + fullQuery;\r
321             }\r
322             string strCmdLine = queries;\r
323 \r
324             SaveFile.ShowDialog();\r
325             string filename = SaveFile.FileName;\r
326 \r
327             if (filename != "")\r
328             {\r
329                 try\r
330                 {\r
331                     // Create a StreamWriter and open the file, Write the batch file query to the file and \r
332                     // Close the stream\r
333                     StreamWriter line = new StreamWriter(filename);\r
334                     line.WriteLine(strCmdLine);\r
335                     line.Close();\r
336 \r
337                     MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
338                 }\r
339                 catch (Exception)\r
340                 {\r
341                     MessageBox.Show("Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
342                 }\r
343 \r
344             }\r
345         }\r
346 \r
347         // Hide's the window from the users view.\r
348         private void btn_Close_Click(object sender, EventArgs e)\r
349         {\r
350             this.Hide();\r
351         }\r
352 \r
353         // Hide's the window when the user tries to "x" out of the window instead of closing it.\r
354         protected override void OnClosing(CancelEventArgs e)\r
355         {\r
356             e.Cancel = true;\r
357             this.Hide();\r
358             base.OnClosing(e);\r
359         }\r
360 \r
361     }\r
362 }