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
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(Queue.Queue qw)
\r
46 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
48 // Recalculate the progress bar, but only if the queue has already started.
\r
49 if (progressBar.Value != 0)
\r
51 progressBar.Value = 0;
\r
52 if (queue.count() == 0)
\r
53 progressBar.Step = 100;
\r
55 progressBar.Step = 100 / queue.count();
\r
56 progressBar.PerformStep();
\r
57 lbl_progressValue.Text = string.Format("{0} %", progressBar.Value);
\r
62 /// Returns if there is currently an item being encoded by the queue
\r
64 /// <returns>Boolean true if encoding</returns>
\r
65 public Boolean isEncoding()
\r
74 /// This disables encoding from the queue when a single encode from the main window is running.
\r
76 public void frmMain_encode()
\r
82 if (queue.count() != 0)
\r
84 // Setup or reset some values
\r
85 btn_encode.Enabled = false;
\r
86 btn_stop.Visible = true;
\r
87 progressBar.Value = 0;
\r
88 lbl_progressValue.Text = "0 %";
\r
89 if (queue.count() == 0)
\r
90 progressBar.Step = 100;
\r
92 progressBar.Step = 100 / queue.count();
\r
93 Thread theQ = new Thread(startProc);
\r
94 theQ.IsBackground = true;
\r
98 catch (Exception exc)
\r
100 MessageBox.Show(exc.ToString());
\r
104 // Redraw's the queue with the latest data from the Queue class
\r
105 private void redrawQueue()
\r
107 list_queue.Items.Clear();
\r
108 List<Queue.QueueItem> theQueue = queue.getQueue();
\r
109 foreach (Queue.QueueItem queue_item in theQueue)
\r
111 string q_item = queue_item.Query;
\r
112 Functions.QueryParser parsed = Functions.QueryParser.Parse(q_item);
\r
114 // Get the DVD Title
\r
116 if (parsed.DVDTitle == 0)
\r
119 title = parsed.DVDTitle.ToString();
\r
121 // Get the DVD Chapters
\r
122 string chapters = "";
\r
123 if (parsed.DVDChapterStart == 0)
\r
127 chapters = parsed.DVDChapterStart.ToString();
\r
128 if (parsed.DVDChapterFinish != 0)
\r
129 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
132 ListViewItem item = new ListViewItem();
\r
133 item.Text = title; // Title
\r
134 item.SubItems.Add(chapters); // Chapters
\r
135 item.SubItems.Add(queue_item.Source); // Source
\r
136 item.SubItems.Add(queue_item.Destination); // Destination
\r
137 item.SubItems.Add(parsed.VideoEncoder); // Video
\r
138 item.SubItems.Add(parsed.AudioEncoder1); // Audio
\r
140 list_queue.Items.Add(item);
\r
144 // Initializes the encode process
\r
145 private void btn_encode_Click(object sender, EventArgs e)
\r
147 if (queue.count() != 0)
\r
149 btn_encode.Enabled = false;
\r
150 mainWindow.setLastAction("encode");
\r
151 mainWindow.setEncodeStatus(1);
\r
155 // Start the encode
\r
158 // Setup or reset some values
\r
159 btn_encode.Enabled = false;
\r
160 btn_stop.Visible = true;
\r
161 progressBar.Value = 0;
\r
162 lbl_progressValue.Text = "0 %";
\r
163 if (queue.count() == 0)
\r
164 progressBar.Step = 100;
\r
166 progressBar.Step = 100 / queue.count();
\r
167 Thread theQ = new Thread(startProc);
\r
168 theQ.IsBackground = true;
\r
171 catch (Exception exc)
\r
173 MessageBox.Show(exc.ToString());
\r
178 // Starts the encoding process
\r
179 private void startProc(object state)
\r
183 // Run through each item on the queue
\r
184 while (queue.count() != 0)
\r
186 string query = queue.getNextItemForEncoding();
\r
187 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
191 updateUIElements();
\r
193 hbProc = cliObj.runCli(this, query);
\r
195 hbProc.WaitForExit();
\r
196 cliObj.addCLIQueryToLog(query);
\r
197 cliObj.copyLog(query, queue.getLastQuery().Destination);
\r
204 if (cancel == true)
\r
211 mainWindow.setEncodeStatus(0); // Tell the main window encodes have finished.
\r
213 // After the encode is done, we may want to shutdown, suspend etc.
\r
214 cliObj.afterEncodeAction();
\r
216 catch (Exception exc)
\r
218 MessageBox.Show(exc.ToString());
\r
222 // Reset's the window to the default state.
\r
223 private void resetQueue()
\r
227 if (this.InvokeRequired)
\r
229 this.BeginInvoke(new ProgressUpdateHandler(resetQueue));
\r
233 btn_stop.Visible = false;
\r
234 btn_encode.Enabled = true;
\r
236 if (cancel == true)
\r
238 lbl_progressValue.Text = "Encode Queue Cancelled!";
\r
242 lbl_progressValue.Text = "Encode Queue Completed!";
\r
245 progressBar.Value = 0;
\r
247 lbl_source.Text = "-";
\r
248 lbl_dest.Text = "-";
\r
249 lbl_vEnc.Text = "-";
\r
250 lbl_aEnc.Text = "-";
\r
251 lbl_title.Text = "-";
\r
252 lbl_chapt.Text = "-";
\r
254 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
256 catch (Exception exc)
\r
258 MessageBox.Show(exc.ToString());
\r
262 // Stop's the queue from continuing.
\r
263 private void btn_stop_Click(object sender, EventArgs e)
\r
266 btn_stop.Visible = false;
\r
267 btn_encode.Enabled = true;
\r
268 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
271 // Updates the progress bar and progress label for a new status.
\r
272 private void updateUIElements()
\r
276 if (this.InvokeRequired)
\r
278 this.BeginInvoke(new ProgressUpdateHandler(updateUIElements));
\r
284 progressBar.PerformStep();
\r
285 lbl_progressValue.Text = string.Format("{0} %", progressBar.Value);
\r
286 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
288 catch (Exception exc)
\r
290 MessageBox.Show(exc.ToString());
\r
294 // Set's the information lables about the current encode.
\r
295 private void setEncValue()
\r
299 if (this.InvokeRequired)
\r
301 this.BeginInvoke(new setEncoding(setEncValue));
\r
304 // found query is a global varible
\r
305 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue.getLastQuery().Query);
\r
306 lbl_source.Text = queue.getLastQuery().Source;
\r
307 lbl_dest.Text = queue.getLastQuery().Destination;
\r
310 if (parsed.DVDTitle == 0)
\r
311 lbl_title.Text = "Auto";
\r
313 lbl_title.Text = parsed.DVDTitle.ToString();
\r
315 string chapters = "";
\r
316 if (parsed.DVDChapterStart == 0)
\r
318 lbl_chapt.Text = "Auto";
\r
322 chapters = parsed.DVDChapterStart.ToString();
\r
323 if (parsed.DVDChapterFinish != 0)
\r
324 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
325 lbl_chapt.Text = chapters;
\r
328 lbl_vEnc.Text = parsed.VideoEncoder;
\r
329 lbl_aEnc.Text = parsed.AudioEncoder1;
\r
337 // Queue Management
\r
338 private void btn_up_Click(object sender, EventArgs e)
\r
340 if (list_queue.SelectedIndices.Count != 0)
\r
342 int selected = list_queue.SelectedIndices[0];
\r
344 queue.moveUp(selected);
\r
345 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
348 if (selected - 1 > 0)
\r
349 list_queue.Items[selected - 1].Selected = true;
\r
351 list_queue.Select();
\r
354 private void btn_down_Click(object sender, EventArgs e)
\r
356 if (list_queue.SelectedIndices.Count != 0)
\r
358 int selected = list_queue.SelectedIndices[0];
\r
360 queue.moveDown(list_queue.SelectedIndices[0]);
\r
361 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
364 if (selected + 1 < list_queue.Items.Count)
\r
365 list_queue.Items[selected + 1].Selected = true;
\r
367 list_queue.Select();
\r
370 private void btn_delete_Click(object sender, EventArgs e)
\r
372 if (list_queue.SelectedIndices.Count != 0)
\r
374 queue.remove(list_queue.SelectedIndices[0]);
\r
375 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
377 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
380 private void list_queue_deleteKey(object sender, KeyEventArgs e)
\r
382 if (e.KeyCode == Keys.Delete)
\r
384 if (list_queue.SelectedIndices.Count != 0)
\r
386 queue.remove(list_queue.SelectedIndices[0]);
\r
387 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
393 // Queue Import/Export Features
\r
394 private void mnu_batch_Click(object sender, EventArgs e)
\r
396 SaveFile.FileName = "";
\r
397 SaveFile.Filter = "Batch|.bat";
\r
398 SaveFile.ShowDialog();
\r
399 if (SaveFile.FileName != String.Empty)
\r
400 queue.writeBatchScript(SaveFile.FileName);
\r
402 private void mnu_export_Click(object sender, EventArgs e)
\r
404 SaveFile.FileName = "";
\r
405 SaveFile.Filter = "HandBrake Queue|*.queue";
\r
406 SaveFile.ShowDialog();
\r
407 if (SaveFile.FileName != String.Empty)
\r
408 queue.write2disk(SaveFile.FileName);
\r
410 private void mnu_import_Click(object sender, EventArgs e)
\r
412 OpenFile.FileName = "";
\r
413 OpenFile.ShowDialog();
\r
414 if (OpenFile.FileName != String.Empty)
\r
415 queue.recoverQueue(OpenFile.FileName);
\r
419 // Hide's the window when the user tries to "x" out of the window instead of closing it.
\r
420 protected override void OnClosing(CancelEventArgs e)
\r