OSDN Git Service

f8600d3e98f836047062aa2e063aeacccf3ac97c
[handbrake-jp/handbrake-jp.git] / win / C# / Functions / Main.cs
1 /*  Common.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;\r
9 using System.Text;\r
10 using System.Windows.Forms;\r
11 using System.Globalization;\r
12 using System.IO;\r
13 using System.Drawing;\r
14 using System.Diagnostics;\r
15 using System.Text.RegularExpressions;\r
16 \r
17 namespace Handbrake.Functions\r
18 {\r
19     class Main\r
20     {\r
21         /// <summary>\r
22         /// Calculate the duration of the selected title and chapters\r
23         /// </summary>\r
24         public TimeSpan calculateDuration(string chapter_start, string chapter_end, Parsing.Title selectedTitle)\r
25         {\r
26             TimeSpan Duration = TimeSpan.FromSeconds(0.0);\r
27 \r
28             // Get the durations between the 2 chapter points and add them together.\r
29             if (chapter_start != "Auto" && chapter_end != "Auto")\r
30             {\r
31                 int start_chapter, end_chapter = 0;\r
32                 int.TryParse(chapter_start, out start_chapter);\r
33                 int.TryParse(chapter_end, out end_chapter);\r
34 \r
35                 int position = start_chapter - 1;\r
36 \r
37                 if (start_chapter <= end_chapter)\r
38                 {\r
39                     if (end_chapter > selectedTitle.Chapters.Count)\r
40                         end_chapter = selectedTitle.Chapters.Count;\r
41 \r
42                     while (position != end_chapter)\r
43                     {\r
44                         TimeSpan dur = selectedTitle.Chapters[position].Duration;\r
45                         Duration = Duration + dur;\r
46                         position++;\r
47                     }\r
48                 }\r
49             }\r
50             return Duration;\r
51         }\r
52 \r
53         /// <summary>\r
54         /// Calculate the non-anamorphic resoltuion of the source\r
55         /// </summary>\r
56         /// <param name="width"></param>\r
57         /// <returns></returns>\r
58         public int cacluateNonAnamorphicHeight(int width, decimal top, decimal bottom, decimal left, decimal right, Parsing.Title selectedTitle)\r
59         {\r
60             float aspect = selectedTitle.AspectRatio;\r
61             int aw;\r
62             int ah;\r
63             if (aspect.ToString() == "1.78")\r
64             {\r
65                 aw = 16;\r
66                 ah = 9;\r
67             }\r
68             else\r
69             {\r
70                 aw = 4;\r
71                 ah = 3;\r
72             }\r
73 \r
74             double a = width * selectedTitle.Resolution.Width * ah * (selectedTitle.Resolution.Height - (double)top - (double)bottom);\r
75             double b = selectedTitle.Resolution.Height * aw * (selectedTitle.Resolution.Width - (double)left - (double)right);\r
76 \r
77             double y = a / b;\r
78 \r
79             // If it's not Mod 16, make it mod 16\r
80             if ((y % 16) != 0)\r
81             {\r
82                 double mod16 = y % 16;\r
83                 if (mod16 >= 8)\r
84                 {\r
85                     mod16 = 16 - mod16;\r
86                     y = y + mod16;\r
87                 }\r
88                 else\r
89                 {\r
90                     y = y - mod16;\r
91                 }\r
92             }\r
93 \r
94             //16 * (421 / 16)\r
95             //double z = ( 16 * (( y + 8 ) / 16 ) );\r
96             int x = int.Parse(y.ToString());\r
97             return x;\r
98         }\r
99 \r
100         /// <summary>\r
101         /// Select the longest title in the DVD title dropdown menu on frmMain\r
102         /// </summary>\r
103         public Handbrake.Parsing.Title selectLongestTitle(ComboBox drp_dvdtitle)\r
104         {\r
105             int current_largest = 0;\r
106             Handbrake.Parsing.Title title2Select;\r
107 \r
108             // Check if there are titles in the DVD title dropdown menu and make sure, it's not just "Automatic"\r
109             if (drp_dvdtitle.Items[0].ToString() != "Automatic")\r
110                 title2Select = (Handbrake.Parsing.Title)drp_dvdtitle.Items[0];\r
111             else\r
112                 title2Select = null;\r
113 \r
114             // So, If there are titles in the DVD Title dropdown menu, lets select the longest.\r
115             if (title2Select != null)\r
116             {\r
117                 foreach (Handbrake.Parsing.Title x in drp_dvdtitle.Items)\r
118                 {\r
119                     string title = x.ToString();\r
120                     if (title != "Automatic")\r
121                     {\r
122                         string[] y = title.Split(' ');\r
123                         string time = y[1].Replace("(", "").Replace(")", "");\r
124                         string[] z = time.Split(':');\r
125 \r
126                         int hours = int.Parse(z[0]) * 60 * 60;\r
127                         int minutes = int.Parse(z[1]) * 60;\r
128                         int seconds = int.Parse(z[2]);\r
129                         int total_sec = hours + minutes + seconds;\r
130 \r
131                         if (current_largest == 0)\r
132                         {\r
133                             current_largest = hours + minutes + seconds;\r
134                             title2Select = x;\r
135                         }\r
136                         else\r
137                         {\r
138                             if (total_sec > current_largest)\r
139                             {\r
140                                 current_largest = total_sec;\r
141                                 title2Select = x;\r
142                             }\r
143                         }\r
144                     }\r
145                 }\r
146             }\r
147             return title2Select;\r
148         }\r
149 \r
150         /// <summary>\r
151         /// Set's up the DataGridView on the Chapters tab (frmMain)\r
152         /// </summary>\r
153         /// <param name="mainWindow"></param>\r
154         public DataGridView chapterNaming(DataGridView data_chpt, string chapter_start, string chapter_end)\r
155         {\r
156             try\r
157             {\r
158                 int i = 0;\r
159                 int rowCount = 0;\r
160                 int start = 0;\r
161                 int finish = 0;\r
162                 if (chapter_end != "Auto")\r
163                     finish = int.Parse(chapter_end);\r
164 \r
165                 if (chapter_start != "Auto")\r
166                     start = int.Parse(chapter_start);\r
167 \r
168                 rowCount = finish - (start - 1);\r
169 \r
170                 while (i < rowCount)\r
171                 {\r
172                     DataGridViewRow row = new DataGridViewRow();\r
173 \r
174                     data_chpt.Rows.Insert(i, row);\r
175                     data_chpt.Rows[i].Cells[0].Value = (i + 1);\r
176                     data_chpt.Rows[i].Cells[1].Value = "Chapter " + (i + 1);\r
177                     i++;\r
178                 }\r
179                 return data_chpt;\r
180             }\r
181             catch (Exception exc)\r
182             {\r
183                 MessageBox.Show("chapterNaming() Error has occured: \n" + exc.ToString());\r
184                 return null;\r
185             }\r
186         }\r
187 \r
188         /// <summary>\r
189         /// Function which generates the filename and path automatically based on \r
190         /// the Source Name, DVD title and DVD Chapters\r
191         /// </summary>\r
192         /// <param name="mainWindow"></param>\r
193         public string autoName(ComboBox drp_dvdtitle, string chapter_start, string chatper_end, string source, string dest, int format)\r
194         {\r
195 \r
196             string AutoNamePath = string.Empty;\r
197 \r
198             if (drp_dvdtitle.Text != "Automatic")\r
199             {\r
200                 // Todo: This code is a tad messy. Clean it up at some point.\r
201                 // Get the Source Name\r
202                 string[] sourceName = source.Split('\\');\r
203                 source = sourceName[sourceName.Length - 1].Replace(".iso", "").Replace(".mpg", "").Replace(".ts", "").Replace(".ps", "");\r
204 \r
205                 // Get the Selected Title Number\r
206                 string title = drp_dvdtitle.Text;\r
207                 string[] titlesplit = title.Split(' ');\r
208                 title = titlesplit[0];\r
209 \r
210                 // Get the Chapter Start and Chapter End Numbers\r
211                 string cs = chapter_start;\r
212                 string cf = chatper_end;\r
213 \r
214                 // Just incase the above are set to their default Automatic values, set the varible to ""\r
215                 if (title == "Automatic")\r
216                     title = "";\r
217                 if (cs == "Auto")\r
218                     cs = "";\r
219                 if (cf == "Auto")\r
220                     cf = "";\r
221 \r
222                 // If both CS and CF are populated, set the dash varible\r
223                 string dash = "";\r
224                 if (cf != "Auto")\r
225                     dash = "-";\r
226 \r
227                 // Get the destination filename.\r
228                 string destination_filename = "";\r
229                 if (Properties.Settings.Default.autoNameFormat != "")\r
230                 {\r
231                     destination_filename = Properties.Settings.Default.autoNameFormat;\r
232                     destination_filename = destination_filename.Replace("{source}", source).Replace("{title}", title).Replace("{chapters}", cs + dash + cf);\r
233                 }\r
234                 else\r
235                     destination_filename = source + "_T" + title + "_C" + cs + dash + cf;\r
236 \r
237                 // If the text box is blank\r
238                 if (!dest.Contains("\\"))\r
239                 {\r
240                     string filePath = "";\r
241                     if (Properties.Settings.Default.autoNamePath.Trim() != "")\r
242                     {\r
243                         if (Properties.Settings.Default.autoNamePath.Trim() != "Click 'Browse' to set the default location")\r
244                             filePath = Properties.Settings.Default.autoNamePath + "\\";\r
245                     }\r
246 \r
247                     if (format == 0)\r
248                         AutoNamePath = filePath + destination_filename + ".mp4";\r
249                     else if (format == 1)\r
250                         AutoNamePath = filePath + destination_filename + ".m4v";\r
251                     else if (format == 2)\r
252                         AutoNamePath = filePath + destination_filename + ".mkv";\r
253                     else if (format == 3)\r
254                         AutoNamePath = filePath + destination_filename + ".avi";\r
255                     else if (format == 4)\r
256                         AutoNamePath = filePath + destination_filename + ".ogm";\r
257                 }\r
258                 else // If the text box already has a path and file\r
259                 {\r
260                     string destination = AutoNamePath;\r
261                     string[] destName = dest.Split('\\');\r
262                     string[] extension = dest.Split('.');\r
263                     string ext = extension[extension.Length - 1];\r
264 \r
265                     destName[destName.Length - 1] = destination_filename + "." + ext;\r
266 \r
267                     string fullDest = "";\r
268                     foreach (string part in destName)\r
269                     {\r
270                         if (fullDest != "")\r
271                             fullDest = fullDest + "\\" + part;\r
272                         else\r
273                             fullDest = fullDest + part;\r
274                     }\r
275                     return fullDest;\r
276                 }\r
277             }\r
278             return AutoNamePath;\r
279         }\r
280 \r
281         /// <summary>\r
282         /// Checks for updates and returns true if an update is available.\r
283         /// </summary>\r
284         /// <param name="debug">Turns on debug mode. Don't use on program startup</param>\r
285         /// <returns>Boolean True = Update available</returns>\r
286         public Boolean updateCheck(Boolean debug)\r
287         {\r
288             try\r
289             {\r
290                 Functions.AppcastReader rssRead = new Functions.AppcastReader();\r
291                 rssRead.getInfo(); // Initializes the class.\r
292                 string build = rssRead.build();\r
293 \r
294                 int latest = int.Parse(build);\r
295                 int current = Properties.Settings.Default.hb_build;\r
296                 int skip = Properties.Settings.Default.skipversion;\r
297 \r
298                 if (latest == skip)\r
299                     return false;\r
300                 else\r
301                 {\r
302                     Boolean update = (latest > current);\r
303                     return update;\r
304                 }\r
305             }\r
306             catch (Exception exc)\r
307             {\r
308                 if (debug == true)\r
309                     MessageBox.Show("Unable to check for updates, Please try again later. \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
310                 return false;\r
311             }\r
312         }\r
313 \r
314         /// <summary>\r
315         /// Get's HandBrakes version data from the CLI.\r
316         /// </summary>\r
317         /// <returns>Arraylist of Version Data. 0 = hb_version 1 = hb_build</returns>\r
318         public ArrayList getCliVersionData()\r
319         {\r
320             ArrayList cliVersionData = new ArrayList();\r
321             // 0 = SVN Build / Version\r
322             // 1 = Build Date\r
323 \r
324             Process cliProcess = new Process();\r
325             ProcessStartInfo handBrakeCLI = new ProcessStartInfo("HandBrakeCLI.exe", " -u");\r
326             handBrakeCLI.UseShellExecute = false;\r
327             handBrakeCLI.RedirectStandardError = true;\r
328             handBrakeCLI.RedirectStandardOutput = true;\r
329             handBrakeCLI.CreateNoWindow = true;\r
330             cliProcess.StartInfo = handBrakeCLI;\r
331             cliProcess.Start();\r
332 \r
333             // Retrieve standard output and report back to parent thread until the process is complete\r
334             String line;\r
335             TextReader stdOutput = cliProcess.StandardError;\r
336 \r
337             while (!cliProcess.HasExited)\r
338             {\r
339                 line = stdOutput.ReadLine();\r
340                 if (line == null) line = "";\r
341                 Match m = Regex.Match(line, @"HandBrake ([0-9\.]*)*(svn[0-9]*[M]*)* \([0-9]*\)");\r
342 \r
343                 if (m.Success != false)\r
344                 {\r
345                     string data = line.Replace("(", "").Replace(")", "").Replace("HandBrake ", "");\r
346                     string[] arr = data.Split(' ');\r
347                     cliVersionData.Add(arr[0]);\r
348                     cliVersionData.Add(arr[1]);\r
349                     return cliVersionData;\r
350                 }\r
351             }\r
352             return null;\r
353         }\r
354 \r
355         /// <summary>\r
356         /// Check if the queue recovery file contains records.\r
357         /// If it does, it means the last queue did not complete before HandBrake closed.\r
358         /// So, return a boolean if true. \r
359         /// </summary>\r
360         public Boolean check_queue_recovery()\r
361         {\r
362             try\r
363             {\r
364                 string tempPath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.dat");\r
365                 using (StreamReader reader = new StreamReader(tempPath))\r
366                 {\r
367                     string queue_item = reader.ReadLine();\r
368                     if (queue_item == null)\r
369                     {\r
370                         reader.Close();\r
371                         reader.Dispose();\r
372                         return false;\r
373                     }\r
374                     else // There exists an item in the recovery queue file, so try and recovr it.\r
375                     {\r
376                         reader.Close();\r
377                         reader.Dispose();\r
378                         return true;\r
379                     }\r
380                 }\r
381             }\r
382             catch (Exception)\r
383             {\r
384                 // Keep quiet about the error.\r
385                 return false;\r
386             }\r
387         }\r
388 \r
389     }\r
390 }