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