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
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
62 /// This disables encoding from the queue when a single encode from the main window is running.
\r
64 public void frmMain_encode()
\r
70 if (queue.count() != 0)
\r
72 // Setup or reset some values
\r
73 btn_encode.Enabled = false;
\r
74 btn_stop.Visible = true;
\r
76 Thread theQ = new Thread(startProc);
\r
77 theQ.IsBackground = true;
\r
81 catch (Exception exc)
\r
83 MessageBox.Show(exc.ToString());
\r
87 public void frmMain_cancelEncode()
\r
89 Process[] aProc = Process.GetProcessesByName("HandBrakeCLI");
\r
90 Process HandBrakeCLI;
\r
91 if (aProc.Length > 0)
\r
93 HandBrakeCLI = aProc[0];
\r
94 HandBrakeCLI.Kill();
\r
98 // Redraw's the queue with the latest data from the Queue class
\r
99 private void redrawQueue()
\r
101 list_queue.Items.Clear();
\r
102 List<Queue.QueueItem> theQueue = queue.getQueue();
\r
103 foreach (Queue.QueueItem queue_item in theQueue)
\r
105 string q_item = queue_item.Query;
\r
106 Functions.QueryParser parsed = Functions.QueryParser.Parse(q_item);
\r
108 // Get the DVD Title
\r
110 if (parsed.DVDTitle == 0)
\r
113 title = parsed.DVDTitle.ToString();
\r
115 // Get the DVD Chapters
\r
116 string chapters = "";
\r
117 if (parsed.DVDChapterStart == 0)
\r
121 chapters = parsed.DVDChapterStart.ToString();
\r
122 if (parsed.DVDChapterFinish != 0)
\r
123 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
126 ListViewItem item = new ListViewItem();
\r
127 item.Text = title; // Title
\r
128 item.SubItems.Add(chapters); // Chapters
\r
129 item.SubItems.Add(queue_item.Source); // Source
\r
130 item.SubItems.Add(queue_item.Destination); // Destination
\r
131 item.SubItems.Add(parsed.VideoEncoder); // Video
\r
132 item.SubItems.Add(parsed.AudioEncoder1); // Audio
\r
134 list_queue.Items.Add(item);
\r
138 // Initializes the encode process
\r
139 private void btn_encode_Click(object sender, EventArgs e)
\r
141 if (queue.count() != 0)
\r
143 btn_encode.Enabled = false;
\r
144 mainWindow.setLastAction("encode");
\r
145 mainWindow.setEncodeStatus(1);
\r
149 // Start the encode
\r
152 // Setup or reset some values
\r
153 btn_encode.Enabled = false;
\r
154 btn_stop.Visible = true;
\r
156 Thread theQ = new Thread(startProc);
\r
157 theQ.IsBackground = true;
\r
160 catch (Exception exc)
\r
162 MessageBox.Show(exc.ToString());
\r
167 // Starts the encoding process
\r
168 private void startProc(object state)
\r
172 // Run through each item on the queue
\r
173 while (queue.count() != 0)
\r
175 string query = queue.getNextItemForEncoding();
\r
176 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
180 updateUIElements();
\r
182 hbProc = cliObj.runCli(this, query);
\r
184 hbProc.WaitForExit();
\r
185 cliObj.addCLIQueryToLog(query);
\r
186 cliObj.copyLog(query, queue.getLastQuery().Destination);
\r
193 if (cancel == true)
\r
200 mainWindow.setEncodeStatus(0); // Tell the main window encodes have finished.
\r
202 // After the encode is done, we may want to shutdown, suspend etc.
\r
203 cliObj.afterEncodeAction();
\r
205 catch (Exception exc)
\r
207 MessageBox.Show(exc.ToString());
\r
211 // Reset's the window to the default state.
\r
212 private void resetQueue()
\r
216 if (this.InvokeRequired)
\r
218 this.BeginInvoke(new ProgressUpdateHandler(resetQueue));
\r
222 btn_stop.Visible = false;
\r
223 btn_encode.Enabled = true;
\r
225 lbl_source.Text = "-";
\r
226 lbl_dest.Text = "-";
\r
227 lbl_vEnc.Text = "-";
\r
228 lbl_aEnc.Text = "-";
\r
229 lbl_title.Text = "-";
\r
230 lbl_chapt.Text = "-";
\r
232 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
234 catch (Exception exc)
\r
236 MessageBox.Show(exc.ToString());
\r
240 // Stop's the queue from continuing.
\r
241 private void btn_stop_Click(object sender, EventArgs e)
\r
244 btn_stop.Visible = false;
\r
245 btn_encode.Enabled = true;
\r
246 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
249 // Updates the progress bar and progress label for a new status.
\r
250 private void updateUIElements()
\r
254 if (this.InvokeRequired)
\r
256 this.BeginInvoke(new ProgressUpdateHandler(updateUIElements));
\r
261 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
263 catch (Exception exc)
\r
265 MessageBox.Show(exc.ToString());
\r
269 // Set's the information lables about the current encode.
\r
270 private void setEncValue()
\r
274 if (this.InvokeRequired)
\r
276 this.BeginInvoke(new setEncoding(setEncValue));
\r
279 // found query is a global varible
\r
280 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue.getLastQuery().Query);
\r
281 lbl_source.Text = queue.getLastQuery().Source;
\r
282 lbl_dest.Text = queue.getLastQuery().Destination;
\r
285 if (parsed.DVDTitle == 0)
\r
286 lbl_title.Text = "Auto";
\r
288 lbl_title.Text = parsed.DVDTitle.ToString();
\r
290 string chapters = "";
\r
291 if (parsed.DVDChapterStart == 0)
\r
293 lbl_chapt.Text = "Auto";
\r
297 chapters = parsed.DVDChapterStart.ToString();
\r
298 if (parsed.DVDChapterFinish != 0)
\r
299 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
300 lbl_chapt.Text = chapters;
\r
303 lbl_vEnc.Text = parsed.VideoEncoder;
\r
304 lbl_aEnc.Text = parsed.AudioEncoder1;
\r
312 // Queue Management
\r
313 private void btn_up_Click(object sender, EventArgs e)
\r
315 if (list_queue.SelectedIndices.Count != 0)
\r
317 int selected = list_queue.SelectedIndices[0];
\r
319 queue.moveUp(selected);
\r
320 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
323 if (selected - 1 > 0)
\r
324 list_queue.Items[selected - 1].Selected = true;
\r
326 list_queue.Select();
\r
329 private void btn_down_Click(object sender, EventArgs e)
\r
331 if (list_queue.SelectedIndices.Count != 0)
\r
333 int selected = list_queue.SelectedIndices[0];
\r
335 queue.moveDown(list_queue.SelectedIndices[0]);
\r
336 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
339 if (selected + 1 < list_queue.Items.Count)
\r
340 list_queue.Items[selected + 1].Selected = true;
\r
342 list_queue.Select();
\r
345 private void btn_delete_Click(object sender, EventArgs e)
\r
347 if (list_queue.SelectedIndices.Count != 0)
\r
349 queue.remove(list_queue.SelectedIndices[0]);
\r
350 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
352 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
355 private void list_queue_deleteKey(object sender, KeyEventArgs e)
\r
357 if (e.KeyCode == Keys.Delete)
\r
359 if (list_queue.SelectedIndices.Count != 0)
\r
361 queue.remove(list_queue.SelectedIndices[0]);
\r
362 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
368 // Queue Import/Export Features
\r
369 private void mnu_batch_Click(object sender, EventArgs e)
\r
371 SaveFile.FileName = "";
\r
372 SaveFile.Filter = "Batch|.bat";
\r
373 SaveFile.ShowDialog();
\r
374 if (SaveFile.FileName != String.Empty)
\r
375 queue.writeBatchScript(SaveFile.FileName);
\r
377 private void mnu_export_Click(object sender, EventArgs e)
\r
379 SaveFile.FileName = "";
\r
380 SaveFile.Filter = "HandBrake Queue|*.queue";
\r
381 SaveFile.ShowDialog();
\r
382 if (SaveFile.FileName != String.Empty)
\r
383 queue.write2disk(SaveFile.FileName);
\r
385 private void mnu_import_Click(object sender, EventArgs e)
\r
387 OpenFile.FileName = "";
\r
388 OpenFile.ShowDialog();
\r
389 if (OpenFile.FileName != String.Empty)
\r
390 queue.recoverQueue(OpenFile.FileName);
\r
394 // Hide's the window when the user tries to "x" out of the window instead of closing it.
\r
395 protected override void OnClosing(CancelEventArgs e)
\r