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 Thread theQ = new Thread(startProc);
\r
85 theQ.IsBackground = true;
\r
89 catch (Exception exc)
\r
91 MessageBox.Show(exc.ToString());
\r
95 // Redraw's the queue with the latest data from the Queue class
\r
96 private void redrawQueue()
\r
98 list_queue.Items.Clear();
\r
99 List<Queue.QueueItem> theQueue = queue.getQueue();
\r
100 foreach (Queue.QueueItem queue_item in theQueue)
\r
102 string q_item = queue_item.Query;
\r
103 Functions.QueryParser parsed = Functions.QueryParser.Parse(q_item);
\r
105 // Get the DVD Title
\r
107 if (parsed.DVDTitle == 0)
\r
110 title = parsed.DVDTitle.ToString();
\r
112 // Get the DVD Chapters
\r
113 string chapters = "";
\r
114 if (parsed.DVDChapterStart == 0)
\r
118 chapters = parsed.DVDChapterStart.ToString();
\r
119 if (parsed.DVDChapterFinish != 0)
\r
120 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
123 ListViewItem item = new ListViewItem();
\r
124 item.Text = title; // Title
\r
125 item.SubItems.Add(chapters); // Chapters
\r
126 item.SubItems.Add(queue_item.Source); // Source
\r
127 item.SubItems.Add(queue_item.Destination); // Destination
\r
128 item.SubItems.Add(parsed.VideoEncoder); // Video
\r
129 item.SubItems.Add(parsed.AudioEncoder1); // Audio
\r
131 list_queue.Items.Add(item);
\r
135 // Initializes the encode process
\r
136 private void btn_encode_Click(object sender, EventArgs e)
\r
138 mainWindow.setLastAction("encode");
\r
139 mainWindow.setEncodeStatus(1);
\r
141 if (queue.count() != 0)
\r
143 btn_encode.Enabled = false;
\r
147 // Start the encode
\r
150 if (queue.count() != 0)
\r
152 // Setup or reset some values
\r
153 btn_encode.Enabled = false;
\r
154 btn_stop.Visible = true;
\r
155 progressBar.Value = 0;
\r
156 lbl_progressValue.Text = "0 %";
\r
157 if (queue.count() == 0)
\r
158 progressBar.Step = 100;
\r
160 progressBar.Step = 100 / queue.count();
\r
161 Thread theQ = new Thread(startProc);
\r
162 theQ.IsBackground = true;
\r
166 catch (Exception exc)
\r
168 MessageBox.Show(exc.ToString());
\r
172 // Starts the encoding process
\r
173 private void startProc(object state)
\r
177 // Run through each item on the queue
\r
178 while (queue.count() != 0)
\r
180 string query = queue.getNextItemForEncoding();
\r
181 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
185 updateUIElements();
\r
187 hbProc = cliObj.runCli(this, query);
\r
189 hbProc.WaitForExit();
\r
190 cliObj.addCLIQueryToLog(query);
\r
191 cliObj.copyLog(query, queue.getLastQuery().Destination);
\r
198 if (cancel == true)
\r
205 mainWindow.setEncodeStatus(0); // Tell the main window encodes have finished.
\r
207 // After the encode is done, we may want to shutdown, suspend etc.
\r
208 cliObj.afterEncodeAction();
\r
210 catch (Exception exc)
\r
212 MessageBox.Show(exc.ToString());
\r
216 // Reset's the window to the default state.
\r
217 private void resetQueue()
\r
221 if (this.InvokeRequired)
\r
223 this.BeginInvoke(new ProgressUpdateHandler(resetQueue));
\r
227 btn_stop.Visible = false;
\r
228 btn_encode.Enabled = true;
\r
230 if (cancel == true)
\r
232 lbl_progressValue.Text = "Encode Queue Cancelled!";
\r
236 lbl_progressValue.Text = "Encode Queue Completed!";
\r
239 progressBar.Value = 0;
\r
241 lbl_source.Text = "-";
\r
242 lbl_dest.Text = "-";
\r
243 lbl_vEnc.Text = "-";
\r
244 lbl_aEnc.Text = "-";
\r
245 lbl_title.Text = "-";
\r
246 lbl_chapt.Text = "-";
\r
248 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
250 catch (Exception exc)
\r
252 MessageBox.Show(exc.ToString());
\r
256 // Stop's the queue from continuing.
\r
257 private void btn_stop_Click(object sender, EventArgs e)
\r
260 btn_stop.Visible = false;
\r
261 btn_encode.Enabled = true;
\r
262 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
265 // Updates the progress bar and progress label for a new status.
\r
266 private void updateUIElements()
\r
270 if (this.InvokeRequired)
\r
272 this.BeginInvoke(new ProgressUpdateHandler(updateUIElements));
\r
278 progressBar.PerformStep();
\r
279 lbl_progressValue.Text = string.Format("{0} %", progressBar.Value);
\r
280 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
282 catch (Exception exc)
\r
284 MessageBox.Show(exc.ToString());
\r
288 // Set's the information lables about the current encode.
\r
289 private void setEncValue()
\r
293 if (this.InvokeRequired)
\r
295 this.BeginInvoke(new setEncoding(setEncValue));
\r
298 // found query is a global varible
\r
299 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue.getLastQuery().Query);
\r
300 lbl_source.Text = queue.getLastQuery().Source;
\r
301 lbl_dest.Text = queue.getLastQuery().Destination;
\r
304 if (parsed.DVDTitle == 0)
\r
305 lbl_title.Text = "Auto";
\r
307 lbl_title.Text = parsed.DVDTitle.ToString();
\r
309 string chapters = "";
\r
310 if (parsed.DVDChapterStart == 0)
\r
312 lbl_chapt.Text = "Auto";
\r
316 chapters = parsed.DVDChapterStart.ToString();
\r
317 if (parsed.DVDChapterFinish != 0)
\r
318 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
319 lbl_chapt.Text = chapters;
\r
322 lbl_vEnc.Text = parsed.VideoEncoder;
\r
323 lbl_aEnc.Text = parsed.AudioEncoder1;
\r
331 // Queue Management
\r
332 private void btn_up_Click(object sender, EventArgs e)
\r
334 if (list_queue.SelectedIndices.Count != 0)
\r
336 int selected = list_queue.SelectedIndices[0];
\r
338 queue.moveUp(selected);
\r
339 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
342 if (selected - 1 > 0)
\r
343 list_queue.Items[selected -1].Selected = true;
\r
345 list_queue.Select();
\r
348 private void btn_down_Click(object sender, EventArgs e)
\r
350 if (list_queue.SelectedIndices.Count != 0)
\r
352 int selected = list_queue.SelectedIndices[0];
\r
354 queue.moveDown(list_queue.SelectedIndices[0]);
\r
355 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
358 if (selected +1 < list_queue.Items.Count)
\r
359 list_queue.Items[selected + 1].Selected = true;
\r
361 list_queue.Select();
\r
364 private void btn_delete_Click(object sender, EventArgs e)
\r
366 if (list_queue.SelectedIndices.Count != 0)
\r
368 queue.remove(list_queue.SelectedIndices[0]);
\r
369 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
371 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
374 private void list_queue_deleteKey(object sender, KeyEventArgs e)
\r
376 if (e.KeyCode == Keys.Delete)
\r
378 if (list_queue.SelectedIndices.Count != 0)
\r
380 queue.remove(list_queue.SelectedIndices[0]);
\r
381 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
387 // Queue Import/Export Features
\r
388 private void mnu_batch_Click(object sender, EventArgs e)
\r
390 SaveFile.FileName = "";
\r
391 SaveFile.Filter = "Batch|.bat";
\r
392 SaveFile.ShowDialog();
\r
393 if (SaveFile.FileName != String.Empty)
\r
394 queue.writeBatchScript(SaveFile.FileName);
\r
396 private void mnu_export_Click(object sender, EventArgs e)
\r
398 SaveFile.FileName = "";
\r
399 SaveFile.Filter = "HandBrake Queue|*.queue";
\r
400 SaveFile.ShowDialog();
\r
401 if (SaveFile.FileName != String.Empty)
\r
402 queue.write2disk(SaveFile.FileName);
\r
404 private void mnu_import_Click(object sender, EventArgs e)
\r
406 OpenFile.FileName = "";
\r
407 OpenFile.ShowDialog();
\r
408 if (OpenFile.FileName != String.Empty)
\r
409 queue.recoverQueue(OpenFile.FileName);
\r
413 // Hide's the window when the user tries to "x" out of the window instead of closing it.
\r
414 protected override void OnClosing(CancelEventArgs e)
\r