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
131 /// Handle the Encode button Click event
\r
133 /// <param name="sender">The sender</param>
\r
134 /// <param name="e">the EventArgs</param>
\r
135 private void BtnEncodeClick(object sender, EventArgs e)
\r
139 SetUiEncodeStarted();
\r
142 if (!queue.IsEncoding)
\r
147 /// Handle the Pause button click event.
\r
149 /// <param name="sender">
\r
152 /// <param name="e">
\r
155 private void BtnPauseClick(object sender, EventArgs e)
\r
158 SetUiEncodeFinished();
\r
161 "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
162 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
168 /// Setup the UI to show that an encode has started
\r
170 private void SetUiEncodeStarted()
\r
172 if (InvokeRequired)
\r
174 BeginInvoke(new UpdateHandler(SetUiEncodeStarted));
\r
177 btn_encode.Enabled = false;
\r
178 btn_pause.Visible = true;
\r
182 /// Setup the UI to indicate that an encode has finished.
\r
184 private void SetUiEncodeFinished()
\r
186 if (InvokeRequired)
\r
188 BeginInvoke(new UpdateHandler(SetUiEncodeFinished));
\r
191 btn_pause.Visible = false;
\r
192 btn_encode.Enabled = true;
\r
196 /// Reset the Queue Window display
\r
198 private void ResetQueue()
\r
200 if (InvokeRequired)
\r
202 BeginInvoke(new UpdateHandler(ResetQueue));
\r
205 btn_pause.Visible = false;
\r
206 btn_encode.Enabled = true;
\r
208 lbl_source.Text = "-";
\r
209 lbl_dest.Text = "-";
\r
210 lbl_vEnc.Text = "-";
\r
211 lbl_aEnc.Text = "-";
\r
212 lbl_title.Text = "-";
\r
213 lbl_chapt.Text = "-";
\r
215 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
219 /// Redraw the Queue window with the latest information about HandBrakes status
\r
221 private void RedrawQueue()
\r
223 if (InvokeRequired)
\r
225 BeginInvoke(new UpdateHandler(RedrawQueue));
\r
229 list_queue.Items.Clear();
\r
230 ReadOnlyCollection<Job> theQueue = queue.CurrentQueue;
\r
231 foreach (Job queueItem in theQueue)
\r
233 string qItem = queueItem.Query;
\r
234 QueryParser parsed = Functions.QueryParser.Parse(qItem);
\r
236 // Get the DVD Title
\r
237 string title = parsed.Title == 0 ? "Auto" : parsed.Title.ToString();
\r
239 // Get the DVD Chapters
\r
241 if (parsed.ChapterStart == 0)
\r
245 chapters = parsed.ChapterStart.ToString();
\r
246 if (parsed.ChapterFinish != 0)
\r
247 chapters = chapters + " - " + parsed.ChapterFinish;
\r
250 ListViewItem item = new ListViewItem();
\r
251 item.Text = title; // Title
\r
252 item.SubItems.Add(chapters); // Chapters
\r
253 item.SubItems.Add(queueItem.Source); // Source
\r
254 item.SubItems.Add(queueItem.Destination); // Destination
\r
255 item.SubItems.Add(parsed.VideoEncoder); // Video
\r
257 // Display The Audio Track Information
\r
258 string audio = string.Empty;
\r
259 foreach (AudioTrack track in parsed.AudioInformation)
\r
261 if (audio != string.Empty)
\r
262 audio += ", " + track.Encoder;
\r
264 audio = track.Encoder;
\r
266 item.SubItems.Add(audio); // Audio
\r
268 list_queue.Items.Add(item);
\r
273 /// Update the UI elements
\r
275 private void UpdateUiElements()
\r
277 if (InvokeRequired)
\r
279 BeginInvoke(new UpdateHandler(UpdateUiElements));
\r
284 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
288 /// Set the window up with the current encode information
\r
290 private void SetCurrentEncodeInformation()
\r
294 if (InvokeRequired)
\r
296 BeginInvoke(new UpdateHandler(SetCurrentEncodeInformation));
\r
299 // found query is a global varible
\r
300 QueryParser parsed = QueryParser.Parse(queue.LastEncode.Query);
\r
301 lbl_source.Text = queue.LastEncode.Source;
\r
302 lbl_dest.Text = queue.LastEncode.Destination;
\r
304 lbl_title.Text = parsed.Title == 0 ? "Auto" : parsed.Title.ToString();
\r
306 if (Equals(parsed.ChapterStart, 0))
\r
307 lbl_chapt.Text = "Auto";
\r
310 string chapters = parsed.ChapterStart.ToString();
\r
311 if (parsed.ChapterFinish != 0)
\r
312 chapters = chapters + " - " + parsed.ChapterFinish;
\r
313 lbl_chapt.Text = chapters;
\r
316 lbl_vEnc.Text = parsed.VideoEncoder;
\r
318 // Display The Audio Track Information
\r
319 string audio = string.Empty;
\r
320 foreach (AudioTrack track in parsed.AudioInformation)
\r
322 if (audio != string.Empty)
\r
323 audio += ", " + track.Encoder;
\r
325 audio = track.Encoder;
\r
327 lbl_aEnc.Text = audio;
\r
336 /// Delete the currently selected items on the queue
\r
338 private void DeleteSelectedItems()
\r
340 // If there are selected items
\r
341 if (list_queue.SelectedIndices.Count > 0)
\r
343 // Save the selected indices to select them after the move
\r
344 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);
\r
345 foreach (int selectedIndex in list_queue.SelectedIndices)
\r
346 selectedIndices.Add(selectedIndex);
\r
348 int firstSelectedIndex = selectedIndices[0];
\r
350 // Reverse the list to delete the items from last to first (preserves indices)
\r
351 selectedIndices.Reverse();
\r
353 // Remove each selected item
\r
354 foreach (int selectedIndex in selectedIndices)
\r
355 queue.Remove(selectedIndex);
\r
357 UpdateUiElements();
\r
359 // Select the item where the first deleted item was previously
\r
360 if (firstSelectedIndex < list_queue.Items.Count)
\r
361 list_queue.Items[firstSelectedIndex].Selected = true;
\r
364 list_queue.Select(); // Activate the control to show the selected items
\r
367 // Queue Management
\r
369 /// Handle the Move Up Menu Item
\r
371 /// <param name="sender">
\r
374 /// <param name="e">
\r
377 private void MnuUpClick(object sender, EventArgs e)
\r
383 /// Handle the Move down Menu Item
\r
385 /// <param name="sender">
\r
388 /// <param name="e">
\r
391 private void MnuDownClick(object sender, EventArgs e)
\r
399 /// <param name="sender">
\r
402 /// <param name="e">
\r
405 private void MnuEditClick(object sender, EventArgs e)
\r
407 if (list_queue.SelectedIndices != null && list_queue.SelectedIndices.Count != 0)
\r
413 int index = list_queue.SelectedIndices[0];
\r
414 mainWindow.RecievingJob(queue.GetJob(index));
\r
415 queue.Remove(index);
\r
423 /// Handle the delete Menu Item
\r
425 /// <param name="sender">
\r
428 /// <param name="e">
\r
431 private void MnuDeleteClick(object sender, EventArgs e)
\r
433 DeleteSelectedItems();
\r
437 /// Handle the Button Up Click
\r
439 /// <param name="sender">
\r
442 /// <param name="e">
\r
445 private void BtnUpClick(object sender, EventArgs e)
\r
451 /// Handle the button down click
\r
453 /// <param name="sender">
\r
456 /// <param name="e">
\r
459 private void BtnDownClick(object sender, EventArgs e)
\r
465 /// Handle the delete button click
\r
467 /// <param name="sender">
\r
470 /// <param name="e">
\r
473 private void BtnDeleteClick(object sender, EventArgs e)
\r
475 DeleteSelectedItems();
\r
479 /// Handle the delete keyboard press
\r
481 /// <param name="sender">
\r
484 /// <param name="e">
\r
487 private void ListQueueDeleteKey(object sender, KeyEventArgs e)
\r
489 if (e.KeyCode == Keys.Delete)
\r
490 DeleteSelectedItems();
\r
494 /// Move items up in the queue
\r
496 private void MoveUp()
\r
498 // If there are selected items and the first item is not selected
\r
499 if (list_queue.SelectedIndices.Count > 0 && !list_queue.SelectedIndices.Contains(0))
\r
501 // Copy the selected indices to preserve them during the movement
\r
502 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);
\r
503 foreach (int selectedIndex in list_queue.SelectedIndices)
\r
504 selectedIndices.Add(selectedIndex);
\r
506 // Move up each selected item
\r
507 foreach (int selectedIndex in selectedIndices)
\r
508 queue.MoveUp(selectedIndex);
\r
510 UpdateUiElements();
\r
512 // Keep the selected item(s) selected, now moved up one index
\r
513 foreach (int selectedIndex in selectedIndices)
\r
514 if (selectedIndex - 1 > -1) // Defensive programming: ensure index is good
\r
515 list_queue.Items[selectedIndex - 1].Selected = true;
\r
518 list_queue.Select(); // Activate the control to show the selected items
\r
522 /// Move items down in the queue
\r
524 private void MoveDown()
\r
526 // If there are selected items and the last item is not selected
\r
527 if (list_queue.SelectedIndices.Count > 0 &&
\r
528 !list_queue.SelectedIndices.Contains(list_queue.Items[list_queue.Items.Count - 1].Index))
\r
530 // Copy the selected indices to preserve them during the movement
\r
531 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);
\r
532 foreach (int selectedIndex in list_queue.SelectedIndices)
\r
533 selectedIndices.Add(selectedIndex);
\r
535 // Reverse the indices to move the items down from last to first (preserves indices)
\r
536 selectedIndices.Reverse();
\r
538 // Move down each selected item
\r
539 foreach (int selectedIndex in selectedIndices)
\r
540 queue.MoveDown(selectedIndex);
\r
542 UpdateUiElements();
\r
544 // Keep the selected item(s) selected, now moved down one index
\r
545 foreach (int selectedIndex in selectedIndices)
\r
546 if (selectedIndex + 1 < list_queue.Items.Count) // Defensive programming: ensure index is good
\r
547 list_queue.Items[selectedIndex + 1].Selected = true;
\r
550 list_queue.Select(); // Activate the control to show the selected items
\r
553 // Queue Import/Export Features
\r
556 /// Create a batch script
\r
558 /// <param name="sender">
\r
561 /// <param name="e">
\r
564 private void MnuBatchClick(object sender, EventArgs e)
\r
566 SaveFile.FileName = string.Empty;
\r
567 SaveFile.Filter = "Batch|.bat";
\r
568 SaveFile.ShowDialog();
\r
569 if (SaveFile.FileName != String.Empty)
\r
570 queue.WriteBatchScriptToFile(SaveFile.FileName);
\r
576 /// <param name="sender">
\r
579 /// <param name="e">
\r
582 private void MnuExportClick(object sender, EventArgs e)
\r
584 SaveFile.FileName = string.Empty;
\r
585 SaveFile.Filter = "HandBrake Queue|*.queue";
\r
586 SaveFile.ShowDialog();
\r
587 if (SaveFile.FileName != String.Empty)
\r
588 queue.WriteQueueStateToFile(SaveFile.FileName);
\r
594 /// <param name="sender">
\r
597 /// <param name="e">
\r
600 private void MnuImportClick(object sender, EventArgs e)
\r
602 OpenFile.FileName = string.Empty;
\r
603 OpenFile.ShowDialog();
\r
604 if (OpenFile.FileName != String.Empty)
\r
605 queue.LoadQueueFromFile(OpenFile.FileName);
\r
606 UpdateUiElements();
\r
610 /// Readd current job to queue
\r
612 /// <param name="sender">
\r
615 /// <param name="e">
\r
618 private void MnuReaddClick(object sender, EventArgs e)
\r
620 if (!queue.LastEncode.IsEmpty)
\r
623 queue.LastEncode.Query,
\r
624 queue.LastEncode.Title,
\r
625 queue.LastEncode.Source,
\r
626 queue.LastEncode.Destination,
\r
627 queue.LastEncode.CustomQuery);
\r
628 UpdateUiElements();
\r
633 /// Hide's the window when the user tries to "x" out of the window instead of closing it.
\r
635 /// <param name="e">
\r
638 protected override void OnClosing(CancelEventArgs e)
\r