OSDN Git Service

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