OSDN Git Service

bec77f402cda2d97bcb5e0375e0335942ea93d00
[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 \r
18         public frmQueue()\r
19         {\r
20             InitializeComponent();\r
21         }\r
22 \r
23         #region Queue Handling\r
24         Boolean cancel = false;\r
25         private void btn_q_encoder_Click(object sender, EventArgs e)\r
26         {\r
27             // Reset some values\r
28             \r
29             lbl_status.Visible = false;\r
30             cancel = false;\r
31 \r
32             // Start the encode\r
33             try\r
34             {\r
35                 if (list_queue.Items.Count != 0)\r
36                 {\r
37                     // Setup or reset some values\r
38                     btn_cancel.Visible = true;\r
39                     progressBar.Value = 0;\r
40                     lbl_progressValue.Text = "0 %";\r
41                     progressBar.Step = 100 / list_queue.Items.Count;\r
42                     progressBar.Update();\r
43                     ThreadPool.QueueUserWorkItem(startProc);\r
44                 }\r
45             }\r
46             catch (Exception exc)\r
47             {\r
48                 MessageBox.Show(exc.ToString());\r
49             }\r
50              \r
51         }\r
52         private void btn_cancel_Click(object sender, EventArgs e)\r
53         {\r
54             cancel = true;\r
55             btn_cancel.Visible = false;\r
56             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
57         }\r
58 \r
59         [DllImport("user32.dll")]\r
60         public static extern void LockWorkStation();\r
61         [DllImport("user32.dll")]\r
62         public static extern int ExitWindowsEx(int uFlags, int dwReason);\r
63 \r
64         private void startProc(object state)\r
65         {\r
66             try\r
67             {\r
68                 while (list_queue.Items.Count != 0)\r
69                 {\r
70                     string query = list_queue.Items[0].ToString();\r
71                     \r
72                     updateUIElements();\r
73                         \r
74                     Functions.CLI process = new Functions.CLI();\r
75                     Process hbProc = process.runCli(this, query, false, false, false, false);\r
76 \r
77                     hbProc.WaitForExit();\r
78                     hbProc.Close();\r
79                     hbProc.Dispose();\r
80 \r
81                     query = "";\r
82 \r
83                     if (cancel == true)\r
84                     {\r
85                         break;\r
86                     }\r
87                     \r
88                 }\r
89 \r
90                 resetQueue();\r
91                 \r
92 \r
93                 // Do something whent he encode ends.\r
94                 switch (Properties.Settings.Default.CompletionOption)\r
95                 {\r
96                     case "Shutdown":\r
97                         System.Diagnostics.Process.Start("Shutdown", "-s -t 60");\r
98                         break;\r
99                     case "Log Off":\r
100                         ExitWindowsEx(0, 0);\r
101                         break;\r
102                     case "Suspend":\r
103                         Application.SetSuspendState(PowerState.Suspend, true, true);\r
104                         break;\r
105                     case "Hibernate":\r
106                         Application.SetSuspendState(PowerState.Hibernate, true, true);\r
107                         break;\r
108                     case "Lock System":\r
109                         LockWorkStation();\r
110                         break;\r
111                     case "Quit HandBrake":\r
112                         Application.Exit();\r
113                         break;\r
114                     default:\r
115                         break;\r
116                 }\r
117             }\r
118             catch (Exception exc)\r
119             {\r
120                 MessageBox.Show(exc.ToString());\r
121             }\r
122         }\r
123 \r
124         private void updateUIElements()\r
125         {\r
126             try\r
127             {\r
128                 if (this.InvokeRequired)\r
129                 {\r
130                     this.BeginInvoke(new ProgressUpdateHandler(updateUIElements));\r
131                     return;\r
132                 }\r
133                 this.list_queue.Items.RemoveAt(0);\r
134 \r
135                 progressBar.PerformStep();\r
136                 lbl_progressValue.Text = string.Format("{0} %", progressBar.Value);\r
137                 progressBar.Update();\r
138             }\r
139             catch (Exception exc)\r
140             {\r
141                 MessageBox.Show(exc.ToString());\r
142             }\r
143         }\r
144 \r
145         private void resetQueue()\r
146         {\r
147             try\r
148             {\r
149                 if (this.InvokeRequired)\r
150                 {\r
151                     this.BeginInvoke(new ProgressUpdateHandler(resetQueue));\r
152                     return;\r
153                 }\r
154 \r
155                 if (cancel == true)\r
156                 {\r
157                     lbl_status.Visible = true;\r
158                     lbl_status.Text = "Encode Queue Cancelled!";\r
159                 }\r
160                 else\r
161                 {\r
162                     lbl_status.Visible = true;\r
163                     lbl_status.Text = "Encode Queue Completed!";\r
164                 }\r
165                 btn_cancel.Visible = false;\r
166 \r
167                 lbl_progressValue.Text = "0 %";\r
168                 progressBar.Value = 0;\r
169                 progressBar.Update();\r
170             }\r
171             catch (Exception exc)\r
172             {\r
173                 MessageBox.Show(exc.ToString());\r
174             }\r
175         }\r
176 \r
177         #endregion\r
178 \r
179         #region Queue Management\r
180         private void btn_up_Click(object sender, EventArgs e)\r
181         {\r
182             int count = list_queue.Items.Count;\r
183             int itemToMove = list_queue.SelectedIndex;\r
184             int previousItemint = 0;\r
185             String previousItem = "";\r
186 \r
187             if (itemToMove > 0)\r
188             {\r
189                 previousItemint = itemToMove - 1;\r
190                 previousItem = list_queue.Items[previousItemint].ToString();\r
191                 list_queue.Items[previousItemint] = list_queue.Items[itemToMove];\r
192                 list_queue.Items[itemToMove] = previousItem;\r
193                 list_queue.SelectedIndex = list_queue.SelectedIndex - 1;\r
194             }\r
195         }\r
196 \r
197         private void btn_down_Click(object sender, EventArgs e)\r
198         {\r
199             int count = list_queue.Items.Count;\r
200             int itemToMove = list_queue.SelectedIndex;\r
201             int itemAfterInt = 0;\r
202             String itemAfter = "";\r
203 \r
204             if (itemToMove < (count - 1))\r
205             {\r
206                 itemAfterInt = itemToMove + 1;\r
207                 itemAfter = list_queue.Items[itemAfterInt].ToString();\r
208                 list_queue.Items[itemAfterInt] = list_queue.Items[itemToMove];\r
209                 list_queue.Items[itemToMove] = itemAfter;\r
210                 list_queue.SelectedIndex = list_queue.SelectedIndex + 1;\r
211             }\r
212         }\r
213 \r
214         private void btn_delete_Click(object sender, EventArgs e)\r
215         {\r
216             list_queue.Items.Remove(list_queue.SelectedItem);\r
217         }\r
218         #endregion\r
219 \r
220 \r
221         private void btn_Close_Click(object sender, EventArgs e)\r
222         {\r
223             this.Hide();\r
224         }\r
225 \r
226         protected override void OnClosing(CancelEventArgs e)\r
227         {\r
228             e.Cancel = true;\r
229             this.Hide();\r
230             base.OnClosing(e);\r
231         }\r
232 \r
233 \r
234 \r
235     }\r
236 }