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 cancel = false;
\r
28 Process hbProc = null;
\r
29 Functions.Queue queue;
\r
33 InitializeComponent();
\r
37 /// Initializes the Queue list with the Arraylist from the Queue class
\r
39 /// <param name="qw"></param>
\r
40 public void setQueue(Functions.Queue qw)
\r
44 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
48 /// Returns if there is currently an item being encoded by the queue
\r
50 /// <returns>Boolean true if encoding</returns>
\r
51 public Boolean isEncoding()
\r
59 // Redraw's the queue with the latest data from the Queue class
\r
60 private void redrawQueue()
\r
62 list_queue.Items.Clear();
\r
63 ArrayList theQueue = queue.getQueue();
\r
64 foreach (ArrayList queue_item in theQueue)
\r
66 string q_item = queue_item[1].ToString();
\r
67 Functions.QueryParser parsed = Functions.QueryParser.Parse(q_item);
\r
69 // Get the DVD Title
\r
71 if (parsed.DVDTitle == 0)
\r
74 title = parsed.DVDTitle.ToString();
\r
76 // Get the DVD Chapters
\r
77 string chapters = "";
\r
78 if (parsed.DVDChapterStart == 0)
\r
82 chapters = parsed.DVDChapterStart.ToString();
\r
83 if (parsed.DVDChapterFinish != 0)
\r
84 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
87 ListViewItem item = new ListViewItem();
\r
88 item.Text = title; // Title
\r
89 item.SubItems.Add(chapters); // Chapters
\r
90 item.SubItems.Add(parsed.Source); // Source
\r
91 item.SubItems.Add(parsed.Destination); // Destination
\r
92 item.SubItems.Add(parsed.VideoEncoder); // Video
\r
93 item.SubItems.Add(parsed.AudioEncoder1); // Audio
\r
95 list_queue.Items.Add(item);
\r
99 // Initializes the encode process
\r
100 private void btn_encode_Click(object sender, EventArgs e)
\r
102 if (queue.count() != 0)
\r
104 btn_encode.Enabled = false;
\r
108 // Start the encode
\r
111 if (queue.count() != 0)
\r
113 // Setup or reset some values
\r
114 btn_stop.Visible = true;
\r
115 progressBar.Value = 0;
\r
116 lbl_progressValue.Text = "0 %";
\r
117 progressBar.Step = 100 / queue.count();
\r
118 Thread theQ = new Thread(startProc);
\r
119 theQ.IsBackground = true;
\r
123 catch (Exception exc)
\r
125 MessageBox.Show(exc.ToString());
\r
129 // Starts the encoding process
\r
130 private void startProc(object state)
\r
134 // Run through each item on the queue
\r
135 while (queue.count() != 0)
\r
137 string query = queue.getNextItemForEncoding();
\r
138 queue.write2disk("hb_queue_recovery.dat"); // Update the queue recovery file
\r
141 updateUIElements();
\r
143 hbProc = cliObj.runCli(this, query);
\r
145 hbProc.WaitForExit();
\r
152 if (cancel == true)
\r
160 // After the encode is done, we may want to shutdown, suspend etc.
\r
161 cliObj.afterEncodeAction();
\r
163 catch (Exception exc)
\r
165 MessageBox.Show(exc.ToString());
\r
169 // Reset's the window to the default state.
\r
170 private void resetQueue()
\r
174 if (this.InvokeRequired)
\r
176 this.BeginInvoke(new ProgressUpdateHandler(resetQueue));
\r
180 btn_stop.Visible = false;
\r
181 btn_encode.Enabled = true;
\r
183 if (cancel == true)
\r
185 lbl_progressValue.Text = "Encode Queue Cancelled!";
\r
189 lbl_progressValue.Text = "Encode Queue Completed!";
\r
192 progressBar.Value = 0;
\r
194 lbl_source.Text = "-";
\r
195 lbl_dest.Text = "-";
\r
196 lbl_vEnc.Text = "-";
\r
197 lbl_aEnc.Text = "-";
\r
198 lbl_title.Text = "-";
\r
199 lbl_chapt.Text = "-";
\r
201 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
203 catch (Exception exc)
\r
205 MessageBox.Show(exc.ToString());
\r
209 // Stop's the queue from continuing.
\r
210 private void btn_stop_Click(object sender, EventArgs e)
\r
213 btn_stop.Visible = false;
\r
214 btn_encode.Enabled = true;
\r
215 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
218 // Updates the progress bar and progress label for a new status.
\r
219 private void updateUIElements()
\r
223 if (this.InvokeRequired)
\r
225 this.BeginInvoke(new ProgressUpdateHandler(updateUIElements));
\r
231 progressBar.PerformStep();
\r
232 lbl_progressValue.Text = string.Format("{0} %", progressBar.Value);
\r
233 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
235 catch (Exception exc)
\r
237 MessageBox.Show(exc.ToString());
\r
241 // Set's the information lables about the current encode.
\r
242 private void setEncValue()
\r
246 if (this.InvokeRequired)
\r
248 this.BeginInvoke(new setEncoding(setEncValue));
\r
251 // found query is a global varible
\r
252 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue.getLastQuery());
\r
253 lbl_source.Text = parsed.Source;
\r
254 lbl_dest.Text = parsed.Destination;
\r
257 if (parsed.DVDTitle == 0)
\r
258 lbl_title.Text = "Auto";
\r
260 lbl_title.Text = parsed.DVDTitle.ToString();
\r
262 string chapters = "";
\r
263 if (parsed.DVDChapterStart == 0)
\r
265 lbl_chapt.Text = "Auto";
\r
269 chapters = parsed.DVDChapterStart.ToString();
\r
270 if (parsed.DVDChapterFinish != 0)
\r
271 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
272 lbl_chapt.Text = chapters;
\r
275 lbl_vEnc.Text = parsed.VideoEncoder;
\r
276 lbl_aEnc.Text = parsed.AudioEncoder1;
\r
284 // Move an item up the Queue
\r
285 private void btn_up_Click(object sender, EventArgs e)
\r
287 if (list_queue.SelectedIndices.Count != 0)
\r
289 int selected = list_queue.SelectedIndices[0];
\r
291 queue.moveUp(selected);
\r
292 queue.write2disk("hb_queue_recovery.dat"); // Update the queue recovery file
\r
295 if (selected - 1 > 0)
\r
296 list_queue.Items[selected -1].Selected = true;
\r
298 list_queue.Select();
\r
302 // Move an item down the Queue
\r
303 private void btn_down_Click(object sender, EventArgs e)
\r
305 if (list_queue.SelectedIndices.Count != 0)
\r
307 int selected = list_queue.SelectedIndices[0];
\r
309 queue.moveDown(list_queue.SelectedIndices[0]);
\r
310 queue.write2disk("hb_queue_recovery.dat"); // Update the queue recovery file
\r
313 if (selected +1 < list_queue.Items.Count)
\r
314 list_queue.Items[selected + 1].Selected = true;
\r
316 list_queue.Select();
\r
320 // Remove an item from the queue
\r
321 private void btn_delete_Click(object sender, EventArgs e)
\r
323 if (list_queue.SelectedIndices.Count != 0)
\r
325 queue.remove(list_queue.SelectedIndices[0]);
\r
326 queue.write2disk("hb_queue_recovery.dat"); // Update the queue recovery file
\r
328 lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";
\r
332 // Generate a Saveable batch script on the users request
\r
333 private void mnu_batch_Click(object sender, EventArgs e)
\r
335 SaveFile.FileName = "";
\r
336 SaveFile.Filter = "Batch|.bat";
\r
337 SaveFile.ShowDialog();
\r
338 if (SaveFile.FileName != String.Empty)
\r
339 queue.writeBatchScript(SaveFile.FileName);
\r
342 // Export the HandBrake Queue to a file.
\r
343 private void mnu_export_Click(object sender, EventArgs e)
\r
345 SaveFile.FileName = "";
\r
346 SaveFile.Filter = "HandBrake Queue|*.queue";
\r
347 SaveFile.ShowDialog();
\r
348 if (SaveFile.FileName != String.Empty)
\r
349 queue.write2disk(SaveFile.FileName);
\r
352 // Import an exported queue
\r
353 private void mnu_import_Click(object sender, EventArgs e)
\r
355 OpenFile.FileName = "";
\r
356 OpenFile.ShowDialog();
\r
357 if (OpenFile.FileName != String.Empty)
\r
358 queue.recoverQueue(OpenFile.FileName);
\r
362 // Delete a selected item on the queue, if the delete key is pressed.
\r
363 private void list_queue_deleteKey(object sender, KeyEventArgs e)
\r
365 if (e.KeyCode == Keys.Delete)
\r
367 if (list_queue.SelectedIndices.Count != 0)
\r
369 queue.remove(list_queue.SelectedIndices[0]);
\r
370 queue.write2disk("hb_queue_recovery.dat"); // Update the queue recovery file
\r
376 // Hide's the window from the users view.
\r
377 private void btn_Close_Click(object sender, EventArgs e)
\r
382 // Hide's the window when the user tries to "x" out of the window instead of closing it.
\r
383 protected override void OnClosing(CancelEventArgs e)
\r