// -------------------------------------------------------------------------------------------------------------------- // // This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. // // // A Set of Static Utilites // // -------------------------------------------------------------------------------------------------------------------- namespace HandBrake.ApplicationServices.Utilities { using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Text; using System.Windows.Forms; using Caliburn.Micro; using HandBrake.ApplicationServices.Model; using HandBrake.ApplicationServices.Services; using HandBrake.ApplicationServices.Services.Interfaces; /// /// A Set of Static Utilites /// public class GeneralUtilities { #region Constants and Fields /// /// The Default Log Directory /// private static readonly string LogDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; /// /// The Instance ID /// private static int instanceId = 0; #endregion #region Properties /// /// Gets the number of HandBrake instances running. /// public static string GetInstanceCount { get { return instanceId == 0 ? string.Empty : instanceId.ToString(CultureInfo.InvariantCulture); } } /// /// Gets a value indicating whether HandBrake is running in multi instance mode /// /// True if the UI has another instance running public static bool IsMultiInstance { get { return Process.GetProcessesByName("HandBrake").Length > 1 ? true : false; } } #endregion #region Public Methods /// /// Clear all the log files older than 30 Days /// /// /// The Number of Days to Keep /// public static void ClearLogFiles(int daysToKeep) { if (Directory.Exists(LogDir)) { // Get all the log files var info = new DirectoryInfo(LogDir); FileInfo[] logFiles = info.GetFiles("*.txt"); // Delete Them foreach (FileInfo file in logFiles) { if (file.LastWriteTime < DateTime.Now.AddDays(-daysToKeep)) { if (!file.Name.Contains("last_scan_log.txt") && !file.Name.Contains("last_encode_log.txt")) { File.Delete(file.FullName); } else if (file.Length > 104857600) { File.Delete(file.FullName); } } } } } /// /// Add the CLI Query to the Log File. /// /// /// The create cli log header. /// public static StringBuilder CreateCliLogHeader() { var logHeader = new StringBuilder(); IUserSettingService userSettingService; try { userSettingService = IoC.Get(); } catch (Exception) { // TODO Sort this out. Should not be calling IoC.Get or creating a new instance here. userSettingService = new UserSettingService(); } logHeader.AppendLine( String.Format( "HandBrake {0} {1}", userSettingService.GetUserSetting(ASUserSettingConstants.HandBrakeVersion), userSettingService.GetUserSetting(ASUserSettingConstants.HandBrakeBuild))); logHeader.AppendLine(String.Format("OS: {0}", Environment.OSVersion)); logHeader.AppendLine(String.Format("CPU: {0}", SystemInfo.GetCpuCount)); logHeader.Append(String.Format("Ram: {0} MB, ", SystemInfo.TotalPhysicalMemory)); logHeader.AppendLine( String.Format( "Screen: {0}x{1}", SystemInfo.ScreenBounds.Bounds.Width, SystemInfo.ScreenBounds.Bounds.Height)); logHeader.AppendLine(String.Format("Temp Dir: {0}", Path.GetTempPath())); logHeader.AppendLine(String.Format("Install Dir: {0}", Application.StartupPath)); logHeader.AppendLine(String.Format("Data Dir: {0}\n", Application.UserAppDataPath)); logHeader.AppendLine("-------------------------------------------"); return logHeader; } /// /// Get the Process ID of HandBrakeCLI for the current instance. /// /// A list of processes public static Process[] GetCliProcess() { return Process.GetProcessesByName("HandBrakeCLI"); } /// /// Get a list of available DVD drives which are ready and contain DVD content. /// /// A List of Drives with their details public static List GetDrives() { var drives = new List(); DriveInfo[] theCollectionOfDrives = DriveInfo.GetDrives(); int id = 0; foreach (DriveInfo curDrive in theCollectionOfDrives) { if (curDrive.DriveType == DriveType.CDRom && curDrive.IsReady) { if (Directory.Exists(curDrive.RootDirectory + "VIDEO_TS") || Directory.Exists(curDrive.RootDirectory + "BDMV")) { drives.Add( new DriveInformation { Id = id, VolumeLabel = curDrive.VolumeLabel, RootDirectory = curDrive.RootDirectory.ToString() }); id++; } } } return drives; } /// /// Return the standard log format line of text for a given log message /// /// /// The Log Message /// /// /// A Log Message in the format: "[hh:mm:ss] message" /// public static string LogLine(string message) { return string.Format("[{0}] {1}", DateTime.Now.TimeOfDay, message); } /// /// Set the Instance ID /// public static void SetInstanceId() { instanceId = Process.GetProcessesByName("HandBrake").Length; } #endregion } }