OSDN Git Service

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