OSDN Git Service

93587dba888cd0989c3f29cddd4c1d8b2b99bae8
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmQueue.cs
1 /*  frmQueue.cs $\r
2     This file is part of the HandBrake source code.\r
3     Homepage: <http://handbrake.fr>.\r
4     It may be used under the terms of the GNU General Public License. */\r
5 \r
6 namespace Handbrake\r
7 {\r
8     using System;\r
9     using System.Collections.Generic;\r
10     using System.Collections.ObjectModel;\r
11     using System.ComponentModel;\r
12     using System.Windows.Forms;\r
13     using Functions;\r
14     using Model;\r
15     using Services;\r
16 \r
17     /// <summary>\r
18     /// The Queue Window\r
19     /// </summary>\r
20     public partial class frmQueue : Form\r
21     {\r
22         /// <summary>\r
23         /// Update Handler Delegate\r
24         /// </summary>\r
25         private delegate void UpdateHandler();\r
26 \r
27         /// <summary>\r
28         /// An instance of the Queue service\r
29         /// </summary>\r
30         private readonly Queue queue;\r
31 \r
32         /// <summary>\r
33         /// A reference to the main application window\r
34         /// </summary>\r
35         private readonly frmMain mainWindow;\r
36 \r
37         /// <summary>\r
38         /// Initializes a new instance of the <see cref="frmQueue"/> class.\r
39         /// </summary>\r
40         /// <param name="q">\r
41         /// An instance of the queue service.\r
42         /// </param>\r
43         /// <param name="mw">\r
44         /// The main window.\r
45         /// </param>\r
46         public frmQueue(Queue q, frmMain mw)\r
47         {\r
48             InitializeComponent();\r
49 \r
50             this.mainWindow = mw;\r
51 \r
52             this.queue = q;\r
53             queue.EncodeStarted += new EventHandler(QueueOnEncodeStart);\r
54             queue.QueueCompleted += new EventHandler(QueueOnQueueFinished);\r
55             queue.QueuePauseRequested += new EventHandler(QueueOnPaused);\r
56         }\r
57 \r
58         /// <summary>\r
59         /// Handle the Queue Paused event\r
60         /// </summary>\r
61         /// <param name="sender">\r
62         /// The sender.\r
63         /// </param>\r
64         /// <param name="e">\r
65         /// The EventArgs.\r
66         /// </param>\r
67         private void QueueOnPaused(object sender, EventArgs e)\r
68         {\r
69             SetUiEncodeFinished();\r
70             UpdateUiElements();\r
71         }\r
72 \r
73         /// <summary>\r
74         /// Handle the Queue Finished event.\r
75         /// </summary>\r
76         /// <param name="sender">\r
77         /// The sender.\r
78         /// </param>\r
79         /// <param name="e">\r
80         /// The EventArgs.\r
81         /// </param>\r
82         private void QueueOnQueueFinished(object sender, EventArgs e)\r
83         {\r
84             SetUiEncodeFinished();\r
85             ResetQueue(); // Reset the Queue Window\r
86         }\r
87 \r
88         /// <summary>\r
89         /// Handle the Encode Started event\r
90         /// </summary>\r
91         /// <param name="sender">\r
92         /// The sender.\r
93         /// </param>\r
94         /// <param name="e">\r
95         /// The e.\r
96         /// </param>\r
97         private void QueueOnEncodeStart(object sender, EventArgs e)\r
98         {\r
99             SetUiEncodeStarted(); // make sure the UI is set correctly\r
100             SetCurrentEncodeInformation();\r
101             UpdateUiElements(); // Redraw the Queue, a new encode has started.\r
102         }\r
103 \r
104         /// <summary>\r
105         /// Initializes the Queue list with the Arraylist from the Queue class\r
106         /// </summary>\r
107         public void SetQueue()\r
108         {\r
109             UpdateUiElements();\r
110         }\r
111 \r
112         /// <summary>\r
113         /// Initializes the Queue list, then shows and activates the window\r
114         /// </summary>\r
115         public new void Show()\r
116         {\r
117             Show(true);\r
118         }\r
119 \r
120         /// <summary>\r
121         /// Initializes the Queue list only if doSetQueue is true, then shows and activates the window\r
122         /// </summary>\r
123         /// <param name="doSetQueue">Indicates whether to call setQueue() before showing the window</param>\r
124         public void Show(bool doSetQueue)\r
125         {\r
126             if (doSetQueue) SetQueue();\r
127             base.Show();\r
128         }\r
129 \r
130         /// <summary>\r
131         /// Handle the Encode button Click event\r
132         /// </summary>\r
133         /// <param name="sender">The sender</param>\r
134         /// <param name="e">the EventArgs</param>\r
135         private void BtnEncodeClick(object sender, EventArgs e)\r
136         {\r
137             if (queue.Paused)\r
138             {\r
139                 SetUiEncodeStarted();\r
140             }\r
141 \r
142             if (!queue.IsEncoding)\r
143                 queue.Start();\r
144         }\r
145 \r
146         /// <summary>\r
147         /// Handle the Pause button click event.\r
148         /// </summary>\r
149         /// <param name="sender">\r
150         /// The sender.\r
151         /// </param>\r
152         /// <param name="e">\r
153         /// The EventArgs.\r
154         /// </param>\r
155         private void BtnPauseClick(object sender, EventArgs e)\r
156         {\r
157             queue.Pause();\r
158             SetUiEncodeFinished();\r
159             ResetQueue();\r
160             MessageBox.Show(\r
161                 "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.",\r
162                 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
163         }\r
164 \r
165         // UI Work\r
166 \r
167         /// <summary>\r
168         /// Setup the UI to show that an encode has started\r
169         /// </summary>\r
170         private void SetUiEncodeStarted()\r
171         {\r
172             if (InvokeRequired)\r
173             {\r
174                 BeginInvoke(new UpdateHandler(SetUiEncodeStarted));\r
175                 return;\r
176             }\r
177             btn_encode.Enabled = false;\r
178             btn_pause.Visible = true;\r
179         }\r
180 \r
181         /// <summary>\r
182         /// Setup the UI to indicate that an encode has finished.\r
183         /// </summary>\r
184         private void SetUiEncodeFinished()\r
185         {\r
186             if (InvokeRequired)\r
187             {\r
188                 BeginInvoke(new UpdateHandler(SetUiEncodeFinished));\r
189                 return;\r
190             }\r
191             btn_pause.Visible = false;\r
192             btn_encode.Enabled = true;\r
193         }\r
194 \r
195         /// <summary>\r
196         /// Reset the Queue Window display\r
197         /// </summary>\r
198         private void ResetQueue()\r
199         {\r
200             if (InvokeRequired)\r
201             {\r
202                 BeginInvoke(new UpdateHandler(ResetQueue));\r
203                 return;\r
204             }\r
205             btn_pause.Visible = false;\r
206             btn_encode.Enabled = true;\r
207 \r
208             lbl_source.Text = "-";\r
209             lbl_dest.Text = "-";\r
210             lbl_vEnc.Text = "-";\r
211             lbl_aEnc.Text = "-";\r
212             lbl_title.Text = "-";\r
213             lbl_chapt.Text = "-";\r
214 \r
215             lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";\r
216         }\r
217 \r
218         /// <summary>\r
219         /// Redraw the Queue window with the latest information about HandBrakes status\r
220         /// </summary>\r
221         private void RedrawQueue()\r
222         {\r
223             if (InvokeRequired)\r
224             {\r
225                 BeginInvoke(new UpdateHandler(RedrawQueue));\r
226                 return;\r
227             }\r
228 \r
229             list_queue.Items.Clear();\r
230             ReadOnlyCollection<Job> theQueue = queue.CurrentQueue;\r
231             foreach (Job queueItem in theQueue)\r
232             {\r
233                 string qItem = queueItem.Query;\r
234                 QueryParser parsed = Functions.QueryParser.Parse(qItem);\r
235 \r
236                 // Get the DVD Title\r
237                 string title = parsed.Title == 0 ? "Auto" : parsed.Title.ToString();\r
238 \r
239                 // Get the DVD Chapters\r
240                 string chapters;\r
241                 if (parsed.ChapterStart == 0)\r
242                     chapters = "Auto";\r
243                 else\r
244                 {\r
245                     chapters = parsed.ChapterStart.ToString();\r
246                     if (parsed.ChapterFinish != 0)\r
247                         chapters = chapters + " - " + parsed.ChapterFinish;\r
248                 }\r
249 \r
250                 ListViewItem item = new ListViewItem();\r
251                 item.Text = title; // Title\r
252                 item.SubItems.Add(chapters); // Chapters\r
253                 item.SubItems.Add(queueItem.Source); // Source\r
254                 item.SubItems.Add(queueItem.Destination); // Destination\r
255                 item.SubItems.Add(parsed.VideoEncoder); // Video\r
256 \r
257                 // Display The Audio Track Information\r
258                 string audio = string.Empty;\r
259                 foreach (AudioTrack track in parsed.AudioInformation)\r
260                 {\r
261                     if (audio != string.Empty)\r
262                         audio += ", " + track.Encoder;\r
263                     else\r
264                         audio = track.Encoder;\r
265                 }\r
266                 item.SubItems.Add(audio); // Audio\r
267 \r
268                 list_queue.Items.Add(item);\r
269             }\r
270         }\r
271 \r
272         /// <summary>\r
273         /// Update the UI elements\r
274         /// </summary>\r
275         private void UpdateUiElements()\r
276         {\r
277             if (InvokeRequired)\r
278             {\r
279                 BeginInvoke(new UpdateHandler(UpdateUiElements));\r
280                 return;\r
281             }\r
282 \r
283             RedrawQueue();\r
284             lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";\r
285         }\r
286 \r
287         /// <summary>\r
288         /// Set the window up with the current encode information\r
289         /// </summary>\r
290         private void SetCurrentEncodeInformation()\r
291         {\r
292             try\r
293             {\r
294                 if (InvokeRequired)\r
295                 {\r
296                     BeginInvoke(new UpdateHandler(SetCurrentEncodeInformation));\r
297                 }\r
298 \r
299                 // found query is a global varible\r
300                 QueryParser parsed = QueryParser.Parse(queue.LastEncode.Query);\r
301                 lbl_source.Text = queue.LastEncode.Source;\r
302                 lbl_dest.Text = queue.LastEncode.Destination;\r
303 \r
304                 lbl_title.Text = parsed.Title == 0 ? "Auto" : parsed.Title.ToString();\r
305 \r
306                 if (Equals(parsed.ChapterStart, 0))\r
307                     lbl_chapt.Text = "Auto";\r
308                 else\r
309                 {\r
310                     string chapters = parsed.ChapterStart.ToString();\r
311                     if (parsed.ChapterFinish != 0)\r
312                         chapters = chapters + " - " + parsed.ChapterFinish;\r
313                     lbl_chapt.Text = chapters;\r
314                 }\r
315 \r
316                 lbl_vEnc.Text = parsed.VideoEncoder;\r
317 \r
318                 // Display The Audio Track Information\r
319                 string audio = string.Empty;\r
320                 foreach (AudioTrack track in parsed.AudioInformation)\r
321                 {\r
322                     if (audio != string.Empty)\r
323                         audio += ", " + track.Encoder;\r
324                     else\r
325                         audio = track.Encoder;\r
326                 }\r
327                 lbl_aEnc.Text = audio;\r
328             }\r
329             catch (Exception)\r
330             {\r
331                 // Do Nothing\r
332             }\r
333         }\r
334 \r
335         /// <summary>\r
336         /// Delete the currently selected items on the queue\r
337         /// </summary>\r
338         private void DeleteSelectedItems()\r
339         {\r
340             // If there are selected items\r
341             if (list_queue.SelectedIndices.Count > 0)\r
342             {\r
343                 // Save the selected indices to select them after the move\r
344                 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);\r
345                 foreach (int selectedIndex in list_queue.SelectedIndices)\r
346                     selectedIndices.Add(selectedIndex);\r
347 \r
348                 int firstSelectedIndex = selectedIndices[0];\r
349 \r
350                 // Reverse the list to delete the items from last to first (preserves indices)\r
351                 selectedIndices.Reverse();\r
352 \r
353                 // Remove each selected item\r
354                 foreach (int selectedIndex in selectedIndices)\r
355                     queue.Remove(selectedIndex);\r
356 \r
357                 UpdateUiElements();\r
358 \r
359                 // Select the item where the first deleted item was previously\r
360                 if (firstSelectedIndex < list_queue.Items.Count)\r
361                     list_queue.Items[firstSelectedIndex].Selected = true;\r
362             }\r
363 \r
364             list_queue.Select(); // Activate the control to show the selected items\r
365         }\r
366 \r
367         // Queue Management\r
368         /// <summary>\r
369         /// Handle the Move Up Menu Item\r
370         /// </summary>\r
371         /// <param name="sender">\r
372         /// The sender.\r
373         /// </param>\r
374         /// <param name="e">\r
375         /// The e.\r
376         /// </param>\r
377         private void MnuUpClick(object sender, EventArgs e)\r
378         {\r
379             MoveUp();\r
380         }\r
381 \r
382         /// <summary>\r
383         /// Handle the Move down Menu Item\r
384         /// </summary>\r
385         /// <param name="sender">\r
386         /// The sender.\r
387         /// </param>\r
388         /// <param name="e">\r
389         /// The e.\r
390         /// </param>\r
391         private void MnuDownClick(object sender, EventArgs e)\r
392         {\r
393             MoveDown();\r
394         }\r
395 \r
396         /// <summary>\r
397         /// Edit a job\r
398         /// </summary>\r
399         /// <param name="sender">\r
400         /// The sender.\r
401         /// </param>\r
402         /// <param name="e">\r
403         /// The e.\r
404         /// </param>\r
405         private void MnuEditClick(object sender, EventArgs e)\r
406         {\r
407             if (list_queue.SelectedIndices != null && list_queue.SelectedIndices.Count != 0)\r
408             {\r
409                 lock (queue)\r
410                 {\r
411                     lock (list_queue)\r
412                     {\r
413                         int index = list_queue.SelectedIndices[0];\r
414                         mainWindow.RecievingJob(queue.GetJob(index));\r
415                         queue.Remove(index);\r
416                         RedrawQueue();\r
417                     }\r
418                 }\r
419             }\r
420         }\r
421 \r
422         /// <summary>\r
423         /// Handle the delete Menu Item\r
424         /// </summary>\r
425         /// <param name="sender">\r
426         /// The sender.\r
427         /// </param>\r
428         /// <param name="e">\r
429         /// The e.\r
430         /// </param>\r
431         private void MnuDeleteClick(object sender, EventArgs e)\r
432         {\r
433             DeleteSelectedItems();\r
434         }\r
435 \r
436         /// <summary>\r
437         /// Handle the Button Up Click\r
438         /// </summary>\r
439         /// <param name="sender">\r
440         /// The sender.\r
441         /// </param>\r
442         /// <param name="e">\r
443         /// The e.\r
444         /// </param>\r
445         private void BtnUpClick(object sender, EventArgs e)\r
446         {\r
447             MoveUp();\r
448         }\r
449 \r
450         /// <summary>\r
451         /// Handle the button down click\r
452         /// </summary>\r
453         /// <param name="sender">\r
454         /// The sender.\r
455         /// </param>\r
456         /// <param name="e">\r
457         /// The e.\r
458         /// </param>\r
459         private void BtnDownClick(object sender, EventArgs e)\r
460         {\r
461             MoveDown();\r
462         }\r
463 \r
464         /// <summary>\r
465         /// Handle the delete button click\r
466         /// </summary>\r
467         /// <param name="sender">\r
468         /// The sender.\r
469         /// </param>\r
470         /// <param name="e">\r
471         /// The e.\r
472         /// </param>\r
473         private void BtnDeleteClick(object sender, EventArgs e)\r
474         {\r
475             DeleteSelectedItems();\r
476         }\r
477 \r
478         /// <summary>\r
479         /// Handle the delete keyboard press\r
480         /// </summary>\r
481         /// <param name="sender">\r
482         /// The sender.\r
483         /// </param>\r
484         /// <param name="e">\r
485         /// The e.\r
486         /// </param>\r
487         private void ListQueueDeleteKey(object sender, KeyEventArgs e)\r
488         {\r
489             if (e.KeyCode == Keys.Delete)\r
490                 DeleteSelectedItems();\r
491         }\r
492 \r
493         /// <summary>\r
494         /// Move items up in the queue\r
495         /// </summary>\r
496         private void MoveUp()\r
497         {\r
498             // If there are selected items and the first item is not selected\r
499             if (list_queue.SelectedIndices.Count > 0 && !list_queue.SelectedIndices.Contains(0))\r
500             {\r
501                 // Copy the selected indices to preserve them during the movement\r
502                 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);\r
503                 foreach (int selectedIndex in list_queue.SelectedIndices)\r
504                     selectedIndices.Add(selectedIndex);\r
505 \r
506                 // Move up each selected item\r
507                 foreach (int selectedIndex in selectedIndices)\r
508                     queue.MoveUp(selectedIndex);\r
509 \r
510                 UpdateUiElements();\r
511 \r
512                 // Keep the selected item(s) selected, now moved up one index\r
513                 foreach (int selectedIndex in selectedIndices)\r
514                     if (selectedIndex - 1 > -1) // Defensive programming: ensure index is good\r
515                         list_queue.Items[selectedIndex - 1].Selected = true;\r
516             }\r
517 \r
518             list_queue.Select(); // Activate the control to show the selected items\r
519         }\r
520 \r
521         /// <summary>\r
522         /// Move items down in the queue\r
523         /// </summary>\r
524         private void MoveDown()\r
525         {\r
526             // If there are selected items and the last item is not selected\r
527             if (list_queue.SelectedIndices.Count > 0 &&\r
528                 !list_queue.SelectedIndices.Contains(list_queue.Items[list_queue.Items.Count - 1].Index))\r
529             {\r
530                 // Copy the selected indices to preserve them during the movement\r
531                 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);\r
532                 foreach (int selectedIndex in list_queue.SelectedIndices)\r
533                     selectedIndices.Add(selectedIndex);\r
534 \r
535                 // Reverse the indices to move the items down from last to first (preserves indices)\r
536                 selectedIndices.Reverse();\r
537 \r
538                 // Move down each selected item\r
539                 foreach (int selectedIndex in selectedIndices)\r
540                     queue.MoveDown(selectedIndex);\r
541 \r
542                 UpdateUiElements();\r
543 \r
544                 // Keep the selected item(s) selected, now moved down one index\r
545                 foreach (int selectedIndex in selectedIndices)\r
546                     if (selectedIndex + 1 < list_queue.Items.Count) // Defensive programming: ensure index is good\r
547                         list_queue.Items[selectedIndex + 1].Selected = true;\r
548             }\r
549 \r
550             list_queue.Select(); // Activate the control to show the selected items\r
551         }\r
552 \r
553         // Queue Import/Export Features\r
554 \r
555         /// <summary>\r
556         /// Create a batch script\r
557         /// </summary>\r
558         /// <param name="sender">\r
559         /// The sender.\r
560         /// </param>\r
561         /// <param name="e">\r
562         /// The e.\r
563         /// </param>\r
564         private void MnuBatchClick(object sender, EventArgs e)\r
565         {\r
566             SaveFile.FileName = string.Empty;\r
567             SaveFile.Filter = "Batch|.bat";\r
568             SaveFile.ShowDialog();\r
569             if (SaveFile.FileName != String.Empty)\r
570                 queue.WriteBatchScriptToFile(SaveFile.FileName);\r
571         }\r
572 \r
573         /// <summary>\r
574         /// Export Queue\r
575         /// </summary>\r
576         /// <param name="sender">\r
577         /// The sender.\r
578         /// </param>\r
579         /// <param name="e">\r
580         /// The e.\r
581         /// </param>\r
582         private void MnuExportClick(object sender, EventArgs e)\r
583         {\r
584             SaveFile.FileName = string.Empty;\r
585             SaveFile.Filter = "HandBrake Queue|*.queue";\r
586             SaveFile.ShowDialog();\r
587             if (SaveFile.FileName != String.Empty)\r
588                 queue.WriteQueueStateToFile(SaveFile.FileName);\r
589         }\r
590 \r
591         /// <summary>\r
592         /// Import Queue\r
593         /// </summary>\r
594         /// <param name="sender">\r
595         /// The sender.\r
596         /// </param>\r
597         /// <param name="e">\r
598         /// The e.\r
599         /// </param>\r
600         private void MnuImportClick(object sender, EventArgs e)\r
601         {\r
602             OpenFile.FileName = string.Empty;\r
603             OpenFile.ShowDialog();\r
604             if (OpenFile.FileName != String.Empty)\r
605                 queue.LoadQueueFromFile(OpenFile.FileName);\r
606             UpdateUiElements();\r
607         }\r
608 \r
609         /// <summary>\r
610         /// Readd current job to queue\r
611         /// </summary>\r
612         /// <param name="sender">\r
613         /// The sender.\r
614         /// </param>\r
615         /// <param name="e">\r
616         /// The e.\r
617         /// </param>\r
618         private void MnuReaddClick(object sender, EventArgs e)\r
619         {\r
620             if (!queue.LastEncode.IsEmpty)\r
621             {\r
622                 queue.Add(\r
623                     queue.LastEncode.Query, \r
624                     queue.LastEncode.Title, \r
625                     queue.LastEncode.Source,\r
626                     queue.LastEncode.Destination,\r
627                     queue.LastEncode.CustomQuery);\r
628                 UpdateUiElements();\r
629             }\r
630         }\r
631 \r
632         /// <summary>\r
633         /// Hide's the window when the user tries to "x" out of the window instead of closing it.\r
634         /// </summary>\r
635         /// <param name="e">\r
636         /// The e.\r
637         /// </param>\r
638         protected override void OnClosing(CancelEventArgs e)\r
639         {\r
640             e.Cancel = true;\r
641             this.Hide();\r
642             base.OnClosing(e);\r
643         }\r
644     }\r
645 }