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