OSDN Git Service

Change minimum vobsub time from 3sec to 2sec, been meaning to do this for a while.
[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 \r
12 namespace Handbrake\r
13 {\r
14     public partial class frmQueue : Form\r
15     {\r
16         private delegate void UpdateHandler();\r
17         Queue.QueueHandler queue;\r
18 \r
19         public frmQueue(Queue.QueueHandler q)\r
20         {\r
21             InitializeComponent();\r
22 \r
23             this.queue = q;\r
24             queue.OnEncodeStart += new EventHandler(queue_OnEncodeStart);\r
25             queue.OnQueueFinished += new EventHandler(queue_OnQueueFinished);\r
26             queue.OnPaused += new EventHandler(queue_OnPaused);\r
27         }\r
28         void queue_OnPaused(object sender, EventArgs e)\r
29         {\r
30             setUIEncodeFinished();\r
31             updateUIElements();\r
32         }\r
33         void queue_OnQueueFinished(object sender, EventArgs e)\r
34         {\r
35             setUIEncodeFinished();\r
36             resetQueue(); // Reset the Queue Window\r
37         }\r
38         void queue_OnEncodeStart(object sender, EventArgs e)\r
39         {\r
40             setUIEncodeStarted(); // make sure the UI is set correctly\r
41             setCurrentEncodeInformation();\r
42             updateUIElements(); // Redraw the Queue, a new encode has started.\r
43         }\r
44 \r
45         /// <summary>\r
46         /// Initializes the Queue list with the Arraylist from the Queue class\r
47         /// </summary>\r
48         public void setQueue()\r
49         {\r
50             updateUIElements();\r
51         }\r
52 \r
53         /// <summary>\r
54         /// Initializes the Queue list, then shows and activates the window\r
55         /// </summary>\r
56         public new void Show()\r
57         {\r
58             Show(true);\r
59         }\r
60 \r
61         /// <summary>\r
62         /// Initializes the Queue list only if doSetQueue is true, then shows and activates the window\r
63         /// </summary>\r
64         /// <param name="doSetQueue">Indicates whether to call setQueue() before showing the window</param>\r
65         public void Show(bool doSetQueue)\r
66         {\r
67             if (doSetQueue) setQueue();\r
68             base.Show();\r
69             Activate();\r
70         }\r
71 \r
72         // Start and Stop Controls\r
73         private void btn_encode_Click(object sender, EventArgs e)\r
74         {\r
75             if (queue.isPaused)\r
76             {\r
77                 setUIEncodeStarted();\r
78                 MessageBox.Show("Encoding restarted", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
79             }\r
80 \r
81             if (!queue.isEncodeStarted)\r
82                 queue.startEncode();\r
83 \r
84         }\r
85         private void btn_pause_Click(object sender, EventArgs e)\r
86         {\r
87             queue.pauseEncode();\r
88             setUIEncodeFinished();\r
89             resetQueue();\r
90             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
91         }\r
92 \r
93         // Window Display Management\r
94         private void setUIEncodeStarted()\r
95         {\r
96             if (InvokeRequired)\r
97             {\r
98                 BeginInvoke(new UpdateHandler(setUIEncodeStarted));\r
99                 return;\r
100             }\r
101             btn_encode.Enabled = false;\r
102             btn_pause.Visible = true;\r
103         }\r
104         private void setUIEncodeFinished()\r
105         {\r
106             if (InvokeRequired)\r
107             {\r
108                 BeginInvoke(new UpdateHandler(setUIEncodeFinished));\r
109                 return;\r
110             }\r
111             btn_pause.Visible = false;\r
112             btn_encode.Enabled = true;\r
113         }\r
114         private void resetQueue()\r
115         {\r
116             if (InvokeRequired)\r
117             {\r
118                 BeginInvoke(new UpdateHandler(resetQueue));\r
119                 return;\r
120             }\r
121             btn_pause.Visible = false;\r
122             btn_encode.Enabled = true;\r
123 \r
124             lbl_source.Text = "-";\r
125             lbl_dest.Text = "-";\r
126             lbl_vEnc.Text = "-";\r
127             lbl_aEnc.Text = "-";\r
128             lbl_title.Text = "-";\r
129             lbl_chapt.Text = "-";\r
130 \r
131             lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";\r
132         }\r
133         private void redrawQueue()\r
134         {\r
135             if (InvokeRequired)\r
136             {\r
137                 BeginInvoke(new UpdateHandler(redrawQueue));\r
138                 return;\r
139             }\r
140 \r
141             list_queue.Items.Clear();\r
142             List<Queue.QueueItem> theQueue = queue.getQueue();\r
143             foreach (Queue.QueueItem queue_item in theQueue)\r
144             {\r
145                 string q_item = queue_item.Query;\r
146                 Functions.QueryParser parsed = Functions.QueryParser.Parse(q_item);\r
147 \r
148                 // Get the DVD Title\r
149                 string title = parsed.DVDTitle == 0 ? "Auto" : parsed.DVDTitle.ToString();\r
150 \r
151                 // Get the DVD Chapters\r
152                 string chapters;\r
153                 if (parsed.DVDChapterStart == 0)\r
154                     chapters = "Auto";\r
155                 else\r
156                 {\r
157                     chapters = parsed.DVDChapterStart.ToString();\r
158                     if (parsed.DVDChapterFinish != 0)\r
159                         chapters = chapters + " - " + parsed.DVDChapterFinish;\r
160                 }\r
161 \r
162                 ListViewItem item = new ListViewItem();\r
163                 item.Text = title; // Title\r
164                 item.SubItems.Add(chapters); // Chapters\r
165                 item.SubItems.Add(queue_item.Source); // Source\r
166                 item.SubItems.Add(queue_item.Destination); // Destination\r
167                 item.SubItems.Add(parsed.VideoEncoder); // Video\r
168 \r
169                 // Display The Audio Track Information\r
170                 string audio = string.Empty;\r
171                 foreach (Functions.AudioTrack track in parsed.AudioInformation)\r
172                 {\r
173                     if (audio != "")\r
174                         audio += ", " + track.Encoder;\r
175                     else\r
176                         audio = track.Encoder;\r
177                 }\r
178                 item.SubItems.Add(audio); // Audio\r
179 \r
180                 list_queue.Items.Add(item);\r
181             }\r
182         }\r
183         private void updateUIElements()\r
184         {\r
185             if (InvokeRequired)\r
186             {\r
187                 BeginInvoke(new UpdateHandler(updateUIElements));\r
188                 return;\r
189             }\r
190 \r
191             redrawQueue();\r
192             lbl_encodesPending.Text = list_queue.Items.Count + " encode(s) pending";\r
193         }\r
194         private void setCurrentEncodeInformation()\r
195         {\r
196             try\r
197             {\r
198                 if (InvokeRequired)\r
199                 {\r
200                     BeginInvoke(new UpdateHandler(setCurrentEncodeInformation));\r
201                 }\r
202 \r
203                 // found query is a global varible\r
204                 Functions.QueryParser parsed = Functions.QueryParser.Parse(queue.lastQueueItem.Query);\r
205                 lbl_source.Text = queue.lastQueueItem.Source;\r
206                 lbl_dest.Text = queue.lastQueueItem.Destination;\r
207 \r
208                 lbl_title.Text = parsed.DVDTitle == 0 ? "Auto" : parsed.DVDTitle.ToString();\r
209 \r
210                 if (Equals(parsed.DVDChapterStart, 0))\r
211                     lbl_chapt.Text = "Auto";\r
212                 else\r
213                 {\r
214                     string chapters = parsed.DVDChapterStart.ToString();\r
215                     if (parsed.DVDChapterFinish != 0)\r
216                         chapters = chapters + " - " + parsed.DVDChapterFinish;\r
217                     lbl_chapt.Text = chapters;\r
218                 }\r
219 \r
220                 lbl_vEnc.Text = parsed.VideoEncoder;\r
221 \r
222                 // Display The Audio Track Information\r
223                 string audio = string.Empty;\r
224                 foreach (Functions.AudioTrack track in parsed.AudioInformation)\r
225                 {\r
226                     if (audio != "")\r
227                         audio += ", " + track.Encoder;\r
228                     else\r
229                         audio = track.Encoder;\r
230                 }\r
231                 lbl_aEnc.Text = audio;\r
232             }\r
233             catch (Exception)\r
234             {\r
235                 // Do Nothing\r
236             }\r
237         }\r
238         private void deleteSelectedItems()\r
239         {\r
240             // If there are selected items\r
241             if (list_queue.SelectedIndices.Count > 0)\r
242             {\r
243                 // Save the selected indices to select them after the move\r
244                 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);\r
245                 foreach (int selectedIndex in list_queue.SelectedIndices)\r
246                     selectedIndices.Add(selectedIndex);\r
247 \r
248                 int firstSelectedIndex = selectedIndices[0];\r
249 \r
250                 // Reverse the list to delete the items from last to first (preserves indices)\r
251                 selectedIndices.Reverse();\r
252 \r
253                 // Remove each selected item\r
254                 foreach (int selectedIndex in selectedIndices)\r
255                     queue.remove(selectedIndex);\r
256 \r
257                 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file\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                 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file\r
313                 updateUIElements();\r
314 \r
315                 // Keep the selected item(s) selected, now moved up one index\r
316                 foreach (int selectedIndex in selectedIndices)\r
317                     if (selectedIndex - 1 > -1) // Defensive programming: ensure index is good\r
318                         list_queue.Items[selectedIndex - 1].Selected = true;\r
319             }\r
320 \r
321             list_queue.Select(); // Activate the control to show the selected items\r
322         }\r
323         private void moveDown()\r
324         {\r
325             // If there are selected items and the last item is not selected\r
326             if (list_queue.SelectedIndices.Count > 0 &&\r
327                 !list_queue.SelectedIndices.Contains(list_queue.Items[list_queue.Items.Count - 1].Index))\r
328             {\r
329                 // Copy the selected indices to preserve them during the movement\r
330                 List<int> selectedIndices = new List<int>(list_queue.SelectedIndices.Count);\r
331                 foreach (int selectedIndex in list_queue.SelectedIndices)\r
332                     selectedIndices.Add(selectedIndex);\r
333 \r
334                 // Reverse the indices to move the items down from last to first (preserves indices)\r
335                 selectedIndices.Reverse();\r
336 \r
337                 // Move down each selected item\r
338                 foreach (int selectedIndex in selectedIndices)\r
339                     queue.moveDown(selectedIndex);\r
340 \r
341                 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file\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.writeBatchScript(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.write2disk(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.recoverQueue(OpenFile.FileName);\r
376             updateUIElements();\r
377         }\r
378         private void mnu_readd_Click(object sender, EventArgs e)\r
379         {\r
380             if (queue.lastQueueItem != null)\r
381             {\r
382                 queue.add(queue.lastQueueItem.Query, queue.lastQueueItem.Source, queue.lastQueueItem.Destination);\r
383                 queue.write2disk("hb_queue_recovery.xml"); // Update the queue recovery file\r
384                 updateUIElements();\r
385             }\r
386         }\r
387 \r
388         // Hide's the window when the user tries to "x" out of the window instead of closing it.\r
389         protected override void OnClosing(CancelEventArgs e)\r
390         {\r
391             e.Cancel = true;\r
392             this.Hide();\r
393             base.OnClosing(e);\r
394         }\r
395 \r
396     }\r
397 }\r