OSDN Git Service

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