OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmQueue.cs
1 /*  frmQueue.cs $\r
2         \r
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
6 \r
7 using System;\r
8 using System.Collections.Generic;\r
9 using System.ComponentModel;\r
10 using System.Windows.Forms;\r
11 using Handbrake.EncodeQueue;\r
12 using System.Collections.ObjectModel;\r
13 \r
14 namespace Handbrake\r
15 {\r
16     public partial class frmQueue : Form\r
17     {\r
18         private delegate void UpdateHandler();\r
19         private Queue queue;\r
20 \r
21         public frmQueue(Queue q)\r
22         {\r
23             InitializeComponent();\r
24 \r
25             this.queue = q;\r
26             queue.NewJobStarted += new EventHandler(QueueOnEncodeStart);\r
27             queue.QueueCompleted += new EventHandler(QueueOnQueueFinished);\r
28             queue.QueuePauseRequested += new EventHandler(QueueOnPaused);\r
29         }\r
30 \r
31         private void QueueOnPaused(object sender, EventArgs e)\r
32         {\r
33             SetUIEncodeFinished();\r
34             UpdateUIElements();\r
35         }\r
36         private void QueueOnQueueFinished(object sender, EventArgs e)\r
37         {\r
38             SetUIEncodeFinished();\r
39             ResetQueue(); // Reset the Queue Window\r
40         }\r
41         private void QueueOnEncodeStart(object sender, EventArgs e)\r
42         {\r
43             SetUIEncodeStarted(); // make sure the UI is set correctly\r
44             SetCurrentEncodeInformation();\r
45             UpdateUIElements(); // Redraw the Queue, a new encode has started.\r
46         }\r
47 \r
48         /// <summary>\r
49         /// Initializes the Queue list with the Arraylist from the Queue class\r
50         /// </summary>\r
51         public void SetQueue()\r
52         {\r
53             UpdateUIElements();\r
54         }\r
55 \r
56         /// <summary>\r
57         /// Initializes the Queue list, then shows and activates the window\r
58         /// </summary>\r
59         public new void Show()\r
60         {\r
61            Show(true);\r
62         }\r
63 \r
64         /// <summary>\r
65         /// Initializes the Queue list only if doSetQueue is true, then shows and activates the window\r
66         /// </summary>\r
67         /// <param name="doSetQueue">Indicates whether to call setQueue() before showing the window</param>\r
68         public void Show(bool doSetQueue)\r
69         {\r
70             if (doSetQueue) SetQueue();\r
71             base.Show();\r
72 \r
73             //Activate();\r
74         }\r
75 \r
76         // Start and Stop Controls\r
77         private void btn_encode_Click(object sender, EventArgs e)\r
78         {\r
79             if (queue.PauseRequested)\r
80             {\r
81                 SetUIEncodeStarted();\r
82                 MessageBox.Show("Encoding restarted", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
83             }\r
84 \r
85             if (!queue.IsEncoding)\r
86                 queue.Start();\r
87 \r
88         }\r
89         private void btn_pause_Click(object sender, EventArgs e)\r
90         {\r
91             queue.Pause();\r
92             SetUIEncodeFinished();\r
93             ResetQueue();\r
94             MessageBox.Show("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.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
95         }\r
96 \r
97         // Window Display Management\r
98         private void SetUIEncodeStarted()\r
99         {\r
100             if (InvokeRequired)\r
101             {\r
102                 BeginInvoke(new UpdateHandler(SetUIEncodeStarted));\r
103                 return;\r
104             }\r
105             btn_encode.Enabled = false;\r
106             btn_pause.Visible = true;\r
107         }\r
108         private void SetUIEncodeFinished()\r
109         {\r
110             if (InvokeRequired)\r
111             {\r
112                 BeginInvoke(new UpdateHandler(SetUIEncodeFinished));\r
113                 return;\r
114             }\r
115             btn_pause.Visible = false;\r
116             btn_encode.Enabled = true;\r
117         }\r
118         private void ResetQueue()\r
119         {\r
120             if (InvokeRequired)\r
121             {\r
122                 BeginInvoke(new UpdateHandler(ResetQueue));\r
123                 return;\r
124             }\r
125             btn_pause.Visible = false;\r
126             btn_encode.Enabled = true;\r
127 \r
128             lbl_source.Text = "-";\r
129             lbl_dest.Text = "-";\r
130             lbl_vEnc.Text = "-";\r
131             lbl_aEnc.Text = "-";\r
132             lbl_title.Text = "-";\r
133             lbl_chapt.Text = "-";\r
134 \r
135             lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";\r
136         }\r
137         private void RedrawQueue()\r
138         {\r
139             if (InvokeRequired)\r
140             {\r
141                 BeginInvoke(new UpdateHandler(RedrawQueue));\r
142                 return;\r
143             }\r
144 \r
145             list_queue.Items.Clear();\r
146             ReadOnlyCollection<Job> theQueue = queue.CurrentQueue;\r
147             foreach (Job queue_item in theQueue)\r
148             {\r
149                 string q_item = queue_item.Query;\r
150                 Functions.QueryParser parsed = Functions.QueryParser.Parse(q_item);\r
151 \r
152                 // Get the DVD Title\r
153                 string title = parsed.DVDTitle == 0 ? "Auto" : parsed.DVDTitle.ToString();\r
154 \r
155                 // Get the DVD Chapters\r
156                 string chapters;\r
157                 if (parsed.DVDChapterStart == 0)\r
158                     chapters = "Auto";\r
159                 else\r
160                 {\r
161                     chapters = parsed.DVDChapterStart.ToString();\r
162                     if (parsed.DVDChapterFinish != 0)\r
163                         chapters = chapters + " - " + parsed.DVDChapterFinish;\r
164                 }\r
165 \r
166                 ListViewItem item = new ListViewItem();\r
167                 item.Text = title; // Title\r
168                 item.SubItems.Add(chapters); // Chapters\r
169                 item.SubItems.Add(queue_item.Source); // Source\r
170                 item.SubItems.Add(queue_item.Destination); // Destination\r
171                 item.SubItems.Add(parsed.VideoEncoder); // Video\r
172 \r
173                 // Display The Audio Track Information\r
174                 string audio = string.Empty;\r
175                 foreach (Functions.AudioTrack track in parsed.AudioInformation)\r
176                 {\r
177                     if (audio != "")\r
178                         audio += ", " + track.Encoder;\r
179                     else\r
180                         audio = track.Encoder;\r
181                 }\r
182                 item.SubItems.Add(audio); // Audio\r
183 \r
184                 list_queue.Items.Add(item);\r
185             }\r
186         }\r
187         private void UpdateUIElements()\r
188         {\r
189             if (InvokeRequired)\r
190             {\r
191                 BeginInvoke(new UpdateHandler(UpdateUIElements));\r
192                 return;\r
193             }\r
194 \r
195             RedrawQueue();\r
196             lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";\r
197         }\r
198         private void SetCurrentEncodeInformation()\r
199         {\r
200             try\r
201             {\r
202                 if (InvokeRequired)\r
203                 {\r
204                     BeginInvoke(new UpdateHandler(SetCurrentEncodeInformation));\r
205                 }\r
206 \r
207                 // found query is a global varible\r
208                 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue.LastEncode.Query);\r
209                 lbl_source.Text = queue.LastEncode.Source;\r
210                 lbl_dest.Text = queue.LastEncode.Destination;\r
211 \r
212                 lbl_title.Text = parsed.DVDTitle == 0 ? "Auto" : parsed.DVDTitle.ToString();\r
213 \r
214                 if (Equals(parsed.DVDChapterStart, 0))\r
215                     lbl_chapt.Text = "Auto";\r
216                 else\r
217                 {\r
218                     string chapters = parsed.DVDChapterStart.ToString();\r
219                     if (parsed.DVDChapterFinish != 0)\r
220                         chapters = chapters + " - " + parsed.DVDChapterFinish;\r
221                     lbl_chapt.Text = chapters;\r
222                 }\r
223 \r
224                 lbl_vEnc.Text = parsed.VideoEncoder;\r
225 \r
226                 // Display The Audio Track Information\r
227                 string audio = string.Empty;\r
228                 foreach (Functions.AudioTrack track in parsed.AudioInformation)\r
229                 {\r
230                     if (audio != "")\r
231                         audio += ", " + track.Encoder;\r
232                     else\r
233                         audio = track.Encoder;\r
234                 }\r
235                 lbl_aEnc.Text = audio;\r
236             }\r
237             catch (Exception)\r
238             {\r
239                 // Do Nothing\r
240             }\r
241         }\r
242         private void DeleteSelectedItems()\r
243         {\r
244             // If there are selected items\r
245             if (list_queue.SelectedIndices.Count > 0)\r
246             {\r
247                 // Save the selected indices to select them after the move\r
248                 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);\r
249                 foreach (int selectedIndex in list_queue.SelectedIndices)\r
250                     selectedIndices.Add(selectedIndex);\r
251 \r
252                 int firstSelectedIndex = selectedIndices[0];\r
253 \r
254                 // Reverse the list to delete the items from last to first (preserves indices)\r
255                 selectedIndices.Reverse();\r
256 \r
257                 // Remove each selected item\r
258                 foreach (int selectedIndex in selectedIndices)\r
259                     queue.Remove(selectedIndex);\r
260 \r
261                 UpdateUIElements();\r
262 \r
263                 // Select the item where the first deleted item was previously\r
264                 if (firstSelectedIndex < list_queue.Items.Count)\r
265                     list_queue.Items[firstSelectedIndex].Selected = true;\r
266             }\r
267 \r
268             list_queue.Select(); // Activate the control to show the selected items\r
269         }\r
270 \r
271         // Queue Management\r
272         private void mnu_up_Click(object sender, EventArgs e)\r
273         {\r
274             MoveUp();\r
275         }\r
276         private void mnu_Down_Click(object sender, EventArgs e)\r
277         {\r
278             MoveDown();\r
279         }\r
280         private void mnu_delete_Click(object sender, EventArgs e)\r
281         {\r
282             DeleteSelectedItems();\r
283         }\r
284         private void btn_up_Click(object sender, EventArgs e)\r
285         {\r
286             MoveUp();\r
287         }\r
288         private void btn_down_Click(object sender, EventArgs e)\r
289         {\r
290             MoveDown();\r
291         }\r
292         private void btn_delete_Click(object sender, EventArgs e)\r
293         {\r
294             DeleteSelectedItems();\r
295         }\r
296         private void list_queue_deleteKey(object sender, KeyEventArgs e)\r
297         {\r
298             if (e.KeyCode == Keys.Delete)\r
299                 DeleteSelectedItems();\r
300         }\r
301         private void MoveUp()\r
302         {\r
303             // If there are selected items and the first item is not selected\r
304             if (list_queue.SelectedIndices.Count > 0 && !list_queue.SelectedIndices.Contains(0))\r
305             {\r
306                 // Copy the selected indices to preserve them during the movement\r
307                 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);\r
308                 foreach (int selectedIndex in list_queue.SelectedIndices)\r
309                     selectedIndices.Add(selectedIndex);\r
310 \r
311                 // Move up each selected item\r
312                 foreach (int selectedIndex in selectedIndices)\r
313                     queue.MoveUp(selectedIndex);\r
314 \r
315                 UpdateUIElements();\r
316 \r
317                 // Keep the selected item(s) selected, now moved up one index\r
318                 foreach (int selectedIndex in selectedIndices)\r
319                     if (selectedIndex - 1 > -1) // Defensive programming: ensure index is good\r
320                         list_queue.Items[selectedIndex - 1].Selected = true;\r
321             }\r
322 \r
323             list_queue.Select(); // Activate the control to show the selected items\r
324         }\r
325         private void MoveDown()\r
326         {\r
327             // If there are selected items and the last item is not selected\r
328             if (list_queue.SelectedIndices.Count > 0 &&\r
329                 !list_queue.SelectedIndices.Contains(list_queue.Items[list_queue.Items.Count - 1].Index))\r
330             {\r
331                 // Copy the selected indices to preserve them during the movement\r
332                 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);\r
333                 foreach (int selectedIndex in list_queue.SelectedIndices)\r
334                     selectedIndices.Add(selectedIndex);\r
335 \r
336                 // Reverse the indices to move the items down from last to first (preserves indices)\r
337                 selectedIndices.Reverse();\r
338 \r
339                 // Move down each selected item\r
340                 foreach (int selectedIndex in selectedIndices)\r
341                     queue.MoveDown(selectedIndex);\r
342 \r
343                 UpdateUIElements();\r
344 \r
345                 // Keep the selected item(s) selected, now moved down one index\r
346                 foreach (int selectedIndex in selectedIndices)\r
347                     if (selectedIndex + 1 < list_queue.Items.Count) // Defensive programming: ensure index is good\r
348                         list_queue.Items[selectedIndex + 1].Selected = true;\r
349             }\r
350 \r
351             list_queue.Select(); // Activate the control to show the selected items\r
352         }\r
353 \r
354         // Queue Import/Export Features\r
355         private void mnu_batch_Click(object sender, EventArgs e)\r
356         {\r
357             SaveFile.FileName = "";\r
358             SaveFile.Filter = "Batch|.bat";\r
359             SaveFile.ShowDialog();\r
360             if (SaveFile.FileName != String.Empty)\r
361                 queue.WriteBatchScriptToFile(SaveFile.FileName);\r
362         }\r
363         private void mnu_export_Click(object sender, EventArgs e)\r
364         {\r
365             SaveFile.FileName = "";\r
366             SaveFile.Filter = "HandBrake Queue|*.queue";\r
367             SaveFile.ShowDialog();\r
368             if (SaveFile.FileName != String.Empty)\r
369                 queue.WriteQueueStateToFile(SaveFile.FileName);\r
370         }\r
371         private void mnu_import_Click(object sender, EventArgs e)\r
372         {\r
373             OpenFile.FileName = "";\r
374             OpenFile.ShowDialog();\r
375             if (OpenFile.FileName != String.Empty)\r
376                 queue.LoadQueueFromFile(OpenFile.FileName);\r
377             UpdateUIElements();\r
378         }\r
379         private void mnu_readd_Click(object sender, EventArgs e)\r
380         {\r
381             if (!queue.LastEncode.IsEmpty)\r
382             {\r
383                 queue.Add(queue.LastEncode.Query, queue.LastEncode.Source, queue.LastEncode.Destination, queue.LastEncode.CustomQuery);\r
384                 UpdateUIElements();\r
385             }\r
386         }\r
387 \r
388         // Hide's the window when the user tries to "x" out of the window instead of closing it.\r
389         protected override void OnClosing(CancelEventArgs e)\r
390         {\r
391             e.Cancel = true;\r
392             this.Hide();\r
393             base.OnClosing(e);\r
394         }\r
395 \r
396     }\r
397 }\r