OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Functions / Main.cs
1 /*  Main.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.Windows.Forms;\r
9 using System.IO;\r
10 using System.Diagnostics;\r
11 using System.Text.RegularExpressions;\r
12 using System.Collections.Generic;\r
13 using System.Xml.Serialization;\r
14 using System.Threading;\r
15 using Handbrake.EncodeQueue;\r
16 using System.Net;\r
17 \r
18 namespace Handbrake.Functions\r
19 {\r
20     static class Main\r
21     {\r
22         // Private Variables\r
23         private static readonly XmlSerializer ser = new XmlSerializer(typeof(List<Job>));\r
24 \r
25         /// <summary>\r
26         /// Calculate the duration of the selected title and chapters\r
27         /// </summary>\r
28         public static TimeSpan calculateDuration(string chapter_start, string chapter_end, Parsing.Title selectedTitle)\r
29         {\r
30             TimeSpan Duration = TimeSpan.FromSeconds(0.0);\r
31 \r
32             // Get the durations between the 2 chapter points and add them together.\r
33             if (chapter_start != "Auto" && chapter_end != "Auto")\r
34             {\r
35                 int start_chapter, end_chapter;\r
36                 int.TryParse(chapter_start, out start_chapter);\r
37                 int.TryParse(chapter_end, out end_chapter);\r
38 \r
39                 int position = start_chapter - 1;\r
40 \r
41                 if (start_chapter <= end_chapter)\r
42                 {\r
43                     if (end_chapter > selectedTitle.Chapters.Count)\r
44                         end_chapter = selectedTitle.Chapters.Count;\r
45 \r
46                     while (position != end_chapter)\r
47                     {\r
48                         TimeSpan dur = selectedTitle.Chapters[position].Duration;\r
49                         Duration = Duration + dur;\r
50                         position++;\r
51                     }\r
52                 }\r
53             }\r
54             return Duration;\r
55         }\r
56 \r
57         /// <summary>\r
58         /// Select the longest title in the DVD title dropdown menu on frmMain\r
59         /// </summary>\r
60         public static Parsing.Title selectLongestTitle(ComboBox drp_dvdtitle)\r
61         {\r
62             int current_largest = 0;\r
63             Parsing.Title title2Select;\r
64 \r
65             // Check if there are titles in the DVD title dropdown menu and make sure, it's not just "Automatic"\r
66             if (drp_dvdtitle.Items[0].ToString() != "Automatic")\r
67                 title2Select = (Parsing.Title)drp_dvdtitle.Items[0];\r
68             else\r
69                 title2Select = null;\r
70 \r
71             // So, If there are titles in the DVD Title dropdown menu, lets select the longest.\r
72             if (title2Select != null)\r
73             {\r
74                 foreach (Parsing.Title x in drp_dvdtitle.Items)\r
75                 {\r
76                     string title = x.ToString();\r
77                     if (title != "Automatic")\r
78                     {\r
79                         string[] y = title.Split(' ');\r
80                         string time = y[1].Replace("(", "").Replace(")", "");\r
81                         string[] z = time.Split(':');\r
82 \r
83                         int hours = int.Parse(z[0]) * 60 * 60;\r
84                         int minutes = int.Parse(z[1]) * 60;\r
85                         int seconds = int.Parse(z[2]);\r
86                         int total_sec = hours + minutes + seconds;\r
87 \r
88                         if (current_largest == 0)\r
89                         {\r
90                             current_largest = hours + minutes + seconds;\r
91                             title2Select = x;\r
92                         }\r
93                         else\r
94                         {\r
95                             if (total_sec > current_largest)\r
96                             {\r
97                                 current_largest = total_sec;\r
98                                 title2Select = x;\r
99                             }\r
100                         }\r
101                     }\r
102                 }\r
103             }\r
104             return title2Select;\r
105         }\r
106 \r
107         /// <summary>\r
108         /// Set's up the DataGridView on the Chapters tab (frmMain)\r
109         /// </summary>\r
110         public static DataGridView chapterNaming(DataGridView data_chpt, string chapter_end)\r
111         {\r
112             int i = 0, finish = 0;\r
113 \r
114             if (chapter_end != "Auto")\r
115                 int.TryParse(chapter_end, out finish);\r
116 \r
117             while (i < finish)\r
118             {\r
119                 int n = data_chpt.Rows.Add();\r
120                 data_chpt.Rows[n].Cells[0].Value = (i + 1);\r
121                 data_chpt.Rows[n].Cells[1].Value = "Chapter " + (i + 1);\r
122                 data_chpt.Rows[n].Cells[0].ValueType = typeof(int);\r
123                 data_chpt.Rows[n].Cells[1].ValueType = typeof(string);\r
124                 i++;\r
125             }\r
126 \r
127             return data_chpt;\r
128         }\r
129 \r
130         /// <summary>\r
131         /// Function which generates the filename and path automatically based on \r
132         /// the Source Name, DVD title and DVD Chapters\r
133         /// </summary>\r
134         public static string autoName(ComboBox drp_dvdtitle, string chapter_start, string chatper_end, string source, string dest, int format)\r
135         {\r
136             string AutoNamePath = string.Empty;\r
137             if (drp_dvdtitle.Text != "Automatic")\r
138             {\r
139                 // Get the Source Name \r
140                 string sourceName = Path.GetFileNameWithoutExtension(source);\r
141 \r
142                 // Get the Selected Title Number\r
143                 string[] titlesplit = drp_dvdtitle.Text.Split(' ');\r
144                 string dvdTitle = titlesplit[0].Replace("Automatic", "");\r
145 \r
146                 // Get the Chapter Start and Chapter End Numbers\r
147                 string chapterStart = chapter_start.Replace("Auto", "");\r
148                 string chapterFinish = chatper_end.Replace("Auto", "");\r
149                 string combinedChapterTag = chapterStart;\r
150                 if (chapterFinish != chapterStart && chapterFinish != "")\r
151                     combinedChapterTag = chapterStart + "-" + chapterFinish;\r
152 \r
153                 // Get the destination filename.\r
154                 string destination_filename;\r
155                 if (Properties.Settings.Default.autoNameFormat != "")\r
156                 {\r
157                     destination_filename = Properties.Settings.Default.autoNameFormat;\r
158                     destination_filename = destination_filename.Replace("{source}", sourceName).Replace("{title}", dvdTitle).Replace("{chapters}", combinedChapterTag);\r
159                 }\r
160                 else\r
161                     destination_filename = sourceName + "_T" + dvdTitle + "_C" + combinedChapterTag;\r
162 \r
163                 // Add the appropriate file extension\r
164                 if (format == 0)\r
165                 {\r
166                     if (Properties.Settings.Default.useM4v)\r
167                         destination_filename += ".m4v";\r
168                     else\r
169                         destination_filename += ".mp4";\r
170                 }\r
171                 else if (format == 1)\r
172                     destination_filename += ".mkv";\r
173 \r
174                 // Now work out the path where the file will be stored.\r
175                 // First case: If the destination box doesn't already contain a path, make one.\r
176                 if (!dest.Contains(Path.DirectorySeparatorChar.ToString()))\r
177                 {\r
178                     // If there is an auto name path, use it...\r
179                     if (Properties.Settings.Default.autoNamePath.Trim() != "" && Properties.Settings.Default.autoNamePath.Trim() != "Click 'Browse' to set the default location")\r
180                         AutoNamePath = Path.Combine(Properties.Settings.Default.autoNamePath, destination_filename);\r
181                     else // ...otherwise, output to the source directory\r
182                         AutoNamePath = null;\r
183                 }\r
184                 else // Otherwise, use the path that is already there.\r
185                 {\r
186                     // Use the path and change the file extension to match the previous destination\r
187                     AutoNamePath = Path.Combine(Path.GetDirectoryName(dest), destination_filename);\r
188                     AutoNamePath = Path.ChangeExtension(AutoNamePath, Path.GetExtension(dest));\r
189                 }\r
190             }\r
191 \r
192             return AutoNamePath;\r
193         }\r
194 \r
195         /// <summary>\r
196         /// Get's HandBrakes version data from the CLI.\r
197         /// </summary>\r
198         /// <returns>Arraylist of Version Data. 0 = hb_version 1 = hb_build</returns>\r
199         public static void setCliVersionData()\r
200         {\r
201             String line;\r
202 \r
203             // 0 = SVN Build / Version\r
204             // 1 = Build Date\r
205             Process cliProcess = new Process();\r
206             ProcessStartInfo handBrakeCLI = new ProcessStartInfo("HandBrakeCLI.exe", " -u")\r
207                                                 {\r
208                                                     UseShellExecute = false,\r
209                                                     RedirectStandardError = true,\r
210                                                     RedirectStandardOutput = true,\r
211                                                     CreateNoWindow = true\r
212                                                 };\r
213             cliProcess.StartInfo = handBrakeCLI;\r
214 \r
215             try\r
216             {\r
217                 cliProcess.Start();\r
218                 // Retrieve standard output and report back to parent thread until the process is complete\r
219                 TextReader stdOutput = cliProcess.StandardError;\r
220 \r
221                 while (!cliProcess.HasExited)\r
222                 {\r
223                     line = stdOutput.ReadLine() ?? "";\r
224                     Match m = Regex.Match(line, @"HandBrake ([0-9.]*)(svn[0-9M]*) \([0-9]*\)");\r
225 \r
226                     if (m.Success)\r
227                     {\r
228                         string data = line.Replace("(", "").Replace(")", "").Replace("HandBrake ", "");\r
229                         string[] arr = data.Split(' ');\r
230 \r
231                         Properties.Settings.Default.hb_build = int.Parse(arr[1]);\r
232                         Properties.Settings.Default.hb_version = arr[0];\r
233                     }\r
234                     if (cliProcess.TotalProcessorTime.Seconds > 10) // Don't wait longer than 10 seconds.\r
235                     {\r
236                         Process cli = Process.GetProcessById(cliProcess.Id);\r
237                         if (!cli.HasExited)\r
238                             cli.Kill();\r
239                     }\r
240                 }\r
241             }\r
242             catch (Exception e)\r
243             {\r
244                 MessageBox.Show("Unable to retrieve version information from the CLI. \nError:\n" + e);\r
245             }\r
246         }\r
247 \r
248         /// <summary>\r
249         /// Check if the queue recovery file contains records.\r
250         /// If it does, it means the last queue did not complete before HandBrake closed.\r
251         /// So, return a boolean if true. \r
252         /// </summary>\r
253         public static Boolean check_queue_recovery()\r
254         {\r
255             try\r
256             {\r
257                 string tempPath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml");\r
258                 if (File.Exists(tempPath))\r
259                 {\r
260                     using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))\r
261                     {\r
262                         List<Job> list = ser.Deserialize(strm) as List<Job>;\r
263                         if (list != null)\r
264                             if (list.Count != 0)\r
265                                 return true;\r
266                     }\r
267                 }\r
268                 return false;\r
269             }\r
270             catch (Exception)\r
271             {\r
272                 return false; // Keep quiet about the error.\r
273             }\r
274         }\r
275 \r
276         /// <summary>\r
277         /// Get the Process ID of HandBrakeCLI for the current instance.\r
278         /// </summary>\r
279         /// <param name="before">List of processes before the new process was started</param>\r
280         /// <returns>Int - Process ID</returns>\r
281         public static int getCliProcess(Process[] before)\r
282         {\r
283             // This is a bit of a cludge. Maybe someone has a better idea on how to impliment this.\r
284             // Since we used CMD to start HandBrakeCLI, we don't get the process ID from hbProc.\r
285             // Instead we take the processes before and after, and get the ID of HandBrakeCLI.exe\r
286             // avoiding any previous instances of HandBrakeCLI.exe in before.\r
287             // Kill the current process.\r
288 \r
289             Process[] hbProcesses = Process.GetProcessesByName("HandBrakeCLI");\r
290 \r
291             // Another hack. We maybe need to wait a few seconds for HandBrakeCLI to launch\r
292             if (hbProcesses.Length == 0)\r
293             {\r
294                 Thread.Sleep(2000);\r
295                 hbProcesses = Process.GetProcessesByName("HandBrakeCLI");\r
296             }\r
297 \r
298             Process hbProcess = null;\r
299             if (hbProcesses.Length > 0)\r
300                 foreach (Process process in hbProcesses)\r
301                 {\r
302                     Boolean found = false;\r
303                     // Check if the current CLI instance was running before we started the current one\r
304                     foreach (Process bprocess in before)\r
305                     {\r
306                         if (process.Id == bprocess.Id)\r
307                             found = true;\r
308                     }\r
309 \r
310                     // If it wasn't running before, we found the process we want.\r
311                     if (!found)\r
312                     {\r
313                         hbProcess = process;\r
314                         break;\r
315                     }\r
316                 }\r
317             if (hbProcess != null)\r
318                 return hbProcess.Id;\r
319 \r
320             return -1;\r
321         }\r
322 \r
323         /// <summary>\r
324         ///  Clear all the encode log files.\r
325         /// </summary>\r
326         public static void clearLogs()\r
327         {\r
328             string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
329             if (Directory.Exists(logDir))\r
330             {\r
331                 DirectoryInfo info = new DirectoryInfo(logDir);\r
332                 FileInfo[] logFiles = info.GetFiles("*.txt");\r
333                 foreach (FileInfo file in logFiles)\r
334                 {\r
335                     if (!file.Name.Contains("last_scan_log") && !file.Name.Contains("last_encode_log") && !file.Name.Contains("tmp_appReadable_log.txt"))\r
336                     {\r
337                         File.Delete(file.FullName);\r
338                     }\r
339                 }\r
340             }\r
341         }\r
342 \r
343         /// <summary>\r
344         /// Begins checking for an update to HandBrake.\r
345         /// </summary>\r
346         /// <param name="callback">The method that will be called when the check is finished.</param>\r
347         /// <param name="debug">Whether or not to execute this in debug mode.</param>\r
348         public static void BeginCheckForUpdates(AsyncCallback callback, bool debug)\r
349         {\r
350             ThreadPool.QueueUserWorkItem(new WaitCallback(delegate\r
351             {\r
352                 try\r
353                 {\r
354                     // Is this a stable or unstable build?\r
355                     string url = Properties.Settings.Default.hb_build.ToString().EndsWith("1") ? Properties.Settings.Default.appcast_unstable : Properties.Settings.Default.appcast;\r
356 \r
357                     // Initialize variables\r
358                     WebRequest request = WebRequest.Create(url);\r
359                     WebResponse response = request.GetResponse();\r
360                     AppcastReader reader = new AppcastReader();\r
361 \r
362                     // Get the data, convert it to a string, and parse it into the AppcastReader\r
363                     reader.getInfo(new StreamReader(response.GetResponseStream()).ReadToEnd());\r
364 \r
365                     // Further parse the information\r
366                     string build = reader.build;\r
367 \r
368                     int latest = int.Parse(build);\r
369                     int current = Properties.Settings.Default.hb_build;\r
370                     int skip = Properties.Settings.Default.skipversion;\r
371 \r
372                     // If the user wanted to skip this version, don't report the update\r
373                     if (latest == skip)\r
374                     {\r
375                         UpdateCheckInformation info = new UpdateCheckInformation() { NewVersionAvailable = false, BuildInformation = null };\r
376                         callback(new UpdateCheckResult(debug, info));\r
377                         return;\r
378                     }\r
379 \r
380                     // Set when the last update was\r
381                     Properties.Settings.Default.lastUpdateCheckDate = DateTime.Now;\r
382                     Properties.Settings.Default.Save();\r
383 \r
384                     UpdateCheckInformation info2 = new UpdateCheckInformation() { NewVersionAvailable = latest > current, BuildInformation = reader };\r
385                     callback(new UpdateCheckResult(debug, info2));\r
386                 }\r
387                 catch (Exception exc)\r
388                 {\r
389                     callback(new UpdateCheckResult(debug, new UpdateCheckInformation() { Error = exc }));\r
390                 }\r
391             }));\r
392         }\r
393 \r
394         /// <summary>\r
395         /// \r
396         /// </summary>\r
397         /// <param name="result"></param>\r
398         /// <returns></returns>\r
399         public static UpdateCheckInformation EndCheckForUpdates(IAsyncResult result)\r
400         {\r
401             UpdateCheckResult checkResult = (UpdateCheckResult)result;\r
402             return checkResult.Result;\r
403         }\r
404 \r
405         /// <summary>\r
406         /// Used in EndUpdateCheck() for update checking and the IAsyncResult design pattern.\r
407         /// </summary>\r
408         private class UpdateCheckResult : IAsyncResult\r
409         {\r
410             public UpdateCheckResult(object asyncState, UpdateCheckInformation info)\r
411             {\r
412                 AsyncState = asyncState;\r
413                 Result = info;\r
414             }\r
415 \r
416             /// <summary>\r
417             /// Gets whether the check was executed in debug mode.\r
418             /// </summary>\r
419             public object AsyncState { get; private set; }\r
420 \r
421             /// <summary>\r
422             /// Gets the result of the update check.\r
423             /// </summary>\r
424             public UpdateCheckInformation Result { get; private set; }\r
425 \r
426             public WaitHandle AsyncWaitHandle { get { throw new NotImplementedException(); } }\r
427             public bool CompletedSynchronously { get { throw new NotImplementedException(); } }\r
428             public bool IsCompleted { get { throw new NotImplementedException(); } }\r
429         }\r
430     }\r
431 }\r