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
11 using System.Drawing;
\r
13 using System.Windows.Forms;
\r
14 using System.Threading;
\r
15 using System.Diagnostics;
\r
16 using System.Runtime.InteropServices;
\r
21 public partial class frmQueue : Form
\r
23 private delegate void ProgressUpdateHandler();
\r
24 private delegate void setEncoding();
\r
25 Functions.Encode cliObj = new Functions.Encode();
\r
26 Boolean cancel = false;
\r
27 Process hbProc = null;
\r
28 Functions.Queue queue;
\r
32 InitializeComponent();
\r
36 /// Initializes the Queue list with the Arraylist from the Queue class
\r
38 /// <param name="qw"></param>
\r
39 public void setQueue(Functions.Queue qw)
\r
46 /// Returns if there is currently an item being encoded by the queue
\r
48 /// <returns>Boolean true if encoding</returns>
\r
49 public Boolean isEncoding()
\r
57 // Redraw's the queue with the latest data from the Queue class
\r
58 private void redrawQueue()
\r
60 list_queue.Items.Clear();
\r
61 foreach (string queue_item in queue.getQueue())
\r
63 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue_item);
\r
65 // Get the DVD Title
\r
67 if (parsed.DVDTitle == 0)
\r
70 title = parsed.DVDTitle.ToString();
\r
72 // Get the DVD Chapters
\r
73 string chapters = "";
\r
74 if (parsed.DVDChapterStart == 0)
\r
78 chapters = parsed.DVDChapterStart.ToString();
\r
79 if (parsed.DVDChapterFinish != 0)
\r
80 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
83 ListViewItem item = new ListViewItem();
\r
84 item.Text = title; // Title
\r
85 item.SubItems.Add(chapters); // Chapters
\r
86 item.SubItems.Add(parsed.Source); // Source
\r
87 item.SubItems.Add(parsed.Destination); // Destination
\r
88 item.SubItems.Add(parsed.VideoEncoder); // Video
\r
89 item.SubItems.Add(parsed.AudioEncoder1); // Audio
\r
91 list_queue.Items.Add(item);
\r
95 // Initializes the encode process
\r
96 private void btn_encode_Click(object sender, EventArgs e)
\r
98 if (queue.count() != 0)
\r
100 btn_encode.Enabled = false;
\r
104 // Start the encode
\r
107 if (queue.count() != 0)
\r
109 // Setup or reset some values
\r
110 btn_stop.Visible = true;
\r
111 progressBar.Value = 0;
\r
112 lbl_progressValue.Text = "0 %";
\r
113 progressBar.Step = 100 / queue.count();
\r
114 Thread theQ = new Thread(startProc);
\r
115 theQ.IsBackground = true;
\r
119 catch (Exception exc)
\r
121 MessageBox.Show(exc.ToString());
\r
125 // Starts the encoding process
\r
126 private void startProc(object state)
\r
130 // Run through each item on the queue
\r
131 while (queue.count() != 0)
\r
133 string query = queue.getNextItemForEncoding();
\r
136 updateUIElements();
\r
138 hbProc = cliObj.runCli(this, query);
\r
140 hbProc.WaitForExit();
\r
147 if (cancel == true)
\r
155 // After the encode is done, we may want to shutdown, suspend etc.
\r
156 cliObj.afterEncodeAction();
\r
158 catch (Exception exc)
\r
160 MessageBox.Show(exc.ToString());
\r
164 // Reset's the window to the default state.
\r
165 private void resetQueue()
\r
169 if (this.InvokeRequired)
\r
171 this.BeginInvoke(new ProgressUpdateHandler(resetQueue));
\r
175 btn_stop.Visible = false;
\r
176 btn_encode.Enabled = true;
\r
178 if (cancel == true)
\r
180 lbl_progressValue.Text = "Encode Queue Cancelled!";
\r
184 lbl_progressValue.Text = "Encode Queue Completed!";
\r
187 progressBar.Value = 0;
\r
189 lbl_source.Text = "-";
\r
190 lbl_dest.Text = "-";
\r
191 lbl_vEnc.Text = "-";
\r
192 lbl_aEnc.Text = "-";
\r
193 lbl_title.Text = "-";
\r
194 lbl_chapt.Text = "-";
\r
196 catch (Exception exc)
\r
198 MessageBox.Show(exc.ToString());
\r
202 // Stop's the queue from continuing.
\r
203 private void btn_stop_Click(object sender, EventArgs e)
\r
206 btn_stop.Visible = false;
\r
207 btn_encode.Enabled = true;
\r
208 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
211 // Updates the progress bar and progress label for a new status.
\r
212 private void updateUIElements()
\r
216 if (this.InvokeRequired)
\r
218 this.BeginInvoke(new ProgressUpdateHandler(updateUIElements));
\r
224 progressBar.PerformStep();
\r
225 lbl_progressValue.Text = string.Format("{0} %", progressBar.Value);
\r
227 catch (Exception exc)
\r
229 MessageBox.Show(exc.ToString());
\r
233 // Set's the information lables about the current encode.
\r
234 private void setEncValue()
\r
238 if (this.InvokeRequired)
\r
240 this.BeginInvoke(new setEncoding(setEncValue));
\r
243 // found query is a global varible
\r
244 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue.getLastQuery());
\r
245 lbl_source.Text = parsed.Source;
\r
246 lbl_dest.Text = parsed.Destination;
\r
249 if (parsed.DVDTitle == 0)
\r
250 lbl_title.Text = "Auto";
\r
252 lbl_title.Text = parsed.DVDTitle.ToString();
\r
254 string chapters = "";
\r
255 if (parsed.DVDChapterStart == 0)
\r
257 lbl_chapt.Text = "Auto";
\r
261 chapters = parsed.DVDChapterStart.ToString();
\r
262 if (parsed.DVDChapterFinish != 0)
\r
263 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
264 lbl_chapt.Text = chapters;
\r
267 lbl_vEnc.Text = parsed.VideoEncoder;
\r
268 lbl_aEnc.Text = parsed.AudioEncoder1;
\r
276 // Move an item up the Queue
\r
277 private void btn_up_Click(object sender, EventArgs e)
\r
279 if (list_queue.SelectedIndices.Count != 0)
\r
281 queue.moveUp(list_queue.SelectedIndices[0]);
\r
282 queue.write2disk(); // Update the queue recovery file
\r
287 // Move an item down the Queue
\r
288 private void btn_down_Click(object sender, EventArgs e)
\r
290 if (list_queue.SelectedIndices.Count != 0)
\r
292 queue.moveDown(list_queue.SelectedIndices[0]);
\r
293 queue.write2disk(); // Update the queue recovery file
\r
298 // Remove an item from the queue
\r
299 private void btn_delete_Click(object sender, EventArgs e)
\r
301 if (list_queue.SelectedIndices.Count != 0)
\r
303 queue.remove(list_queue.SelectedIndices[0]);
\r
304 queue.write2disk(); // Update the queue recovery file
\r
309 // Generate a batch file script to run the encode seperate of HandBrake
\r
310 private void btn_batch_Click(object sender, EventArgs e)
\r
312 string queries = "";
\r
313 foreach (string query_item in queue.getQueue())
\r
315 string fullQuery = '"' + Application.StartupPath.ToString() + "\\HandBrakeCLI.exe" + '"' + query_item;
\r
317 if (queries == string.Empty)
\r
318 queries = queries + fullQuery;
\r
320 queries = queries + " && " + fullQuery;
\r
322 string strCmdLine = queries;
\r
324 SaveFile.ShowDialog();
\r
325 string filename = SaveFile.FileName;
\r
327 if (filename != "")
\r
331 // Create a StreamWriter and open the file, Write the batch file query to the file and
\r
332 // Close the stream
\r
333 StreamWriter line = new StreamWriter(filename);
\r
334 line.WriteLine(strCmdLine);
\r
337 MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
\r
341 MessageBox.Show("Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
\r
347 // Hide's the window from the users view.
\r
348 private void btn_Close_Click(object sender, EventArgs e)
\r
353 // Hide's the window when the user tries to "x" out of the window instead of closing it.
\r
354 protected override void OnClosing(CancelEventArgs e)
\r