OSDN Git Service

WinGui:
[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             // Activate();\r
130         }\r
131 \r
132         /// <summary>\r
133         /// Handle the Encode button Click event\r
134         /// </summary>\r
135         /// <param name="sender">The sender</param>\r
136         /// <param name="e">the EventArgs</param>\r
137         private void BtnEncodeClick(object sender, EventArgs e)\r
138         {\r
139             if (queue.PauseRequested)\r
140             {\r
141                 SetUiEncodeStarted();\r
142                 MessageBox.Show("Encoding restarted", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
143             }\r
144 \r
145             if (!queue.IsEncoding)\r
146                 queue.Start();\r
147         }\r
148 \r
149         /// <summary>\r
150         /// Handle the Pause button click event.\r
151         /// </summary>\r
152         /// <param name="sender">\r
153         /// The sender.\r
154         /// </param>\r
155         /// <param name="e">\r
156         /// The EventArgs.\r
157         /// </param>\r
158         private void BtnPauseClick(object sender, EventArgs e)\r
159         {\r
160             queue.Pause();\r
161             SetUiEncodeFinished();\r
162             ResetQueue();\r
163             MessageBox.Show(\r
164                 "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
165                 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
166         }\r
167 \r
168         // UI Work\r
169 \r
170         /// <summary>\r
171         /// Setup the UI to show that an encode has started\r
172         /// </summary>\r
173         private void SetUiEncodeStarted()\r
174         {\r
175             if (InvokeRequired)\r
176             {\r
177                 BeginInvoke(new UpdateHandler(SetUiEncodeStarted));\r
178                 return;\r
179             }\r
180             btn_encode.Enabled = false;\r
181             btn_pause.Visible = true;\r
182         }\r
183 \r
184         /// <summary>\r
185         /// Setup the UI to indicate that an encode has finished.\r
186         /// </summary>\r
187         private void SetUiEncodeFinished()\r
188         {\r
189             if (InvokeRequired)\r
190             {\r
191                 BeginInvoke(new UpdateHandler(SetUiEncodeFinished));\r
192                 return;\r
193             }\r
194             btn_pause.Visible = false;\r
195             btn_encode.Enabled = true;\r
196         }\r
197 \r
198         /// <summary>\r
199         /// Reset the Queue Window display\r
200         /// </summary>\r
201         private void ResetQueue()\r
202         {\r
203             if (InvokeRequired)\r
204             {\r
205                 BeginInvoke(new UpdateHandler(ResetQueue));\r
206                 return;\r
207             }\r
208             btn_pause.Visible = false;\r
209             btn_encode.Enabled = true;\r
210 \r
211             lbl_source.Text = "-";\r
212             lbl_dest.Text = "-";\r
213             lbl_vEnc.Text = "-";\r
214             lbl_aEnc.Text = "-";\r
215             lbl_title.Text = "-";\r
216             lbl_chapt.Text = "-";\r
217 \r
218             lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";\r
219         }\r
220 \r
221         /// <summary>\r
222         /// Redraw the Queue window with the latest information about HandBrakes status\r
223         /// </summary>\r
224         private void RedrawQueue()\r
225         {\r
226             if (InvokeRequired)\r
227             {\r
228                 BeginInvoke(new UpdateHandler(RedrawQueue));\r
229                 return;\r
230             }\r
231 \r
232             list_queue.Items.Clear();\r
233             ReadOnlyCollection<Job> theQueue = queue.CurrentQueue;\r
234             foreach (Job queueItem in theQueue)\r
235             {\r
236                 string qItem = queueItem.Query;\r
237                 QueryParser parsed = Functions.QueryParser.Parse(qItem);\r
238 \r
239                 // Get the DVD Title\r
240                 string title = parsed.DVDTitle == 0 ? "Auto" : parsed.DVDTitle.ToString();\r
241 \r
242                 // Get the DVD Chapters\r
243                 string chapters;\r
244                 if (parsed.DVDChapterStart == 0)\r
245                     chapters = "Auto";\r
246                 else\r
247                 {\r
248                     chapters = parsed.DVDChapterStart.ToString();\r
249                     if (parsed.DVDChapterFinish != 0)\r
250                         chapters = chapters + " - " + parsed.DVDChapterFinish;\r
251                 }\r
252 \r
253                 ListViewItem item = new ListViewItem();\r
254                 item.Text = title; // Title\r
255                 item.SubItems.Add(chapters); // Chapters\r
256                 item.SubItems.Add(queueItem.Source); // Source\r
257                 item.SubItems.Add(queueItem.Destination); // Destination\r
258                 item.SubItems.Add(parsed.VideoEncoder); // Video\r
259 \r
260                 // Display The Audio Track Information\r
261                 string audio = string.Empty;\r
262                 foreach (AudioTrack track in parsed.AudioInformation)\r
263                 {\r
264                     if (audio != string.Empty)\r
265                         audio += ", " + track.Encoder;\r
266                     else\r
267                         audio = track.Encoder;\r
268                 }\r
269                 item.SubItems.Add(audio); // Audio\r
270 \r
271                 list_queue.Items.Add(item);\r
272             }\r
273         }\r
274 \r
275         /// <summary>\r
276         /// Update the UI elements\r
277         /// </summary>\r
278         private void UpdateUiElements()\r
279         {\r
280             if (InvokeRequired)\r
281             {\r
282                 BeginInvoke(new UpdateHandler(UpdateUiElements));\r
283                 return;\r
284             }\r
285 \r
286             RedrawQueue();\r
287             lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";\r
288         }\r
289 \r
290         /// <summary>\r
291         /// Set the window up with the current encode information\r
292         /// </summary>\r
293         private void SetCurrentEncodeInformation()\r
294         {\r
295             try\r
296             {\r
297                 if (InvokeRequired)\r
298                 {\r
299                     BeginInvoke(new UpdateHandler(SetCurrentEncodeInformation));\r
300                 }\r
301 \r
302                 // found query is a global varible\r
303                 QueryParser parsed = Functions.QueryParser.Parse(queue.LastEncode.Query);\r
304                 lbl_source.Text = queue.LastEncode.Source;\r
305                 lbl_dest.Text = queue.LastEncode.Destination;\r
306 \r
307                 lbl_title.Text = parsed.DVDTitle == 0 ? "Auto" : parsed.DVDTitle.ToString();\r
308 \r
309                 if (Equals(parsed.DVDChapterStart, 0))\r
310                     lbl_chapt.Text = "Auto";\r
311                 else\r
312                 {\r
313                     string chapters = parsed.DVDChapterStart.ToString();\r
314                     if (parsed.DVDChapterFinish != 0)\r
315                         chapters = chapters + " - " + parsed.DVDChapterFinish;\r
316                     lbl_chapt.Text = chapters;\r
317                 }\r
318 \r
319                 lbl_vEnc.Text = parsed.VideoEncoder;\r
320 \r
321                 // Display The Audio Track Information\r
322                 string audio = string.Empty;\r
323                 foreach (AudioTrack track in parsed.AudioInformation)\r
324                 {\r
325                     if (audio != string.Empty)\r
326                         audio += ", " + track.Encoder;\r
327                     else\r
328                         audio = track.Encoder;\r
329                 }\r
330                 lbl_aEnc.Text = audio;\r
331             }\r
332             catch (Exception)\r
333             {\r
334                 // Do Nothing\r
335             }\r
336         }\r
337 \r
338         /// <summary>\r
339         /// Delete the currently selected items on the queue\r
340         /// </summary>\r
341         private void DeleteSelectedItems()\r
342         {\r
343             // If there are selected items\r
344             if (list_queue.SelectedIndices.Count > 0)\r
345             {\r
346                 // Save the selected indices to select them after the move\r
347                 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);\r
348                 foreach (int selectedIndex in list_queue.SelectedIndices)\r
349                     selectedIndices.Add(selectedIndex);\r
350 \r
351                 int firstSelectedIndex = selectedIndices[0];\r
352 \r
353                 // Reverse the list to delete the items from last to first (preserves indices)\r
354                 selectedIndices.Reverse();\r
355 \r
356                 // Remove each selected item\r
357                 foreach (int selectedIndex in selectedIndices)\r
358                     queue.Remove(selectedIndex);\r
359 \r
360                 UpdateUiElements();\r
361 \r
362                 // Select the item where the first deleted item was previously\r
363                 if (firstSelectedIndex < list_queue.Items.Count)\r
364                     list_queue.Items[firstSelectedIndex].Selected = true;\r
365             }\r
366 \r
367             list_queue.Select(); // Activate the control to show the selected items\r
368         }\r
369 \r
370         // Queue Management\r
371         /// <summary>\r
372         /// Handle the Move Up Menu Item\r
373         /// </summary>\r
374         /// <param name="sender">\r
375         /// The sender.\r
376         /// </param>\r
377         /// <param name="e">\r
378         /// The e.\r
379         /// </param>\r
380         private void MnuUpClick(object sender, EventArgs e)\r
381         {\r
382             MoveUp();\r
383         }\r
384 \r
385         /// <summary>\r
386         /// Handle the Move down Menu Item\r
387         /// </summary>\r
388         /// <param name="sender">\r
389         /// The sender.\r
390         /// </param>\r
391         /// <param name="e">\r
392         /// The e.\r
393         /// </param>\r
394         private void MnuDownClick(object sender, EventArgs e)\r
395         {\r
396             MoveDown();\r
397         }\r
398 \r
399         /// <summary>\r
400         /// Edit a job\r
401         /// </summary>\r
402         /// <param name="sender">\r
403         /// The sender.\r
404         /// </param>\r
405         /// <param name="e">\r
406         /// The e.\r
407         /// </param>\r
408         private void MnuEditClick(object sender, EventArgs e)\r
409         {\r
410             if (list_queue.SelectedIndices != null)\r
411             {\r
412                 lock (queue)\r
413                 {\r
414                     lock (list_queue)\r
415                     {\r
416                         int index = list_queue.SelectedIndices[0];\r
417                         mainWindow.RecievingJob(queue.GetJob(index));\r
418                         queue.Remove(index);\r
419                         RedrawQueue();\r
420                     }\r
421                 }\r
422             }\r
423         }\r
424 \r
425         /// <summary>\r
426         /// Handle the delete Menu Item\r
427         /// </summary>\r
428         /// <param name="sender">\r
429         /// The sender.\r
430         /// </param>\r
431         /// <param name="e">\r
432         /// The e.\r
433         /// </param>\r
434         private void MnuDeleteClick(object sender, EventArgs e)\r
435         {\r
436             DeleteSelectedItems();\r
437         }\r
438 \r
439         /// <summary>\r
440         /// Handle the Button Up Click\r
441         /// </summary>\r
442         /// <param name="sender">\r
443         /// The sender.\r
444         /// </param>\r
445         /// <param name="e">\r
446         /// The e.\r
447         /// </param>\r
448         private void BtnUpClick(object sender, EventArgs e)\r
449         {\r
450             MoveUp();\r
451         }\r
452 \r
453         /// <summary>\r
454         /// Handle the button down click\r
455         /// </summary>\r
456         /// <param name="sender">\r
457         /// The sender.\r
458         /// </param>\r
459         /// <param name="e">\r
460         /// The e.\r
461         /// </param>\r
462         private void BtnDownClick(object sender, EventArgs e)\r
463         {\r
464             MoveDown();\r
465         }\r
466 \r
467         /// <summary>\r
468         /// Handle the delete button click\r
469         /// </summary>\r
470         /// <param name="sender">\r
471         /// The sender.\r
472         /// </param>\r
473         /// <param name="e">\r
474         /// The e.\r
475         /// </param>\r
476         private void BtnDeleteClick(object sender, EventArgs e)\r
477         {\r
478             DeleteSelectedItems();\r
479         }\r
480 \r
481         /// <summary>\r
482         /// Handle the delete keyboard press\r
483         /// </summary>\r
484         /// <param name="sender">\r
485         /// The sender.\r
486         /// </param>\r
487         /// <param name="e">\r
488         /// The e.\r
489         /// </param>\r
490         private void ListQueueDeleteKey(object sender, KeyEventArgs e)\r
491         {\r
492             if (e.KeyCode == Keys.Delete)\r
493                 DeleteSelectedItems();\r
494         }\r
495 \r
496         /// <summary>\r
497         /// Move items up in the queue\r
498         /// </summary>\r
499         private void MoveUp()\r
500         {\r
501             // If there are selected items and the first item is not selected\r
502             if (list_queue.SelectedIndices.Count > 0 && !list_queue.SelectedIndices.Contains(0))\r
503             {\r
504                 // Copy the selected indices to preserve them during the movement\r
505                 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);\r
506                 foreach (int selectedIndex in list_queue.SelectedIndices)\r
507                     selectedIndices.Add(selectedIndex);\r
508 \r
509                 // Move up each selected item\r
510                 foreach (int selectedIndex in selectedIndices)\r
511                     queue.MoveUp(selectedIndex);\r
512 \r
513                 UpdateUiElements();\r
514 \r
515                 // Keep the selected item(s) selected, now moved up one index\r
516                 foreach (int selectedIndex in selectedIndices)\r
517                     if (selectedIndex - 1 > -1) // Defensive programming: ensure index is good\r
518                         list_queue.Items[selectedIndex - 1].Selected = true;\r
519             }\r
520 \r
521             list_queue.Select(); // Activate the control to show the selected items\r
522         }\r
523 \r
524         /// <summary>\r
525         /// Move items down in the queue\r
526         /// </summary>\r
527         private void MoveDown()\r
528         {\r
529             // If there are selected items and the last item is not selected\r
530             if (list_queue.SelectedIndices.Count > 0 &&\r
531                 !list_queue.SelectedIndices.Contains(list_queue.Items[list_queue.Items.Count - 1].Index))\r
532             {\r
533                 // Copy the selected indices to preserve them during the movement\r
534                 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);\r
535                 foreach (int selectedIndex in list_queue.SelectedIndices)\r
536                     selectedIndices.Add(selectedIndex);\r
537 \r
538                 // Reverse the indices to move the items down from last to first (preserves indices)\r
539                 selectedIndices.Reverse();\r
540 \r
541                 // Move down each selected item\r
542                 foreach (int selectedIndex in selectedIndices)\r
543                     queue.MoveDown(selectedIndex);\r
544 \r
545                 UpdateUiElements();\r
546 \r
547                 // Keep the selected item(s) selected, now moved down one index\r
548                 foreach (int selectedIndex in selectedIndices)\r
549                     if (selectedIndex + 1 < list_queue.Items.Count) // Defensive programming: ensure index is good\r
550                         list_queue.Items[selectedIndex + 1].Selected = true;\r
551             }\r
552 \r
553             list_queue.Select(); // Activate the control to show the selected items\r
554         }\r
555 \r
556         // Queue Import/Export Features\r
557 \r
558         /// <summary>\r
559         /// Create a batch script\r
560         /// </summary>\r
561         /// <param name="sender">\r
562         /// The sender.\r
563         /// </param>\r
564         /// <param name="e">\r
565         /// The e.\r
566         /// </param>\r
567         private void MnuBatchClick(object sender, EventArgs e)\r
568         {\r
569             SaveFile.FileName = string.Empty;\r
570             SaveFile.Filter = "Batch|.bat";\r
571             SaveFile.ShowDialog();\r
572             if (SaveFile.FileName != String.Empty)\r
573                 queue.WriteBatchScriptToFile(SaveFile.FileName);\r
574         }\r
575 \r
576         /// <summary>\r
577         /// Export Queue\r
578         /// </summary>\r
579         /// <param name="sender">\r
580         /// The sender.\r
581         /// </param>\r
582         /// <param name="e">\r
583         /// The e.\r
584         /// </param>\r
585         private void MnuExportClick(object sender, EventArgs e)\r
586         {\r
587             SaveFile.FileName = string.Empty;\r
588             SaveFile.Filter = "HandBrake Queue|*.queue";\r
589             SaveFile.ShowDialog();\r
590             if (SaveFile.FileName != String.Empty)\r
591                 queue.WriteQueueStateToFile(SaveFile.FileName);\r
592         }\r
593 \r
594         /// <summary>\r
595         /// Import Queue\r
596         /// </summary>\r
597         /// <param name="sender">\r
598         /// The sender.\r
599         /// </param>\r
600         /// <param name="e">\r
601         /// The e.\r
602         /// </param>\r
603         private void MnuImportClick(object sender, EventArgs e)\r
604         {\r
605             OpenFile.FileName = string.Empty;\r
606             OpenFile.ShowDialog();\r
607             if (OpenFile.FileName != String.Empty)\r
608                 queue.LoadQueueFromFile(OpenFile.FileName);\r
609             UpdateUiElements();\r
610         }\r
611 \r
612         /// <summary>\r
613         /// Readd current job to queue\r
614         /// </summary>\r
615         /// <param name="sender">\r
616         /// The sender.\r
617         /// </param>\r
618         /// <param name="e">\r
619         /// The e.\r
620         /// </param>\r
621         private void MnuReaddClick(object sender, EventArgs e)\r
622         {\r
623             if (!queue.LastEncode.IsEmpty)\r
624             {\r
625                 queue.Add(\r
626                     queue.LastEncode.Query, \r
627                     queue.LastEncode.Title, \r
628                     queue.LastEncode.Source,\r
629                     queue.LastEncode.Destination,\r
630                     queue.LastEncode.CustomQuery);\r
631                 UpdateUiElements();\r
632             }\r
633         }\r
634 \r
635         /// <summary>\r
636         /// Hide's the window when the user tries to "x" out of the window instead of closing it.\r
637         /// </summary>\r
638         /// <param name="e">\r
639         /// The e.\r
640         /// </param>\r
641         protected override void OnClosing(CancelEventArgs e)\r
642         {\r
643             e.Cancel = true;\r
644             this.Hide();\r
645             base.OnClosing(e);\r
646         }\r
647     }\r
648 }