public Source(string scanPath, List <Title> titles, string sourceName) { this.ScanPath = scanPath; this.Titles = titles; this.SourceName = sourceName; if (string.IsNullOrEmpty(sourceName)) { // Scan Path is a File. if (File.Exists(this.ScanPath)) { this.SourceName = Path.GetFileNameWithoutExtension(this.ScanPath); } // Scan Path is a folder. if (Directory.Exists(this.ScanPath)) { // Check to see if it's a Drive. If yes, use the volume label. foreach (DriveInformation item in DriveUtilities.GetDrives()) { if (item.RootDirectory.Contains(this.ScanPath.Replace("\\\\", "\\"))) { this.SourceName = item.VolumeLabel; } } // Otherwise, it may be a path of files. if (string.IsNullOrEmpty(this.SourceName)) { this.SourceName = Path.GetFileName(this.ScanPath); } } } }
public void StartQueue() { if (!this.QueueTasks.Any(a => a.Status == QueueItemStatus.Waiting || a.Status == QueueItemStatus.InProgress)) { this.errorService.ShowMessageBox( Resources.QueueViewModel_NoPendingJobs, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error); return; } var firstOrDefault = this.QueueTasks.FirstOrDefault(s => s.Status == QueueItemStatus.Waiting); if (firstOrDefault != null && !DriveUtilities.HasMinimumDiskSpace(firstOrDefault.Task.Destination, this.userSettingService.GetUserSetting <long>(UserSettingConstants.PauseQueueOnLowDiskspaceLevel))) { MessageBoxResult result = this.errorService.ShowMessageBox(Resources.Main_LowDiskspace, Resources.Warning, MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.No) { return; } } this.JobsPending = string.Format(Resources.QueueViewModel_JobsPending, this.queueProcessor.Count); this.IsQueueRunning = true; this.queueProcessor.Start(); }
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); if (e.Args.Contains("/format")) { var drive = e.Args[Array.IndexOf(e.Args, "/format") + 1]; if (drive.Length != 2) { Environment.Exit(100); } Environment.Exit((int)DriveUtilities.FormatDrive(drive, "FAT32", true, 8192, DriveLabel, false)); } new MainWindow().Show(); }
private bool CheckDiskSpace(QueueTask job) { if (this.userSettingService.GetUserSetting <bool>(UserSettingConstants.PauseOnLowDiskspace) && !DriveUtilities.HasMinimumDiskSpace(job.Task.Destination, this.userSettingService.GetUserSetting <long>(UserSettingConstants.PauseQueueOnLowDiskspaceLevel))) { this.logService.LogMessage(Resources.PauseOnLowDiskspace); job.Status = QueueItemStatus.Waiting; this.Pause(true); this.BackupQueue(string.Empty); return(true); // Don't start the next job. } return(false); }
private void ProcessNextJob() { QueueTask job = this.GetNextJobForProcessing(); if (job != null) { if (this.userSettingService.GetUserSetting <bool>(UserSettingConstants.PauseOnLowDiskspace) && !DriveUtilities.HasMinimumDiskSpace(job.Task.Destination, this.userSettingService.GetUserSetting <long>(UserSettingConstants.PauseQueueOnLowDiskspaceLevel))) { this.logService.LogMessage(Resources.PauseOnLowDiskspace); job.Status = QueueItemStatus.Waiting; this.Pause(); this.BackupQueue(string.Empty); return; // Don't start the next job. } job.Status = QueueItemStatus.InProgress; job.Statistics.StartTime = DateTime.Now; this.LastProcessedJob = job; this.IsProcessing = true; this.InvokeQueueChanged(EventArgs.Empty); this.InvokeJobProcessingStarted(new QueueProgressEventArgs(job)); if (!Directory.Exists(Path.GetDirectoryName(job.Task.Destination))) { this.EncodeServiceEncodeCompleted(null, new EncodeCompletedEventArgs(false, null, "Destination Directory Missing", null, null, null, 0)); this.BackupQueue(string.Empty); return; } this.EncodeService.Start(job.Task, job.Configuration, job.SelectedPresetKey); this.BackupQueue(string.Empty); } else { // No more jobs to process, so unsubscribe the event this.EncodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted; this.BackupQueue(string.Empty); // Fire the event to tell connected services. this.OnQueueCompleted(new QueueCompletedEventArgs(false)); } }
/// <summary> /// Run through all the jobs on the queue. /// </summary> private void ProcessNextJob() { QueueTask job = this.GetNextJobForProcessing(); if (job != null) { if (this.userSettingService.GetUserSetting <bool>(UserSettingConstants.PauseOnLowDiskspace) && !DriveUtilities.HasMinimumDiskSpace(job.Task.Destination, this.userSettingService.GetUserSetting <long>(UserSettingConstants.PauseOnLowDiskspaceLevel))) { LogService.GetLogger().LogMessage(Resources.PauseOnLowDiskspace, LogMessageType.ScanOrEncode, LogLevel.Info); job.Status = QueueItemStatus.Waiting; this.Pause(); return; // Don't start the next job. } this.IsProcessing = true; this.InvokeJobProcessingStarted(new QueueProgressEventArgs(job)); this.EncodeService.Start(job.Task, job.Configuration); } else { // No more jobs to process, so unsubscribe the event this.EncodeService.EncodeCompleted -= this.EncodeServiceEncodeCompleted; // Fire the event to tell connected services. this.OnQueueCompleted(new QueueCompletedEventArgs(false)); } }
private void CheckDiskSpaceForDirectory(string directory) { if (!string.IsNullOrEmpty(directory) && this.queueService.IsEncoding) { long lowLevel = this.userSettingService.GetUserSetting <long>(UserSettingConstants.PauseQueueOnLowDiskspaceLevel); if (!this.storageLowPause && this.userSettingService.GetUserSetting <bool>(UserSettingConstants.PauseOnLowDiskspace) && !DriveUtilities.HasMinimumDiskSpace(directory, lowLevel)) { this.log.LogMessage( string.Format( Resources.SystemService_LowDiskSpaceLog, lowLevel / 1000 / 1000 / 1000)); this.queueService.Pause(true); this.storageLowPause = true; } } }
private void StorageCheck() { string directory = this.encodeService.GetActiveJob()?.Destination; if (!string.IsNullOrEmpty(directory) && this.encodeService.IsEncoding) { long lowLevel = this.userSettingService.GetUserSetting <long>(UserSettingConstants.PauseEncodeOnLowDiskspaceLevel); if (!this.storageLowPause && this.userSettingService.GetUserSetting <bool>(UserSettingConstants.PauseOnLowDiskspace) && !DriveUtilities.HasMinimumDiskSpace(directory, lowLevel)) { LogService.GetLogger().LogMessage( string.Format( Resources.SystemService_LowDiskSpaceLog, lowLevel / 1000 / 1000 / 1000), LogMessageType.Application, LogLevel.Info); this.encodeService.Pause(); this.storageLowPause = true; } } }
private async Task <bool> DoYourStuff(ProcessViewModel processView) { await Task.Delay(500); //wait for the view to build up and subscribe to the logger event processView.Logger.Status("Check if drive is still plugged in..."); if (!UsbStickSettings.Drive.IsReady) { processView.Logger.Error("Drive is not ready. Please plug it in again"); return(false); } var stopwatch = Stopwatch.StartNew(); //progress: //10 % = install syslinux etc. //60 % = install systems //30 % = download & copy applications processView.Logger.Success("Drive plugged in"); if (UsbStickSettings.FormatDrive) { if (MessageBoxEx.Show(Application.Current.MainWindow, $"Warning: You selected the formatting option. Formatting will erease ALL data on the drive {UsbStickSettings.Drive.Name}. If you want to continue, press ok.", $"Format drive {UsbStickSettings.Drive.Name}", MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel) != MessageBoxResult.OK) { return(false); } processView.Message = "Format USB stick to FAT32"; processView.Logger.Status($"Formatting drive {UsbStickSettings.Drive.Name} with FAT32"); var driveLetter = UsbStickSettings.Drive.Name.Substring(0, 2); FormatResponse formatResponse; if (User.IsAdministrator) { formatResponse = await Task.Run( () => DriveUtilities.FormatDrive(driveLetter, "FAT32", true, 8192, App.DriveLabel, false)); } else { var process = new Process { StartInfo = { FileName = Assembly.GetExecutingAssembly().Location, Arguments = $"/format {driveLetter}", Verb = "runas" } }; if (!process.Start()) { processView.Logger.Error( "Was not able to restart application with administrator privileges to format the drive"); return(false); } await Task.Run(() => process.WaitForExit()); formatResponse = (FormatResponse)process.ExitCode; } if (formatResponse != FormatResponse.Success) { processView.Logger.Error("Formatting drive failed: " + formatResponse); return(false); } processView.Logger.Success("Successfully formatted drive"); } processView.Message = "Creating bootable stick"; if (UsbStickSettings.Drive.VolumeLabel != App.DriveLabel) { processView.Logger.Status("Change volume label to " + App.DriveLabel); UsbStickSettings.Drive.VolumeLabel = App.DriveLabel; } if (UsbStickSettings.Drive.DriveFormat != "FAT32") { processView.Logger.Warn( $"The drive {UsbStickSettings.Drive.RootDirectory.FullName} is formatted with {UsbStickSettings.Drive.DriveFormat} which may be incompatible with SysLinux (it is recommended to format it as FAT32)"); } var bootStickCreator = new BootStickCreator(UsbStickSettings.Drive, UsbStickSettings.SysLinuxAppearance); processView.CurrentProgress = 0.05; await Task.Run(() => bootStickCreator.CreateBootStick(processView.Logger)); processView.CurrentProgress = 0.1; var installSystemPercentage = UsbStickSettings.ApplicationInfo.Any(x => x.Add) ? 0.6 : 0.9; var systemSizes = UsbStickSettings.Systems.ToDictionary(x => x, y => new FileInfo(y.Filename).Length); long processedSize = 0; var totalSize = systemSizes.Sum(x => x.Value); bool?skipAllExistingSystems = null; for (int i = 0; i < UsbStickSettings.Systems.Count; i++) { var systemInfo = UsbStickSettings.Systems[i]; var progressReporter = new SystemProgressReporter(); progressReporter.ProgressChanged += (sender, d) => processView.CurrentProgress = 0.1 + (processedSize + systemSizes[systemInfo] * d) / totalSize * installSystemPercentage; progressReporter.MessageChanged += (sender, s) => { processView.Message = $"Install {systemInfo.Name} ({i + 1} / {UsbStickSettings.Systems.Count}): " + s; processView.Logger.Status(s); }; if (bootStickCreator.SysLinuxConfigFile.ContainsSystem(systemInfo)) { if (skipAllExistingSystems == true) { continue; } if (skipAllExistingSystems == null) { var directory = bootStickCreator.SysLinuxConfigFile.GetSystemDirectory(systemInfo); var newFileName = Path.GetFileNameWithoutExtension(systemInfo.Filename); if (directory == newFileName) { var applyForAll = false; if (MessageBoxChk.Show(Application.Current.MainWindow, $"The system \"{systemInfo.Name}\" already exists on the drive. Should it be overwritten (else skip)?", systemInfo.Name + " already exists", "Apply for all", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No, ref applyForAll) != MessageBoxResult.Yes) { if (applyForAll) { skipAllExistingSystems = true; } continue; } if (applyForAll) { skipAllExistingSystems = false; } } } await Task.Run(() => bootStickCreator.RemoveSystem(systemInfo)); } processView.Message = $"Install {systemInfo.Name} ({i + 1} / {UsbStickSettings.Systems.Count}): Add to config"; await Task.Run( () => bootStickCreator.AddSystemToBootStick(systemInfo, processView.Logger, progressReporter)); processView.Logger.Success("Added " + systemInfo.Name); processedSize += systemSizes[systemInfo]; } processView.CurrentProgress = 0.7; processView.Logger.Success("All systems installed"); var applicationsToInstall = UsbStickSettings.ApplicationInfo.Where(x => x.Add).ToList(); for (int i = 0; i < applicationsToInstall.Count; i++) { var application = applicationsToInstall[i]; processView.Message = $"Install {application.Name} ({i + 1} / {applicationsToInstall.Count}): Download"; var downloadUrl = await Task.Run(() => application.DownloadUrl.Value); processView.Logger.Status($"Download {downloadUrl}"); var tempFile = FileSystemExtensions.GetFreeTempFileName(application.Extension ?? new FileInfo(new Uri(downloadUrl).AbsolutePath).Extension.Remove(0, 1)); //remove the dot of the extension var webClient = new WebClient(); webClient.DownloadProgressChanged += (sender, args) => processView.CurrentProgress = 0.1 + 0.6 + (0.3 / applicationsToInstall.Count * i + 0.3 / applicationsToInstall.Count * (args.ProgressPercentage / 100d)); await webClient.DownloadFileTaskAsync(downloadUrl, tempFile); processView.Logger.Success("Application downloaded successfully"); processView.Logger.Status("Extract zip archive"); processView.Message = $"Install {application.Name} ({i + 1} / {applicationsToInstall.Count}): Extract"; var targetDirectory = new DirectoryInfo(Path.Combine(UsbStickSettings.Drive.RootDirectory.FullName, "Tools", EnumUtilities.GetDescription(application.ApplicationCategory), application.Name)); if (targetDirectory.Exists) { processView.Logger.Warn("Old application found, removing..."); FileSystemExtensions.DeleteDirectory(targetDirectory.FullName); } targetDirectory.Create(); using (var file = new SevenZipExtractor(tempFile)) using (var autoResetEvent = new AutoResetEvent(false)) { EventHandler <EventArgs> setHandler = (sender, args) => autoResetEvent.Set(); file.ExtractionFinished += setHandler; file.BeginExtractArchive(targetDirectory.FullName); await Task.Run(() => autoResetEvent.WaitOne()); file.ExtractionFinished -= setHandler; } File.Delete(tempFile); var entries = targetDirectory.GetFileSystemInfos(); if (entries.Length == 1 && (entries[0].Attributes & FileAttributes.Directory) == FileAttributes.Directory) { var directory = new DirectoryInfo(entries[0].FullName); await Task.Run(() => { directory.MoveTo(Path.Combine(directory.Parent.FullName, Guid.NewGuid().ToString("D"))); //in case that there's a directory with the same name foreach (var fileInfo in directory.GetFiles()) { fileInfo.MoveTo(Path.Combine(targetDirectory.FullName, fileInfo.Name)); } foreach (var directoryInfo in directory.GetDirectories()) { directoryInfo.MoveTo(Path.Combine(targetDirectory.FullName, directoryInfo.Name)); } directory.Delete(); }); } processView.Logger.Success("Application successfully installed"); } processView.CurrentProgress = 1; processView.Logger.Success($"Finished in {stopwatch.Elapsed.ToString("g")}"); return(true); }