2 This file is part of the HandBrake source code.
\r
3 Homepage: <http://handbrake.fr>.
\r
4 It may be used under the terms of the GNU General Public License. */
\r
9 using System.Collections.Generic;
\r
10 using System.Collections.ObjectModel;
\r
11 using System.ComponentModel;
\r
12 using System.Windows.Forms;
\r
18 /// The Queue Window
\r
20 public partial class frmQueue : Form
\r
23 /// Update Handler Delegate
\r
25 private delegate void UpdateHandler();
\r
28 /// An instance of the Queue service
\r
30 private readonly Queue queue;
\r
33 /// A reference to the main application window
\r
35 private readonly frmMain mainWindow;
\r
38 /// Initializes a new instance of the <see cref="frmQueue"/> class.
\r
40 /// <param name="q">
\r
41 /// An instance of the queue service.
\r
43 /// <param name="mw">
\r
44 /// The main window.
\r
46 public frmQueue(Queue q, frmMain mw)
\r
48 InitializeComponent();
\r
50 this.mainWindow = mw;
\r
53 queue.EncodeStarted += new EventHandler(QueueOnEncodeStart);
\r
54 queue.QueueCompleted += new EventHandler(QueueOnQueueFinished);
\r
55 queue.QueuePauseRequested += new EventHandler(QueueOnPaused);
\r
59 /// Handle the Queue Paused event
\r
61 /// <param name="sender">
\r
64 /// <param name="e">
\r
67 private void QueueOnPaused(object sender, EventArgs e)
\r
69 SetUiEncodeFinished();
\r
74 /// Handle the Queue Finished event.
\r
76 /// <param name="sender">
\r
79 /// <param name="e">
\r
82 private void QueueOnQueueFinished(object sender, EventArgs e)
\r
84 SetUiEncodeFinished();
\r
85 ResetQueue(); // Reset the Queue Window
\r
89 /// Handle the Encode Started event
\r
91 /// <param name="sender">
\r
94 /// <param name="e">
\r
97 private void QueueOnEncodeStart(object sender, EventArgs e)
\r
99 SetUiEncodeStarted(); // make sure the UI is set correctly
\r
100 SetCurrentEncodeInformation();
\r
101 UpdateUiElements(); // Redraw the Queue, a new encode has started.
\r
105 /// Initializes the Queue list with the Arraylist from the Queue class
\r
107 public void SetQueue()
\r
109 UpdateUiElements();
\r
113 /// Initializes the Queue list, then shows and activates the window
\r
115 public new void Show()
\r
121 /// Initializes the Queue list only if doSetQueue is true, then shows and activates the window
\r
123 /// <param name="doSetQueue">Indicates whether to call setQueue() before showing the window</param>
\r
124 public void Show(bool doSetQueue)
\r
126 if (doSetQueue) SetQueue();
\r
133 /// Handle the Encode button Click event
\r
135 /// <param name="sender">The sender</param>
\r
136 /// <param name="e">the EventArgs</param>
\r
137 private void BtnEncodeClick(object sender, EventArgs e)
\r
139 if (queue.PauseRequested)
\r
141 SetUiEncodeStarted();
\r
142 MessageBox.Show("Encoding restarted", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
\r
145 if (!queue.IsEncoding)
\r
150 /// Handle the Pause button click event.
\r
152 /// <param name="sender">
\r
155 /// <param name="e">
\r
158 private void BtnPauseClick(object sender, EventArgs e)
\r
161 SetUiEncodeFinished();
\r
164 "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.",
\r
165 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
171 /// Setup the UI to show that an encode has started
\r
173 private void SetUiEncodeStarted()
\r
175 if (InvokeRequired)
\r
177 BeginInvoke(new UpdateHandler(SetUiEncodeStarted));
\r
180 btn_encode.Enabled = false;
\r
181 btn_pause.Visible = true;
\r
185 /// Setup the UI to indicate that an encode has finished.
\r
187 private void SetUiEncodeFinished()
\r
189 if (InvokeRequired)
\r
191 BeginInvoke(new UpdateHandler(SetUiEncodeFinished));
\r
194 btn_pause.Visible = false;
\r
195 btn_encode.Enabled = true;
\r
199 /// Reset the Queue Window display
\r
201 private void ResetQueue()
\r
203 if (InvokeRequired)
\r
205 BeginInvoke(new UpdateHandler(ResetQueue));
\r
208 btn_pause.Visible = false;
\r
209 btn_encode.Enabled = true;
\r
211 lbl_source.Text = "-";
\r
212 lbl_dest.Text = "-";
\r
213 lbl_vEnc.Text = "-";
\r
214 lbl_aEnc.Text = "-";
\r
215 lbl_title.Text = "-";
\r
216 lbl_chapt.Text = "-";
\r
218 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
222 /// Redraw the Queue window with the latest information about HandBrakes status
\r
224 private void RedrawQueue()
\r
226 if (InvokeRequired)
\r
228 BeginInvoke(new UpdateHandler(RedrawQueue));
\r
232 list_queue.Items.Clear();
\r
233 ReadOnlyCollection<Job> theQueue = queue.CurrentQueue;
\r
234 foreach (Job queueItem in theQueue)
\r
236 string qItem = queueItem.Query;
\r
237 QueryParser parsed = Functions.QueryParser.Parse(qItem);
\r
239 // Get the DVD Title
\r
240 string title = parsed.DVDTitle == 0 ? "Auto" : parsed.DVDTitle.ToString();
\r
242 // Get the DVD Chapters
\r
244 if (parsed.DVDChapterStart == 0)
\r
248 chapters = parsed.DVDChapterStart.ToString();
\r
249 if (parsed.DVDChapterFinish != 0)
\r
250 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
253 ListViewItem item = new ListViewItem();
\r
254 item.Text = title; // Title
\r
255 item.SubItems.Add(chapters); // Chapters
\r
256 item.SubItems.Add(queueItem.Source); // Source
\r
257 item.SubItems.Add(queueItem.Destination); // Destination
\r
258 item.SubItems.Add(parsed.VideoEncoder); // Video
\r
260 // Display The Audio Track Information
\r
261 string audio = string.Empty;
\r
262 foreach (AudioTrack track in parsed.AudioInformation)
\r
264 if (audio != string.Empty)
\r
265 audio += ", " + track.Encoder;
\r
267 audio = track.Encoder;
\r
269 item.SubItems.Add(audio); // Audio
\r
271 list_queue.Items.Add(item);
\r
276 /// Update the UI elements
\r
278 private void UpdateUiElements()
\r
280 if (InvokeRequired)
\r
282 BeginInvoke(new UpdateHandler(UpdateUiElements));
\r
287 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
291 /// Set the window up with the current encode information
\r
293 private void SetCurrentEncodeInformation()
\r
297 if (InvokeRequired)
\r
299 BeginInvoke(new UpdateHandler(SetCurrentEncodeInformation));
\r
302 // found query is a global varible
\r
303 QueryParser parsed = Functions.QueryParser.Parse(queue.LastEncode.Query);
\r
304 lbl_source.Text = queue.LastEncode.Source;
\r
305 lbl_dest.Text = queue.LastEncode.Destination;
\r
307 lbl_title.Text = parsed.DVDTitle == 0 ? "Auto" : parsed.DVDTitle.ToString();
\r
309 if (Equals(parsed.DVDChapterStart, 0))
\r
310 lbl_chapt.Text = "Auto";
\r
313 string chapters = parsed.DVDChapterStart.ToString();
\r
314 if (parsed.DVDChapterFinish != 0)
\r
315 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
316 lbl_chapt.Text = chapters;
\r
319 lbl_vEnc.Text = parsed.VideoEncoder;
\r
321 // Display The Audio Track Information
\r
322 string audio = string.Empty;
\r
323 foreach (AudioTrack track in parsed.AudioInformation)
\r
325 if (audio != string.Empty)
\r
326 audio += ", " + track.Encoder;
\r
328 audio = track.Encoder;
\r
330 lbl_aEnc.Text = audio;
\r
339 /// Delete the currently selected items on the queue
\r
341 private void DeleteSelectedItems()
\r
343 // If there are selected items
\r
344 if (list_queue.SelectedIndices.Count > 0)
\r
346 // Save the selected indices to select them after the move
\r
347 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);
\r
348 foreach (int selectedIndex in list_queue.SelectedIndices)
\r
349 selectedIndices.Add(selectedIndex);
\r
351 int firstSelectedIndex = selectedIndices[0];
\r
353 // Reverse the list to delete the items from last to first (preserves indices)
\r
354 selectedIndices.Reverse();
\r
356 // Remove each selected item
\r
357 foreach (int selectedIndex in selectedIndices)
\r
358 queue.Remove(selectedIndex);
\r
360 UpdateUiElements();
\r
362 // Select the item where the first deleted item was previously
\r
363 if (firstSelectedIndex < list_queue.Items.Count)
\r
364 list_queue.Items[firstSelectedIndex].Selected = true;
\r
367 list_queue.Select(); // Activate the control to show the selected items
\r
370 // Queue Management
\r
372 /// Handle the Move Up Menu Item
\r
374 /// <param name="sender">
\r
377 /// <param name="e">
\r
380 private void MnuUpClick(object sender, EventArgs e)
\r
386 /// Handle the Move down Menu Item
\r
388 /// <param name="sender">
\r
391 /// <param name="e">
\r
394 private void MnuDownClick(object sender, EventArgs e)
\r
400 /// Handle the delete Menu Item
\r
402 /// <param name="sender">
\r
405 /// <param name="e">
\r
408 private void MnuDeleteClick(object sender, EventArgs e)
\r
410 DeleteSelectedItems();
\r
414 /// Handle the Button Up Click
\r
416 /// <param name="sender">
\r
419 /// <param name="e">
\r
422 private void BtnUpClick(object sender, EventArgs e)
\r
428 /// Handle the button down click
\r
430 /// <param name="sender">
\r
433 /// <param name="e">
\r
436 private void BtnDownClick(object sender, EventArgs e)
\r
442 /// Handle the delete button click
\r
444 /// <param name="sender">
\r
447 /// <param name="e">
\r
450 private void BtnDeleteClick(object sender, EventArgs e)
\r
452 DeleteSelectedItems();
\r
456 /// Handle the delete keyboard press
\r
458 /// <param name="sender">
\r
461 /// <param name="e">
\r
464 private void ListQueueDeleteKey(object sender, KeyEventArgs e)
\r
466 if (e.KeyCode == Keys.Delete)
\r
467 DeleteSelectedItems();
\r
471 /// Move items up in the queue
\r
473 private void MoveUp()
\r
475 // If there are selected items and the first item is not selected
\r
476 if (list_queue.SelectedIndices.Count > 0 && !list_queue.SelectedIndices.Contains(0))
\r
478 // Copy the selected indices to preserve them during the movement
\r
479 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);
\r
480 foreach (int selectedIndex in list_queue.SelectedIndices)
\r
481 selectedIndices.Add(selectedIndex);
\r
483 // Move up each selected item
\r
484 foreach (int selectedIndex in selectedIndices)
\r
485 queue.MoveUp(selectedIndex);
\r
487 UpdateUiElements();
\r
489 // Keep the selected item(s) selected, now moved up one index
\r
490 foreach (int selectedIndex in selectedIndices)
\r
491 if (selectedIndex - 1 > -1) // Defensive programming: ensure index is good
\r
492 list_queue.Items[selectedIndex - 1].Selected = true;
\r
495 list_queue.Select(); // Activate the control to show the selected items
\r
499 /// Move items down in the queue
\r
501 private void MoveDown()
\r
503 // If there are selected items and the last item is not selected
\r
504 if (list_queue.SelectedIndices.Count > 0 &&
\r
505 !list_queue.SelectedIndices.Contains(list_queue.Items[list_queue.Items.Count - 1].Index))
\r
507 // Copy the selected indices to preserve them during the movement
\r
508 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);
\r
509 foreach (int selectedIndex in list_queue.SelectedIndices)
\r
510 selectedIndices.Add(selectedIndex);
\r
512 // Reverse the indices to move the items down from last to first (preserves indices)
\r
513 selectedIndices.Reverse();
\r
515 // Move down each selected item
\r
516 foreach (int selectedIndex in selectedIndices)
\r
517 queue.MoveDown(selectedIndex);
\r
519 UpdateUiElements();
\r
521 // Keep the selected item(s) selected, now moved down one index
\r
522 foreach (int selectedIndex in selectedIndices)
\r
523 if (selectedIndex + 1 < list_queue.Items.Count) // Defensive programming: ensure index is good
\r
524 list_queue.Items[selectedIndex + 1].Selected = true;
\r
527 list_queue.Select(); // Activate the control to show the selected items
\r
530 // Queue Import/Export Features
\r
533 /// Create a batch script
\r
535 /// <param name="sender">
\r
538 /// <param name="e">
\r
541 private void MnuBatchClick(object sender, EventArgs e)
\r
543 SaveFile.FileName = string.Empty;
\r
544 SaveFile.Filter = "Batch|.bat";
\r
545 SaveFile.ShowDialog();
\r
546 if (SaveFile.FileName != String.Empty)
\r
547 queue.WriteBatchScriptToFile(SaveFile.FileName);
\r
553 /// <param name="sender">
\r
556 /// <param name="e">
\r
559 private void MnuExportClick(object sender, EventArgs e)
\r
561 SaveFile.FileName = string.Empty;
\r
562 SaveFile.Filter = "HandBrake Queue|*.queue";
\r
563 SaveFile.ShowDialog();
\r
564 if (SaveFile.FileName != String.Empty)
\r
565 queue.WriteQueueStateToFile(SaveFile.FileName);
\r
571 /// <param name="sender">
\r
574 /// <param name="e">
\r
577 private void MnuImportClick(object sender, EventArgs e)
\r
579 OpenFile.FileName = string.Empty;
\r
580 OpenFile.ShowDialog();
\r
581 if (OpenFile.FileName != String.Empty)
\r
582 queue.LoadQueueFromFile(OpenFile.FileName);
\r
583 UpdateUiElements();
\r
587 /// Readd current job to queue
\r
589 /// <param name="sender">
\r
592 /// <param name="e">
\r
595 private void MnuReaddClick(object sender, EventArgs e)
\r
597 if (!queue.LastEncode.IsEmpty)
\r
599 queue.Add(queue.LastEncode.Query, queue.LastEncode.Source, queue.LastEncode.Destination,
\r
600 queue.LastEncode.CustomQuery);
\r
601 UpdateUiElements();
\r
608 /// <param name="sender">
\r
611 /// <param name="e">
\r
614 private void MnuReconfigureJobClick(object sender, EventArgs e)
\r
616 if (list_queue.SelectedIndices != null)
\r
622 int index = list_queue.SelectedIndices[0];
\r
623 mainWindow.RecievingJob(queue.GetJob(index));
\r
624 queue.Remove(index);
\r
632 /// Hide's the window when the user tries to "x" out of the window instead of closing it.
\r
634 /// <param name="e">
\r
637 protected override void OnClosing(CancelEventArgs e)
\r