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 paused = false;
\r
28 Process hbProc = null;
\r
30 frmMain mainWindow = null;
\r
33 public frmQueue(frmMain main)
\r
35 InitializeComponent();
\r
40 /// Initializes the Queue list with the Arraylist from the Queue class
\r
42 /// <param name="qw"></param>
\r
43 public void setQueue(Queue.Queue qw)
\r
47 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
51 /// Returns if there is currently an item being encoded by the queue
\r
53 /// <returns>Boolean true if encoding</returns>
\r
54 public Boolean isEncoding()
\r
63 /// This disables encoding from the queue when a single encode from the main window is running.
\r
65 public void frmMain_encode()
\r
71 if (queue.count() != 0)
\r
73 // Setup or reset some values
\r
74 btn_encode.Enabled = false;
\r
75 btn_stop.Visible = true;
\r
77 Thread theQ = new Thread(startProc);
\r
78 theQ.IsBackground = true;
\r
82 catch (Exception exc)
\r
84 MessageBox.Show(exc.ToString());
\r
88 public void frmMain_cancelEncode()
\r
90 Process[] aProc = Process.GetProcessesByName("HandBrakeCLI");
\r
91 Process HandBrakeCLI;
\r
92 if (aProc.Length > 0)
\r
94 HandBrakeCLI = aProc[0];
\r
95 HandBrakeCLI.Kill();
\r
99 // Redraw's the queue with the latest data from the Queue class
\r
100 private void redrawQueue()
\r
102 list_queue.Items.Clear();
\r
103 List<Queue.QueueItem> theQueue = queue.getQueue();
\r
104 foreach (Queue.QueueItem queue_item in theQueue)
\r
106 string q_item = queue_item.Query;
\r
107 Functions.QueryParser parsed = Functions.QueryParser.Parse(q_item);
\r
109 // Get the DVD Title
\r
111 if (parsed.DVDTitle == 0)
\r
114 title = parsed.DVDTitle.ToString();
\r
116 // Get the DVD Chapters
\r
117 string chapters = "";
\r
118 if (parsed.DVDChapterStart == 0)
\r
122 chapters = parsed.DVDChapterStart.ToString();
\r
123 if (parsed.DVDChapterFinish != 0)
\r
124 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
127 ListViewItem item = new ListViewItem();
\r
128 item.Text = title; // Title
\r
129 item.SubItems.Add(chapters); // Chapters
\r
130 item.SubItems.Add(queue_item.Source); // Source
\r
131 item.SubItems.Add(queue_item.Destination); // Destination
\r
132 item.SubItems.Add(parsed.VideoEncoder); // Video
\r
133 item.SubItems.Add(parsed.AudioEncoder1); // Audio
\r
135 list_queue.Items.Add(item);
\r
139 // Initializes the encode process
\r
140 private void btn_encode_Click(object sender, EventArgs e)
\r
142 if (queue.count() != 0)
\r
144 if (paused == true)
\r
147 btn_encode.Enabled = false;
\r
148 btn_stop.Visible = true;
\r
149 MessageBox.Show("Encoding will now continue!","Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
\r
154 btn_encode.Enabled = false;
\r
155 mainWindow.setLastAction("encode");
\r
156 mainWindow.setEncodeStatus(1);
\r
158 // Start the encode
\r
161 // Setup or reset some values
\r
162 btn_encode.Enabled = false;
\r
163 btn_stop.Visible = true;
\r
165 theQ = new Thread(startProc);
\r
166 theQ.IsBackground = true;
\r
169 catch (Exception exc)
\r
171 MessageBox.Show(exc.ToString());
\r
177 // Starts the encoding process
\r
178 private void startProc(object state)
\r
182 // Run through each item on the queue
\r
183 while (queue.count() != 0)
\r
185 string query = queue.getNextItemForEncoding();
\r
186 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
190 updateUIElements();
\r
192 hbProc = cliObj.runCli(this, query);
\r
194 hbProc.WaitForExit();
\r
195 cliObj.addCLIQueryToLog(query);
\r
196 cliObj.copyLog(query, queue.getLastQuery().Destination);
\r
203 while (paused == true) // Need to find a better way of doing this.
\r
205 Thread.Sleep(10000);
\r
210 mainWindow.setEncodeStatus(0); // Tell the main window encodes have finished.
\r
212 // After the encode is done, we may want to shutdown, suspend etc.
\r
213 cliObj.afterEncodeAction();
\r
215 catch (Exception exc)
\r
217 MessageBox.Show(exc.ToString());
\r
221 // Reset's the window to the default state.
\r
222 private void resetQueue()
\r
226 if (this.InvokeRequired)
\r
228 this.BeginInvoke(new ProgressUpdateHandler(resetQueue));
\r
232 btn_stop.Visible = false;
\r
233 btn_encode.Enabled = true;
\r
235 lbl_source.Text = "-";
\r
236 lbl_dest.Text = "-";
\r
237 lbl_vEnc.Text = "-";
\r
238 lbl_aEnc.Text = "-";
\r
239 lbl_title.Text = "-";
\r
240 lbl_chapt.Text = "-";
\r
242 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
244 catch (Exception exc)
\r
246 MessageBox.Show(exc.ToString());
\r
250 // Stop's the queue from continuing.
\r
251 private void btn_stop_Click(object sender, EventArgs e)
\r
254 btn_stop.Visible = false;
\r
255 btn_encode.Enabled = true;
\r
256 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
259 // Updates the progress bar and progress label for a new status.
\r
260 private void updateUIElements()
\r
264 if (this.InvokeRequired)
\r
266 this.BeginInvoke(new ProgressUpdateHandler(updateUIElements));
\r
271 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
273 catch (Exception exc)
\r
275 MessageBox.Show(exc.ToString());
\r
279 // Set's the information lables about the current encode.
\r
280 private void setEncValue()
\r
284 if (this.InvokeRequired)
\r
286 this.BeginInvoke(new setEncoding(setEncValue));
\r
289 // found query is a global varible
\r
290 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue.getLastQuery().Query);
\r
291 lbl_source.Text = queue.getLastQuery().Source;
\r
292 lbl_dest.Text = queue.getLastQuery().Destination;
\r
295 if (parsed.DVDTitle == 0)
\r
296 lbl_title.Text = "Auto";
\r
298 lbl_title.Text = parsed.DVDTitle.ToString();
\r
300 string chapters = "";
\r
301 if (parsed.DVDChapterStart == 0)
\r
303 lbl_chapt.Text = "Auto";
\r
307 chapters = parsed.DVDChapterStart.ToString();
\r
308 if (parsed.DVDChapterFinish != 0)
\r
309 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
310 lbl_chapt.Text = chapters;
\r
313 lbl_vEnc.Text = parsed.VideoEncoder;
\r
314 lbl_aEnc.Text = parsed.AudioEncoder1;
\r
322 // Queue Management
\r
323 private void btn_up_Click(object sender, EventArgs e)
\r
325 if (list_queue.SelectedIndices.Count != 0)
\r
327 int selected = list_queue.SelectedIndices[0];
\r
329 queue.moveUp(selected);
\r
330 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
333 if (selected - 1 > 0)
\r
334 list_queue.Items[selected - 1].Selected = true;
\r
336 list_queue.Select();
\r
339 private void btn_down_Click(object sender, EventArgs e)
\r
341 if (list_queue.SelectedIndices.Count != 0)
\r
343 int selected = list_queue.SelectedIndices[0];
\r
345 queue.moveDown(list_queue.SelectedIndices[0]);
\r
346 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
349 if (selected + 1 < list_queue.Items.Count)
\r
350 list_queue.Items[selected + 1].Selected = true;
\r
352 list_queue.Select();
\r
355 private void btn_delete_Click(object sender, EventArgs e)
\r
357 if (list_queue.SelectedIndices.Count != 0)
\r
359 queue.remove(list_queue.SelectedIndices[0]);
\r
360 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
362 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
365 private void list_queue_deleteKey(object sender, KeyEventArgs e)
\r
367 if (e.KeyCode == Keys.Delete)
\r
369 if (list_queue.SelectedIndices.Count != 0)
\r
371 queue.remove(list_queue.SelectedIndices[0]);
\r
372 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file
\r
378 // Queue Import/Export Features
\r
379 private void mnu_batch_Click(object sender, EventArgs e)
\r
381 SaveFile.FileName = "";
\r
382 SaveFile.Filter = "Batch|.bat";
\r
383 SaveFile.ShowDialog();
\r
384 if (SaveFile.FileName != String.Empty)
\r
385 queue.writeBatchScript(SaveFile.FileName);
\r
387 private void mnu_export_Click(object sender, EventArgs e)
\r
389 SaveFile.FileName = "";
\r
390 SaveFile.Filter = "HandBrake Queue|*.queue";
\r
391 SaveFile.ShowDialog();
\r
392 if (SaveFile.FileName != String.Empty)
\r
393 queue.write2disk(SaveFile.FileName);
\r
395 private void mnu_import_Click(object sender, EventArgs e)
\r
397 OpenFile.FileName = "";
\r
398 OpenFile.ShowDialog();
\r
399 if (OpenFile.FileName != String.Empty)
\r
400 queue.recoverQueue(OpenFile.FileName);
\r
404 // Hide's the window when the user tries to "x" out of the window instead of closing it.
\r
405 protected override void OnClosing(CancelEventArgs e)
\r