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.Data;\r
11 using System.Drawing;\r
12 using System.Text;\r
13 using System.Windows.Forms;\r
14 using System.Threading;\r
15 using System.Diagnostics;\r
16 using System.Runtime.InteropServices;\r
17 using System.IO;\r
18 \r
19 namespace Handbrake\r
20 {\r
21     public partial class frmQueue : Form\r
22     {\r
23         private delegate void ProgressUpdateHandler();\r
24         private delegate void setEncoding();\r
25 \r
26         public frmQueue()\r
27         {\r
28             InitializeComponent();\r
29         }\r
30 \r
31         #region Queue Handling\r
32         Boolean cancel = false;\r
33 \r
34         private void btn_encode_Click(object sender, EventArgs e)\r
35         {\r
36             // Reset some values\r
37 \r
38             if (list_queue.Items.Count != 0)\r
39             {\r
40                 lbl_status.Visible = false;\r
41                 btn_encode.Enabled = false;\r
42             }\r
43             cancel = false;\r
44 \r
45             // Start the encode\r
46             try\r
47             {\r
48                 if (list_queue.Items.Count != 0)\r
49                 {\r
50                     // Setup or reset some values\r
51                     btn_stop.Visible = true;\r
52                     progressBar.Value = 0;\r
53                     lbl_progressValue.Text = "0 %";\r
54                     progressBar.Step = 100 / list_queue.Items.Count;\r
55                     progressBar.Update();\r
56                     //ThreadPool.QueueUserWorkItem(startProc);\r
57                     // Testing a new way of launching a thread. Hopefully will fix a random freeze up of the main thread.\r
58                     Thread theQ = new Thread(startProc);\r
59                     theQ.Start();\r
60                 }\r
61             }\r
62             catch (Exception exc)\r
63             {\r
64                 MessageBox.Show(exc.ToString());\r
65             }\r
66         }\r
67 \r
68         private void btn_stop_Click(object sender, EventArgs e)\r
69         {\r
70             cancel = true;\r
71             btn_stop.Visible = false;\r
72             btn_encode.Enabled = true;\r
73             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
74         }\r
75 \r
76         [DllImport("user32.dll")]\r
77         public static extern void LockWorkStation();\r
78         [DllImport("user32.dll")]\r
79         public static extern int ExitWindowsEx(int uFlags, int dwReason);\r
80 \r
81         private void startProc(object state)\r
82         {\r
83             try\r
84             {\r
85                 while (list_queue.Items.Count != 0)\r
86                 {\r
87                     string query = list_queue.Items[0].ToString();\r
88                     setEncValue();\r
89                     updateUIElements();\r
90                    \r
91                     Functions.CLI process = new Functions.CLI();\r
92                     Process hbProc = process.runCli(this, query, false, false, false, false);\r
93 \r
94                     hbProc.WaitForExit();\r
95                     hbProc.Close();\r
96                     hbProc.Dispose();\r
97 \r
98                     query = "";\r
99 \r
100                     if (cancel == true)\r
101                     {\r
102                         break;\r
103                     }              \r
104                 }\r
105 \r
106                 resetQueue();\r
107                 \r
108                 // Do something whent he encode ends.\r
109                 switch (Properties.Settings.Default.CompletionOption)\r
110                 {\r
111                     case "Shutdown":\r
112                         System.Diagnostics.Process.Start("Shutdown", "-s -t 60");\r
113                         break;\r
114                     case "Log Off":\r
115                         ExitWindowsEx(0, 0);\r
116                         break;\r
117                     case "Suspend":\r
118                         Application.SetSuspendState(PowerState.Suspend, true, true);\r
119                         break;\r
120                     case "Hibernate":\r
121                         Application.SetSuspendState(PowerState.Hibernate, true, true);\r
122                         break;\r
123                     case "Lock System":\r
124                         LockWorkStation();\r
125                         break;\r
126                     case "Quit HandBrake":\r
127                         Application.Exit();\r
128                         break;\r
129                     default:\r
130                         break;\r
131                 }\r
132             }\r
133             catch (Exception exc)\r
134             {\r
135                 MessageBox.Show(exc.ToString());\r
136             }\r
137         }\r
138 \r
139         private void updateUIElements()\r
140         {\r
141             try\r
142             {\r
143                 if (this.InvokeRequired)\r
144                 {\r
145                     this.BeginInvoke(new ProgressUpdateHandler(updateUIElements));\r
146                     return;\r
147                 }\r
148                 this.list_queue.Items.RemoveAt(0);\r
149 \r
150                 progressBar.PerformStep();\r
151                 lbl_progressValue.Text = string.Format("{0} %", progressBar.Value);\r
152                 progressBar.Update();\r
153             }\r
154             catch (Exception exc)\r
155             {\r
156                 MessageBox.Show(exc.ToString());\r
157             }\r
158         }\r
159 \r
160         private void resetQueue()\r
161         {\r
162             try\r
163             {\r
164                 if (this.InvokeRequired)\r
165                 {\r
166                     this.BeginInvoke(new ProgressUpdateHandler(resetQueue));\r
167                     return;\r
168 \r
169                 }\r
170                 btn_stop.Visible = false;\r
171                 btn_encode.Enabled = true;\r
172 \r
173                 if (cancel == true)\r
174                 {\r
175                     lbl_status.Visible = true;\r
176                     lbl_status.Text = "Encode Queue Cancelled!";\r
177                     text_edit.Text = "";\r
178                 }\r
179                 else\r
180                 {\r
181                     lbl_status.Visible = true;\r
182                     lbl_status.Text = "Encode Queue Completed!";\r
183                     text_edit.Text = "";\r
184                 }\r
185                 \r
186                 lbl_progressValue.Text = "0 %";\r
187                 progressBar.Value = 0;\r
188                 progressBar.Update();\r
189 \r
190                 lbl_source.Text = "-";\r
191                 lbl_dest.Text = "-";\r
192                 lbl_vEnc.Text = "-";\r
193                 lbl_aEnc.Text = "-";\r
194                 lbl_title.Text = "-";\r
195                 lbl_chapt.Text = "-";\r
196             }\r
197             catch (Exception exc)\r
198             {\r
199                 MessageBox.Show(exc.ToString());\r
200             }\r
201         }\r
202         \r
203         private void setEncValue()\r
204         {\r
205             try\r
206             {\r
207                 if (this.InvokeRequired)\r
208                 {\r
209                     this.BeginInvoke(new setEncoding(setEncValue));\r
210                 }\r
211 \r
212                 string query = query = list_queue.Items[0].ToString();\r
213 \r
214                 // found query is a global varible\r
215                 Functions.QueryParser parsed = Functions.QueryParser.Parse(query);\r
216                 lbl_source.Text = parsed.Source;\r
217                 lbl_dest.Text = parsed.Destination;\r
218 \r
219 \r
220                 if (parsed.DVDTitle == 0)\r
221                     lbl_title.Text = "Auto";\r
222                 else\r
223                     lbl_title.Text = parsed.DVDTitle.ToString();\r
224 \r
225                 string chapters = "";\r
226                 if (parsed.DVDChapterStart == 0)\r
227                 {\r
228                     lbl_chapt.Text = "Auto";\r
229                 }\r
230                 else\r
231                 {\r
232                     chapters = parsed.DVDChapterStart.ToString();\r
233                     if (parsed.DVDChapterFinish != 0)\r
234                         chapters = chapters + " - " + parsed.DVDChapterFinish;\r
235                     lbl_chapt.Text = chapters;\r
236                 }\r
237 \r
238                 lbl_vEnc.Text = parsed.VideoEncoder;\r
239                 lbl_aEnc.Text = parsed.AudioEncoder1;\r
240             }\r
241             catch (Exception)\r
242             {\r
243                 // Do Nothing\r
244             }\r
245         }\r
246 \r
247         #endregion\r
248 \r
249         #region Queue Management\r
250 \r
251         private void btn_up_Click(object sender, EventArgs e)\r
252         {\r
253             int count = list_queue.Items.Count;\r
254             int itemToMove = list_queue.SelectedIndex;\r
255             int previousItemint = 0;\r
256             String previousItem = "";\r
257 \r
258             if (itemToMove > 0)\r
259             {\r
260                 previousItemint = itemToMove - 1;\r
261                 previousItem = list_queue.Items[previousItemint].ToString();\r
262                 list_queue.Items[previousItemint] = list_queue.Items[itemToMove];\r
263                 list_queue.Items[itemToMove] = previousItem;\r
264                 list_queue.SelectedIndex = list_queue.SelectedIndex - 1;\r
265             }\r
266         }\r
267 \r
268         private void btn_down_Click(object sender, EventArgs e)\r
269         {\r
270             int count = list_queue.Items.Count;\r
271             int itemToMove = list_queue.SelectedIndex;\r
272             int itemAfterInt = 0;\r
273             String itemAfter = "";\r
274 \r
275             if (itemToMove < (count - 1))\r
276             {\r
277                 itemAfterInt = itemToMove + 1;\r
278                 itemAfter = list_queue.Items[itemAfterInt].ToString();\r
279                 list_queue.Items[itemAfterInt] = list_queue.Items[itemToMove];\r
280                 list_queue.Items[itemToMove] = itemAfter;\r
281                 list_queue.SelectedIndex = list_queue.SelectedIndex + 1;\r
282             }\r
283         }\r
284 \r
285         private void btn_delete_Click(object sender, EventArgs e)\r
286         {\r
287             list_queue.Items.Remove(list_queue.SelectedItem);\r
288         }\r
289 \r
290         #endregion\r
291 \r
292         #region Queue Item Modification\r
293 \r
294         int listCount = 0;\r
295 \r
296         private void btn_updateQuery_Click(object sender, EventArgs e)\r
297         {\r
298             if (text_edit.Text != "")\r
299             {\r
300                 if (list_queue.Items.Count != listCount)\r
301                 {\r
302                     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
303                 }\r
304                 else\r
305                 {\r
306                     if (list_queue.SelectedItem != null)\r
307                         list_queue.Items[list_queue.SelectedIndex] = text_edit.Text;\r
308           \r
309                 }\r
310             }\r
311         }\r
312 \r
313         private void list_queue_SelectedIndexChanged(object sender, EventArgs e)\r
314         {\r
315             if (list_queue.SelectedItem != null)\r
316                 text_edit.Text = list_queue.SelectedItem.ToString();\r
317 \r
318             listCount = list_queue.Items.Count;\r
319         }\r
320 \r
321         #endregion\r
322 \r
323         #region Queue Save & Batch Script\r
324         private void btn_batch_Click(object sender, EventArgs e)\r
325         {\r
326             string queries = "";\r
327             for (int i = 0; i < list_queue.Items.Count; i++)\r
328             {\r
329                 string query = list_queue.Items[i].ToString();\r
330                 string fullQuery = '"' + Application.StartupPath.ToString()+ "\\HandBrakeCLI.exe"+ '"' + query;\r
331 \r
332                 if (queries == "")\r
333                     queries = queries + fullQuery;\r
334                 else\r
335                     queries = queries + " && " + fullQuery;\r
336             }\r
337             string strCmdLine = queries;\r
338 \r
339             SaveFile.ShowDialog();\r
340             string filename = SaveFile.FileName;\r
341 \r
342             if (filename != "")\r
343             {\r
344                 try\r
345                 {\r
346                     // Create a StreamWriter and open the file\r
347                     StreamWriter line = new StreamWriter(filename);\r
348 \r
349                     // Write the batch file query to the file\r
350                     line.WriteLine(strCmdLine);\r
351 \r
352                     // close the stream\r
353                     line.Close();\r
354 \r
355                     MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
356                 }\r
357                 catch (Exception)\r
358                 {\r
359                     MessageBox.Show("Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
360                 }\r
361 \r
362             }\r
363         }\r
364 \r
365         #endregion\r
366 \r
367         #region other\r
368         private void btn_Close_Click(object sender, EventArgs e)\r
369         {\r
370             this.Hide();\r
371         }\r
372 \r
373         protected override void OnClosing(CancelEventArgs e)\r
374         {\r
375             e.Cancel = true;\r
376             this.Hide();\r
377             base.OnClosing(e);\r
378         }\r
379         #endregion\r
380     }\r
381 }