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         /// Handle the delete Menu Item\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 MnuDeleteClick(object sender, EventArgs e)\r
409         {\r
410             DeleteSelectedItems();\r
411         }\r
412 \r
413         /// <summary>\r
414         /// Handle the Button Up Click\r
415         /// </summary>\r
416         /// <param name="sender">\r
417         /// The sender.\r
418         /// </param>\r
419         /// <param name="e">\r
420         /// The e.\r
421         /// </param>\r
422         private void BtnUpClick(object sender, EventArgs e)\r
423         {\r
424             MoveUp();\r
425         }\r
426 \r
427         /// <summary>\r
428         /// Handle the button down click\r
429         /// </summary>\r
430         /// <param name="sender">\r
431         /// The sender.\r
432         /// </param>\r
433         /// <param name="e">\r
434         /// The e.\r
435         /// </param>\r
436         private void BtnDownClick(object sender, EventArgs e)\r
437         {\r
438             MoveDown();\r
439         }\r
440 \r
441         /// <summary>\r
442         /// Handle the delete button click\r
443         /// </summary>\r
444         /// <param name="sender">\r
445         /// The sender.\r
446         /// </param>\r
447         /// <param name="e">\r
448         /// The e.\r
449         /// </param>\r
450         private void BtnDeleteClick(object sender, EventArgs e)\r
451         {\r
452             DeleteSelectedItems();\r
453         }\r
454 \r
455         /// <summary>\r
456         /// Handle the delete keyboard press\r
457         /// </summary>\r
458         /// <param name="sender">\r
459         /// The sender.\r
460         /// </param>\r
461         /// <param name="e">\r
462         /// The e.\r
463         /// </param>\r
464         private void ListQueueDeleteKey(object sender, KeyEventArgs e)\r
465         {\r
466             if (e.KeyCode == Keys.Delete)\r
467                 DeleteSelectedItems();\r
468         }\r
469 \r
470         /// <summary>\r
471         /// Move items up in the queue\r
472         /// </summary>\r
473         private void MoveUp()\r
474         {\r
475             // If there are selected items and the first item is not selected\r
476             if (list_queue.SelectedIndices.Count > 0 && !list_queue.SelectedIndices.Contains(0))\r
477             {\r
478                 // Copy the selected indices to preserve them during the movement\r
479                 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);\r
480                 foreach (int selectedIndex in list_queue.SelectedIndices)\r
481                     selectedIndices.Add(selectedIndex);\r
482 \r
483                 // Move up each selected item\r
484                 foreach (int selectedIndex in selectedIndices)\r
485                     queue.MoveUp(selectedIndex);\r
486 \r
487                 UpdateUiElements();\r
488 \r
489                 // Keep the selected item(s) selected, now moved up one index\r
490                 foreach (int selectedIndex in selectedIndices)\r
491                     if (selectedIndex - 1 > -1) // Defensive programming: ensure index is good\r
492                         list_queue.Items[selectedIndex - 1].Selected = true;\r
493             }\r
494 \r
495             list_queue.Select(); // Activate the control to show the selected items\r
496         }\r
497 \r
498         /// <summary>\r
499         /// Move items down in the queue\r
500         /// </summary>\r
501         private void MoveDown()\r
502         {\r
503             // If there are selected items and the last item is not selected\r
504             if (list_queue.SelectedIndices.Count > 0 &&\r
505                 !list_queue.SelectedIndices.Contains(list_queue.Items[list_queue.Items.Count - 1].Index))\r
506             {\r
507                 // Copy the selected indices to preserve them during the movement\r
508                 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);\r
509                 foreach (int selectedIndex in list_queue.SelectedIndices)\r
510                     selectedIndices.Add(selectedIndex);\r
511 \r
512                 // Reverse the indices to move the items down from last to first (preserves indices)\r
513                 selectedIndices.Reverse();\r
514 \r
515                 // Move down each selected item\r
516                 foreach (int selectedIndex in selectedIndices)\r
517                     queue.MoveDown(selectedIndex);\r
518 \r
519                 UpdateUiElements();\r
520 \r
521                 // Keep the selected item(s) selected, now moved down one index\r
522                 foreach (int selectedIndex in selectedIndices)\r
523                     if (selectedIndex + 1 < list_queue.Items.Count) // Defensive programming: ensure index is good\r
524                         list_queue.Items[selectedIndex + 1].Selected = true;\r
525             }\r
526 \r
527             list_queue.Select(); // Activate the control to show the selected items\r
528         }\r
529 \r
530         // Queue Import/Export Features\r
531 \r
532         /// <summary>\r
533         /// Create a batch script\r
534         /// </summary>\r
535         /// <param name="sender">\r
536         /// The sender.\r
537         /// </param>\r
538         /// <param name="e">\r
539         /// The e.\r
540         /// </param>\r
541         private void MnuBatchClick(object sender, EventArgs e)\r
542         {\r
543             SaveFile.FileName = string.Empty;\r
544             SaveFile.Filter = "Batch|.bat";\r
545             SaveFile.ShowDialog();\r
546             if (SaveFile.FileName != String.Empty)\r
547                 queue.WriteBatchScriptToFile(SaveFile.FileName);\r
548         }\r
549 \r
550         /// <summary>\r
551         /// Export Queue\r
552         /// </summary>\r
553         /// <param name="sender">\r
554         /// The sender.\r
555         /// </param>\r
556         /// <param name="e">\r
557         /// The e.\r
558         /// </param>\r
559         private void MnuExportClick(object sender, EventArgs e)\r
560         {\r
561             SaveFile.FileName = string.Empty;\r
562             SaveFile.Filter = "HandBrake Queue|*.queue";\r
563             SaveFile.ShowDialog();\r
564             if (SaveFile.FileName != String.Empty)\r
565                 queue.WriteQueueStateToFile(SaveFile.FileName);\r
566         }\r
567 \r
568         /// <summary>\r
569         /// Import Queue\r
570         /// </summary>\r
571         /// <param name="sender">\r
572         /// The sender.\r
573         /// </param>\r
574         /// <param name="e">\r
575         /// The e.\r
576         /// </param>\r
577         private void MnuImportClick(object sender, EventArgs e)\r
578         {\r
579             OpenFile.FileName = string.Empty;\r
580             OpenFile.ShowDialog();\r
581             if (OpenFile.FileName != String.Empty)\r
582                 queue.LoadQueueFromFile(OpenFile.FileName);\r
583             UpdateUiElements();\r
584         }\r
585 \r
586         /// <summary>\r
587         /// Readd current job to queue\r
588         /// </summary>\r
589         /// <param name="sender">\r
590         /// The sender.\r
591         /// </param>\r
592         /// <param name="e">\r
593         /// The e.\r
594         /// </param>\r
595         private void MnuReaddClick(object sender, EventArgs e)\r
596         {\r
597             if (!queue.LastEncode.IsEmpty)\r
598             {\r
599                 queue.Add(queue.LastEncode.Query, queue.LastEncode.Source, queue.LastEncode.Destination,\r
600                           queue.LastEncode.CustomQuery);\r
601                 UpdateUiElements();\r
602             }\r
603         }\r
604 \r
605         /// <summary>\r
606         /// Edit Job\r
607         /// </summary>\r
608         /// <param name="sender">\r
609         /// The sender.\r
610         /// </param>\r
611         /// <param name="e">\r
612         /// The e.\r
613         /// </param>\r
614         private void MnuReconfigureJobClick(object sender, EventArgs e)\r
615         {\r
616             if (list_queue.SelectedIndices != null)\r
617             {\r
618                 lock (queue)\r
619                 {\r
620                     lock (list_queue)\r
621                     {\r
622                         int index = list_queue.SelectedIndices[0];\r
623                         mainWindow.RecievingJob(queue.GetJob(index));\r
624                         queue.Remove(index);\r
625                         RedrawQueue();\r
626                     }\r
627                 }\r
628             }\r
629         }\r
630 \r
631         /// <summary>\r
632         /// Hide's the window when the user tries to "x" out of the window instead of closing it.\r
633         /// </summary>\r
634         /// <param name="e">\r
635         /// The e.\r
636         /// </param>\r
637         protected override void OnClosing(CancelEventArgs e)\r
638         {\r
639             e.Cancel = true;\r
640             this.Hide();\r
641             base.OnClosing(e);\r
642         }\r
643     }\r
644 }