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.ComponentModel;
\r
10 using System.Windows.Forms;
\r
11 using Handbrake.EncodeQueue;
\r
12 using System.Collections.ObjectModel;
\r
16 public partial class frmQueue : Form
\r
18 private delegate void UpdateHandler();
\r
19 private Queue queue;
\r
21 public frmQueue(Queue q)
\r
23 InitializeComponent();
\r
26 queue.NewJobStarted += new EventHandler(QueueOnEncodeStart);
\r
27 queue.QueueCompleted += new EventHandler(QueueOnQueueFinished);
\r
28 queue.QueuePauseRequested += new EventHandler(QueueOnPaused);
\r
31 private void QueueOnPaused(object sender, EventArgs e)
\r
33 SetUIEncodeFinished();
\r
36 private void QueueOnQueueFinished(object sender, EventArgs e)
\r
38 SetUIEncodeFinished();
\r
39 ResetQueue(); // Reset the Queue Window
\r
41 private void QueueOnEncodeStart(object sender, EventArgs e)
\r
43 SetUIEncodeStarted(); // make sure the UI is set correctly
\r
44 SetCurrentEncodeInformation();
\r
45 UpdateUIElements(); // Redraw the Queue, a new encode has started.
\r
49 /// Initializes the Queue list with the Arraylist from the Queue class
\r
51 public void SetQueue()
\r
57 /// Initializes the Queue list, then shows and activates the window
\r
59 public new void Show()
\r
65 /// Initializes the Queue list only if doSetQueue is true, then shows and activates the window
\r
67 /// <param name="doSetQueue">Indicates whether to call setQueue() before showing the window</param>
\r
68 public void Show(bool doSetQueue)
\r
70 if (doSetQueue) SetQueue();
\r
76 // Start and Stop Controls
\r
77 private void btn_encode_Click(object sender, EventArgs e)
\r
79 if (queue.PauseRequested)
\r
81 SetUIEncodeStarted();
\r
82 MessageBox.Show("Encoding restarted", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
\r
85 if (!queue.IsEncoding)
\r
89 private void btn_pause_Click(object sender, EventArgs e)
\r
92 SetUIEncodeFinished();
\r
94 MessageBox.Show("No further items on the queue will start. The current encode process will continue until it is finished. \nClick 'Encode' when you wish to continue encoding the queue.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
97 // Window Display Management
\r
98 private void SetUIEncodeStarted()
\r
100 if (InvokeRequired)
\r
102 BeginInvoke(new UpdateHandler(SetUIEncodeStarted));
\r
105 btn_encode.Enabled = false;
\r
106 btn_pause.Visible = true;
\r
108 private void SetUIEncodeFinished()
\r
110 if (InvokeRequired)
\r
112 BeginInvoke(new UpdateHandler(SetUIEncodeFinished));
\r
115 btn_pause.Visible = false;
\r
116 btn_encode.Enabled = true;
\r
118 private void ResetQueue()
\r
120 if (InvokeRequired)
\r
122 BeginInvoke(new UpdateHandler(ResetQueue));
\r
125 btn_pause.Visible = false;
\r
126 btn_encode.Enabled = true;
\r
128 lbl_source.Text = "-";
\r
129 lbl_dest.Text = "-";
\r
130 lbl_vEnc.Text = "-";
\r
131 lbl_aEnc.Text = "-";
\r
132 lbl_title.Text = "-";
\r
133 lbl_chapt.Text = "-";
\r
135 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
137 private void RedrawQueue()
\r
139 if (InvokeRequired)
\r
141 BeginInvoke(new UpdateHandler(RedrawQueue));
\r
145 list_queue.Items.Clear();
\r
146 ReadOnlyCollection<Job> theQueue = queue.CurrentQueue;
\r
147 foreach (Job queue_item in theQueue)
\r
149 string q_item = queue_item.Query;
\r
150 Functions.QueryParser parsed = Functions.QueryParser.Parse(q_item);
\r
152 // Get the DVD Title
\r
153 string title = parsed.DVDTitle == 0 ? "Auto" : parsed.DVDTitle.ToString();
\r
155 // Get the DVD Chapters
\r
157 if (parsed.DVDChapterStart == 0)
\r
161 chapters = parsed.DVDChapterStart.ToString();
\r
162 if (parsed.DVDChapterFinish != 0)
\r
163 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
166 ListViewItem item = new ListViewItem();
\r
167 item.Text = title; // Title
\r
168 item.SubItems.Add(chapters); // Chapters
\r
169 item.SubItems.Add(queue_item.Source); // Source
\r
170 item.SubItems.Add(queue_item.Destination); // Destination
\r
171 item.SubItems.Add(parsed.VideoEncoder); // Video
\r
173 // Display The Audio Track Information
\r
174 string audio = string.Empty;
\r
175 foreach (Functions.AudioTrack track in parsed.AudioInformation)
\r
178 audio += ", " + track.Encoder;
\r
180 audio = track.Encoder;
\r
182 item.SubItems.Add(audio); // Audio
\r
184 list_queue.Items.Add(item);
\r
187 private void UpdateUIElements()
\r
189 if (InvokeRequired)
\r
191 BeginInvoke(new UpdateHandler(UpdateUIElements));
\r
196 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
198 private void SetCurrentEncodeInformation()
\r
202 if (InvokeRequired)
\r
204 BeginInvoke(new UpdateHandler(SetCurrentEncodeInformation));
\r
207 // found query is a global varible
\r
208 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue.LastEncode.Query);
\r
209 lbl_source.Text = queue.LastEncode.Source;
\r
210 lbl_dest.Text = queue.LastEncode.Destination;
\r
212 lbl_title.Text = parsed.DVDTitle == 0 ? "Auto" : parsed.DVDTitle.ToString();
\r
214 if (Equals(parsed.DVDChapterStart, 0))
\r
215 lbl_chapt.Text = "Auto";
\r
218 string chapters = parsed.DVDChapterStart.ToString();
\r
219 if (parsed.DVDChapterFinish != 0)
\r
220 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
221 lbl_chapt.Text = chapters;
\r
224 lbl_vEnc.Text = parsed.VideoEncoder;
\r
226 // Display The Audio Track Information
\r
227 string audio = string.Empty;
\r
228 foreach (Functions.AudioTrack track in parsed.AudioInformation)
\r
231 audio += ", " + track.Encoder;
\r
233 audio = track.Encoder;
\r
235 lbl_aEnc.Text = audio;
\r
242 private void DeleteSelectedItems()
\r
244 // If there are selected items
\r
245 if (list_queue.SelectedIndices.Count > 0)
\r
247 // Save the selected indices to select them after the move
\r
248 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);
\r
249 foreach (int selectedIndex in list_queue.SelectedIndices)
\r
250 selectedIndices.Add(selectedIndex);
\r
252 int firstSelectedIndex = selectedIndices[0];
\r
254 // Reverse the list to delete the items from last to first (preserves indices)
\r
255 selectedIndices.Reverse();
\r
257 // Remove each selected item
\r
258 foreach (int selectedIndex in selectedIndices)
\r
259 queue.Remove(selectedIndex);
\r
261 UpdateUIElements();
\r
263 // Select the item where the first deleted item was previously
\r
264 if (firstSelectedIndex < list_queue.Items.Count)
\r
265 list_queue.Items[firstSelectedIndex].Selected = true;
\r
268 list_queue.Select(); // Activate the control to show the selected items
\r
271 // Queue Management
\r
272 private void mnu_up_Click(object sender, EventArgs e)
\r
276 private void mnu_Down_Click(object sender, EventArgs e)
\r
280 private void mnu_delete_Click(object sender, EventArgs e)
\r
282 DeleteSelectedItems();
\r
284 private void btn_up_Click(object sender, EventArgs e)
\r
288 private void btn_down_Click(object sender, EventArgs e)
\r
292 private void btn_delete_Click(object sender, EventArgs e)
\r
294 DeleteSelectedItems();
\r
296 private void list_queue_deleteKey(object sender, KeyEventArgs e)
\r
298 if (e.KeyCode == Keys.Delete)
\r
299 DeleteSelectedItems();
\r
301 private void MoveUp()
\r
303 // If there are selected items and the first item is not selected
\r
304 if (list_queue.SelectedIndices.Count > 0 && !list_queue.SelectedIndices.Contains(0))
\r
306 // Copy the selected indices to preserve them during the movement
\r
307 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);
\r
308 foreach (int selectedIndex in list_queue.SelectedIndices)
\r
309 selectedIndices.Add(selectedIndex);
\r
311 // Move up each selected item
\r
312 foreach (int selectedIndex in selectedIndices)
\r
313 queue.MoveUp(selectedIndex);
\r
315 UpdateUIElements();
\r
317 // Keep the selected item(s) selected, now moved up one index
\r
318 foreach (int selectedIndex in selectedIndices)
\r
319 if (selectedIndex - 1 > -1) // Defensive programming: ensure index is good
\r
320 list_queue.Items[selectedIndex - 1].Selected = true;
\r
323 list_queue.Select(); // Activate the control to show the selected items
\r
325 private void MoveDown()
\r
327 // If there are selected items and the last item is not selected
\r
328 if (list_queue.SelectedIndices.Count > 0 &&
\r
329 !list_queue.SelectedIndices.Contains(list_queue.Items[list_queue.Items.Count - 1].Index))
\r
331 // Copy the selected indices to preserve them during the movement
\r
332 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);
\r
333 foreach (int selectedIndex in list_queue.SelectedIndices)
\r
334 selectedIndices.Add(selectedIndex);
\r
336 // Reverse the indices to move the items down from last to first (preserves indices)
\r
337 selectedIndices.Reverse();
\r
339 // Move down each selected item
\r
340 foreach (int selectedIndex in selectedIndices)
\r
341 queue.MoveDown(selectedIndex);
\r
343 UpdateUIElements();
\r
345 // Keep the selected item(s) selected, now moved down one index
\r
346 foreach (int selectedIndex in selectedIndices)
\r
347 if (selectedIndex + 1 < list_queue.Items.Count) // Defensive programming: ensure index is good
\r
348 list_queue.Items[selectedIndex + 1].Selected = true;
\r
351 list_queue.Select(); // Activate the control to show the selected items
\r
354 // Queue Import/Export Features
\r
355 private void mnu_batch_Click(object sender, EventArgs e)
\r
357 SaveFile.FileName = "";
\r
358 SaveFile.Filter = "Batch|.bat";
\r
359 SaveFile.ShowDialog();
\r
360 if (SaveFile.FileName != String.Empty)
\r
361 queue.WriteBatchScriptToFile(SaveFile.FileName);
\r
363 private void mnu_export_Click(object sender, EventArgs e)
\r
365 SaveFile.FileName = "";
\r
366 SaveFile.Filter = "HandBrake Queue|*.queue";
\r
367 SaveFile.ShowDialog();
\r
368 if (SaveFile.FileName != String.Empty)
\r
369 queue.WriteQueueStateToFile(SaveFile.FileName);
\r
371 private void mnu_import_Click(object sender, EventArgs e)
\r
373 OpenFile.FileName = "";
\r
374 OpenFile.ShowDialog();
\r
375 if (OpenFile.FileName != String.Empty)
\r
376 queue.LoadQueueFromFile(OpenFile.FileName);
\r
377 UpdateUIElements();
\r
379 private void mnu_readd_Click(object sender, EventArgs e)
\r
381 if (!queue.LastEncode.IsEmpty)
\r
383 queue.Add(queue.LastEncode.Query, queue.LastEncode.Source, queue.LastEncode.Destination, queue.LastEncode.CustomQuery);
\r
384 UpdateUIElements();
\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