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