-using System;\r
-using System.Collections.Generic;\r
-using System.ComponentModel;\r
-using System.Data;\r
-using System.Drawing;\r
-using System.Text;\r
-using System.Windows.Forms;\r
+/* frmQueue.cs $\r
+ \r
+ This file is part of the HandBrake source code.\r
+ Homepage: <http://handbrake.fr>.\r
+ It may be used under the terms of the GNU General Public License. */\r
\r
namespace Handbrake\r
{\r
+ using System;\r
+ using System.Collections.Generic;\r
+ using System.Collections.ObjectModel;\r
+ using System.ComponentModel;\r
+ using System.Windows.Forms;\r
+ using EncodeQueue;\r
+ using Functions;\r
+ using Model;\r
+\r
public partial class frmQueue : Form\r
{\r
- public frmQueue()\r
+ private delegate void UpdateHandler();\r
+\r
+ private Queue queue;\r
+ private frmMain mainWindow;\r
+\r
+ public frmQueue(Queue q, frmMain mw)\r
{\r
InitializeComponent();\r
+\r
+ this.mainWindow = mw;\r
+\r
+ this.queue = q;\r
+ queue.EncodeStarted += new EventHandler(QueueOnEncodeStart);\r
+ queue.QueueCompleted += new EventHandler(QueueOnQueueFinished);\r
+ queue.QueuePauseRequested += new EventHandler(QueueOnPaused);\r
+ }\r
+\r
+ private void QueueOnPaused(object sender, EventArgs e)\r
+ {\r
+ SetUIEncodeFinished();\r
+ UpdateUIElements();\r
+ }\r
+\r
+ private void QueueOnQueueFinished(object sender, EventArgs e)\r
+ {\r
+ SetUIEncodeFinished();\r
+ ResetQueue(); // Reset the Queue Window\r
+ }\r
+\r
+ private void QueueOnEncodeStart(object sender, EventArgs e)\r
+ {\r
+ SetUIEncodeStarted(); // make sure the UI is set correctly\r
+ SetCurrentEncodeInformation();\r
+ UpdateUIElements(); // Redraw the Queue, a new encode has started.\r
+ }\r
+\r
+ /// <summary>\r
+ /// Initializes the Queue list with the Arraylist from the Queue class\r
+ /// </summary>\r
+ public void SetQueue()\r
+ {\r
+ UpdateUIElements();\r
+ }\r
+\r
+ /// <summary>\r
+ /// Initializes the Queue list, then shows and activates the window\r
+ /// </summary>\r
+ public new void Show()\r
+ {\r
+ Show(true);\r
+ }\r
+\r
+ /// <summary>\r
+ /// Initializes the Queue list only if doSetQueue is true, then shows and activates the window\r
+ /// </summary>\r
+ /// <param name="doSetQueue">Indicates whether to call setQueue() before showing the window</param>\r
+ public void Show(bool doSetQueue)\r
+ {\r
+ if (doSetQueue) SetQueue();\r
+ base.Show();\r
+\r
+ // Activate();\r
+ }\r
+\r
+ // Start and Stop Controls\r
+ private void btn_encode_Click(object sender, EventArgs e)\r
+ {\r
+ if (queue.PauseRequested)\r
+ {\r
+ SetUIEncodeStarted();\r
+ MessageBox.Show("Encoding restarted", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
+ }\r
+\r
+ if (!queue.IsEncoding)\r
+ queue.Start();\r
+ }\r
+\r
+ private void btn_pause_Click(object sender, EventArgs e)\r
+ {\r
+ queue.Pause();\r
+ SetUIEncodeFinished();\r
+ ResetQueue();\r
+ MessageBox.Show(\r
+ "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
+ "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+ }\r
+\r
+\r
+ // Window Display Management\r
+ private void SetUIEncodeStarted()\r
+ {\r
+ if (InvokeRequired)\r
+ {\r
+ BeginInvoke(new UpdateHandler(SetUIEncodeStarted));\r
+ return;\r
+ }\r
+ btn_encode.Enabled = false;\r
+ btn_pause.Visible = true;\r
+ }\r
+\r
+ private void SetUIEncodeFinished()\r
+ {\r
+ if (InvokeRequired)\r
+ {\r
+ BeginInvoke(new UpdateHandler(SetUIEncodeFinished));\r
+ return;\r
+ }\r
+ btn_pause.Visible = false;\r
+ btn_encode.Enabled = true;\r
+ }\r
+\r
+ private void ResetQueue()\r
+ {\r
+ if (InvokeRequired)\r
+ {\r
+ BeginInvoke(new UpdateHandler(ResetQueue));\r
+ return;\r
+ }\r
+ btn_pause.Visible = false;\r
+ btn_encode.Enabled = true;\r
+\r
+ lbl_source.Text = "-";\r
+ lbl_dest.Text = "-";\r
+ lbl_vEnc.Text = "-";\r
+ lbl_aEnc.Text = "-";\r
+ lbl_title.Text = "-";\r
+ lbl_chapt.Text = "-";\r
+\r
+ lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";\r
+ }\r
+\r
+ private void RedrawQueue()\r
+ {\r
+ if (InvokeRequired)\r
+ {\r
+ BeginInvoke(new UpdateHandler(RedrawQueue));\r
+ return;\r
+ }\r
+\r
+ list_queue.Items.Clear();\r
+ ReadOnlyCollection<Job> theQueue = queue.CurrentQueue;\r
+ foreach (Job queue_item in theQueue)\r
+ {\r
+ string q_item = queue_item.Query;\r
+ QueryParser parsed = Functions.QueryParser.Parse(q_item);\r
+\r
+ // Get the DVD Title\r
+ string title = parsed.DVDTitle == 0 ? "Auto" : parsed.DVDTitle.ToString();\r
+\r
+ // Get the DVD Chapters\r
+ string chapters;\r
+ if (parsed.DVDChapterStart == 0)\r
+ chapters = "Auto";\r
+ else\r
+ {\r
+ chapters = parsed.DVDChapterStart.ToString();\r
+ if (parsed.DVDChapterFinish != 0)\r
+ chapters = chapters + " - " + parsed.DVDChapterFinish;\r
+ }\r
+\r
+ ListViewItem item = new ListViewItem();\r
+ item.Text = title; // Title\r
+ item.SubItems.Add(chapters); // Chapters\r
+ item.SubItems.Add(queue_item.Source); // Source\r
+ item.SubItems.Add(queue_item.Destination); // Destination\r
+ item.SubItems.Add(parsed.VideoEncoder); // Video\r
+\r
+ // Display The Audio Track Information\r
+ string audio = string.Empty;\r
+ foreach (AudioTrack track in parsed.AudioInformation)\r
+ {\r
+ if (audio != string.Empty)\r
+ audio += ", " + track.Encoder;\r
+ else\r
+ audio = track.Encoder;\r
+ }\r
+ item.SubItems.Add(audio); // Audio\r
+\r
+ list_queue.Items.Add(item);\r
+ }\r
+ }\r
+\r
+ private void UpdateUIElements()\r
+ {\r
+ if (InvokeRequired)\r
+ {\r
+ BeginInvoke(new UpdateHandler(UpdateUIElements));\r
+ return;\r
+ }\r
+\r
+ RedrawQueue();\r
+ lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";\r
+ }\r
+\r
+ private void SetCurrentEncodeInformation()\r
+ {\r
+ try\r
+ {\r
+ if (InvokeRequired)\r
+ {\r
+ BeginInvoke(new UpdateHandler(SetCurrentEncodeInformation));\r
+ }\r
+\r
+ // found query is a global varible\r
+ QueryParser parsed = Functions.QueryParser.Parse(queue.LastEncode.Query);\r
+ lbl_source.Text = queue.LastEncode.Source;\r
+ lbl_dest.Text = queue.LastEncode.Destination;\r
+\r
+ lbl_title.Text = parsed.DVDTitle == 0 ? "Auto" : parsed.DVDTitle.ToString();\r
+\r
+ if (Equals(parsed.DVDChapterStart, 0))\r
+ lbl_chapt.Text = "Auto";\r
+ else\r
+ {\r
+ string chapters = parsed.DVDChapterStart.ToString();\r
+ if (parsed.DVDChapterFinish != 0)\r
+ chapters = chapters + " - " + parsed.DVDChapterFinish;\r
+ lbl_chapt.Text = chapters;\r
+ }\r
+\r
+ lbl_vEnc.Text = parsed.VideoEncoder;\r
+\r
+ // Display The Audio Track Information\r
+ string audio = string.Empty;\r
+ foreach (AudioTrack track in parsed.AudioInformation)\r
+ {\r
+ if (audio != string.Empty)\r
+ audio += ", " + track.Encoder;\r
+ else\r
+ audio = track.Encoder;\r
+ }\r
+ lbl_aEnc.Text = audio;\r
+ }\r
+ catch (Exception)\r
+ {\r
+ // Do Nothing\r
+ }\r
+ }\r
+\r
+ private void DeleteSelectedItems()\r
+ {\r
+ // If there are selected items\r
+ if (list_queue.SelectedIndices.Count > 0)\r
+ {\r
+ // Save the selected indices to select them after the move\r
+ List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);\r
+ foreach (int selectedIndex in list_queue.SelectedIndices)\r
+ selectedIndices.Add(selectedIndex);\r
+\r
+ int firstSelectedIndex = selectedIndices[0];\r
+\r
+ // Reverse the list to delete the items from last to first (preserves indices)\r
+ selectedIndices.Reverse();\r
+\r
+ // Remove each selected item\r
+ foreach (int selectedIndex in selectedIndices)\r
+ queue.Remove(selectedIndex);\r
+\r
+ UpdateUIElements();\r
+\r
+ // Select the item where the first deleted item was previously\r
+ if (firstSelectedIndex < list_queue.Items.Count)\r
+ list_queue.Items[firstSelectedIndex].Selected = true;\r
+ }\r
+\r
+ list_queue.Select(); // Activate the control to show the selected items\r
}\r
\r
- public void addItem(String item)\r
+ // Queue Management\r
+ private void mnu_up_Click(object sender, EventArgs e)\r
{\r
- list_queue.Items.Add(item);\r
+ MoveUp();\r
}\r
\r
- private void btn_Close_Click(object sender, EventArgs e)\r
+ private void mnu_Down_Click(object sender, EventArgs e)\r
+ {\r
+ MoveDown();\r
+ }\r
+\r
+ private void mnu_delete_Click(object sender, EventArgs e)\r
+ {\r
+ DeleteSelectedItems();\r
+ }\r
+\r
+ private void btn_up_Click(object sender, EventArgs e)\r
+ {\r
+ MoveUp();\r
+ }\r
+\r
+ private void btn_down_Click(object sender, EventArgs e)\r
+ {\r
+ MoveDown();\r
+ }\r
+\r
+ private void btn_delete_Click(object sender, EventArgs e)\r
+ {\r
+ DeleteSelectedItems();\r
+ }\r
+\r
+ private void list_queue_deleteKey(object sender, KeyEventArgs e)\r
+ {\r
+ if (e.KeyCode == Keys.Delete)\r
+ DeleteSelectedItems();\r
+ }\r
+\r
+ private void MoveUp()\r
+ {\r
+ // If there are selected items and the first item is not selected\r
+ if (list_queue.SelectedIndices.Count > 0 && !list_queue.SelectedIndices.Contains(0))\r
+ {\r
+ // Copy the selected indices to preserve them during the movement\r
+ List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);\r
+ foreach (int selectedIndex in list_queue.SelectedIndices)\r
+ selectedIndices.Add(selectedIndex);\r
+\r
+ // Move up each selected item\r
+ foreach (int selectedIndex in selectedIndices)\r
+ queue.MoveUp(selectedIndex);\r
+\r
+ UpdateUIElements();\r
+\r
+ // Keep the selected item(s) selected, now moved up one index\r
+ foreach (int selectedIndex in selectedIndices)\r
+ if (selectedIndex - 1 > -1) // Defensive programming: ensure index is good\r
+ list_queue.Items[selectedIndex - 1].Selected = true;\r
+ }\r
+\r
+ list_queue.Select(); // Activate the control to show the selected items\r
+ }\r
+\r
+ private void MoveDown()\r
+ {\r
+ // If there are selected items and the last item is not selected\r
+ if (list_queue.SelectedIndices.Count > 0 &&\r
+ !list_queue.SelectedIndices.Contains(list_queue.Items[list_queue.Items.Count - 1].Index))\r
+ {\r
+ // Copy the selected indices to preserve them during the movement\r
+ List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);\r
+ foreach (int selectedIndex in list_queue.SelectedIndices)\r
+ selectedIndices.Add(selectedIndex);\r
+\r
+ // Reverse the indices to move the items down from last to first (preserves indices)\r
+ selectedIndices.Reverse();\r
+\r
+ // Move down each selected item\r
+ foreach (int selectedIndex in selectedIndices)\r
+ queue.MoveDown(selectedIndex);\r
+\r
+ UpdateUIElements();\r
+\r
+ // Keep the selected item(s) selected, now moved down one index\r
+ foreach (int selectedIndex in selectedIndices)\r
+ if (selectedIndex + 1 < list_queue.Items.Count) // Defensive programming: ensure index is good\r
+ list_queue.Items[selectedIndex + 1].Selected = true;\r
+ }\r
+\r
+ list_queue.Select(); // Activate the control to show the selected items\r
+ }\r
+\r
+ // Queue Import/Export Features\r
+ private void mnu_batch_Click(object sender, EventArgs e)\r
+ {\r
+ SaveFile.FileName = string.Empty;\r
+ SaveFile.Filter = "Batch|.bat";\r
+ SaveFile.ShowDialog();\r
+ if (SaveFile.FileName != String.Empty)\r
+ queue.WriteBatchScriptToFile(SaveFile.FileName);\r
+ }\r
+\r
+ private void mnu_export_Click(object sender, EventArgs e)\r
+ {\r
+ SaveFile.FileName = string.Empty;\r
+ SaveFile.Filter = "HandBrake Queue|*.queue";\r
+ SaveFile.ShowDialog();\r
+ if (SaveFile.FileName != String.Empty)\r
+ queue.WriteQueueStateToFile(SaveFile.FileName);\r
+ }\r
+\r
+ private void mnu_import_Click(object sender, EventArgs e)\r
+ {\r
+ OpenFile.FileName = string.Empty;\r
+ OpenFile.ShowDialog();\r
+ if (OpenFile.FileName != String.Empty)\r
+ queue.LoadQueueFromFile(OpenFile.FileName);\r
+ UpdateUIElements();\r
+ }\r
+\r
+ private void mnu_readd_Click(object sender, EventArgs e)\r
+ {\r
+ if (!queue.LastEncode.IsEmpty)\r
+ {\r
+ queue.Add(queue.LastEncode.Query, queue.LastEncode.Source, queue.LastEncode.Destination, \r
+ queue.LastEncode.CustomQuery);\r
+ UpdateUIElements();\r
+ }\r
+ }\r
+\r
+ private void mnu_reconfigureJob_Click(object sender, EventArgs e)\r
+ {\r
+ if (list_queue.SelectedIndices != null)\r
+ {\r
+ lock (queue)\r
+ {\r
+ lock (list_queue)\r
+ {\r
+ int index = list_queue.SelectedIndices[0];\r
+ mainWindow.RecievingJob(queue.GetJob(index));\r
+ queue.Remove(index);\r
+ RedrawQueue();\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+\r
+ // Hide's the window when the user tries to "x" out of the window instead of closing it.\r
+ protected override void OnClosing(CancelEventArgs e)\r
{\r
- this.Close();\r
+ e.Cancel = true;\r
+ this.Hide();\r
+ base.OnClosing(e);\r
}\r
}\r
}
\ No newline at end of file