OSDN Git Service

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