OSDN Git Service

LinGui: move the picture filters to the preview window.
[handbrake-jp/handbrake-jp-git.git] / win / C# / Queue / QueueHandler.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Text;\r
4 using System.Collections;\r
5 using System.IO;\r
6 using System.Windows.Forms;\r
7 using System.Xml.Serialization;\r
8 \r
9 namespace Handbrake.Queue\r
10 {\r
11     public class Queue\r
12     {\r
13         private static XmlSerializer ser = new XmlSerializer(typeof(List<QueueItem>));\r
14         List<QueueItem> queue = new List<QueueItem>();\r
15         int id = 0; // Unique identifer number for each job\r
16         private QueueItem lastItem;\r
17 \r
18         public List<QueueItem> getQueue()\r
19         {\r
20              return queue;\r
21         }\r
22 \r
23         /// <summary>\r
24         /// Get's the next CLI query for encoding\r
25         /// </summary>\r
26         /// <returns>String</returns>\r
27         public string getNextItemForEncoding()\r
28         {\r
29             QueueItem job = queue[0];\r
30             String query = job.Query;\r
31             lastItem = job;\r
32             remove(0);    // Remove the item which we are about to pass out.\r
33             return query;\r
34         }\r
35 \r
36         /// <summary>\r
37         /// Get the last query that was returned by getNextItemForEncoding()\r
38         /// </summary>\r
39         /// <returns></returns>\r
40         public QueueItem getLastQuery()\r
41         {\r
42             return lastItem;\r
43         }\r
44 \r
45         /// <summary>\r
46         /// Add's a new item to the queue\r
47         /// </summary>\r
48         /// <param name="query">String</param>\r
49         public void add(string query, string source, string destination)\r
50         {\r
51             QueueItem newJob = new QueueItem();\r
52             newJob.Id = id;\r
53             newJob.Query = query;\r
54             newJob.Source = source;\r
55             newJob.Destination = destination;\r
56             id++;\r
57 \r
58             // Adds the job to the queue\r
59             queue.Add(newJob);\r
60         }\r
61 \r
62         /// <summary>\r
63         /// Removes an item from the queue.\r
64         /// </summary>\r
65         /// <param name="index">Index</param>\r
66         /// <returns>Bolean true if successful</returns>\r
67         public Boolean remove(int index)\r
68         {\r
69             queue.RemoveAt(index);\r
70             return true;\r
71         }\r
72 \r
73         /// <summary>\r
74         /// Returns how many items are in the queue\r
75         /// </summary>\r
76         /// <returns>Int</returns>\r
77         public int count()\r
78         {\r
79             return queue.Count;\r
80         }\r
81 \r
82         /// <summary>\r
83         /// Move an item with an index x, up in the queue\r
84         /// </summary>\r
85         /// <param name="index">Int</param>\r
86         public void moveUp(int index)\r
87         {\r
88             if (index != 0)\r
89             {\r
90                 QueueItem item = (QueueItem)queue[index];\r
91 \r
92                 queue.Insert((index - 1), item);\r
93                 queue.RemoveAt((index + 1));\r
94             }\r
95         }\r
96 \r
97         /// <summary>\r
98         /// Move an item with an index x, down in the queue\r
99         /// </summary>\r
100         /// <param name="index">Int</param>\r
101         public void moveDown(int index)\r
102         {\r
103             if (index != queue.Count - 1)\r
104             {\r
105                 QueueItem item = (QueueItem)queue[index];\r
106 \r
107                 queue.Insert((index + 2), item);\r
108                 queue.RemoveAt((index));\r
109             }\r
110         }\r
111 \r
112         /// <summary>\r
113         /// Writes the current queue to disk. hb_queue_recovery.xml\r
114         /// This function is called after getNextItemForEncoding()\r
115         /// </summary>\r
116         public void write2disk(string file)\r
117         {\r
118             string tempPath = "";\r
119             if (file == "hb_queue_recovery.xml")\r
120                 tempPath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml");\r
121             else\r
122                 tempPath = file;\r
123 \r
124             try\r
125             {\r
126                 using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))\r
127                 {\r
128                     ser.Serialize(strm, queue);\r
129                     strm.Close();\r
130                     strm.Dispose();\r
131                 }\r
132             }\r
133             catch (Exception)\r
134             {\r
135                 // Any Errors will be out of diskspace/permissions problems. \r
136                 // Don't report them as they'll annoy the user.\r
137             }\r
138         }\r
139 \r
140         /// <summary>\r
141         /// Writes the current queue to disk to the location specified in file\r
142         /// </summary>\r
143         /// <param name="file"></param>\r
144         public void writeBatchScript(string file)\r
145         {\r
146             string queries = "";\r
147             foreach (QueueItem queue_item in queue)\r
148             {\r
149                 string q_item = queue_item.Query;\r
150                 string fullQuery = '"' + Application.StartupPath.ToString() + "\\HandBrakeCLI.exe" + '"' + q_item;\r
151 \r
152                 if (queries == string.Empty)\r
153                     queries = queries + fullQuery;\r
154                 else\r
155                     queries = queries + " && " + fullQuery;\r
156             }\r
157             string strCmdLine = queries;\r
158 \r
159             if (file != "")\r
160             {\r
161                 try\r
162                 {\r
163                     // Create a StreamWriter and open the file, Write the batch file query to the file and \r
164                     // Close the stream\r
165                     StreamWriter line = new StreamWriter(file);\r
166                     line.WriteLine(strCmdLine);\r
167                     line.Close();\r
168 \r
169                     MessageBox.Show("Your batch script has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
170                 }\r
171                 catch (Exception)\r
172                 {\r
173                     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
174                 }\r
175 \r
176             }\r
177         }\r
178 \r
179         /// <summary>\r
180         /// Recover the queue from hb_queue_recovery.xml\r
181         /// </summary>\r
182         public void recoverQueue(string file)\r
183         {\r
184             string tempPath = "";\r
185             if (file == "hb_queue_recovery.xml")\r
186                 tempPath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml");\r
187             else\r
188                 tempPath = file;\r
189 \r
190             if (File.Exists(tempPath))\r
191             {\r
192                 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))\r
193                 {\r
194                     if (strm.Length != 0)\r
195                     {\r
196                         List<QueueItem> list = ser.Deserialize(strm) as List<QueueItem>;\r
197 \r
198                         foreach (QueueItem item in list)\r
199                             queue.Add(item);\r
200                     }\r
201                 }\r
202             }\r
203         }\r
204     }\r
205 }\r