OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Queue / QueueHandler.cs
1 /*  QueueHandler.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.IO;\r
10 using System.Windows.Forms;\r
11 using System.Xml.Serialization;\r
12 using System.Threading;\r
13 using System.Diagnostics;\r
14 \r
15 namespace Handbrake.Queue\r
16 {\r
17     public class QueueHandler\r
18     {\r
19         private static XmlSerializer ser = new XmlSerializer(typeof(List<QueueItem>));\r
20         List<QueueItem> queue = new List<QueueItem>();\r
21         int id; // Unique identifer number for each job\r
22         private QueueItem lastItem;\r
23 \r
24         #region Queue Handling\r
25         public List<QueueItem> getQueue()\r
26         {\r
27             return queue;\r
28         }\r
29 \r
30         /// <summary>\r
31         /// Get's the next CLI query for encoding\r
32         /// </summary>\r
33         /// <returns>String</returns>\r
34         public string getNextItemForEncoding()\r
35         {\r
36             QueueItem job = queue[0];\r
37             String query = job.Query;\r
38             lastItem = job;\r
39             remove(0);    // Remove the item which we are about to pass out.\r
40             return query;\r
41         }\r
42 \r
43         /// <summary>\r
44         /// Get the last query that was returned by getNextItemForEncoding()\r
45         /// </summary>\r
46         /// <returns></returns>\r
47         public QueueItem getLastQueryItem()\r
48         {\r
49             return lastItem;\r
50         }\r
51 \r
52         /// <summary>\r
53         /// Add's a new item to the queue\r
54         /// </summary>\r
55         /// <param name="query">String</param>\r
56         /// <param name="source"></param>\r
57         /// <param name="destination"></param>\r
58         public void add(string query, string source, string destination)\r
59         {\r
60             QueueItem newJob = new QueueItem();\r
61             newJob.Id = id;\r
62             newJob.Query = query;\r
63             newJob.Source = source;\r
64             newJob.Destination = destination;\r
65             id++;\r
66 \r
67             // Adds the job to the queue\r
68             queue.Add(newJob);\r
69         }\r
70 \r
71         /// <summary>\r
72         /// Removes an item from the queue.\r
73         /// </summary>\r
74         /// <param name="index">Index</param>\r
75         /// <returns>Bolean true if successful</returns>\r
76         public Boolean remove(int index)\r
77         {\r
78             queue.RemoveAt(index);\r
79             return true;\r
80         }\r
81 \r
82         /// <summary>\r
83         /// Returns how many items are in the queue\r
84         /// </summary>\r
85         /// <returns>Int</returns>\r
86         public int count()\r
87         {\r
88             return queue.Count;\r
89         }\r
90 \r
91         /// <summary>\r
92         /// Move an item with an index x, up in the queue\r
93         /// </summary>\r
94         /// <param name="index">Int</param>\r
95         public void moveUp(int index)\r
96         {\r
97             if (index > 0)\r
98             {\r
99                 QueueItem item = queue[index];\r
100 \r
101                 queue.RemoveAt(index);\r
102                 queue.Insert((index - 1), item);\r
103             }\r
104         }\r
105 \r
106         /// <summary>\r
107         /// Move an item with an index x, down in the queue\r
108         /// </summary>\r
109         /// <param name="index">Int</param>\r
110         public void moveDown(int index)\r
111         {\r
112             if (index < queue.Count - 1)\r
113             {\r
114                 QueueItem item = queue[index];\r
115 \r
116                 queue.RemoveAt(index);\r
117                 queue.Insert((index + 1), item);\r
118             }\r
119         }\r
120 \r
121         /// <summary>\r
122         /// Writes the current queue to disk. hb_queue_recovery.xml\r
123         /// This function is called after getNextItemForEncoding()\r
124         /// </summary>\r
125         public void write2disk(string file)\r
126         {\r
127             string tempPath;\r
128             if (file == "hb_queue_recovery.xml")\r
129                 tempPath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml");\r
130             else\r
131                 tempPath = file;\r
132 \r
133             try\r
134             {\r
135                 using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))\r
136                 {\r
137                     ser.Serialize(strm, queue);\r
138                     strm.Close();\r
139                     strm.Dispose();\r
140                 }\r
141             }\r
142             catch (Exception)\r
143             {\r
144                 // Any Errors will be out of diskspace/permissions problems. \r
145                 // Don't report them as they'll annoy the user.\r
146             }\r
147         }\r
148 \r
149         /// <summary>\r
150         /// Writes the current queue to disk to the location specified in file\r
151         /// </summary>\r
152         /// <param name="file"></param>\r
153         public void writeBatchScript(string file)\r
154         {\r
155             string queries = "";\r
156             foreach (QueueItem queue_item in queue)\r
157             {\r
158                 string q_item = queue_item.Query;\r
159                 string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + q_item;\r
160 \r
161                 if (queries == string.Empty)\r
162                     queries = queries + fullQuery;\r
163                 else\r
164                     queries = queries + " && " + fullQuery;\r
165             }\r
166             string strCmdLine = queries;\r
167 \r
168             if (file != "")\r
169             {\r
170                 try\r
171                 {\r
172                     // Create a StreamWriter and open the file, Write the batch file query to the file and \r
173                     // Close the stream\r
174                     StreamWriter line = new StreamWriter(file);\r
175                     line.WriteLine(strCmdLine);\r
176                     line.Close();\r
177 \r
178                     MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
179                 }\r
180                 catch (Exception)\r
181                 {\r
182                     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
183                 }\r
184 \r
185             }\r
186         }\r
187 \r
188         /// <summary>\r
189         /// Recover the queue from hb_queue_recovery.xml\r
190         /// </summary>\r
191         public void recoverQueue(string file)\r
192         {\r
193             string tempPath;\r
194             if (file == "hb_queue_recovery.xml")\r
195                 tempPath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml");\r
196             else\r
197                 tempPath = file;\r
198 \r
199             if (File.Exists(tempPath))\r
200             {\r
201                 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))\r
202                 {\r
203                     if (strm.Length != 0)\r
204                     {\r
205                         List<QueueItem> list = ser.Deserialize(strm) as List<QueueItem>;\r
206 \r
207                         foreach (QueueItem item in list)\r
208                             queue.Add(item);\r
209 \r
210                         if (file != "hb_queue_recovery.xml")\r
211                             write2disk("hb_queue_recovery.xml");\r
212                     }\r
213                 }\r
214             }\r
215         }\r
216         #endregion\r
217 \r
218         //------------------------------------------------------------------------\r
219         Functions.Encode encodeHandler = new Functions.Encode();\r
220         private Boolean started = false;\r
221         private Boolean paused;\r
222         private Boolean encoding;\r
223 \r
224         #region Encoding\r
225 \r
226         public Boolean isEncodeStarted\r
227         {\r
228             get { return started; }\r
229         }\r
230         public Boolean isPaused\r
231         {\r
232             get { return paused; }\r
233         }\r
234         public Boolean isEncoding\r
235         {\r
236             get { return encoding; }\r
237         }\r
238 \r
239         public void startEncode()\r
240         {\r
241             Thread theQueue;\r
242             if (this.count() != 0)\r
243             {\r
244                 if (paused)\r
245                     paused = false;\r
246                 else\r
247                 {\r
248                     paused = false;\r
249                     try\r
250                     {\r
251                         theQueue = new Thread(startProc) {IsBackground = true};\r
252                         theQueue.Start();\r
253                     }\r
254                     catch (Exception exc)\r
255                     {\r
256                         MessageBox.Show(exc.ToString());\r
257                     }\r
258                 }\r
259             }\r
260         }\r
261         public void pauseEncode()\r
262         {\r
263             paused = true;\r
264             EncodePaused(null);\r
265         }\r
266 \r
267         private void startProc(object state)\r
268         {\r
269             Process hbProc;\r
270             try\r
271             {\r
272                 // Run through each item on the queue\r
273                 while (this.count() != 0)\r
274                 {\r
275                     string query = getNextItemForEncoding();\r
276                     write2disk("hb_queue_recovery.xml"); // Update the queue recovery file\r
277 \r
278                     EncodeStarted(null);\r
279                     hbProc = encodeHandler.runCli(this, query);\r
280                     hbProc.WaitForExit();\r
281 \r
282                     encodeHandler.addCLIQueryToLog(query);\r
283                     encodeHandler.copyLog(query, getLastQueryItem().Destination);\r
284 \r
285                     hbProc.Close();\r
286                     hbProc.Dispose();\r
287                     EncodeFinished(null);\r
288 \r
289                     while (paused) // Need to find a better way of doing this.\r
290                     {\r
291                         Thread.Sleep(10000);\r
292                     }\r
293                 }\r
294                 EncodeQueueFinished(null);\r
295 \r
296                 // After the encode is done, we may want to shutdown, suspend etc.\r
297                 encodeHandler.afterEncodeAction();\r
298             }\r
299             catch (Exception exc)\r
300             {\r
301                 throw new Exception(exc.ToString());\r
302             }\r
303         }\r
304         #endregion\r
305 \r
306         #region Events\r
307         public event EventHandler OnEncodeStart;\r
308         public event EventHandler OnPaused;\r
309         public event EventHandler OnEncodeEnded;\r
310         public event EventHandler OnQueueFinished;\r
311 \r
312         // Invoke the Changed event; called whenever encodestatus changes:\r
313         protected virtual void EncodeStarted(EventArgs e)\r
314         {\r
315             if (OnEncodeStart != null)\r
316                 OnEncodeStart(this, e);\r
317 \r
318             encoding = true;\r
319         }\r
320         protected virtual void EncodePaused(EventArgs e)\r
321         {\r
322             if (OnPaused != null)\r
323                 OnPaused(this, e);\r
324         }\r
325         protected virtual void EncodeFinished(EventArgs e)\r
326         {\r
327             if (OnEncodeEnded != null)\r
328                 OnEncodeEnded(this, e);\r
329 \r
330             encoding = false;\r
331         }\r
332         protected virtual void EncodeQueueFinished(EventArgs e)\r
333         {\r
334             if (OnQueueFinished != null)\r
335                 OnQueueFinished(this, e);\r
336         }\r
337         #endregion\r
338 \r
339     }\r
340 }\r