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.CLI cliObj = new Functions.CLI();
\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 lbl_status.Visible = false;
\r
101 btn_encode.Enabled = false;
\r
105 // Start the encode
\r
108 if (queue.count() != 0)
\r
110 // Setup or reset some values
\r
111 btn_stop.Visible = true;
\r
112 progressBar.Value = 0;
\r
113 lbl_progressValue.Text = "0 %";
\r
114 progressBar.Step = 100 / queue.count();
\r
115 progressBar.Update();
\r
116 Thread theQ = new Thread(startProc);
\r
120 catch (Exception exc)
\r
122 MessageBox.Show(exc.ToString());
\r
126 // Starts the encoding process
\r
127 private void startProc(object state)
\r
131 // Run through each item on the queue
\r
132 while (queue.count() != 0)
\r
134 string query = queue.getNextItemForEncoding();
\r
137 updateUIElements();
\r
139 hbProc = cliObj.runCli(this, query);
\r
141 hbProc.WaitForExit();
\r
148 if (cancel == true)
\r
156 // After the encode is done, we may want to shutdown, suspend etc.
\r
157 cliObj.afterEncodeAction();
\r
159 catch (Exception exc)
\r
161 MessageBox.Show(exc.ToString());
\r
165 // Reset's the window to the default state.
\r
166 private void resetQueue()
\r
170 if (this.InvokeRequired)
\r
172 this.BeginInvoke(new ProgressUpdateHandler(resetQueue));
\r
176 btn_stop.Visible = false;
\r
177 btn_encode.Enabled = true;
\r
179 if (cancel == true)
\r
181 lbl_status.Visible = true;
\r
182 lbl_status.Text = "Encode Queue Cancelled!";
\r
186 lbl_status.Visible = true;
\r
187 lbl_status.Text = "Encode Queue Completed!";
\r
190 lbl_progressValue.Text = "0 %";
\r
191 progressBar.Value = 0;
\r
192 progressBar.Update();
\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 catch (Exception exc)
\r
203 MessageBox.Show(exc.ToString());
\r
207 // Stop's the queue from continuing.
\r
208 private void btn_stop_Click(object sender, EventArgs e)
\r
211 btn_stop.Visible = false;
\r
212 btn_encode.Enabled = true;
\r
213 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
216 // Updates the progress bar and progress label for a new status.
\r
217 private void updateUIElements()
\r
221 if (this.InvokeRequired)
\r
223 this.BeginInvoke(new ProgressUpdateHandler(updateUIElements));
\r
229 progressBar.PerformStep();
\r
230 lbl_progressValue.Text = string.Format("{0} %", progressBar.Value);
\r
231 progressBar.Update();
\r
233 catch (Exception exc)
\r
235 MessageBox.Show(exc.ToString());
\r
239 // Set's the information lables about the current encode.
\r
240 private void setEncValue()
\r
244 if (this.InvokeRequired)
\r
246 this.BeginInvoke(new setEncoding(setEncValue));
\r
249 // found query is a global varible
\r
250 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue.getLastQuery());
\r
251 lbl_source.Text = parsed.Source;
\r
252 lbl_dest.Text = parsed.Destination;
\r
255 if (parsed.DVDTitle == 0)
\r
256 lbl_title.Text = "Auto";
\r
258 lbl_title.Text = parsed.DVDTitle.ToString();
\r
260 string chapters = "";
\r
261 if (parsed.DVDChapterStart == 0)
\r
263 lbl_chapt.Text = "Auto";
\r
267 chapters = parsed.DVDChapterStart.ToString();
\r
268 if (parsed.DVDChapterFinish != 0)
\r
269 chapters = chapters + " - " + parsed.DVDChapterFinish;
\r
270 lbl_chapt.Text = chapters;
\r
273 lbl_vEnc.Text = parsed.VideoEncoder;
\r
274 lbl_aEnc.Text = parsed.AudioEncoder1;
\r
282 // Move an item up the Queue
\r
283 private void btn_up_Click(object sender, EventArgs e)
\r
285 if (list_queue.SelectedIndices.Count != 0)
\r
287 queue.moveUp(list_queue.SelectedIndices[0]);
\r
292 // Move an item down the Queue
\r
293 private void btn_down_Click(object sender, EventArgs e)
\r
295 if (list_queue.SelectedIndices.Count != 0)
\r
297 queue.moveDown(list_queue.SelectedIndices[0]);
\r
302 // Remove an item from the queue
\r
303 private void btn_delete_Click(object sender, EventArgs e)
\r
305 if (list_queue.SelectedIndices.Count != 0)
\r
307 queue.remove(list_queue.SelectedIndices[0]);
\r
312 // Generate a batch file script to run the encode seperate of HandBrake
\r
313 private void btn_batch_Click(object sender, EventArgs e)
\r
315 string queries = "";
\r
316 foreach (string query_item in queue.getQueue())
\r
318 string fullQuery = '"' + Application.StartupPath.ToString() + "\\HandBrakeCLI.exe" + '"' + query_item;
\r
321 queries = queries + fullQuery;
\r
323 queries = queries + " && " + fullQuery;
\r
325 string strCmdLine = queries;
\r
327 SaveFile.ShowDialog();
\r
328 string filename = SaveFile.FileName;
\r
330 if (filename != "")
\r
334 // Create a StreamWriter and open the file, Write the batch file query to the file and
\r
335 // Close the stream
\r
336 StreamWriter line = new StreamWriter(filename);
\r
337 line.WriteLine(strCmdLine);
\r
340 MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
\r
344 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
350 // Hide's the window from the users view.
\r
351 private void btn_Close_Click(object sender, EventArgs e)
\r
356 // Hide's the window when the user tries to "x" out of the window instead of closing it.
\r
357 protected override void OnClosing(CancelEventArgs e)
\r