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
8 using System.Collections.Generic;
\r
9 using System.Collections;
\r
10 using System.ComponentModel;
\r
12 using System.Drawing;
\r
14 using System.Windows.Forms;
\r
15 using System.Threading;
\r
16 using System.Diagnostics;
\r
17 using System.Runtime.InteropServices;
\r
22 public partial class frmQueue : Form
\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
32 public frmQueue(frmMain main)
\r
34 InitializeComponent();
\r
39 /// Initializes the Queue list with the Arraylist from the Queue class
\r
41 /// <param name="qw"></param>
\r
42 public void setQueue(Functions.Queue qw)
\r
46 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
50 /// Returns if there is currently an item being encoded by the queue
\r
52 /// <returns>Boolean true if encoding</returns>
\r
53 public Boolean isEncoding()
\r
61 // Redraw's the queue with the latest data from the Queue class
\r
62 private void redrawQueue()
\r
64 list_queue.Items.Clear();
\r
65 ArrayList theQueue = queue.getQueue();
\r
66 foreach (ArrayList queue_item in theQueue)
\r
68 string q_item = queue_item[1].ToString();
\r
69 Functions.QueryParser parsed = Functions.QueryParser.Parse(q_item);
\r
71 // Get the DVD Title
\r
73 if (parsed.DVDTitle == 0)
\r
76 title = parsed.DVDTitle.ToString();
\r
78 // Get the DVD Chapters
\r
79 string chapters = "";
\r
80 if (parsed.DVDChapterStart == 0)
\r
84 chapters = parsed.DVDChapterStart.ToString();
\r
85 if (parsed.DVDChapterFinish != 0)
\r
86 chapters = chapters + " - " + parsed.DVDChapterFinish;
\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
97 list_queue.Items.Add(item);
\r
101 // Initializes the encode process
\r
102 private void btn_encode_Click(object sender, EventArgs e)
\r
104 mainWindow.setLastAction("encode");
\r
106 if (queue.count() != 0)
\r
108 btn_encode.Enabled = false;
\r
112 // Start the encode
\r
115 if (queue.count() != 0)
\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
127 catch (Exception exc)
\r
129 MessageBox.Show(exc.ToString());
\r
133 // Starts the encoding process
\r
134 private void startProc(object state)
\r
138 // Run through each item on the queue
\r
139 while (queue.count() != 0)
\r
141 string query = queue.getNextItemForEncoding();
\r
142 queue.write2disk("hb_queue_recovery.dat"); // Update the queue recovery file
\r
145 updateUIElements();
\r
147 hbProc = cliObj.runCli(this, query);
\r
149 hbProc.WaitForExit();
\r
150 cliObj.addCLIQueryToLog(query);
\r
151 cliObj.copyLog(query);
\r
158 if (cancel == true)
\r
166 // After the encode is done, we may want to shutdown, suspend etc.
\r
167 cliObj.afterEncodeAction();
\r
169 catch (Exception exc)
\r
171 MessageBox.Show(exc.ToString());
\r
175 // Reset's the window to the default state.
\r
176 private void resetQueue()
\r
180 if (this.InvokeRequired)
\r
182 this.BeginInvoke(new ProgressUpdateHandler(resetQueue));
\r
186 btn_stop.Visible = false;
\r
187 btn_encode.Enabled = true;
\r
189 if (cancel == true)
\r
191 lbl_progressValue.Text = "Encode Queue Cancelled!";
\r
195 lbl_progressValue.Text = "Encode Queue Completed!";
\r
198 progressBar.Value = 0;
\r
200 lbl_source.Text = "-";
\r
201 lbl_dest.Text = "-";
\r
202 lbl_vEnc.Text = "-";
\r
203 lbl_aEnc.Text = "-";
\r
204 lbl_title.Text = "-";
\r
205 lbl_chapt.Text = "-";
\r
207 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
209 catch (Exception exc)
\r
211 MessageBox.Show(exc.ToString());
\r
215 // Stop's the queue from continuing.
\r
216 private void btn_stop_Click(object sender, EventArgs e)
\r
219 btn_stop.Visible = false;
\r
220 btn_encode.Enabled = true;
\r
221 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
224 // Updates the progress bar and progress label for a new status.
\r
225 private void updateUIElements()
\r
229 if (this.InvokeRequired)
\r
231 this.BeginInvoke(new ProgressUpdateHandler(updateUIElements));
\r
237 progressBar.PerformStep();
\r
238 lbl_progressValue.Text = string.Format("{0} %", progressBar.Value);
\r
239 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
241 catch (Exception exc)
\r
243 MessageBox.Show(exc.ToString());
\r
247 // Set's the information lables about the current encode.
\r
248 private void setEncValue()
\r
252 if (this.InvokeRequired)
\r
254 this.BeginInvoke(new setEncoding(setEncValue));
\r
257 // found query is a global varible
\r
258 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue.getLastQuery());
\r
259 lbl_source.Text = parsed.Source;
\r
260 lbl_dest.Text = parsed.Destination;
\r
263 if (parsed.DVDTitle == 0)
\r
264 lbl_title.Text = "Auto";
\r
266 lbl_title.Text = parsed.DVDTitle.ToString();
\r
268 string chapters = "";
\r
269 if (parsed.DVDChapterStart == 0)
\r
271 lbl_chapt.Text = "Auto";
\r
275 chapters = parsed.DVDChapterStart.ToString();
\r
276 if (parsed.DVDChapterFinish != 0)
\r
277 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
278 lbl_chapt.Text = chapters;
\r
281 lbl_vEnc.Text = parsed.VideoEncoder;
\r
282 lbl_aEnc.Text = parsed.AudioEncoder1;
\r
290 // Move an item up the Queue
\r
291 private void btn_up_Click(object sender, EventArgs e)
\r
293 if (list_queue.SelectedIndices.Count != 0)
\r
295 int selected = list_queue.SelectedIndices[0];
\r
297 queue.moveUp(selected);
\r
298 queue.write2disk("hb_queue_recovery.dat"); // Update the queue recovery file
\r
301 if (selected - 1 > 0)
\r
302 list_queue.Items[selected -1].Selected = true;
\r
304 list_queue.Select();
\r
308 // Move an item down the Queue
\r
309 private void btn_down_Click(object sender, EventArgs e)
\r
311 if (list_queue.SelectedIndices.Count != 0)
\r
313 int selected = list_queue.SelectedIndices[0];
\r
315 queue.moveDown(list_queue.SelectedIndices[0]);
\r
316 queue.write2disk("hb_queue_recovery.dat"); // Update the queue recovery file
\r
319 if (selected +1 < list_queue.Items.Count)
\r
320 list_queue.Items[selected + 1].Selected = true;
\r
322 list_queue.Select();
\r
326 // Remove an item from the queue
\r
327 private void btn_delete_Click(object sender, EventArgs e)
\r
329 if (list_queue.SelectedIndices.Count != 0)
\r
331 queue.remove(list_queue.SelectedIndices[0]);
\r
332 queue.write2disk("hb_queue_recovery.dat"); // Update the queue recovery file
\r
334 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
338 // Generate a Saveable batch script on the users request
\r
339 private void mnu_batch_Click(object sender, EventArgs e)
\r
341 SaveFile.FileName = "";
\r
342 SaveFile.Filter = "Batch|.bat";
\r
343 SaveFile.ShowDialog();
\r
344 if (SaveFile.FileName != String.Empty)
\r
345 queue.writeBatchScript(SaveFile.FileName);
\r
348 // Export the HandBrake Queue to a file.
\r
349 private void mnu_export_Click(object sender, EventArgs e)
\r
351 SaveFile.FileName = "";
\r
352 SaveFile.Filter = "HandBrake Queue|*.queue";
\r
353 SaveFile.ShowDialog();
\r
354 if (SaveFile.FileName != String.Empty)
\r
355 queue.write2disk(SaveFile.FileName);
\r
358 // Import an exported queue
\r
359 private void mnu_import_Click(object sender, EventArgs e)
\r
361 OpenFile.FileName = "";
\r
362 OpenFile.ShowDialog();
\r
363 if (OpenFile.FileName != String.Empty)
\r
364 queue.recoverQueue(OpenFile.FileName);
\r
368 // Delete a selected item on the queue, if the delete key is pressed.
\r
369 private void list_queue_deleteKey(object sender, KeyEventArgs e)
\r
371 if (e.KeyCode == Keys.Delete)
\r
373 if (list_queue.SelectedIndices.Count != 0)
\r
375 queue.remove(list_queue.SelectedIndices[0]);
\r
376 queue.write2disk("hb_queue_recovery.dat"); // Update the queue recovery file
\r
382 // Hide's the window from the users view.
\r
383 private void btn_Close_Click(object sender, EventArgs e)
\r
388 // Hide's the window when the user tries to "x" out of the window instead of closing it.
\r
389 protected override void OnClosing(CancelEventArgs e)
\r