private string ArchivePresetFile(string file, bool delete) { try { // Recover from Error. PresetVersion version = HandBrakePresetService.GetCurrentPresetVersion(); string archiveFile = string.Format("{0}.{1}_{2}_{3}.archive", file, version.Major, version.Minor, version.Micro); if (File.Exists(file)) { int counter = 0; while (File.Exists(archiveFile)) { counter = counter + 1; string appendedNumber = string.Format("({0})", counter); archiveFile = string.Format("{0}.{1}_{2}_{3} {4}.archive", file, version.Major, version.Minor, version.Micro, appendedNumber); } File.Copy(file, archiveFile); if (delete) { File.Delete(file); } } return(archiveFile); } catch (IOException e) { this.ServiceLogMessage(e.ToString()); } return("Sorry, the archiving failed."); }
public void UpdateBuiltInPresets() { // Clear the current built in Presets and now parse the temporary Presets file. this.ClearBuiltIn(); IList <HBPresetCategory> presetCategories = HandBrakePresetService.GetBuiltInPresets(); foreach (var category in presetCategories) { foreach (var hbpreset in category.ChildrenArray) { Preset preset = JsonPresetFactory.ImportPreset(hbpreset); preset.IsBuildIn = true; preset.Category = category.PresetName; preset.Task.AllowedPassthruOptions = new AllowedPassthru(true); // We don't want to override the built-in preset preset.IsPresetDisabled = this.IsPresetDisabled(preset) || hbpreset.PresetDisabled; this.Add(preset, true); } } // Verify we have presets. if (this.presets.Count == 0) { this.ServiceLogMessage("Failed to load built-in presets."); throw new GeneralApplicationException("Failed to load built-in presets.", "Restarting HandBrake may resolve this issue", null); } // Store the changes to disk this.SavePresetFiles(); }
/// <summary> /// The export. /// </summary> /// <param name="filename"> /// The filename. /// </param> /// <param name="preset"> /// The preset. /// </param> /// <param name="configuration"> /// The configuration. /// </param> public void Export(string filename, Preset preset, HBConfiguration configuration) { // TODO Add support for multiple export PresetTransportContainer container = JsonPresetFactory.ExportPreset(preset, configuration); HandBrakePresetService.ExportPreset(filename, container); }
public void Export(string filename, string presetName) { Preset foundPreset = this.flatPresetList.FirstOrDefault(s => s.Name == presetName); if (foundPreset != null) { PresetTransportContainer container = JsonPresetFactory.ExportPreset(foundPreset); HandBrakePresetService.ExportPreset(filename, container); } }
/// <summary> /// The export preset. /// </summary> /// <param name="export"> /// The export. /// </param> /// <param name="config"> /// HandBrakes configuration options. /// </param> /// <returns> /// The <see cref="Preset"/>. /// </returns> public static PresetTransportContainer ExportPreset(Preset export, HBConfiguration config) { PresetVersion presetVersion = HandBrakePresetService.GetCurrentPresetVersion(); PresetTransportContainer container = new PresetTransportContainer(presetVersion.Major, presetVersion.Minor, presetVersion.Micro); container.PresetList = new List <object> { CreateHbPreset(export, config) }; return(container); }
/// <summary> /// The import. /// </summary> /// <param name="filename"> /// The filename. /// </param> public void Import(string filename) { if (!string.IsNullOrEmpty(filename)) { PresetTransportContainer container = null; try { container = HandBrakePresetService.GetPresetFromFile(filename); } catch (Exception exc) { this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, exc); return; } if (container?.PresetList == null || container.PresetList.Count == 0) { this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, Resources.NoAdditionalInformation); return; } // HBPreset Handling if (container.PresetList != null) { foreach (var objectPreset in container.PresetList) { PresetCategory category = JsonConvert.DeserializeObject <PresetCategory>(objectPreset.ToString()); if (category != null && category.ChildrenArray != null && category.ChildrenArray.Count > 0) { foreach (HBPreset hbPreset in category.ChildrenArray) { Preset preset = this.ConvertHbPreset(hbPreset); if (preset != null) { this.AddOrUpdateImportedPreset(preset); } } } else { HBPreset hbPreset = JsonConvert.DeserializeObject <HBPreset>(objectPreset.ToString()); if (hbPreset != null) { Preset preset = this.ConvertHbPreset(hbPreset); if (preset != null) { this.AddOrUpdateImportedPreset(preset); } } } } } } }
/// <summary> /// Export a list of Presets. /// </summary> /// <param name="exportList">A list of presets to export</param> /// <param name="config">HB's configuration</param> /// <returns>A list of JSON object presets.</returns> public static PresetTransportContainer ExportPresets(IEnumerable <Preset> exportList, HBConfiguration config) { PresetVersion presetVersion = HandBrakePresetService.GetCurrentPresetVersion(); PresetTransportContainer container = new PresetTransportContainer(presetVersion.Major, presetVersion.Minor, presetVersion.Micro); List <HBPreset> presets = exportList.Select(item => CreateHbPreset(item, config)).ToList(); container.PresetList = new List <object>(); container.PresetList.AddRange(presets); return(container); }
private void SavePresetFiles() { try { // Verify Directories. string directory = Path.GetDirectoryName(this.presetFile); if (directory != null && !Directory.Exists(directory)) { Directory.CreateDirectory(directory); } // Organise the Presets list into Json Equivalent objects. Dictionary <string, HBPresetCategory> presetCategories = new Dictionary <string, HBPresetCategory>(); List <HBPreset> uncategorisedPresets = new List <HBPreset>(); // Handle User Presets. this.HandlePresetListsForSave(this.flatPresetList.Where(o => !o.IsBuildIn).ToList(), presetCategories, uncategorisedPresets); // Handle Built-in Presets this.HandlePresetListsForSave(this.flatPresetList.Where(o => o.IsBuildIn).ToList(), presetCategories, uncategorisedPresets); // Wrap the categories in a container. JsonSerializerSettings settings = new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore }; PresetVersion presetVersion = HandBrakePresetService.GetCurrentPresetVersion(); PresetTransportContainer container = new PresetTransportContainer(presetVersion.Major, presetVersion.Minor, presetVersion.Micro) { PresetList = new List <object>() }; container.PresetList.AddRange(presetCategories.Values); container.PresetList.AddRange(uncategorisedPresets); // Write the preset container out to file. using (FileStream strm = new FileStream(this.presetFile, FileMode.Create, FileAccess.Write)) { string presetsJson = JsonConvert.SerializeObject(container, Formatting.Indented, settings); using (StreamWriter writer = new StreamWriter(strm)) { writer.WriteLine(presetsJson); } } } catch (Exception exc) { throw new GeneralApplicationException("Unable to write to the presets file.", "The details section below may indicate why this error has occurred.", exc); } }
public static PresetTransportContainer ExportPresetCategories(IList <PresetDisplayCategory> categories, HBConfiguration config) { PresetVersion presetVersion = HandBrakePresetService.GetCurrentPresetVersion(); PresetTransportContainer container = new PresetTransportContainer(presetVersion.Major, presetVersion.Minor, presetVersion.Micro); List <object> presets = new List <object>(); foreach (var category in categories) { presets.Add(CreatePresetCategory(category, config)); } container.PresetList = presets; return(container); }
/// <summary> /// Reads the CLI's CLI output format and load's them into the preset List Preset /// </summary> public void UpdateBuiltInPresets() { // Clear the current built in Presets and now parse the tempory Presets file. this.ClearBuiltIn(); IList <PresetCategory> presetCategories = HandBrakePresetService.GetBuiltInPresets(); foreach (var item in presetCategories) { foreach (var hbpreset in item.ChildrenArray) { Preset preset = JsonPresetFactory.ImportPreset(hbpreset); preset.Version = VersionHelper.GetVersion(); preset.UsePictureFilters = true; preset.IsBuildIn = true; // Older versions did not have this flag so explicitly make sure it is set. preset.Category = item.PresetName; if (preset.Name == "iPod") { preset.Task.KeepDisplayAspect = true; } preset.Task.AllowedPassthruOptions = new AllowedPassthru(true); // We don't want to override the built-in preset if (preset.Name == "Normal") { preset.IsDefault = true; } this.presets.Add(preset); } } // Verify we have presets. if (this.presets.Count == 0) { throw new GeneralApplicationException("Failed to load built-in presets.", "Restarting HandBrake may resolve this issue", null); } // Store the changes to disk this.UpdatePresetFiles(); }
public void UpdateBuiltInPresets() { // Clear the current built in Presets and now parse the temporary Presets file. this.ClearBuiltIn(); bool hasUserDefault = this.flatPresetDict.Values.FirstOrDefault(f => f.IsDefault) != null; IList <HBPresetCategory> presetCategories = HandBrakePresetService.GetBuiltInPresets(); foreach (var category in presetCategories) { foreach (var hbpreset in category.ChildrenArray) { Preset preset = JsonPresetFactory.ImportPreset(hbpreset); preset.IsBuildIn = true; preset.Category = category.PresetName; preset.IsPresetDisabled = this.IsPresetDisabled(preset) || hbpreset.PresetDisabled; if (hbpreset.Default && hasUserDefault) { preset.IsDefault = false; } this.Add(preset, true); } } // Verify we have presets. if (this.presets.Count == 0) { this.ServiceLogMessage("Failed to load built-in presets."); throw new GeneralApplicationException("Failed to load built-in presets.", "Restarting HandBrake may resolve this issue", null); } // Store the changes to disk this.SavePresetFiles(); }
private void LoadPresets() { // First clear the Presets arraylists this.presets.Clear(); // Load the presets file. try { // If we don't have a presets file. Create one for first load. if (!File.Exists(this.presetFile)) { this.UpdateBuiltInPresets(); return; } // Otherwise, we already have a file, so lets try load it. PresetTransportContainer container = null; try { container = HandBrakePresetService.GetPresetsFromFile(this.presetFile); } catch (Exception exc) { this.ServiceLogMessage("Corrupted Presets File Detected: " + Environment.NewLine + exc); } // Sanity Check. Did the container deserialize. if (container?.PresetList == null) { this.ServiceLogMessage("Attempting Preset Recovery ..."); string filename = this.ArchivePresetFile(this.presetFile, true); this.errorService.ShowMessageBox( Resources.PresetService_UnableToLoadPresets + filename, Resources.PresetService_UnableToLoad, MessageBoxButton.OK, MessageBoxImage.Exclamation); this.UpdateBuiltInPresets(); this.ServiceLogMessage("Recovery Completed!"); return; // Update built-in presets stores the presets locally, so just return. } // Force Upgrade of presets if (this.userSettingService.GetUserSetting <int>(UserSettingConstants.ForcePresetReset) < ForcePresetReset) { this.userSettingService.SetUserSetting(UserSettingConstants.ForcePresetReset, ForcePresetReset); string fileName = this.ArchivePresetFile(this.presetFile, true); this.errorService.ShowMessageBox( Resources.Presets_PresetForceReset + Environment.NewLine + Environment.NewLine + Resources.PresetService_ArchiveFile + fileName, Resources.PresetService_UnableToLoad, MessageBoxButton.OK, MessageBoxImage.Information); this.UpdateBuiltInPresets(); // Update built-in presets stores the presets locally, so just return. return; } this.ProcessPresetList(container); PresetVersion presetVersion = HandBrakePresetService.GetCurrentPresetVersion(); if (container.VersionMajor != presetVersion.Major || container.VersionMinor != presetVersion.Minor || container.VersionMicro != presetVersion.Micro) { this.UpdateBuiltInPresets(); } CheckAndSetDefault(); // Make a preset default if one we have none. } catch (Exception ex) { this.ServiceLogMessage(ex.ToString()); this.ArchivePresetFile(this.presetFile, true); this.UpdateBuiltInPresets(); } }
public PresetsService() { List <Preset> userPresets = PresetStorage.UserPresets; this.presetFolders = PresetFolderStorage.PresetFolders; this.collapsedBuiltInFolders = CustomConfig.CollapsedBuiltInFolders; var unmodifiedPresets = userPresets.Where(preset => !preset.IsModified); Preset modifiedPreset = userPresets.FirstOrDefault(preset => preset.IsModified); this.allPresets = new ObservableCollection <PresetViewModel>(); int modifiedPresetIndex = -1; int defaultPresetIndex = 0; foreach (Preset userPreset in unmodifiedPresets) { PresetViewModel presetVM; if (modifiedPreset != null && modifiedPreset.Name == userPreset.Name) { modifiedPresetIndex = this.allPresets.Count; presetVM = new PresetViewModel(modifiedPreset); presetVM.OriginalProfile = userPreset.EncodingProfile; } else { presetVM = new PresetViewModel(userPreset); } this.allPresets.Add(presetVM); } // Populate the custom preset folder before built-in presets are added to AllPresets collection. this.customPresetFolder = new PresetFolderViewModel(this, !this.collapsedBuiltInFolders.Contains(CustomFolderKey)) { Name = EncodingRes.PresetFolder_Custom, Id = 0, IsBuiltIn = false }; this.PopulateCustomFolder(this.customPresetFolder); // Populate built-in folder from HandBrake presets IList <PresetCategory> handBrakePresets = HandBrakePresetService.GetBuiltInPresets(); this.builtInFolder = new PresetFolderViewModel(this, !this.collapsedBuiltInFolders.Contains(BuiltInFolderKey)) { Name = EncodingRes.PresetFolder_BuiltIn, IsBuiltIn = true }; foreach (PresetCategory handbrakePresetCategory in handBrakePresets) { var builtInSubfolder = new PresetFolderViewModel(this, !this.collapsedBuiltInFolders.Contains(handbrakePresetCategory.PresetName)) { Name = handbrakePresetCategory.PresetName, IsBuiltIn = true, }; this.builtInFolder.AddSubfolder(builtInSubfolder); foreach (HBPreset handbrakePreset in handbrakePresetCategory.ChildrenArray) { if (handbrakePreset.Default) { defaultPresetIndex = this.allPresets.Count; } Preset builtInPreset = PresetConverter.ConvertHandBrakePresetToVC(handbrakePreset); PresetViewModel builtInPresetViewModel = new PresetViewModel(builtInPreset); this.allPresets.Add(builtInPresetViewModel); builtInSubfolder.AddItem(builtInPresetViewModel); } } this.allPresetsTree = new ObservableCollection <PresetFolderViewModel>(); if (this.customPresetFolder.Items.Count > 0 || this.customPresetFolder.SubFolders.Count > 0) { this.AllPresetsTree.Add(this.customPresetFolder); } this.AllPresetsTree.Add(this.builtInFolder); // Always select the modified preset if it exists. // Otherwise, choose the last selected preset. int presetIndex; if (modifiedPresetIndex >= 0) { presetIndex = modifiedPresetIndex; } else { int lastPresetIndex = Config.LastPresetIndex; if (lastPresetIndex >= 0) { presetIndex = lastPresetIndex; } else { presetIndex = defaultPresetIndex; } } if (presetIndex >= this.allPresets.Count) { presetIndex = 0; } this.SelectedPreset = this.allPresets[presetIndex]; }
public void ExportCategories(string filename, IList <PresetDisplayCategory> categories, HBConfiguration configuration) { PresetTransportContainer container = JsonPresetFactory.ExportPresetCategories(categories, configuration); HandBrakePresetService.ExportPreset(filename, container); }
public void Import(string filename) { if (!string.IsNullOrEmpty(filename)) { PresetTransportContainer container; try { container = HandBrakePresetService.GetPresetsFromFile(filename); } catch (Exception exc) { this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, exc); return; } if (container?.PresetList == null || container.PresetList.Count == 0) { this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, Resources.NoAdditionalInformation); return; } // HBPreset Handling if (container.PresetList != null) { bool containsBuildInPreset = false; foreach (var objectPreset in container.PresetList) { HBPresetCategory category = JsonSerializer.Deserialize <HBPresetCategory>(objectPreset.ToString(), JsonSettings.Options); if (category != null && category.ChildrenArray != null && category.ChildrenArray.Count > 0) { foreach (HBPreset hbPreset in category.ChildrenArray) { Preset preset = this.ConvertHbPreset(hbPreset, category.PresetName); preset.IsPresetDisabled = this.IsPresetDisabled(preset); preset.IsDefault = false; // When importing, force the user to reset default manually. This prevents conflicts. if (!preset.IsBuildIn) { this.AddOrUpdateImportedPreset(preset); } else { containsBuildInPreset = true; } } } else { HBPreset hbPreset = JsonSerializer.Deserialize <HBPreset>(objectPreset.ToString(), JsonSettings.Options); if (hbPreset != null) { Preset preset = this.ConvertHbPreset(hbPreset, null); preset.IsDefault = false; // When importing, force the user to reset default manually. This prevents conflicts. preset.IsPresetDisabled = this.IsPresetDisabled(preset); if (!preset.IsBuildIn) { this.AddOrUpdateImportedPreset(preset); } else { containsBuildInPreset = true; } } } } if (containsBuildInPreset) { this.errorService.ShowMessageBox( Properties.Resources.PresetService_ImportingBuiltInWarning, Properties.Resources.Warning, MessageBoxButton.OK, MessageBoxImage.Warning); } } } }
/// <summary> /// The import. /// </summary> /// <param name="filename"> /// The filename. /// </param> public void Import(string filename) { if (!string.IsNullOrEmpty(filename)) { PresetTransportContainer container = HandBrakePresetService.GetPresetFromFile(filename); if (container?.PresetList == null || container.PresetList.Count == 0) { this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, string.Empty); return; } // HBPreset Handling if (container.PresetList != null) { foreach (var objectPreset in container.PresetList) { HBPreset hbPreset = JsonConvert.DeserializeObject <HBPreset>(objectPreset.ToString()); Preset preset = null; try { preset = JsonPresetFactory.ImportPreset(hbPreset); preset.Category = UserPresetCatgoryName; // IF we are using Source Max, Set the Max Width / Height values. if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum) { preset.Task.MaxWidth = preset.Task.Height; preset.Task.MaxHeight = preset.Task.Width; } } catch (Exception exc) { this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, exc); } if (preset == null) { this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, string.Empty); return; } if (this.CheckIfPresetExists(preset.Name)) { if (!this.CanUpdatePreset(preset.Name)) { MessageBox.Show(Resources.Main_PresetErrorBuiltInName, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error); return; } MessageBoxResult result = MessageBox.Show(Resources.Main_PresetOverwriteWarning, Resources.Overwrite, MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.Yes) { this.Update(preset); } } else { this.Add(preset); } } } // Category Handling. // TODO maybe for a future release. } }
/// <summary> /// The import. /// </summary> /// <param name="filename"> /// The filename. /// </param> public void Import(string filename) { // TODO needs a tidy up but will do for now. if (!string.IsNullOrEmpty(filename)) { PresetTransportContainer container = HandBrakePresetService.GetPresetFromFile(filename); if (container == null || container.PresetList == null || container.PresetList.Count == 0) { this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, string.Empty); return; } // HBPreset Handling IList <HBPreset> hbPresets = container.PresetList as IList <HBPreset>; if (hbPresets != null) { foreach (var hbPreset in hbPresets) { Preset preset = null; try { preset = JsonPresetFactory.ImportPreset(hbPreset); preset.Category = UserPresetCatgoryName; // IF we are using Source Max, Set the Max Width / Height values. if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum) { preset.Task.MaxWidth = preset.Task.Height; preset.Task.MaxHeight = preset.Task.Width; } } catch (Exception exc) { this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, exc); } if (preset == null) { this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, string.Empty); return; } // TODO Better version checking. if (this.CheckIfPresetExists(preset.Name)) { if (!CanUpdatePreset(preset.Name)) { MessageBox.Show(Resources.Main_PresetErrorBuiltInName, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error); return; } MessageBoxResult result = MessageBox.Show(Resources.Main_PresetOverwriteWarning, Resources.Overwrite, MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.Yes) { Update(preset); } } else { Add(preset); } } } // Category Handling. } }