OSDN Git Service

ae53558257b7725bdd6c13692d9fe62b3d174f01
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmQueue.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.ComponentModel;\r
4 using System.Data;\r
5 using System.Drawing;\r
6 using System.Text;\r
7 using System.Windows.Forms;\r
8 using System.Threading;\r
9 using System.Diagnostics;\r
10 using System.Runtime.InteropServices;\r
11 \r
12 namespace Handbrake\r
13 {\r
14     public partial class frmQueue : Form\r
15     {\r
16         private delegate void ProgressUpdateHandler();\r
17         private delegate void setEncoding();\r
18 \r
19         public frmQueue()\r
20         {\r
21             InitializeComponent();\r
22         }\r
23 \r
24         #region Queue Handling\r
25         Boolean cancel = false;\r
26         private void btn_q_encoder_Click(object sender, EventArgs e)\r
27         {\r
28             // Reset some values\r
29             \r
30             lbl_status.Visible = false;\r
31             cancel = false;\r
32 \r
33             // Start the encode\r
34             try\r
35             {\r
36                 if (list_queue.Items.Count != 0)\r
37                 {\r
38                     // Setup or reset some values\r
39                     btn_cancel.Visible = true;\r
40                     progressBar.Value = 0;\r
41                     lbl_progressValue.Text = "0 %";\r
42                     progressBar.Step = 100 / list_queue.Items.Count;\r
43                     progressBar.Update();\r
44                     //ThreadPool.QueueUserWorkItem(startProc);\r
45                     // Testing a new way of launching a thread. Hopefully will fix a random freeze up of the main thread.\r
46                     Thread theQ = new Thread(startProc);\r
47                     theQ.Start();\r
48                  }\r
49             }\r
50             catch (Exception exc)\r
51             {\r
52                 MessageBox.Show(exc.ToString());\r
53             }\r
54              \r
55         }\r
56         private void btn_cancel_Click(object sender, EventArgs e)\r
57         {\r
58             cancel = true;\r
59             btn_cancel.Visible = false;\r
60             MessageBox.Show("No further items on the queue will start. The current encode process will continue until it is finished. \nClick 'Encode Video' when you wish to continue encoding the queue.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
61         }\r
62 \r
63         [DllImport("user32.dll")]\r
64         public static extern void LockWorkStation();\r
65         [DllImport("user32.dll")]\r
66         public static extern int ExitWindowsEx(int uFlags, int dwReason);\r
67 \r
68         private void startProc(object state)\r
69         {\r
70             try\r
71             {\r
72                 while (list_queue.Items.Count != 0)\r
73                 {\r
74                     string query = list_queue.Items[0].ToString();\r
75                     setEncValue();\r
76                     updateUIElements();\r
77                    \r
78                     Functions.CLI process = new Functions.CLI();\r
79                     Process hbProc = process.runCli(this, query, false, false, false, false);\r
80 \r
81                     hbProc.WaitForExit();\r
82                     hbProc.Close();\r
83                     hbProc.Dispose();\r
84 \r
85                     query = "";\r
86 \r
87                     if (cancel == true)\r
88                     {\r
89                         break;\r
90                     }\r
91                     \r
92                 }\r
93 \r
94                 resetQueue();\r
95                 \r
96                 // Do something whent he encode ends.\r
97                 switch (Properties.Settings.Default.CompletionOption)\r
98                 {\r
99                     case "Shutdown":\r
100                         System.Diagnostics.Process.Start("Shutdown", "-s -t 60");\r
101                         break;\r
102                     case "Log Off":\r
103                         ExitWindowsEx(0, 0);\r
104                         break;\r
105                     case "Suspend":\r
106                         Application.SetSuspendState(PowerState.Suspend, true, true);\r
107                         break;\r
108                     case "Hibernate":\r
109                         Application.SetSuspendState(PowerState.Hibernate, true, true);\r
110                         break;\r
111                     case "Lock System":\r
112                         LockWorkStation();\r
113                         break;\r
114                     case "Quit HandBrake":\r
115                         Application.Exit();\r
116                         break;\r
117                     default:\r
118                         break;\r
119                 }\r
120             }\r
121             catch (Exception exc)\r
122             {\r
123                 MessageBox.Show(exc.ToString());\r
124             }\r
125         }\r
126 \r
127         private void updateUIElements()\r
128         {\r
129             try\r
130             {\r
131                 if (this.InvokeRequired)\r
132                 {\r
133                     this.BeginInvoke(new ProgressUpdateHandler(updateUIElements));\r
134                     return;\r
135                 }\r
136                 this.list_queue.Items.RemoveAt(0);\r
137 \r
138                 progressBar.PerformStep();\r
139                 lbl_progressValue.Text = string.Format("{0} %", progressBar.Value);\r
140                 progressBar.Update();\r
141             }\r
142             catch (Exception exc)\r
143             {\r
144                 MessageBox.Show(exc.ToString());\r
145             }\r
146         }\r
147 \r
148         private void resetQueue()\r
149         {\r
150             try\r
151             {\r
152                 if (this.InvokeRequired)\r
153                 {\r
154                     this.BeginInvoke(new ProgressUpdateHandler(resetQueue));\r
155                     return;\r
156                 }\r
157 \r
158                 if (cancel == true)\r
159                 {\r
160                     lbl_status.Visible = true;\r
161                     lbl_status.Text = "Encode Queue Cancelled!";\r
162                     text_edit.Text = "";\r
163                 }\r
164                 else\r
165                 {\r
166                     lbl_status.Visible = true;\r
167                     lbl_status.Text = "Encode Queue Completed!";\r
168                     text_edit.Text = "";\r
169                 }\r
170                 btn_cancel.Visible = false;\r
171 \r
172                 lbl_progressValue.Text = "0 %";\r
173                 progressBar.Value = 0;\r
174                 progressBar.Update();\r
175 \r
176                 lbl_source.Text = "-";\r
177                 lbl_dest.Text = "-";\r
178                 lbl_vEnc.Text = "-";\r
179                 lbl_aEnc.Text = "-";\r
180                 lbl_title.Text = "-";\r
181                 lbl_chapt.Text = "-";\r
182             }\r
183             catch (Exception exc)\r
184             {\r
185                 MessageBox.Show(exc.ToString());\r
186             }\r
187         }\r
188         \r
189         private void setEncValue()\r
190         {\r
191             try\r
192             {\r
193                 if (this.InvokeRequired)\r
194                 {\r
195                     this.BeginInvoke(new setEncoding(setEncValue));\r
196                 }\r
197 \r
198                 string query = query = list_queue.Items[0].ToString();\r
199 \r
200                 // found query is a global varible\r
201                 Functions.QueryParser parsed = Functions.QueryParser.Parse(query);\r
202                 lbl_source.Text = parsed.Source;\r
203                 lbl_dest.Text = parsed.Destination;\r
204 \r
205 \r
206                 if (parsed.DVDTitle == 0)\r
207                     lbl_title.Text = "Auto";\r
208                 else\r
209                     lbl_title.Text = parsed.DVDTitle.ToString();\r
210 \r
211                 string chapters = "";\r
212                 if (parsed.DVDChapterStart == 0)\r
213                 {\r
214                     lbl_chapt.Text = "Auto";\r
215                 }\r
216                 else\r
217                 {\r
218                     chapters = parsed.DVDChapterStart.ToString();\r
219                     if (parsed.DVDChapterFinish != 0)\r
220                         chapters = chapters + " - " + parsed.DVDChapterFinish;\r
221                     lbl_chapt.Text = chapters;\r
222                 }\r
223 \r
224                 lbl_vEnc.Text = parsed.VideoEncoder;\r
225                 lbl_aEnc.Text = parsed.AudioEncoder;\r
226             }\r
227             catch (Exception)\r
228             {\r
229                 // Do Nothing\r
230             }\r
231         }\r
232 \r
233         #endregion\r
234 \r
235         #region Queue Management\r
236 \r
237         private void btn_up_Click(object sender, EventArgs e)\r
238         {\r
239             int count = list_queue.Items.Count;\r
240             int itemToMove = list_queue.SelectedIndex;\r
241             int previousItemint = 0;\r
242             String previousItem = "";\r
243 \r
244             if (itemToMove > 0)\r
245             {\r
246                 previousItemint = itemToMove - 1;\r
247                 previousItem = list_queue.Items[previousItemint].ToString();\r
248                 list_queue.Items[previousItemint] = list_queue.Items[itemToMove];\r
249                 list_queue.Items[itemToMove] = previousItem;\r
250                 list_queue.SelectedIndex = list_queue.SelectedIndex - 1;\r
251             }\r
252         }\r
253 \r
254         private void btn_down_Click(object sender, EventArgs e)\r
255         {\r
256             int count = list_queue.Items.Count;\r
257             int itemToMove = list_queue.SelectedIndex;\r
258             int itemAfterInt = 0;\r
259             String itemAfter = "";\r
260 \r
261             if (itemToMove < (count - 1))\r
262             {\r
263                 itemAfterInt = itemToMove + 1;\r
264                 itemAfter = list_queue.Items[itemAfterInt].ToString();\r
265                 list_queue.Items[itemAfterInt] = list_queue.Items[itemToMove];\r
266                 list_queue.Items[itemToMove] = itemAfter;\r
267                 list_queue.SelectedIndex = list_queue.SelectedIndex + 1;\r
268             }\r
269         }\r
270 \r
271         private void btn_delete_Click(object sender, EventArgs e)\r
272         {\r
273             list_queue.Items.Remove(list_queue.SelectedItem);\r
274         }\r
275 \r
276         #endregion\r
277 \r
278         #region Queue Item Modification\r
279 \r
280         int listCount = 0;\r
281 \r
282         private void btn_updateQuery_Click(object sender, EventArgs e)\r
283         {\r
284             if (text_edit.Text != "")\r
285             {\r
286                 if (list_queue.Items.Count != listCount)\r
287                 {\r
288                     MessageBox.Show("Unable to modify the selected item. The number of items on the list has changed.  \nPlease avoid modifying an item when a new encode is about to start!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
289                 }\r
290                 else\r
291                 {\r
292                     if (list_queue.SelectedItem != null)\r
293                     {\r
294                     }\r
295           \r
296                 }\r
297             }\r
298         }\r
299 \r
300         private void list_queue_SelectedIndexChanged(object sender, EventArgs e)\r
301         {\r
302             if (list_queue.SelectedItem != null)\r
303                 text_edit.Text = list_queue.SelectedItem.ToString();\r
304 \r
305             listCount = list_queue.Items.Count;\r
306         }\r
307 \r
308         #endregion\r
309 \r
310         private void btn_Close_Click(object sender, EventArgs e)\r
311         {\r
312             this.Hide();\r
313         }\r
314 \r
315         protected override void OnClosing(CancelEventArgs e)\r
316         {\r
317             e.Cancel = true;\r
318             this.Hide();\r
319             base.OnClosing(e);\r
320         }\r
321 \r
322     }\r
323 }